@@ -12,6 +12,9 @@ import internalError from "~/utils/internalError";
1212import { getSetting } from "~/utils/extensionSettings" ;
1313import { getFormattedConfigTree } from "~/utils/discourseConfigRef" ;
1414import { roamNodeToCondition } from "~/utils/parseQuery" ;
15+ import type { DiscourseRelation } from "~/utils/getDiscourseRelations" ;
16+ import type { DiscourseNode } from "~/utils/getDiscourseNodes" ;
17+ import type { Condition } from "~/utils/types" ;
1518import { z } from "zod" ;
1619
1720import {
@@ -27,7 +30,7 @@ import {
2730 type GlobalSettings ,
2831 type PersonalSettings ,
2932 type DiscourseNodeSettings ,
30- type DiscourseRelationSettings ,
33+ type Condition as SchemaCondition ,
3134} from "./zodSchema" ;
3235
3336const isRecord = ( value : unknown ) : value is Record < string , unknown > =>
@@ -250,11 +253,6 @@ const getLegacyPersonalSetting = (keys: string[]): unknown => {
250253 return undefined ;
251254} ;
252255
253- // NOTE(ENG-1469): This returns the block props schema shape (Record<uid, {label, source,
254- // destination, complement, ifConditions }>). Runtime consumers use getDiscourseRelations()
255- // which returns a flat DiscourseRelation[] with a different structure (one entry per
256- // if-block, triples at top level, no nodePositions). When migrating getDiscourseRelations()
257- // to read from block props, it will need a conversion from this shape to the flat array.
258256const getLegacyRelationsSetting = ( ) : Record < string , unknown > => {
259257 const settingsUid = getPageUidByPageTitle ( DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ) ;
260258 if ( ! settingsUid ) return DEFAULT_GLOBAL_SETTINGS . Relations ;
@@ -766,13 +764,19 @@ export const setGlobalSetting = (keys: string[], value: json): void => {
766764 } ) ;
767765} ;
768766
769- export const getAllRelations = ( ) : DiscourseRelationSettings [ ] => {
767+ export const getAllRelations = ( ) : DiscourseRelation [ ] => {
770768 const settings = getGlobalSettings ( ) ;
771769
772- return Object . entries ( settings . Relations ) . map ( ( [ id , relation ] ) => ( {
773- ...relation ,
774- id,
775- } ) ) ;
770+ return Object . entries ( settings . Relations ) . flatMap ( ( [ id , relation ] ) =>
771+ relation . ifConditions . map ( ( ifCondition ) => ( {
772+ id,
773+ label : relation . label ,
774+ source : relation . source ,
775+ destination : relation . destination ,
776+ complement : relation . complement ,
777+ triples : ifCondition . triples ,
778+ } ) ) ,
779+ ) ;
776780} ;
777781
778782export const getPersonalSettings = ( ) : PersonalSettings => {
@@ -936,7 +940,52 @@ export const setDiscourseNodeSetting = (
936940 setBlockPropAtPath ( pageUid , keys , value ) ;
937941} ;
938942
939- export const getAllDiscourseNodes = ( ) : DiscourseNodeSettings [ ] => {
943+ const addConditionUids = ( conditions : SchemaCondition [ ] ) : Condition [ ] =>
944+ conditions . map ( ( c ) => {
945+ const uid = window . roamAlphaAPI . util . generateUID ( ) ;
946+ if ( c . type === "or" || c . type === "not or" ) {
947+ return {
948+ uid,
949+ type : c . type ,
950+ conditions : c . conditions . map ( addConditionUids ) ,
951+ } ;
952+ }
953+ return {
954+ uid,
955+ type : c . type ,
956+ source : c . source ,
957+ relation : c . relation ,
958+ target : c . target ,
959+ not : c . not ,
960+ } ;
961+ } ) as Condition [ ] ;
962+
963+ const toDiscourseNode = ( settings : DiscourseNodeSettings ) : DiscourseNode => ( {
964+ text : settings . text ,
965+ type : settings . type ,
966+ shortcut : settings . shortcut ,
967+ tag : settings . tag || undefined ,
968+ format : settings . format ,
969+ description : settings . description || undefined ,
970+ graphOverview : settings . graphOverview || undefined ,
971+ backedBy : settings . backedBy ,
972+ specification : addConditionUids (
973+ settings . specification . query . conditions as SchemaCondition [ ] ,
974+ ) ,
975+ canvasSettings : Object . fromEntries (
976+ Object . entries ( settings . canvasSettings ) . map ( ( [ k , v ] ) => [
977+ k ,
978+ typeof v === "boolean" ? ( v ? "true" : "" ) : String ( v ) ,
979+ ] ) ,
980+ ) ,
981+ template : settings . template . length > 0 ? settings . template : undefined ,
982+ embeddingRef : settings . suggestiveRules . embeddingRef || undefined ,
983+ isFirstChild : settings . suggestiveRules . isFirstChild
984+ ? { uid : "" , value : true }
985+ : undefined ,
986+ } ) ;
987+
988+ export const getAllDiscourseNodes = ( ) : DiscourseNode [ ] => {
940989 const results = window . roamAlphaAPI . data . fast . q ( `
941990 [:find ?uid ?title (pull ?page [:block/props])
942991 :where
@@ -945,7 +994,7 @@ export const getAllDiscourseNodes = (): DiscourseNodeSettings[] => {
945994 [(clojure.string/starts-with? ?title "${ DISCOURSE_NODE_PAGE_PREFIX } ")]]
946995 ` ) as [ string , string , Record < string , json > | null ] [ ] ;
947996
948- const nodes : DiscourseNodeSettings [ ] = [ ] ;
997+ const nodes : DiscourseNode [ ] = [ ] ;
949998
950999 for ( const [ pageUid , title , rawProps ] of results ) {
9511000 if ( typeof pageUid !== "string" || typeof title !== "string" ) continue ;
@@ -962,11 +1011,13 @@ export const getAllDiscourseNodes = (): DiscourseNodeSettings[] => {
9621011
9631012 const result = DiscourseNodeSchema . safeParse ( blockProps ) ;
9641013 if ( result . success ) {
965- nodes . push ( {
966- ...result . data ,
967- type : pageUid ,
968- text : title . replace ( DISCOURSE_NODE_PAGE_PREFIX , "" ) ,
969- } ) ;
1014+ nodes . push (
1015+ toDiscourseNode ( {
1016+ ...result . data ,
1017+ type : pageUid ,
1018+ text : title . replace ( DISCOURSE_NODE_PAGE_PREFIX , "" ) ,
1019+ } ) ,
1020+ ) ;
9701021 } else {
9711022 internalError ( {
9721023 error : result . error ,
0 commit comments