update splitListItem (https://github.com/ProseMirror/prosemirror/issues/1146)
This commit is contained in:
@@ -57,14 +57,26 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
|
||||
|
||||
if (dispatch) {
|
||||
let wrap = Fragment.empty
|
||||
const keepItem = $from.index(-1) > 0
|
||||
// eslint-disable-next-line
|
||||
const depthBefore = $from.index(-1)
|
||||
? 1
|
||||
: $from.index(-2)
|
||||
? 2
|
||||
: 3
|
||||
|
||||
// Build a fragment containing empty versions of the structure
|
||||
// from the outer list item to the parent node of the cursor
|
||||
for (let d = $from.depth - (keepItem ? 1 : 2); d >= $from.depth - 3; d -= 1) {
|
||||
for (let d = $from.depth - depthBefore; d >= $from.depth - 3; d -= 1) {
|
||||
wrap = Fragment.from($from.node(d).copy(wrap))
|
||||
}
|
||||
|
||||
// eslint-disable-next-line
|
||||
const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount
|
||||
? 1
|
||||
: $from.indexAfter(-2) < $from.node(-3).childCount
|
||||
? 2
|
||||
: 3
|
||||
|
||||
// Add a second list item with an empty default start node
|
||||
const newNextTypeAttributes = getSplittedAttributes(
|
||||
extensionAttributes,
|
||||
@@ -72,16 +84,30 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
|
||||
$from.node().attrs,
|
||||
)
|
||||
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
|
||||
|
||||
wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || undefined))
|
||||
|
||||
tr
|
||||
.replace(
|
||||
$from.before(keepItem ? undefined : -1),
|
||||
$from.after(-3),
|
||||
new Slice(wrap, keepItem ? 3 : 2, 2),
|
||||
)
|
||||
.setSelection(TextSelection.near(tr.doc.resolve($from.pos + (keepItem ? 3 : 2))))
|
||||
.scrollIntoView()
|
||||
const start = $from.before($from.depth - (depthBefore - 1))
|
||||
|
||||
tr.replace(start, $from.after(-depthAfter), new Slice(wrap, 4 - depthBefore, 0))
|
||||
|
||||
let sel = -1
|
||||
|
||||
tr.doc.nodesBetween(start, tr.doc.content.size, (n, pos) => {
|
||||
if (sel > -1) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (n.isTextblock && n.content.size === 0) {
|
||||
sel = pos + 1
|
||||
}
|
||||
})
|
||||
|
||||
if (sel > -1) {
|
||||
tr.setSelection(TextSelection.near(tr.doc.resolve(sel)))
|
||||
}
|
||||
|
||||
tr.scrollIntoView()
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user