// columns.test.mjs — COL3's commands (columns and icons spec §1.7, §9): // making a layout from a block and from a selection, adding, removing // and undoing columns, Backspace in an empty column, ⌥⌘ arrows, and // typed fences. Each result is written, parsed back as the layout the // document holds, and undone to the body it started from. import { test } from 'node:test' import assert from 'node:assert/strict' import { TextSelection } from 'prosemirror-state' import { undo } from 'prosemirror-history' import { editor } from './testkit.mjs' import { serialise, structureCheck } from './serialize.mjs' import { parse, isFence, Columns } from './markup.mjs' import { makeColumns, addColumn, removeColumn, undoColumns, emptyColumnBackspace, toColumn, layoutAround, layoutEdge, deleteInColumns } from './columns.mjs' import { copyData, pasteTransaction } from './clipboard.mjs' function at (view, pos) { view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, pos))) } // textPos is the position just inside the first textblock holding text. function textPos (view, text) { let found = null view.state.doc.descendants((node, pos) => { if (found === null && node.isTextblock && node.textContent.includes(text)) found = pos + 1 return found === null }) return found } function saved (view, fidelity) { const check = structureCheck(view.state.doc, fidelity) assert.ok(check.ok, 'the body reloads as this document') return serialise(view.state.doc, fidelity) } function fenceKinds (body) { return parse(body).filter(block => isFence(block.kind)).map(block => block.kind) } function undoAll (view) { while (undo(view.state, tr => view.dispatch(tr))); } const layout = ':::: columns\n\n::: column\nLeft.\n\n:::\n\n::: column\nRight.\n\n:::\n\n::::' test('a block becomes column 1 of two, and undo gives it back', () => { const body = 'Before.\n\nTurn me.\n\nAfter.' const { view, fidelity } = editor(body) at(view, textPos(view, 'Turn me')) assert.ok(view.run(makeColumns(2))) const out = saved(view, fidelity) assert.equal(out, 'Before.\n\n:::: columns\n\n::: column\nTurn me.\n\n:::\n\n' + '::: column\n:::\n\n::::\n\nAfter.') assert.ok(layoutAround(view.state.selection.$head)?.index === 1, 'the caret is in column 2') undoAll(view) assert.equal(serialise(view.state.doc, fidelity), body) }) test('a selection over blocks moves them all into column 1', () => { const body = 'One.\n\nTwo.\n\nThree.' const { view, fidelity } = editor(body) view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, textPos(view, 'One'), textPos(view, 'Two') + 2))) assert.ok(view.run(makeColumns(3))) const out = saved(view, fidelity) assert.deepEqual(fenceKinds(out).filter(kind => kind === 'column').length, 3) assert.match(out, /::: column\nOne\.\n\nTwo\.\n\n:::/u) }) test('no layout is made inside a layout', () => { const { view } = editor(layout) at(view, textPos(view, 'Left')) assert.equal(view.run(makeColumns(2)), false) }) test('Add a column, Remove this column and Undo columns', () => { const { view, fidelity } = editor(layout) at(view, textPos(view, 'Left')) assert.ok(view.run(addColumn)) assert.equal(fenceKinds(saved(view, fidelity)).filter(kind => kind === 'column').length, 3) at(view, textPos(view, 'Right')) assert.ok(view.run(removeColumn)) const two = saved(view, fidelity) assert.equal(fenceKinds(two).filter(kind => kind === 'column').length, 2) assert.match(two, /Right\./u, 'removing a column keeps its text') at(view, textPos(view, 'Left')) assert.ok(view.run(removeColumn), 'from two, removing undoes the layout') const flat = saved(view, fidelity) assert.deepEqual(fenceKinds(flat), []) assert.match(flat, /Left\.\n\nRight\./u) }) test('Undo columns puts the blocks in reading order', () => { const { view, fidelity } = editor(layout) at(view, textPos(view, 'Right')) assert.ok(view.run(undoColumns)) assert.equal(saved(view, fidelity), 'Left.\n\nRight.') }) test('Backspace in an empty column takes the column away', () => { const body = ':::: columns\n\n::: column\nA\n\n:::\n\n::: column\nB\n\n:::\n\n' + '::: column\n:::\n\n::::' const { view, fidelity } = editor(body) let empty = null view.state.doc.descendants((node, pos) => { if (node.type.name === 'column' && node.textContent === '') empty = pos + 2 }) at(view, empty) assert.ok(view.run(emptyColumnBackspace)) assert.equal(fenceKinds(saved(view, fidelity)).filter(kind => kind === 'column').length, 2) at(view, textPos(view, 'A')) assert.equal(view.run(emptyColumnBackspace), false, 'a column with text is not taken') }) test('⌥⌘→ and ⌥⌘← move between columns', () => { const { view } = editor(layout) at(view, textPos(view, 'Left')) assert.ok(view.run(toColumn(1))) assert.equal(layoutAround(view.state.selection.$head).index, 1) assert.equal(view.run(toColumn(1)), false) assert.ok(view.run(toColumn(-1))) assert.equal(layoutAround(view.state.selection.$head).index, 0) }) test('typing a fence line in a column never closes it (D16)', () => { const { view, fidelity } = editor(layout) const pos = textPos(view, 'Left') for (const typed of [':::', '::: column', ':::: columns', ' :::']) { view.dispatch(view.state.tr.insertText(typed, pos, pos + view.state.doc.resolve(pos).parent.content.size)) const out = saved(view, fidelity) assert.deepEqual(fenceKinds(out), [Columns, 'column', 'column_end', 'column', 'column_end', 'columns_end'], `after typing ${JSON.stringify(typed)}: ${JSON.stringify(out)}`) assert.ok(out.includes('​'), 'the typed line is neutralised') } }) // Fable on the build: H1, H2, M1 to M4. const fences = [Columns, 'column', 'column_end', 'column', 'column_end', 'columns_end'] test('a selection holding a layout makes no nested layout (H1)', () => { const body = 'Before.\n\n' + layout + '\n\nAfter.' const { view, fidelity } = editor(body) view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, textPos(view, 'Before'), textPos(view, 'After') + 2))) assert.ok(view.run(makeColumns(2))) view.state.doc.check() const out = saved(view, fidelity) assert.deepEqual(fenceKinds(out), fences) assert.match(out, /Before\.\n\nLeft\.\n\nRight\.\n\nAfter\./u) }) test('a fence typed after a break in a column stays in the column (H2)', () => { const body = layout + '\n\nAfter one.' const { view, fidelity } = editor(body) const pos = textPos(view, 'Left') + 'Left.'.length view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) view.dispatch(view.state.tr.insertText('```', pos + 1)) const out = saved(view, fidelity) assert.deepEqual(fenceKinds(out), fences, JSON.stringify(out)) assert.match(out, /After one\.$/u) }) test('code holding a fence line and ::: stays code in its column (H2)', () => { const body = ':::: columns\n\n::: column\n```\nx\n```\n\n:::\n\n::: column\nB.\n\n:::\n\n::::' const { view, fidelity } = editor(body) const pos = textPos(view, 'x') + 1 view.dispatch(view.state.tr.insertText('\n```\n:::', pos)) const out = saved(view, fidelity) assert.deepEqual(fenceKinds(out), fences, JSON.stringify(out)) }) test('Backspace after a layout and Delete before one join nothing (M4)', () => { const { view, fidelity } = editor('Before.\n\n' + layout + '\n\nAfter.') at(view, textPos(view, 'After')) assert.ok(view.run(layoutEdge(true))) assert.equal(layoutAround(view.state.selection.$head).index, 1) at(view, textPos(view, 'Before') + 'Before.'.length) assert.ok(view.run(layoutEdge(false))) assert.equal(layoutAround(view.state.selection.$head).index, 0) assert.equal(saved(view, fidelity), 'Before.\n\n' + layout + '\n\nAfter.') }) test('deleting across columns keeps every column (M3)', () => { const three = ':::: columns\n\n::: column\nAlpha.\n\n:::\n\n::: column\nBeta.\n\n:::\n\n' + '::: column\nGamma.\n\n:::\n\n::::' const { view, fidelity } = editor(three) view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, textPos(view, 'Alpha') + 2, textPos(view, 'Beta') + 2))) assert.ok(view.run(deleteInColumns)) const out = saved(view, fidelity) assert.equal(fenceKinds(out).filter(kind => kind === 'column').length, 3) assert.match(out, /::: column\nAl\n/u) assert.match(out, /::: column\nta\.\n/u) }) test('a layout pasted into a column arrives as its blocks (M1)', () => { const { view, fidelity } = editor(layout) const source = editor('One.\n\n' + layout + '\n\nTwo.') const { mapa } = copyData(source.view.state, source.fidelity, 0, source.view.state.doc.content.size - 2) at(view, textPos(view, 'Left') + 'Left.'.length) const tr = pasteTransaction(view.state, fidelity, mapa) assert.ok(tr) view.dispatch(tr) view.state.doc.check() assert.deepEqual(fenceKinds(saved(view, fidelity)), fences) }) test('copying inside one column puts no fence on the clipboard (M2)', () => { const { view, fidelity } = editor(layout) const from = textPos(view, 'Left') const { text } = copyData(view.state, fidelity, from, from + 4) assert.equal(text, 'Left') }) test('typing goes on in the column after a structural reparse (round 2)', () => { const body = ':::: columns\n\n::: column\nAlpha.\n\nSecond.\n\n:::\n\n::: column\nRight.\n\n:::\n\n::::' const { view, fidelity } = editor(body) const pos = textPos(view, 'Alpha') + 'Alpha.'.length view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) at(view, pos + 1) for (const char of '- item') view.dispatch(view.state.tr.insertText(char)) const out = saved(view, fidelity) assert.match(out, /::: column\nRight\.\n/u, JSON.stringify(out)) // What the top level gives for the same typing ("Alpha.item"), in the // block typed in: the caret never moves to the block after it. assert.match(out, /::: column\nAlpha\.item\n\nSecond\.\n/u, JSON.stringify(out)) }) test('a structural reparse in a column leaves code as it was (round 2)', () => { const body = ':::: columns\n\n::: column\n```\n:::\nkeep\n```\n\nText.\n\n:::\n\n' + '::: column\nB.\n\n:::\n\n::::' const { view, fidelity } = editor(body) const pos = textPos(view, 'Text') + 'Text.'.length view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) view.dispatch(view.state.tr.insertText('- x', pos + 1)) const out = saved(view, fidelity) assert.match(out, /```\n:::\nkeep\n```/u, JSON.stringify(out)) })