Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/multicolumn layout: add a "Remove layout" button in the toolbar
Adorian Doran30342dd7f9a3
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 8639c72c35..e79a3044c9 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4395,7 +4395,8 @@"multicolumn-layout-toolbar": "Multicolumn layout toolbar","column-layout": "Column layout","arrange-content-in-two-to-four-columns-side-by-side": "Arrange content in two to four columns side by side.",- "0-columns-1": "%0 columns (%1)"+ "0-columns-1": "%0 columns (%1)",+ "remove-layout": "Remove layout"},"task-state-dropped-title": "Invalid task states","task-state-dropped-message_one": "A custom task state definition is invalid and was ignored.",diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Multicolumn layout.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Multicolumn layout.htmlindex 5888bff2fa..6336b5b3d8 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Multicolumn layout.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Multicolumn layout.html@@ -43,8 +43,14 @@<li>Choosing a layout with fewer columns removes the last columns and movestheir content to the end of the last remaining column.</li></ul>-<p>To remove the whole layout, click the handle at its top-left corner to- select it, then press <kbd>Delete</kbd>.</p>+<h2>Removing a layout</h2>+<ul>+ <li>To remove the layout but keep its content, click the <span class="tn-icon cke cke-cancel"></span> <em>Remove layout</em> button+ in the layout's toolbar. The content of the columns is placed one after+ another where the layout was, and empty columns are left out.</li>+ <li>To delete the layout together with its content, click the handle at its+ top-left corner to select it, then press <kbd>Delete</kbd>.</li>+</ul><h2>Narrow screens</h2><p>When a layout has too little room for its columns (narrower than about500 pixels, such as on a phone), the columns are stacked one below thediff --git a/docs/User Guide/User Guide/Note Types/Text/Multicolumn layout.md b/docs/User Guide/User Guide/Note Types/Text/Multicolumn layout.mdindex fc9ca7f283..2a91bf74a5 100644--- a/docs/User Guide/User Guide/Note Types/Text/Multicolumn layout.md+++ b/docs/User Guide/User Guide/Note Types/Text/Multicolumn layout.md@@ -21,7 +21,10 @@ While the cursor is inside a layout, a toolbar appears above it with a button th* Choosing a layout with more columns adds empty columns at the end.* Choosing a layout with fewer columns removes the last columns and moves their content to the end of the last remaining column.-To remove the whole layout, click the handle at its top-left corner to select it, then press <kbd>Delete</kbd>.+## Removing a layout++* To remove the layout but keep its content, click the <span class="tn-icon cke cke-cancel"></span> _Remove layout_ button in the layout's toolbar. The content of the columns is placed one after another where the layout was, and empty columns are left out.+* To delete the layout together with its content, click the handle at its top-left corner to select it, then press <kbd>Delete</kbd>.## Narrow screensdiff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn.tsindex d88ed60f36..363ee9be4a 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn.ts@@ -2,7 +2,7 @@ import { Plugin } from "ckeditor5";import "../../theme/multicolumn.css";import type {- ColumnLayoutCommand, InsertMulticolumnLayoutCommand+ ColumnLayoutCommand, InsertMulticolumnLayoutCommand, RemoveMulticolumnLayoutCommand} from "./multicolumn_commands.js";import MulticolumnEditing from "./multicolumn_editing.js";import MulticolumnUI from "./multicolumn_ui.js";@@ -34,5 +34,6 @@ declare module "ckeditor5" {interface CommandsMap {multicolumnLayout: InsertMulticolumnLayoutCommand;columnLayout: ColumnLayoutCommand;+ removeMulticolumnLayout: RemoveMulticolumnLayoutCommand;}}diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.tsindex d6b0164ee4..d58b487b45 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.spec.ts@@ -221,4 +221,103 @@ describe("multicolumn commands", () => {expect(editor.getData()).toBe(before);});});++ describe("removeMulticolumnLayout", () => {+ function command() {+ const removeLayout = editor.commands.get("removeMulticolumnLayout");+ if (!removeLayout) {+ throw new Error("the editor should register the removeMulticolumnLayout command");+ }+ return removeLayout;+ }++ it("is enabled in or on a layout", () => {+ setModelData(editor.model, "<paragraph>[]</paragraph>");+ expect(command().isEnabled).toBe(false);++ setModelData(editor.model, layout("1-3", "A[]", "B"));+ expect(command().isEnabled).toBe(true);++ setModelData(editor.model, `[${layout("1-3", "A", "B")}]`);+ expect(command().isEnabled).toBe(true);+ });++ it("puts the content of the columns one after another in its place", () => {+ const cell = "<tableCell><paragraph>Cell</paragraph></tableCell>";+ const table = `<table><tableRow>${cell}</tableRow></table>`;+ setModelData(editor.model,+ "<paragraph>Before</paragraph>" ++ "<multicolumnLayout columnRatios=\"1-2-1\">" ++ "<multicolumnColumn><paragraph>A</paragraph><paragraph>B</paragraph>" ++ "</multicolumnColumn>" ++ `<multicolumnColumn><paragraph>C[]</paragraph>${table}</multicolumnColumn>` ++ `<multicolumnColumn>${layout("1-1", "D", "E")}</multicolumnColumn>` ++ "</multicolumnLayout>" ++ "<paragraph>After</paragraph>"+ );++ editor.execute("removeMulticolumnLayout");++ expect(modelWithSelection()).toBe(+ "<paragraph>Before</paragraph>" ++ "<paragraph>A</paragraph><paragraph>B</paragraph>" ++ `<paragraph>C[]</paragraph>${table}` ++ layout("1-1", "D", "E") ++ "<paragraph>After</paragraph>"+ );+ });++ it("adds no blank line for an empty column, and leaves one for an empty layout", () => {+ setModelData(editor.model, layout("1-1-1", "A[]", "", "C"));+ editor.execute("removeMulticolumnLayout");+ expect(modelWithSelection()).toBe("<paragraph>A[]</paragraph><paragraph>C</paragraph>");++ setModelData(editor.model, layout("1-1", "", "[]"));+ editor.execute("removeMulticolumnLayout");+ expect(modelWithSelection()).toBe("<paragraph>[]</paragraph>");+ });++ it("moves the caret out of an empty column, and selects the content of the layout", () => {+ setModelData(editor.model,+ `${layout("1-1-1", "A", "[]", "C")}<paragraph>D</paragraph>`);+ editor.execute("removeMulticolumnLayout");+ expect(modelWithSelection())+ .toBe("<paragraph>A</paragraph><paragraph>C[]</paragraph><paragraph>D</paragraph>");++ setModelData(editor.model, `[${layout("1-1", "A", "B")}]<paragraph>C</paragraph>`);+ editor.execute("removeMulticolumnLayout");+ expect(modelWithSelection())+ .toBe("<paragraph>[A</paragraph><paragraph>B]</paragraph><paragraph>C</paragraph>");+ });++ it("removes only the innermost layout", () => {+ setModelData(editor.model,+ "<multicolumnLayout columnRatios=\"1-3\">" ++ `<multicolumnColumn>${layout("1-1", "A[]", "B")}</multicolumnColumn>` ++ "<multicolumnColumn><paragraph>C</paragraph></multicolumnColumn>" ++ "</multicolumnLayout>"+ );++ editor.execute("removeMulticolumnLayout");++ expect(modelWithSelection()).toBe(+ "<multicolumnLayout columnRatios=\"1-3\">" ++ "<multicolumnColumn><paragraph>A[]</paragraph><paragraph>B</paragraph>" ++ "</multicolumnColumn>" ++ "<multicolumnColumn><paragraph>C</paragraph></multicolumnColumn>" ++ "</multicolumnLayout>"+ );+ });++ it("is one undo step", () => {+ setModelData(editor.model, layout("1-1-1", "A", "", "C[]"));+ const before = editor.getData();++ editor.execute("removeMulticolumnLayout");+ expect(editor.getData()).not.toBe(before);+ editor.execute("undo");++ expect(editor.getData()).toBe(before);+ });+ });});diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.tsindex 8bc3e6d099..9138f38ca8 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_commands.ts@@ -65,6 +65,34 @@ export class ColumnLayoutCommand extends Command {}}+/**+ * Replaces the selected layout with the content of its columns, one column after another. Empty+ * columns add nothing, and a layout with only empty columns leaves one empty paragraph.+ */+export class RemoveMulticolumnLayoutCommand extends Command {++ public override refresh(): void {+ this.isEnabled = !!getSelectedLayout(this.editor);+ }++ public override execute(): void {+ const layout = getSelectedLayout(this.editor);+ /* v8 ignore next 3 -- execute() runs only while refresh() keeps the command enabled */+ if (!layout) {+ return;+ }++ this.editor.model.change(writer => {+ const columns = [...layout.getChildren()] as ModelElement[];+ const filled = columns.filter(hasContent);+ for (const column of filled.length ? filled : columns.slice(0, 1)) {+ writer.move(writer.createRangeIn(column), writer.createPositionBefore(layout));+ }+ writer.remove(layout);+ });+ }+}+/*** Returns the selected layout, or the innermost layout that contains the selection.*/diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_editing.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_editing.tsindex 78e79e4a14..6c380ecff8 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_editing.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_editing.ts@@ -16,7 +16,8 @@ import {MAX_COLUMNS, MIN_COLUMNS, RATIOS_ATTRIBUTE, RATIOS_DATA_ATTRIBUTE} from "./constants.js";import {- ColumnLayoutCommand, InsertMulticolumnLayoutCommand, setColumnCount+ ColumnLayoutCommand, InsertMulticolumnLayoutCommand, RemoveMulticolumnLayoutCommand,+ setColumnCount} from "./multicolumn_commands.js";/**@@ -37,6 +38,7 @@ export default class MulticolumnEditing extends Plugin {const editor = this.editor;editor.commands.add("multicolumnLayout", new InsertMulticolumnLayoutCommand(editor));editor.commands.add("columnLayout", new ColumnLayoutCommand(editor));+ editor.commands.add("removeMulticolumnLayout", new RemoveMulticolumnLayoutCommand(editor));this.registerSchema();this.registerConversion();diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.spec.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.spec.tsindex 32b7133849..a98740ceba 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.spec.ts@@ -6,6 +6,7 @@ import {ContextualBalloon,type DropdownView,Essentials,+ IconCancel,type ModelElement,Paragraph,type ToolbarView,@@ -60,6 +61,21 @@ describe("MulticolumnUI", () => {expect(button.isEnabled).toBe(false);});+ it("removes the layout from its button, keeping the content", () => {+ setModelData(editor.model, layout("1-3", paragraph("A[]"), paragraph("B")));+ const button = editor.ui.componentFactory.create("removeMulticolumnLayout") as ButtonView;+ expect([button.label, button.icon, button.tooltip])+ .toEqual(["Remove layout", IconCancel, true]);+ expect(button.isEnabled).toBe(true);+ const focus = vi.spyOn(editor.editing.view, "focus");++ button.fire("execute");++ expect(getModelData(editor.model)).toBe(paragraph("A[]") + paragraph("B"));+ expect(focus).toHaveBeenCalled();+ expect(button.isEnabled).toBe(false);+ });+describe("layout dropdown", () => {function createDropdown() {const dropdown = editor.ui.componentFactory.create("columnLayout") as DropdownView;@@ -141,7 +157,7 @@ describe("MulticolumnUI", () => {return related?.getAttribute("data-trilium-column-ratios") ?? null;}- it("shows the layout dropdown while the selection is in a layout", () => {+ it("shows the layout dropdown and the remove button while in a layout", () => {setModelData(editor.model, layout("1-3", paragraph("A[]"), paragraph("B")));editor.ui.focusTracker.isFocused = true;editor.ui.update();@@ -149,9 +165,12 @@ describe("MulticolumnUI", () => {const toolbar = toolbarDefinition().view;expect(editor.plugins.get(ContextualBalloon).visibleView).toBe(toolbar);expect(toolbar.ariaLabel).toBe("Multicolumn layout toolbar");- const items = [...toolbar.items] as DropdownView[];- expect(items.map(item => [item.buttonView.label, item.buttonView.icon]))- .toEqual([["Column layout", createLayoutFigure("1-3", 20)]]);+ const items = [...toolbar.items] as (DropdownView | ButtonView)[];+ const buttons = items.map(item => "buttonView" in item ? item.buttonView : item);+ expect(buttons.map(button => [button.label, button.icon])).toEqual([+ ["Column layout", createLayoutFigure("1-3", 20)],+ ["Remove layout", IconCancel]+ ]);});it("belongs to the innermost layout that holds or is the selection", () => {@@ -207,7 +226,7 @@ describe("MulticolumnUI", () => {expect(figures[0].box).toEqual([4, 6.6, 36, 30.8]);});- it("spaces the dots evenly and keeps 1px from the outline at both ends, at any size", () => {+ it("spaces the dots evenly, 1px from the outline at both ends, at any size", () => {for (const [size, count] of [[44, 9], [20, 4]]) {const { box: [, top, , height], dots } = draw("1-1", size);const outline = 1.5 / 2;diff --git a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.ts b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.tsindex ccbb8b891d..ced8f6e848 100644--- a/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.ts+++ b/packages/ckeditor5/src/plugins/multicolumn/multicolumn_ui.ts@@ -1,7 +1,9 @@import {ButtonView,+ type Command,createDropdown,focusChildOnDropdownOpen,+ IconCancel,type Locale,Plugin,WidgetToolbarRepository@@ -11,13 +13,11 @@ import multicolumnIcon from "../../icons/multicolumn.svg?raw";import TileRowView from "../tile_row_view.js";import { findSelectedWidget } from "../widget_utils.js";import { COLUMN_RATIOS, getColumnCount, LAYOUT_WIDGET_PROPERTY } from "./constants.js";-import type {- ColumnLayoutCommand, InsertMulticolumnLayoutCommand-} from "./multicolumn_commands.js";+import type { ColumnLayoutCommand } from "./multicolumn_commands.js";/*** The insert button for multicolumn layouts, and the contextual toolbar with the column layout- * dropdown, shown while the selection is inside a layout.+ * dropdown and the remove button, shown while the selection is inside a layout.*/export default class MulticolumnUI extends Plugin {@@ -30,32 +30,37 @@ export default class MulticolumnUI extends Plugin {}public init(): void {+ const t = this.editor.t;const factory = this.editor.ui.componentFactory;- factory.add("multicolumnLayout", locale => this.createInsertButton(locale));+ this.addCommandButton("multicolumnLayout", t("Multicolumn layout"), multicolumnIcon);factory.add("columnLayout", locale => this.createLayoutDropdown(locale));+ this.addCommandButton("removeMulticolumnLayout", t("Remove layout"), IconCancel);}public afterInit(): void {this.editor.plugins.get(WidgetToolbarRepository).register("multicolumnLayout", {ariaLabel: this.editor.t("Multicolumn layout toolbar"),- items: ["columnLayout"],+ items: ["columnLayout", "removeMulticolumnLayout"],getRelatedElement: selection => findSelectedWidget(selection, LAYOUT_WIDGET_PROPERTY)});}- private createInsertButton(locale: Locale) {+ /** Adds a button named after the command it runs. */+ private addCommandButton(name: string, label: string, icon: string) {const editor = this.editor;- // MulticolumnEditing, which the glue plugin loads first, registers every command.- const command = editor.commands.get("multicolumnLayout") as InsertMulticolumnLayoutCommand;- const button = new ButtonView(locale);- button.set({ label: editor.t("Multicolumn layout"), icon: multicolumnIcon, tooltip: true });- button.bind("isEnabled").to(command, "isEnabled");-- this.listenTo(button, "execute", () => {- editor.execute("multicolumnLayout");- editor.editing.view.focus();+ editor.ui.componentFactory.add(name, locale => {+ // MulticolumnEditing, which the glue plugin loads first, registers every command.+ const command = editor.commands.get(name) as Command;+ const button = new ButtonView(locale);+ button.set({ label, icon, tooltip: true });+ button.bind("isEnabled").to(command, "isEnabled");++ this.listenTo(button, "execute", () => {+ editor.execute(name);+ editor.editing.view.focus();+ });+ return button;});- return button;}/** A dropdown with a figure of every column layout, grouped by column count. */