Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: paste multiple line to table issue #3906

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cypress/e2e/nodes/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ describe('table plugin', () => {
.should('have.length', 3)
.each(td => cy.wrap(td).should('have.css', 'text-align', 'center'))
})

it('Creates table and add multilines', function() {
const multilinesContent = 'Line 1\nLine 2\nLine 3'

cy.getActionEntry('table').click()
cy.getContent()
.find('table:nth-of-type(1) tr:nth-child(2) td:nth-child(1)')
.click()

cy.getContent()
.type(multilinesContent)

cy.getContent()
.find('table:nth-of-type(1) tr:nth-child(2) td:nth-child(1) .content')
.then(($el) => {
expect($el.get(0).innerHTML).to.equal(multilinesContent.replace(/\n/g, '<br>'))
})
})
})

describe('Table extension integrated in the editor', () => {
Expand Down
1 change: 1 addition & 0 deletions cypress/e2e/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('Workspace', function() {
checkContent()
})
})

})

const openSidebar = filename => {
Expand Down
33 changes: 33 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,35 @@ export default TableCell.extend({
},
}
},

addProseMirrorPlugins() {
return [
new Plugin({
props: {
// Special-treat empty lines in pasted content to prevent jumping out of cell
handlePaste: (view, event, slice) => {
luka-nextcloud marked this conversation as resolved.
Show resolved Hide resolved
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
luka-nextcloud marked this conversation as resolved.
Show resolved Hide resolved
}

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)
}
},
},
}),
]
},
})