Skip to content

Commit

Permalink
fix: paste multiple line to table issue
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 Apr 18, 2023
1 parent b40fc52 commit 4621b08
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cypress/e2e/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ describe('Workspace', function() {
})
})

it('creates table and add multilines', function() {
const tag = 'table'
const multilinesContent = 'Line 1\nLine 2\nLine 3'
cy.visit(`apps/files?dir=/${encodeURIComponent(currentFolder)}`)
cy.openWorkspace()
.type('Hello !')

cy.getMenuEntry(tag)
.click()
.should('have.class', 'is-active')

cy.getContent()
.find(`${tag}`).should('be.visible')

cy.get('#rich-workspace .ProseMirror')
.get('table:nth-of-type(1) tr:nth-child(2) td:nth-child(1)')
.click()
.type(multilinesContent)

cy.getEditor()
.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>'))
})
})

it('takes README.md into account', function() {
cy.uploadFile('test.md', 'text/markdown', `${Cypress.currentTest.title}/README.md`)
cy.visit(`apps/files?dir=/${encodeURIComponent(currentFolder)}`)
Expand Down Expand Up @@ -332,6 +358,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) => {
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
}

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 4621b08

Please sign in to comment.