mirror of
https://github.com/zadam/trilium.git
synced 2025-11-14 18:39:01 +01:00
refactor(board/promoted_attributes): reduce duplication
This commit is contained in:
parent
3036d18df5
commit
b13c0fe7a2
@ -5,7 +5,7 @@ import { useTriliumEvent } from "../react/hooks";
|
|||||||
import attributes from "../../services/attributes";
|
import attributes from "../../services/attributes";
|
||||||
import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";
|
import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";
|
||||||
import { formatDateTime } from "../../utils/formatters";
|
import { formatDateTime } from "../../utils/formatters";
|
||||||
import { ComponentChildren, CSSProperties } from "preact";
|
import { ComponentChild, ComponentChildren, CSSProperties } from "preact";
|
||||||
import Icon from "../react/Icon";
|
import Icon from "../react/Icon";
|
||||||
import NoteLink from "../react/NoteLink";
|
import NoteLink from "../react/NoteLink";
|
||||||
import { getReadableTextColor } from "../../services/css_class_manager";
|
import { getReadableTextColor } from "../../services/css_class_manager";
|
||||||
@ -56,39 +56,53 @@ function PromotedAttribute({ attr, children, style }: { attr: AttributeWithDefin
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildPromotedAttribute(attr: AttributeWithDefinitions): ComponentChildren {
|
function buildPromotedAttribute(attr: AttributeWithDefinitions): ComponentChildren {
|
||||||
if (attr.type === "relation") {
|
const defaultLabel = <><strong>{attr.friendlyName}:</strong>{" "}</>;
|
||||||
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> <NoteLink notePath={attr.value} showNoteIcon /></PromotedAttribute>
|
let content: ComponentChildren;
|
||||||
}
|
let style: CSSProperties | undefined;
|
||||||
|
|
||||||
|
if (attr.type === "label") {
|
||||||
let value = attr.value;
|
let value = attr.value;
|
||||||
switch (attr.def.labelType) {
|
switch (attr.def.labelType) {
|
||||||
case "number":
|
case "number":
|
||||||
let formattedValue = value;
|
let formattedValue = value;
|
||||||
const numberValue = Number(value);
|
const numberValue = Number(value);
|
||||||
if (attr.def.numberPrecision) {
|
if (!Number.isNaN(numberValue) && attr.def.numberPrecision) formattedValue = numberValue.toFixed(attr.def.numberPrecision);
|
||||||
formattedValue = numberValue.toFixed(attr.def.numberPrecision);
|
content = <>{defaultLabel}{formattedValue}</>;
|
||||||
}
|
break;
|
||||||
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formattedValue}</PromotedAttribute>;
|
|
||||||
case "date":
|
case "date":
|
||||||
case "datetime": {
|
case "datetime": {
|
||||||
const date = new Date(value);
|
const date = new Date(value);
|
||||||
const timeFormat = attr.def.labelType !== "date" ? "short" : "none";
|
const timeFormat = attr.def.labelType !== "date" ? "short" : "none";
|
||||||
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formatDateTime(date, "short", timeFormat)}</PromotedAttribute>;
|
const formattedValue = formatDateTime(date, "short", timeFormat);
|
||||||
|
content = <>{defaultLabel}{formattedValue}</>;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case "time": {
|
case "time": {
|
||||||
const date = new Date(`1970-01-01T${value}Z`);
|
const date = new Date(`1970-01-01T${value}Z`);
|
||||||
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formatDateTime(date, "none", "short")}</PromotedAttribute>;
|
const formattedValue = formatDateTime(date, "none", "short");
|
||||||
|
content = <>{defaultLabel}{formattedValue}</>;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return <PromotedAttribute attr={attr}><Icon icon={value === "true" ? "bx bx-check-square" : "bx bx-square"} /> <strong>{attr.friendlyName}</strong></PromotedAttribute>;
|
content = <><Icon icon={value === "true" ? "bx bx-check-square" : "bx bx-square"} />{" "}<strong>{attr.friendlyName}</strong></>;
|
||||||
|
break;
|
||||||
case "url":
|
case "url":
|
||||||
return <PromotedAttribute attr={attr}><a href={value} target="_blank" rel="noopener noreferrer">{attr.friendlyName}</a></PromotedAttribute>;
|
content = <a href={value} target="_blank" rel="noopener noreferrer">{attr.friendlyName}</a>;
|
||||||
|
break;
|
||||||
case "color":
|
case "color":
|
||||||
return <PromotedAttribute attr={attr} style={{ backgroundColor: value, color: getReadableTextColor(value) }}>{attr.friendlyName}</PromotedAttribute>;
|
style = { backgroundColor: value, color: getReadableTextColor(value) };
|
||||||
|
content = <>{attr.friendlyName}</>;
|
||||||
|
break;
|
||||||
case "text":
|
case "text":
|
||||||
default:
|
default:
|
||||||
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {value}</PromotedAttribute>;
|
content = <>{defaultLabel}{value}</>;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (attr.type === "relation") {
|
||||||
|
content = <>{defaultLabel}<NoteLink notePath={attr.value} showNoteIcon /></>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <PromotedAttribute attr={attr} style={style}>{content}</PromotedAttribute>
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
|
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user