Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tables: add an option to disable sticky header rows

Adorian Doran1d5326448658
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 401fa47d99..75be00748a 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3461,7 +3461,9 @@
"auto_link_previews_enabled": "Autogenerate link previews",
"auto_link_previews_description": "Automatically turn a detected link into a preview showing the title, description and image of the web page, and save the tabs sent by the web clipper as inline previews. Inserting a preview from the toolbar always works, regardless of this setting.",
"html_support_enabled": "Preserve unsupported HTML tags",
- "html_support_description": "Keep the HTML tags that the editor has no feature of its own for, as listed under Options → Other → HTML import tags. They appear as blocks that cannot be edited directly, and an unrecognised tag may not be visible at all while editing. With this off, such tags are dropped from a note the first time it is edited and saved; importing is unaffected either way."
+ "html_support_description": "Keep the HTML tags that the editor has no feature of its own for, as listed under Options → Other → HTML import tags. They appear as blocks that cannot be edited directly, and an unrecognised tag may not be visible at all while editing. With this off, such tags are dropped from a note the first time it is edited and saved; importing is unaffected either way.",
+ "sticky_table_headers_enabled": "Use sticky header rows for tables",
+ "sticky_table_headers_description": "Keep the header rows of a table at the top of the note while scrolling through the rest of the table."
},
"table_view": {
"new-row": "New row",
diff --git a/apps/client/src/widgets/containers/root_container.spec.ts b/apps/client/src/widgets/containers/root_container.spec.ts
index 10cebc2e2e..de59ab1573 100644
--- a/apps/client/src/widgets/containers/root_container.spec.ts
+++ b/apps/client/src/widgets/containers/root_container.spec.ts
@@ -1,5 +1,7 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import LoadResults from "../../services/load_results";
+import options from "../../services/options";
import RootContainer from "./root_container";
/**
@@ -23,6 +25,7 @@ describe("RootContainer", () => {
afterEach(() => {
Object.defineProperty(window, "visualViewport", { value: undefined, configurable: true });
window.glob = { ...window.glob, device: "desktop" };
+ document.body.classList.remove("sticky-table-headers-disabled");
});
/** Applies a new window/viewport geometry and lets the container react to it. */
@@ -67,4 +70,18 @@ describe("RootContainer", () => {
resizeTo({ width: 800, height: 400, viewportHeight: 200 });
expect($widget.hasClass("virtual-keyboard-opened")).toBe(true);
});
+
+ it("marks the body while sticky table headers are off, and follows a change", () => {
+ const isMarked = () => document.body.classList.contains("sticky-table-headers-disabled");
+ options.set("textNoteStickyTableHeadersEnabled", "true");
+ const rootContainer = new RootContainer(false);
+ rootContainer.render();
+ expect(isMarked()).toBe(false);
+
+ options.set("textNoteStickyTableHeadersEnabled", "false");
+ const loadResults = new LoadResults([]);
+ loadResults.addOption("textNoteStickyTableHeadersEnabled");
+ rootContainer.entitiesReloadedEvent({ loadResults });
+ expect(isMarked()).toBe(true);
+ });
});
diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.ts
index 9b14afd806..49e866a01a 100644
--- a/apps/client/src/widgets/containers/root_container.ts
+++ b/apps/client/src/widgets/containers/root_container.ts
@@ -57,6 +57,7 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
this.#setShadows();
this.#setBackdropEffects();
this.#setMonospaceLigatures();
+ this.#setStickyTableHeaders();
updateThemeCapabilities();
this.#setLocaleAndDirection(options.get("locale"));
// The fonts stylesheet went out with the page; the files the user's own fonts are stored in
@@ -98,6 +99,10 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
this.#setMonospaceLigatures();
}
+ if (loadResults.isOptionReloaded("textNoteStickyTableHeadersEnabled")) {
+ this.#setStickyTableHeaders();
+ }
+
if (loadResults.isOptionReloaded("maxContentWidth")
|| loadResults.isOptionReloaded("centerContent")) {
@@ -167,6 +172,11 @@ export default class RootContainer extends FlexContainer<BasicWidget> {
document.body.classList.toggle("monospace-ligatures-disabled", !enabled);
}
+ #setStickyTableHeaders() {
+ const enabled = options.is("textNoteStickyTableHeadersEnabled");
+ document.body.classList.toggle("sticky-table-headers-disabled", !enabled);
+ }
+
#setExperimentalFeatures() {
for (const featureId of getEnabledExperimentalFeatureIds()) {
document.body.classList.add(`experimental-feature-${featureId}`);
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.spec.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.spec.tsx
index 2b654c6ea2..07ced1c07a 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.spec.tsx
@@ -102,6 +102,20 @@ describe("the formatting toolbar", () => {
});
});
+describe("the editor features", () => {
+ it("offers sticky table header rows after link previews, and writes the switch back", () => {
+ open();
+ const labels = optionLabels();
+ expect(labels.indexOf("editorfeatures.sticky_table_headers_enabled"))
+ .toBe(labels.indexOf("editorfeatures.auto_link_previews_enabled") + 1);
+
+ const stickyHeaders = toggle("sticky-table-headers-enabled");
+ expect(stickyHeaders).not.toBeNull();
+ act(() => stickyHeaders?.click());
+ expect(mocks.saved).toContainEqual([ "textNoteStickyTableHeadersEnabled", true ]);
+ });
+});
+
describe("the code block appearance", () => {
it("asks for one theme when it is not to follow the app, and for a pair when it is", () => {
open();
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
index 830930b7ba..46beac1416 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -150,6 +150,8 @@ function EditorFeatures() {
const [slashCommandsEnabled, setSlashCommandsEnabled] = useTriliumOptionBool("textNoteSlashCommandsEnabled");
const [contentHintsEnabled, setContentHintsEnabled] = useTriliumOptionBool("textNoteContentHintsEnabled");
const [autoLinkPreviewsEnabled, setAutoLinkPreviewsEnabled] = useTriliumOptionBool("textNoteAutoLinkPreviewsEnabled");
+ const [stickyTableHeadersEnabled, setStickyTableHeadersEnabled] =
+ useTriliumOptionBool("textNoteStickyTableHeadersEnabled");
const [htmlSupportEnabled, setHtmlSupportEnabled] = useTriliumOptionBool("textNoteHtmlSupportEnabled");
return (
@@ -178,6 +180,17 @@ function EditorFeatures() {
<FormToggle currentValue={autoLinkPreviewsEnabled} onChange={setAutoLinkPreviewsEnabled} />
</OptionCardSection>
+ <OptionCardSection
+ name="sticky-table-headers-enabled"
+ label={t("editorfeatures.sticky_table_headers_enabled")}
+ description={t("editorfeatures.sticky_table_headers_description")}
+ >
+ <FormToggle
+ currentValue={stickyTableHeadersEnabled}
+ onChange={setStickyTableHeadersEnabled}
+ />
+ </OptionCardSection>
+
<OptionCardSection
name="note-completion-enabled"
label={t("editorfeatures.note_completion_enabled")}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
index b2208bb77a..a9e69e217b 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Tables.html
@@ -204,6 +204,10 @@ src="3_Tables_image.png" width="384" height="100">
<p>To turn rows into header rows, use <em>Set as header</em> in the <a href="#context-menu">context menu</a>,
or <em>Header row</em> in the <span class="tn-icon cke cke-table-row"></span> button
of the formatting toolbar.</p>
+<p>To let header rows scroll away with the rest of the table, turn off <a
+ class="reference-link" href="#root/_help_4TIF1oA4VQRO">Options</a> → <em>Text Notes</em> → <em>Features</em> → <em>Use sticky header rows for tables</em>.
+ The setting applies to the current device only, and shared pages keep their
+ sticky header rows.</p>
<h2>Inserting new rows and new columns</h2>
<ul>
<li>To insert a new column, click on a desired location, then press the&nbsp;<span
diff --git a/docs/User Guide/User Guide/Note Types/Text/Tables.md b/docs/User Guide/User Guide/Note Types/Text/Tables.md
index d32f19f76e..719c8e22ef 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Tables.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Tables.md
@@ -103,6 +103,8 @@ Since v0.107.0, the header rows of a table stay visible at the top of the note w
To turn rows into header rows, use _Set as header_ in the [context menu](#context-menu), or _Header row_ in the <span class="tn-icon cke cke-table-row"></span> button of the formatting toolbar.
+To let header rows scroll away with the rest of the table, turn off <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/UI%20Elements/Options.md">Options</a> → _Text Notes_ → _Features_ → _Use sticky header rows for tables_. The setting applies to the current device only, and shared pages keep their sticky header rows.
+
## Inserting new rows and new columns
* To insert a new column, click on a desired location, then press the <span class="tn-icon cke cke-table-column"></span> button from the formatting toolbar and select _Insert column left or right._
diff --git a/packages/ckeditor5/src/plugins/table_sticky_header_read_only.spec.ts b/packages/ckeditor5/src/plugins/table_sticky_header_read_only.spec.ts
index f79508b05c..721a496095 100644
--- a/packages/ckeditor5/src/plugins/table_sticky_header_read_only.spec.ts
+++ b/packages/ckeditor5/src/plugins/table_sticky_header_read_only.spec.ts
@@ -56,6 +56,24 @@ describe("applyStickyTableHeaders", () => {
expect(bordersOf(thead)).toBe("none");
});
+ it("leaves the header rows in place while `sticky-table-headers-disabled` is set", async () => {
+ document.body.classList.add("sticky-table-headers-disabled");
+ onTestFinished(() => document.body.classList.remove("sticky-table-headers-disabled"));
+ const { scroller, content } = renderContent(tableFigure(1, 20));
+ applyStickyTableHeaders(content);
+ await settle();
+
+ const { thead } = parts(content);
+ await scrollTo(scroller, offsetIn(scroller, thead) + 150);
+ expect(topIn(scroller, thead)).toBeCloseTo(-150, 0);
+ expect(getComputedStyle(thead).translate).toBe("none");
+
+ // Content already on screen follows the class.
+ document.body.classList.remove("sticky-table-headers-disabled");
+ await settle();
+ expect(topIn(scroller, thead)).toBeCloseTo(0, 0);
+ });
+
it("measures the header rows again when the table grows while they stick", async () => {
const { scroller, content } = renderContent(tableFigure(1, 20));
applyStickyTableHeaders(content);
diff --git a/packages/ckeditor5/src/theme/table_sticky_header.css b/packages/ckeditor5/src/theme/table_sticky_header.css
index a5017e294d..517e384c94 100644
--- a/packages/ckeditor5/src/theme/table_sticky_header.css
+++ b/packages/ckeditor5/src/theme/table_sticky_header.css
@@ -1,7 +1,7 @@
/* Without a scroll timeline, an `animation` runs on time and ends at once. Longhands only: Lightning
CSS expands the `animation` shorthand with a `0s` duration, which ends a scroll-driven one too. */
@supports (view-timeline-name: --table-header) {
- .ck-content figure.table:not(.layout-table) {
+ .ck-content figure.table:not(.layout-table, .sticky-table-headers-disabled *) {
view-timeline: --table-header block;
view-timeline-inset: auto;
diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts
index b9be46852a..09ab7d26df 100644
--- a/packages/commons/src/lib/options_interface.ts
+++ b/packages/commons/src/lib/options_interface.ts
@@ -292,6 +292,11 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActi
textNoteContentHintsEnabled: boolean;
/** Whether a URL typed or pasted into a text note is automatically turned into a link preview. The "Link preview" dialog is unaffected and always inserts one on request. */
textNoteAutoLinkPreviewsEnabled: boolean;
+ /**
+ * Whether the header rows of a table stay at the top of the note while the rest of the table
+ * scrolls past.
+ */
+ textNoteStickyTableHeadersEnabled: boolean;
/**
* Whether the editor preserves the HTML tags it has no editing feature of its own for — the
* ones named by `allowedHtmlTags`, carried through CKEditor's General HTML Support.
diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.ts
index 0524567801..1ea835851c 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -141,6 +141,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>([
"mathFieldEnabled",
"textNoteContentHintsEnabled",
"textNoteAutoLinkPreviewsEnabled",
+ "textNoteStickyTableHeadersEnabled",
"textNoteHtmlSupportEnabled",
"clipboardImageEmbedEnabled",
"layoutOrientation",
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index 092c24ef77..399a1c639b 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -328,6 +328,7 @@ const defaultOptions: DefaultOption[] = [
{ name: "mathFieldEnabled", value: "true", isSynced: true },
{ name: "textNoteContentHintsEnabled", value: "true", isSynced: true },
{ name: "textNoteAutoLinkPreviewsEnabled", value: "true", isSynced: true },
+ { name: "textNoteStickyTableHeadersEnabled", value: "true", isSynced: false },
// Off: the tags this carries are the ones the editor has no feature for, and GHS's handling of
// them is worse than their absence. See `textNoteHtmlSupportEnabled` for what turning it off costs.
{ name: "textNoteHtmlSupportEnabled", value: "false", isSynced: true },