|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { createRichEditor } from '../../EditorFactory' |
| 7 | +import markdownit from '../../markdownit/index.js' |
| 8 | + |
| 9 | +describe('List type conversion', () => { |
| 10 | + describe('editor.can() — menubar enabled state', () => { |
| 11 | + it('toggleTaskList() returns true when cursor is inside a bulletList', () => { |
| 12 | + const editor = createRichEditor() |
| 13 | + editor.commands.setContent(markdownit.render('- item\n')) |
| 14 | + editor.commands.focus('start') |
| 15 | + expect(editor.can().toggleTaskList()).to.equal(true) |
| 16 | + editor.destroy() |
| 17 | + }) |
| 18 | + it('toggleTaskList() returns true when cursor is inside an orderedList', () => { |
| 19 | + const editor = createRichEditor() |
| 20 | + editor.commands.setContent(markdownit.render('1. item\n')) |
| 21 | + editor.commands.focus('start') |
| 22 | + expect(editor.can().toggleTaskList()).to.equal(true) |
| 23 | + editor.destroy() |
| 24 | + }) |
| 25 | + it('toggleBulletList() returns true when cursor is inside a taskList', () => { |
| 26 | + const editor = createRichEditor() |
| 27 | + editor.commands.setContent(markdownit.render('- [ ] item\n')) |
| 28 | + editor.commands.focus('start') |
| 29 | + expect(editor.can().toggleBulletList()).to.equal(true) |
| 30 | + editor.destroy() |
| 31 | + }) |
| 32 | + it('toggleOrderedList() returns true when cursor is inside a taskList', () => { |
| 33 | + const editor = createRichEditor() |
| 34 | + editor.commands.setContent(markdownit.render('- [ ] item\n')) |
| 35 | + editor.commands.focus('start') |
| 36 | + expect(editor.can().toggleOrderedList()).to.equal(true) |
| 37 | + editor.destroy() |
| 38 | + }) |
| 39 | + }) |
| 40 | + describe('conversion at top level', () => { |
| 41 | + it('converts all items when toggling bulletList → taskList', () => { |
| 42 | + const editor = createRichEditor() |
| 43 | + editor.commands.setContent(markdownit.render('- item 1\n- item 2\n')) |
| 44 | + editor.commands.focus('start') |
| 45 | + editor.commands.toggleTaskList() |
| 46 | + const list = editor.state.doc.firstChild! |
| 47 | + expect(list.type.name).to.equal('taskList') |
| 48 | + expect(list.childCount).to.equal(2) |
| 49 | + expect(list.child(0).type.name).to.equal('taskItem') |
| 50 | + expect(list.child(0).attrs.checked).to.equal(false) |
| 51 | + expect(list.child(0).firstChild!.textContent).to.equal('item 1') |
| 52 | + expect(list.child(1).firstChild!.textContent).to.equal('item 2') |
| 53 | + editor.destroy() |
| 54 | + }) |
| 55 | + it('converts all items when toggling taskList → bulletList', () => { |
| 56 | + const editor = createRichEditor() |
| 57 | + editor.commands.setContent(markdownit.render('- [ ] item 1\n- [x] item 2\n')) |
| 58 | + editor.commands.focus('start') |
| 59 | + editor.commands.toggleBulletList() |
| 60 | + const list = editor.state.doc.firstChild! |
| 61 | + expect(list.type.name).to.equal('bulletList') |
| 62 | + expect(list.child(0).type.name).to.equal('listItem') |
| 63 | + expect(list.child(0).firstChild!.textContent).to.equal('item 1') |
| 64 | + expect(list.child(1).firstChild!.textContent).to.equal('item 2') |
| 65 | + editor.destroy() |
| 66 | + }) |
| 67 | + it('converts all items when toggling orderedList → taskList', () => { |
| 68 | + const editor = createRichEditor() |
| 69 | + editor.commands.setContent(markdownit.render('1. item 1\n2. item 2\n')) |
| 70 | + editor.commands.focus('start') |
| 71 | + editor.commands.toggleTaskList() |
| 72 | + const list = editor.state.doc.firstChild! |
| 73 | + expect(list.type.name).to.equal('taskList') |
| 74 | + expect(list.child(0).type.name).to.equal('taskItem') |
| 75 | + editor.destroy() |
| 76 | + }) |
| 77 | + }) |
| 78 | + describe('conversion at nested level', () => { |
| 79 | + it('converts only the innermost list when cursor is in a nested item', () => { |
| 80 | + const editor = createRichEditor() |
| 81 | + editor.commands.setContent(markdownit.render('- outer\n - inner\n')) |
| 82 | + // Position cursor inside the nested "inner" paragraph |
| 83 | + let innerPos = -1 |
| 84 | + editor.state.doc.descendants((node, pos) => { |
| 85 | + if (node.type.name === 'paragraph' && node.textContent === 'inner') { |
| 86 | + innerPos = pos + 1 |
| 87 | + } |
| 88 | + }) |
| 89 | + editor.commands.setTextSelection(innerPos) |
| 90 | + editor.commands.toggleTaskList() |
| 91 | + const outerList = editor.state.doc.firstChild! |
| 92 | + expect(outerList.type.name).to.equal('bulletList') // outer unchanged |
| 93 | + const outerItem = outerList.firstChild! |
| 94 | + const innerList = outerItem.lastChild! |
| 95 | + expect(innerList.type.name).to.equal('taskList') // inner converted |
| 96 | + expect(innerList.firstChild!.type.name).to.equal('taskItem') |
| 97 | + expect(innerList.firstChild!.firstChild!.textContent).to.equal('inner') |
| 98 | + editor.destroy() |
| 99 | + }) |
| 100 | + it('can().toggleTaskList() returns true for a nested item inside a bulletList', () => { |
| 101 | + const editor = createRichEditor() |
| 102 | + editor.commands.setContent(markdownit.render('- outer\n - inner\n')) |
| 103 | + let innerPos = -1 |
| 104 | + editor.state.doc.descendants((node, pos) => { |
| 105 | + if (node.type.name === 'paragraph' && node.textContent === 'inner') { |
| 106 | + innerPos = pos + 1 |
| 107 | + } |
| 108 | + }) |
| 109 | + editor.commands.setTextSelection(innerPos) |
| 110 | + expect(editor.can().toggleTaskList()).to.equal(true) |
| 111 | + editor.destroy() |
| 112 | + }) |
| 113 | + }) |
| 114 | + it('preserves cursor position when converting bulletList → taskList', () => { |
| 115 | + const editor = createRichEditor() |
| 116 | + editor.commands.setContent(markdownit.render('- item 1\n- item 2\n')) |
| 117 | + editor.commands.focus('start') |
| 118 | + const posBefore = editor.state.selection.from |
| 119 | + editor.commands.toggleTaskList() |
| 120 | + expect(editor.state.selection.from).to.equal(posBefore) |
| 121 | + editor.destroy() |
| 122 | + }) |
| 123 | +}) |
0 commit comments