diff --git a/assets/js/hooks/js_view.js b/assets/js/hooks/js_view.js index 96053e9d4..660b40818 100644 --- a/assets/js/hooks/js_view.js +++ b/assets/js/hooks/js_view.js @@ -226,17 +226,18 @@ const JSView = { resizeObserver.observe(notebookContentEl); resizeObserver.observe(notebookEl); - // On lower level cell/section reordering is applied as element - // removal followed by insert, consequently the intersection - // between the placeholder and notebook content changes (becomes - // none for a brief moment) - const intersectionObserver = new IntersectionObserver( - (entries) => { - this.repositionIframe(); - }, - { root: notebookContentEl } + // On certain events, like section/cell moved, a global event is + // dispatched to trigger reposition. This way we don't need to + // use deep MutationObserver, which would be expensive, especially + // with code editor + const unsubscribeFromJSViewsEvents = globalPubSub.subscribe( + "js_views", + (event) => { + if (event.type === "reposition") { + this.repositionIframe(); + } + } ); - intersectionObserver.observe(this.iframePlaceholder); // Emulate mouse enter and leave on the placeholder. Note that we // intentionally use bubbling to notify all parents that may have @@ -277,7 +278,7 @@ const JSView = { const remove = () => { resizeObserver.disconnect(); - intersectionObserver.disconnect(); + unsubscribeFromJSViewsEvents(); viewportIntersectionObserver && viewportIntersectionObserver.disconnect(); this.iframe.remove(); this.iframePlaceholder.remove(); diff --git a/assets/js/hooks/session.js b/assets/js/hooks/session.js index 4f4236fb7..e1c92f36c 100644 --- a/assets/js/hooks/session.js +++ b/assets/js/hooks/session.js @@ -1054,6 +1054,8 @@ const Session = { }, handleCellMoved(cellId) { + this.repositionJSViews(); + if (this.focusedId === cellId) { globalPubSub.broadcast("cells", { type: "cell_moved", cellId }); } @@ -1076,6 +1078,8 @@ const Session = { }, handleSectionMoved(sectionId) { + this.repositionJSViews(); + const section = this.getSectionById(sectionId); smoothlyScrollToElement(section); }, @@ -1154,6 +1158,10 @@ const Session = { } }, + repositionJSViews() { + globalPubSub.broadcast("js_views", { type: "reposition" }); + }, + /** * Broadcast new location report coming from the server to all the cells. */