Skip to content

Commit

Permalink
fix: paste multiple line to table
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <luka@nextcloud.com>
  • Loading branch information
luka-nextcloud committed Mar 14, 2023
1 parent 51e9463 commit 39df7ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
23 changes: 0 additions & 23 deletions src/nodes/Table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
selectedRect,
selectionCell,
} from '@tiptap/pm/tables'
// eslint-disable-next-line n/no-extraneous-import
import { Fragment } from 'prosemirror-model'

/**
*
Expand Down Expand Up @@ -209,25 +207,4 @@ export default Table.extend({
}
},

addProseMirrorPlugins() {
return [
new Plugin({
props: {
handlePaste: (view, event, slice) => {
if (slice.content.childCount > 1) {
const firstNode = slice.content.firstChild
let newTextContent = firstNode.textContent

for (let i = 1; i < slice.content.childCount; i++) {
newTextContent += '\n' + slice.content.child(i).textContent
}

slice.content = Fragment.empty.addToStart(view.state.schema.text(newTextContent, firstNode.marks))
}
},
},
}),
]
},

})
25 changes: 25 additions & 0 deletions src/nodes/Table/TableCell.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TableCell } from '@tiptap/extension-table-cell'
import { Plugin } from '@tiptap/pm/state'
import { Fragment } from '@tiptap/pm/model'

export default TableCell.extend({
content: 'inline*',
Expand Down Expand Up @@ -30,4 +32,27 @@ export default TableCell.extend({
},
}
},

addProseMirrorPlugins() {
return [
new Plugin({
props: {
handlePaste: (view, event, slice) => {
if (slice.content.childCount > 1) {
const state = view.state
const childNodes = []
for (let i = 0; i < slice.content.childCount; i++) {
if (i) {
childNodes.push(state.schema.text('\n'))
}
childNodes.push(state.schema.text(slice.content.child(i).textContent, slice.content.child(i).firstChild.marks))
}
const newNode = view.state.schema.node('paragraph', [], childNodes)
slice.content = Fragment.empty.addToStart(newNode)
}
},
},
}),
]
},
})

0 comments on commit 39df7ed

Please sign in to comment.