fix delete node in table

pull/238/head
fantasticit 2022-11-28 10:52:18 +08:00
parent 103de8d263
commit ab6affa3cc
1 changed files with 20 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import { Editor } from '@tiptap/core';
export function deleteNode(nodeType: string, editor: Editor) { export function deleteNode(nodeType: string, editor: Editor) {
const { state } = editor; const { state } = editor;
const $pos = state.selection.$anchor; const $pos = state.selection.$anchor;
let done = false;
if ($pos.depth) { if ($pos.depth) {
for (let d = $pos.depth; d > 0; d--) { for (let d = $pos.depth; d > 0; d--) {
@ -12,7 +13,7 @@ export function deleteNode(nodeType: string, editor: Editor) {
if (editor.dispatchTransaction) if (editor.dispatchTransaction)
// @ts-ignore // @ts-ignore
editor.dispatchTransaction(state.tr.delete($pos.before(d), $pos.after(d)).scrollIntoView()); editor.dispatchTransaction(state.tr.delete($pos.before(d), $pos.after(d)).scrollIntoView());
return true; done = true;
} }
} }
} else { } else {
@ -20,8 +21,25 @@ export function deleteNode(nodeType: string, editor: Editor) {
const node = state.selection.node; const node = state.selection.node;
if (node && node.type.name === nodeType) { if (node && node.type.name === nodeType) {
editor.chain().deleteSelection().run(); editor.chain().deleteSelection().run();
done = true;
} }
} }
return false; if (!done) {
const pos = $pos.pos;
if (pos) {
const node = state.tr.doc.nodeAt(pos);
if (node && node.type.name === nodeType) {
// @ts-ignore
if (editor.dispatchTransaction)
// @ts-ignore
editor.dispatchTransaction(state.tr.delete(pos, pos + node.nodeSize));
done = true;
}
}
}
return done;
} }