Skip to content
Open
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
Copy Markdown
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
Copy Markdown
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