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 { writable } from "svelte/store";
|
|
|
|
import type { Writable } from "svelte/store";
|
|
|
|
import storeSubscribe from "./store-subscribe";
|
2022-01-08 02:46:01 +01:00
|
|
|
// import { getSelection } from "../lib/cross-browser";
|
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 = (
|
|
|
|
element: Element,
|
|
|
|
params: { store: Writable<DocumentFragment> },
|
|
|
|
) => { destroy(): void };
|
|
|
|
|
|
|
|
interface DOMMirrorAPI {
|
|
|
|
mirror: MirrorAction;
|
2021-10-18 14:01:15 +02:00
|
|
|
preventResubscription(): () => void;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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(
|
|
|
|
element: Element,
|
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 {
|
|
|
|
const range = document.createRange();
|
|
|
|
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
const contents = range.cloneContents();
|
|
|
|
|
|
|
|
store.set(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver(saveHTMLToStore);
|
|
|
|
observer.observe(element, config);
|
|
|
|
|
|
|
|
function mirrorToNode(node: Node): void {
|
|
|
|
observer.disconnect();
|
|
|
|
const clone = node.cloneNode(true);
|
|
|
|
|
|
|
|
/* TODO use Element.replaceChildren */
|
|
|
|
while (element.firstChild) {
|
|
|
|
element.firstChild.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (clone.firstChild) {
|
|
|
|
element.appendChild(clone.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.observe(element, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { subscribe, unsubscribe } = storeSubscribe(store, mirrorToNode);
|
2022-01-08 02:46:01 +01:00
|
|
|
// const selection = getSelection(element)!;
|
|
|
|
|
|
|
|
function doSubscribe(): void {
|
|
|
|
// Might not be needed after all:
|
|
|
|
// /**
|
|
|
|
// * Focused element and caret are two independent things in the browser.
|
|
|
|
// * When the ContentEditable calls blur, it will still have the selection inside of it.
|
|
|
|
// * Some elements (e.g. FrameElement) need to figure whether the intended focus is still
|
|
|
|
// * in the contenteditable or elsewhere because they might change the selection.
|
|
|
|
// */
|
|
|
|
// selection.removeAllRanges();
|
|
|
|
|
|
|
|
subscribe();
|
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
/* do not update when focused as it will reset caret */
|
|
|
|
element.addEventListener("focus", unsubscribe);
|
|
|
|
|
|
|
|
const unsubResubscription = allowResubscription.subscribe(
|
|
|
|
(allow: boolean): void => {
|
|
|
|
if (allow) {
|
2022-01-08 02:46:01 +01:00
|
|
|
element.addEventListener("blur", doSubscribe);
|
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-08 02:46:01 +01:00
|
|
|
doSubscribe();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-08 02:46:01 +01:00
|
|
|
element.removeEventListener("blur", doSubscribe);
|
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();
|
|
|
|
|
|
|
|
// removes blur event listener
|
|
|
|
allowResubscription.set(false);
|
|
|
|
element.removeEventListener("focus", unsubscribe);
|
|
|
|
|
|
|
|
unsubscribe();
|
|
|
|
unsubResubscription();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mirror,
|
|
|
|
preventResubscription,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getDOMMirror;
|