diff --git a/apps/obsidian/src/utils/__tests__/conceptConversion.test.ts b/apps/obsidian/src/utils/__tests__/conceptConversion.test.ts index 4e12e6ee9..a20044e8d 100644 --- a/apps/obsidian/src/utils/__tests__/conceptConversion.test.ts +++ b/apps/obsidian/src/utils/__tests__/conceptConversion.test.ts @@ -117,7 +117,16 @@ describe("discourseNodeSchemaToLocalConcept format", () => { ).toMatchObject({ literal_content: { format: "CLM - {content}" } }); }); - it("keeps the other metadata in source data without the format", () => { + it("publishes the color at the top level of literal content", () => { + expect( + discourseNodeSchemaToLocalConcept({ + accountLocalId: "account-1", + node: NODE_TYPE, + }), + ).toMatchObject({ literal_content: { color: "#7DA13E" } }); + }); + + it("keeps the other metadata in source data without the format or the color", () => { expect( discourseNodeSchemaToLocalConcept({ accountLocalId: "account-1", @@ -125,7 +134,6 @@ describe("discourseNodeSchemaToLocalConcept format", () => { }), ).toHaveProperty("literal_content.source_data", { shortcut: "C", - color: "#7DA13E", tag: "clm-candidate", keyImage: true, folderPath: "Claims", diff --git a/apps/obsidian/src/utils/__tests__/syncDgNodesToSupabase.test.ts b/apps/obsidian/src/utils/__tests__/syncDgNodesToSupabase.test.ts index 879445c24..4203cf85a 100644 --- a/apps/obsidian/src/utils/__tests__/syncDgNodesToSupabase.test.ts +++ b/apps/obsidian/src/utils/__tests__/syncDgNodesToSupabase.test.ts @@ -613,8 +613,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => { format: "CLM - {content}", template: "Claim template", template_content: "## Evidence\n", + color: "#7DA13E", source_data: { - color: "#7DA13E", tag: "clm", shortcut: "C", keyImage: true, @@ -671,7 +671,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => { roles: ["source", "destination"], label: "cites", complement: "is cited by", - source_data: { color: "blue" }, + color: "blue", + source_data: {}, }, }); }); @@ -689,7 +690,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => { label: "opposes", complement: "is opposed by", importedFromRid: "orn:obsidian.schema:other-vault/opposes-type", - source_data: { color: "red", authorId: 9 }, + color: "red", + source_data: { authorId: 9 }, }, }); }); diff --git a/apps/obsidian/src/utils/conceptConversion.ts b/apps/obsidian/src/utils/conceptConversion.ts index c9b5da6e6..7d18262dc 100644 --- a/apps/obsidian/src/utils/conceptConversion.ts +++ b/apps/obsidian/src/utils/conceptConversion.ts @@ -42,6 +42,7 @@ export const discourseNodeSchemaToLocalConcept = ({ id, name, format, + color, created, modified, importedFromRid, @@ -49,7 +50,7 @@ export const discourseNodeSchemaToLocalConcept = ({ } = node; const concept = crossAppNodeSchemaToDbConcept( nodeTypeToCrossApp({ - nodeType: { id, name, format, template, created, modified }, + nodeType: { id, name, format, color, template, created, modified }, templateContent, accountLocalId, }), @@ -77,6 +78,7 @@ export const discourseRelationTypeToLocalConcept = ({ id, label, complement, + color, created, modified, importedFromRid, @@ -86,7 +88,7 @@ export const discourseRelationTypeToLocalConcept = ({ } = relationType; const concept = crossAppRelationTypeSchemaToDbConcept( relationTypeToCrossApp({ - relationType: { id, label, complement, created, modified }, + relationType: { id, label, complement, color, created, modified }, accountLocalId, }), ); diff --git a/apps/obsidian/src/utils/obsidianToCrossAppConverters.ts b/apps/obsidian/src/utils/obsidianToCrossAppConverters.ts index db065b5d6..12d1fbbc8 100644 --- a/apps/obsidian/src/utils/obsidianToCrossAppConverters.ts +++ b/apps/obsidian/src/utils/obsidianToCrossAppConverters.ts @@ -25,7 +25,7 @@ export const nodeTypeToCrossApp = ({ }: { nodeType: Pick< DiscourseNode, - "id" | "name" | "format" | "template" | "created" | "modified" + "id" | "name" | "format" | "color" | "template" | "created" | "modified" >; templateContent?: string; accountLocalId: string; @@ -33,6 +33,7 @@ export const nodeTypeToCrossApp = ({ localId: nodeType.id, label: nodeType.name, format: nodeType.format, + color: nodeType.color, templateTitle: nodeType.template || undefined, template: templateContent || undefined, authorId: accountLocalId, @@ -46,13 +47,14 @@ export const relationTypeToCrossApp = ({ }: { relationType: Pick< DiscourseRelationType, - "id" | "label" | "complement" | "created" | "modified" + "id" | "label" | "complement" | "color" | "created" | "modified" >; accountLocalId: string; }): CrossAppRelationTypeSchema => ({ localId: relationType.id, label: relationType.label, complement: relationType.complement, + color: relationType.color, authorId: accountLocalId, createdAt: new Date(relationType.created), modifiedAt: new Date(relationType.modified), diff --git a/packages/database/doc/concept_design.md b/packages/database/doc/concept_design.md index f85daa3a4..50f9e2f4a 100644 --- a/packages/database/doc/concept_design.md +++ b/packages/database/doc/concept_design.md @@ -106,7 +106,7 @@ CrossAppRelation: Note on metadata: It is read from the database, but currently not written. We will phase it out. -When some specific keys of `literal_content` are mapped to a CrossApp field, those key-value pairs are not included again in the `metadata`. Thus `metadata` is a grab-bag for residual data. (Eg color for now.) +When some specific keys of `literal_content` are mapped to a CrossApp field, those key-value pairs are not included again in the `metadata`. Thus `metadata` is a grab-bag for residual data. (Eg `source_data` for now.) Residual (not otherwise accounted for) keys in Obsidian frontmatter are mapped to `literal_content->source_data`. (Not through CrossApp.) @@ -119,6 +119,7 @@ Residual (not otherwise accounted for) keys in Obsidian frontmatter are mapped t | `template` | `literal_content->template_content` | | | `templateTitle` | `literal_content->template` | | | `format` | `literal_content->format` | | +| `color` | `literal_content->color` | hex | | - | `is_schema` | true | | - | `schema_id` | null | | - | `arity` | 0 | @@ -169,6 +170,7 @@ Query filter: `.eq("is_relation", false).eq("is_schema", false)` | - | `reference_content` | {} | | `label` | `name`, `literal_content->label` | | | `complement` | `literal_content->complement` | | +| `color` | `literal_content->color` | tldraw color name | | - | `is_relation` | true | Query filter: `.eq("is_relation", true).eq("is_schema", true).is("reference_content->source", "null")` diff --git a/packages/database/src/crossAppContracts.ts b/packages/database/src/crossAppContracts.ts index 47483c408..9bdaa32ef 100644 --- a/packages/database/src/crossAppContracts.ts +++ b/packages/database/src/crossAppContracts.ts @@ -26,6 +26,7 @@ export type CrossAppNodeSchema = CrossAppSchemaBase & { template?: string; templateTitle?: string; format?: string; + color?: string; slotDefinitions?: Record; }; @@ -33,7 +34,8 @@ export type CrossAppNodeSchema = CrossAppSchemaBase & { export type CrossAppRelationTypeSchema = CrossAppSchemaBase & { label: string; complement: string; - // should we add colour? format? + color?: string; + // should we add format? }; // A relation triple schema diff --git a/packages/database/src/lib/__tests__/crossAppConverters.test.ts b/packages/database/src/lib/__tests__/crossAppConverters.test.ts index 7481b56ba..f0a4639dd 100644 --- a/packages/database/src/lib/__tests__/crossAppConverters.test.ts +++ b/packages/database/src/lib/__tests__/crossAppConverters.test.ts @@ -3,6 +3,7 @@ import type { CrossAppNode, CrossAppNodeSchema } from "../../crossAppContracts"; import { crossAppNodeSchemaToDbConcept, crossAppNodeToDbConcept, + crossAppRelationTypeSchemaToDbConcept, } from "../crossAppConverters"; const baseSchema: CrossAppNodeSchema = { @@ -50,6 +51,17 @@ describe("crossAppNodeSchemaToDbConcept", () => { }); }); + it("maps color to literal_content.color", () => { + const concept = crossAppNodeSchemaToDbConcept({ + ...baseSchema, + color: "#7DA13E", + }); + expect(concept.literal_content).toEqual({ + label: "Some concept", + color: "#7DA13E", + }); + }); + it("writes the label into literal_content as well as the name", () => { const concept = crossAppNodeSchemaToDbConcept(baseSchema); expect(concept.name).toBe("Some concept"); @@ -83,6 +95,36 @@ describe("crossAppNodeSchemaToDbConcept", () => { }); }); +describe("crossAppRelationTypeSchemaToDbConcept", () => { + const relationType = { + ...baseSchema, + label: "supports", + complement: "is supported by", + }; + + it("maps color to literal_content.color", () => { + expect( + crossAppRelationTypeSchemaToDbConcept({ ...relationType, color: "green" }) + .literal_content, + ).toStrictEqual({ + roles: ["source", "destination"], + label: "supports", + complement: "is supported by", + color: "green", + }); + }); + + it("leaves color out of literal_content when the relation type has none", () => { + expect( + crossAppRelationTypeSchemaToDbConcept(relationType).literal_content, + ).toStrictEqual({ + roles: ["source", "destination"], + label: "supports", + complement: "is supported by", + }); + }); +}); + describe("crossAppNodeToDbConcept", () => { it("stores node slots as local reference content", () => { expect( diff --git a/packages/database/src/lib/__tests__/dbToCrossAppConverters.test.ts b/packages/database/src/lib/__tests__/dbToCrossAppConverters.test.ts index e27f998d8..3fd9b6f55 100644 --- a/packages/database/src/lib/__tests__/dbToCrossAppConverters.test.ts +++ b/packages/database/src/lib/__tests__/dbToCrossAppConverters.test.ts @@ -93,6 +93,20 @@ describe("dbNodeSchemaToCrossApp", () => { expect(result.metadata).toEqual({ extra: "kept" }); }); + it("splits color out of metadata onto the result", () => { + const schema = baseConcept({ + literal_content: { color: "#7DA13E", extra: "kept" }, + }); + const result = dbNodeSchemaToCrossApp({ + schema, + spaceMap, + accountMap, + schemaMap: {}, + }); + expect(result.color).toBe("#7DA13E"); + expect(result.metadata).toEqual({ extra: "kept" }); + }); + it("keeps format undefined when literal_content has no format", () => { const schema = baseConcept({ literal_content: { extra: "kept" }, @@ -198,6 +212,21 @@ describe("dbRelationTypeSchemaToCrossApp", () => { authorId: "account-local-1", }); }); + + it("splits color out of metadata onto the result", () => { + const schema = baseConcept({ + literal_content: { + roles: ["source", "destination"], + label: "supports", + complement: "is supported by", + color: "green", + extra: "kept", + }, + }); + const result = dbRelationTypeSchemaToCrossApp(schema, spaceMap, accountMap); + expect(result.color).toBe("green"); + expect(result.metadata).toEqual({ extra: "kept" }); + }); }); describe("dbRelationTripleSchemaToCrossApp", () => { diff --git a/packages/database/src/lib/crossAppConverters.ts b/packages/database/src/lib/crossAppConverters.ts index 88416c5c1..d190e2db7 100644 --- a/packages/database/src/lib/crossAppConverters.ts +++ b/packages/database/src/lib/crossAppConverters.ts @@ -109,6 +109,7 @@ export const crossAppNodeSchemaToDbConcept = ( template: node.templateTitle, template_content: node.template, format: node.format, + color: node.color, roles: slots.length > 0 ? slots : undefined, }); const referenceContent = slots.length ? node.slotDefinitions : undefined; @@ -141,11 +142,12 @@ export const crossAppRelationTypeSchemaToDbConcept = ( name: node.label, author_local_id: node.authorId, is_schema: true, - literal_content: { + literal_content: filterUndefined({ roles: ["source", "destination"], label: node.label, complement: node.complement, - }, + color: node.color, + }), created: node.createdAt?.toISOString(), last_modified: node.modifiedAt?.toISOString(), }); diff --git a/packages/database/src/lib/dbToCrossAppConverters.ts b/packages/database/src/lib/dbToCrossAppConverters.ts index da0105f6b..7f7fe2fa1 100644 --- a/packages/database/src/lib/dbToCrossAppConverters.ts +++ b/packages/database/src/lib/dbToCrossAppConverters.ts @@ -168,7 +168,7 @@ export const dbNodeSchemaToCrossApp = ({ string, number >; - const { template, template_content, roles, format, ...other } = + const { template, template_content, roles, format, color, ...other } = schema.literal_content as Record; const authorId = accountMap[schema.author_id || 0]; if (authorId === undefined) throw new Error("Missing author"); @@ -195,6 +195,7 @@ export const dbNodeSchemaToCrossApp = ({ template: template_content as string | undefined, templateTitle: template as string | undefined, format: format as string | undefined, + color: color as string | undefined, authorId, slotDefinitions, }; @@ -409,7 +410,7 @@ export const dbRelationTypeSchemaToCrossApp = ( accountMap: Record, ): CrossAppRelationTypeSchema => { // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { roles, label, complement, ...other } = + const { roles, label, complement, color, ...other } = schema.literal_content as Record; const authorId = accountMap[schema.author_id || 0]; if (authorId === undefined) throw new Error("Missing author"); @@ -428,6 +429,7 @@ export const dbRelationTypeSchemaToCrossApp = ( metadata: other, label: label as string, complement: complement as string, + color: color as string | undefined, authorId, }; };