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
fix(table): edit a colour column through the field the promoted one uses
Elian Doran928cd5cabd83
diff --git a/apps/client/src/stylesheets/table.css b/apps/client/src/stylesheets/table.cssindex dace356a9c..1f942d06b0 100644--- a/apps/client/src/stylesheets/table.css+++ b/apps/client/src/stylesheets/table.css@@ -120,7 +120,7 @@padding: var(--cell-vert-padding-size) var(--cell-horiz-padding-size);}-.tabulator-row .tabulator-cell input {+.tabulator-row .tabulator-cell input:not([type="color"]) {padding-inline-start: var(--cell-horiz-padding-size) !important;padding-inline-end: var(--cell-horiz-padding-size) !important;}diff --git a/apps/client/src/widgets/PromotedAttributes.css b/apps/client/src/widgets/PromotedAttributes.cssindex ef15905192..9ab82358e1 100644--- a/apps/client/src/widgets/PromotedAttributes.css+++ b/apps/client/src/widgets/PromotedAttributes.css@@ -58,44 +58,10 @@ body.experimental-feature-new-layout .promoted-attributes-container {appearance: auto;}-.promoted-attribute-cell input[type="color"] {- width: 24px;- height: 24px;- margin-top: 2px;- appearance: none;- padding: 0;- border: 0;- outline: none;- border-radius: 25% !important;-}--.promoted-attribute-cell input[type="color"]::-webkit-color-swatch-wrapper {- padding: 0;-}--.promoted-attribute-cell input[type="color"]::-webkit-color-swatch {- border: none;- border-radius: 25%;-}+/* The colour swatch, and how it shows an unset label, are dressed with the field itself — see+ label_value_input.css — so that a promoted colour and one edited anywhere else look alike. */.promoted-attribute-label-number input {text-align: right;width: 120px;}--.promoted-attribute-label-color input[type="hidden"][value=""] + input[type="color"] {- position: relative;- opacity: 0.5;-}--.promoted-attribute-label-color input[type="hidden"][value=""] + input[type="color"]:after {- content: "";- position: absolute;- top: 10px;- inset-inline-start: 0px;- inset-inline-end: 0;- height: 2px;- background: rgba(0, 0, 0, 0.5);- transform: rotate(45deg);- pointer-events: none;-}diff --git a/apps/client/src/widgets/attribute_widgets/label_value_input.css b/apps/client/src/widgets/attribute_widgets/label_value_input.cssindex 7a1f380229..f8597546f4 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_input.css+++ b/apps/client/src/widgets/attribute_widgets/label_value_input.css@@ -27,3 +27,75 @@ body[dir=rtl] .label-select-combobox.form-control:hover,body[dir=rtl] .label-select-combobox.form-control:focus {background-position: left calc(0.75rem - var(--input-group-padding-inline-end, 0px)) center;}++/*+ * A colour is picked from a swatch rather than typed into a box, so it is given a size of its own+ * instead of running the width a field would — the same size wherever a label is edited: the promoted+ * grid, the attribute editor, a table cell.+ *+ * Stated from `:root` so as to outweigh the group rule that would otherwise have it fill the row+ * (`.input-group > .form-control`, of the same weight) whichever way the stylesheets are ordered.+ */+:root .label-color-picker.form-control {+ flex: 0 0 auto;+ align-self: center;+ width: 24px;+ height: 24px;+ padding: 0;+ border: 0;+ outline: none;+ appearance: none;+ border-radius: 25%;+}++/* The swatch is the whole of the field: what the browser would inset it by is room the colour wants.+ Split by engine because a selector list is dropped whole where one of its parts is not understood. */+.label-color-picker::-webkit-color-swatch-wrapper {+ padding: 0;+}++.label-color-picker::-webkit-color-swatch {+ border: none;+ border-radius: 25%;+}++.label-color-picker::-moz-color-swatch {+ border: none;+ border-radius: 25%;+}++/*+ * A colour input has no empty value, so an unset label is said by the field rather than by the colour+ * it shows: the default it falls back to is dimmed and crossed through, so that it does not read as a+ * colour somebody chose.+ *+ * Told by a class rather than by reading the hidden field's `value` attribute, which is never there —+ * preact assigns a value as a property, so an attribute selector would match nothing.+ */+.label-color-picker-unset {+ /* Named once and used from each engine's own swatch rule below, those having to be stated apart. */+ --label-color-unset-cross: linear-gradient(+ to bottom right,+ transparent calc(50% - 1px),+ rgb(0 0 0 / 50%) calc(50% - 1px),+ rgb(0 0 0 / 50%) calc(50% + 1px),+ transparent calc(50% + 1px)+ );++ opacity: 0.5;+}++.label-color-picker-unset::-webkit-color-swatch {+ background-image: var(--label-color-unset-cross);+}++.label-color-picker-unset::-moz-color-swatch {+ background-image: var(--label-color-unset-cross);+}++/* The group holds the pair to its own width rather than the row's: a swatch and the button emptying it+ belong together, and stretched apart they would read as two controls that have nothing to do with+ each other. */+.input-group:has(> .label-color-picker) {+ width: fit-content;+}diff --git a/apps/client/src/widgets/attribute_widgets/label_value_input.spec.tsx b/apps/client/src/widgets/attribute_widgets/label_value_input.spec.tsxindex 013b12f8a4..14d3852909 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_input.spec.tsx+++ b/apps/client/src/widgets/attribute_widgets/label_value_input.spec.tsx@@ -66,6 +66,9 @@ describe("LabelValueInput", () => {const picker = container.querySelector<HTMLInputElement>("input[type=color]");expect(stored?.value).toBe("");expect(picker?.value).toBe("#ffffff");+ // The default it shows is not a colour anybody chose, and the field says so: the swatch is+ // marked by a class, the hidden field's value never reaching the DOM as an attribute to read.+ expect(picker?.className).toContain("label-color-picker-unset");await act(async () => {if (picker) {@@ -78,6 +81,13 @@ describe("LabelValueInput", () => {const clear = container.querySelector<HTMLElement>(".input-group-text");await act(async () => clear?.click());expect(onCommit).toHaveBeenLastCalledWith("");++ // A colour that was chosen wears the swatch's own class alone, the marking being for the+ // default a field falls to rather than for the field itself.+ await mount({ labelType: "color", value: "#ff0000", onCommit });+ const chosen = container.querySelector<HTMLInputElement>("input[type=color]");+ expect(chosen?.className).toContain("label-color-picker");+ expect(chosen?.className).not.toContain("label-color-picker-unset");});it("reports a flag as the words a label stores rather than as a checked box", async () => {diff --git a/apps/client/src/widgets/attribute_widgets/label_value_input.tsx b/apps/client/src/widgets/attribute_widgets/label_value_input.tsxindex 303f92dd50..512920cd55 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/label_value_input.tsx@@ -188,7 +188,10 @@ export default function LabelValueInput({<><inputref={colorInputRef}- className={clsx("form-control", inputProps?.className)}+ // The swatch dresses the same wherever a label is edited, and says for itself+ // when the label is unset — the one thing the value it shows cannot, a colour+ // input having no empty value to fall to.+ className={clsx("form-control label-color-picker", !value && "label-color-picker-unset", inputProps?.className)}type="color"disabled={inputProps?.disabled || inputProps?.readOnly}value={value || DEFAULT_COLOR}diff --git a/apps/client/src/widgets/collections/table/columns.css b/apps/client/src/widgets/collections/table/columns.cssindex 7c461b13c0..a5dcaeda00 100644--- a/apps/client/src/widgets/collections/table/columns.css+++ b/apps/client/src/widgets/collections/table/columns.css@@ -71,3 +71,37 @@inset-block-start: auto;inset-block-end: 0;}++/*+ * The colour picker and the button emptying it, framed as the one group they are — the same group,+ * dressed the same way, that the promoted field and the attribute editor show. It sits within the cell+ * rather than standing over it, as the select editor does: a swatch and a button ask for no more room+ * than a cell has.+ *+ * All this adds is where it sits: an editing cell gives up its padding and is a line tall, so the pair+ * is centred in what is left rather than hanging from the top of it.+ */+.table-color-editor.input-group {+ height: 100%;+ align-items: center;+ padding-inline-start: 4px;++ /* A row is only as tall as the text in it, which a column of colours need not be taller than: the+ swatch gives up its squareness rather than being cut off by the cell. */+ > .label-color-picker {+ max-height: 100%;+ }+}++/*+ * A colour cell is read as the colour filling it, which it gives up while it is being edited to look+ * like any other cell in hand: the swatch is what says the colour then, and a cell still flooded with+ * it would leave the pair standing on the very value they are there to change — the field lost in it,+ * and a colour picked showing the old one all around.+ *+ * Insisted upon because the fill is written onto the cell itself by the formatter, which the rule+ * dressing an editing cell is otherwise powerless against.+ */+.tabulator-cell.tabulator-editing:has(> .table-color-editor) {+ background-color: var(--cell-editing-background-color) !important;+}diff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.tsindex 79be8ac7e9..505534e74e 100644--- a/apps/client/src/widgets/collections/table/columns.spec.ts+++ b/apps/client/src/widgets/collections/table/columns.spec.ts@@ -1,3 +1,4 @@+import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";import { buildColumnDefinitions, formatLabelDate, restoreExistingData, type SelectEditorParams } from "./columns";import type { CellComponent, ColumnDefinition } from "tabulator-tables";@@ -155,6 +156,59 @@ describe("buildColumnDefinitions — typed columns", () => {});});+describe("buildColumnDefinitions — colour columns", () => {+ /** Opens a colour cell's editor, as Tabulator does, and hands back what it built. */+ async function editColorCell(value: string) {+ const [ column ] = buildColumnDefinitions({+ info: [ { name: "tint", type: "color" } ],+ movableRows: false,+ existingColumnData: undefined,+ rowNumberHint: 1+ }).filter((candidate) => candidate.field === "labels.tint");++ const editor = column?.editor;+ if (typeof editor !== "function") throw new Error("expected an editor of its own");++ const success = vi.fn();+ let element: HTMLElement | false = false;+ await act(async () => {+ element = editor({ getValue: () => value } as CellComponent, () => {}, success, () => {}, {});+ });+ if (!element) throw new Error("expected the editor to build a field");+ return { element: element as HTMLElement, success };+ }++ it("reports the picked colour once the pick is settled, not through the drag", async () => {+ const { element, success } = await editColorCell("#ff0000");+ const picker = element.querySelector<HTMLInputElement>("input[type=color]");+ if (!picker) throw new Error("expected a colour picker");++ picker.value = "#00ff00";+ // Reporting ends the edit, so a colour reported as the picker is dragged through would tear+ // the editor down — and with it the open picker — at the first step.+ picker.dispatchEvent(new Event("input", { bubbles: true }));+ expect(success).not.toHaveBeenCalled();++ picker.dispatchEvent(new Event("change", { bubbles: true }));+ expect(success).toHaveBeenCalledWith("#00ff00");+ });++ it("clears a colour back to the unset value a bare picker cannot express", async () => {+ const { element, success } = await editColorCell("#ff0000");++ element.querySelector<HTMLElement>(".input-group-text")?.click();+ expect(success).toHaveBeenCalledWith("");+ });++ it("leaves a cell alone until a colour is picked, an opened picker being no decision", async () => {+ // A colour input has no empty value, so it shows black over an unset cell; committing that on+ // the way out would fill in a colour nobody chose.+ const { element, success } = await editColorCell("");+ expect(element.querySelector<HTMLInputElement>("input[type=hidden]")?.value).toBe("");+ expect(success).not.toHaveBeenCalled();+ });+});+describe("buildColumnDefinitions — select columns", () => {/** Resolves the params a column hands its editor, which Tabulator asks for as a function. */function editorParamsOf(column: ColumnDefinition | undefined) {diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex aeada477aa..00624b589b 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -91,13 +91,8 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {editor: "input"},color: {- editor: "input",- formatter: "color",- editorParams: {- elementAttributes: {- type: "color"- }- }+ editor: wrapEditor(ColorEditor),+ formatter: "color"},relation: {editor: wrapEditor(RelationEditor),@@ -445,6 +440,45 @@ export type SelectEditorParams = {onCreateOption?: (option: string) => void | Promise<void>;};+/**+ * A colour cell is edited through the very field the promoted grid and the attribute editor offer: a+ * picker, and beside it the button that empties it. A bare colour input cannot be emptied — it has no+ * such value — so a cell opened by accident used to fall to black, with no way back to unset.+ *+ * The colour is taken from the picker's own `change` rather than through the field's commit, which a+ * colour input fires at every step of a drag through it: reporting is Tabulator's "the edit is done",+ * so the first step would tear the editor out from under the open picker. `change` comes once, when+ * the pick is settled, which is also when a cell is finished with.+ *+ * The picker is reached through the container because {@link LabelValueInput} hands no reference to+ * what it builds, as {@link SelectEditor} reaches its box for the same reason.+ */+function ColorEditor({ cell, success }: EditorOpts) {+ const containerRef = useRef<HTMLDivElement>(null);++ useEffect(() => {+ const picker = containerRef.current?.querySelector<HTMLInputElement>("input[type=color]");+ if (!picker) return;++ picker.focus();+ const onPicked = () => success(picker.value);+ picker.addEventListener("change", onPicked);+ return () => picker.removeEventListener("change", onPicked);+ }, [ success ]);++ return (+ <div ref={containerRef} className="input-group table-color-editor">+ <LabelValueInput+ labelType="color"+ value={cell.getValue() ?? ""}+ // Nothing is what the clear button hands back, and only it — a picker always names a+ // colour. There being nothing to follow a clear with, it finishes the edit itself.+ onCommit={(value) => !value && success("")}+ />+ </div>+ );+}+function RelationEditor({ cell, success }: EditorOpts) {const inputRef = useRef<HTMLInputElement>(null);useEffect(() => inputRef.current?.focus());