Skip to content

Commit 39df7ed

Browse files
fix: paste multiple line to table
Signed-off-by: Luka Trovic <luka@nextcloud.com>
1 parent 51e9463 commit 39df7ed

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/nodes/Table/Table.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
selectedRect,
1515
selectionCell,
1616
} from '@tiptap/pm/tables'
17-
// eslint-disable-next-line n/no-extraneous-import
18-
import { Fragment } from 'prosemirror-model'
1917

2018
/**
2119
*
@@ -209,25 +207,4 @@ export default Table.extend({
209207
}
210208
},
211209

212-
addProseMirrorPlugins() {
213-
return [
214-
new Plugin({
215-
props: {
216-
handlePaste: (view, event, slice) => {
217-
if (slice.content.childCount > 1) {
218-
const firstNode = slice.content.firstChild
219-
let newTextContent = firstNode.textContent
220-
221-
for (let i = 1; i < slice.content.childCount; i++) {
222-
newTextContent += '\n' + slice.content.child(i).textContent
223-
}
224-
225-
slice.content = Fragment.empty.addToStart(view.state.schema.text(newTextContent, firstNode.marks))
226-
}
227-
},
228-
},
229-
}),
230-
]
231-
},
232-
233210
})

src/nodes/Table/TableCell.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { TableCell } from '@tiptap/extension-table-cell'
2+
import { Plugin } from '@tiptap/pm/state'
3+
import { Fragment } from '@tiptap/pm/model'
24

35
export default TableCell.extend({
46
content: 'inline*',
@@ -30,4 +32,27 @@ export default TableCell.extend({
3032
},
3133
}
3234
},
35+
36+
addProseMirrorPlugins() {
37+
return [
38+
new Plugin({
39+
props: {
40+
handlePaste: (view, event, slice) => {
41+
if (slice.content.childCount > 1) {
42+
const state = view.state
43+
const childNodes = []
44+
for (let i = 0; i < slice.content.childCount; i++) {
45+
if (i) {
46+
childNodes.push(state.schema.text('\n'))
47+
}
48+
childNodes.push(state.schema.text(slice.content.child(i).textContent, slice.content.child(i).firstChild.marks))
49+
}
50+
const newNode = view.state.schema.node('paragraph', [], childNodes)
51+
slice.content = Fragment.empty.addToStart(newNode)
52+
}
53+
},
54+
},
55+
}),
56+
]
57+
},
3358
})

0 commit comments

Comments
 (0)