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 a link target in editable notes
Elian Doraneabf1ffcc624
diff --git a/apps/client/src/widgets/sidebar/HighlightsList.tsx b/apps/client/src/widgets/sidebar/HighlightsList.tsxindex 0c9a9fb733..2feb4ee264 100644--- a/apps/client/src/widgets/sidebar/HighlightsList.tsx+++ b/apps/client/src/widgets/sidebar/HighlightsList.tsx@@ -1,6 +1,7 @@import type { CKTextEditor, ModelText } from "@triliumnext/ckeditor5";import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { revealElement } from "../../services/collapsible";import { t } from "../../services/i18n";import { randomString } from "../../services/utils";import Dropdown from "../react/Dropdown";@@ -218,10 +219,10 @@ function EditableTextHighlightsList() {const domPos = domConverter.viewPositionToDom(viewPos);if (!domPos) return;- if (domPos.parent instanceof HTMLElement) {- domPos.parent.scrollIntoView();- } else if (domPos.parent instanceof Text) {- domPos.parent.parentElement?.scrollIntoView();+ const element = domPos.parent instanceof Text ? domPos.parent.parentElement : domPos.parent;+ if (element instanceof HTMLElement) {+ revealElement(element);+ element.scrollIntoView();}}, [ textEditor ]);@@ -284,6 +285,7 @@ function ReadOnlyTextHighlightsList() {const highlights = extractHighlightsFromStaticHtml(contentEl);const scrollToHighlight = useCallback((highlight: DomHighlight) => {+ revealElement(highlight.element);highlight.element.scrollIntoView();}, []);diff --git a/apps/client/src/widgets/sidebar/TableOfContents.tsx b/apps/client/src/widgets/sidebar/TableOfContents.tsxindex 7e5ebfd14d..7e7732cac1 100644--- a/apps/client/src/widgets/sidebar/TableOfContents.tsx+++ b/apps/client/src/widgets/sidebar/TableOfContents.tsx@@ -4,6 +4,7 @@ import type { CKTextEditor, ModelElement, ModelNode } from "@triliumnext/ckeditoimport clsx from "clsx";import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { revealElement } from "../../services/collapsible";import { t } from "../../services/i18n";import { randomString } from "../../services/utils";import { useActiveNoteContext, useContentElement, useGetContextData, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor } from "../react/hooks";@@ -293,7 +294,10 @@ function EditableTextTableOfContents() {if (!viewEl) return;const domEl = textEditor.editing.view.domConverter.mapViewToDom(viewEl);- domEl?.scrollIntoView();+ if (domEl) {+ revealElement(domEl);+ domEl.scrollIntoView();+ }}, [ textEditor ]);return <AbstractTableOfContents@@ -392,6 +396,7 @@ function ReadOnlyTextTableOfContents() {}, [contentEl]);const scrollToHeading = useCallback((heading: DomHeading) => {+ revealElement(heading.element);heading.element.scrollIntoView();}, []);diff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.tsindex 992ffc0e91..78030f6422 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts@@ -1,6 +1,7 @@import {_getModelData as getModelData,_setModelData as setModelData,+ Bookmark,ClassicEditor,Essentials,FindAndReplaceEditing,@@ -10,6 +11,7 @@ import {import { afterEach, beforeEach, describe, expect, it } from "vitest";import Tabs from "./tabs.js";+import { revealTab } from "./tabs_read_only.js";const TWO_TABS ="<div class=\"trilium-tabs\">" +@@ -28,7 +30,7 @@ describe("Tabs", () => {document.body.appendChild(domElement);editor = await ClassicEditor.create(domElement, {licenseKey: "GPL",- plugins: [Essentials, FindAndReplaceEditing, Paragraph, Tabs]+ plugins: [Bookmark, Essentials, FindAndReplaceEditing, Paragraph, Tabs]});});@@ -155,6 +157,31 @@ describe("Tabs", () => {expect(activeTitles()).toEqual(["Linux"]);});+ it("shows the tab holding a link target 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);++ const target = [...editor.editing.view.getDomRoot()?.querySelectorAll("p") ?? []]+ .find(paragraph => paragraph.textContent === "Use the package.");+ expect(target).toBeDefined();+ revealTab(target as Element);++ expect(activeTitles()).toEqual(["Linux"]);+ expect(getModelData(editor.model)).toBe(caret);++ editor.setData(TWO_TABS.replace("Run the installer.", "<a id=\"install\"></a>Run the installer."));+ const linux = tabsElement().getChild(1) as ModelElement;+ editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));+ expect(activeTitles()).toEqual(["Linux"]);++ const bookmark = editor.editing.view.getDomRoot()?.querySelector("#install");+ expect(bookmark).toBeTruthy();+ revealTab(bookmark as Element);+ expect(activeTitles()).toEqual(["Windows"]);+ });+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 75852c1942..f475bc3452 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts@@ -66,6 +66,25 @@ export default class TabsEditing extends Plugin {return first?.is("element", ELEMENTS.tab) ? first : null;}+ /** Shows every tab that encloses `position`, without moving the selection. */+ public showTabsAround(position: ModelPosition | null | undefined) {+ const blocks = new Set<ModelElement>();+ this.activateTabsAround(position, blocks);+ if (blocks.size) {+ this.updateActiveClasses(blocks);+ }+ }++ /**+ * Shows every tab that encloses `domNode`, a node of the editing view such as a link target,+ * without moving the selection.+ */+ public showTabsAroundDomNode(domNode: Node) {+ const editing = this.editor.editing;+ const viewPosition = editing.view.domConverter.domPositionToView(domNode, 0);+ this.showTabsAround(viewPosition ? editing.mapper.toModelPosition(viewPosition) : null);+ }+private isActiveTab(tab: ModelElement) {const tabs = tab.parent;return !!tabs?.is("element", ELEMENTS.tabs) && this.getActiveTab(tabs) === tab;@@ -271,11 +290,7 @@ export default class TabsEditing extends Plugin {}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);- }+ this.showTabsAround(highlighted?.marker?.getStart());});}diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.tsindex f090e07a8e..182da49f60 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts@@ -1,4 +1,7 @@+import type { Editor } from "ckeditor5";+import { CLASSES } from "./constants.js";+import type TabsEditing from "./tabs_editing.js";export interface ApplyTabsOptions {/** The text a title without text shows, the editor's "Tab title" placeholder. */@@ -11,7 +14,7 @@ export interface ApplyTabsOptions {* and End move between the titles of a block. A block that is already set up keeps its listeners* and its active tab.*- * The module imports nothing from CKEditor, so pages that only display content can load it.+ * The module imports only types from CKEditor, so pages that only display content can load it.*/export function applyTabs(container: ParentNode, { placeholder }: ApplyTabsOptions) {for (const block of container.querySelectorAll<HTMLElement>(`.${CLASSES.tabs}`)) {@@ -95,9 +98,18 @@ export function applyTabs(container: ParentNode, { placeholder }: ApplyTabsOptio/*** Shows every tab that encloses `element`, such as the one holding a find result, in blocks that- * {@link applyTabs} set up.+ * {@link applyTabs} set up. Inside an editor, `TabsEditing` shows the tabs instead, without moving+ * the selection.*/export function revealTab(element: Element) {+ const editor = getEditor(element);+ if (editor) {+ if (editor.plugins.has("TabsEditing")) {+ (editor.plugins.get("TabsEditing") as TabsEditing).showTabsAroundDomNode(element);+ }+ return;+ }+let tab = element.closest(`.${CLASSES.tab}`);while (tab) {const block = tab.parentElement;@@ -127,6 +139,19 @@ export function revealFragment(hash: string): Element | null {return target;}+/** Returns the editor whose editing view holds `element`; only the root editable carries it. */+function getEditor(element: Element): Editor | null {+ let editable = element.closest(".ck-editor__editable");+ while (editable) {+ const editor = (editable as Element & { ckeditorInstance?: Editor | null }).ckeditorInstance;+ if (editor) {+ return editor;+ }+ editable = editable.parentElement?.closest(".ck-editor__editable") ?? null;+ }+ return null;+}+/** Shows the tab of the given title, keyed by the tabs block that {@link applyTabs} set up. */const activators = new WeakMap<HTMLElement, (title: HTMLElement) => void>();let lastTabId = 0;