From 55020eab5b185875502262a79d3cd7b13ac67296 Mon Sep 17 00:00:00 2001 From: Luka Trovic Date: Mon, 6 Mar 2023 21:18:47 +0100 Subject: [PATCH] fix: paste multiple line to table issue Signed-off-by: Luka Trovic --- cypress/e2e/nodes/Table.spec.js | 18 ++++++++++++++++++ cypress/e2e/workspace.spec.js | 1 + src/nodes/Table/TableCell.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/cypress/e2e/nodes/Table.spec.js b/cypress/e2e/nodes/Table.spec.js index 2af6a57071b..620eb8541d7 100644 --- a/cypress/e2e/nodes/Table.spec.js +++ b/cypress/e2e/nodes/Table.spec.js @@ -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, '
')) + }) + }) }) describe('Table extension integrated in the editor', () => { diff --git a/cypress/e2e/workspace.spec.js b/cypress/e2e/workspace.spec.js index d160a0c0c50..6c7ae668ca7 100644 --- a/cypress/e2e/workspace.spec.js +++ b/cypress/e2e/workspace.spec.js @@ -298,6 +298,7 @@ describe('Workspace', function() { checkContent() }) }) + }) const openSidebar = filename => { diff --git a/src/nodes/Table/TableCell.js b/src/nodes/Table/TableCell.js index 14ef389b349..0891633b0ad 100644 --- a/src/nodes/Table/TableCell.js +++ b/src/nodes/Table/TableCell.js @@ -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*', @@ -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) + } + }, + }, + }), + ] + }, })