Skip to content

Commit 02a4e04

Browse files
authored
[ENG-1481] Bug fix inserting node via relations menu on tldraw canvas (#863)
* bug fix * lint
1 parent 415f5bb commit 02a4e04

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

apps/obsidian/src/components/canvas/DiscourseRelationTool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getRelationTypeById } from "~/utils/typeUtils";
66
import { DiscourseRelationShape } from "./shapes/DiscourseRelationShape";
77
import { getNodeTypeById } from "~/utils/typeUtils";
88
import { showToast } from "./utils/toastUtils";
9-
import { DEFAULT_TLDRAW_COLOR } from "~/utils/tldrawColors";
9+
import { toTldrawColor } from "~/utils/tldrawColors";
1010

1111
type RelationToolContext = {
1212
plugin: DiscourseGraphPlugin;
@@ -251,7 +251,7 @@ class Pointing extends StateNode {
251251
props: {
252252
relationTypeId: relationToolContext.relationTypeId,
253253
text: relationType?.label ?? "",
254-
color: relationType?.color ?? DEFAULT_TLDRAW_COLOR,
254+
color: toTldrawColor(relationType?.color),
255255
scale: this.editor.user.getIsDynamicResizeMode()
256256
? 1 / this.editor.getZoomLevel()
257257
: 1,

apps/obsidian/src/components/canvas/overlays/RelationPanel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { getFrontmatterForFile } from "~/components/canvas/shapes/discourseNodeShapeUtils";
2020
import { getRelationTypeById } from "~/utils/typeUtils";
2121
import { showToast } from "~/components/canvas/utils/toastUtils";
22-
import { DEFAULT_TLDRAW_COLOR } from "~/utils/tldrawColors";
22+
import { toTldrawColor } from "~/utils/tldrawColors";
2323
import {
2424
getNodeInstanceIdForFile,
2525
getRelationsForNodeInstanceId,
@@ -400,7 +400,7 @@ export const RelationsPanel = ({
400400
dash: "draw",
401401
size: "m",
402402
fill: "none",
403-
color: relationType?.color ?? DEFAULT_TLDRAW_COLOR,
403+
color: toTldrawColor(relationType?.color),
404404
labelColor: "black",
405405
bend: 0,
406406
// Will be updated by bindings

apps/obsidian/src/utils/tldrawColors.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,26 @@ export const COLOR_PALETTE: Record<string, string> = {
5858
white: "#ffffff",
5959
yellow: "#ffc078",
6060
};
61+
62+
/**
63+
* Ensures a color value is a valid TldrawColorName.
64+
* If the value is already a valid name, returns it directly.
65+
* If it's a hex value, finds the matching tldraw color.
66+
* Falls back to DEFAULT_TLDRAW_COLOR if no match is found.
67+
*/
68+
export const toTldrawColor = (color: string | undefined): TldrawColorName => {
69+
if (!color) return DEFAULT_TLDRAW_COLOR;
70+
71+
if (TLDRAW_COLOR_NAMES.includes(color as TldrawColorName)) {
72+
return color as TldrawColorName;
73+
}
74+
75+
const hex = color.toLowerCase();
76+
for (const [name, paletteHex] of Object.entries(COLOR_PALETTE)) {
77+
if (paletteHex.toLowerCase() === hex) {
78+
return name as TldrawColorName;
79+
}
80+
}
81+
82+
return DEFAULT_TLDRAW_COLOR;
83+
};

0 commit comments

Comments
 (0)