Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(editor): preserve trailing mixed-list items when outdenting (#35176)

OrcaWin5058476b157d
diff --git a/src/renderer/src/components/editor/rich-markdown-list-indent.ts b/src/renderer/src/components/editor/rich-markdown-list-indent.ts
index 4e77c6b51b0..b4f3ddc740d 100644
--- a/src/renderer/src/components/editor/rich-markdown-list-indent.ts
+++ b/src/renderer/src/components/editor/rich-markdown-list-indent.ts
@@ -1,5 +1,5 @@
import type { Editor } from '@tiptap/react'
-import { Fragment } from '@tiptap/pm/model'
+import { Fragment, type Attrs } from '@tiptap/pm/model'
import { TextSelection } from '@tiptap/pm/state'
type ListItemTypeName = 'listItem' | 'taskItem'
@@ -65,10 +65,21 @@ function splitListBeforeCursorItem(editor: Editor): void {
}
const { state } = editor
const { $from } = state.selection
- if ($from.index(context.itemDepth - 1) === 0) {
+ const index = $from.index(context.itemDepth - 1)
+ if (index === 0) {
return
}
- editor.view.dispatch(state.tr.split($from.before(context.itemDepth), 1))
+ const list = $from.node(context.itemDepth - 1)
+ editor.view.dispatch(
+ state.tr.split($from.before(context.itemDepth), 1, [
+ { type: list.type, attrs: withShiftedStart(list.attrs, index) }
+ ])
+ )
+}
+
+/** Keep ordered-list numbering when a list is split or its tail is moved. */
+function withShiftedStart(attrs: Attrs, offset: number): Attrs {
+ return typeof attrs.start === 'number' ? { ...attrs, start: attrs.start + offset } : attrs
}
function retypeListAtCursor(editor: Editor, target: ListItemTypeName): void {
@@ -86,14 +97,27 @@ function retypeListAtCursor(editor: Editor, target: ListItemTypeName): void {
// Replace the subtree atomically because separately retyping list/items creates schema-invalid intermediate states.
const itemType = state.schema.nodes[target]
- const items = list.children.map((item) => itemType.create(null, item.content, item.marks))
+ const range = state.selection.$from.blockRange(state.selection.$to, (node) => node === list)
+ const selectedEnd = range?.endIndex ?? list.childCount
+ const trailing = Fragment.fromArray(list.children.slice(selectedEnd))
+ const items = list.children.slice(0, selectedEnd).map((item, index) => {
+ const content =
+ index === selectedEnd - 1 && trailing.size > 0
+ ? item.content.append(
+ Fragment.from(
+ list.type.create(withShiftedStart(list.attrs, selectedEnd), trailing, list.marks)
+ )
+ )
+ : item.content
+ return itemType.create(null, content, item.marks)
+ })
const retyped = state.schema.nodes[LIST_TYPE_FOR_ITEM[target]].create(
null,
Fragment.from(items),
list.marks
)
const tr = state.tr.replaceWith(listPos, listPos + list.nodeSize, retyped)
- // Size-preserving retyping deletes/reinserts the subtree; restore selection rather than keeping replaceWith’s collapsed cursor.
+ // Retyping replaces the subtree; keep the selected text instead of the replacement cursor.
tr.setSelection(TextSelection.create(tr.doc, from, to))
editor.view.dispatch(tr)
}
diff --git a/src/renderer/src/components/editor/rich-markdown-list-outdent.test.ts b/src/renderer/src/components/editor/rich-markdown-list-outdent.test.ts
new file mode 100644
index 00000000000..5626c91794d
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-list-outdent.test.ts
@@ -0,0 +1,109 @@
+// @vitest-environment happy-dom
+
+import { describe, expect, it, vi } from 'vitest'
+import { Editor } from '@tiptap/core'
+import { createRichMarkdownExtensions } from './rich-markdown-extensions'
+import { createRichMarkdownEditorCodec } from './rich-markdown-source-transport'
+import { outdentRichMarkdownListItem } from './rich-markdown-list-indent'
+
+function createEditor(content: string): Editor {
+ const editor = new Editor({
+ element: document.createElement('div'),
+ extensions: createRichMarkdownExtensions({ codec: createRichMarkdownEditorCodec() }),
+ content,
+ contentType: 'markdown'
+ })
+ vi.spyOn(editor.view, 'coordsAtPos').mockReturnValue({ top: 0, bottom: 20, left: 0, right: 0 })
+ return editor
+}
+
+function itemAtText(editor: Editor, text: string) {
+ let result: { position: number; node: ReturnType<typeof editor.state.doc.child> } | undefined
+ editor.state.doc.descendants((node, position) => {
+ if (
+ (node.type.name === 'listItem' || node.type.name === 'taskItem') &&
+ node.firstChild?.textContent === text
+ ) {
+ result = { position, node }
+ }
+ })
+ if (!result) {
+ throw new Error(`Expected list item: ${text}`)
+ }
+ return result
+}
+
+describe('mixed list outdent', () => {
+ it.each([false, true])(
+ 'preserves checked tasks after a selected item range (reverse %s)',
+ (reverse) => {
+ const editor = createEditor('- Parent\n - [ ] First\n - [x] Middle\n - [x] Last')
+ try {
+ const first = itemAtText(editor, 'First').position + 3
+ const middle = itemAtText(editor, 'Middle').position + 4
+ const originalLast = itemAtText(editor, 'Last').node.toJSON()
+ editor.commands.setTextSelection({
+ from: reverse ? middle : first,
+ to: reverse ? first : middle
+ })
+ const before = editor.getJSON()
+
+ expect(outdentRichMarkdownListItem(editor)).toBe(true)
+
+ expect(itemAtText(editor, 'Last').node.toJSON()).toEqual(originalLast)
+ for (const text of ['First', 'Middle']) {
+ expect(editor.state.doc.resolve(itemAtText(editor, text).position).depth).toBe(1)
+ }
+ const after = editor.getJSON()
+ expect(editor.commands.undo()).toBe(true)
+ expect(editor.getJSON()).toEqual(before)
+ expect(editor.commands.redo()).toBe(true)
+ expect(editor.getJSON()).toEqual(after)
+ } finally {
+ editor.destroy()
+ }
+ }
+ )
+
+ it.each([
+ ['- Parent\n - [ ] First\n - [x] Middle\n - [x] Last', 'First'],
+ ['- Parent\n - [ ] First\n - [x] Middle\n - [x] Last', 'Middle'],
+ ['- [ ] Parent\n - First\n - Middle\n - **Last**', 'First'],
+ ['- [ ] Parent\n 4. First\n 5. Middle\n 6. **Last**', 'Middle']
+ ])('preserves the unselected trailing item after outdenting %s at %s', (source, selected) => {
+ const editor = createEditor(source)
+ try {
+ const last = itemAtText(editor, 'Last')
+ const originalLast = last.node.toJSON()
+ const $originalLast = editor.state.doc.resolve(last.position)
+ const originalList = $originalLast.parent
+ const originalIndex = $originalLast.index()
+ const selectedItem = itemAtText(editor, selected)
+ editor.commands.setTextSelection(selectedItem.position + 3)
+ const before = editor.getJSON()
+
+ expect(outdentRichMarkdownListItem(editor)).toBe(true)
+
+ const result = itemAtText(editor, 'Last')
+ expect(result.node.toJSON()).toEqual(originalLast)
+ const $result = editor.state.doc.resolve(result.position)
+ const resultList = $result.parent
+ expect(resultList.type).toBe(originalList.type)
+ expect({ ...resultList.attrs, start: null }).toEqual({ ...originalList.attrs, start: null })
+ if (typeof originalList.attrs.start === 'number') {
+ expect(resultList.attrs.start + $result.index()).toBe(
+ originalList.attrs.start + originalIndex
+ )
+ }
+ const lifted = itemAtText(editor, selected)
+ expect(editor.state.doc.resolve(lifted.position).depth).toBe(1)
+ const after = editor.getJSON()
+ expect(editor.commands.undo()).toBe(true)
+ expect(editor.getJSON()).toEqual(before)
+ expect(editor.commands.redo()).toBe(true)
+ expect(editor.getJSON()).toEqual(after)
+ } finally {
+ editor.destroy()
+ }
+ })
+})