Check for id when inserting dynamic elements
* I'm not sure it behaved, if there were multiple elements inserted by different add-ons into the same container
This commit is contained in:
parent
cf38cb334e
commit
8b0c6ba5df
@ -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)
|
||||
|
@ -8,7 +8,7 @@ import { find } from "./identifier";
|
||||
|
||||
export interface SvelteComponent {
|
||||
component: SvelteComponentTyped;
|
||||
id: string | undefined;
|
||||
id: string;
|
||||
props: Record<string, unknown> | undefined;
|
||||
}
|
||||
|
||||
@ -32,6 +32,10 @@ export interface DynamicRegistrationAPI<T> {
|
||||
) => void;
|
||||
}
|
||||
|
||||
export function nodeIsElement(node: Node): node is Element {
|
||||
return node.nodeType === Node.ELEMENT_NODE;
|
||||
}
|
||||
|
||||
export function makeInterface<T>(makeRegistration: () => T): RegistrationAPI<T> {
|
||||
const registrations: T[] = [];
|
||||
const items = writable(registrations);
|
||||
@ -60,18 +64,21 @@ export function makeInterface<T>(makeRegistration: () => T): RegistrationAPI<T>
|
||||
observer: MutationObserver
|
||||
): void => {
|
||||
for (const mutation of mutations) {
|
||||
const addedNode = mutation.addedNodes[0];
|
||||
|
||||
if (addedNode.nodeType === Node.ELEMENT_NODE) {
|
||||
const index = add(addedNode as Element, elementRef);
|
||||
for (const addedNode of mutation.addedNodes) {
|
||||
if (
|
||||
nodeIsElement(addedNode) &&
|
||||
(!component.id || addedNode.id === component.id)
|
||||
) {
|
||||
const index = add(addedNode, elementRef);
|
||||
|
||||
if (index >= 0) {
|
||||
registerComponent(index, registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
observer.disconnect();
|
||||
return observer.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const observer = new MutationObserver(callback);
|
||||
|
Loading…
Reference in New Issue
Block a user