use findParentNode to find parent list instead of arbitary offsets

This commit is contained in:
Chrissi2812 2019-05-20 14:30:40 +02:00
parent ac89ca404a
commit 6cb1c0436b
No known key found for this signature in database
GPG Key ID: B4B82C7E618271DA
2 changed files with 16 additions and 12 deletions

View File

@ -25,6 +25,7 @@
"prosemirror-model": "^1.7.0",
"prosemirror-schema-list": "^1.0.3",
"prosemirror-state": "^1.2.3",
"prosemirror-utils": "^0.8.1",
"tiptap-utils": "^1.5.2"
}
}

View File

@ -1,4 +1,5 @@
import { wrapInList, liftListItem } from 'prosemirror-schema-list'
import { findParentNode } from 'prosemirror-utils'
function isList(node, schema) {
return (node.type === schema.nodes.bullet_list || node.type === schema.nodes.ordered_list)
@ -6,26 +7,28 @@ function isList(node, schema) {
export default function toggleList(listType) {
return (state, dispatch) => {
const { schema } = state
const { schema, selection } = state
const lift = liftListItem(schema.nodes.list_item)
const wrap = wrapInList(listType)
const { $from, $to } = state.selection
const { $from, $to } = selection
const range = $from.blockRange($to)
const depthOffset = range.depth > 1 ? ((range.depth + 1) % 2) : 0
if (!range) {
return false
}
if (range.depth >= 1 && $from.node(range.depth - depthOffset).type === listType) {
return lift(state, dispatch)
}
const parentList = findParentNode(node => isList(node, schema))(selection)
if (range.depth >= 1 && isList($from.node(range.depth - depthOffset), schema)) {
const { tr } = state
const $insert = state.doc.resolve(range.start - depthOffset)
tr.setNodeMarkup(range.start - (1 + depthOffset) - $insert.parentOffset, listType)
if (dispatch) dispatch(tr)
return false
if (range.depth >= 1 && parentList) {
if (parentList.node.type === listType) {
return lift(state, dispatch)
}
if (isList(parentList.node, schema)) {
const { tr } = state
tr.setNodeMarkup(parentList.pos, listType)
if (dispatch) dispatch(tr)
return false
}
}
return wrap(state, dispatch)