bookish-press 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/dist/components/chapter/Block.svelte +19 -0
  2. package/dist/components/chapter/Block.svelte.d.ts +1 -0
  3. package/dist/components/chapter/ChapterBody.test.js +69 -0
  4. package/dist/components/chapter/Format.svelte +4 -0
  5. package/dist/components/chapter/Marginal.svelte +2 -1
  6. package/dist/components/chapter/Symbol.svelte +27 -0
  7. package/dist/components/chapter/Symbol.svelte.d.ts +7 -0
  8. package/dist/components/editor/BookishEditor.svelte +41 -153
  9. package/dist/components/editor/Commands.js +45 -44
  10. package/dist/components/editor/Commands.test.d.ts +1 -0
  11. package/dist/components/editor/Commands.test.js +791 -0
  12. package/dist/components/editor/Editing.d.ts +51 -0
  13. package/dist/components/editor/Editing.js +201 -0
  14. package/dist/components/editor/Permissions.svelte +10 -2
  15. package/dist/components/page/BookLoader.svelte +43 -6
  16. package/dist/components/page/BulkReferenceEditor.svelte +2 -2
  17. package/dist/components/page/Chapter.svelte +77 -70
  18. package/dist/components/page/Contexts.d.ts +14 -0
  19. package/dist/components/page/Contexts.js +14 -4
  20. package/dist/components/page/Edition.svelte +2 -10
  21. package/dist/components/page/EditionLoader.svelte +40 -7
  22. package/dist/components/page/Index.svelte +1 -1
  23. package/dist/components/page/Publish.svelte +0 -42
  24. package/dist/components/page/Reference.svelte +2 -2
  25. package/dist/components/page/References.svelte +5 -6
  26. package/dist/components/page/TitlePage.svelte +65 -0
  27. package/dist/components/page/TitlePage.svelte.d.ts +20 -0
  28. package/dist/components/page/UnsavedNotice.svelte +96 -0
  29. package/dist/components/page/UnsavedNotice.svelte.d.ts +7 -0
  30. package/dist/epub/images.d.ts +21 -0
  31. package/dist/epub/images.js +27 -10
  32. package/dist/epub/images.test.js +33 -1
  33. package/dist/epub/xhtml.js +15 -0
  34. package/dist/models/CRUD.d.ts +28 -12
  35. package/dist/models/CRUD.emu.test.d.ts +1 -0
  36. package/dist/models/CRUD.emu.test.js +250 -0
  37. package/dist/models/CRUD.js +110 -72
  38. package/dist/models/book/Book.js +1 -1
  39. package/dist/models/book/Book.test.d.ts +1 -0
  40. package/dist/models/book/Book.test.js +124 -0
  41. package/dist/models/book/BookMedia.d.ts +5 -0
  42. package/dist/models/book/BookMedia.js +23 -7
  43. package/dist/models/book/BookMedia.test.d.ts +1 -0
  44. package/dist/models/book/BookMedia.test.js +40 -0
  45. package/dist/models/book/Chapter.js +28 -12
  46. package/dist/models/book/Chapter.test.d.ts +1 -0
  47. package/dist/models/book/Chapter.test.js +167 -0
  48. package/dist/models/book/Edition.d.ts +2 -0
  49. package/dist/models/book/Edition.js +34 -25
  50. package/dist/models/book/Edition.test.d.ts +1 -0
  51. package/dist/models/book/Edition.test.js +342 -0
  52. package/dist/models/book/Reference.js +5 -9
  53. package/dist/models/book/Reference.test.d.ts +1 -0
  54. package/dist/models/book/Reference.test.js +40 -0
  55. package/dist/models/chapter/BlocksNode.d.ts +2 -0
  56. package/dist/models/chapter/BlocksNode.js +59 -3
  57. package/dist/models/chapter/BlocksNode.test.js +2 -2
  58. package/dist/models/chapter/Caret.js +12 -3
  59. package/dist/models/chapter/Caret.test.d.ts +1 -0
  60. package/dist/models/chapter/Caret.test.js +127 -0
  61. package/dist/models/chapter/ChapterNode.d.ts +3 -0
  62. package/dist/models/chapter/ChapterNode.js +11 -1
  63. package/dist/models/chapter/CommentNode.js +3 -2
  64. package/dist/models/chapter/EmbedNode.js +1 -1
  65. package/dist/models/chapter/FootnoteNode.js +1 -1
  66. package/dist/models/chapter/FormatNode.d.ts +5 -1
  67. package/dist/models/chapter/FormatNode.js +60 -20
  68. package/dist/models/chapter/LabelNode.js +4 -2
  69. package/dist/models/chapter/ListNode.js +11 -13
  70. package/dist/models/chapter/Node.test.js +2 -11
  71. package/dist/models/chapter/Operations.test.d.ts +1 -0
  72. package/dist/models/chapter/Operations.test.js +298 -0
  73. package/dist/models/chapter/ParagraphNode.js +29 -3
  74. package/dist/models/chapter/Parser.d.ts +49 -4
  75. package/dist/models/chapter/Parser.js +210 -88
  76. package/dist/models/chapter/Parser.test.js +31 -0
  77. package/dist/models/chapter/Roundtrip.test.d.ts +1 -0
  78. package/dist/models/chapter/Roundtrip.test.js +313 -0
  79. package/dist/models/chapter/SymbolBlockNode.d.ts +40 -0
  80. package/dist/models/chapter/SymbolBlockNode.js +75 -0
  81. package/dist/models/chapter/SymbolBlockNode.test.d.ts +1 -0
  82. package/dist/models/chapter/SymbolBlockNode.test.js +130 -0
  83. package/dist/models/chapter/SymbolNode.d.ts +32 -0
  84. package/dist/models/chapter/SymbolNode.js +61 -0
  85. package/dist/models/chapter/SymbolNode.test.d.ts +1 -0
  86. package/dist/models/chapter/SymbolNode.test.js +177 -0
  87. package/dist/models/chapter/TableNode.js +2 -2
  88. package/dist/models/chapter/TextNode.d.ts +8 -1
  89. package/dist/models/chapter/TextNode.js +22 -15
  90. package/dist/util/getSubdomain.js +5 -1
  91. package/dist/util/getSubdomain.test.d.ts +1 -0
  92. package/dist/util/getSubdomain.test.js +53 -0
  93. package/dist/util/mineReference.js +12 -5
  94. package/dist/util/mineReference.test.d.ts +1 -0
  95. package/dist/util/mineReference.test.js +62 -0
  96. package/package.json +12 -7
