Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new scroll to demo logic #13

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
37 changes: 36 additions & 1 deletion components/EditorOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,22 @@ const { $track } = useNuxtApp();
function playDemoClicked(){
demoEnabled.value = true;

demoCanvas.value?.scrollIntoViewIfNeeded();

/**
* Listen to DOM Mutations and wait for editorjs element to be inserted
* then scroll to it. We have to wait for the editorjs element to be inserted
* otherwise the scroll will not work
*/
const onDomChange=()=>{
if(document.getElementById('editorjs')){
smoothScrollToCenter(demoCanvas.value)
window.removeEventListener('DOMNodeInserted',onDomChange)
}
}

window.addEventListener('DOMNodeInserted',onDomChange)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to bind this event inside the onMounted hook to prevent SSR issues

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it, I placed mobile focus part inside setTimeout because it solves to problems

  1. Virtual keyboard interrupts scroll
  2. Editor is focused before it's initialised




const isMobile = window.matchMedia('(max-width: 710px)').matches;

Expand All @@ -93,6 +108,26 @@ function playDemoClicked(){
*/
$track(AnalyticEvent.PlayWithDemoClicked)
}

/**
* Scrolls to targetEle
*/
function smoothScrollToCenter(targetEle:HTMLElement|null) {

if (!targetEle) {
return;
}

const targetPosition = targetEle.getBoundingClientRect().top + window.scrollY;
const screenHeight = window.innerHeight;
const targetDivHeight=targetEle.getBoundingClientRect().height
const targetOffset = targetPosition - (screenHeight / 2)+(targetDivHeight/2);

window.scrollTo({
top: targetOffset,
behavior: 'smooth'
});
}
</script>

<style lang="postcss">
Expand Down