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
feat(text): show the tab holding the highlighted find result
Elian Doran5b0ba751b35f
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.tsindex 6c1f7cd0fa..992ffc0e91 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts@@ -3,6 +3,7 @@ import {_setModelData as setModelData,ClassicEditor,Essentials,+ FindAndReplaceEditing,type ModelElement,Paragraph} from "ckeditor5";@@ -27,7 +28,7 @@ describe("Tabs", () => {document.body.appendChild(domElement);editor = await ClassicEditor.create(domElement, {licenseKey: "GPL",- plugins: [Essentials, Paragraph, Tabs]+ plugins: [Essentials, FindAndReplaceEditing, Paragraph, Tabs]});});@@ -134,6 +135,26 @@ describe("Tabs", () => {expect(activeTitles()).toEqual(["Linux"]);});+ it("shows the tab holding the highlighted find result without moving the caret", () => {+ editor.setData(TWO_TABS);+ const windowsPanel = (tabsElement().getChild(0) as ModelElement).getChild(1) as ModelElement;+ editor.model.change(writer => writer.setSelection(windowsPanel, 0));+ const caret = getModelData(editor.model);++ editor.execute("find", "the");+ expect(activeTitles()).toEqual(["Windows"]);++ editor.execute("findNext");+ expect(activeTitles()).toEqual(["Linux"]);+ expect(getModelData(editor.model)).toBe(caret);++ editor.execute("findNext");+ expect(activeTitles()).toEqual(["Windows"]);++ editor.execute("findPrevious");+ expect(activeTitles()).toEqual(["Linux"]);+ });+it("exposes the active tab and each panel's title to assistive technology", () => {editor.setData(TWO_TABS);const root = editor.editing.view.getDomRoot();diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.tsindex 9179eacde0..9c16d577cc 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts@@ -62,6 +62,7 @@ export default class TabsEditing extends Plugin {this.registerPostFixer();this.registerActiveTabTracking();this.registerEnterInTitle();+ this.registerFindReveal();}/**@@ -246,15 +247,19 @@ export default class TabsEditing extends Plugin {const editor = this.editor;const model = editor.model;+ // A change that leaves the selection where it was, such as a find marker moving, keeps+ // the tab that is showing.+ let selectionMoved = true;+ this.listenTo(model.document.selection, "change:range", () => {+ selectionMoved = true;+ });+this.listenTo(model.document, "change", () => {const blocks = new Set<ModelElement>();- const position = model.document.selection.getFirstPosition();- for (const ancestor of position?.getAncestors() ?? []) {- if (ancestor.is("element", ELEMENTS.tab) && ancestor.parent?.is("element", ELEMENTS.tabs)) {- this.activeTabs.set(ancestor.parent, ancestor);- blocks.add(ancestor.parent);- }+ if (selectionMoved) {+ this.activateTabsAround(model.document.selection.getFirstPosition(), blocks);+ selectionMoved = false;}for (const change of model.document.differ.getChanges()) {@@ -269,6 +274,33 @@ export default class TabsEditing extends Plugin {}, { priority: "lowest" });}+ /** Shows the tab that holds the highlighted find-in-note result, without moving the caret. */+ private registerFindReveal() {+ const plugins = this.editor.plugins;+ const state = plugins.has("FindAndReplaceEditing") ? plugins.get("FindAndReplaceEditing").state : undefined;+ if (!state) {+ return;+ }++ this.listenTo(state, "change:highlightedResult", (_evt, _name, highlighted) => {+ const blocks = new Set<ModelElement>();+ this.activateTabsAround(highlighted?.marker?.getStart(), blocks);+ if (blocks.size) {+ this.updateActiveClasses(blocks);+ }+ });+ }++ /** Makes every tab that encloses `position` the active one of its block and adds the block to `blocks`. */+ private activateTabsAround(position: ModelPosition | null | undefined, blocks: Set<ModelElement>) {+ for (const ancestor of position?.getAncestors() ?? []) {+ if (ancestor.is("element", ELEMENTS.tab) && ancestor.parent?.is("element", ELEMENTS.tabs)) {+ this.activeTabs.set(ancestor.parent, ancestor);+ blocks.add(ancestor.parent);+ }+ }+ }+private updateActiveClasses(blocks: Set<ModelElement>) {const editing = this.editor.editing;editing.view.change(writer => {