Skip to content

Commit

Permalink
fix(frontend): Improve paste handler for table cells
Browse files Browse the repository at this point in the history
* Only regard paste handler if pasting to a table cell (Fixes: #4443)
* Add all (marked) text nodes with newlines in between
* Only add a newline for non-text nodes to prevent newlines in between
  text with changing marks.

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Jul 7, 2023
1 parent 1a1ebde commit 09db13b
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/nodes/Table/TableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
}),
Expand Down

0 comments on commit 09db13b

Please sign in to comment.