Skip to content

Build your personal knowledge base with Trilium Notes

feat(table): show and edit a column of flags as the set it holds

Elian Dorandca02a9a44cb
diff --git a/apps/client/src/widgets/attribute_widgets/select_input.tsx b/apps/client/src/widgets/attribute_widgets/select_input.tsx
index 8babff3eff..1dcefa7241 100644
--- a/apps/client/src/widgets/attribute_widgets/select_input.tsx
+++ b/apps/client/src/widgets/attribute_widgets/select_input.tsx
@@ -1,7 +1,7 @@
import "./select_input.css";
import clsx from "clsx";
-import type { InputHTMLAttributes, TargetedKeyboardEvent } from "preact";
+import type { ComponentChildren, InputHTMLAttributes, TargetedKeyboardEvent } from "preact";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../services/i18n";
@@ -123,6 +123,11 @@ interface SelectValuesInputProps {
* the field picks from the options as they stand.
*/
onCreateOption?(option: string): void | Promise<void>;
+ /**
+ * Shows a value as something other than the text it is stored as — a flag as its mark. Left out,
+ * a chip reads as the option it stands for, which is what a list of options names it.
+ */
+ renderValue?(value: string): ComponentChildren;
placeholder?: string;
disabled?: boolean;
}
@@ -135,7 +140,7 @@ interface SelectValuesInputProps {
* is ever kept: an entry has to be picked, and what is left behind is dropped when the field is. The
* list stays open across picks, since taking one value is rarely the end of it.
*/
-export function SelectValuesInput({ options, values, onCommit, onCreateOption, placeholder, disabled }: SelectValuesInputProps) {
+export function SelectValuesInput({ options, values, onCommit, onCreateOption, renderValue, placeholder, disabled }: SelectValuesInputProps) {
const [ filter, setFilter ] = useState("");
// Taken values leave the list: what is held is shown as a chip, so offering it again would only
@@ -183,7 +188,7 @@ export function SelectValuesInput({ options, values, onCommit, onCreateOption, p
disabled={disabled}
onRemove={() => drop(value)}
>
- <span>{value}</span>
+ <span>{renderValue ? renderValue(value) : value}</span>
</Chip>
))}
onChange={setFilter}
diff --git a/apps/client/src/widgets/collections/table/columns.css b/apps/client/src/widgets/collections/table/columns.css
index 233f4a7598..aaf629e5ff 100644
--- a/apps/client/src/widgets/collections/table/columns.css
+++ b/apps/client/src/widgets/collections/table/columns.css
@@ -12,6 +12,17 @@
}
}
+/* A flag among several, marked as the cell of a single flag marks it: set is told from unset by the
+ mark itself, so an unset one is drawn back rather than in a colour of its own. */
+.table-flag-set {
+ color: var(--muted-text-color);
+}
+
+.table-flag-unset {
+ color: var(--muted-text-color);
+ opacity: 0.55;
+}
+
/*
* The editor opens over its cell rather than inside it. A cell is as tall as one line and as wide as
* the column was dragged to, which a set of chips and a box to type in outgrows at once — laid out
diff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.ts
index 1f49ebebb5..051af6093d 100644
--- a/apps/client/src/widgets/collections/table/columns.spec.ts
+++ b/apps/client/src/widgets/collections/table/columns.spec.ts
@@ -302,7 +302,7 @@ describe("buildColumnDefinitions — multi-valued columns", () => {
}
it("edits and shows a set as chips whatever it holds, not only a set of options", () => {
- for (const type of [ "text", "date", "url", "select", "color" ] as const) {
+ for (const type of [ "text", "date", "url", "select", "color", "boolean" ] as const) {
const column = multiColumn(type);
// The type's own single-value editor would show one of the values and store back one.
expect(typeof column?.editor).toBe("function");
@@ -358,6 +358,27 @@ describe("buildColumnDefinitions — multi-valued columns", () => {
element.remove();
});
+ it("shows each value as its type reads it, rather than as the text it is stored as", () => {
+ const format = (type: AttributeDefinitionInformation["type"], values: string[]) => {
+ const formatter = multiColumn(type)?.formatter;
+ if (typeof formatter !== "function") throw new Error("expected a formatter of its own");
+ return formatter(
+ { getValue: () => values } as CellComponent, { type }, () => {}
+ ) as HTMLElement;
+ };
+
+ // A flag reads as the mark a column of single flags shows, not as the word "true".
+ const flags = format("boolean", [ "true", "false" ]);
+ expect([ ...flags.querySelectorAll(".tn-icon") ].map((mark) => mark.getAttribute("title")))
+ .toEqual([ "true", "false" ]);
+ expect(flags.querySelector(".table-flag-set")?.className).toContain("bx-check");
+ expect(flags.querySelector(".table-flag-unset")?.className).toContain("bx-x");
+
+ // And a colour as the same swatch a single one is shown by.
+ expect(format("color", [ "#ff2e88" ]).querySelector(".table-color-swatch")?.getAttribute("title"))
+ .toBe("#ff2e88");
+ });
+
it("sorts by the values a cell holds, not by the array they arrive in", () => {
const sorter = multiColumn("text")?.sorter;
if (typeof sorter !== "function") throw new Error("expected a sorter of its own");
diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsx
index f5c430f41a..79a414c05f 100644
--- a/apps/client/src/widgets/collections/table/columns.tsx
+++ b/apps/client/src/widgets/collections/table/columns.tsx
@@ -424,12 +424,15 @@ function ValuesEditor({ cell, success, editorParams }: EditorOpts) {
return (
<div ref={containerRef} className={clsx("table-values-editor", growsUpwards && "grows-upwards")}>
- {labelType === "select" ? (
+ {/* A flag is picked from a closed set as a select is — its set is simply the two values
+ a flag has — so the two are gathered through the same field. */}
+ {labelType === "select" || labelType === "boolean" ? (
<SelectValuesInput
- options={options}
+ options={labelType === "boolean" ? BOOLEAN_OPTIONS : options}
values={values}
placeholder={t("promoted_attributes.select_values_placeholder")}
- onCreateOption={onCreateOption}
+ onCreateOption={labelType === "boolean" ? undefined : onCreateOption}
+ renderValue={labelType === "boolean" ? renderFlag : undefined}
onCommit={editValues}
/>
) : (
@@ -462,6 +465,9 @@ function ValuesFormatter({ cell, formatterParams }: FormatterOpts) {
/** The schemes that make a value of a link-like type clickable; a url carries its own. */
const LINK_SCHEMES: Partial<Record<ColumnType, string>> = { url: "", email: "mailto:", phone: "tel:" };
+/** The whole of what a flag can be, which is what a column of flags picks its values from. */
+const BOOLEAN_OPTIONS = [ "true", "false" ];
+
/**
* One value of a set, read as its type reads it — so that a set of dates is as legible as a single
* one, and a set of addresses is as clickable. Anything else is the value as stored.
@@ -475,12 +481,29 @@ function renderValue(value: string, type: ColumnType | undefined) {
return <ColorSwatch color={value} />;
}
+ if (type === "boolean") {
+ return renderFlag(value);
+ }
+
const scheme = type && LINK_SCHEMES[type];
return scheme !== undefined
? <a href={applyLinkScheme(value, scheme)}>{value}</a>
: value;
}
+/**
+ * A flag as the mark a column of single flags shows, so that a cell holding several reads the same
+ * way as one holding the one. The stored text is kept in the tooltip, a value that is neither of the
+ * two being a thing worth being able to see.
+ */
+function renderFlag(value: string) {
+ return <Icon
+ className={value === "true" ? "table-flag-set" : "table-flag-unset"}
+ icon={value === "true" ? "bx bx-check" : "bx bx-x"}
+ title={value}
+ />;
+}
+
/**
* A multi-valued cell's values. Stored as an array, but a cell can hold what an older single-valued
* definition left behind, or nothing at all, so anything else is read as the set it stands for.
diff --git a/apps/client/src/widgets/collections/table/rows.spec.ts b/apps/client/src/widgets/collections/table/rows.spec.ts
index f718197070..3563553605 100644
--- a/apps/client/src/widgets/collections/table/rows.spec.ts
+++ b/apps/client/src/widgets/collections/table/rows.spec.ts
@@ -24,7 +24,7 @@ describe("getAttributeDefinitionInformation", () => {
]);
});
- it("holds several values of the types a set means something for, and one of the rest", async () => {
+ it("holds several values of every kind of label, a relation excepted", async () => {
const note = buildNote({
title: "Note 1",
"#label:tags(inheritable)": "promoted,multi,text",
@@ -32,20 +32,17 @@ describe("getAttributeDefinitionInformation", () => {
"#label:site(inheritable)": "promoted,multi,url",
"#label:status(inheritable)": "promoted,multi,select,options=Todo;Done",
"#label:tint(inheritable)": "promoted,multi,color",
- // A flag means what it means by being there at all, so several are the same one flag.
"#label:done(inheritable)": "promoted,multi,boolean",
"#label:owner(inheritable)": "promoted,single,text"
});
- // The one left out still has a column — holding the first value, as before it was declared
- // to hold many. It used to be dropped from the table altogether.
expect(getAttributeDefinitionInformation(note)).toMatchObject([
{ name: "tags", type: "text", isMulti: true },
{ name: "due", type: "date", isMulti: true },
{ name: "site", type: "url", isMulti: true },
{ name: "status", type: "select", isMulti: true },
{ name: "tint", type: "color", isMulti: true },
- { name: "done", type: "boolean", isMulti: false },
+ { name: "done", type: "boolean", isMulti: true },
{ name: "owner", type: "text", isMulti: false }
]);
});
diff --git a/apps/client/src/widgets/collections/table/rows.ts b/apps/client/src/widgets/collections/table/rows.ts
index 520a14142f..e1d26ec1c4 100644
--- a/apps/client/src/widgets/collections/table/rows.ts
+++ b/apps/client/src/widgets/collections/table/rows.ts
@@ -105,13 +105,14 @@ export default function getAttributeDefinitionInformation(parentNote: FNote) {
}
/**
- * The types a column can hold several values of, shown and edited as chips.
+ * The types a column can hold several values of, shown and edited as chips: every kind of label a
+ * note can carry, since a note can carry any of them more than once and the table has no business
+ * showing fewer than it holds.
*
- * The one left out is the one a set says nothing for: a flag means what it means by being there at
- * all, so several of them are the same one flag. Such a column shows the first value it finds, as it
- * did before it was declared to hold many — better a value than a column that quietly goes missing.
+ * Only a relation is left out, its values being notes rather than text — a column of them is still
+ * a column of one.
*/
const MULTI_VALUE_TYPES = new Set<LabelType | "relation">([
- "text", "textarea", "number", "select", "date", "datetime", "time", "url", "email", "phone",
- "color"
+ "text", "textarea", "number", "boolean", "select", "date", "datetime", "time", "url", "email",
+ "phone", "color"
]);