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
This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-26 09:17:32 -05:00
1 parent e5cdb22cc9
commit c7cf99aabc
5 files changed
+69

No files matched your search

+14
View File
@@ -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);
+24
View File
@@ -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([
+8
View File
@@ -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 {
+20
View File
@@ -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: {
@@ -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";