diff --git a/edit/sections-editor-section.js b/edit/sections-editor-section.js index d69d8f3c..8885cfc6 100644 --- a/edit/sections-editor-section.js +++ b/edit/sections-editor-section.js @@ -94,6 +94,7 @@ function createSection({ const cm = cmFactory.create(wrapper => { el.insertBefore(wrapper, $('.code-label', el).nextSibling); }, {value: originalSection.code}); + el.CodeMirror = cm; // used by getAssociatedEditor const changeListeners = new Set(); diff --git a/edit/sections-editor.js b/edit/sections-editor.js index c4b7cab3..6cd09104 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -154,9 +154,7 @@ function createSectionsEditor({style, onTitleChanged}) { function closestVisible(nearbyElement) { const cm = nearbyElement instanceof CodeMirror ? nearbyElement : - nearbyElement instanceof Node && - (nearbyElement.closest('#sections > .section') || {}).CodeMirror || - getLastActivatedEditor(); + nearbyElement instanceof Node && getAssociatedEditor(nearbyElement) || getLastActivatedEditor(); if (nearbyElement instanceof Node && cm) { const {left, top} = nearbyElement.getBoundingClientRect(); const bounds = cm.display.wrapper.getBoundingClientRect(); @@ -228,6 +226,15 @@ function createSectionsEditor({style, onTitleChanged}) { } } + function getAssociatedEditor(nearbyElement) { + for (let el = nearbyElement; el; el = el.parentElement) { + // added by createSection + if (el.CodeMirror) { + return el.CodeMirror; + } + } + } + function getEditors() { return sections.filter(s => !s.isRemoved()).map(s => s.cm); }