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): offer email and phone as promoted attribute types
Elian Doran94af742af0a2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 600c5aaaf4..4b9b923f87 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -542,6 +542,8 @@"date_time": "Date & Time","time": "Time","url": "URL",+ "email": "Email",+ "phone": "Phone","precision_title": "Max number of decimal places.","precision": "Precision","digits": "digits",@@ -1319,6 +1321,8 @@"unset-field-placeholder": "not set","url_placeholder": "http://website...","open_external_link": "Open external link",+ "send_email": "Send an email to this address",+ "call_phone": "Call this number","unknown_label_type": "Unknown label type '{{type}}'","unknown_attribute_type": "Unknown attribute type '{{type}}'","add_new_attribute": "Add new attribute",diff --git a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsxindex 82f5380aad..d3e2ddcbce 100644--- a/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx+++ b/apps/client/src/widgets/attribute_widgets/UserAttributesList.tsx@@ -91,6 +91,14 @@ function buildUserAttribute(attr: AttributeWithDefinitions): ComponentChildren {case "url":content = <a href={value} target="_blank" rel="noopener noreferrer" onClick={(e) => e.stopPropagation()}>{attr.friendlyName}</a>;break;+ // The address is short enough to show, and is the part worth clicking; the scheme check+ // keeps a value imported as a url label (already schemed) from being doubled.+ case "email":+ content = <>{defaultLabel}<a href={value.startsWith("mailto:") ? value : `mailto:${value}`} onClick={(e) => e.stopPropagation()}>{value}</a></>;+ break;+ case "phone":+ content = <>{defaultLabel}<a href={value.startsWith("tel:") ? value : `tel:${value}`} onClick={(e) => e.stopPropagation()}>{value}</a></>;+ break;case "color":style = { backgroundColor: value, color: getReadableTextColor(value) };content = <>{attr.friendlyName}</>;diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsxindex a351c918b1..285db4176c 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx@@ -749,6 +749,8 @@ export const LABEL_TYPES = [{ value: "datetime", title: t("attribute_detail.date_time"), icon: "bx bx-calendar-event" },{ value: "time", title: t("attribute_detail.time"), icon: "bx bx-time" },{ value: "url", title: t("attribute_detail.url"), icon: "bx bx-link" },+ { value: "email", title: t("attribute_detail.email"), icon: "bx bx-envelope" },+ { value: "phone", title: t("attribute_detail.phone"), icon: "bx bx-phone" },{ value: "color", title: t("attribute_detail.color_type"), icon: "bx bx-palette" }];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 f15d5b1422..cdb2e4d1f4 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@@ -7,7 +7,7 @@ import LabelValueInput, { getTypedInputForLabel, LABEL_MAPPINGS } from "./label_describe("getTypedInputForLabel", () => {it("offers a field of its own only where one beats the box a host already shows", () => {- for (const labelType of [ "color", "date", "datetime", "time", "number", "url" ] as const) {+ for (const labelType of [ "color", "date", "datetime", "time", "number", "url", "email", "phone" ] as const) {expect(getTypedInputForLabel(labelType)).toBe(labelType);}@@ -118,6 +118,24 @@ describe("LabelValueInput", () => {expect(container.querySelector("input")?.getAttribute("step")).toBe("0.01");});+ it("opens an email or a phone through its scheme, not doubling one an old value carries", async () => {+ const open = vi.spyOn(window, "open").mockImplementation(() => null);+ try {+ await mount({ labelType: "email", value: "[email protected]", onCommit: vi.fn() });+ expect(container.querySelector("input")?.type).toBe("email");+ await act(async () => container.querySelector<HTMLElement>(".open-external-link-button")?.click());+ expect(open).toHaveBeenLastCalledWith("mailto:[email protected]", "_blank");++ // A value imported as a url label before the typed field existed already carries its scheme.+ await mount({ labelType: "phone", value: "tel:12345", onCommit: vi.fn() });+ expect(container.querySelector("input")?.type).toBe("tel");+ await act(async () => container.querySelector<HTMLElement>(".open-external-link-button")?.click());+ expect(open).toHaveBeenLastCalledWith("tel:12345", "_blank");+ } finally {+ open.mockRestore();+ }+ });+it("maps every label type to a field, colour included", () => {// Guards the mapping against a type being added to the vocabulary without a field to edit it.expect(Object.keys(LABEL_MAPPINGS).length).toBeGreaterThan(0);diff --git a/apps/client/src/widgets/attribute_widgets/label_value_input.tsx b/apps/client/src/widgets/attribute_widgets/label_value_input.tsxindex f67b62c4fb..ca4181a71d 100644--- a/apps/client/src/widgets/attribute_widgets/label_value_input.tsx+++ b/apps/client/src/widgets/attribute_widgets/label_value_input.tsx@@ -21,11 +21,46 @@ export const LABEL_MAPPINGS: Record<LabelType, HTMLInputTypeAttribute | undefinedatetime: "datetime-local",time: "time",color: "hidden",- url: "url"+ url: "url",+ email: "email",+ phone: "tel"};const DEFAULT_COLOR = "#ffffff";+/**+ * The button beside a link-like field, opening what the field holds: a url as a page, an email or a+ * phone number through whatever the platform has registered for its scheme. The title is a thunk so+ * it is translated when rendered, not when the module loads.+ */+const OPEN_BUTTONS: Partial<Record<LabelType, {+ icon: string;+ title: () => string;+ /** Builds the href to open from the bare stored value. */+ toHref: (value: string) => string;+}>> = {+ url: {+ icon: "bx bx-window-open",+ title: () => t("promoted_attributes.open_external_link"),+ toHref: (value) => value+ },+ email: {+ icon: "bx bx-envelope",+ title: () => t("promoted_attributes.send_email"),+ toHref: (value) => applyScheme(value, "mailto:")+ },+ phone: {+ icon: "bx bx-phone",+ title: () => t("promoted_attributes.call_phone"),+ toHref: (value) => applyScheme(value, "tel:")+ }+};++/** A stored value is the bare address; an older one imported as a url may already carry the scheme. */+function applyScheme(value: string, scheme: string) {+ return value.startsWith(scheme) ? value : `${scheme}${value}`;+}+interface LabelValueInputProps {labelType: LabelType;/** The value as stored, which for a colour or a flag may be the empty string. */@@ -70,6 +105,8 @@ export default function LabelValueInput({}}+ const openButton = OPEN_BUTTONS[labelType];+const extraProps: InputHTMLAttributes<HTMLInputElement> = {};if (labelType === "number" && numberPrecision) {let step = 1;@@ -119,15 +156,15 @@ export default function LabelValueInput({/></>)}- {labelType === "url" && (+ {openButton && (<InputButtonclassName="open-external-link-button"- icon="bx bx-window-open"- title={t("promoted_attributes.open_external_link")}+ icon={openButton.icon}+ title={openButton.title()}onClick={() => {- const url = inputRef.current?.value || value;- if (url) {- window.open(url, "_blank");+ const target = inputRef.current?.value || value;+ if (target) {+ window.open(openButton.toHref(target), "_blank");}}}/>diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex bf4ca831bc..eca9085148 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -56,6 +56,22 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {formatter: "link",editor: "input"},+ email: {+ // The stored value is the bare address; the link formatter's url callback gives it its+ // scheme without repeating one an older value (imported as a url) may already carry.+ formatter: "link",+ formatterParams: {+ url: (cell) => applyLinkScheme(cell.getValue(), "mailto:")+ },+ editor: "input"+ },+ phone: {+ formatter: "link",+ formatterParams: {+ url: (cell) => applyLinkScheme(cell.getValue(), "tel:")+ },+ editor: "input"+ },color: {editor: "input",formatter: "color",@@ -71,6 +87,12 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {}};+/** Gives an email/phone value its clickable scheme unless it (an older value, stored as a url) already carries it. */+function applyLinkScheme(value: unknown, scheme: string): string {+ if (typeof value !== "string" || !value) return "";+ return value.startsWith(scheme) ? value : `${scheme}${value}`;+}+/*** Renders a stored date label through the user's formatting locale, all-numeric so that columns stay* narrow and every locale shows a four-digit year.diff --git a/packages/commons/src/lib/promoted_attribute_definition_parser.ts b/packages/commons/src/lib/promoted_attribute_definition_parser.tsindex a581abc6ca..8052bed3d8 100644--- a/packages/commons/src/lib/promoted_attribute_definition_parser.ts+++ b/packages/commons/src/lib/promoted_attribute_definition_parser.ts@@ -6,7 +6,8 @@ import { filterAttributeName } from "./attribute_names.js";* type cannot be added to the union without {@link parse} also accepting it.*/export const LABEL_TYPES = [- "text", "textarea", "number", "boolean", "date", "datetime", "time", "url", "color"+ "text", "textarea", "number", "boolean", "date", "datetime", "time", "url", "email", "phone",+ "color"] as const;export type LabelType = typeof LABEL_TYPES[number];