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(mobile/text): stop scrolling up with a widget selected
Elian Doran9145163b840a
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts b/apps/client/src/widgets/type_widgets/text/EditableText.spec.tsindex 68e580d3fd..9f08e9859b 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts@@ -376,6 +376,22 @@ describe("isCaretInFocus", () => {editable.remove();menu.remove();});++ it("does not hold for the fake selection of a selected widget, which sits at the top of the viewport", () => {+ const editable = document.createElement("div");+ editable.contentEditable = "true";+ editable.innerHTML = "<figure class=\"image ck-widget ck-widget_selected\"></figure>"+ + "<div class=\"ck-fake-selection-container\" style=\"position: fixed; top: 0\"> </div>";+ document.body.append(editable);+ const container = editable.querySelector(".ck-fake-selection-container");+ expect(container?.firstChild).toBeTruthy();++ editable.focus();+ window.getSelection()?.selectAllChildren(container as Node);+ expect(isCaretInFocus(document.activeElement, window.getSelection())).toBe(false);++ editable.remove();+ });});describe("parseLinkHref", () => {diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex bc3c414568..ceae420bd6 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -796,11 +796,15 @@ 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.+ * Neither is the fake selection CKEditor puts in a hidden element pinned to the top of the viewport+ * while a widget, such as an image, is selected.*/export function isCaretInFocus(focused: Element | null, selection: Selection | null) {const focusNode = selection?.focusNode;+ const focusElement = focusNode instanceof Element ? focusNode : focusNode?.parentElement;return focused instanceof HTMLElement && focused.isContentEditable- && !!focusNode && focused.contains(focusNode);+ && !!focusNode && focused.contains(focusNode)+ && !focusElement?.closest(".ck-fake-selection-container");}/** The blocks that an editor of the blocks `block` holds, preferring the ids of `block`. */