From 4717d7ff9a42050c95d192cf8da84ca49623315d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sun, 28 Mar 2021 21:30:47 +0200 Subject: [PATCH] fix more commands for cell selections --- packages/core/src/commands/clearNodes.ts | 30 ++++++++++--------- .../core/src/commands/resetNodeAttributes.ts | 12 ++++---- packages/core/src/commands/unsetAllMarks.ts | 6 ++-- .../core/src/commands/updateNodeAttributes.ts | 18 ++++++----- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/packages/core/src/commands/clearNodes.ts b/packages/core/src/commands/clearNodes.ts index 03e12fff..ac4bbba0 100644 --- a/packages/core/src/commands/clearNodes.ts +++ b/packages/core/src/commands/clearNodes.ts @@ -14,26 +14,28 @@ declare module '@tiptap/core' { export const clearNodes: RawCommands['clearNodes'] = () => ({ state, tr, dispatch }) => { const { selection } = tr - const { from, to } = selection + const { ranges } = selection - state.doc.nodesBetween(from, to, (node, pos) => { - if (!node.type.isText) { - const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1)) - const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1)) - const nodeRange = fromPos.blockRange(toPos) + ranges.forEach(range => { + state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { + if (!node.type.isText) { + const fromPos = tr.doc.resolve(tr.mapping.map(pos + 1)) + const toPos = tr.doc.resolve(tr.mapping.map(pos + node.nodeSize - 1)) + const nodeRange = fromPos.blockRange(toPos) - if (nodeRange) { - const targetLiftDepth = liftTarget(nodeRange) + if (nodeRange) { + const targetLiftDepth = liftTarget(nodeRange) - if (node.type.isTextblock && dispatch) { - tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) - } + if (node.type.isTextblock && dispatch) { + tr.setNodeMarkup(nodeRange.start, state.doc.type.contentMatch.defaultType) + } - if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { - tr.lift(nodeRange, targetLiftDepth) + if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { + tr.lift(nodeRange, targetLiftDepth) + } } } - } + }) }) return true diff --git a/packages/core/src/commands/resetNodeAttributes.ts b/packages/core/src/commands/resetNodeAttributes.ts index caf1af09..6a51546e 100644 --- a/packages/core/src/commands/resetNodeAttributes.ts +++ b/packages/core/src/commands/resetNodeAttributes.ts @@ -17,12 +17,14 @@ declare module '@tiptap/core' { export const resetNodeAttributes: RawCommands['resetNodeAttributes'] = (typeOrName, attributes) => ({ tr, state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const { selection } = tr - const { from, to } = selection + const { ranges } = selection - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.type === type && dispatch) { - tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes)) - } + ranges.forEach(range => { + state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { + if (node.type === type && dispatch) { + tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes)) + } + }) }) return true diff --git a/packages/core/src/commands/unsetAllMarks.ts b/packages/core/src/commands/unsetAllMarks.ts index 310bb92b..b84307d0 100644 --- a/packages/core/src/commands/unsetAllMarks.ts +++ b/packages/core/src/commands/unsetAllMarks.ts @@ -13,7 +13,7 @@ declare module '@tiptap/core' { export const unsetAllMarks: RawCommands['unsetAllMarks'] = () => ({ tr, state, dispatch }) => { const { selection } = tr - const { from, to, empty } = selection + const { empty, ranges } = selection if (empty) { return true @@ -23,7 +23,9 @@ export const unsetAllMarks: RawCommands['unsetAllMarks'] = () => ({ tr, state, d Object .entries(state.schema.marks) .forEach(([, mark]) => { - tr.removeMark(from, to, mark as any) + ranges.forEach(range => { + tr.removeMark(range.$from.pos, range.$to.pos, mark as any) + }) }) } diff --git a/packages/core/src/commands/updateNodeAttributes.ts b/packages/core/src/commands/updateNodeAttributes.ts index 8c314ceb..fd16f1e5 100644 --- a/packages/core/src/commands/updateNodeAttributes.ts +++ b/packages/core/src/commands/updateNodeAttributes.ts @@ -16,15 +16,17 @@ declare module '@tiptap/core' { export const updateNodeAttributes: RawCommands['updateNodeAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const { selection } = tr - const { from, to } = selection + const { ranges } = selection - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.type === type && dispatch) { - tr.setNodeMarkup(pos, undefined, { - ...node.attrs, - ...attributes, - }) - } + ranges.forEach(range => { + state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { + if (node.type === type && dispatch) { + tr.setNodeMarkup(pos, undefined, { + ...node.attrs, + ...attributes, + }) + } + }) }) return true