| 1 | // columns.test.mjs — COL3's commands (columns and icons spec §1.7, §9): |
| 2 | // making a layout from a block and from a selection, adding, removing |
| 3 | // and undoing columns, Backspace in an empty column, ⌥⌘ arrows, and |
| 4 | // typed fences. Each result is written, parsed back as the layout the |
| 5 | // document holds, and undone to the body it started from. |
| 6 | |
| 7 | import { test } from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import { TextSelection } from 'prosemirror-state' |
| 10 | import { undo } from 'prosemirror-history' |
| 11 | import { editor } from './testkit.mjs' |
| 12 | import { serialise, structureCheck } from './serialize.mjs' |
| 13 | import { parse, isFence, Columns } from './markup.mjs' |
| 14 | import { |
| 15 | makeColumns, addColumn, removeColumn, undoColumns, emptyColumnBackspace, |
| 16 | toColumn, layoutAround, layoutEdge, deleteInColumns |
| 17 | } from './columns.mjs' |
| 18 | import { copyData, pasteTransaction } from './clipboard.mjs' |
| 19 | |
| 20 | function at (view, pos) { |
| 21 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, pos))) |
| 22 | } |
| 23 | |
| 24 | // textPos is the position just inside the first textblock holding text. |
| 25 | function textPos (view, text) { |
| 26 | let found = null |
| 27 | view.state.doc.descendants((node, pos) => { |
| 28 | if (found === null && node.isTextblock && node.textContent.includes(text)) found = pos + 1 |
| 29 | return found === null |
| 30 | }) |
| 31 | return found |
| 32 | } |
| 33 | |
| 34 | function saved (view, fidelity) { |
| 35 | const check = structureCheck(view.state.doc, fidelity) |
| 36 | assert.ok(check.ok, 'the body reloads as this document') |
| 37 | return serialise(view.state.doc, fidelity) |
| 38 | } |
| 39 | |
| 40 | function fenceKinds (body) { |
| 41 | return parse(body).filter(block => isFence(block.kind)).map(block => block.kind) |
| 42 | } |
| 43 | |
| 44 | function undoAll (view) { |
| 45 | while (undo(view.state, tr => view.dispatch(tr))); |
| 46 | } |
| 47 | |
| 48 | const layout = ':::: columns\n\n::: column\nLeft.\n\n:::\n\n::: column\nRight.\n\n:::\n\n::::' |
| 49 | |
| 50 | test('a block becomes column 1 of two, and undo gives it back', () => { |
| 51 | const body = 'Before.\n\nTurn me.\n\nAfter.' |
| 52 | const { view, fidelity } = editor(body) |
| 53 | at(view, textPos(view, 'Turn me')) |
| 54 | assert.ok(view.run(makeColumns(2))) |
| 55 | const out = saved(view, fidelity) |
| 56 | assert.equal(out, 'Before.\n\n:::: columns\n\n::: column\nTurn me.\n\n:::\n\n' + |
| 57 | '::: column\n:::\n\n::::\n\nAfter.') |
| 58 | assert.ok(layoutAround(view.state.selection.$head)?.index === 1, 'the caret is in column 2') |
| 59 | undoAll(view) |
| 60 | assert.equal(serialise(view.state.doc, fidelity), body) |
| 61 | }) |
| 62 | |
| 63 | test('a selection over blocks moves them all into column 1', () => { |
| 64 | const body = 'One.\n\nTwo.\n\nThree.' |
| 65 | const { view, fidelity } = editor(body) |
| 66 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, |
| 67 | textPos(view, 'One'), textPos(view, 'Two') + 2))) |
| 68 | assert.ok(view.run(makeColumns(3))) |
| 69 | const out = saved(view, fidelity) |
| 70 | assert.deepEqual(fenceKinds(out).filter(kind => kind === 'column').length, 3) |
| 71 | assert.match(out, /::: column\nOne\.\n\nTwo\.\n\n:::/u) |
| 72 | }) |
| 73 | |
| 74 | test('no layout is made inside a layout', () => { |
| 75 | const { view } = editor(layout) |
| 76 | at(view, textPos(view, 'Left')) |
| 77 | assert.equal(view.run(makeColumns(2)), false) |
| 78 | }) |
| 79 | |
| 80 | test('Add a column, Remove this column and Undo columns', () => { |
| 81 | const { view, fidelity } = editor(layout) |
| 82 | at(view, textPos(view, 'Left')) |
| 83 | assert.ok(view.run(addColumn)) |
| 84 | assert.equal(fenceKinds(saved(view, fidelity)).filter(kind => kind === 'column').length, 3) |
| 85 | at(view, textPos(view, 'Right')) |
| 86 | assert.ok(view.run(removeColumn)) |
| 87 | const two = saved(view, fidelity) |
| 88 | assert.equal(fenceKinds(two).filter(kind => kind === 'column').length, 2) |
| 89 | assert.match(two, /Right\./u, 'removing a column keeps its text') |
| 90 | at(view, textPos(view, 'Left')) |
| 91 | assert.ok(view.run(removeColumn), 'from two, removing undoes the layout') |
| 92 | const flat = saved(view, fidelity) |
| 93 | assert.deepEqual(fenceKinds(flat), []) |
| 94 | assert.match(flat, /Left\.\n\nRight\./u) |
| 95 | }) |
| 96 | |
| 97 | test('Undo columns puts the blocks in reading order', () => { |
| 98 | const { view, fidelity } = editor(layout) |
| 99 | at(view, textPos(view, 'Right')) |
| 100 | assert.ok(view.run(undoColumns)) |
| 101 | assert.equal(saved(view, fidelity), 'Left.\n\nRight.') |
| 102 | }) |
| 103 | |
| 104 | test('Backspace in an empty column takes the column away', () => { |
| 105 | const body = ':::: columns\n\n::: column\nA\n\n:::\n\n::: column\nB\n\n:::\n\n' + |
| 106 | '::: column\n:::\n\n::::' |
| 107 | const { view, fidelity } = editor(body) |
| 108 | let empty = null |
| 109 | view.state.doc.descendants((node, pos) => { |
| 110 | if (node.type.name === 'column' && node.textContent === '') empty = pos + 2 |
| 111 | }) |
| 112 | at(view, empty) |
| 113 | assert.ok(view.run(emptyColumnBackspace)) |
| 114 | assert.equal(fenceKinds(saved(view, fidelity)).filter(kind => kind === 'column').length, 2) |
| 115 | at(view, textPos(view, 'A')) |
| 116 | assert.equal(view.run(emptyColumnBackspace), false, 'a column with text is not taken') |
| 117 | }) |
| 118 | |
| 119 | test('⌥⌘→ and ⌥⌘← move between columns', () => { |
| 120 | const { view } = editor(layout) |
| 121 | at(view, textPos(view, 'Left')) |
| 122 | assert.ok(view.run(toColumn(1))) |
| 123 | assert.equal(layoutAround(view.state.selection.$head).index, 1) |
| 124 | assert.equal(view.run(toColumn(1)), false) |
| 125 | assert.ok(view.run(toColumn(-1))) |
| 126 | assert.equal(layoutAround(view.state.selection.$head).index, 0) |
| 127 | }) |
| 128 | |
| 129 | test('typing a fence line in a column never closes it (D16)', () => { |
| 130 | const { view, fidelity } = editor(layout) |
| 131 | const pos = textPos(view, 'Left') |
| 132 | for (const typed of [':::', '::: column', ':::: columns', ' :::']) { |
| 133 | view.dispatch(view.state.tr.insertText(typed, pos, pos + view.state.doc.resolve(pos).parent.content.size)) |
| 134 | const out = saved(view, fidelity) |
| 135 | assert.deepEqual(fenceKinds(out), [Columns, 'column', 'column_end', 'column', 'column_end', 'columns_end'], |
| 136 | `after typing ${JSON.stringify(typed)}: ${JSON.stringify(out)}`) |
| 137 | assert.ok(out.includes(''), 'the typed line is neutralised') |
| 138 | } |
| 139 | }) |
| 140 | |
| 141 | // Fable on the build: H1, H2, M1 to M4. |
| 142 | |
| 143 | const fences = [Columns, 'column', 'column_end', 'column', 'column_end', 'columns_end'] |
| 144 | |
| 145 | test('a selection holding a layout makes no nested layout (H1)', () => { |
| 146 | const body = 'Before.\n\n' + layout + '\n\nAfter.' |
| 147 | const { view, fidelity } = editor(body) |
| 148 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, |
| 149 | textPos(view, 'Before'), textPos(view, 'After') + 2))) |
| 150 | assert.ok(view.run(makeColumns(2))) |
| 151 | view.state.doc.check() |
| 152 | const out = saved(view, fidelity) |
| 153 | assert.deepEqual(fenceKinds(out), fences) |
| 154 | assert.match(out, /Before\.\n\nLeft\.\n\nRight\.\n\nAfter\./u) |
| 155 | }) |
| 156 | |
| 157 | test('a fence typed after a break in a column stays in the column (H2)', () => { |
| 158 | const body = layout + '\n\nAfter one.' |
| 159 | const { view, fidelity } = editor(body) |
| 160 | const pos = textPos(view, 'Left') + 'Left.'.length |
| 161 | view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) |
| 162 | view.dispatch(view.state.tr.insertText('```', pos + 1)) |
| 163 | const out = saved(view, fidelity) |
| 164 | assert.deepEqual(fenceKinds(out), fences, JSON.stringify(out)) |
| 165 | assert.match(out, /After one\.$/u) |
| 166 | }) |
| 167 | |
| 168 | test('code holding a fence line and ::: stays code in its column (H2)', () => { |
| 169 | const body = ':::: columns\n\n::: column\n```\nx\n```\n\n:::\n\n::: column\nB.\n\n:::\n\n::::' |
| 170 | const { view, fidelity } = editor(body) |
| 171 | const pos = textPos(view, 'x') + 1 |
| 172 | view.dispatch(view.state.tr.insertText('\n```\n:::', pos)) |
| 173 | const out = saved(view, fidelity) |
| 174 | assert.deepEqual(fenceKinds(out), fences, JSON.stringify(out)) |
| 175 | }) |
| 176 | |
| 177 | test('Backspace after a layout and Delete before one join nothing (M4)', () => { |
| 178 | const { view, fidelity } = editor('Before.\n\n' + layout + '\n\nAfter.') |
| 179 | at(view, textPos(view, 'After')) |
| 180 | assert.ok(view.run(layoutEdge(true))) |
| 181 | assert.equal(layoutAround(view.state.selection.$head).index, 1) |
| 182 | at(view, textPos(view, 'Before') + 'Before.'.length) |
| 183 | assert.ok(view.run(layoutEdge(false))) |
| 184 | assert.equal(layoutAround(view.state.selection.$head).index, 0) |
| 185 | assert.equal(saved(view, fidelity), 'Before.\n\n' + layout + '\n\nAfter.') |
| 186 | }) |
| 187 | |
| 188 | test('deleting across columns keeps every column (M3)', () => { |
| 189 | const three = ':::: columns\n\n::: column\nAlpha.\n\n:::\n\n::: column\nBeta.\n\n:::\n\n' + |
| 190 | '::: column\nGamma.\n\n:::\n\n::::' |
| 191 | const { view, fidelity } = editor(three) |
| 192 | view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, |
| 193 | textPos(view, 'Alpha') + 2, textPos(view, 'Beta') + 2))) |
| 194 | assert.ok(view.run(deleteInColumns)) |
| 195 | const out = saved(view, fidelity) |
| 196 | assert.equal(fenceKinds(out).filter(kind => kind === 'column').length, 3) |
| 197 | assert.match(out, /::: column\nAl\n/u) |
| 198 | assert.match(out, /::: column\nta\.\n/u) |
| 199 | }) |
| 200 | |
| 201 | test('a layout pasted into a column arrives as its blocks (M1)', () => { |
| 202 | const { view, fidelity } = editor(layout) |
| 203 | const source = editor('One.\n\n' + layout + '\n\nTwo.') |
| 204 | const { mapa } = copyData(source.view.state, source.fidelity, 0, |
| 205 | source.view.state.doc.content.size - 2) |
| 206 | at(view, textPos(view, 'Left') + 'Left.'.length) |
| 207 | const tr = pasteTransaction(view.state, fidelity, mapa) |
| 208 | assert.ok(tr) |
| 209 | view.dispatch(tr) |
| 210 | view.state.doc.check() |
| 211 | assert.deepEqual(fenceKinds(saved(view, fidelity)), fences) |
| 212 | }) |
| 213 | |
| 214 | test('copying inside one column puts no fence on the clipboard (M2)', () => { |
| 215 | const { view, fidelity } = editor(layout) |
| 216 | const from = textPos(view, 'Left') |
| 217 | const { text } = copyData(view.state, fidelity, from, from + 4) |
| 218 | assert.equal(text, 'Left') |
| 219 | }) |
| 220 | |
| 221 | test('typing goes on in the column after a structural reparse (round 2)', () => { |
| 222 | const body = ':::: columns\n\n::: column\nAlpha.\n\nSecond.\n\n:::\n\n::: column\nRight.\n\n:::\n\n::::' |
| 223 | const { view, fidelity } = editor(body) |
| 224 | const pos = textPos(view, 'Alpha') + 'Alpha.'.length |
| 225 | view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) |
| 226 | at(view, pos + 1) |
| 227 | for (const char of '- item') view.dispatch(view.state.tr.insertText(char)) |
| 228 | const out = saved(view, fidelity) |
| 229 | assert.match(out, /::: column\nRight\.\n/u, JSON.stringify(out)) |
| 230 | // What the top level gives for the same typing ("Alpha.item"), in the |
| 231 | // block typed in: the caret never moves to the block after it. |
| 232 | assert.match(out, /::: column\nAlpha\.item\n\nSecond\.\n/u, JSON.stringify(out)) |
| 233 | }) |
| 234 | |
| 235 | test('a structural reparse in a column leaves code as it was (round 2)', () => { |
| 236 | const body = ':::: columns\n\n::: column\n```\n:::\nkeep\n```\n\nText.\n\n:::\n\n' + |
| 237 | '::: column\nB.\n\n:::\n\n::::' |
| 238 | const { view, fidelity } = editor(body) |
| 239 | const pos = textPos(view, 'Text') + 'Text.'.length |
| 240 | view.dispatch(view.state.tr.insert(pos, view.state.schema.nodes.hard_break.create())) |
| 241 | view.dispatch(view.state.tr.insertText('- x', pos + 1)) |
| 242 | const out = saved(view, fidelity) |
| 243 | assert.match(out, /```\n:::\nkeep\n```/u, JSON.stringify(out)) |
| 244 | }) |
| 245 | |