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
feat(attributes): turn a flag over on the press, no editor between
Elian Doranc4f7849f3f6b
diff --git a/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx b/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsxindex 609bd51884..3d6839ed2e 100644--- a/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx+++ b/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx@@ -1,5 +1,6 @@import "./AttributeCreationEditor.css";+import type { ComponentChildren } from "preact";import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";import type FNote from "../../entities/fnote";@@ -162,27 +163,37 @@ export default function AttributeCreationEditor({ note, attribute, onCommit, onR}}/>) : typed && (- <LabelValueInput- // Remounted when the name resolves to another kind of field, reseeded from- // whatever the draft holds by then.- labelType={typed.labelType}- value={attribute.value ?? ""}- onCommit={(value) => {- attribute.value = value;- }}- commitOn="input"- numberPrecision={typed.numberPrecision}- selectOptions={typed.selectOptions}- hideOpenButton- inputProps={{- className: "form-control",- ...(typed.labelType === "select" && {- placeholder: t("promoted_attributes.unset-field-placeholder")- })- }}- />+ // A flag's switch inside the label the theme dresses a checkbox through; bare, it+ // comes out a broken sliver. No commit per toggle here, unlike the value editor's+ // (see LabelField there): the name may still be waiting to be typed.+ <FlagWrapper isFlag={typed.labelType === "boolean"}>+ <LabelValueInput+ // Remounted when the name resolves to another kind of field, reseeded from+ // whatever the draft holds by then.+ labelType={typed.labelType}+ value={attribute.value ?? ""}+ onCommit={(value) => {+ attribute.value = value;+ }}+ commitOn="input"+ numberPrecision={typed.numberPrecision}+ selectOptions={typed.selectOptions}+ hideOpenButton+ inputProps={{+ className: "form-control",+ ...(typed.labelType === "select" && {+ placeholder: t("promoted_attributes.unset-field-placeholder")+ })+ }}+ />+ </FlagWrapper>)}</span></AttributeEditorOverlay>);}++/** The `tn-checkbox` label a flag's switch is dressed through; everything else passes bare. */+function FlagWrapper({ isFlag, children }: { isFlag: boolean; children: ComponentChildren }) {+ return isFlag ? <label className="tn-checkbox">{children}</label> : <>{children}</>;+}diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.cssindex ce8b4c1646..22fd266f48 100644--- a/apps/client/src/widgets/sidebar/AttributeList.css+++ b/apps/client/src/widgets/sidebar/AttributeList.css@@ -130,6 +130,11 @@cursor: text;}+/* A flag's press toggles rather than types (see startValueEdit), and the pointer says so. */+.attribute-list-panel .attribute-value.editable.flag {+ cursor: pointer;+}+/* The placeholder holding an empty label's slot open. Hidden rather than left out, so the slot keepsthe height the text gives it, and shown as the row's buttons are: over the row being pointed at,which is also the only row whose slot could be pressed. */diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsxindex e5a9467166..5b256aa9c6 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx@@ -633,6 +633,26 @@ describe("AttributeList", () => {expect(editor?.querySelector(".input-group-text")).toBeNull();});+ it("previews a flag as its mark and turns it over on the press itself, no editor between", async () => {+ renderPanel(buildNote({+ id: "flagged", title: "Flagged",+ "#done": "false",+ "#label:done": "promoted,single,boolean"+ }));++ // The mark a table cell reads a flag as, in place of the stored word.+ expect(firstRow().querySelector(".attribute-value .label-flag-unset")).not.toBeNull();++ await act(async () => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());++ // The press toggled and saved; nothing opened, and the mark turned with it.+ expect(container.querySelector(".attribute-value-editor")).toBeNull();+ expect(put).toHaveBeenCalledOnce();+ const [ , saved ] = put.mock.calls[0] as [ string, { name: string; value: string }[] ];+ expect(saved.find((attribute) => attribute.name === "done")?.value).toBe("true");+ expect(firstRow().querySelector(".attribute-value .label-flag-set")).not.toBeNull();+ });+it("shows a colour label as the colour itself, and edits it through the picker", () => {renderPanel(buildNote({ id: "tinted", title: "Tinted", "#color": "#8000ff" }));diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex 9c87d2ba31..73285fb4f1 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -16,7 +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 { ColorChip, renderLabelValue } from "../attribute_widgets/label_value_display";import ActionButton from "../react/ActionButton";import { FormListItem } from "../react/FormList";import HelpButton from "../react/HelpButton";@@ -215,6 +215,16 @@ export default function AttributeList() {commit();}commitCreation();++ // A flag has nothing to type: the press that would open an editor just turns it over, saved+ // at once — the one-toggle edit an editor could only have added a second press to.+ if (note && attribute.type === "label" && resolveValueField(note, attribute.name).labelType === "boolean") {+ attribute.value = attribute.value === "true" ? "false" : "true";+ rerender();+ void save();+ return;+ }+setValueEdit({ attribute, original: attribute.value ?? "" });}@@ -822,21 +832,25 @@ function AttributeValue({ attribute, note, attrType, onEdit }: {// 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 flag likewise previews as the mark a table cell reads it as, and the press that would edit+ // any other value just turns it over (see startValueEdit) — hence the pointer, not the text beam.+ const labelType = note ? resolveValueField(note, attribute.name).labelType : undefined;+ const isColor = labelType === "color";+ const isFlag = labelType === "boolean";// An editable slot holding nothing gets a placeholder instead of the blank: the row centres its// items, which collapses an empty span to a height of nothing — width but no height, a click// target that cannot be hit. The placeholder holds the slot open and names the way in, though only// over the row being pointed at (see the stylesheet): many a bare label is a flag meaning what its// presence means, and a standing "no value" against every one of them would read as a lack.- const placeholder = !isColor && !attribute.value && onEdit;+ const placeholder = !isColor && !isFlag && !attribute.value && onEdit;// 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", placeholder && "empty value-placeholder")}- title={isColor || placeholder ? undefined : attribute.value}+ class={clsx("attribute-value", onEdit && "editable", isFlag && "flag", placeholder && "empty value-placeholder")}+ title={isColor || isFlag || placeholder ? undefined : attribute.value}// Kept from the row, which would open the popup over the field this press asks for.onClick={onEdit ? (e) => {e.stopPropagation();@@ -844,8 +858,9 @@ function AttributeValue({ attribute, note, attrType, onEdit }: {} : undefined}>{isColor ? <ColorChip color={attribute.value ?? ""} />- : placeholder ? t("attribute_list_panel.no_value")- : attribute.value}+ : isFlag ? renderLabelValue(attribute.value ?? "", "boolean")+ : placeholder ? t("attribute_list_panel.no_value")+ : attribute.value}</span>);}diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsxindex 7efb633806..423dcb7ed1 100644--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx@@ -97,24 +97,7 @@ export default function AttributeValueEditor({ note, attribute, onEdit, onCommit}}>{typed ? (- <LabelValueInput- labelType={typed.labelType}- value={attribute.value ?? ""}- onCommit={onEdit}- commitOn="input"- numberPrecision={typed.numberPrecision}- selectOptions={typed.selectOptions}- // The row has no room to spare beside the field for the button opening a- // link-like value; the detail form keeps it, having the room.- hideOpenButton- inputProps={{- className: "form-control",- // Names the dropdown's empty entry, as the detail form does for the same field.- ...(typed.labelType === "select" && {- placeholder: t("promoted_attributes.unset-field-placeholder")- })- }}- />+ <LabelField typed={typed} attribute={attribute} onEdit={onEdit} onCommit={onCommit} />) : (<NoteAutocompletenoteId={attribute.value || undefined}@@ -126,6 +109,48 @@ export default function AttributeValueEditor({ note, attribute, onEdit, onCommit);}+/**+ * The field a label's value is typed into, dressed by its kind. A flag gets two things of its own:+ * the `tn-checkbox` label the theme dresses a checkbox through — bare, the switch comes out a broken+ * sliver — and a commit per toggle, the toggle being the whole of a flag's edit: nothing further to+ * type, so the switch both writes and closes.+ */+function LabelField({ typed, attribute, onEdit, onCommit }: {+ typed: ReturnType<typeof resolveValueField>;+ attribute: Attribute;+ onEdit(value: string): void;+ onCommit(): void;+}) {+ const isBoolean = typed.labelType === "boolean";+ const input = (+ <LabelValueInput+ labelType={typed.labelType}+ value={attribute.value ?? ""}+ onCommit={(value) => {+ onEdit(value);+ if (isBoolean) {+ onCommit();+ }+ }}+ commitOn="input"+ numberPrecision={typed.numberPrecision}+ selectOptions={typed.selectOptions}+ // The row has no room to spare beside the field for the button opening a+ // link-like value; the detail form keeps it, having the room.+ hideOpenButton+ inputProps={{+ className: "form-control",+ // Names the dropdown's empty entry, as the detail form does for the same field.+ ...(typed.labelType === "select" && {+ placeholder: t("promoted_attributes.unset-field-placeholder")+ })+ }}+ />+ );++ return isBoolean ? <label className="tn-checkbox">{input}</label> : input;+}+/** The input types whose content `select()` applies to; the pickers select nothing and mind nothing. */const SELECT_ALL_TYPES = new Set([ "text", "number", "url", "email", "tel" ]);