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 some issues
Adorian Doran5e6bd5360bca
diff --git a/apps/client/src/services/content_renderer_text.spec.ts b/apps/client/src/services/content_renderer_text.spec.tsindex 55d335c8ba..8d095e354d 100644--- a/apps/client/src/services/content_renderer_text.spec.ts+++ b/apps/client/src/services/content_renderer_text.spec.ts@@ -2,7 +2,9 @@// DOMPurify relies on browser-faithful DOM traversal (NodeIterator); happy-dom// mishandles it and strips valid markup (surfaced by dompurify 3.4.8). Run the// sanitization-dependent specs under jsdom, which matches real-browser behavior.-import { KATEX_MACROS, trimIndentation } from "@triliumnext/commons";+import {+ KATEX_MACROS, MULTICOLUMN_COLUMN_CLASS, MULTICOLUMN_LAYOUT_CLASS, trimIndentation+} from "@triliumnext/commons";import DOMPurify from "dompurify";import { beforeEach, describe, expect, it, vi } from "vitest";@@ -266,14 +268,25 @@ describe("Text content renderer", () => {expect(emptied.querySelector("a")?.textContent).toBe("Child note");});- it("sizes the tables of the content for scrolling", async () => {+ it("sizes the tables of the content for scrolling, in an excerpt too", async () => {const note = buildNote({ title: "Tables" });- const contentEl = document.createElement("div");- const table = "<table><tbody><tr><td>a</td></tr></tbody></table>";- const html = `<figure class="table" style="width:800px;">${table}</figure>`;- await renderTextContent(note, html, $(contentEl));- const figure = contentEl.querySelector<HTMLElement>(".ck-content > figure.table");- expect(figure?.style.getPropertyValue("--table-width")).toBe("800px");+ const table = `<figure class="table" style="width:800px;" data-trilium-block-id="t1">`+ + "<table><tbody><tr><td>a</td></tr></tbody></table></figure>";+ const tableWidth = (contentEl: HTMLElement) => contentEl+ .querySelector<HTMLElement>(".ck-content > figure.table")+ ?.style.getPropertyValue("--table-width");++ const full = document.createElement("div");+ await renderTextContent(note, table, $(full));+ expect(tableWidth(full)).toBe("800px");++ // The excerpt of a table in one column of a layout has the table at its top level.+ const excerpt = document.createElement("div");+ const layout = `<div class="${MULTICOLUMN_LAYOUT_CLASS}">`+ + `<div class="${MULTICOLUMN_COLUMN_CLASS}">${table}</div>`+ + `<div class="${MULTICOLUMN_COLUMN_CLASS}"><p>Other</p></div></div>`;+ await renderTextContent(note, layout, $(excerpt), { block: "t1" });+ expect(tableWidth(excerpt)).toBe("800px");});it("renders nothing for an empty note with no children when noChildrenList is set", async () => {diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.tsindex 288be46583..036f16685e 100644--- a/apps/client/src/services/content_renderer_text.ts+++ b/apps/client/src/services/content_renderer_text.ts@@ -41,13 +41,13 @@ export async function renderTextContent(const $content = $('<div class="ck-content">').html(hasContent ? resolveContentImageUrls(sanitizeNoteContentHtml(content)) : "");applyNoteImageVersions($content[0]);- applyTableScroll($content[0]);if (options.block !== undefined && !sliceToBlockReference($content[0], options.block)) {$renderedContent.append($("<p>").addClass("block-reference-broken").text(t("block_reference.broken")));} else if (hasContent) {+ applyTableScroll($content[0]);// Runs before `postProcessRichContent()` renders embeds, so their blocks are not searched.if (options.highlightBlock) {highlightBlockReference($content[0], options.highlightBlock);diff --git a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.tsindex 121f7d86f5..06112d12b5 100644--- a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts+++ b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.spec.ts@@ -491,30 +491,63 @@ describe("TableColumnBar", () => {const widths = ["100px", "200px", "100px"];await showBar(editor, resizedTable([["a[]", "b", "c"]], widths, 'tableWidth="400px"'));const table = firstTable(editor);- const tap = (column: number) => {- press(dividers(editor)[column]);- release();- };- tap(0);- tap(1);- tap(2);- tap(2);+ tapDivider(editor, 0);+ tapDivider(editor, 1);+ tapDivider(editor, 2);+ tapDivider(editor, 2);expect(getTableColumnsWidths(table)).toEqual(widths);const now = vi.spyOn(performance, "now");onTestFinished(() => now.mockRestore());now.mockReturnValue(1000);- tap(0);+ tapDivider(editor, 0);now.mockReturnValue(2000);- tap(0);+ tapDivider(editor, 0);expect(getTableColumnsWidths(table)).toEqual(widths);now.mockReturnValue(2100);- tap(0);+ tapDivider(editor, 0);expect(getTableColumnsWidths(table)).toEqual(["150px", "150px", "100px"]);});+ it("takes a tap after a drag, a press or on another table as a first tap", async () => {+ const widths = ["100px", "200px", "100px"];+ const tables = [["a[]", "b", "c"], ["d", "e", "f"]]+ .map((row) => resizedTable([row], widths, 'tableWidth="400px"'));+ await showBar(editor, tables.join(""));+ const first = firstTable(editor);+ const second = editor.model.document.getRoot()?.getChild(1);+ if (!second?.is("element", "table")) {+ throw new Error("The second root child is not a table.");+ }+ const now = vi.spyOn(performance, "now").mockReturnValue(1000);+ onTestFinished(() => now.mockRestore());++ tapDivider(editor, 0);+ const { x, y } = press(dividers(editor)[0]);+ moveTo(x + 30, y);+ release();+ const dragged = getTableColumnsWidths(first);+ tapDivider(editor, 0);+ const afterDrag = getTableColumnsWidths(first);++ tapDivider(editor, 1);+ press(segments(editor)[0]);+ release();+ tapDivider(editor, 1);+ const afterPress = getTableColumnsWidths(first);++ tapDivider(editor, 0);+ editor.model.change((writer) => writer.setSelection(second.getNodeByPath([0, 0]), 0));+ await frames();+ tapDivider(editor, 0);++ const afterSwitch = [getTableColumnsWidths(first), getTableColumnsWidths(second)];+ expect([afterDrag, afterPress, ...afterSwitch])+ .toEqual([dragged, dragged, dragged, widths]);+ });+it("resizes towards the left in right-to-left content", async () => {const rtl = await createTestEditor(PLUGINS, { language: { content: "ar" } });setEditableWidth(rtl, 400);@@ -590,6 +623,11 @@ function release(pointerId = 1) {window.dispatchEvent(new PointerEvent("pointerup", { pointerId }));}+function tapDivider(editor: Editor, column: number) {+ press(dividers(editor)[column]);+ release();+}+function barView(editor: Editor) {return editor.plugins.get(TableColumnBar).view;}diff --git a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.tsindex 6830b0965a..9ab92a1a1d 100644--- a/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts+++ b/packages/ckeditor5/src/plugins/table_column_bar/table_column_bar.ts@@ -56,7 +56,7 @@ export default class TableColumnBar extends Plugin {private resizedColumn: number | null = null;private cancelGesture: (() => void) | null = null;private menu: DropdownView | null = null;- private lastTap: { column: number; time: number } | null = null;+ private lastTap: Tap | null = null;private readonly domEmitter = new (DomEmitterMixin())();private readonly resizeObserver = new ResizeObserver(() => this.scheduleUpdate(true));@@ -289,9 +289,12 @@ export default class TableColumnBar extends Plugin {this.menu.isOpen = false;}+ // A double tap is two taps on a divider with no other gesture on the bar between them.+ const previousTap = this.lastTap;+ this.lastTap = null;const column = Number(handle.dataset.column);if (handle.classList.contains("ck-table-column-bar__divider")) {- this.startResize(event, column, target);+ this.startResize(event, column, target, previousTap);} else {this.startPress(event, column, target);}@@ -333,9 +336,15 @@ export default class TableColumnBar extends Plugin {/*** Resizes `column` while the divider at its end is dragged. The table shows each width without- * a change to the model, which takes the width at which the divider is released.+ * a change to the model, which takes the width at which the divider is released. A tap that+ * follows `previousTap` on the same divider evens out the two columns beside it.*/- private startResize(event: PointerEvent, column: number, target: BarTarget) {+ private startResize(+ event: PointerEvent,+ column: number,+ target: BarTarget,+ previousTap: Tap | null+ ) {const editor = this.editor;const { table, domContainer, domTable } = target;const direction = editor.locale.contentLanguageDirection === "rtl" ? -1 : 1;@@ -410,8 +419,14 @@ export default class TableColumnBar extends Plugin {this.commit(target, replaceAt(widths, column, width));}finish();- const isPair = column + 1 < widths.length;- if (!isMoved && this.isDoubleTap(column) && isPair) {+ if (isMoved) {+ return;+ }++ const tap = { table, column, time: performance.now() };+ if (!isDoubleTap(previousTap, tap)) {+ this.lastTap = tap;+ } else if (column + 1 < widths.length) {distributeColumns(editor, table, { first: column, last: column + 1 });}},@@ -520,15 +535,6 @@ export default class TableColumnBar extends Plugin {this.view.placeMenu(column);this.menu.isOpen = true;}-- /** Whether a tap on the divider of `column` follows another one on it closely enough. */- private isDoubleTap(column: number) {- const now = performance.now();- const lastTap = this.lastTap;- const isDoubleTap = lastTap?.column === column && now - lastTap.time < DOUBLE_TAP_MS;- this.lastTap = isDoubleTap ? null : { column, time: now };- return isDoubleTap;- }}interface BarTarget {@@ -548,6 +554,13 @@ interface ResizePreview {isClassAdded: boolean;}+/** A tap on the divider at the end of `column`, at `time` from `performance.now()`. */+interface Tap {+ table: ModelElement;+ column: number;+ time: number;+}+interface ClientBox {left: number;top: number;@@ -647,6 +660,12 @@ export function findSnapPoint(width: number, points: readonly number[]): numberreturn closest;}+/** Whether `tap` follows `previousTap` on the same divider closely enough. */+function isDoubleTap(previousTap: Tap | null, tap: Tap) {+ return previousTap?.table === tap.table && previousTap.column === tap.column+ && tap.time - previousTap.time < DOUBLE_TAP_MS;+}+function findColgroup(viewTable: ViewElement) {return Array.from(viewTable.getChildren()).find((child): child is ViewElement => child.is("element", "colgroup"));diff --git a/packages/ckeditor5/src/theme/table_scroll.css b/packages/ckeditor5/src/theme/table_scroll.cssindex 926b778c9b..87f29a222e 100644--- a/packages/ckeditor5/src/theme/table_scroll.css+++ b/packages/ckeditor5/src/theme/table_scroll.css@@ -27,7 +27,6 @@}}-/* The widget controls of a scrolling table stay in view as sticky flex items that take no space. */.ck.ck-editor__editable.ck-content figure.table.ck-widget.ck-table-overflowing {> figcaption {position: sticky;diff --git a/packages/share-theme/src/index.spec.ts b/packages/share-theme/src/index.spec.tsindex 9d05ce267a..b2175504c1 100644--- a/packages/share-theme/src/index.spec.ts+++ b/packages/share-theme/src/index.spec.ts@@ -99,7 +99,8 @@ describe("share theme entry", () => {it("enhances a text note's content and reveals the tab panel the address points to", () => {document.body.className = "type-text";- document.body.innerHTML = `<div id="content" data-tab-title-placeholder="Tab"></div>`;+ document.body.innerHTML = `<div id="content" data-tab-title-placeholder="Tab">`+ + `<blockquote><figure class="include-note"></figure></blockquote></div>`;const panel = { scrollIntoView: vi.fn() };mocks.revealFragment.mockReturnValue(panel);@@ -109,7 +110,8 @@ describe("share theme entry", () => {expect(mocks.setupMermaid).toHaveBeenCalledOnce();expect(mocks.setupMath).toHaveBeenCalledOnce();expect(mocks.enhanceLinkPreviews).toHaveBeenCalledWith(document.body);- expect(mocks.applyTableScroll).toHaveBeenCalledWith(content);+ expect(mocks.applyTableScroll.mock.calls)+ .toEqual([ [ content ], [ content?.querySelector(".include-note") ] ]);expect(mocks.applyTabs).toHaveBeenCalledWith(content, { placeholder: "Tab" });expect(panel.scrollIntoView).toHaveBeenCalledOnce();diff --git a/packages/share-theme/src/index.ts b/packages/share-theme/src/index.tsindex d0b24d0817..a7aa44e100 100644--- a/packages/share-theme/src/index.ts+++ b/packages/share-theme/src/index.ts@@ -91,10 +91,16 @@ document.addEventListener("DOMContentLoaded", () => {whenActivated(() => $try(() => showLinkedBlocks(diagrams)));});+/** Sizes the tables of the content, and those of each note it includes, as the app does. */function setupTableScroll() {const content = document.getElementById("content");- if (content) {- applyTableScroll(content);+ if (!content) {+ return;+ }++ applyTableScroll(content);+ for (const embed of content.querySelectorAll(".include-note")) {+ applyTableScroll(embed);}}