Skip to content

Commit 93a2eb4

Browse files
mejo-backportbot-nextcloud[bot]
authored andcommitted
fix(frontend): Improve paste handler for table cells
This replaces the broken approach from #3906. * 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>
1 parent 3f4f6bd commit 93a2eb4

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/nodes/Table/TableCell.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ export default TableCell.extend({
3737
return [
3838
new Plugin({
3939
props: {
40-
// Special-treat empty lines in pasted content to prevent jumping out of cell
40+
// Only paste (marked) text into table cells to prevent jumping out of cell
4141
handlePaste: (view, event, slice) => {
42-
if (slice.content.childCount > 1) {
43-
const state = view.state
44-
const childCount = slice.content.childCount
45-
const childNodes = []
46-
for (let i = 0; i < childCount; i++) {
47-
if (i === 0) {
48-
childNodes.push(state.schema.text('\n'))
49-
}
50-
51-
// Ignore empty children (i.e. empty lines)
52-
if (!slice.content.child(i).firstChild) {
53-
continue
54-
}
42+
if (!this.editor.isActive(this.type.name)) {
43+
return false
44+
}
5545

56-
childNodes.push(state.schema.text(slice.content.child(i).textContent, slice.content.child(i).firstChild.marks))
46+
const { state } = view
47+
const childNodes = []
48+
let newLineAdded = false
49+
slice.content.descendants((node, pos) => {
50+
if (node.isText) {
51+
childNodes.push(state.schema.text(node.textContent, node.marks))
52+
newLineAdded = false
53+
} else if (!newLineAdded) {
54+
childNodes.push(state.schema.text('\n'))
55+
newLineAdded = true
5756
}
58-
const newNode = view.state.schema.node('paragraph', [], childNodes)
59-
slice.content = Fragment.empty.addToStart(newNode)
60-
}
57+
})
58+
59+
const newNode = state.schema.node('paragraph', [], childNodes)
60+
slice.content = Fragment.empty.addToStart(newNode)
6161
},
6262
},
6363
}),

0 commit comments

Comments
 (0)