Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/multicolumn layout: fix copying block references inside the columns

Adorian Doranad9e094aeb25
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
index 8770f3cae8..b73d4fa808 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Block references.html
@@ -15,7 +15,10 @@
<li>Go to the note where to insert the link and press <kbd>Ctrl</kbd>+<kbd>V</kbd>.</li>
</ol>
<p>Inside a <a class="reference-link" href="#root/_help_EK2ckIZ74tbT">Content tabs</a> block,
- the reference points to the whole block. To link to a single tab, use <em>Copy link to tab</em> instead.</p>
+ the reference points to the whole block. To link to a single tab, use <em>Copy link to tab</em> instead.
+ Inside a <a class="reference-link" href="#root/_help_wYDzQ1fBFgfT">Multicolumn layout</a>,
+ the reference points to the blocks in the column, and an include shows
+ them without the layout around them.</p>
<p>The link shows the title of the note, followed by the beginning of the
referenced text. A link to blocks of the same note shows only the beginning
of the referenced text.</p>
diff --git a/docs/User Guide/User Guide/Note Types/Text/Block references.md b/docs/User Guide/User Guide/Note Types/Text/Block references.md
index 7ce99c500a..5c620fa77a 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Block references.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Block references.md
@@ -8,7 +8,7 @@ Block references are [links](Links.md) to a block of a text note, such as a para
3. Select _Copy reference to this block_, or _Copy reference to these blocks_ for a selection. The referenced blocks flash briefly.
4. Go to the note where to insert the link and press <kbd>Ctrl</kbd>+<kbd>V</kbd>.
-Inside a <a class="reference-link" href="Content%20tabs.md">Content tabs</a> block, the reference points to the whole block. To link to a single tab, use _Copy link to tab_ instead.
+Inside a <a class="reference-link" href="Content%20tabs.md">Content tabs</a> block, the reference points to the whole block. To link to a single tab, use _Copy link to tab_ instead. Inside a <a class="reference-link" href="Multicolumn%20layout.md">Multicolumn layout</a>, the reference points to the blocks in the column, and an include shows them without the layout around them.
The link shows the title of the note, followed by the beginning of the referenced text. A link to blocks of the same note shows only the beginning of the referenced text.
diff --git a/packages/ckeditor5/src/plugins/block_reference/block_reference.spec.ts b/packages/ckeditor5/src/plugins/block_reference/block_reference.spec.ts
index 818573276d..1f1a7053ae 100644
--- a/packages/ckeditor5/src/plugins/block_reference/block_reference.spec.ts
+++ b/packages/ckeditor5/src/plugins/block_reference/block_reference.spec.ts
@@ -1,12 +1,14 @@
import {
_getModelData as getModelData, _setModelData as setModelData, BalloonEditor, BlockQuote,
- BlockToolbar, type ClassicEditor, CodeBlock, Essentials, Heading, List, Paragraph, Table
+ BlockToolbar, type ClassicEditor, CodeBlock, Essentials, Heading, List, type ModelNode,
+ Paragraph, Table
} from "ckeditor5";
import { describe, expect, it, vi } from "vitest";
import { createTestEditor, createTestEditorOf } from "../../../test/editor-kit.js";
import { installGlobMock } from "../../../test/globals-test-kit.js";
import BlockDragHandle from "../block_drag_handle.js";
+import Multicolumn from "../multicolumn/multicolumn.js";
import BlockReference from "./block_reference.js";
const PLUGINS = [
@@ -124,6 +126,24 @@ describe("BlockReference", () => {
expect(getBlock(editor, 0).getAttribute("blockId")).toBe(reference?.startId);
});
+ it("references a block in a column of a layout, and a table there as a whole", async () => {
+ const editor = await createTestEditor([ ...PLUGINS, Multicolumn ]);
+ setModelData(editor.model,
+ "<multicolumnLayout columnRatios=\"1-1\"><multicolumnColumn>"
+ + "<paragraph>one</paragraph><paragraph>two</paragraph></multicolumnColumn>"
+ + "<multicolumnColumn><table><tableRow><tableCell><paragraph>cell</paragraph>"
+ + "</tableCell></tableRow></table></multicolumnColumn></multicolumnLayout>"
+ );
+ const layout = getBlock(editor, 0);
+ const paragraph = layout.getNodeByPath([ 0, 1 ]);
+ const table = layout.getNodeByPath([ 1, 0 ]);
+
+ expect(getReferenced(editor, paragraph, "end")).toBe(paragraph);
+ expect(getReferenced(editor, layout.getNodeByPath([ 1, 0, 0, 0, 0 ]), "end"))
+ .toBe(table);
+ expect(getReferenced(editor, layout, "on")).toBe(layout);
+ });
+
it("is disabled in read-only mode", async () => {
const editor = await createTestEditor(PLUGINS);
setModelData(editor.model, "<paragraph>[]one</paragraph>");
@@ -355,6 +375,15 @@ function getBlock(editor: ClassicEditor, index: number) {
return block;
}
+/** The element that gets the id when a reference is copied with `node` selected at `place`. */
+function getReferenced(editor: ClassicEditor, node: ModelNode, place: "end" | "on") {
+ editor.model.change((writer) => writer.setSelection(node, place));
+ const id = editor.execute("assignBlockReference")?.startId;
+ const root = editor.model.document.getRoot();
+ const items = root ? Array.from(editor.model.createRangeIn(root).getItems()) : [];
+ return items.find((item) => item.is("element") && item.getAttribute("blockId") === id);
+}
+
function getHandle(element: HTMLElement | undefined) {
if (!element) {
throw new Error("The block handle is not rendered.");
diff --git a/packages/ckeditor5/src/plugins/block_reference/block_reference_editing.ts b/packages/ckeditor5/src/plugins/block_reference/block_reference_editing.ts
index c6da5c4bf8..046e368cae 100644
--- a/packages/ckeditor5/src/plugins/block_reference/block_reference_editing.ts
+++ b/packages/ckeditor5/src/plugins/block_reference/block_reference_editing.ts
@@ -3,6 +3,8 @@ import {
Command, type Model, type ModelElement, type ModelWriter, Plugin, type ViewElement
} from "ckeditor5";
+import { ELEMENTS as MULTICOLUMN_ELEMENTS } from "../multicolumn/constants.js";
+
/** The model attribute with the id of a block. */
export const BLOCK_ID = "blockId";
@@ -120,14 +122,16 @@ export class AssignBlockReferenceCommand extends Command {
/**
* The blocks of the selection that a reference can point at. A block inside an object, such as a
- * table cell, is replaced by the outermost object.
+ * table cell, is replaced by the outermost object. A multicolumn layout does not count, so a block
+ * in one of its columns is referenced itself.
*/
export function getReferenceBlocks(model: Model) {
const blocks: ModelElement[] = [];
for (const block of model.document.selection.getSelectedBlocks()) {
const outerObject = block.getAncestors()
.find((ancestor): ancestor is ModelElement =>
- ancestor.is("element") && model.schema.isObject(ancestor));
+ ancestor.is("element") && model.schema.isObject(ancestor)
+ && !ancestor.is("element", MULTICOLUMN_ELEMENTS.layout));
const target = outerObject ?? block;
if (!blocks.includes(target) && model.schema.checkAttribute(target, BLOCK_ID)) {
blocks.push(target);
diff --git a/packages/ckeditor5/src/plugins/multicolumn/constants.ts b/packages/ckeditor5/src/plugins/multicolumn/constants.ts
index 3f8c09da3c..a5ee942e36 100644
--- a/packages/ckeditor5/src/plugins/multicolumn/constants.ts
+++ b/packages/ckeditor5/src/plugins/multicolumn/constants.ts
@@ -1,3 +1,5 @@
+import { MULTICOLUMN_LAYOUT_CLASS } from "@triliumnext/commons";
+
/** Model element names of the multicolumn layout. */
export const ELEMENTS = {
layout: "multicolumnLayout",
@@ -7,7 +9,7 @@ export const ELEMENTS = {
/** Model attribute with the column weights of a layout, such as `1-3`. */
export const RATIOS_ATTRIBUTE = "columnRatios";
-export const LAYOUT_CLASS = "trilium-multicolumn-layout";
+export const LAYOUT_CLASS = MULTICOLUMN_LAYOUT_CLASS;
export const RATIOS_DATA_ATTRIBUTE = "data-trilium-column-ratios";
/** Custom property that marks the editing-view widget element of a layout. */
diff --git a/packages/commons/src/lib/block_reference.spec.ts b/packages/commons/src/lib/block_reference.spec.ts
index 05a71d7256..54193bc470 100644
--- a/packages/commons/src/lib/block_reference.spec.ts
+++ b/packages/commons/src/lib/block_reference.spec.ts
@@ -8,6 +8,7 @@ import {
getEditableBlockRun,
getListItemNumber,
isValidBlockId,
+ MULTICOLUMN_LAYOUT_CLASS,
parseBlockRange,
resolveBlockRange,
resolveBlockReference,
@@ -144,6 +145,29 @@ describe("sliceToBlockReference", () => {
);
});
+ it("drops a layout left with one column around the blocks, and keeps one with more", () => {
+ const layout = (...columns: BlockNode[]) =>
+ el("section", { class: `${MULTICOLUMN_LAYOUT_CLASS} wide` }, ...columns);
+ const target = block("p", "b", text("B"));
+ const inner = layout(
+ el("section", {}, el("p", {}, text("A")), target),
+ el("section", {}, el("p", {}, text("C")))
+ );
+ const root = el("div", {}, layout(el("section", {}, inner), el("section", {})));
+
+ expect(sliceToBlockReference(root, "b")).toBe(true);
+ expect(render(root)).toBe(`<div>${render(target)}</div>`);
+
+ const across = layout(
+ el("section", {}, block("p", "x")),
+ el("section", {}, block("p", "y"))
+ );
+ const otherRoot = el("div", {}, across);
+ sliceToBlockReference(otherRoot, "x:y");
+ expect(otherRoot.childNodes).toEqual([ across ]);
+ expect(across.childNodes).toHaveLength(2);
+ });
+
it("changes nothing when a block is missing", () => {
const root = el("div", {}, block("p", "a"), el("p", {}));
@@ -238,6 +262,16 @@ describe("getEditableBlockRun", () => {
});
});
+ it("finds the blocks of a column of a multicolumn layout", () => {
+ const first = block("p", "a");
+ const last = block("p", "b");
+ const column = el("section", {}, el("p", {}), first, last);
+ const root = el("div", {},
+ el("section", { class: MULTICOLUMN_LAYOUT_CLASS }, column, el("section", {})));
+
+ expect(getEditableBlockRun(root, "a:b")).toEqual({ parent: column, first, last });
+ });
+
it("finds nothing for a missing block, or an ancestor that holds other content", () => {
const root = el("div", {},
el("ul", {},
@@ -283,6 +317,17 @@ class TestElement implements BlockNode {
removeFromParent(this);
}
+ replaceWith(...nodes: BlockNode[]) {
+ const siblings = this.parentNode?.childNodes;
+ if (Array.isArray(siblings)) {
+ siblings.splice(siblings.indexOf(this), 1, ...nodes);
+ }
+ for (const node of nodes) {
+ node.parentNode = this.parentNode;
+ }
+ this.parentNode = null;
+ }
+
render(): string {
const attributes = Object.entries(this.attributes)
.map(([ name, value ]) => ` ${name}="${value}"`);
diff --git a/packages/commons/src/lib/block_reference.ts b/packages/commons/src/lib/block_reference.ts
index 4d84fad24b..11c2b66aad 100644
--- a/packages/commons/src/lib/block_reference.ts
+++ b/packages/commons/src/lib/block_reference.ts
@@ -1,6 +1,9 @@
/** The HTML attribute with the id of a block in a text note. */
export const BLOCK_ID_ATTRIBUTE = "data-trilium-block-id";
+/** The class of a multicolumn layout, a `<section>` whose child sections are its columns. */
+export const MULTICOLUMN_LAYOUT_CLASS = "trilium-multicolumn-layout";
+
/** The blocks that a block reference points at. `startId` equals `endId` for a single block. */
export interface BlockRange {
startId: string;
@@ -16,6 +19,7 @@ export interface BlockNode {
getAttribute?(name: string): string | null | undefined;
setAttribute?(name: string, value: string): unknown;
remove?(): unknown;
+ replaceWith?(...nodes: unknown[]): unknown;
}
/** Sibling nodes, children of `parent`, from `first` to `last`. */
@@ -87,7 +91,8 @@ export function resolveBlockReference<T extends BlockNode>(root: BlockNode, valu
/**
* Keeps in `root` only the blocks that `value`, a `block` link parameter, points at, inside their
- * ancestors. Returns `false` and changes nothing when a block is missing.
+ * ancestors. A multicolumn layout left with one column is replaced by the column's content.
+ * Returns `false` and changes nothing when a block is missing.
*/
export function sliceToBlockReference(root: BlockNode, value: string) {
const { start, end } = resolveBlockReference(root, value);
@@ -190,6 +195,19 @@ function sliceToBlockRange(root: BlockNode, start: BlockNode, end: BlockNode) {
const siblings = Array.from(node.parentNode.childNodes);
removeNodes(siblings.slice(siblings.indexOf(node) + 1));
}
+
+ for (const node of new Set([ ...getPath(start, root), ...getPath(end, root) ])) {
+ unwrapSingleColumnLayout(node);
+ }
+}
+
+/** Replaces `node`, when it is a multicolumn layout with one column, by the column's content. */
+function unwrapSingleColumnLayout(node: BlockNode) {
+ const isLayout = node.getAttribute?.("class")?.split(/\s+/).includes(MULTICOLUMN_LAYOUT_CLASS);
+ const columns = Array.from(node.childNodes).filter((child) => child.tagName);
+ if (isLayout && columns.length === 1) {
+ node.replaceWith?.(...Array.from(columns[0].childNodes));
+ }
}
function* walk(node: BlockNode): Generator<BlockNode> {