Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): keep a text note's scroll when focus leaves the editor

Elian Dorande55eb65553c
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts b/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts
index 832f176e71..605fd03317 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts
@@ -33,6 +33,7 @@ vi.mock("@triliumnext/ckeditor5", () => ({}));
const {
applyPendingAttachmentChanges,
getBlockData,
+ isCaretInFocus,
notifyAttachmentChanges,
onNotificationInfo,
onNotificationWarning,
@@ -349,3 +350,28 @@ describe("revealLinkTargetWhenReady", () => {
expect(reveal).toHaveBeenCalledTimes(2);
});
});
+
+describe("isCaretInFocus", () => {
+ it("holds while focus stays in the editable element with the selection, not once a menu takes it", () => {
+ const editable = document.createElement("div");
+ editable.contentEditable = "true";
+ editable.innerHTML = "<p>First paragraph</p>";
+ const menu = document.createElement("div");
+ menu.tabIndex = -1;
+ document.body.append(editable, menu);
+ const paragraph = editable.querySelector("p");
+ expect(paragraph).toBeTruthy();
+
+ editable.focus();
+ window.getSelection()?.collapse(paragraph, 0);
+ expect(isCaretInFocus(document.activeElement, window.getSelection())).toBe(true);
+
+ // The selection stays in the editor while the menu holds focus.
+ menu.focus();
+ expect(window.getSelection()?.focusNode).toBe(paragraph);
+ expect(isCaretInFocus(document.activeElement, window.getSelection())).toBe(false);
+
+ editable.remove();
+ menu.remove();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 70cc96000c..9753cc7d65 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -513,6 +513,8 @@ export default function EditableText({
};
const scrollCaretIntoView = () => {
+ if (!isCaretInFocus(document.activeElement, window.getSelection())) return;
+
const caretRect = getCaretRect();
if (!caretRect) return;
const scroller = findScrollableAncestor(window.getSelection()?.focusNode);
@@ -737,6 +739,16 @@ export function revealLinkTargetWhenReady(
}
}
+/**
+ * Whether focus is in an editable element that holds the selection. Focus taken by something else,
+ * such as a menu, leaves the selection behind in the editor, which is then no caret to scroll to.
+ */
+export function isCaretInFocus(focused: Element | null, selection: Selection | null) {
+ const focusNode = selection?.focusNode;
+ return focused instanceof HTMLElement && focused.isContentEditable
+ && !!focusNode && focused.contains(focusNode);
+}
+
/** The blocks that an editor of the blocks `block` holds, preferring the ids of `block`. */
function getEditorBlocks(editor: CKTextEditor, block: string) {
const range = editor.plugins.get("BlockReferenceEditing").getRange(parseBlockRange(block));