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
text editor/collapsible: fix another issue with caret navigation
Adorian Doran68249c84d668
diff --git a/packages/ckeditor5-collapsible/src/collapsibleediting.ts b/packages/ckeditor5-collapsible/src/collapsibleediting.tsindex beacdb80be..605cc2c0aa 100644--- a/packages/ckeditor5-collapsible/src/collapsibleediting.ts+++ b/packages/ckeditor5-collapsible/src/collapsibleediting.ts@@ -157,6 +157,13 @@ export default class CollapsibleEditing extends Plugin {return;}+ // If the title wraps onto multiple visual lines and the caret isn't on+ // the last one yet, let native arrow navigation move within the summary.+ const dom = summaryDom(summaryModel);+ if (dom && !isCaretOnLastVisualLine(dom)) {+ return;+ }+let target: any;if (!isDetailsOpen(detailsModel)) {target = detailsModel.nextSibling;@@ -201,6 +208,29 @@ export default class CollapsibleEditing extends Plugin {return !(domEl instanceof HTMLDetailsElement) || domEl.open;};+ // Helpers: detect whether the caret is on the first/last *visual* line of a+ // wrapped element. Used to avoid hijacking up/down arrows when the cursor+ // still has somewhere to go within a multi-line summary.+ const isCaretOnFirstVisualLine = (dom: HTMLElement): boolean => {+ const sel = dom.ownerDocument.defaultView?.getSelection();+ if (!sel || sel.rangeCount === 0) return true;+ const caretRect = sel.getRangeAt(0).getBoundingClientRect();+ const lineHeight = parseFloat(dom.ownerDocument.defaultView!.getComputedStyle(dom).lineHeight) || 16;+ return caretRect.top < dom.getBoundingClientRect().top + lineHeight / 2;+ };+ const isCaretOnLastVisualLine = (dom: HTMLElement): boolean => {+ const sel = dom.ownerDocument.defaultView?.getSelection();+ if (!sel || sel.rangeCount === 0) return true;+ const caretRect = sel.getRangeAt(0).getBoundingClientRect();+ const lineHeight = parseFloat(dom.ownerDocument.defaultView!.getComputedStyle(dom).lineHeight) || 16;+ return caretRect.bottom > dom.getBoundingClientRect().bottom - lineHeight / 2;+ };+ const summaryDom = (summaryModel: any): HTMLElement | null => {+ const view = editor.editing.mapper.toViewElement(summaryModel);+ const dom = view ? editor.editing.view.domConverter.viewToDom(view) : null;+ return dom instanceof HTMLElement ? dom : null;+ };+// UX: allow up-arrow to move cursor into the last block of a collapsible from below.this.listenTo<ViewDocumentArrowKeyEvent>(viewDocument, "arrowKey", (evt, data) => {if (data.keyCode !== 38 || data.shiftKey || !selection.isCollapsed) {@@ -261,6 +291,13 @@ export default class CollapsibleEditing extends Plugin {return;}+ // Multi-line title: if the caret isn't on the first visual line yet, let+ // native arrow navigation move within the summary.+ const dom = summaryDom(summary);+ if (dom && !isCaretOnFirstVisualLine(dom)) {+ return;+ }+const prevSibling = details.previousSibling;if (!prevSibling?.is("element", "details")) {return;