From c7cf99aabc4a6fb196791018d82301ca8ccd439c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Nov 2026 09:17:32 -0500 Subject: [PATCH] feat: Clamp narrow table column cells to 3 lines (#14724) * feat: Clamp narrow table column cells to 3 lines * fix: Skip clamp in print and static HTML, sum merged column widths --- shared/editor/components/Styles.ts | 14 +++++++++++++ shared/editor/lib/table.test.ts | 24 +++++++++++++++++++++++ shared/editor/lib/table.ts | 8 ++++++++ shared/editor/nodes/TableCell.ts | 20 +++++++++++++++++++ shared/editor/styles/EditorStyleHelper.ts | 3 +++ 5 files changed, 69 insertions(+) diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index bb8f03588f..dc666aeba3 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -2385,6 +2385,20 @@ table { padding: 4px 0; } + ${ + !props.staticHTML && + css` + @media screen { + [data-narrow]:not(.${EditorStyleHelper.tableCellActive}) > p { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 3; + overflow: hidden; + } + } + ` + } + td[data-bgcolor], th[data-bgcolor] { color: var(--cell-text-color); diff --git a/shared/editor/lib/table.test.ts b/shared/editor/lib/table.test.ts index 18785a8266..b151d7f36e 100644 --- a/shared/editor/lib/table.test.ts +++ b/shared/editor/lib/table.test.ts @@ -9,6 +9,7 @@ import { getCellsStartingInRow, isValidCellAlignment, isValidCellMarks, + setCellAttrs, } from "./table"; const schema = new Schema({ @@ -226,6 +227,29 @@ describe.runIf(typeof document !== "undefined")("getCellAttrs", () => { }); }); +describe("setCellAttrs", () => { + const cellWithWidth = (width: number) => + doc([table([tr([td("A", { colwidth: [width] })])])]) + .child(0) + .child(0) + .child(0); + + it("marks cells in narrow columns", () => { + expect(setCellAttrs(cellWithWidth(60))["data-narrow"]).toBe("true"); + expect(setCellAttrs(cellWithWidth(200))["data-narrow"]).toBeUndefined(); + }); + + it("sums the widths of merged columns", () => { + const cell = doc([ + table([tr([td("A", { colspan: 2, colwidth: [60, 200] })])]), + ]) + .child(0) + .child(0) + .child(0); + expect(setCellAttrs(cell)["data-narrow"]).toBeUndefined(); + }); +}); + describe("getCellSpan", () => { const map = TableMap.get( doc([ diff --git a/shared/editor/lib/table.ts b/shared/editor/lib/table.ts index 690775bab8..fce21c1e1d 100644 --- a/shared/editor/lib/table.ts +++ b/shared/editor/lib/table.ts @@ -21,6 +21,9 @@ export interface CellAttrs { const ALLOWED_ALIGNMENTS = new Set(["left", "center", "right"]); +/** Columns narrower than this, in pixels, clamp their cell content. */ +const narrowColumnWidth = 100; + /** * Validates an alignment attribute value. * @@ -167,6 +170,11 @@ export function setCellAttrs(node: Node): Attrs { attrs.style = `text-align: ${node.attrs.alignment};`; } if (node.attrs.colwidth) { + const widths: number[] = node.attrs.colwidth.map(Number); + const width = widths.reduce((sum, w) => sum + w, 0); + if (widths.every((w) => w > 0) && width < narrowColumnWidth) { + attrs["data-narrow"] = "true"; + } if (isBrowser) { attrs["data-colwidth"] = node.attrs.colwidth.map(Number).join(","); } else { diff --git a/shared/editor/nodes/TableCell.ts b/shared/editor/nodes/TableCell.ts index 094fd4c4a2..c7de3e0b70 100644 --- a/shared/editor/nodes/TableCell.ts +++ b/shared/editor/nodes/TableCell.ts @@ -17,6 +17,7 @@ import { setCellAttrs, } from "../lib/table"; import { hasTableRole } from "../queries/table"; +import { EditorStyleHelper } from "../styles/EditorStyleHelper"; import Node from "./Node"; import { presetColors, rgbaToHex } from "@shared/utils/color"; import { parseToRgb, transparentize } from "polished"; @@ -172,6 +173,25 @@ export default class TableCell extends Node { }, }, }), + new Plugin({ + key: new PluginKey("table-cell-active"), + props: { + decorations: (state) => { + const { $from } = state.selection; + for (let depth = $from.depth; depth > 0; depth--) { + const role = $from.node(depth).type.spec.tableRole; + if (role === "cell" || role === "header_cell") { + return DecorationSet.create(state.doc, [ + Decoration.node($from.before(depth), $from.after(depth), { + class: EditorStyleHelper.tableCellActive, + }), + ]); + } + } + return null; + }, + }, + }), new Plugin({ key: new PluginKey("table-cell-copy-transform"), props: { diff --git a/shared/editor/styles/EditorStyleHelper.ts b/shared/editor/styles/EditorStyleHelper.ts index fe182d3731..1be9dbd3df 100644 --- a/shared/editor/styles/EditorStyleHelper.ts +++ b/shared/editor/styles/EditorStyleHelper.ts @@ -205,6 +205,9 @@ export class EditorStyleHelper { /** Table row hidden by a filter */ static readonly tableRowFiltered = "table-row-filtered"; + /** Table cell that contains the selection */ + static readonly tableCellActive = "table-cell-active"; + /** Button in a header cell that opens the column filter */ static readonly tableFilterButton = "table-filter-button";