Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(dialogs): show a dialog over whatever has the screen to itself
Elian Doran0ac0f1ce7849
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.tsindex 9f8cb332c1..14c3606dcb 100644--- a/apps/client/src/services/dialog.spec.ts+++ b/apps/client/src/services/dialog.spec.ts@@ -289,6 +289,104 @@ describe("dialog service", () => {});});+ /**+ * A browser showing an element fullscreen draws that element and nothing else, and hands it every+ * press. A dialog lives in the shell, so over a fullscreen map (see the geo map's toolbar) the+ * quick editor was opened and focused while nothing of it was ever painted — the button on a+ * marker's preview appeared to swallow the click.+ */+ describe("showing a dialog over an element that has the screen", () => {+ afterEach(() => {+ setFullscreenElement(null);+ document.body.className = "";+ document.body.innerHTML = "";+ });++ function setFullscreenElement(element: Element | null) {+ Object.defineProperty(document, "fullscreenElement", { value: element, configurable: true });+ }++ /** The shell a dialog is built in, and a map that may be given the screen. */+ function buildPage() {+ const map = $("<div class='map'></div>").appendTo(document.body)[0];+ const shell = $("<div class='shell'></div>").appendTo(document.body)[0];+ const $dialog = makeDialog().appendTo(shell);+ // The backdrop Bootstrap would append during show(), which is mocked here.+ const backdrop = $("<div class='modal-backdrop'></div>").appendTo(document.body)[0];+ return { map, shell, $dialog, backdrop };+ }++ it("leaves a dialog where it was built while nothing has the screen", async () => {+ const { shell, $dialog, backdrop } = buildPage();++ await openDialog($dialog, true);++ expect($dialog[0].parentElement).toBe(shell);+ expect(backdrop.parentElement).toBe(document.body);+ });++ it("hosts the dialog and its dim inside whatever has the screen", async () => {+ const { map, $dialog, backdrop } = buildPage();+ setFullscreenElement(map);++ await openDialog($dialog, true);++ expect($dialog[0].parentElement).toBe(map);+ expect(backdrop.parentElement).toBe(map);+ // Ahead of the dialog and on no layer of its own: Bootstrap's own 1050 would put the dim+ // over a dialog the theme has lowered (the quick editor sits at 999), and the rule that+ // lowers it hangs on a class the page has yet to be given, so there is no number to read.+ expect(backdrop.nextElementSibling).toBe($dialog[0]);+ expect(backdrop.style.zIndex).toBe("0");+ });++ it("puts both back, exactly where they stood, once the dialog is closed", async () => {+ const { map, shell, $dialog, backdrop } = buildPage();+ const neighbour = $("<div></div>").appendTo(shell)[0];+ setFullscreenElement(map);++ await openDialog($dialog, true);+ $dialog.trigger("hidden.bs.modal");++ expect($dialog[0].parentElement).toBe(shell);+ expect($dialog[0].nextElementSibling).toBe(neighbour);+ expect(backdrop.parentElement).toBe(document.body);+ // Without this the layer it was lent would be inherited by the next open.+ expect(backdrop.style.zIndex).toBe("");+ });++ it("sends the dialog home the moment the screen is given back under it", async () => {+ const { map, shell, $dialog } = buildPage();+ setFullscreenElement(map);++ await openDialog($dialog, true);+ expect($dialog[0].parentElement).toBe(map);++ // As pressing Escape does, which the browser answers itself while the dialog stays open.+ setFullscreenElement(null);+ document.dispatchEvent(new Event("fullscreenchange"));++ expect($dialog[0].parentElement).toBe(shell);+ });++ it("stops watching the screen once the dialog is home, wherever it was closed", async () => {+ const { map, shell, $dialog } = buildPage();+ setFullscreenElement(map);++ await openDialog($dialog, true);+ $dialog.trigger("hidden.bs.modal");+ expect($dialog[0].parentElement).toBe(shell);++ // A later change of screen has nothing left to put back, and must not move a dialog that+ // is standing where it belongs.+ const elsewhere = $("<div></div>").appendTo(document.body)[0];+ $dialog.appendTo(elsewhere);+ document.dispatchEvent(new Event("fullscreenchange"));++ expect($dialog[0].parentElement).toBe(elsewhere);+ });+ });+describe("closeActiveDialog", () => {it("hides the active dialog and clears the reference", () => {const $dialog = makeDialog();diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.tsindex 06c8d527db..773ff9ec85 100644--- a/apps/client/src/services/dialog.ts+++ b/apps/client/src/services/dialog.ts@@ -37,7 +37,13 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =}}+ // After that normalising, which is written for a backdrop hanging at the end of the page and+ // would undo the layer this gives it there.+ showAboveWhateverHasTheScreen($dialog[0]);+$dialog.on("hidden.bs.modal", () => {+ sendDialogHome($dialog[0]);+const $autocompleteEl = $(".aa-input");if ("autocomplete" in $autocompleteEl) {$autocompleteEl.autocomplete("close");@@ -53,6 +59,86 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =return $dialog;}+/** Where a dialog stands while nothing has the screen, what came with it, and how to stop watching. */+const dialogHomes = new WeakMap<HTMLElement, {+ parent: Node;+ nextTo: Node | null;+ backdrop: HTMLElement | null;+ stopWatching: () => void;+}>();++/**+ * Puts a dialog where it can be seen and used over an element that has the screen to itself.+ *+ * A browser showing an element fullscreen draws that element and nothing else, and hands it every+ * press. A dialog lives in the shell rather than inside whatever was given the screen, so over a+ * fullscreen map or diagram (see `useFullscreen`) it was opened, focused and typed into while nothing+ * of it was ever painted — the quick editor reached for from a marker's preview simply swallowed the+ * click. Hosting it inside whatever is being shown puts it back on the screen and back in the way of+ * the pointer, exactly as the context menu is (see `hostInWhateverHasTheScreen`).+ *+ * Raising it into the top layer as a popover looks like the tidier answer and is not one: it is+ * painted there, above the fullscreen element, but every press still goes to the element underneath —+ * a dialog you can read and not use, which is worse than one you cannot see.+ *+ * The dialog is positioned against the viewport either way, so nothing about where it lands changes;+ * its backdrop goes with it, being at the end of the page too, and Bootstrap takes that away itself+ * whatever it is a child of.+ */+function showAboveWhateverHasTheScreen(dialogEl: HTMLElement) {+ const shownOver = document.fullscreenElement;+ const parent = dialogEl.parentNode;+ if (!shownOver || !parent || shownOver.contains(dialogEl)) {+ return;+ }++ // The screen may be given back while the dialog is still open — by pressing Escape, which the+ // browser answers itself — and the dialog belongs in the shell again the moment it is.+ const sendHomeOnScreenChange = () => sendDialogHome(dialogEl);+ document.addEventListener("fullscreenchange", sendHomeOnScreenChange);++ const backdrops = document.querySelectorAll<HTMLElement>(".modal-backdrop");+ const ownBackdrop = backdrops[backdrops.length - 1] ?? null;++ dialogHomes.set(dialogEl, {+ parent,+ nextTo: dialogEl.nextSibling,+ backdrop: ownBackdrop,+ stopWatching: () => document.removeEventListener("fullscreenchange", sendHomeOnScreenChange)+ });++ shownOver.appendChild(dialogEl);++ if (ownBackdrop) {+ // The dim comes along, ahead of the dialog rather than behind it, and on no layer of its+ // own. Bootstrap's 1050 would put it *over* a dialog the theme has lowered (a quick editor+ // sits at 999), which is a sheet of grey across the very thing that was opened; and reading+ // the dialog's own layer here is no answer either, since the rule that lowers it hangs on a+ // class the page has yet to be given. Standing first among equals settles it without a+ // number: the backdrop covers the map, being later in the page than the map is, and every+ // dialog worth the name is on a layer above zero.+ shownOver.insertBefore(ownBackdrop, dialogEl);+ ownBackdrop.style.zIndex = "0";+ }+}++/** Puts a dialog back where it was built. See {@link showAboveWhateverHasTheScreen}. */+function sendDialogHome(dialogEl: HTMLElement) {+ const home = dialogHomes.get(dialogEl);+ if (!home) return;++ dialogHomes.delete(dialogEl);+ home.stopWatching();+ home.parent.insertBefore(dialogEl, home.nextTo);++ if (home.backdrop) {+ // Back to the end of the page, where the rules that dress a backdrop can reach it again —+ // and without the layer it was lent, which the next open would otherwise inherit.+ home.backdrop.style.zIndex = "";+ document.body.appendChild(home.backdrop);+ }+}+export function closeActiveDialog() {if (glob.activeDialog) {Modal.getOrCreateInstance(glob.activeDialog[0]).hide();