diff --git a/src/nodes/Table/TableCell.ts b/src/nodes/Table/TableCell.ts index 66e9f605881..61057a33678 100644 --- a/src/nodes/Table/TableCell.ts +++ b/src/nodes/Table/TableCell.ts @@ -7,7 +7,7 @@ import type { Node as PMNode, Schema } from '@tiptap/pm/model' import { showWarning } from '@nextcloud/dialogs' import { t } from '@nextcloud/l10n' -import { mergeAttributes } from '@tiptap/core' +import { getChangedRanges, mergeAttributes } from '@tiptap/core' import { TableCell } from '@tiptap/extension-table' import { Fragment, Slice } from '@tiptap/pm/model' import { Plugin, PluginKey } from '@tiptap/pm/state' @@ -66,19 +66,26 @@ export default TableCell.extend({ return true } + // Only changed ranges can contain a new nested table + const { doc } = transaction let hasNestedTable = false - transaction.doc.descendants((node, pos) => { - if (node.type.name.startsWith('table')) { - const $pos = transaction.doc.resolve(pos) - for (let depth = $pos.depth; depth >= 0; depth--) { - const ancestor = $pos.node(depth) - if (ancestor.type.name === 'tableCell') { - // eslint-disable-next-line no-console - console.warn('Detected nested table, filtering out transaction') - hasNestedTable = true + getChangedRanges(transaction).forEach(({ newRange }) => { + doc.nodesBetween(newRange.from, newRange.to, (node, pos) => { + if (hasNestedTable || node.isTextblock) { + return false + } + if (node.type.name === 'table') { + const $pos = doc.resolve(pos) + for (let depth = $pos.depth; depth >= 0; depth--) { + if ($pos.node(depth).type.name === 'tableCell') { + // eslint-disable-next-line no-console + console.warn('Detected nested table, filtering out transaction') + hasNestedTable = true + return false + } } } - } + }) }) return !hasNestedTable diff --git a/src/tests/nodes/Table.spec.js b/src/tests/nodes/Table.spec.js index c076dfe091f..ac1ab70ae38 100644 --- a/src/tests/nodes/Table.spec.js +++ b/src/tests/nodes/Table.spec.js @@ -417,6 +417,38 @@ describe('Table extension', () => { expect(serializeMarkdown()).toBe('| H1 | H2 |\n|--------|------|\n| a1 | a2 |\n| b1 | H1b2 | \\\n| | | \\\n| - item | |\n') }) }) + + describe('nested tables', () => { + const html = '
H1

a1

  • item

after

' + + test('transaction inserting a table into a cell is filtered out', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(findText(editor, 'a1') + 3, doc.firstChild)) + expect(editor.state.doc.eq(doc)).toBe(true) + }) + + test('transaction inserting a table into a list in a cell is filtered out', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(findText(editor, 'item') + 5, doc.firstChild)) + expect(editor.state.doc.eq(doc)).toBe(true) + }) + + test('transaction inserting a table outside of tables is applied', ({ editor }) => { + editor.commands.setContent(html) + const doc = editor.state.doc + editor.view.dispatch(editor.state.tr.insert(doc.content.size, doc.firstChild)) + const tables = editor.state.doc.children.filter((node) => node.type.name === 'table') + expect(tables).toHaveLength(2) + }) + + test('typing in a cell is applied', ({ editor, serializeMarkdown }) => { + editor.commands.setContent(html) + editor.view.dispatch(editor.state.tr.insertText('x', findText(editor, 'a1'))) + expect(serializeMarkdown()).toContain('xa1') + }) + }) }) function getHeaderCell(editor, targetIndex = 0) {