Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): read and pick a colour label as the colour itself

Elian Doran66c48eb08728
diff --git a/apps/client/src/widgets/attribute_widgets/label_value_display.tsx b/apps/client/src/widgets/attribute_widgets/label_value_display.tsx
index df5d85b941..23f68d1fd8 100644
--- a/apps/client/src/widgets/attribute_widgets/label_value_display.tsx
+++ b/apps/client/src/widgets/attribute_widgets/label_value_display.tsx
@@ -91,6 +91,16 @@ export function ColorSwatch({ color }: { color: string }) {
return <span className="label-color-swatch" title={color} style={{ backgroundColor: color }} />;
}
+/**
+ * A colour as a chip that is nothing but the colour: for wherever a colour is only read — a table
+ * cell, a chip among several, an attribute row — and so has no text or button needing a ground of
+ * their own. The stored text is kept to the tooltip, and one naming no colour is dropped by the
+ * browser, leaving the chip's own empty ring rather than showing a colour it is not.
+ */
+export function ColorChip({ color }: { color: string }) {
+ return <span className="tn-chip label-color-chip" title={color} style={{ backgroundColor: color }} />;
+}
+
/**
* The values a field holding several shows, read-only: the set as the very chips it is edited as, so
* that reading and editing show the same thing.
@@ -105,10 +115,8 @@ export function LabelValueChips({ values, labelType, className }: {
{values.map((value) => (
labelType === "color" ? (
// Read-only, the chip holds nothing — no text, no button — so the colour can be
- // its whole ground, the stored text kept to the tooltip. Set as the property so
- // that text naming no colour is dropped by the browser, leaving the chip's own
- // ground rather than showing a colour it is not.
- <span key={value} className="tn-chip label-color-chip" title={value} style={{ backgroundColor: value }} />
+ // its whole ground.
+ <ColorChip key={value} color={value} />
) : (
// A flag's chip takes a wash of the answer it carries, so a row of checks and
// crosses reads apart at a glance, before the marks themselves are.
diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.css
index ef20f4a05f..63e0ffeb15 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.css
+++ b/apps/client/src/widgets/sidebar/AttributeList.css
@@ -108,6 +108,12 @@
cursor: text;
}
+/* A colour's chip keeps to its own size: a chip is laid out as a flex box, which alone in the value's
+ slot would run the width of it, a bar rather than a chip. */
+.attribute-list-panel .attribute-value > .label-color-chip {
+ width: fit-content;
+}
+
/* The settings of a definition, which are a summary and not a value: quieter than one, and never the
reason the row is as wide as it is. Lowercased, the settings being sentence-cased words of Trilium's
own read as a list where they read as a sentence otherwise. */
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index 48e0f6beff..7ab8457924 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -417,6 +417,20 @@ describe("AttributeList", () => {
expect(input?.step).toBe("0.01");
});
+ it("shows a colour label as the colour itself, and edits it through the picker", () => {
+ renderPanel(buildNote({ id: "tinted", title: "Tinted", "#color": "#8000ff" }));
+
+ // The preview is the colour, not its text — which is kept to the chip's tooltip.
+ const chip = firstRow().querySelector<HTMLElement>(".attribute-value .label-color-chip");
+ expect(chip?.title).toBe("#8000ff");
+ expect(chip?.style.backgroundColor).toBeTruthy();
+
+ act(() => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());
+
+ const picker = container.querySelector<HTMLInputElement>(".attribute-value-editor input[type=color]");
+ expect(picker?.value).toBe("#8000ff");
+ });
+
it("discards what the popup was told when it is closed rather than pressed away from", async () => {
renderPanel(noteWithAttributes());
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index 23e6fcbd83..90daa01a23 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -16,6 +16,7 @@ import { t } from "../../services/i18n";
import server from "../../services/server";
import { isMobile } from "../../services/utils";
import { AttributeDetail, AttributeDetailOpts, AttributeForm, AttrType, DEFINITION_TYPES, getAttrType, RELATION_DEFINITION_TYPE } from "../attribute_widgets/attribute_detail";
+import { ColorChip } from "../attribute_widgets/label_value_display";
import ActionButton from "../react/ActionButton";
import { FormListItem } from "../react/FormList";
import HelpButton from "../react/HelpButton";
@@ -27,7 +28,7 @@ import NoteLink from "../react/NoteLink";
import { ParentComponent } from "../react/react_utils";
import { ATTRIBUTE_HELP_PAGE } from "../ribbon/components/AttributeHelp";
import OptionsSection from "../type_widgets/options/components/OptionsSection";
-import AttributeValueEditor from "./AttributeValueEditor";
+import AttributeValueEditor, { resolveValueField } from "./AttributeValueEditor";
import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";
/**
@@ -247,6 +248,7 @@ export default function AttributeList() {
)
} : undefined;
const rowProps = {
+ note,
activeAttribute: detail?.attribute,
valueEditor,
onOpen: openDetail,
@@ -424,6 +426,8 @@ function AttributeSection({ id, title, children, buttons, grow }: AttributeSecti
interface AttributeRowListProps {
rows: AttributeEntry[];
+ /** The note the rows belong to, read for the definitions that type their values. */
+ note?: FNote | null;
/** The attribute the detail popup is showing, marked as such in the list. */
activeAttribute?: Attribute;
/** The in-place editor over one row's value, shown by that row in the value's place. */
@@ -446,7 +450,7 @@ interface AttributeRowListProps {
* Trilium reads for itself. What a row offers follows from whether the note owns its attribute rather
* than from the card it is in: the definitions card holds the note's own alongside a template's.
*/
-function AttributeRowList({ rows, activeAttribute, valueEditor, readOnly, onOpen, onEditValue, onDelete }: AttributeRowListProps) {
+function AttributeRowList({ rows, note, activeAttribute, valueEditor, readOnly, onOpen, onEditValue, onDelete }: AttributeRowListProps) {
function renderRows(group: AttributeEntry[]) {
return (
// The rows are menu items on a phone (see AttributeRow), and the theme dresses a menu item
@@ -458,6 +462,7 @@ function AttributeRowList({ rows, activeAttribute, valueEditor, readOnly, onOpen
<AttributeRow
key={attribute.attributeId ?? `new-${index}`}
attribute={attribute}
+ note={note}
active={activeAttribute === attribute}
valueEditor={valueEditor?.attribute === attribute ? valueEditor.element : undefined}
isSystem={isSystem && !readOnly}
@@ -494,6 +499,8 @@ function AttributeRowList({ rows, activeAttribute, valueEditor, readOnly, onOpen
interface AttributeRowProps {
attribute: Attribute;
+ /** The note the row belongs to, read for the definition that types its value. */
+ note?: FNote | null;
/** Whether the detail popup is currently showing this attribute. */
active: boolean;
/** The in-place editor over this row's value, rendered in the value's place. */
@@ -508,7 +515,7 @@ interface AttributeRowProps {
onDelete?: () => void;
}
-function AttributeRow({ attribute, active, valueEditor, isSystem, showOwner, onOpen, onEditValue, onDelete }: AttributeRowProps) {
+function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwner, onOpen, onEditValue, onDelete }: AttributeRowProps) {
const rowRef = useRef<HTMLLIElement>(null);
const attrType = getAttributeKind(attribute);
const marker = getKindMarker(attribute, attrType, isSystem);
@@ -525,7 +532,7 @@ function AttributeRow({ attribute, active, valueEditor, isSystem, showOwner, onO
<>
<span class="attribute-name">{getDisplayName(attribute, attrType)}</span>
- {valueEditor ?? <AttributeValue attribute={attribute} attrType={attrType} onEdit={onEditValue} />}
+ {valueEditor ?? <AttributeValue attribute={attribute} note={note} attrType={attrType} onEdit={onEditValue} />}
{attribute.isInheritable && (
<Icon
@@ -647,8 +654,10 @@ function getDefinitionType(attribute: Attribute, attrType: AttributeKind) {
* full. A label's preview is also where its value is edited: pressing it swaps the text for the field
* (see {@link AttributeValueEditor}), where the row around it opens the popup.
*/
-function AttributeValue({ attribute, attrType, onEdit }: {
+function AttributeValue({ attribute, note, attrType, onEdit }: {
attribute: Attribute;
+ /** The note the attribute is shown on, read for the definition that types the value. */
+ note?: FNote | null;
attrType: AttributeKind;
/** Starts the in-place edit; only ever handed to a label's preview. */
onEdit?: () => void;
@@ -664,19 +673,24 @@ function AttributeValue({ attribute, attrType, onEdit }: {
return <DefinitionSummary attribute={attribute} />;
}
+ // A colour is read by eye rather than by its text, so the preview is the colour itself — the same
+ // chip a table cell reads it as — with the stored text kept to the chip's tooltip. An empty value
+ // still shows the chip, as the empty ring it draws: unset is an answer a colour field can give.
+ const isColor = note && resolveValueField(note, attribute.name).labelType === "color";
+
// A label with no value still gets its slot: it is what takes up the room between the name and what
// the row ends with, so a bare label lines its markers and its delete button up with every other row's.
return (
<span
class={clsx("attribute-value", onEdit && "editable")}
- title={attribute.value}
+ title={isColor ? undefined : attribute.value}
// Kept from the row, which would open the popup over the field this press asks for.
onClick={onEdit ? (e) => {
e.stopPropagation();
onEdit();
} : undefined}
>
- {attribute.value}
+ {isColor ? <ColorChip color={attribute.value ?? ""} /> : attribute.value}
</span>
);
}
diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.css b/apps/client/src/widgets/sidebar/AttributeValueEditor.css
index 8b1a6962f2..d381c8b801 100644
--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.css
+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.css
@@ -5,8 +5,10 @@
align-items: center;
gap: 4px;
- /* Sized to sit in a row of text rather than in a form. */
- & .form-control,
+ /* Sized to sit in a row of text rather than in a form. The colour swatch is left out: it has a
+ size of its own (see label_value_input.css), stated at the same weight as this rule — matching
+ it here would leave the winner to stylesheet order. */
+ & .form-control:not(.label-color-picker),
& .form-select {
min-width: 0;
height: auto;
diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
index 01f456b03a..4d1091d06c 100644
--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
@@ -35,12 +35,22 @@ export default function AttributeValueEditor({ note, attribute, onEdit, onCommit
const typed = useMemo(() => resolveValueField(note, attribute.name), [ note, attribute.name ]);
// The editor exists because its row was pressed, so the field is ready to type into at once — and
- // what is there is selected, a short value being more often replaced than appended to.
+ // what is there is selected, a short value being more often replaced than appended to. A colour
+ // goes further: picking is all there is to do to it, so the picker's dialog opens with the editor
+ // rather than waiting to be asked by a second press on the swatch.
useEffect(() => {
const field = containerRef.current?.querySelector<HTMLElement>("input:not([type=hidden]), textarea, select");
field?.focus();
if ((field instanceof HTMLInputElement && SELECT_ALL_TYPES.has(field.type)) || field instanceof HTMLTextAreaElement) {
field.select();
+ } else if (field instanceof HTMLInputElement && field.type === "color") {
+ try {
+ // Optional because not every engine offers it, and guarded because it refuses to open
+ // without the user activation the press provides — which a test's render lacks.
+ field.showPicker?.();
+ } catch {
+ // The swatch is still there to be pressed, as it would be without the head start.
+ }
}
}, []);
@@ -118,8 +128,10 @@ const SELECT_ALL_TYPES = new Set([ "text", "number", "url", "email", "tel" ]);
* kinds the detail form offers, exclusions included (a flag means what its presence means, so no
* checkbox over it); for the rest whatever definition reaches the note for the name, promoted or not,
* down to the plain text box a label defined by nothing is typed into.
+ *
+ * Exported for the row's preview, which reads the same answer to know a colour when it shows one.
*/
-function resolveValueField(note: FNote, name: string): {
+export function resolveValueField(note: FNote, name: string): {
labelType: LabelType;
selectOptions?: readonly string[];
numberPrecision?: number;