Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): label untitled tabs in read-only notes

Elian Dorancc39d55ac403
diff --git a/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts b/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts
index 66db4b2a62..cc99eea717 100644
--- a/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/read_only_helper.spec.ts
@@ -1,7 +1,9 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { applyTabs } from "./read_only_helper";
+vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
+
function renderTabs(html: string) {
const container = document.createElement("div");
container.innerHTML = html;
@@ -101,6 +103,23 @@ describe("applyTabs", () => {
container.remove();
});
+ it("labels a title that has no text with the editor's placeholder", () => {
+ const container = renderTabs(
+ `<div class="trilium-tabs">${tab("&nbsp;", "")}${tab("", "")}${tab("Named", "")}</div>`
+ );
+ const label = "text-editor.ck.tab-title";
+ const titles = [...container.querySelectorAll<HTMLElement>(".trilium-tab-title")];
+ expect(titles).toHaveLength(3);
+
+ for (const title of titles.slice(0, 2)) {
+ expect(title.textContent).toBe("");
+ expect(title.dataset.placeholder).toBe(label);
+ expect(title.getAttribute("aria-label")).toBe(label);
+ }
+ expect(titles[2].dataset.placeholder).toBeUndefined();
+ expect(titles[2].hasAttribute("aria-label")).toBe(false);
+ });
+
it("keeps nested tabs blocks independent", () => {
const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "")}</div>`;
const container = renderTabs(
diff --git a/apps/client/src/widgets/type_widgets/text/read_only_helper.ts b/apps/client/src/widgets/type_widgets/text/read_only_helper.ts
index 02e810bf17..19bc14af2f 100644
--- a/apps/client/src/widgets/type_widgets/text/read_only_helper.ts
+++ b/apps/client/src/widgets/type_widgets/text/read_only_helper.ts
@@ -1,3 +1,4 @@
+import { t } from "../../../services/i18n";
import link from "../../../services/link";
import { randomString } from "../../../services/utils";
@@ -20,7 +21,8 @@ export async function applyReferenceLinks(
/**
* Turns the tabs blocks in `container` into working tabs: the first tab of each block shows, and
* a click on a title, Enter or Space shows that tab. The arrow keys, Home and End move between
- * the titles of a block. A block that is already set up keeps its listeners and its active tab.
+ * the titles of a block. A title without text shows the editor's placeholder. A block that is
+ * already set up keeps its listeners and its active tab.
*/
export function applyTabs(container: HTMLElement) {
for (const block of container.querySelectorAll<HTMLElement>(".trilium-tabs")) {
@@ -37,6 +39,13 @@ export function applyTabs(container: HTMLElement) {
continue;
}
+ if (!title.textContent?.trim() && !title.querySelector(":not(br)")) {
+ const placeholder = t("text-editor.ck.tab-title");
+ title.replaceChildren();
+ title.dataset.placeholder = placeholder;
+ title.setAttribute("aria-label", placeholder);
+ }
+
const id = `trilium-tab-${randomString(8)}`;
title.id = `${id}-title`;
title.tabIndex = 0;
diff --git a/packages/ckeditor5/src/theme/tabs.css b/packages/ckeditor5/src/theme/tabs.css
index 951ae93564..37799bca06 100644
--- a/packages/ckeditor5/src/theme/tabs.css
+++ b/packages/ckeditor5/src/theme/tabs.css
@@ -82,7 +82,14 @@
}
}
-.ck-content:not(.ck-editor__editable) .trilium-tabs > .trilium-tab > .trilium-tab-title:focus-visible {
- outline: 2px solid var(--input-focus-outline-color, var(--ck-color-focus-border));
- outline-offset: -2px;
+.ck-content:not(.ck-editor__editable) .trilium-tabs > .trilium-tab > .trilium-tab-title {
+ &:focus-visible {
+ outline: 2px solid var(--input-focus-outline-color, var(--ck-color-focus-border));
+ outline-offset: -2px;
+ }
+
+ &[data-placeholder]::before {
+ content: attr(data-placeholder);
+ color: var(--ck-color-engine-placeholder-text);
+ }
}