2021-10-18 14:01:15 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import type { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
import { on } from "../lib/events";
|
2022-02-04 09:36:34 +01:00
|
|
|
import storeSubscribe from "./store-subscribe";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
attributes: true,
|
|
|
|
characterData: true,
|
|
|
|
};
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export type MirrorAction = (
|
2022-01-11 23:39:41 +01:00
|
|
|
element: HTMLElement,
|
2022-01-08 02:46:01 +01:00
|
|
|
params: { store: Writable<DocumentFragment> },
|
|
|
|
) => { destroy(): void };
|
|
|
|
|
|
|
|
interface DOMMirrorAPI {
|
|
|
|
mirror: MirrorAction;
|
2021-10-18 14:01:15 +02:00
|
|
|
preventResubscription(): () => void;
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function cloneNode(node: Node): DocumentFragment {
|
|
|
|
/**
|
|
|
|
* Creates a deep clone
|
|
|
|
* This seems to be less buggy than node.cloneNode(true)
|
|
|
|
*/
|
|
|
|
const range = document.createRange();
|
|
|
|
|
|
|
|
range.selectNodeContents(node);
|
|
|
|
return range.cloneContents();
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
/**
|
|
|
|
* Allows you to keep an element's inner HTML bidirectionally
|
|
|
|
* in sync with a store containing a DocumentFragment.
|
|
|
|
* While the element has focus, this connection is tethered.
|
2022-01-11 23:39:41 +01:00
|
|
|
* In practice, this will sync changes from PlainTextInput to RichTextInput.
|
2022-01-08 02:46:01 +01:00
|
|
|
*/
|
|
|
|
function getDOMMirror(): DOMMirrorAPI {
|
2021-10-18 14:01:15 +02:00
|
|
|
const allowResubscription = writable(true);
|
|
|
|
|
|
|
|
function preventResubscription() {
|
|
|
|
allowResubscription.set(false);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
allowResubscription.set(true);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mirror(
|
2022-01-11 23:39:41 +01:00
|
|
|
element: HTMLElement,
|
2021-10-19 01:06:00 +02:00
|
|
|
{ store }: { store: Writable<DocumentFragment> },
|
2021-10-18 14:01:15 +02:00
|
|
|
): { destroy(): void } {
|
|
|
|
function saveHTMLToStore(): void {
|
2022-01-11 23:39:41 +01:00
|
|
|
store.set(cloneNode(element));
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver(saveHTMLToStore);
|
|
|
|
observer.observe(element, config);
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function mirrorToElement(node: Node): void {
|
2021-10-18 14:01:15 +02:00
|
|
|
observer.disconnect();
|
2022-01-11 23:39:41 +01:00
|
|
|
// element.replaceChildren(...node.childNodes); // TODO use once available
|
2021-10-18 14:01:15 +02:00
|
|
|
while (element.firstChild) {
|
|
|
|
element.firstChild.remove();
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
while (node.firstChild) {
|
|
|
|
element.appendChild(node.firstChild);
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
observer.observe(element, config);
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
function mirrorFromFragment(fragment: DocumentFragment): void {
|
|
|
|
mirrorToElement(cloneNode(fragment));
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
const { subscribe, unsubscribe } = storeSubscribe(
|
|
|
|
store,
|
|
|
|
mirrorFromFragment,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
/* do not update when focused as it will reset caret */
|
2022-01-11 23:39:41 +01:00
|
|
|
const removeFocus = on(element, "focus", unsubscribe);
|
|
|
|
let removeBlur: (() => void) | undefined;
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
const unsubResubscription = allowResubscription.subscribe(
|
|
|
|
(allow: boolean): void => {
|
|
|
|
if (allow) {
|
2022-01-11 23:39:41 +01:00
|
|
|
if (!removeBlur) {
|
|
|
|
removeBlur = on(element, "blur", subscribe);
|
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
const root = element.getRootNode() as Document | ShadowRoot;
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
if (root.activeElement !== element) {
|
2022-01-11 23:39:41 +01:00
|
|
|
subscribe();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
2022-01-11 23:39:41 +01:00
|
|
|
} else if (removeBlur) {
|
|
|
|
removeBlur();
|
|
|
|
removeBlur = undefined;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
2021-10-19 01:06:00 +02:00
|
|
|
},
|
2021-10-18 14:01:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
destroy() {
|
|
|
|
observer.disconnect();
|
|
|
|
|
2022-01-11 23:39:41 +01:00
|
|
|
removeFocus();
|
|
|
|
removeBlur?.();
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
unsubscribe();
|
|
|
|
unsubResubscription();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mirror,
|
|
|
|
preventResubscription,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getDOMMirror;
|