diff --git a/src/nodes/Table/TableCell.js b/src/nodes/Table/TableCell.js index 0891633b0ad..aa622a8de03 100644 --- a/src/nodes/Table/TableCell.js +++ b/src/nodes/Table/TableCell.js @@ -37,27 +37,27 @@ export default TableCell.extend({ return [ new Plugin({ props: { - // Special-treat empty lines in pasted content to prevent jumping out of cell + // Only paste (marked) text into table cells to prevent jumping out of cell handlePaste: (view, event, slice) => { - if (slice.content.childCount > 1) { - const state = view.state - const childCount = slice.content.childCount - const childNodes = [] - for (let i = 0; i < childCount; i++) { - if (i === 0) { - childNodes.push(state.schema.text('\n')) - } - - // Ignore empty children (i.e. empty lines) - if (!slice.content.child(i).firstChild) { - continue - } + if (!this.editor.isActive(this.type.name)) { + return false + } - childNodes.push(state.schema.text(slice.content.child(i).textContent, slice.content.child(i).firstChild.marks)) + const { state } = view + const childNodes = [] + let newLineAdded = false + slice.content.descendants((node, pos) => { + if (node.isText) { + childNodes.push(state.schema.text(node.textContent, node.marks)) + newLineAdded = false + } else if (!newLineAdded) { + childNodes.push(state.schema.text('\n')) + newLineAdded = true } - const newNode = view.state.schema.node('paragraph', [], childNodes) - slice.content = Fragment.empty.addToStart(newNode) - } + }) + + const newNode = state.schema.node('paragraph', [], childNodes) + slice.content = Fragment.empty.addToStart(newNode) }, }, }),