diff --git a/qt/aqt/browser/browser.py b/qt/aqt/browser/browser.py index 72eb654dd..47eb23bb6 100644 --- a/qt/aqt/browser/browser.py +++ b/qt/aqt/browser/browser.py @@ -382,7 +382,7 @@ class Browser(QMainWindow): editor._links["preview"] = lambda _editor: self.onTogglePreview() editor.web.eval( - "$editorToolbar.then(({ notetypeButtons }) => notetypeButtons.appendButton({ component: editorToolbar.PreviewButton }));" + "$editorToolbar.then(({ notetypeButtons }) => notetypeButtons.appendButton({ component: editorToolbar.PreviewButton, id: 'preview' }));" ) gui_hooks.editor_did_init.append(add_preview_button) diff --git a/ts/components/registration.ts b/ts/components/registration.ts index 8faf67be1..06f03c009 100644 --- a/ts/components/registration.ts +++ b/ts/components/registration.ts @@ -8,7 +8,7 @@ import { find } from "./identifier"; export interface SvelteComponent { component: SvelteComponentTyped; - id: string | undefined; + id: string; props: Record | undefined; } @@ -32,6 +32,10 @@ export interface DynamicRegistrationAPI { ) => void; } +export function nodeIsElement(node: Node): node is Element { + return node.nodeType === Node.ELEMENT_NODE; +} + export function makeInterface(makeRegistration: () => T): RegistrationAPI { const registrations: T[] = []; const items = writable(registrations); @@ -60,18 +64,21 @@ export function makeInterface(makeRegistration: () => T): RegistrationAPI observer: MutationObserver ): void => { for (const mutation of mutations) { - const addedNode = mutation.addedNodes[0]; + for (const addedNode of mutation.addedNodes) { + if ( + nodeIsElement(addedNode) && + (!component.id || addedNode.id === component.id) + ) { + const index = add(addedNode, elementRef); - if (addedNode.nodeType === Node.ELEMENT_NODE) { - const index = add(addedNode as Element, elementRef); + if (index >= 0) { + registerComponent(index, registration); + } - if (index >= 0) { - registerComponent(index, registration); + return observer.disconnect(); } } } - - observer.disconnect(); }; const observer = new MutationObserver(callback);