Files
Docwell/Packages/editor/src/tablemove.test.ts
T
tajniak81andClaude Opus 5.5 3dea2bb2f4
CI / go (Server) (push) Failing after 8m51s
CI / go (Web-App) (push) Successful in 11m46s
CI / mobile (push) Failing after 3m44s
CI / docker (push) Successful in 54m15s
Docs / build (push) Successful in 12m28s
Docs / deploy (push) Skipped
CI / frontend (push) Failing after 7m47s
CI / desktop (ubuntu-latest) (push) Successful in 1h27m22s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add Word export, column dragging and the rest of editor parity
Word export: pages download as .docx from the page menu and from
GET /pages/{id}/export?format=docx. The file keeps headings, alignment,
lists, to-dos, quotes, callouts, code, tables with a repeating header
row, text formatting, colours, highlights, links and the page's images.
Word import now reads callouts, to-dos and shaded highlights, so a page
exported to Word imports back as it was. The user guide says how to move
pages to Notion, whose Import reads Word and Markdown files.

Editor:
- Columns move like rows: a grip on the column's top edge drags it, and
  the table toolbar and Ctrl+Alt+Left/Right move it from the keyboard.
  Row and column moving share tablemove.ts.
- Ctrl+Shift+C copies a link to the block the cursor is in.
- Headings 4 to 6 sit under "Other headings" in the slash menu; a search
  still finds them directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:01:06 +01:00

119 lines
3.9 KiB
TypeScript

// SPDX-License-Identifier: AGPL-3.0-or-later
import { Editor } from "@tiptap/core";
import { afterEach, describe, expect, it } from "vitest";
import { docwellExtensions } from "./extensions";
import { moveInTable } from "./tablemove";
let editor: Editor | undefined;
afterEach(() => editor?.destroy());
/** A table after a paragraph; each row is its cells' text. */
const table = (rows: string[][]) =>
`<p>before</p><table>${rows.map((r) => `<tr>${r.map((c) => `<td><p>${c}</p></td>`).join("")}</tr>`).join("")}</table>`;
function setup(html: string) {
editor = new Editor({ extensions: docwellExtensions(), content: html });
return editor;
}
const grid = (e: Editor) => {
const out: string[][] = [];
e.state.doc.descendants((n) => {
if (n.type.name === "tableRow") out.push(Array.from({ length: n.childCount }, (_, i) => n.child(i).textContent));
return n.type.name !== "tableRow";
});
return out;
};
const tablePos = (e: Editor) => e.state.doc.child(0).nodeSize;
/** A position inside the cell at row, column. */
function inCell(e: Editor, row: number, column: number) {
let pos = -1;
let r = 0;
e.state.doc.descendants((n, p) => {
if (pos >= 0) return false;
if (n.type.name !== "tableRow") return true;
if (r++ === row) {
pos = p + 1;
for (let i = 0; i < column; i++) pos += n.child(i).nodeSize;
pos += 2;
}
return false;
});
return pos;
}
const abc = [
["a", "a2"],
["b", "b2"],
["c", "c2"],
];
describe("moving table rows", () => {
it("moves a row to another place and keeps the cursor in it", () => {
const e = setup(table(abc));
e.view.dispatch(moveInTable(e.state, tablePos(e), "row", 0, 2)!);
expect(grid(e).map((r) => r[0])).toEqual(["b", "c", "a"]);
expect(e.state.selection.$from.parent.textContent).toBe("a");
});
it("moves the cursor's row up and down with the commands", () => {
const e = setup(table(abc));
e.commands.setTextSelection(inCell(e, 1, 0));
expect(e.commands.moveTableRow(-1)).toBe(true);
expect(grid(e).map((r) => r[0])).toEqual(["b", "a", "c"]);
expect(e.commands.moveTableRow(-1)).toBe(false);
e.commands.moveTableRow(1);
e.commands.moveTableRow(1);
expect(grid(e).map((r) => r[0])).toEqual(["a", "c", "b"]);
expect(e.commands.moveTableRow(1)).toBe(false);
});
it("does nothing outside a table, or where a cell spans rows", () => {
const e = setup(
"<p>before</p><table><tr><td rowspan='2'><p>x</p></td><td><p>a</p></td></tr><tr><td><p>b</p></td></tr></table>",
);
expect(moveInTable(e.state, tablePos(e), "row", 0, 1)).toBeNull();
e.commands.setTextSelection(2);
expect(e.commands.moveTableRow(1)).toBe(false);
});
});
describe("moving table columns", () => {
const cols = [
["n", "r", "x"],
["ada", "lead", "1"],
];
it("moves a column in every row and keeps the cursor in its cell", () => {
const e = setup(table(cols));
e.view.dispatch(moveInTable(e.state, tablePos(e), "column", 0, 2, 1)!);
expect(grid(e)).toEqual([
["r", "x", "n"],
["lead", "1", "ada"],
]);
expect(e.state.selection.$from.parent.textContent).toBe("ada");
});
it("moves the cursor's column left and right with the commands", () => {
const e = setup(table(cols));
e.commands.setTextSelection(inCell(e, 1, 1));
expect(e.commands.moveTableColumn(1)).toBe(true);
expect(grid(e)[0]).toEqual(["n", "x", "r"]);
expect(e.state.selection.$from.parent.textContent).toBe("lead");
expect(e.commands.moveTableColumn(1)).toBe(false);
e.commands.moveTableColumn(-1);
e.commands.moveTableColumn(-1);
expect(grid(e)[1]).toEqual(["lead", "ada", "1"]);
});
it("does nothing where a cell spans columns", () => {
const e = setup(
"<p>before</p><table><tr><td colspan='2'><p>x</p></td></tr><tr><td><p>a</p></td><td><p>b</p></td></tr></table>",
);
expect(moveInTable(e.state, tablePos(e), "column", 0, 1)).toBeNull();
});
});