Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile/text): move blocks up and down from the toolbar

Elian Doran131d83d08064
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts b/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
index 09d37a565d..8b8a96fd8a 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/toolbar.spec.ts
@@ -148,7 +148,7 @@ describe("buildMobileToolbar", () => {
]
} ]);
expect((textFormattingToolbar as unknown[]).every((item) => typeof item === "string")).toBe(true);
- expect(toolbar.items.slice(9)).toStrictEqual([
+ expect(toolbar.items.slice(11)).toStrictEqual([
"|",
"link", "internallink", "aiAssistantMenu", "attachMenu", "insertTableMenu",
"|",
@@ -158,8 +158,9 @@ describe("buildMobileToolbar", () => {
it("keeps text formatting on a row of its own, opened from the main row", () => {
const { toolbar, textFormattingToolbar } = buildMobileToolbar(true);
- expect(toolbar.items.slice(0, 9).map(itemName)).toStrictEqual([
- "undo", "redo", "|", "text-editor.toolbar-groups.insert", "blockType", "textFormatting", "|", "outdent", "indent"
+ expect(toolbar.items.slice(0, 11).map(itemName)).toStrictEqual([
+ "undo", "redo", "|", "text-editor.toolbar-groups.insert", "blockType", "textFormatting",
+ "|", "outdent", "indent", "moveBlockUp", "moveBlockDown"
]);
expect(textFormattingToolbar).toStrictEqual([
"bold", "italic", "underline", "strikethrough", "code",
@@ -212,12 +213,14 @@ describe("buildMobileToolbar", () => {
"alignment:justify": "alignmentMenu",
aiAssistant: "aiAssistantMenu"
};
+ // Alt+Up and Alt+Down move blocks on a keyboard.
+ const MOBILE_ONLY = [ "moveBlockUp", "moveBlockDown" ];
for (const aiAssistant of [ true, false ]) {
const classicItems = collectItems({ items: buildClassicToolbar(false, aiAssistant).toolbar.items })
.map((item) => MOBILE_VARIANTS[item] ?? item);
const { toolbar, textFormattingToolbar } = buildMobileToolbar(aiAssistant);
const mobileItems = collectItems({ items: [ ...toolbar.items, ...textFormattingToolbar ] })
- .filter((item) => item !== "textFormatting");
+ .filter((item) => item !== "textFormatting" && !MOBILE_ONLY.includes(item));
expect([ ...new Set(mobileItems) ].toSorted()).toStrictEqual([ ...new Set(classicItems) ].toSorted());
}
});
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.ts b/apps/client/src/widgets/type_widgets/text/toolbar.ts
index 549fd26eda..1941cf11e3 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar.ts
+++ b/apps/client/src/widgets/type_widgets/text/toolbar.ts
@@ -81,6 +81,8 @@ export function buildMobileToolbar(aiAssistant: boolean) {
"|",
"outdent",
"indent",
+ "moveBlockUp",
+ "moveBlockDown",
"|",
"link",
"internallink",
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
index a93516e9d4..05c8a1d425 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.spec.ts
@@ -1,8 +1,11 @@
import {
_getModelData as getModelData,
_setModelData as setModelData,
+ ButtonView,
ClassicEditor,
Essentials,
+ IconArrowDown,
+ IconArrowUp,
Paragraph,
Plugin,
Table,
@@ -622,3 +625,59 @@ describe("MoveBlockUpDownPlugin inside tables", () => {
});
});
});
+
+describe("MoveBlockUpDownPlugin buttons", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ editor = await createTestEditor([Essentials, Paragraph, Table, MoveBlockUpDownPlugin, TableMove], {
+ toolbar: ["moveBlockUp", "moveBlockDown"]
+ });
+ });
+
+ function buttons() {
+ const [up, down] = [...editor.ui.view.toolbar.items];
+ if (!(up instanceof ButtonView) || !(down instanceof ButtonView)) {
+ throw new Error("The toolbar has no move buttons.");
+ }
+ return { up, down };
+ }
+
+ it("moves the block, and is disabled where it cannot move", () => {
+ const { up, down } = buttons();
+ expect(up).toMatchObject({ label: "Move up", icon: IconArrowUp, tooltip: true });
+ expect(down).toMatchObject({ label: "Move down", icon: IconArrowDown, tooltip: true });
+
+ setModelData(editor.model, "<paragraph>First[]</paragraph><paragraph>Second</paragraph>");
+ expect(up.isEnabled).toBe(false);
+ expect(down.isEnabled).toBe(true);
+
+ down.fire("execute");
+ expect(getModelData(editor.model)).toBe("<paragraph>Second</paragraph><paragraph>First[]</paragraph>");
+ expect(up.isEnabled).toBe(true);
+ expect(down.isEnabled).toBe(false);
+
+ editor.enableReadOnlyMode("spec");
+ expect(up.isEnabled).toBe(false);
+ });
+
+ it("moves the table row in a table, as Alt+Up and Alt+Down do", () => {
+ const { up, down } = buttons();
+ setModelData(editor.model,
+ "<table>" +
+ "<tableRow><tableCell><paragraph>a[]</paragraph></tableCell></tableRow>" +
+ "<tableRow><tableCell><paragraph>b</paragraph></tableCell></tableRow>" +
+ "</table>"
+ );
+ expect(up.isEnabled).toBe(false);
+ expect(down.isEnabled).toBe(true);
+
+ down.fire("execute");
+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(
+ "<table>" +
+ "<tableRow><tableCell><paragraph>b</paragraph></tableCell></tableRow>" +
+ "<tableRow><tableCell><paragraph>a</paragraph></tableCell></tableRow>" +
+ "</table>"
+ );
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.ts b/packages/ckeditor5/src/plugins/move_block_updown.ts
index e9ec9af9d1..73994da7b0 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.ts
@@ -2,7 +2,10 @@
* https://github.com/TriliumNext/Trilium/issues/1002
*/
-import { Command, ModelDocumentSelection, ModelElement, ModelLiveRange, ModelNode, Plugin, _isMac, Editor } from 'ckeditor5';
+import {
+ ButtonView, Command, IconArrowDown, IconArrowUp, ModelDocumentSelection, ModelElement, ModelLiveRange,
+ ModelNode, Plugin, _isMac, Editor
+} from 'ckeditor5';
import { isSelectionInTable } from "./table_move/table_move_commands.js";
@@ -11,24 +14,78 @@ interface MoveKeyCommands {
block: "moveBlockUp" | "moveBlockDown";
}
+const MOVE_UP: MoveKeyCommands = { table: "moveTableRowUp", block: "moveBlockUp" };
+const MOVE_DOWN: MoveKeyCommands = { table: "moveTableRowDown", block: "moveBlockDown" };
+
const keyMap = new Map<string, MoveKeyCommands>([
- ["ArrowUp", { table: "moveTableRowUp", block: "moveBlockUp" }],
- ["ArrowDown", { table: "moveTableRowDown", block: "moveBlockDown" }]
+ ["ArrowUp", MOVE_UP],
+ ["ArrowDown", MOVE_DOWN]
]);
+/**
+ * Registers the `moveBlockUp` and `moveBlockDown` commands, their Alt+↑/↓ shortcuts and buttons
+ * of the same names. The shortcuts and the buttons move the table row instead in a table.
+ */
export default class MoveBlockUpDownPlugin extends Plugin {
init() {
const editor = this.editor;
+ const t = editor.t;
editor.commands.add('moveBlockUp', new MoveBlockUpCommand(editor));
editor.commands.add('moveBlockDown', new MoveBlockDownCommand(editor));
// Use native DOM capturing to intercept Ctrl/Alt + ↑/↓,
// as plugin-level keystroke handling may fail when the selection is near an object.
- this.bindMoveBlockShortcuts(editor);
+ this.bindMoveBlockShortcuts();
+
+ this.addButton('moveBlockUp', t('Move up'), IconArrowUp, MOVE_UP);
+ this.addButton('moveBlockDown', t('Move down'), IconArrowDown, MOVE_DOWN);
+ }
+
+ private addButton(name: string, label: string, icon: string, commands: MoveKeyCommands) {
+ const editor = this.editor;
+ editor.ui.componentFactory.add(name, (locale) => {
+ const button = new ButtonView(locale);
+ button.set({ label, icon, tooltip: true });
+
+ const refresh = () => {
+ button.isEnabled = this.canMove(commands);
+ };
+ refresh();
+ button.listenTo(editor.model.document, 'change', refresh);
+ button.listenTo(editor, 'change:isReadOnly', refresh);
+
+ button.on('execute', () => {
+ this.move(commands);
+ editor.editing.view.focus();
+ });
+ return button;
+ });
+ }
+
+ /**
+ * The command that moves the selection: the row of the table it is in, or its blocks. Inside
+ * a table the block commands would only shuffle paragraphs inside one cell. The block commands
+ * still serve editor builds without the TableMove plugin.
+ */
+ private getMoveCommandName(commands: MoveKeyCommands) {
+ const editor = this.editor;
+ return editor.commands.get(commands.table) && isSelectionInTable(editor.model.document.selection)
+ ? commands.table
+ : commands.block;
}
- bindMoveBlockShortcuts(editor: Editor) {
+ private canMove(commands: MoveKeyCommands) {
+ return !!this.editor.commands.get(this.getMoveCommandName(commands))?.isEnabled;
+ }
+
+ private move(commands: MoveKeyCommands) {
+ if (this.canMove(commands)) {
+ this.editor.execute(this.getMoveCommandName(commands));
+ }
+ }
+
+ bindMoveBlockShortcuts() {
addEditingKeydownCapture(this, (e) => {
const commands = keyMap.get(e.key);
if (!commands || !hasMoveModifier(e)) {
@@ -37,17 +94,7 @@ export default class MoveBlockUpDownPlugin extends Plugin {
e.preventDefault();
e.stopImmediatePropagation();
- // Inside a table the whole row moves; the block commands would only shuffle
- // paragraphs inside one cell. The guard keeps block moves working in editor
- // builds without the TableMove plugin.
- const tableCommand = editor.commands.get(commands.table);
- if (tableCommand && isSelectionInTable(editor.model.document.selection)) {
- if (tableCommand.isEnabled) {
- editor.execute(commands.table);
- }
- return;
- }
- editor.execute(commands.block);
+ this.move(commands);
});
}
@@ -111,6 +158,11 @@ abstract class MoveBlockUpDownCommand extends Command {
abstract getSibling(selectedBlock: ModelElement): ModelNode | null;
abstract get offset(): "before" | "after";
+ override refresh() {
+ const blocks = this.getSelectedBlocks(this.editor.model.document.selection);
+ this.isEnabled = blocks.length > 0 && blocks.every(block => !!this.getSibling(block));
+ }
+
override execute() {
const model = this.editor.model;
const selection = model.document.selection;