From 3036d18df5880c75a727b68b1717a46d1692b774 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 13 Nov 2025 09:14:13 +0200 Subject: [PATCH] feat(board/promoted_attributes): improve rendering for color badge --- apps/client/src/services/css_class_manager.ts | 5 ++ .../PromotedAttributesDisplay.tsx | 49 ++++++++++--------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/apps/client/src/services/css_class_manager.ts b/apps/client/src/services/css_class_manager.ts index ff0270cf3..b28b24b23 100644 --- a/apps/client/src/services/css_class_manager.ts +++ b/apps/client/src/services/css_class_manager.ts @@ -76,6 +76,11 @@ function getHue(color: ColorInstance) { } } +export function getReadableTextColor(bgColor: string) { + const colorInstance = Color(bgColor); + return colorInstance.isLight() ? "#000" : "#fff"; +} + export default { createClassForColor }; diff --git a/apps/client/src/widgets/attribute_widgets/PromotedAttributesDisplay.tsx b/apps/client/src/widgets/attribute_widgets/PromotedAttributesDisplay.tsx index 51cb4649e..3b90dc3d2 100644 --- a/apps/client/src/widgets/attribute_widgets/PromotedAttributesDisplay.tsx +++ b/apps/client/src/widgets/attribute_widgets/PromotedAttributesDisplay.tsx @@ -5,9 +5,10 @@ import { useTriliumEvent } from "../react/hooks"; import attributes from "../../services/attributes"; import { DefinitionObject } from "../../services/promoted_attribute_definition_parser"; import { formatDateTime } from "../../utils/formatters"; -import { ComponentChildren } from "preact"; +import { ComponentChildren, CSSProperties } from "preact"; import Icon from "../react/Icon"; import NoteLink from "../react/NoteLink"; +import { getReadableTextColor } from "../../services/css_class_manager"; interface PromotedAttributesDisplayProps { note: FNote; @@ -26,15 +27,7 @@ export default function PromotedAttributesDisplay({ note, ignoredAttributes }: P const promotedDefinitionAttributes = useNoteAttributesWithDefinitions(note, ignoredAttributes); return promotedDefinitionAttributes?.length > 0 && (
- {promotedDefinitionAttributes?.map((attr) => { - const className = `${attr.type === "label" ? "label" + " " + attr.def.labelType : "relation"}`; - return ( - - {attr.type === "relation" ? formatRelation(attr) : formatLabelValue(attr)} - - ); - } - )} + {promotedDefinitionAttributes?.map(attr => buildPromotedAttribute(attr))}
) @@ -52,7 +45,21 @@ function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: stri return promotedDefinitionAttributes; } -function formatLabelValue(attr: AttributeWithDefinitions): ComponentChildren { +function PromotedAttribute({ attr, children, style }: { attr: AttributeWithDefinitions, children: ComponentChildren, style?: CSSProperties }) { + const className = `${attr.type === "label" ? "label" + " " + attr.def.labelType : "relation"}`; + + return ( + + {children} + + ) +} + +function buildPromotedAttribute(attr: AttributeWithDefinitions): ComponentChildren { + if (attr.type === "relation") { + return {attr.friendlyName}: + } + let value = attr.value; switch (attr.def.labelType) { case "number": @@ -61,35 +68,29 @@ function formatLabelValue(attr: AttributeWithDefinitions): ComponentChildren { if (attr.def.numberPrecision) { formattedValue = numberValue.toFixed(attr.def.numberPrecision); } - return <>{attr.friendlyName}: {formattedValue}; + return {attr.friendlyName}: {formattedValue}; case "date": case "datetime": { const date = new Date(value); const timeFormat = attr.def.labelType !== "date" ? "short" : "none"; - return <>{attr.friendlyName}: {formatDateTime(date, "short", timeFormat)}; + return {attr.friendlyName}: {formatDateTime(date, "short", timeFormat)}; } case "time": { const date = new Date(`1970-01-01T${value}Z`); - return <>{attr.friendlyName}: {formatDateTime(date, "none", "short")}; + return {attr.friendlyName}: {formatDateTime(date, "none", "short")}; } case "boolean": - return <> {attr.friendlyName}; + return {attr.friendlyName}; case "url": - return <>{attr.friendlyName}; + return {attr.friendlyName}; case "color": - return <>{attr.friendlyName}; + return {attr.friendlyName}; case "text": default: - return <>{attr.friendlyName}: {value}; + return {attr.friendlyName}: {value}; } } -function formatRelation(attr: AttributeWithDefinitions): ComponentChildren { - return ( - <>{attr.friendlyName}: - ) -} - function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] { const promotedDefinitionAttributes = note.getAttributeDefinitions(); const result: AttributeWithDefinitions[] = [];