Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/table insert popover: tweak UI
Adorian Doranabf16f2dd929
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2ab5954e6f..039c3efdd1 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4629,6 +4629,8 @@"move-column-left": "Move column left","move-column-right": "Move column right","insert-table": "Insert table…",+ "table-size": "Table size",+ "table-headers": "Table headers","rows": "Rows","columns": "Columns","use-headers": "Use headers",diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.tsindex 3ce34cd28d..20a616b432 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.spec.ts@@ -1,8 +1,10 @@-import { Locale } from "ckeditor5";+import { IconTableColumn, IconTableRow, Locale } from "ckeditor5";import editorStylesheetUrl from "ckeditor5/ckeditor5.css?url";import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";import { userEvent } from "vitest/browser";+import "../../theme/table_insert.css";+import TableInsertFormView, {DEFAULT_HEADING_COLUMNS,DEFAULT_HEADING_ROWS,@@ -85,6 +87,62 @@ describe("TableInsertFormView", () => {expect(element.lastElementChild).toBe(button);});+ it("lays the fields out as segments of a Table size and a Table headers card", () => {+ const cards = Array.from(getElement(form)+ .querySelectorAll<HTMLElement>(".ck-table-insert-form__card"));+ expect(cards).toHaveLength(2);+ const [sizeCard, headersCard] = cards;++ const headings = cards.map((card) => {+ const heading = card.querySelector(".ck-table-insert-form__card-heading");+ expect(card.getAttribute("role")).toBe("group");+ expect(heading?.id).toBeTruthy();+ expect(card.getAttribute("aria-labelledby")).toBe(heading?.id);+ return heading?.textContent;+ });+ expect(headings).toEqual(["Table size", "Table headers"]);+ expect(sizeCard.getAttribute("aria-labelledby"))+ .not.toBe(headersCard.getAttribute("aria-labelledby"));++ const sizeSegments = getSegments(sizeCard);+ expect(sizeSegments).toHaveLength(2);+ expect(sizeSegments[0]).toBe(getElement(form.columnsField));+ expect(sizeSegments[1]).toBe(getElement(form.rowsField));++ const headerSegments = getSegments(headersCard);+ expect(headerSegments).toHaveLength(3);+ expect(headerSegments[0]).toBe(getSwitchSegment(form));+ expect(headerSegments[1]).toBe(getElement(form.headingColumnsField));+ expect(headerSegments[2]).toBe(getElement(form.headingRowsField));++ const icons = new Map([+ [form.columnsField, IconTableColumn],+ [form.rowsField, IconTableRow],+ [form.headingColumnsField, IconTableColumn],+ [form.headingRowsField, IconTableRow]+ ]);+ for (const [field, icon] of icons) {+ const iconElement = getElement(field.iconView);+ expect(field.iconView.content).toBe(icon);+ expect(iconElement.childElementCount).toBeGreaterThan(0);+ expect(getElement(field).firstElementChild).toBe(iconElement);+ }+ });++ it("rounds the bottom of the switch segment while it is the last one shown", () => {+ const switchStyle = getComputedStyle(getSwitchSegment(form));+ expect(switchStyle.borderStartStartRadius).toBe("8px");+ expect(switchStyle.borderEndEndRadius).toBe("8px");++ form.useHeaders = true;+ expect(switchStyle.borderStartStartRadius).toBe("8px");+ expect(switchStyle.borderEndEndRadius).toBe("0px");+ expect(getComputedStyle(getElement(form.headingColumnsField)).borderEndEndRadius)+ .toBe("0px");+ expect(getComputedStyle(getElement(form.headingRowsField)).borderEndEndRadius)+ .toBe("8px");+ });+it("returns the entered size", () => {getInput(form.rowsField).value = "12";getInput(form.columnsField).value = "3";@@ -387,6 +445,18 @@ function setHeadings(form: TableInsertFormView, rows: number, columns: number) {getInput(form.headingColumnsField).value = String(columns);}+function getSegments(card: HTMLElement): HTMLElement[] {+ return Array.from(card.querySelectorAll<HTMLElement>(".ck-table-insert-form__segment"));+}++function getSwitchSegment(form: TableInsertFormView): HTMLElement {+ const segment = getElement(form.headersSwitchView).parentElement;+ if (!segment?.classList.contains("ck-table-insert-form__segment")) {+ throw new Error("The switch is not in a segment.");+ }+ return segment;+}+function getSizeError(form: TableInsertFormView): HTMLElement {const error = getElement(form).querySelector<HTMLElement>(".ck-table-insert-form__size-error");if (!error) {diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.tsindex 60b5d3092f..a70e4d66e7 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_form.ts@@ -2,6 +2,9 @@ import {ButtonView,FocusCycler,FocusTracker,+ IconTableColumn,+ IconTableRow,+ IconView,InputNumberView,KeystrokeHandler,LabelView,@@ -11,7 +14,7 @@ import {View,ViewCollection} from "ckeditor5";-import type { FocusableView, Locale } from "ckeditor5";+import type { FocusableView, Locale, TemplateDefinition } from "ckeditor5";/** The largest number of rows the form accepts. */export const MAX_TABLE_ROWS = 1000;@@ -40,10 +43,11 @@ export interface TableInsertOptions {}/**- * The balloon form behind "Insert table…": a "Columns" field, a "Rows" field, a "Use headers"- * switch that shows the "Header columns" and "Header rows" fields, and an Insert button. Invalid- * values are reported under their field when the form is submitted, and a table with more than- * {@link MAX_TABLE_CELLS} cells is reported above the button.+ * The balloon form behind "Insert table…": a "Table size" card with the "Columns" and "Rows"+ * fields, a "Table headers" card whose "Use headers" switch shows the "Header columns" and+ * "Header rows" fields, and an Insert button. Invalid values are reported under their field when+ * the form is submitted, and a table with more than {@link MAX_TABLE_CELLS} cells is reported+ * above the button.*/export default class TableInsertFormView extends View {@@ -80,11 +84,13 @@ export default class TableInsertFormView extends View {const getHeadingError = (max: number) => t("Enter a whole number between 0 and %0.", max);this.rowsField = new TableSizeFieldView(locale, t("Rows"), {+ icon: IconTableRow,min: 1,max: MAX_TABLE_ROWS,getErrorText: getSizeError});this.columnsField = new TableSizeFieldView(locale, t("Columns"), {+ icon: IconTableColumn,min: 1,max: MAX_TABLE_COLUMNS,getErrorText: getSizeError@@ -98,11 +104,13 @@ export default class TableInsertFormView extends View {});this.headingRowsField = new TableSizeFieldView(locale, t("Header rows"), {+ icon: IconTableRow,min: 0,max: MAX_TABLE_ROWS,getErrorText: getHeadingError});this.headingColumnsField = new TableSizeFieldView(locale, t("Header columns"), {+ icon: IconTableColumn,min: 0,max: MAX_TABLE_COLUMNS,getErrorText: getHeadingError@@ -129,11 +137,23 @@ export default class TableInsertFormView extends View {novalidate: true},children: [- this.columnsField,- this.rowsField,- this.headersSwitchView,- this.headingColumnsField,- this.headingRowsField,+ createCard(t("Table size"), [this.columnsField, this.rowsField]),+ createCard(t("Table headers"), [+ {+ tag: "div",+ attributes: {+ class: [+ "ck",+ "ck-table-insert-form__segment",+ "ck-table-insert-form__switch",+ bind.if("useHeaders", "ck-on")+ ]+ },+ children: [this.headersSwitchView]+ },+ this.headingColumnsField,+ this.headingRowsField+ ]),{tag: "div",attributes: {@@ -259,7 +279,43 @@ export default class TableInsertFormView extends View {}}+/** A card of the form: a heading over its segments, which the heading names as a group. */+function createCard(+ heading: string,+ segments: Array<View | TemplateDefinition>+): TemplateDefinition {+ const headingId = `ck-table-insert-form-heading-${uid()}`;++ return {+ tag: "div",+ attributes: {+ class: ["ck", "ck-table-insert-form__card"],+ role: "group",+ "aria-labelledby": headingId+ },+ children: [+ {+ tag: "div",+ attributes: {+ id: headingId,+ class: ["ck", "ck-table-insert-form__card-heading"]+ },+ children: [{ text: heading }]+ },+ {+ tag: "div",+ attributes: {+ class: ["ck", "ck-table-insert-form__card-body"]+ },+ children: segments+ }+ ]+ };+}+interface TableSizeFieldOptions {+ /** The SVG shown before the label. */+ icon: string;min: number;max: number;/** Returns the error shown for a value that is not a whole number from `min` to `max`. */@@ -267,8 +323,8 @@ interface TableSizeFieldOptions {}/**- * A number field of {@link TableInsertFormView}: a label, the input and the error under it.- * The element has `display: contents`, so the three parts are laid out by the form's grid.+ * A number field of {@link TableInsertFormView}, drawn as a card segment: an icon, a label, the+ * input and the error under them.*/export class TableSizeFieldView extends View {@@ -280,15 +336,18 @@ export class TableSizeFieldView extends View {declare public isVisible: boolean;+ public readonly iconView: IconView;public readonly labelView: LabelView;public readonly inputView: InputNumberView;private readonly min: number;private readonly getErrorText: (max: number) => string;- constructor(locale: Locale, label: string, { min, max, getErrorText }: TableSizeFieldOptions) {+ constructor(locale: Locale, label: string, options: TableSizeFieldOptions) {super(locale);+ const { icon, min, max, getErrorText } = options;+const inputId = `ck-table-insert-form-input-${uid()}`;const errorId = `ck-table-insert-form-error-${uid()}`;const bind = this.bindTemplate;@@ -299,6 +358,9 @@ export class TableSizeFieldView extends View {this.set("max", max);this.set("isVisible", true);+ this.iconView = new IconView();+ this.iconView.content = icon;+this.labelView = new LabelView(locale);this.labelView.set({ text: label, for: inputId });@@ -319,11 +381,13 @@ export class TableSizeFieldView extends View {attributes: {class: ["ck",+ "ck-table-insert-form__segment","ck-table-insert-form__field",bind.if("isVisible", "ck-hidden", (isVisible) => !isVisible)]},children: [+ this.iconView,this.labelView,this.inputView,{diff --git a/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts b/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.tsindex 010a5f02b9..a7f80acf6b 100644--- a/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/table_insert/table_insert_ui.spec.ts@@ -167,32 +167,50 @@ describe("TableInsertUI", () => {expect(gap).toBeLessThan(30);});- it("is at least 250px wide, with the inputs and the button at the right edge", async () => {+ it("is at least 350px wide, with segments spanning it", async () => {const form = await openForm(editor);form.useHeaders = true;- expect(getElement(form).getBoundingClientRect().width).toBeGreaterThanOrEqual(250);-- const buttonRect = getElement(form.insertButtonView).getBoundingClientRect();- const labelRect = getElement(form.columnsField.labelView).getBoundingClientRect();- const contentRight = getElement(form).getBoundingClientRect().right- - parseFloat(getComputedStyle(getElement(form)).paddingRight);- expect(buttonRect.right).toBeCloseTo(contentRight, 0);+ const formElement = getElement(form);+ const formRect = formElement.getBoundingClientRect();+ const formStyle = getComputedStyle(formElement);+ const contentLeft = formRect.left + parseFloat(formStyle.paddingLeft);+ const contentRight = formRect.right - parseFloat(formStyle.paddingRight);+ expect(formRect.width).toBeGreaterThanOrEqual(350);+ expect(getElement(form.insertButtonView).getBoundingClientRect().right)+ .toBeCloseTo(contentRight, 0);++ const segments = Array.from(formElement+ .querySelectorAll<HTMLElement>(".ck-table-insert-form__segment"));+ expect(segments).toHaveLength(5);+ for (const segment of segments) {+ const rect = segment.getBoundingClientRect();+ expect(rect.left).toBeCloseTo(contentLeft, 0);+ expect(rect.right).toBeCloseTo(contentRight, 0);+ }+ const segmentStyle = getComputedStyle(segments[0]);+ const innerLeft = contentLeft + parseFloat(segmentStyle.borderLeftWidth)+ + parseFloat(segmentStyle.paddingLeft);+ const innerRight = contentRight - parseFloat(segmentStyle.borderRightWidth)+ - parseFloat(segmentStyle.paddingRight);for (const field of ["columns", "rows", "headingColumns", "headingRows"] as const) {const input = getInput(form, field);const rect = input.getBoundingClientRect();const fontSize = parseFloat(getComputedStyle(input).fontSize);+ const iconRect = getElement(form[`${field}Field`].iconView).getBoundingClientRect();expect(rect.width, field).toBeCloseTo(5 * fontSize, 0);- expect(rect.right, field).toBeCloseTo(contentRight, 0);+ expect(rect.right, field).toBeCloseTo(innerRight, 0);+ expect(iconRect.left, field).toBeCloseTo(innerLeft, 0);}- // The switch row lines up with the fields.+ // The switch fills its segment: the label starts at the icons, the toggle ends at+ // the inputs.const toggleElement = getElement(form.headersSwitchView);const switchLabel = toggleElement.querySelector(".ck-button__label");const toggle = toggleElement.querySelector(".ck-button__toggle");- expect(switchLabel?.getBoundingClientRect().left).toBeCloseTo(labelRect.left, 0);- expect(toggle?.getBoundingClientRect().right).toBeCloseTo(contentRight, 0);+ expect(switchLabel?.getBoundingClientRect().left).toBeCloseTo(innerLeft, 0);+ expect(toggle?.getBoundingClientRect().right).toBeCloseTo(innerRight, 0);});it("inserts a table of the typed size and closes", async () => {@@ -383,6 +401,8 @@ describe("TableInsertUI", () => {en: {dictionary: {"Insert table…": "Tabel personalizat…",+ "Table size": "Dimensiune",+ "Table headers": "Antet","Rows": "Rânduri","Columns": "Coloane","Use headers": "Cu antet",@@ -400,6 +420,10 @@ describe("TableInsertUI", () => {getFormButton(dropdown).click();const form = getForm(translated.plugins.get(ContextualBalloon));+ const headings = Array.from(getElement(form)+ .querySelectorAll(".ck-table-insert-form__card-heading"))+ .map((heading) => heading.textContent);+ expect(headings).toEqual(["Dimensiune", "Antet"]);const labels = Array.from(getElement(form).querySelectorAll("label")).map((label) => label.textContent);expect(labels).toEqual(["Coloane", "Rânduri", "Coloane antet", "Rânduri antet"]);diff --git a/packages/ckeditor5/src/theme/table_insert.css b/packages/ckeditor5/src/theme/table_insert.cssindex 5203290fa4..67f171caba 100644--- a/packages/ckeditor5/src/theme/table_insert.css+++ b/packages/ckeditor5/src/theme/table_insert.css@@ -1,22 +1,76 @@:where(:root) {--ck-table-insert-form-error-color: var(--ck-color-base-error);+ --ck-table-insert-form-segment-background: var(--card-background-color,+ var(--ck-color-base-foreground));+ --ck-table-insert-form-segment-border-color: var(--card-border-color,+ var(--main-border-color, var(--ck-color-base-border)));}.ck.ck-table-insert-form {- display: grid;- grid-template-columns: 1fr auto;- align-items: center;- row-gap: var(--ck-spacing-standard, var(--ck-spacing-base));- column-gap: var(--ck-spacing-large, var(--ck-spacing-lg));+ display: flex;+ flex-direction: column;+ gap: var(--ck-spacing-large, var(--ck-spacing-lg));padding: var(--ck-spacing-large, var(--ck-spacing-lg));- min-width: 250px;+ min-width: 350px;&:focus {outline: none;}+ .ck-table-insert-form__card-heading {+ margin-bottom: 10px;+ font-size: .75rem;+ font-weight: 600;+ letter-spacing: .4pt;+ text-transform: uppercase;+ }++ .ck-table-insert-form__card-body {+ display: flex;+ flex-direction: column;+ gap: 3px;+ }++ .ck-table-insert-form__segment {+ border: 1px solid var(--ck-table-insert-form-segment-border-color);+ padding: 8px 12px;+ background: var(--ck-table-insert-form-segment-background);++ &:first-child {+ border-start-start-radius: 8px;+ border-start-end-radius: 8px;+ }++ /* The switch is the last segment shown while the header fields are hidden. */+ &:last-child,+ &.ck-table-insert-form__switch:not(.ck-on) {+ border-end-start-radius: 8px;+ border-end-end-radius: 8px;+ }++ &.ck-table-insert-form__switch {+ padding: 0;+ }+ }++ .ck-table-insert-form__switch .ck.ck-switchbutton {+ display: flex;+ border: 0;+ border-radius: inherit;+ padding: 8px 12px;+ width: 100%;+ }+.ck-table-insert-form__field {- display: contents;+ display: grid;+ grid-template-columns: auto 1fr auto;+ align-items: center;+ row-gap: var(--ck-spacing-small, var(--ck-spacing-sm));+ column-gap: var(--ck-spacing-standard, var(--ck-spacing-base));++ .ck-table-insert-form__error {+ grid-column: 2 / -1;+ }}.ck.ck-label {@@ -28,18 +82,8 @@width: 5em;}- /* Aligns the label and the toggle with the fields. */- .ck.ck-switchbutton {- grid-column: 1 / -1;- margin-inline: calc(-1 * var(--ck-spacing-small, var(--ck-spacing-sm)));- padding-inline: calc(- var(--ck-spacing-small, var(--ck-spacing-sm)) - var(--ck-border-width-control)- );- }-/* `width: 0` keeps the message from widening the form; it wraps instead. */.ck-table-insert-form__error {- grid-column: 1 / -1;min-width: 100%;width: 0;color: var(--ck-table-insert-form-error-color);@@ -48,7 +92,6 @@}.ck.ck-button-action {- grid-column: 1 / -1;- justify-self: end;+ align-self: flex-end;}}