Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/include notes: fix an issue

Adorian Doranffc28bb3b58c
diff --git a/apps/client/src/widgets/react/ImageViewer.css b/apps/client/src/widgets/react/ImageViewer.css
index 6dc931c222..a13c0d53c9 100644
--- a/apps/client/src/widgets/react/ImageViewer.css
+++ b/apps/client/src/widgets/react/ImageViewer.css
@@ -29,6 +29,7 @@
max-width: 100%;
max-height: 100%;
opacity: 1;
+ user-select: none;
transition: opacity 300ms ease-out;
}
diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
index 8ada8f5dd6..bfaf425265 100644
--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
@@ -49,6 +49,7 @@ afterEach(() => {
for (const tooltip of document.querySelectorAll(".tooltip")) {
tooltip.remove();
}
+ Reflect.deleteProperty(document, "elementsFromPoint");
});
function element(html: string) {
@@ -61,6 +62,16 @@ function element(html: string) {
return first;
}
+/** Stubs `document.elementsFromPoint()`, which happy-dom lacks, to find `elements` at any point. */
+function stubElementsFromPoint(elements: Element[]) {
+ const elementsFromPoint = vi.fn((_x: number, _y: number) => elements);
+ Object.defineProperty(document, "elementsFromPoint", {
+ value: elementsFromPoint,
+ configurable: true
+ });
+ return elementsFromPoint;
+}
+
function renderBox(props: Partial<ContentEmbedProps> = {}) {
const boxProps: ContentEmbedProps = {
title: element(`<span><a href="#root/noteA">Note A</a></span>`),
@@ -296,9 +307,15 @@ describe("ContentEmbed", () => {
expect(container.querySelector(".include-note-fullscreen-controls")).toBeNull();
});
- it("keeps its content behind a backdrop until a click on it focuses the content", async () => {
- renderBox();
+ it("keeps its content behind a backdrop until a click on it focuses what is under it", async () => {
+ const content = element(
+ `<div class="rendered-content"><p>body</p><div tabindex="0"><img></div></div>`
+ );
+ renderBox({ content });
const box = contentBox();
+ const text = content.querySelector("p");
+ const viewer = content.querySelector<HTMLElement>("[tabindex]");
+ const image = content.querySelector("img");
const isActive = () => box.parentElement?.classList.contains("active");
const backdrop = () => container.querySelector<HTMLElement>(
".include-note-body > .include-note-content + .include-note-backdrop"
@@ -307,16 +324,29 @@ describe("ContentEmbed", () => {
expect(firstBackdrop).not.toBeNull();
expect(box.tabIndex).toBe(-1);
expect(isActive()).toBe(false);
- if (!firstBackdrop) return;
+ if (!firstBackdrop || !text || !viewer || !image) return;
- click(firstBackdrop);
- expect(document.activeElement).toBe(box);
+ // What takes the focus under the pointer gets it, as it would from a click on it.
+ const elementsFromPoint = stubElementsFromPoint([ firstBackdrop, image, viewer, box ]);
+ act(() => {
+ firstBackdrop.dispatchEvent(new MouseEvent("click", { clientX: 40, clientY: 30 }));
+ });
+ expect(elementsFromPoint).toHaveBeenCalledWith(40, 30);
+ expect(document.activeElement).toBe(viewer);
expect(backdrop()).toBeNull();
expect(isActive()).toBe(true);
- act(() => box.blur());
+ act(() => viewer.blur());
await vi.waitFor(() => expect(backdrop()).not.toBeNull());
expect(isActive()).toBe(false);
+
+ // Over text, the content box takes it.
+ const secondBackdrop = backdrop();
+ if (!secondBackdrop) return;
+ stubElementsFromPoint([ secondBackdrop, text, box ]);
+ click(secondBackdrop);
+ expect(document.activeElement).toBe(box);
+ expect(isActive()).toBe(true);
});
});
diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
index aa5ccde8d0..f1df5c7cf7 100644
--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
@@ -129,7 +129,12 @@ export default function ContentEmbed({
{!isContentActive && (
<div
className="include-note-backdrop"
- onClick={() => contentRef.current?.focus({ preventScroll: true })}
+ onClick={(e) => {
+ const content = contentRef.current;
+ if (!content) return;
+ getFocusTargetAt(content, e.clientX, e.clientY)
+ .focus({ preventScroll: true });
+ }}
/>
)}
</div>
@@ -191,6 +196,18 @@ function getOpenInNewTabAction(notePath: string, viewScope?: ViewScope): Content
};
}
+const FOCUSABLE_SELECTOR = "[tabindex], a[href], iframe, webview, "
+ + ":is(button, input, select, textarea):not(:disabled)";
+
+/**
+ * The element that a click at (`x`, `y`) would focus: the closest focusable element under that
+ * point in `content`, which is focusable itself.
+ */
+function getFocusTargetAt(content: HTMLElement, x: number, y: number) {
+ const hit = document.elementsFromPoint(x, y).find((element) => content.contains(element));
+ return hit?.closest<HTMLElement>(FOCUSABLE_SELECTOR) ?? content;
+}
+
function ContentEmbedTitle({ title }: { title: HTMLElement }) {
const titleRef = useRef<HTMLHeadingElement>(null);
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 0000000000..d405d40364
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,34 @@
+diff --git a/dist/index.cjs.js b/dist/index.cjs.js
+index e4db255..636da4e 100644
+--- a/dist/index.cjs.js
++++ b/dist/index.cjs.js
+@@ -1469,7 +1469,11 @@ var isEditableTarget = function (node) {
+ return true;
+ if (element.isContentEditable)
+ return true;
+- return element.closest(CONTENT_EDITABLE_SELECTOR) !== null;
++ // A `contenteditable="false"` island, such as the widget of an editor, is not editable
++ // even inside an editable region.
++ var host = element.closest(CONTENT_EDITABLE_SELECTOR);
++ var island = element.closest('[contenteditable="false"]');
++ return host !== null && (island === null || island.contains(host));
+ };
+ /**
+ * HTML5 drag sources (or anything nested inside them). A pan started here
+diff --git a/dist/index.esm.js b/dist/index.esm.js
+index 92c7948..597439a 100644
+--- a/dist/index.esm.js
++++ b/dist/index.esm.js
+@@ -1467,7 +1467,11 @@ var isEditableTarget = function (node) {
+ return true;
+ if (element.isContentEditable)
+ return true;
+- return element.closest(CONTENT_EDITABLE_SELECTOR) !== null;
++ // A `contenteditable="false"` island, such as the widget of an editor, is not editable
++ // even inside an editable region.
++ var host = element.closest(CONTENT_EDITABLE_SELECTOR);
++ var island = element.closest('[contenteditable="false"]');
++ return host !== null && (island === null || island.contains(host));
+ };
+ /**
+ * HTML5 drag sources (or anything nested inside them). A pan started here
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b1db43988b..454571f30a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -87,6 +87,7 @@ overrides:
patchedDependencies:
'@excalidraw/[email protected]': 859531a8ff622291b895d36bcd33e07ced6fcb3bcc90631ccaf776789b03f0c2
[email protected]: 8acc288f8e9cde3c8a0c6e89f0b3796aeeb8251b88db9d1ba313e2352a591d68
+ [email protected]: 09dfeb6f635c52ef565ca21aec3f70eb129577661f85eb67262092e8cb9f8548
importers:
@@ -353,7 +354,7 @@ importers:
react-zoom-pan-pinch:
specifier: 4.2.0
+ version: 4.2.0(patch_hash=09dfeb6f635c52ef565ca21aec3f70eb129577661f85eb67262092e8cb9f8548)([email protected]([email protected]))([email protected])
reveal.js:
specifier: 6.0.2
version: 6.0.2
@@ -985,7 +986,7 @@ importers:
react-zoom-pan-pinch:
specifier: 4.2.0
+ version: 4.2.0(patch_hash=09dfeb6f635c52ef565ca21aec3f70eb129577661f85eb67262092e8cb9f8548)([email protected]([email protected]))([email protected])
reveal.js:
specifier: 6.0.2
version: 6.0.2
@@ -3666,6 +3667,88 @@ packages:
'@paralleldrive/[email protected]':
resolution: {integrity: sha512-XO7cAxhnTZl0Yggq6jOgjiOHhbgcO4NqFqwSmQpjK3b6TEE6Uj/jfSk6wzYyemh3+I0sHirKSetjQwn5cZktFw==}
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-trgpLSCKRC/huFjXX/Smh+0sWe4+YtKfktIToiMl59ghz7z+qkH6kMvNnUbLyRs9N11t8l4svSCs1+5B3rOAhA==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm64]
+ os: [android]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-Y3QV0gl7Q1zbfueunkWIERICbEojQFCgpyG7YqOGNFLsckXyI1xu9mAIUpKY9QBYzBtSkN8dBPwd3yiAO9ovMw==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm64]
+ os: [darwin]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-Ohv6OpzhUfKYD7Beb8kDvG0jbIxORCYY1JRdZnaBtnjjkJxgD7ZVL0nw2sCYd0yTMKTvz3nnTnOF3cDifK+kvw==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [x64]
+ os: [darwin]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-5HmXvDgs8VK+74jF9y9/2FE3/OnlcKmc56tjmSrEuZjpSZOGL+fvAu+HKJBdPs9uwoP2hE6TlSUpXZ/C5jUFmQ==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [x64]
+ os: [freebsd]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-Ps/hui3A+vMbjdqlqAowK2ZL8+BO8dBjxeWXj6npTBs3jx4wWmbPpaLuqwrQrSqIVMCnpWo238bJ1U37GhQOYg==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm]
+ os: [linux]
+ libc: [glibc]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-9c6AUHgHoG+IY88MRIHupztQiQnrbqHYQjkM2btA+Bf/wQnQMuiD0Wfk1EVv3TlNT3x41uU71rn6E4xh/+zvkw==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm]
+ os: [linux]
+ libc: [musl]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-yHRqS2owEXe6Hic9z6Mh1ECsCd+ODVOGvZDyciqRd21+v+o+DnXMOrw50DSpIG2sb8GPEaPPmfeCAWKPJdq46g==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm64]
+ os: [linux]
+ libc: [glibc]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-WhB2e/V7rqdHHWZusBSPuy5Ei8S6lSz6FE5TKKQz5h3a0O+C+mhY7vxU9b/stqvMb8beLnPY82ZrFTLKs+SrKA==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm64]
+ os: [linux]
+ libc: [musl]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-ulGE6x6Oz6iAwg75T8YQSoguBWasniIbX+QWpaYPcCnDOpdWX3k+4xbEYPZVLxOuoJI+svJJPD3sEj8G7lrQ3A==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [x64]
+ os: [linux]
+ libc: [glibc]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-tkBYKt7YQrjIJWYDnto2YgO8MRkjlMTSNoRHzsXinBqbLdeOM3L32wPZJvIZxqaLMfSlS/4sUjH/6STVP/XDLw==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [x64]
+ os: [linux]
+ libc: [musl]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-gIZAP23jaHjGWasY/TY6yL7NHFClf0Ga7FN+iINvk+KN94rhm94lYZhFsbYFNcA04/onvGD9kKmiJLJB2HbNwQ==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [arm64]
+ os: [win32]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-cA+/pXV2YkfxlIcXOQ5fSWqAzzPyD78/x5qbK/I0vUkrlYHA8TIz+MXjAbGouguKVSI4bOmkTSJ1/poVSsgt+A==}
+ engines: {node: '>= 10.0.0'}
+ cpu: [x64]
+ os: [win32]
+
+ '@parcel/[email protected]':
+ resolution: {integrity: sha512-7FNeNl8NCE7aINx7WXiKQrPYZWC/hvrTsmk6zmxbI7LTXE7hVek/n8AfVgpe2y82zl3w0HvCHN0bVKMBoJcC0w==}
+ engines: {node: '>= 10.0.0'}
+
'@phosphor-icons/[email protected]':
resolution: {integrity: sha512-rPAR9o/bEcp4Cw4DEeZHXf+nlGCMNGkNDRizYHM47NLxz9vvEHp/Tt6FMK1NcWadzw/pFDPnRBGi/ofRya958A==}
@@ -9310,6 +9393,9 @@ packages:
resolution: {integrity: sha512-ymk4aIzxdPopw2giv8Fs1Ec6vybGkjmyxUwVqhkI4MCy2tVfXdkOGGWieWVjL0THgH+7a8lRdevyupoYj3Js/Q==}
engines: {node: '>=22.12.0'}
+ resolution: {integrity: sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==}
+
resolution: {integrity: sha512-VijLXbi3UACN69I0JVXJsX4tjACjNoQDgv2gTF6sx2wWEi8tkSg2eX8p5gSIFi8z2+DL3oHmY6OyKce38SDolg==}
engines: {node: ^18 || ^20 || >= 21}
@@ -14924,6 +15010,63 @@ snapshots:
dependencies:
'@noble/hashes': 1.8.0
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ optional: true
+
+ '@parcel/[email protected]':
+ dependencies:
+ detect-libc: 2.1.2
+ is-glob: 4.0.3
+ node-addon-api: 7.1.1
+ picomatch: 4.0.7
+ optionalDependencies:
+ '@parcel/watcher-android-arm64': 2.6.0
+ '@parcel/watcher-darwin-arm64': 2.6.0
+ '@parcel/watcher-darwin-x64': 2.6.0
+ '@parcel/watcher-freebsd-x64': 2.6.0
+ '@parcel/watcher-linux-arm-glibc': 2.6.0
+ '@parcel/watcher-linux-arm-musl': 2.6.0
+ '@parcel/watcher-linux-arm64-glibc': 2.6.0
+ '@parcel/watcher-linux-arm64-musl': 2.6.0
+ '@parcel/watcher-linux-x64-glibc': 2.6.0
+ '@parcel/watcher-linux-x64-musl': 2.6.0
+ '@parcel/watcher-win32-arm64': 2.6.0
+ '@parcel/watcher-win32-x64': 2.6.0
+ optional: true
+
'@phosphor-icons/[email protected]': {}
'@playwright/[email protected]':
@@ -21468,6 +21611,9 @@ snapshots:
dependencies:
semver: 7.8.5
+ optional: true
+
@@ -22213,7 +22359,7 @@ snapshots:
react: 18.3.1
react-dom: 18.3.1([email protected])
+ [email protected](patch_hash=09dfeb6f635c52ef565ca21aec3f70eb129577661f85eb67262092e8cb9f8548)([email protected]([email protected]))([email protected]):
dependencies:
react: 18.3.1
react-dom: 18.3.1([email protected])
@@ -22612,6 +22758,8 @@ snapshots:
chokidar: 5.0.0
immutable: 5.1.9
source-map-js: 1.2.1
+ optionalDependencies:
+ '@parcel/watcher': 2.6.0
optional: true
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 1ac48ba380..b32c8b5894 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -127,6 +127,7 @@ ignoredOptionalDependencies:
patchedDependencies:
'@excalidraw/[email protected]': patches/@[email protected]
allowBuilds:
"@parcel/watcher": false