From 6cb1c0436b0115e094e3738807b690555a992579 Mon Sep 17 00:00:00 2001 From: Chrissi2812 Date: Mon, 20 May 2019 14:30:40 +0200 Subject: [PATCH] use findParentNode to find parent list instead of arbitary offsets --- packages/tiptap-commands/package.json | 1 + .../src/commands/toggleList.js | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/tiptap-commands/package.json b/packages/tiptap-commands/package.json index 9b1fa756..0a882b86 100644 --- a/packages/tiptap-commands/package.json +++ b/packages/tiptap-commands/package.json @@ -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" } } diff --git a/packages/tiptap-commands/src/commands/toggleList.js b/packages/tiptap-commands/src/commands/toggleList.js index 0a7a22d7..23529fdf 100644 --- a/packages/tiptap-commands/src/commands/toggleList.js +++ b/packages/tiptap-commands/src/commands/toggleList.js @@ -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)