-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathzodSchema.ts
More file actions
323 lines (289 loc) · 10.3 KB
/
zodSchema.ts
File metadata and controls
323 lines (289 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { z } from "zod";
import DEFAULT_RELATIONS_BLOCK_PROPS from "~/components/settings/data/defaultRelationsBlockProps";
/* eslint-disable @typescript-eslint/naming-convention */
export const CanvasSettingsSchema = z.object({
color: z.string().default(""),
alias: z.string().default(""),
"key-image": z.boolean().default(false),
"key-image-option": z
.enum(["first-image", "query-builder"])
.default("first-image"),
"query-builder-alias": z.string().default(""),
});
export const AttributesSchema = z.record(z.string(), z.string()).default({});
const QBClauseDataSchema = z.object({
source: z.string(),
relation: z.string(),
target: z.string(),
not: z.boolean().optional(),
});
type Condition =
| (z.infer<typeof QBClauseDataSchema> & { type: "clause" })
| (z.infer<typeof QBClauseDataSchema> & { type: "not" })
| { type: "or"; conditions: Condition[][] }
| { type: "not or"; conditions: Condition[][] };
export const ConditionSchema: z.ZodType<Condition> = z.discriminatedUnion(
"type",
[
QBClauseDataSchema.extend({ type: z.literal("clause") }),
QBClauseDataSchema.extend({ type: z.literal("not") }),
z.object({
type: z.literal("or"),
conditions: z.lazy(() => ConditionSchema.array().array()),
}),
z.object({
type: z.literal("not or"),
conditions: z.lazy(() => ConditionSchema.array().array()),
}),
],
);
export const SelectionSchema = z.object({
text: z.string(),
label: z.string(),
});
export const IndexSchema = z.object({
conditions: z.array(ConditionSchema).default([]),
selections: z.array(SelectionSchema).default([]),
custom: z.string().default(""),
returnNode: z.string().default("node"),
});
type RoamNode = {
text: string;
children?: RoamNode[];
uid?: string;
heading?: 0 | 1 | 2 | 3;
open?: boolean;
};
export const RoamNodeSchema: z.ZodType<RoamNode> = z.lazy(() =>
z.object({
text: z.string(),
children: RoamNodeSchema.array().optional(),
uid: z.string().optional(),
heading: z
.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)])
.optional(),
open: z.boolean().optional(),
}),
);
export const SuggestiveRulesSchema = z.object({
embeddingRef: z.string().default(""),
isFirstChild: z.boolean().default(false),
});
const stringWithDefault = (defaultVal: string) =>
z
.string()
.nullable()
.optional()
.transform((val) => val ?? defaultVal);
const booleanWithDefault = (defaultVal: boolean) =>
z
.boolean()
.nullable()
.optional()
.transform((val) => val ?? defaultVal);
export const DiscourseNodeSchema = z.object({
text: z.string(),
type: z.string(),
format: stringWithDefault(""),
shortcut: stringWithDefault(""),
tag: stringWithDefault(""),
description: stringWithDefault(""),
specification: z
.object({
enabled: z.boolean().default(false),
query: IndexSchema.default({}),
})
.nullable()
.optional()
.transform(
(val) =>
val ?? {
enabled: false,
query: { conditions: [], selections: [], custom: "", returnNode: "" },
},
),
template: z
.array(RoamNodeSchema)
.nullable()
.optional()
.transform((val) => val ?? []),
canvasSettings: CanvasSettingsSchema.default({}),
graphOverview: booleanWithDefault(false),
attributes: z
.record(z.string(), z.string())
.nullable()
.optional()
.transform((val) => val ?? {}),
overlay: stringWithDefault(""),
index: IndexSchema.nullable().optional(),
suggestiveRules: SuggestiveRulesSchema.default({}),
backedBy: z
.enum(["user", "default", "relation"])
.nullable()
.transform((val) => val ?? "user"),
});
export const TripleSchema = z.tuple([z.string(), z.string(), z.string()]);
export const RelationConditionSchema = z.object({
triples: z.array(TripleSchema).default([]),
nodePositions: z.record(z.string(), z.string()).default({}),
});
export const DiscourseRelationSchema = z.object({
id: z.string().optional(),
label: z.string(),
source: z.string(),
destination: z.string(),
complement: z.string().default(""),
ifConditions: z.array(RelationConditionSchema).default([]),
});
export const FeatureFlagsSchema = z.object({
"Enable left sidebar": z.boolean().default(false),
"Suggestive mode enabled": z.boolean().default(false),
"Duplicate node alert enabled": z.boolean().default(false),
});
export const ExportSettingsSchema = z.object({
"Remove special characters": z.boolean().default(false),
"Resolve block references": z.boolean().default(false),
"Resolve block embeds": z.boolean().default(false),
"Append referenced node": z.boolean().default(false),
"Link type": z.enum(["alias", "wikilinks", "roam url"]).default("alias"),
"Max filename length": z.number().default(64),
Frontmatter: z.array(z.string()).default([]),
});
export const PageGroupSchema = z.object({
name: z.string(),
pages: z.array(z.string()).default([]),
});
export const SuggestiveModeGlobalSettingsSchema = z.object({
"Include current page relations": z.boolean().default(false),
"Include parent and child blocks": z.boolean().default(false),
"Page groups": z.array(PageGroupSchema).default([]),
});
export const LeftSidebarGlobalSettingsSchema = z.object({
Children: z.array(z.string()).default([]),
Settings: z
.object({
Collapsable: z.boolean().default(false),
Folded: z.boolean().default(false),
})
.default({}),
});
export const GlobalSettingsSchema = z.object({
Trigger: z.string().default("\\"),
"Canvas page format": z.string().default("Canvas/*"),
"Left sidebar": LeftSidebarGlobalSettingsSchema.default({}),
Export: ExportSettingsSchema.default({}),
"Suggestive mode": SuggestiveModeGlobalSettingsSchema.default({}),
Relations: z
.record(z.string(), DiscourseRelationSchema)
.default(DEFAULT_RELATIONS_BLOCK_PROPS),
});
export const PersonalSectionSchema = z.object({
name: z.string(),
Children: z
.array(
z.object({
uid: z.string(),
Alias: z.string().default(""),
}),
)
.default([]),
Settings: z
.object({
"Truncate-result?": z.number().default(75),
Folded: z.boolean().default(false),
})
.default({}),
});
export const LeftSidebarPersonalSettingsSchema = z
.array(PersonalSectionSchema)
.default([]);
export const StoredFiltersSchema = z.object({
includes: z.object({ values: z.array(z.string()).default([]) }).default({}),
excludes: z.object({ values: z.array(z.string()).default([]) }).default({}),
});
export const QuerySettingsSchema = z.object({
"Hide query metadata": z.boolean().default(false),
"Default page size": z.number().default(10),
"Query pages": z.array(z.string()).default([]),
"Default filters": z.record(z.string(), StoredFiltersSchema).default({}),
});
export const PersonalSettingsSchema = z.object({
"Left sidebar": LeftSidebarPersonalSettingsSchema,
"Personal node menu trigger": z
.union([
z.object({ modifiers: z.number(), key: z.string() }),
z.literal(""),
])
.default({ modifiers: 0, key: "" }),
"Node search menu trigger": z.string().default("@"),
"Discourse tool shortcut": z
.object({ modifiers: z.number(), key: z.string() })
.default({ modifiers: 0, key: "" }),
"Discourse context overlay": z.boolean().default(false),
"Reified relation triples": z.boolean().default(false),
"Suggestive mode overlay": z.boolean().default(false),
"Overlay in canvas": z.boolean().default(false),
"Text selection popup": z.boolean().default(true),
"Disable sidebar open": z.boolean().default(false),
"Page preview": z.boolean().default(false),
"Hide feedback button": z.boolean().default(false),
"Streamline styling": z.boolean().default(false),
"Auto canvas relations": z.boolean().default(false),
"Disable product diagnostics": z.boolean().default(false),
"Canvas node shortcuts": z.record(z.string(), z.string()).default({}),
Query: QuerySettingsSchema.default({}),
});
export const GithubSettingsSchema = z.object({
"oauth-github": z.string().optional(),
"selected-repo": z.string().optional(),
});
let cachedPersonalSettingsKey: string | null = null;
export const getPersonalSettingsKey = (): string => {
if (cachedPersonalSettingsKey !== null) return cachedPersonalSettingsKey;
cachedPersonalSettingsKey = window.roamAlphaAPI.user.uid() || "";
return cachedPersonalSettingsKey;
};
export const STATIC_TOP_LEVEL_ENTRIES = {
featureFlags: {
key: "Feature Flags",
schema: FeatureFlagsSchema,
},
global: {
key: "Global",
schema: GlobalSettingsSchema,
},
} as const;
export const getTopLevelBlockPropsConfig = () => [
...Object.values(STATIC_TOP_LEVEL_ENTRIES),
{ key: getPersonalSettingsKey(), schema: PersonalSettingsSchema },
];
export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE = "roam/js/discourse-graph";
export const DISCOURSE_NODE_PAGE_PREFIX = "discourse-graph/nodes/";
export type CanvasSettings = z.infer<typeof CanvasSettingsSchema>;
export type SuggestiveRules = z.infer<typeof SuggestiveRulesSchema>;
export type DiscourseNodeSettings = z.infer<typeof DiscourseNodeSchema>;
export type Triple = z.infer<typeof TripleSchema>;
export type RelationCondition = z.infer<typeof RelationConditionSchema>;
export type DiscourseRelationSettings = z.infer<typeof DiscourseRelationSchema>;
export type FeatureFlags = z.infer<typeof FeatureFlagsSchema>;
export type ExportSettings = z.infer<typeof ExportSettingsSchema>;
export type PageGroup = z.infer<typeof PageGroupSchema>;
export type SuggestiveModeGlobalSettings = z.infer<
typeof SuggestiveModeGlobalSettingsSchema
>;
export type LeftSidebarGlobalSettings = z.infer<
typeof LeftSidebarGlobalSettingsSchema
>;
export type GlobalSettings = z.infer<typeof GlobalSettingsSchema>;
export type PersonalSection = z.infer<typeof PersonalSectionSchema>;
export type LeftSidebarPersonalSettings = z.infer<
typeof LeftSidebarPersonalSettingsSchema
>;
export type StoredFilters = z.infer<typeof StoredFiltersSchema>;
export type QuerySettings = z.infer<typeof QuerySettingsSchema>;
export type PersonalSettings = z.infer<typeof PersonalSettingsSchema>;
export type GithubSettings = z.infer<typeof GithubSettingsSchema>;
export type QueryCondition = z.infer<typeof ConditionSchema>;
export type QuerySelection = z.infer<typeof SelectionSchema>;
export type RoamNodeType = z.infer<typeof RoamNodeSchema>;
export type Index = z.infer<typeof IndexSchema>;