From 4e6572d6861aeb33df0961e52491e28c4a19685b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 18 Oct 2024 08:35:21 -0400 Subject: [PATCH] fix: Mutate clipboard content when copying from a single table cell. (#7798) * fix: Mutate clipboard content when copying from a single table cell. closes #7794 * refactor --- shared/editor/nodes/TableCell.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/shared/editor/nodes/TableCell.ts b/shared/editor/nodes/TableCell.ts index 393b0dedd7..8deeb2aad5 100644 --- a/shared/editor/nodes/TableCell.ts +++ b/shared/editor/nodes/TableCell.ts @@ -1,5 +1,5 @@ import Token from "markdown-it/lib/token"; -import { NodeSpec } from "prosemirror-model"; +import { NodeSpec, Slice } from "prosemirror-model"; import { Plugin } from "prosemirror-state"; import { DecorationSet, Decoration } from "prosemirror-view"; import { addRowBefore, selectRow, selectTable } from "../commands/table"; @@ -71,6 +71,35 @@ export default class TableCell extends Node { return [ new Plugin({ props: { + transformCopied: (slice) => { + // check if the copied selection is a single table, with a single row, with a single cell. If so, + // copy the cell content only – not a table with a single cell. This leads to more predictable pasting + // behavior, both in and outside the app. + if (slice.content.childCount === 1) { + const table = slice.content.firstChild; + if ( + table?.type.spec.tableRole === "table" && + table.childCount === 1 + ) { + const row = table.firstChild; + if ( + row?.type.spec.tableRole === "row" && + row.childCount === 1 + ) { + const cell = row.firstChild; + if (cell?.type.spec.tableRole === "cell") { + return new Slice( + cell.content, + slice.openStart, + slice.openEnd + ); + } + } + } + } + + return slice; + }, handleDOMEvents: { mousedown: (view, event) => { if (!(event.target instanceof HTMLElement)) {