@@ -0,0 +1,791 @@
1
+ import { describe, test, expect, vi, afterEach } from 'vitest';
2
+ import commands from './Commands';
3
+ import { Accents } from './CaretContext';
4
+ import { escapePlainText, withUndoState } from './Editing';
5
+ import { TestEditor, parseWithCaret, renderWithCaret, parseFormat, } from '../../../tests/editor';
6
+ import Parser from '../../models/chapter/Parser.js';
7
+ import FootnoteNode from '../../models/chapter/FootnoteNode.js';
8
+ import CitationsNode from '../../models/chapter/CitationsNode.js';
9
+ import LabelNode from '../../models/chapter/LabelNode.js';
10
+ import CommentNode from '../../models/chapter/CommentNode.js';
11
+ import AtomNode from '../../models/chapter/AtomNode.js';
12
+ import TextNode from '../../models/chapter/TextNode.js';
13
+ import { LINE_BREAK } from '../../models/chapter/Symbols.js';
14
+ function edit(bookdown, command, key = '') {
15
+ const editor = new TestEditor(bookdown);
16
+ const handled = editor.execute(command, key);
17
+ return { editor, handled, bookdown: editor.bookdown };
18
+ }
19
+ describe('the test harness', () => {
20
+ test('round trips carets and selections', () => {
21
+ for (const text of [
22
+ 'a‸b',
23
+ '‸ab',
24
+ 'ab‸',
25
+ 'a‸b‸c',
26
+ '*b‸old*',
27
+ 'one\n\ntw‸o',
28
+ ])
29
+ expect(renderWithCaret(...Object.values(parseWithCaret(text)))).toBe(text);
30
+ });
31
+ test('rejects missing markers', () => {
32
+ expect(() => parseWithCaret('none')).toThrow();
33
+ });
34
+ test('every command has a unique signature or a distinguishing context', () => {
35
+ // Commands are matched in order, so this just checks they all have descriptions and handlers.
36
+ for (const command of commands) {
37
+ expect(command.description.length).toBeGreaterThan(0);
38
+ expect(command.handler).toBeInstanceOf(Function);
39
+ }
40
+ });
41
+ });
42
+ describe('typing', () => {
43
+ test('insert character', () => {
44
+ const editor = new TestEditor('ab‸c');
45
+ editor.type('XY');
46
+ expect(editor.bookdown).toBe('abXY‸c');
47
+ });
48
+ test('typing replaces a selection', () => {
49
+ const editor = new TestEditor('a‸bc‸d');
50
+ editor.type('X');
51
+ expect(editor.bookdown).toBe('aX‸d');
52
+ });
53
+ test('typing markup characters keeps them literal', () => {
54
+ const editor = new TestEditor('‸');
55
+ editor.type('*hi* \\ _x_');
56
+ expect(editor.root.toText()).toBe('*hi* \\ _x_');
57
+ const reparsed = Parser.parseChapter(undefined, editor.text);
58
+ expect(reparsed.toText()).toBe('*hi* \\ _x_');
59
+ });
60
+ test('typing in a code block replaces a selection', () => {
61
+ const editor = new TestEditor('`\nab‸cd‸ef\n`');
62
+ editor.type('X');
63
+ expect(editor.bookdown).toBe('`\nabX‸ef\n`');
64
+ });
65
+ });
66
+ describe('accents', () => {
67
+ const accentCommands = commands.filter((c) => c.category === 'text' &&
68
+ c.mutates === false &&
69
+ c.code !== undefined &&
70
+ c.key === undefined);
71
+ test('there are Windows and Mac shortcuts for every accent', () => {
72
+ const accents = new Set(accentCommands.map((c) => c.icon));
73
+ expect(accents).toEqual(new Set(['`', "'", '^', '~', ':']));
74
+ for (const accent of accents) {
75
+ const forAccent = accentCommands.filter((c) => c.icon === accent);
76
+ expect(forAccent.some((c) => c.control)).toBe(true);
77
+ expect(forAccent.some((c) => c.alt)).toBe(true);
78
+ }
79
+ });
80
+ test('accent command descriptions name their accent', () => {
81
+ const names = {
82
+ '`': 'grave',
83
+ "'": 'apostrophe',
84
+ '^': 'caret',
85
+ '~': 'tilde',
86
+ ':': 'umlaut',
87
+ };
88
+ for (const command of accentCommands)
89
+ expect(command.description).toContain(names[command.icon]);
90
+ });
91
+ const strokes = [
92
+ // accent, code, control, alt, shift
93
+ ['`', 'Backquote', true, false, false],
94
+ ['`', 'Backquote', false, true, false],
95
+ ["'", 'Quote', true, false, false],
96
+ ["'", 'KeyE', false, true, false],
97
+ ['^', 'Digit6', true, false, true],
98
+ ['^', 'KeyI', false, true, false],
99
+ ['~', 'Backquote', true, false, true],
100
+ ['~', 'KeyN', false, true, false],
101
+ [':', 'Semicolon', true, false, true],
102
+ [':', 'KeyU', false, true, false],
103
+ ];
104
+ for (const [accent, code, ctrlKey, altKey, shiftKey] of strokes) {
105
+ test(`${ctrlKey ? 'ctrl' : 'alt'}${shiftKey ? '+shift' : ''}+${code} then a vowel inserts a ${accent} accent`, () => {
106
+ const editor = new TestEditor('x‸');
107
+ const command = editor.press({ code, ctrlKey, altKey, shiftKey });
108
+ expect(command?.icon).toBe(accent);
109
+ expect(editor.accent).toBe(accent);
110
+ editor.type('e');
111
+ expect(editor.bookdown).toBe(`x${Accents[accent + 'e']}‸`);
112
+ expect(editor.accent).toBeUndefined();
113
+ });
114
+ }
115
+ test('every accent has every vowel', () => {
116
+ for (const accent of ['`', "'", '^', '~', ':'])
117
+ for (const vowel of 'aeiouAEIOU')
118
+ expect(Accents[accent + vowel]).toBeDefined();
119
+ });
120
+ test('an accent before a non-vowel inserts the character as is', () => {
121
+ const editor = new TestEditor('‸');
122
+ editor.press({ code: 'KeyE', altKey: true });
123
+ editor.type('x');
124
+ expect(editor.bookdown).toBe('x‸');
125
+ expect(editor.accent).toBeUndefined();
126
+ });
127
+ test('backspace cancels an accent', () => {
128
+ const editor = new TestEditor('ab‸');
129
+ editor.press({ code: 'KeyE', altKey: true });
130
+ editor.press({ key: 'Backspace' });
131
+ editor.type('e');
132
+ expect(editor.bookdown).toBe('ae‸');
133
+ });
134
+ });
135
+ describe('deleting', () => {
136
+ test('backspace deletes the previous character', () => {
137
+ expect(edit('ab‸c', 'delete previous character').bookdown).toBe('a‸c');
138
+ });
139
+ test('delete deletes the next character', () => {
140
+ expect(edit('ab‸c', 'delete next character').bookdown).toBe('ab‸');
141
+ });
142
+ test('backspace deletes a selection', () => {
143
+ expect(edit('a‸bc‸d', 'delete previous character').bookdown).toBe('a‸d');
144
+ expect(edit('a‸bc‸d', 'delete next character').bookdown).toBe('a‸d');
145
+ });
146
+ test('backspace at the start of a paragraph merges it into the previous one', () => {
147
+ expect(edit('one\n\n‸two', 'delete previous character').bookdown).toBe('one‸two');
148
+ });
149
+ test('delete at the end of a paragraph merges the next one into it', () => {
150
+ expect(edit('one‸\n\ntwo', 'delete next character').bookdown).toBe('one‸two');
151
+ });
152
+ test('backspace at the start of a document does nothing', () => {
153
+ expect(edit('‸one', 'delete previous character').handled).toBe(false);
154
+ });
155
+ test('backspace across formatting', () => {
156
+ expect(edit('a *b*‸ c', 'delete previous character').bookdown).toBe('a ‸ c');
157
+ });
158
+ test('backspace marks itself as mutating', () => {
159
+ expect(commands.find((c) => c.description === 'delete previous character')
160
+ ?.mutates).toBe(true);
161
+ });
162
+ });
163
+ describe('paragraphs and breaks', () => {
164
+ test('split paragraph', () => {
165
+ expect(edit('one‸two', 'split paragraph').bookdown).toBe('one\n\n‸two');
166
+ });
167
+ test('split paragraph replaces a selection', () => {
168
+ expect(edit('o‸netw‸o', 'split paragraph').bookdown).toBe('o\n\n‸o');
169
+ });
170
+ test('line break', () => {
171
+ expect(edit('one‸two', 'insert line break').bookdown).toBe(`one${LINE_BREAK}‸two`);
172
+ });
173
+ test('code newline', () => {
174
+ const editor = new TestEditor('`\nab‸c\n`');
175
+ expect(editor.isActive('insert code newline')).toBe(true);
176
+ editor.execute('insert code newline');
177
+ expect(editor.bookdown).toBe('`\nab\n‸c\n`');
178
+ });
179
+ test('pressing enter in a paragraph splits it', () => {
180
+ const editor = new TestEditor('one‸two');
181
+ expect(editor.press({ key: 'Enter' })?.description).toBe('split paragraph');
182
+ });
183
+ test('pressing enter in code inserts a newline', () => {
184
+ const editor = new TestEditor('`\nab‸c\n`');
185
+ expect(editor.press({ key: 'Enter' })?.description).toBe('insert code newline');
186
+ });
187
+ });
188
+ describe('levels', () => {
189
+ for (const level of [1, 2, 3, 4]) {
190
+ test(`header ${level}`, () => {
191
+ const description = `format as ${['1st', '2nd', '3rd', '4th'][level - 1]} level header`;
192
+ expect(edit('he‸llo', description).bookdown).toBe(`${'#'.repeat(level)} he‸llo`);
193
+ });
194
+ }
195
+ test('paragraph', () => {
196
+ const editor = new TestEditor('## he‸llo');
197
+ expect(editor.isActive('format as paragraph')).toBe(true);
198
+ editor.execute('format as paragraph');
199
+ expect(editor.bookdown).toBe('he‸llo');
200
+ expect(editor.isActive('format as paragraph')).toBe(false);
201
+ });
202
+ test('header commands are inactive at their own level', () => {
203
+ const editor = new TestEditor('# he‸llo');
204
+ expect(editor.isActive('format as 1st level header')).toBe(false);
205
+ expect(editor.isActive('format as 2nd level header')).toBe(true);
206
+ });
207
+ });
208
+ describe('inline formatting', () => {
209
+ test('bold, italic, subscript, superscript a selection', () => {
210
+ expect(edit('a ‸bold‸ b', 'bold').editor.text).toBe('a *bold* b');
211
+ expect(edit('a ‸it‸ b', 'italic').editor.text).toBe('a _it_ b');
212
+ expect(edit('H‸2‸O', 'subscript').editor.text).toBe('H^v2^O');
213
+ expect(edit('x‸2‸', 'superscript').editor.text).toBe('x^2^');
214
+ });
215
+ test('bold toggles off', () => {
216
+ expect(edit('a *‸bold‸* b', 'bold').editor.text).toBe('a bold b');
217
+ });
218
+ test('clear formatting', () => {
219
+ expect(edit('‸a *b* _c_‸', 'clear formatting').editor.text).toBe('a b c');
220
+ });
221
+ test('toggle code wraps a selection and unwraps it', () => {
222
+ const editor = new TestEditor('a ‸code‸ b');
223
+ editor.execute('toggle code');
224
+ expect(editor.text).toBe('a `code` b');
225
+ editor.execute('toggle code');
226
+ expect(editor.text).toBe('a code b');
227
+ });
228
+ test('toggle link wraps a selection and unwraps it', () => {
229
+ const editor = new TestEditor('a ‸site‸ b');
230
+ editor.execute('toggle link');
231
+ expect(editor.text).toBe('a [site|] b');
232
+ expect(editor.isActive('toggle link')).toBe(true);
233
+ editor.execute('toggle link');
234
+ expect(editor.text).toBe('a site b');
235
+ });
236
+ test('toggle link wraps a backwards selection', () => {
237
+ const { root, range } = parseWithCaret('a ‸site‸ b');
238
+ const editor = new TestEditor('‸');
239
+ editor.root = root;
240
+ editor.range = { start: range.end, end: range.start };
241
+ editor.execute('toggle link');
242
+ expect(editor.text).toBe('a [site|] b');
243
+ });
244
+ test('toggle definition', () => {
245
+ expect(edit('a ‸term‸ b', 'toggle definition').editor.text).toBe('a ~term~ b');
246
+ });
247
+ });
248
+ describe('annotations', () => {
249
+ test('insert footnote', () => {
250
+ const { editor } = edit('a‸ b', 'insert footnote');
251
+ expect(editor.text).toBe('a{} b');
252
+ // The caret is in the footnote.
253
+ expect(editor.range.start.node.getClosestParentOfType(editor.root, FootnoteNode)).toBeDefined();
254
+ });
255
+ test('insert footnote around a selection uses it as the note', () => {
256
+ expect(edit('a ‸note‸ b', 'insert footnote').editor.text).toBe('a {note} b');
257
+ });
258
+ test('insert citations', () => {
259
+ const { editor } = edit('a‸ b', 'insert citations');
260
+ expect(editor.text).toBe('a<> b');
261
+ expect(editor.range.start.node).toBeInstanceOf(CitationsNode);
262
+ });
263
+ test('insert label', () => {
264
+ const { editor } = edit('a‸ b', 'insert label');
265
+ expect(editor.range.start.node).toBeInstanceOf(LabelNode);
266
+ });
267
+ test('insert comment goes at the end of a selection', () => {
268
+ const { editor } = edit('a ‸b‸ c', 'insert comment');
269
+ expect(editor.root.getNodes().some((n) => n instanceof CommentNode)).toBe(true);
270
+ expect(editor.root.toText()).toBe('a b c');
271
+ });
272
+ test('annotation commands are inactive outside chapters and in atoms', () => {
273
+ const editor = new TestEditor('a‸ b', { chapter: false });
274
+ for (const description of [
275
+ 'insert footnote',
276
+ 'insert citations',
277
+ 'insert label',
278
+ 'insert comment',
279
+ 'toggle definition',
280
+ ])
281
+ expect(editor.isActive(description)).toBe(false);
282
+ const inFootnote = new TestEditor('a{no‸te} b');
283
+ expect(inFootnote.isActive('insert footnote')).toBe(false);
284
+ });
285
+ });
286
+ describe('blocks', () => {
287
+ test('insert horizontal rule before the paragraph', () => {
288
+ const editor = new TestEditor('‸hello');
289
+ expect(editor.isActive('insert horizontal rule')).toBe(true);
290
+ editor.execute('insert horizontal rule');
291
+ expect(editor.bookdown).toBe('-\n\n‸hello');
292
+ });
293
+ test('block insertions are only active at a paragraph start', () => {
294
+ const editor = new TestEditor('he‸llo');
295
+ for (const description of [
296
+ 'insert horizontal rule',
297
+ 'insert callout',
298
+ 'insert quote',
299
+ 'insert code',
300
+ 'insert image or video',
301
+ 'insert table',
302
+ ])
303
+ expect(editor.isActive(description)).toBe(false);
304
+ });
305
+ test('insert callout', () => {
306
+ expect(edit('‸hello', 'insert callout').bookdown).toBe('=\n‸\n=\n\nhello');
307
+ });
308
+ test('insert quote', () => {
309
+ expect(edit('‸hello', 'insert quote').bookdown).toBe('"\n‸\n"\n\nhello');
310
+ });
311
+ test('insert code', () => {
312
+ expect(edit('‸hello', 'insert code').bookdown).toBe('`\n‸\n`\n\nhello');
313
+ });
314
+ test('insert table', () => {
315
+ expect(edit('‸hello', 'insert table').bookdown).toBe(',‸||\n,||\n,||\n\n\nhello');
316
+ });
317
+ test('insert image', () => {
318
+ const { editor, handled } = edit('‸hello', 'insert image or video');
319
+ expect(handled).toBe(true);
320
+ expect(editor.text).toBe('|||||\n\nhello');
321
+ });
322
+ test('convert inline code to block code', () => {
323
+ const editor = new TestEditor('`co‸de`');
324
+ expect(editor.isActive('convert inline code to block code')).toBe(true);
325
+ expect(editor.press({ key: 'Enter' })?.description).toBe('convert inline code to block code');
326
+ expect(editor.text).toContain('`<');
327
+ });
328
+ });
329
+ describe('tables', () => {
330
+ const table = ',a|b\n,c|‸d\n';
331
+ test('commands are only visible in tables', () => {
332
+ expect(new TestEditor('‸x').isVisible('insert row above')).toBe(false);
333
+ expect(new TestEditor(table).isVisible('insert row above')).toBe(true);
334
+ });
335
+ test('insert row above and below', () => {
336
+ expect(edit(table, 'insert row above').bookdown).toBe(',a|b\n,|‸\n,c|d\n');
337
+ expect(edit(table, 'insert row below').bookdown).toBe(',a|b\n,c|d\n,|‸\n');
338
+ });
339
+ test('insert column before and after', () => {
340
+ expect(edit(table, 'insert column before').bookdown).toBe(',a||b\n,c|‸|d\n');
341
+ expect(edit(table, 'insert column after').bookdown).toBe(',a|b|\n,c|d|‸\n');
342
+ });
343
+ test('delete row and column', () => {
344
+ expect(edit(table, 'delete row').bookdown).toBe(',a|‸b\n');
345
+ expect(edit(table, 'delete column').bookdown).toBe(',a\n,‸c\n');
346
+ expect(edit(',‸a|b\n,c|d\n', 'delete row').bookdown).toBe(',‸c|d\n');
347
+ expect(edit(',‸a|b\n,c|d\n', 'delete column').bookdown).toBe(',‸b\n,d\n');
348
+ });
349
+ test("can't delete the last row or column", () => {
350
+ const editor = new TestEditor(',‸a\n');
351
+ expect(editor.isActive('delete row')).toBe(false);
352
+ expect(editor.isActive('delete column')).toBe(false);
353
+ });
354
+ test('tab moves to the next cell', () => {
355
+ const editor = new TestEditor(',‸a|b\n,c|d\n');
356
+ editor.press({ key: 'Tab' });
357
+ expect(editor.bookdown).toBe(',a|‸b\n,c|d\n');
358
+ editor.press({ key: 'Tab', shiftKey: true });
359
+ expect(editor.bookdown).toBe(',‸a|b\n,c|d\n');
360
+ });
361
+ });
362
+ describe('lists', () => {
363
+ test('convert paragraphs to lists', () => {
364
+ expect(edit('o‸ne', 'convert paragraph to bulleted list item').bookdown).toBe('* o‸ne');
365
+ expect(edit('o‸ne', 'convert paragraph to numbered list item').bookdown).toBe('1. o‸ne');
366
+ expect(edit('‸one\n\ntwo‸', 'convert paragraph to bulleted list item')
367
+ .editor.text).toBe('* one\n* two');
368
+ });
369
+ test('convert between list styles', () => {
370
+ expect(edit('* o‸ne', 'convert bulleted list item to numbered').bookdown).toBe('1. o‸ne');
371
+ expect(edit('1. o‸ne', 'convert numbered list item to bulleted').bookdown).toBe('* o‸ne');
372
+ });
373
+ test('convert list items to paragraphs converts the whole list', () => {
374
+ expect(edit('* o‸ne\n* two', 'convert bulleted list item to paragraph')
375
+ .editor.text).toBe('one\n\ntwo');
376
+ });
377
+ test('convert list items with formatting to paragraphs', () => {
378
+ expect(edit('* o‸ne *bold*\n** two{note}', 'convert bulleted list item to paragraph').editor.text).toBe('one *bold*\n\ntwo{note}');
379
+ });
380
+ test('split list item', () => {
381
+ expect(edit('* on‸e\n* two', 'split list item').bookdown).toBe('* on\n* ‸e\n* two');
382
+ });
383
+ test('enter in an empty last item ends the list', () => {
384
+ const editor = new TestEditor('* one\n* ‸');
385
+ editor.press({ key: 'Enter' });
386
+ expect(editor.bookdown).toBe('* one\n\n‸');
387
+ });
388
+ test('enter in an empty last sublist item unindents it', () => {
389
+ const editor = new TestEditor('* one\n** two\n** ‸');
390
+ editor.press({ key: 'Enter' });
391
+ expect(editor.bookdown).toBe('* one\n** two\n* ‸');
392
+ });
393
+ test('indent and unindent', () => {
394
+ const editor = new TestEditor('* one\n* tw‸o');
395
+ expect(editor.isActive('unindent list item')).toBe(false);
396
+ editor.execute('indent list item');
397
+ expect(editor.bookdown).toBe('* one\n** tw‸o');
398
+ expect(editor.isActive('unindent list item')).toBe(true);
399
+ editor.execute('unindent list item');
400
+ expect(editor.bookdown).toBe('* one\n* tw‸o');
401
+ });
402
+ test('tab indents in a list', () => {
403
+ const editor = new TestEditor('* one\n* tw‸o');
404
+ expect(editor.press({ key: 'Tab' })?.description).toBe('indent list item');
405
+ });
406
+ });
407
+ describe('navigation and selection', () => {
408
+ test('move by character', () => {
409
+ const editor = new TestEditor('ab‸c');
410
+ editor.press({ key: 'ArrowLeft' });
411
+ expect(editor.bookdown).toBe('a‸bc');
412
+ editor.press({ key: 'ArrowRight' });
413
+ editor.press({ key: 'ArrowRight' });
414
+ expect(editor.bookdown).toBe('abc‸');
415
+ });
416
+ test('move by word', () => {
417
+ const editor = new TestEditor('one two‸ three');
418
+ editor.press({ key: 'ArrowLeft', altKey: true });
419
+ expect(editor.bookdown).toBe('one ‸two three');
420
+ editor.press({ key: 'ArrowRight', altKey: true });
421
+ expect(editor.bookdown).toBe('one two‸ three');
422
+ });
423
+ test('move to paragraph start and end', () => {
424
+ const editor = new TestEditor('one t‸wo');
425
+ editor.press({ key: 'ArrowLeft', ctrlKey: true });
426
+ expect(editor.bookdown).toBe('‸one two');
427
+ editor.press({ key: 'ArrowRight', metaKey: true });
428
+ expect(editor.bookdown).toBe('one two‸');
429
+ });
430
+ test('extend a selection', () => {
431
+ const editor = new TestEditor('ab‸cd');
432
+ editor.press({ key: 'ArrowRight', shiftKey: true });
433
+ expect(editor.bookdown).toBe('ab‸c‸d');
434
+ editor.press({ key: 'ArrowRight', shiftKey: true, ctrlKey: true });
435
+ expect(editor.bookdown).toBe('ab‸cd‸');
436
+ });
437
+ test('select all', () => {
438
+ const editor = new TestEditor('one\n\ntw‸o');
439
+ editor.press({ key: 'a', metaKey: true });
440
+ expect(editor.bookdown).toBe('‸one\n\ntwo‸');
441
+ });
442
+ test('select all in a footnote selects the footnote', () => {
443
+ const editor = new TestEditor('a{no‸te}');
444
+ editor.press({ key: 'a', ctrlKey: true });
445
+ expect(editor.bookdown).toBe('a{‸note‸}');
446
+ });
447
+ test('navigation does not change the document', () => {
448
+ const editor = new TestEditor('ab‸c');
449
+ const root = editor.root;
450
+ editor.press({ key: 'ArrowLeft' });
451
+ expect(editor.root).toBe(root);
452
+ expect(editor.undoStack).toEqual([]);
453
+ });
454
+ test('arrows onto an atom select it', () => {
455
+ const editor = new TestEditor('a‸{note}b');
456
+ editor.press({ key: 'ArrowRight' });
457
+ expect(editor.range.start.node).toBeInstanceOf(AtomNode);
458
+ });
459
+ });
460
+ describe('clipboard', () => {
461
+ test('copy copies the selection', () => {
462
+ const editor = new TestEditor('a ‸*bold*‸ b');
463
+ editor.press({ key: 'c', metaKey: true });
464
+ expect(editor.copied?.toBookdown()).toBe('*bold*');
465
+ expect(editor.text).toBe('a *bold* b');
466
+ });
467
+ test('cut copies and removes the selection', () => {
468
+ const editor = new TestEditor('a ‸bold‸ b');
469
+ editor.execute('delete the selected content and copy it to the clipboard');
470
+ expect(editor.copied?.toBookdown()).toBe('bold');
471
+ expect(editor.bookdown).toBe('a ‸ b');
472
+ });
473
+ test('paste plain text', () => {
474
+ const editor = new TestEditor('a‸b');
475
+ editor.paste({ plain: 'XY' });
476
+ expect(editor.bookdown).toBe('aXY‸b');
477
+ });
478
+ test('paste keeps markup characters and backslashes literal', () => {
479
+ for (const plain of [
480
+ 'back\\slash',
481
+ 'C:\\Users\\amy',
482
+ '*not bold*',
483
+ '_not italic_',
484
+ '[not|link]',
485
+ '{not a footnote}',
486
+ '<notcited>',
487
+ '`not code`',
488
+ '50% off',
489
+ 'x %not a comment',
490
+ '@notasymbol',
491
+ 'see :label',
492
+ `a ${LINE_BREAK} b`,
493
+ ]) {
494
+ const editor = new TestEditor('‸');
495
+ editor.paste({ plain });
496
+ expect(editor.root.toText()).toBe(plain);
497
+ }
498
+ });
499
+ test('paste keeps line-leading characters literal', () => {
500
+ for (const plain of [
501
+ '# not a header',
502
+ '- not a rule',
503
+ '* not a bullet',
504
+ '1. not a number',
505
+ '| not an embed',
506
+ ', not a table',
507
+ '%not a comment',
508
+ ' indented',
509
+ ]) {
510
+ const editor = new TestEditor('‸');
511
+ editor.paste({ plain });
512
+ expect(editor.root.getBlocks().length).toBe(1);
513
+ expect(editor.root.toText()).toBe(plain);
514
+ }
515
+ });
516
+ test('paste multiple paragraphs splits at the caret', () => {
517
+ const editor = new TestEditor('Hello ‸world');
518
+ editor.paste({ plain: 'big\n\nwide ' });
519
+ expect(editor.bookdown).toBe('Hello big\n\nwide ‸world');
520
+ });
521
+ test('paste windows line endings', () => {
522
+ const editor = new TestEditor('‸');
523
+ editor.paste({ plain: 'one\r\n\r\ntwo' });
524
+ expect(editor.text).toBe('one\n\ntwo');
525
+ });
526
+ test('paste replaces a selection', () => {
527
+ const editor = new TestEditor('a‸bc‸d');
528
+ editor.paste({ plain: 'X' });
529
+ expect(editor.bookdown).toBe('aX‸d');
530
+ });
531
+ test('paste into a footnote is inline', () => {
532
+ const editor = new TestEditor('a{no‸te}');
533
+ editor.paste({ plain: 'one\n\ntwo' });
534
+ expect(editor.text).toBe('a{noone twote}');
535
+ });
536
+ test('paste a copied node', () => {
537
+ const source = new TestEditor('a ‸*bold*‸ b');
538
+ source.press({ key: 'c', metaKey: true });
539
+ const copy = source.copied;
540
+ expect(copy).toBeDefined();
541
+ const editor = new TestEditor('x‸y');
542
+ if (copy) {
543
+ editor.paste(copy);
544
+ // Pasting twice pastes a fresh copy.
545
+ editor.paste(copy);
546
+ }
547
+ expect(editor.text).toBe('x*bold**bold*y');
548
+ });
549
+ test('right and left arrows collapse a selection to its end and start', () => {
550
+ const right = new TestEditor('a‸bc‸d');
551
+ right.press({ key: 'ArrowRight' });
552
+ expect(right.bookdown).toBe('abc‸d');
553
+ const left = new TestEditor('a‸bc‸d');
554
+ left.press({ key: 'ArrowLeft' });
555
+ expect(left.bookdown).toBe('a‸bcd');
556
+ });
557
+ test('paste in a single format editor', () => {
558
+ const editor = new TestEditor('a‸b', {
559
+ parse: parseFormat,
560
+ chapter: false,
561
+ });
562
+ editor.paste({ plain: 'one\ntwo' });
563
+ expect(editor.text).toBe('aone twob');
564
+ });
565
+ describe('the paste command', () => {
566
+ function clipboardWith(read) {
567
+ vi.stubGlobal('navigator', { clipboard: { read } });
568
+ }
569
+ const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
570
+ const textItem = (text) => ({
571
+ types: ['text/plain'],
572
+ getType: async () => new Blob([text], { type: 'text/plain' }),
573
+ });
574
+ afterEach(() => vi.unstubAllGlobals());
575
+ test('pastes system clipboard text', async () => {
576
+ clipboardWith(async () => [textItem('X\\Y')]);
577
+ const editor = new TestEditor('a‸b');
578
+ editor.press({ key: 'v', metaKey: true });
579
+ await flush();
580
+ expect(editor.bookdown).toBe('aX\\\\Y‸b');
581
+ });
582
+ test('pastes the editor clipboard when the system clipboard is empty', async () => {
583
+ clipboardWith(async () => []);
584
+ const editor = new TestEditor('‸*bold*‸ b');
585
+ editor.press({ key: 'c', metaKey: true });
586
+ editor.press({ key: 'ArrowRight' });
587
+ editor.press({ key: 'ArrowRight' });
588
+ editor.press({ key: 'v', metaKey: true });
589
+ await flush();
590
+ expect(editor.text).toBe('*bold* *bold*b');
591
+ });
592
+ test('prefers the editor clipboard when it matches, to keep formatting', async () => {
593
+ clipboardWith(async () => [textItem('bold')]);
594
+ const editor = new TestEditor('‸*bold*‸ b');
595
+ editor.press({ key: 'c', metaKey: true });
596
+ editor.press({ key: 'ArrowRight' });
597
+ editor.press({ key: 'ArrowRight' });
598
+ editor.press({ key: 'v', metaKey: true });
599
+ await flush();
600
+ expect(editor.text).toBe('*bold* *bold*b');
601
+ });
602
+ test('falls back to the editor clipboard when the system one is unreadable', async () => {
603
+ clipboardWith(async () => {
604
+ throw new Error('denied');
605
+ });
606
+ const editor = new TestEditor('‸x‸ ');
607
+ editor.press({ key: 'c', metaKey: true });
608
+ editor.press({ key: 'ArrowRight' });
609
+ editor.press({ key: 'ArrowRight' });
610
+ editor.press({ key: 'v', metaKey: true });
611
+ await flush();
612
+ expect(editor.text).toBe('x x');
613
+ });
614
+ test('falls back to the editor clipboard without a clipboard API', async () => {
615
+ vi.stubGlobal('navigator', {});
616
+ const editor = new TestEditor('‸x‸ ');
617
+ editor.press({ key: 'c', metaKey: true });
618
+ editor.press({ key: 'ArrowRight' });
619
+ editor.press({ key: 'ArrowRight' });
620
+ editor.press({ key: 'v', metaKey: true });
621
+ expect(editor.text).toBe('x x');
622
+ });
623
+ test('the paste command itself adds nothing to the history', () => {
624
+ vi.stubGlobal('navigator', {});
625
+ const editor = new TestEditor('a‸');
626
+ editor.press({ key: 'v', metaKey: true });
627
+ expect(editor.undoStack).toEqual([]);
628
+ });
629
+ });
630
+ test('escapePlainText', () => {
631
+ expect(escapePlainText('a\\b')).toBe('a\\\\b');
632
+ expect(escapePlainText('# x\n\n- y')).toBe('\\# x\n\n\\- y');
633
+ });
634
+ });
635
+ describe('undo and redo', () => {
636
+ test('undo and redo an edit', () => {
637
+ const editor = new TestEditor('ab‸');
638
+ editor.type('c');
639
+ editor.type('d');
640
+ expect(editor.bookdown).toBe('abcd‸');
641
+ expect(editor.isActive('undo the last command')).toBe(true);
642
+ expect(editor.isActive('redo the most recently undone command')).toBe(false);
643
+ editor.press({ key: 'z', metaKey: true });
644
+ expect(editor.bookdown).toBe('abc‸');
645
+ editor.press({ key: 'z', metaKey: true });
646
+ expect(editor.bookdown).toBe('ab‸');
647
+ expect(editor.isActive('undo the last command')).toBe(false);
648
+ editor.press({ key: 'y', metaKey: true });
649
+ expect(editor.bookdown).toBe('abc‸');
650
+ editor.press({ key: 'y', metaKey: true });
651
+ expect(editor.bookdown).toBe('abcd‸');
652
+ expect(editor.isActive('redo the most recently undone command')).toBe(false);
653
+ });
654
+ test('undoing the first edit restores the original caret', () => {
655
+ const editor = new TestEditor('one\n\n‸two');
656
+ editor.execute('delete previous character');
657
+ expect(editor.bookdown).toBe('one‸two');
658
+ editor.execute('undo the last command');
659
+ expect(editor.bookdown).toBe('one\n\n‸two');
660
+ });
661
+ test('editing after undo discards the undone history', () => {
662
+ const editor = new TestEditor('a‸');
663
+ editor.type('b');
664
+ editor.type('c');
665
+ editor.execute('undo the last command');
666
+ editor.type('X');
667
+ expect(editor.bookdown).toBe('abX‸');
668
+ expect(editor.isActive('redo the most recently undone command')).toBe(false);
669
+ editor.execute('undo the last command');
670
+ expect(editor.bookdown).toBe('ab‸');
671
+ editor.execute('undo the last command');
672
+ expect(editor.bookdown).toBe('a‸');
673
+ });
674
+ test('withUndoState', () => {
675
+ const range = { start: 0, end: 0 };
676
+ const one = withUndoState([], -1, { bookdown: 'a', range }, {
677
+ bookdown: 'b',
678
+ command: undefined,
679
+ range,
680
+ });
681
+ expect(one.map((s) => s.bookdown)).toEqual(['b', 'a']);
682
+ const two = withUndoState(one, 0, { bookdown: 'b', range }, {
683
+ bookdown: 'c',
684
+ command: undefined,
685
+ range,
686
+ });
687
+ expect(two.map((s) => s.bookdown)).toEqual(['c', 'b', 'a']);
688
+ // After undoing to 'b', a new edit discards 'c'.
689
+ const three = withUndoState(two, 1, { bookdown: 'b', range }, {
690
+ bookdown: 'd',
691
+ command: undefined,
692
+ range,
693
+ });
694
+ expect(three.map((s) => s.bookdown)).toEqual(['d', 'b', 'a']);
695
+ });
696
+ });
697
+ describe('command metadata', () => {
698
+ test('descriptions are spelled correctly', () => {
699
+ const descriptions = commands.map((c) => c.description);
700
+ expect(descriptions).toContain('format as 4th level header');
701
+ expect(descriptions).toContain('move down one line');
702
+ expect(descriptions).not.toContain('move up down line');
703
+ });
704
+ test('text nodes are in the document after every mutating command', () => {
705
+ // A smoke test that runs every command at a few carets, checking each result is a consistent document.
706
+ for (const text of [
707
+ 'a‸b',
708
+ '* o‸ne\n* two',
709
+ ',a|‸b\n,c|d\n',
710
+ '‸hello',
711
+ 'a{no‸te}',
712
+ '# he‸ad',
713
+ 'a‸b‸c',
714
+ ]) {
715
+ for (const command of commands) {
716
+ if (command.category === 'history')
717
+ continue;
718
+ if (command.description.startsWith('paste'))
719
+ continue;
720
+ const editor = new TestEditor(text);
721
+ const active = command.active instanceof Function
722
+ ? command.active(editor.context(), 'x')
723
+ : command.active;
724
+ if (!active)
725
+ continue;
726
+ editor.execute(command, 'x');
727
+ const { start, end } = editor.range;
728
+ for (const caret of [start, end])
729
+ if (caret.node instanceof TextNode ||
730
+ caret.node instanceof AtomNode)
731
+ expect(editor.root.contains(caret.node), `${command.description} in ${text}: caret`).toBe(true);
732
+ // Indenting a first list item can't be reparsed; see the test.fails below.
733
+ if (command.description === 'indent list item')
734
+ continue;
735
+ // Comments at the start of table cells can't be reparsed; see the test.fails below.
736
+ if (command.description === 'insert comment' &&
737
+ text.startsWith(','))
738
+ continue;
739
+ // The document reparses to the same Bookdown. (Except for trailing whitespace:
740
+ // the parser drops an empty paragraph after a list, which is harmless.)
741
+ expect(editor.parse(editor.text).toBookdown().trimEnd(), `${command.description} in ${text}: reparse`).toBe(editor.text.trimEnd());
742
+ }
743
+ }
744
+ });
745
+ });
746
+ describe('atoms', () => {
747
+ test('a comment at the start of a paragraph keeps the paragraph when saved', () => {
748
+ const editor = new TestEditor('‸hello');
749
+ editor.execute('insert comment');
750
+ const reparsed = editor.parse(editor.text);
751
+ expect(reparsed.toText().trim()).toBe('hello');
752
+ expect(reparsed.getNodes().some((n) => n instanceof CommentNode)).toBe(true);
753
+ });
754
+ test('delete before a footnote deletes the footnote, not its text', () => {
755
+ const editor = new TestEditor('a‸{note}b');
756
+ editor.execute('delete next character');
757
+ expect(editor.text).toBe('ab');
758
+ });
759
+ test('backspace after a footnote deletes the footnote', () => {
760
+ const editor = new TestEditor('a{note}‸b');
761
+ editor.execute('delete previous character');
762
+ expect(editor.text).toBe('ab');
763
+ });
764
+ test('arrowing out of a footnote selects it', () => {
765
+ const editor = new TestEditor('a{not‸e}b');
766
+ editor.press({ key: 'ArrowRight' });
767
+ editor.press({ key: 'ArrowRight' });
768
+ expect(editor.range.start.node).toBeInstanceOf(FootnoteNode);
769
+ });
770
+ test('arrowing within a footnote crosses its formatting', () => {
771
+ const editor = new TestEditor('a{x‸ _y_ z}b');
772
+ editor.press({ key: 'ArrowRight' });
773
+ editor.press({ key: 'ArrowRight' });
774
+ expect(editor.bookdown).toBe('a{x _y‸_ z}b');
775
+ });
776
+ });
777
+ // These edits produce Bookdown that the parser reads differently. Fixing them needs parser changes
778
+ // (lists that start indented; whitespace before a comment at the start of a table cell).
779
+ describe('known round trip gaps', () => {
780
+ test.fails('an indented first list item survives saving', () => {
781
+ const editor = new TestEditor('* o‸ne\n* two');
782
+ editor.execute('indent list item');
783
+ expect(editor.text).toBe('** one\n* two');
784
+ expect(editor.parse(editor.text).toBookdown()).toBe(editor.text);
785
+ });
786
+ test.fails('a comment at the start of a table cell survives saving', () => {
787
+ const editor = new TestEditor(',‸a|b\n');
788
+ editor.execute('insert comment');
789
+ expect(editor.parse(editor.text).toBookdown()).toBe(editor.text);
790
+ });
791
+ });