Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: fix some issues

Adorian Doranf39c3a2ad258
diff --git a/packages/ckeditor5/src/plugins/containers/container_content.ts b/packages/ckeditor5/src/plugins/containers/container_content.ts
index dec35eaa27..300ffc795c 100644
--- a/packages/ckeditor5/src/plugins/containers/container_content.ts
+++ b/packages/ckeditor5/src/plugins/containers/container_content.ts
@@ -38,7 +38,8 @@ export function moveToSibling(writer: ModelWriter, element: ModelElement, siblin
/**
* Returns the innermost `name` element around a non-collapsed selection, with the first and last
* of its children that the selection touches, or `null` when the selection ends outside it. A
- * selection that ends at the start of a child leaves that child out.
+ * selection that ends at the start of a child leaves that child out. A touched list item counts
+ * with all of its blocks.
*/
export function getSelectedChildren(editor: Editor, name: string): SelectedChildren | null {
const model = editor.model;
@@ -59,7 +60,11 @@ export function getSelectedChildren(editor: Editor, name: string): SelectedChild
if (!first || !last) {
return null;
}
- return { parent, first, last };
+ return {
+ parent,
+ first: getListItemEnd(first, "previousSibling"),
+ last: getListItemEnd(last, "nextSibling")
+ };
}
/**
@@ -119,3 +124,18 @@ function getChildAt(parent: ModelElement, position: ModelPosition, side: "before
return position.getAncestors().find((ancestor): ancestor is ModelElement =>
ancestor.is("element") && ancestor.parent === parent);
}
+
+/**
+ * Returns the block on `side` of `node` that ends the list item of `node`, or `node` when it is not
+ * a list block.
+ */
+function getListItemEnd(node: ModelNode, side: "previousSibling" | "nextSibling"): ModelNode {
+ const itemId = node.getAttribute("listItemId");
+ let end = node;
+ let sibling = end[side];
+ while (itemId && sibling?.getAttribute("listItemId") === itemId) {
+ end = sibling;
+ sibling = sibling[side];
+ }
+ return end;
+}
diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts
index 19fb2623a5..d89d16a41a 100644
--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts
+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts
@@ -447,6 +447,19 @@ describe("multicolumn commands", () => {
"<paragraph>B</paragraph>", "<paragraph>[A]</paragraph><paragraph>C</paragraph>"));
});
+ it("moves every block of a list item that the selection touches", () => {
+ const item = (id: string, text: string) => "<paragraph listIndent=\"0\" " +
+ `listItemId="${id}" listType="bulleted">${text}</paragraph>`;
+ const itemA = item("a", "A1") + item("a", "A[2]") + item("a", "A3");
+ setModelData(editor.model, columns("1-1",
+ itemA + item("b", "B"), "<paragraph>C</paragraph>"));
+
+ editor.execute("moveToLayoutColumn", { target: "next" });
+
+ expect(modelWithSelection()).toBe(columns("1-1",
+ item("b", "B"), `${itemA}<paragraph>C</paragraph>`));
+ });
+
it("moves a selected object", () => {
setModelData(editor.model, columns("1-1",
`<paragraph>A</paragraph>[${TABLE}]`, "<paragraph>C</paragraph>"));
diff --git a/packages/ckeditor5/src/plugins/scrollable_bar.spec.ts b/packages/ckeditor5/src/plugins/scrollable_bar.spec.ts
index 7d3c0043b5..0c6baa531b 100644
--- a/packages/ckeditor5/src/plugins/scrollable_bar.spec.ts
+++ b/packages/ckeditor5/src/plugins/scrollable_bar.spec.ts
@@ -114,6 +114,23 @@ describe("scrollable bar", () => {
await expect.poll(() => isOverflowing(bar)).toBe(false);
});
+ it("hides the buttons once the items fit the bar without them", async () => {
+ const { bar, viewport } = renderBar(4);
+ viewport.lastElementChild?.remove();
+ viewport.lastElementChild?.remove();
+ viewport.append(createItem("Item 2", 80));
+ await expect.poll(() => isOverflowing(bar)).toBe(false);
+
+ viewport.append(createItem("Item 3", 120));
+ await expect.poll(() => isOverflowing(bar)).toBe(true);
+ bar.style.width = "380px";
+ await wait(100);
+ expect(isOverflowing(bar)).toBe(true);
+
+ bar.style.width = "420px";
+ await expect.poll(() => isOverflowing(bar)).toBe(false);
+ });
+
it("scrolls by most of its width toward the edge clicked, in either direction", async () => {
document.body.classList.add("motion-disabled");
for (const [ dir, sign ] of [ [ "ltr", 1 ], [ "rtl", -1 ] ] as const) {
diff --git a/packages/ckeditor5/src/plugins/scrollable_bar.ts b/packages/ckeditor5/src/plugins/scrollable_bar.ts
index ae9aa430af..a2b56bacf8 100644
--- a/packages/ckeditor5/src/plugins/scrollable_bar.ts
+++ b/packages/ckeditor5/src/plugins/scrollable_bar.ts
@@ -79,11 +79,11 @@ export function createScrollableBarEdge(
}
/**
- * Makes `bar` scroll: its buttons show while the items overflow `viewport`, and the button of an
- * edge that is reached is disabled. A click on a button scrolls by most of the viewport's width;
- * holding the button down scrolls continuously until it is released. A press on the bar outside
- * the viewport does not move the focus and does not reach the listeners of the bar's ancestors,
- * such as an editor's.
+ * Makes `bar` scroll: its buttons show while the items do not fit the bar without them, and the
+ * button of an edge that is reached is disabled. A click on a button scrolls by most of the
+ * viewport's width; holding the button down scrolls continuously until it is released. A press on
+ * the bar outside the viewport does not move the focus and does not reach the listeners of the
+ * bar's ancestors, such as an editor's.
*/
export function attachScrollableBar(bar: HTMLElement, viewport: HTMLElement): ScrollableBar {
const document = bar.ownerDocument;
@@ -154,7 +154,13 @@ export function attachScrollableBar(bar: HTMLElement, viewport: HTMLElement): Sc
};
const showState = (state: ScrollFadeState) => {
isRtl = state.isRtl;
- bar.classList.toggle(SCROLLABLE_BAR_CLASSES.overflowing, state.overflow > MIN_OVERFLOW);
+ // The edges take their width from the viewport while they show.
+ let overflow = state.overflow;
+ for (const edge of bar.querySelectorAll<HTMLElement>(
+ `:scope > .${SCROLLABLE_BAR_CLASSES.edge}`)) {
+ overflow -= edge.offsetWidth;
+ }
+ bar.classList.toggle(SCROLLABLE_BAR_CLASSES.overflowing, overflow > MIN_OVERFLOW);
const buttons = bar.querySelectorAll<HTMLButtonElement>(
`:scope > .${SCROLLABLE_BAR_CLASSES.edge} > .${SCROLLABLE_BAR_CLASSES.button}`);
for (const button of buttons) {
@@ -174,6 +180,8 @@ export function attachScrollableBar(bar: HTMLElement, viewport: HTMLElement): Sc
return {
reveal(item, { isAnimated = true } = {}) {
+ // A bar that has just been shown still has the buttons it had while hidden.
+ watch.measure();
const distance = getRevealDistance(
viewport.getBoundingClientRect(), item.getBoundingClientRect(), isRtl);
if (distance) {
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
index 40e7d78e2c..39a6d55aff 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
@@ -5,7 +5,7 @@ import { beforeAll, describe, expect, it, onTestFinished } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
import type { TabAlignment } from "./constants.js";
import Tabs from "./tabs.js";
-import { applyTabs } from "./tabs_read_only.js";
+import { applyTabs, revealTab } from "./tabs_read_only.js";
const OPTIONS = { placeholder: "Tab title", scrollLabels: { start: "Back", end: "Next" } };
@@ -327,6 +327,24 @@ describe("tabs layout styles", () => {
expect(edited.viewport.firstElementChild?.textContent).toBe("Step 10 of the guide");
});
+ it("reveals the title of a nested tab that was hidden in saved content", async () => {
+ document.body.classList.add("motion-disabled");
+ onTestFinished(() => document.body.classList.remove("motion-disabled"));
+ const innerTabs = Array.from({ length: 15 }, (_, index) =>
+ tab(`Tab ${index + 1}`, index === 9 ? "<p id=\"target\">x</p>" : ""));
+ const inner = `<div class="trilium-tabs">${innerTabs.join("")}</div>`;
+ const { container } = renderSaved(
+ `<div class="trilium-tabs">${tab("Outer 1", "")}${tab("Outer 2", inner)}</div>`);
+ const { bar, viewport, titles } = barOf(container.querySelectorAll(".trilium-tab")[1]);
+ expect(isOverflowing(bar)).toBe(false);
+
+ revealTab(container.querySelector("#target") as Element);
+ expect(isShown(titles[9], viewport)).toBe(true);
+ await new Promise(resolve => setTimeout(resolve, 100));
+ expect(isOverflowing(bar)).toBe(true);
+ expect(isShown(titles[9], viewport)).toBe(true);
+ });
+
it("stops the title bars of saved content on dispose, and starts them on the next call", () => {
const { container, dispose } = renderSaved(manyTabsHtml());
const { bar } = barOf(container);
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
index b3fff58805..fe30191464 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
@@ -62,14 +62,18 @@ export function revealTab(element: Element) {
return;
}
- let tab = element.closest(`.${CLASSES.tab}`);
- while (tab) {
+ // From the outermost tab, so that each title bar shows when it reveals its title.
+ const tabs: Element[] = [];
+ for (let tab = element.closest(`.${CLASSES.tab}`); tab;
+ tab = tab.parentElement?.closest(`.${CLASSES.tab}`) ?? null) {
+ tabs.unshift(tab);
+ }
+ for (const tab of tabs) {
const block = tab.parentElement;
const title = sectionTitles.get(tab);
if (block && title) {
tabsBlocks.get(block)?.activate(title);
}
- tab = block?.closest(`.${CLASSES.tab}`) ?? null;
}
}