|
| 1 | +import { Editor } from '@tiptap/core' |
| 2 | +import Document from '@tiptap/extension-document' |
| 3 | +import Paragraph from '@tiptap/extension-paragraph' |
| 4 | +import Text from '@tiptap/extension-text' |
| 5 | +import { TrailingNode } from '@tiptap/extensions' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +import { BulletList, ListItem, OrderedList } from '../src/index.js' |
| 9 | + |
| 10 | +describe('toggleList with trailing paragraph', () => { |
| 11 | + let editor: Editor |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + editor?.destroy() |
| 15 | + }) |
| 16 | + |
| 17 | + it('should toggle off bullet list when selecting all content including trailing paragraph', () => { |
| 18 | + editor = new Editor({ |
| 19 | + extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem, TrailingNode], |
| 20 | + content: '<ul><li><p>hello</p></li></ul>', |
| 21 | + }) |
| 22 | + |
| 23 | + // Select all content (Cmd+A) - this includes the trailing empty paragraph |
| 24 | + editor.commands.selectAll() |
| 25 | + |
| 26 | + // Toggle bullet list should remove the list, not wrap trailing paragraph |
| 27 | + editor.commands.toggleBulletList() |
| 28 | + |
| 29 | + // The content should be a plain paragraph, not a list |
| 30 | + const json = editor.getJSON() |
| 31 | + expect(json.content?.[0]?.type).toBe('paragraph') |
| 32 | + expect(json.content?.[0]?.content?.[0]?.text).toBe('hello') |
| 33 | + |
| 34 | + // Should not have bulletList in the document |
| 35 | + expect(json.content?.some(node => node.type === 'bulletList')).toBe(false) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should toggle off ordered list when selecting all content including trailing paragraph', () => { |
| 39 | + editor = new Editor({ |
| 40 | + extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem, TrailingNode], |
| 41 | + content: '<ol><li><p>hello</p></li></ol>', |
| 42 | + }) |
| 43 | + |
| 44 | + // Select all content (Cmd+A) |
| 45 | + editor.commands.selectAll() |
| 46 | + |
| 47 | + // Toggle ordered list should remove the list |
| 48 | + editor.commands.toggleOrderedList() |
| 49 | + |
| 50 | + // The content should be a plain paragraph, not a list |
| 51 | + const json = editor.getJSON() |
| 52 | + expect(json.content?.[0]?.type).toBe('paragraph') |
| 53 | + expect(json.content?.[0]?.content?.[0]?.text).toBe('hello') |
| 54 | + |
| 55 | + // Should not have orderedList in the document |
| 56 | + expect(json.content?.some(node => node.type === 'orderedList')).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should toggle off list with multiple items when selecting all', () => { |
| 60 | + editor = new Editor({ |
| 61 | + extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem, TrailingNode], |
| 62 | + content: '<ul><li><p>item 1</p></li><li><p>item 2</p></li><li><p>item 3</p></li></ul>', |
| 63 | + }) |
| 64 | + |
| 65 | + // Select all content |
| 66 | + editor.commands.selectAll() |
| 67 | + |
| 68 | + // Toggle bullet list should convert all items to paragraphs |
| 69 | + editor.commands.toggleBulletList() |
| 70 | + |
| 71 | + const json = editor.getJSON() |
| 72 | + |
| 73 | + // Should have 3 paragraphs |
| 74 | + expect(json.content?.length).toBeGreaterThanOrEqual(3) |
| 75 | + expect(json.content?.[0]?.type).toBe('paragraph') |
| 76 | + expect(json.content?.[0]?.content?.[0]?.text).toBe('item 1') |
| 77 | + expect(json.content?.[1]?.type).toBe('paragraph') |
| 78 | + expect(json.content?.[1]?.content?.[0]?.text).toBe('item 2') |
| 79 | + expect(json.content?.[2]?.type).toBe('paragraph') |
| 80 | + expect(json.content?.[2]?.content?.[0]?.text).toBe('item 3') |
| 81 | + |
| 82 | + // Should not have bulletList in the document |
| 83 | + expect(json.content?.some(node => node.type === 'bulletList')).toBe(false) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should work correctly when manually selecting only list content (without trailing paragraph)', () => { |
| 87 | + editor = new Editor({ |
| 88 | + extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem], |
| 89 | + content: '<ul><li><p>hello</p></li></ul>', |
| 90 | + }) |
| 91 | + |
| 92 | + // Manually select only the list content (not including trailing paragraph) |
| 93 | + // Position 0 is start of doc, list starts at 1, content is at position 2-7 |
| 94 | + editor.commands.setTextSelection({ from: 1, to: 9 }) |
| 95 | + |
| 96 | + // Toggle bullet list should remove the list |
| 97 | + editor.commands.toggleBulletList() |
| 98 | + |
| 99 | + const json = editor.getJSON() |
| 100 | + expect(json.content?.[0]?.type).toBe('paragraph') |
| 101 | + expect(json.content?.[0]?.content?.[0]?.text).toBe('hello') |
| 102 | + }) |
| 103 | + |
| 104 | + it('should handle list with non-empty trailing paragraph correctly', () => { |
| 105 | + editor = new Editor({ |
| 106 | + extensions: [Document, Paragraph, Text, BulletList, OrderedList, ListItem, TrailingNode], |
| 107 | + content: '<ul><li><p>list item</p></li></ul><p>after list</p>', |
| 108 | + }) |
| 109 | + |
| 110 | + // Select all content - this includes the non-empty paragraph after the list |
| 111 | + editor.commands.selectAll() |
| 112 | + |
| 113 | + // Toggle bullet list - this should wrap "after list" into the list |
| 114 | + // because it's not an empty trailing paragraph (it has content) |
| 115 | + editor.commands.toggleBulletList() |
| 116 | + |
| 117 | + const json = editor.getJSON() |
| 118 | + |
| 119 | + // The behavior with non-empty trailing content is different: |
| 120 | + // It should wrap the trailing paragraph into the list |
| 121 | + expect(json.content?.[0]?.type).toBe('bulletList') |
| 122 | + |
| 123 | + // Should have 2 list items now |
| 124 | + const bulletList = json.content?.[0] |
| 125 | + expect(bulletList?.content?.length).toBe(2) |
| 126 | + expect(bulletList?.content?.[0]?.content?.[0]?.content?.[0]?.text).toBe('list item') |
| 127 | + expect(bulletList?.content?.[1]?.content?.[0]?.content?.[0]?.text).toBe('after list') |
| 128 | + }) |
| 129 | +}) |
0 commit comments