From 3e88b8d8d6307bd7550f49a56ff93dae87cb4e90 Mon Sep 17 00:00:00 2001 From: Vikram Reddy Date: Tue, 15 Aug 2023 18:07:59 +0530 Subject: [PATCH 01/13] added new scroll to demo logic --- components/EditorOverview.vue | 43 ++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/components/EditorOverview.vue b/components/EditorOverview.vue index 2426401..e4980c5 100644 --- a/components/EditorOverview.vue +++ b/components/EditorOverview.vue @@ -56,6 +56,12 @@ function pictureClicked() { */ const demoEnabled = ref(false); + +/** + * Dom mutation listener used to scroll to the demo + */ +const listener = ref(); + /** * Access the Analytics module */ @@ -67,7 +73,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 + */ + listener.value=()=>{ + if(document.getElementById('editorjs')){ + smoothScrollToCenter(demoCanvas.value) + window.removeEventListener('DOMNodeInserted',listener.value) + } + } + + window.addEventListener('DOMNodeInserted',listener.value) + + const isMobile = window.matchMedia('(max-width: 710px)').matches; @@ -93,6 +114,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' + }); +}