use findParentNode to find parent list instead of arbitary offsets
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
"prosemirror-model": "^1.7.0",
|
"prosemirror-model": "^1.7.0",
|
||||||
"prosemirror-schema-list": "^1.0.3",
|
"prosemirror-schema-list": "^1.0.3",
|
||||||
"prosemirror-state": "^1.2.3",
|
"prosemirror-state": "^1.2.3",
|
||||||
|
"prosemirror-utils": "^0.8.1",
|
||||||
"tiptap-utils": "^1.5.2"
|
"tiptap-utils": "^1.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
|
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
|
||||||
|
import { findParentNode } from 'prosemirror-utils'
|
||||||
|
|
||||||
function isList(node, schema) {
|
function isList(node, schema) {
|
||||||
return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list)
|
return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list)
|
||||||
@@ -6,27 +7,29 @@ function isList(node, schema) {
|
|||||||
|
|
||||||
export default function toggleList(listType) {
|
export default function toggleList(listType) {
|
||||||
return (state, dispatch) => {
|
return (state, dispatch) => {
|
||||||
const { schema } = state
|
const { schema, selection } = state
|
||||||
const lift = liftListItem(schema.nodes.list_item)
|
const lift = liftListItem(schema.nodes.list_item)
|
||||||
const wrap = wrapInList(listType)
|
const wrap = wrapInList(listType)
|
||||||
const { $from, $to } = state.selection
|
const { $from, $to } = selection
|
||||||
const range = $from.blockRange($to)
|
const range = $from.blockRange($to)
|
||||||
const depthOffset = range.depth > 1 ? ((range.depth + 1) % 2) : 0
|
|
||||||
if (!range) {
|
if (!range) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range.depth >= 1 && $from.node(range.depth - depthOffset).type === listType) {
|
const parentList = findParentNode(node => isList(node, schema))(selection)
|
||||||
|
|
||||||
|
if (range.depth >= 1 && parentList) {
|
||||||
|
if (parentList.node.type === listType) {
|
||||||
return lift(state, dispatch)
|
return lift(state, dispatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range.depth >= 1 && isList($from.node(range.depth - depthOffset), schema)) {
|
if (isList(parentList.node, schema)) {
|
||||||
const { tr } = state
|
const { tr } = state
|
||||||
const $insert = state.doc.resolve(range.start - depthOffset)
|
tr.setNodeMarkup(parentList.pos, listType)
|
||||||
tr.setNodeMarkup(range.start - (1 + depthOffset) - $insert.parentOffset, listType)
|
|
||||||
if (dispatch) dispatch(tr)
|
if (dispatch) dispatch(tr)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return wrap(state, dispatch)
|
return wrap(state, dispatch)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user