Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/tabs: limit caption width

Adorian Doran164e4eb79816
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
index 895b65e0b8..a635593ad0 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
@@ -228,6 +228,13 @@ export default class TabsEditing extends Plugin {
element: title,
keepOnFocus: true
});
+ // A title clips its overflow, so it shows its start again once the caret leaves.
+ title.on("change:isFocused", (_evt, _name, isFocused) => {
+ const domTitle = editor.editing.view.domConverter.mapViewToDom(title);
+ if (!isFocused && domTitle) {
+ domTitle.scrollLeft = 0;
+ }
+ });
return toWidgetEditable(title, writer, { label: t("Tab title") });
}
});
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
index f9c641bc3d..cf1eccd9a1 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
@@ -43,6 +43,13 @@ function textTopOf(element: Element) {
return range.getBoundingClientRect().top;
}
+/** The height of the text of `element`, which grows with each line it wraps onto. */
+function textHeightOf(element: Element) {
+ const range = document.createRange();
+ range.selectNodeContents(element);
+ return range.getBoundingClientRect().height;
+}
+
/** Where the titles of a tabs block sit around the panel of its first tab, and their corners. */
function measure(block: Element | null, alignment: TabAlignment) {
expect(block).not.toBeNull();
@@ -119,6 +126,53 @@ describe("tabs layout styles", () => {
}
});
+ it("clips a long title at 30% of the block and scrolls it back after editing", async () => {
+ const long = tab("A title far too long for a third of the block", "<p>Body</p>");
+ const html = `<div class="trilium-tabs">${long}${tab("B", "")}</div>`;
+ const container = document.createElement("div");
+ container.className = "ck-content";
+ container.style.width = "600px";
+ container.innerHTML = html;
+ document.body.appendChild(container);
+ onTestFinished(() => container.remove());
+ applyTabs(container, { placeholder: "Tab title" });
+
+ const editor = await createTestEditor([Essentials, Paragraph, Tabs]);
+ const editable = editor.ui.view.editable.element as HTMLElement;
+ editable.style.width = "600px";
+ editor.setData(html);
+
+ const blocks = [container, editable].map(root => root.querySelector(".trilium-tabs"));
+ for (const block of blocks) {
+ const [long, short] = [...block?.querySelectorAll(".trilium-tab-title") ?? []];
+ expect(long).toBeDefined();
+ expect(short).toBeDefined();
+ const width = block?.getBoundingClientRect().width ?? NaN;
+ expect(long.getBoundingClientRect().width / width).toBeCloseTo(0.3, 2);
+ expect(long.scrollWidth).toBeGreaterThan(long.clientWidth);
+ expect(textHeightOf(long)).toBe(textHeightOf(short));
+ }
+
+ const title = editable.querySelector(".trilium-tab-title") as HTMLElement;
+ editor.editing.view.document.isFocused = true;
+ editor.model.change(writer => {
+ const root = editor.model.document.getRoot();
+ if (root) {
+ writer.setSelection(writer.createPositionFromPath(root, [0, 0, 0, 0]));
+ }
+ });
+ title.scrollLeft = 40;
+ expect(title.scrollLeft).toBe(40);
+
+ editor.model.change(writer => {
+ const root = editor.model.document.getRoot();
+ if (root) {
+ writer.setSelection(writer.createPositionFromPath(root, [0, 0, 1, 0, 0]));
+ }
+ });
+ expect(title.scrollLeft).toBe(0);
+ });
+
it("outlines the innermost block holding the caret and rings only a title", async () => {
const editor = await createTestEditor([Essentials, Paragraph, Tabs]);
const editable = editor.ui.view.editable.element as HTMLElement;
diff --git a/packages/ckeditor5/src/theme/tabs.css b/packages/ckeditor5/src/theme/tabs.css
index 8b52c8fa34..7953e0f540 100644
--- a/packages/ckeditor5/src/theme/tabs.css
+++ b/packages/ckeditor5/src/theme/tabs.css
@@ -27,14 +27,17 @@
& > .trilium-tab > .trilium-tab-title {
order: 0;
+ box-sizing: border-box;
min-width: 4em;
- max-width: 100%;
+ max-width: 30%;
margin: 0 0 var(--_tabs-delimiter-width);
padding: 6px 1rem;
border-radius: var(--_tabs-border-radius) var(--_tabs-border-radius) 0 0;
background: var(--_tabs-inactive-background);
+ overflow: hidden;
color: var(--_tabs-title-color);
font-weight: 500;
+ white-space: nowrap;
cursor: pointer;
&:hover {