sveltekit-ui 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/Components/ContentInput/index.svelte.js +1 -0
  2. package/dist/Components/Location/index.svelte +5 -11
  3. package/dist/Components/Location/index.svelte.js +9 -13
  4. package/dist/Components/LocationInput/index.svelte +5 -10
  5. package/dist/Components/LocationInput/index.svelte.js +16 -6
  6. package/dist/Components/TextInput/index.svelte.js +1 -0
  7. package/dist/client/types/index.js +78 -2238
  8. package/package.json +11 -8
  9. package/src/app.html +1 -0
  10. package/src/lib/Components/ContentInput/index.svelte.js +1 -0
  11. package/src/lib/Components/Location/index.svelte +5 -11
  12. package/src/lib/Components/Location/index.svelte.js +9 -13
  13. package/src/lib/Components/LocationInput/index.svelte +5 -10
  14. package/src/lib/Components/LocationInput/index.svelte.js +16 -6
  15. package/src/lib/Components/TextInput/index.svelte.js +1 -0
  16. package/src/lib/client/types/index.js +78 -2238
  17. package/src/routes/[component]/+page.svelte +3 -3
  18. package/svelte.config.js +1 -1
  19. package/dist/Components/FunctionInput/Node/helpers/DefinedTargetInput/index.svelte +0 -110
  20. package/dist/Components/FunctionInput/Node/helpers/Field/index.svelte +0 -109
  21. package/dist/Components/FunctionInput/Node/helpers/Field/index.svelte.js +0 -207
  22. package/dist/Components/FunctionInput/Node/helpers/InlineDropArea/index.svelte +0 -97
  23. package/dist/Components/FunctionInput/Node/helpers/InlineDropArea/index.svelte.js +0 -105
  24. package/dist/Components/FunctionInput/Node/helpers/ObjectInput/index.svelte +0 -198
  25. package/dist/Components/FunctionInput/Node/helpers/StackArea/index.svelte +0 -169
  26. package/dist/Components/FunctionInput/Node/helpers/StackArea/index.svelte.js +0 -152
  27. package/dist/Components/FunctionInput/Node/index.svelte +0 -144
  28. package/dist/Components/FunctionInput/Node/index.svelte.js +0 -134
  29. package/dist/Components/FunctionInput/index.svelte +0 -356
  30. package/dist/Components/FunctionInput/index.svelte.js +0 -1215
  31. package/src/lib/Components/FunctionInput/Node/helpers/DefinedTargetInput/index.svelte +0 -110
  32. package/src/lib/Components/FunctionInput/Node/helpers/Field/index.svelte +0 -109
  33. package/src/lib/Components/FunctionInput/Node/helpers/Field/index.svelte.js +0 -207
  34. package/src/lib/Components/FunctionInput/Node/helpers/InlineDropArea/index.svelte +0 -97
  35. package/src/lib/Components/FunctionInput/Node/helpers/InlineDropArea/index.svelte.js +0 -105
  36. package/src/lib/Components/FunctionInput/Node/helpers/ObjectInput/index.svelte +0 -198
  37. package/src/lib/Components/FunctionInput/Node/helpers/StackArea/index.svelte +0 -169
  38. package/src/lib/Components/FunctionInput/Node/helpers/StackArea/index.svelte.js +0 -152
  39. package/src/lib/Components/FunctionInput/Node/index.svelte +0 -144
  40. package/src/lib/Components/FunctionInput/Node/index.svelte.js +0 -134
  41. package/src/lib/Components/FunctionInput/index.svelte +0 -356
  42. package/src/lib/Components/FunctionInput/index.svelte.js +0 -1215
  43. package/src/routes/[component]/Showcase/FunctionInput/index.svelte +0 -77
@@ -1,1215 +0,0 @@
1
- import { create_unique_id, deep_copy, node_types, function_types, node_categories } from "$lib/client/index.js"
2
- import { create_button_manager } from "$lib/Components/Button/index.svelte.js"
3
- import { create_icon_manager } from "$lib/Components/Icon/index.svelte.js"
4
- import { create_node_manager } from "$lib/Components/FunctionInput/Node/index.svelte.js"
5
- import { browser } from "$app/environment"
6
-
7
- export function create_function_manager(config) {
8
- let init_function = $state(null)
9
- let id = $state(null)
10
- let name = $state(null)
11
- let description = $state(null)
12
- let db_epoch_updated = $state(null)
13
- let db_epoch_created = $state(null)
14
- let versions = $state([])
15
- let selected_version_index = $state(0)
16
- let error_message = $state(null)
17
- let console_logs = $state(null)
18
- let console_result = $state(null)
19
- let node_path = $state([])
20
- let selected_category = $state("main")
21
- let undo_version_button_manager = $state(null)
22
- let redo_version_button_manager = $state(null)
23
- let node_category_buttons = $state(null)
24
- let dragging_node = $state(null)
25
- let root_node_manager = $state(null)
26
- let trashcan_icon_manager = $state(null)
27
-
28
- let node_toolbox_items_prepped = $state(null)
29
-
30
- let selected_version = $derived(versions?.[selected_version_index])
31
-
32
- function set_node_toolbox_items_prepped() {
33
- let node_toolbox_items_loc = []
34
- if (Object.keys(function_types || {}).length > 0) {
35
- console.log("function_types", function_types)
36
- for (let node_type of Object.keys(function_types || {})) {
37
- console.log("node_typeddd", node_type, function_types?.[node_type])
38
- node_toolbox_items_loc.push({
39
- category: function_types?.[node_type]?.category,
40
- node_manager: create_node_manager({
41
- is_clickable_only: false,
42
- is_preview: true,
43
- node_type: node_type,
44
- node_types: function_types,
45
- node_path,
46
- node_categories,
47
- c: 12,
48
- drag_or_select_node: drag_or_select_node,
49
- handle_node_drag_start: () =>
50
- handle_node_drag_start({
51
- node_path: node_path,
52
- node_type: node_type,
53
- }),
54
- handle_node_drag_end: () =>
55
- handle_node_drag_end({
56
- node_path: node_path,
57
- node_type: node_type,
58
- }),
59
- on_set_node_value: (input) => set_node_value(input),
60
- }),
61
- })
62
- }
63
- }
64
- console.log("node_toolbox_items_loc", node_toolbox_items_loc)
65
- node_toolbox_items_prepped = node_toolbox_items_loc
66
- }
67
-
68
- function get_node_category_buttons() {
69
- let sorted_node_categories_array = Object.keys(node_categories)
70
- .sort((a, b) => node_categories[a].display_order - node_categories[b].display_order)
71
- .map((key) => ({
72
- id: key,
73
- h: node_categories[key].h,
74
- display_order: node_categories[key].display_order,
75
- }))
76
- let node_category_buttons_loc = []
77
- if (Array.isArray(sorted_node_categories_array) && sorted_node_categories_array.length > 0) {
78
- for (let node_category of sorted_node_categories_array) {
79
- const is_selected = selected_category == node_category?.id
80
- node_category_buttons_loc.push({
81
- is_selected: is_selected,
82
- button_manager: create_button_manager({
83
- min_height: 5,
84
- text: node_category?.id,
85
- h: node_category?.h,
86
- c: 10,
87
- l: 16,
88
- selected_type: () => (is_selected ? "selected" : null),
89
- on_click: () => (selected_category = node_category?.id),
90
- }),
91
- })
92
- }
93
- }
94
- return node_category_buttons_loc
95
- }
96
-
97
- function undo() {
98
- if (versions.length - selected_version_index > 1) {
99
- selected_version_index += 1
100
- }
101
- }
102
-
103
- function redo() {
104
- if (versions.length > 0) {
105
- selected_version_index -= 1
106
- }
107
- }
108
-
109
- function transition(action) {
110
- if (!browser.startViewTransition) {
111
- return
112
- }
113
- document.startViewTransition(action)
114
- }
115
-
116
- function handle_node_drag_start(node_data) {
117
- transition(() => (dragging_node = node_data))
118
- }
119
-
120
- function handle_node_drag_end() {
121
- transition(() => (dragging_node = null))
122
- }
123
-
124
- function get_defined_at_path(node_path) {
125
- let structure_loc = deep_copy(selected_version)
126
- let res = get_defined_at_path_traverse(structure_loc, node_path)
127
- console.log("get_defined_at_path_res", res)
128
- return res?.defined
129
- }
130
-
131
- function get_defined_at_path_traverse(node, seek_until_node_path, iter_node_path = []) {
132
- let defined = {}
133
- let is_path_found = false
134
- if (!Array.isArray(seek_until_node_path) || !Array.isArray(iter_node_path)) {
135
- is_path_found = true
136
- } else if (seek_until_node_path.length <= iter_node_path.length) {
137
- for (let i = 0; i < iter_node_path.length; i++) {
138
- if (typeof iter_node_path[i] == "number") {
139
- if (iter_node_path[i] >= seek_until_node_path?.[i]) {
140
- is_path_found = true
141
- break
142
- }
143
- } else if (iter_node_path[i] != seek_until_node_path?.[i]) {
144
- break
145
- } else if (i == seek_until_node_path.length - 1) {
146
- is_path_found = true
147
- break
148
- }
149
- }
150
- }
151
- if (!node || !Array.isArray(seek_until_node_path) || seek_until_node_path.length == 0) {
152
- return { defined, is_path_found }
153
- }
154
- let full_node_type = function_types?.[node?.type]
155
- // should add the other define things like in loop or sort or use node kind to find definitions
156
- // maybe also if im showing type in ui then assignments should update type but prob too difficult to predit with if else stuff
157
- if (node?.type == "define") {
158
- if (node?.value?.identifier?.type == "identifier_literal" && node?.value?.identifier?.value) {
159
- let cleaned_identifier = node?.value?.identifier?.value.toLowerCase()
160
- cleaned_identifier = cleaned_identifier.replace(/[^a-z0-9]/g, "_")
161
- cleaned_identifier = cleaned_identifier.replace(/_{2,}/g, "_")
162
- cleaned_identifier = cleaned_identifier.replace(/^_+|_+$/g, "")
163
- defined[cleaned_identifier] = {
164
- path: iter_node_path,
165
- type: node?.value?.value?.type,
166
- }
167
- }
168
- } else if (node?.type == "stack_literal") {
169
- if (Array.isArray(node?.value)) {
170
- for (let i = 0; i < node?.value.length; i++) {
171
- let res = get_defined_at_path_traverse(node?.value?.[i], seek_until_node_path, [...iter_node_path, i])
172
- defined = { ...defined, ...res?.defined }
173
- if (res?.is_path_found) {
174
- is_path_found = true
175
- break
176
- }
177
- }
178
- }
179
- } else if (full_node_type?.kind != "literal" || Object.keys(full_node_type?.fields || {}).length) {
180
- for (let key of Object.keys(full_node_type?.fields || {})) {
181
- if (node?.value) {
182
- let res = get_defined_at_path_traverse(node?.value?.[key], seek_until_node_path, [...iter_node_path, key])
183
- defined = { ...defined, ...res?.defined }
184
- if (res?.is_path_found) {
185
- is_path_found = true
186
- break
187
- }
188
- }
189
- }
190
- }
191
- return { defined, is_path_found }
192
- }
193
-
194
- function get_node_at_path(iter_node, remaining_node_path) {
195
- if (!Array.isArray(remaining_node_path) || remaining_node_path.length == 0) {
196
- return iter_node
197
- }
198
- let [first, ...rest_of_node_path] = remaining_node_path
199
- return get_node_at_path(iter_node?.value?.[first], rest_of_node_path)
200
- }
201
-
202
- function remove_node(iter_node, remaining_node_path) {
203
- if (!Array.isArray(remaining_node_path) || remaining_node_path.length == 0) {
204
- return null
205
- }
206
- let [first, ...rest_of_node_path] = remaining_node_path
207
- if (remaining_node_path.length == 1 && typeof first == "number" && Array.isArray(iter_node?.value)) {
208
- iter_node.value = [...iter_node.value].filter((_, index) => index !== first)
209
- } else {
210
- iter_node.value[first] = remove_node(iter_node?.value?.[first], rest_of_node_path)
211
- }
212
- return iter_node
213
- }
214
-
215
- function get_default_node_value(node_type) {
216
- let default_node = null
217
- if (node_type && Object.keys(function_types?.[node_type]?.fields || {}).length > 0) {
218
- default_node = {
219
- type: node_type,
220
- value: {},
221
- }
222
- for (let [key, value] of Object.entries(function_types?.[node_type]?.fields)) {
223
- default_node.value[key] = null
224
- if (value?.forced_node) {
225
- default_node.value[key] = get_default_node_value(value?.forced_node) // careful about triggering infinate recursion here
226
- }
227
- }
228
- } else if (function_types?.[node_type]?.kind == "literal") {
229
- default_node = {
230
- type: node_type,
231
- value: function_types?.[node_type]?.value ?? null,
232
- }
233
- }
234
- return default_node
235
- }
236
-
237
- function set_value_at_node_path(iter_node, remaining_node_path, value = null, is_insert = false) {
238
- if (!iter_node || !Array.isArray(remaining_node_path) || remaining_node_path.length == 0) {
239
- return value
240
- }
241
- let [first, ...rest_of_node_path] = remaining_node_path
242
- if (remaining_node_path.length == 1) {
243
- if (typeof first == "number") {
244
- if (!Array.isArray(iter_node?.value)) {
245
- iter_node.value = []
246
- }
247
- if (is_insert) {
248
- iter_node.value.splice(first, 0, value)
249
- } else {
250
- iter_node.value[first].value = value
251
- }
252
- } else {
253
- console.log("ggg1", deep_copy({ iter_node, first, value }))
254
- iter_node.value[first].value = value
255
- }
256
- } else {
257
- let temp = set_value_at_node_path(iter_node?.value?.[first], rest_of_node_path, value, is_insert)
258
- iter_node.value[first] = temp
259
- }
260
- return iter_node
261
- }
262
-
263
- function set_node_at_node_path(iter_node, remaining_node_path, node_to_add = null, is_insert = false) {
264
- console.log("set_node_at_node_path", { iter_node, remaining_node_path, node_to_add, is_insert })
265
- if (!iter_node) {
266
- return null
267
- } else if (!Array.isArray(remaining_node_path) || remaining_node_path.length == 0) {
268
- return node_to_add
269
- }
270
- let [first, ...rest_of_node_path] = remaining_node_path
271
- if (remaining_node_path.length == 1) {
272
- if (typeof first == "number") {
273
- if (!Array.isArray(iter_node?.value)) {
274
- iter_node.value = []
275
- }
276
- if (is_insert) {
277
- iter_node.value.splice(first, 0, node_to_add)
278
- } else {
279
- iter_node.value[first] = node_to_add
280
- }
281
- } else {
282
- iter_node.value[first] = node_to_add
283
- }
284
- } else {
285
- let temp = set_node_at_node_path(iter_node?.value?.[first], rest_of_node_path, node_to_add, is_insert)
286
- iter_node.value[first] = temp
287
- }
288
- return iter_node
289
- }
290
-
291
- function set_node_value(node_path, value) {
292
- let structure_loc = deep_copy(selected_version)
293
- structure_loc = set_value_at_node_path(structure_loc, node_path, value)
294
- versions = [structure_loc, ...versions.slice(selected_version_index)]
295
- }
296
-
297
- function set_root_node_prepped() {
298
- console.log("set_root_node_prepped")
299
- let node_value = selected_version?.value
300
- let node_type = selected_version?.type
301
- let node_path = []
302
- root_node_manager = create_node_manager({
303
- node_type: node_type,
304
- node_path: node_path,
305
- node_value: node_value,
306
- is_clickable_only: false,
307
- is_preview: true,
308
- node_types: function_types,
309
- node_categories,
310
- c: 12,
311
- handle_node_drag_start: () =>
312
- handle_node_drag_start({
313
- node_path: node_path,
314
- node_type: node_type,
315
- }),
316
- handle_node_drag_end: () =>
317
- handle_node_drag_end({
318
- node_path: node_path,
319
- node_type: node_type,
320
- }),
321
- on_set_node_value: (input) => set_node_value(input),
322
- drag_or_select_node: drag_or_select_node,
323
- })
324
- }
325
-
326
- function drag_or_select_node(
327
- drop_area_node_path,
328
- drop_area_node_type,
329
- is_click = false,
330
- clicked_node_type = null,
331
- is_trash = false
332
- ) {
333
- console.log("drag_or_select_node", {
334
- drop_area_node_path,
335
- drop_area_node_type,
336
- is_click,
337
- clicked_node_type,
338
- is_trash,
339
- })
340
- let node_to_insert = dragging_node
341
- error_message = null
342
- let structure_loc = deep_copy(selected_version)
343
- if (is_trash) {
344
- structure_loc = remove_node(structure_loc, node_to_insert?.node_path)
345
- console.log("post_rem", deep_copy(structure_loc))
346
- } else {
347
- if (is_click) {
348
- node_to_insert = { node_type: clicked_node_type }
349
- }
350
- let drop_area_node_options = []
351
- if (drop_area_node_type == "stack_literal") {
352
- drop_area_node_options = function_types?.stack_literal?.child_node_options
353
- } else {
354
- drop_area_node_options =
355
- function_types?.[drop_area_node_type]?.fields?.[
356
- Array.isArray(drop_area_node_path) ? drop_area_node_path[drop_area_node_path.length - 1] : -1
357
- ]?.node_options
358
- }
359
- if (!Array.isArray(drop_area_node_options) || !drop_area_node_options.includes(node_to_insert?.node_type)) {
360
- error_message = `"${node_to_insert?.node_type}" not allowed in "${drop_area_node_type}" ${
361
- Array.isArray(drop_area_node_path) && drop_area_node_path.length > 0
362
- ? `at path [${drop_area_node_path.join(", ")}]`
363
- : ""
364
- }`
365
- set_root_node_prepped()
366
- return
367
- }
368
- if (node_to_insert?.node_path) {
369
- let node_value_at_path = get_node_at_path(structure_loc, node_to_insert?.node_path)
370
- structure_loc = remove_node(structure_loc, node_to_insert?.node_path)
371
- if (
372
- drop_area_node_path.length >= node_to_insert?.node_path.length &&
373
- typeof node_to_insert?.node_path[node_to_insert?.node_path.length - 1] == "number" &&
374
- drop_area_node_path[node_to_insert?.node_path.length - 1] >=
375
- node_to_insert?.node_path[node_to_insert?.node_path.length - 1]
376
- ) {
377
- drop_area_node_path[node_to_insert?.node_path.length - 1] =
378
- drop_area_node_path[node_to_insert?.node_path.length - 1] - 1
379
- }
380
- structure_loc = set_node_at_node_path(structure_loc, drop_area_node_path, node_value_at_path, true)
381
- } else if (node_to_insert?.node_type) {
382
- let default_node_value = get_default_node_value(node_to_insert?.node_type)
383
- structure_loc = set_node_at_node_path(structure_loc, drop_area_node_path, default_node_value, true)
384
- } else {
385
- error_message = "Node to insert type_id not set"
386
- return
387
- }
388
- }
389
- versions = [structure_loc, ...versions.slice(selected_version_index)]
390
- selected_version_index = 0
391
- dragging_node = null
392
- set_root_node_prepped()
393
- }
394
-
395
- function run_node_literal(structure, node_path, defined = {}, parent_node_path) {
396
- let logs = []
397
- let output = null
398
- let is_return = false
399
- if (!structure || !structure.hasOwnProperty(node_path)) {
400
- return {
401
- logs: [
402
- {
403
- kind: "error",
404
- label: "Runtime Error",
405
- value: `node_path, ${node_path}, not found.`,
406
- },
407
- ],
408
- output,
409
- is_return,
410
- defined,
411
- }
412
- }
413
- function crunch_literal(structure, input, input_type, defined, parent_node_path) {
414
- let logs = []
415
- if (input_type?.is_custom_literal || input_type?.hasOwnProperty("node_options")) {
416
- if (input?.node_path && input_type?.type != "node") {
417
- let run_node_literal_res = run_node_literal(structure, input?.node_path, defined, parent_node_path)
418
- input = run_node_literal_res?.output
419
- defined = run_node_literal_res?.defined
420
- logs = [...logs, ...run_node_literal_res?.logs]
421
- }
422
- } else if (input_type?.type == "array" || Array.isArray(input)) {
423
- if (Array.isArray(input)) {
424
- for (let i = 0; i < input.length; i++) {
425
- let crunch_literal_res = crunch_literal(structure, input?.[i], input_type?.items, defined, parent_node_path)
426
- defined = crunch_literal_res?.defined
427
- logs = [...logs, ...crunch_literal_res?.logs]
428
- input[i] = crunch_literal_res?.output
429
- if (crunch_literal_res?.is_return) {
430
- return {
431
- defined,
432
- output: input,
433
- logs,
434
- }
435
- }
436
- }
437
- }
438
- } else if (typeof input == "object" && !Array.isArray(input)) {
439
- for (let key of Object.keys(input || {})) {
440
- let crunch_literal_res = crunch_literal(
441
- structure,
442
- input?.[key],
443
- input_type?.properties?.[key],
444
- defined,
445
- parent_node_path
446
- )
447
- defined = crunch_literal_res?.defined
448
- logs = [...logs, ...crunch_literal_res?.logs]
449
- input[key] = crunch_literal_res?.output
450
- if (crunch_literal_res?.is_return) {
451
- return {
452
- defined,
453
- output: input,
454
- logs,
455
- }
456
- }
457
- }
458
- }
459
- return {
460
- defined,
461
- output: input,
462
- logs,
463
- }
464
- }
465
- if (structure?.[node_path]?.node_type == "define") {
466
- defined[node_path] = { defined_in_node_path: parent_node_path }
467
- return { logs, output, defined, is_return }
468
- }
469
- let found_node_type = function_types?.[structure?.[node_path]?.node_type]?.input
470
- let crunch_literal_res = crunch_literal(
471
- deep_copy(structure),
472
- deep_copy(structure?.[node_path]?.input),
473
- found_node_type,
474
- deep_copy(defined),
475
- parent_node_path
476
- )
477
- defined = crunch_literal_res?.defined
478
- logs = [...logs, ...crunch_literal_res?.logs]
479
- let input = crunch_literal_res?.output
480
- switch (structure?.[node_path]?.node_type) {
481
- case "define":
482
- // handle this before to avoid crunching input
483
- break
484
- case "call_function_method":
485
- //tbd
486
- break
487
- case "call_defined":
488
- // tbd need to conslider that may be function method or direct function or maybe seperate these nodes
489
- console.log("call_defined_pre", deep_copy({ defined, input }))
490
- if (!defined.hasOwnProperty(input?.defined_function_node_path)) {
491
- logs = [
492
- ...logs,
493
- {
494
- kind: "error",
495
- label: "Runtime Error",
496
- value: `Called function, ${
497
- structure?.[input?.defined_function_node_path]?.input?.name ?? input?.defined_function_node_path
498
- }, not defined`,
499
- },
500
- ]
501
- break
502
- }
503
- // let found_node_type = function_types?.[structure?.[input?.defined_function_node_path]?.node_type]?.input
504
- // let crunch_literal_res = crunch_literal(
505
- // deep_copy(structure),
506
- // structure?.[input?.defined_function_node_path]?.input,
507
- // found_node_type,
508
- // defined,
509
- // node_path
510
- // )
511
- // defined = crunch_literal_res?.defined
512
- // logs = [...logs, ...crunch_literal_res?.logs]
513
- // the above may be a better equivalent to below
514
- if (Array.isArray(structure?.[input?.defined_function_node_path]?.input?.do)) {
515
- for (let do_item of structure?.[input?.defined_function_node_path]?.input?.do) {
516
- if (do_item?.node_path) {
517
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
518
- logs = [...logs, ...run_node_literal_res?.logs]
519
- defined = run_node_literal_res?.defined
520
- is_return = run_node_literal_res?.is_return
521
- if (is_return) {
522
- output = run_node_literal_res?.output
523
- for (let [key, val] of Object.entries(defined || {})) {
524
- if (val?.defined_in_node_path == node_path) {
525
- delete defined[key]
526
- }
527
- }
528
- break
529
- }
530
- }
531
- }
532
- for (let [key, val] of Object.entries(defined || {})) {
533
- if (val?.defined_in_node_path == node_path) {
534
- delete defined[key]
535
- }
536
- }
537
- }
538
- break
539
- case "if_then":
540
- if (input.condition) {
541
- if (Array.isArray(input?.passed_do)) {
542
- for (let do_item of input?.passed_do) {
543
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
544
- logs = [...logs, ...run_node_literal_res?.logs]
545
- defined = run_node_literal_res?.defined
546
- is_return = run_node_literal_res?.is_return
547
- if (is_return) {
548
- output = run_node_literal_res?.output
549
- break
550
- }
551
- }
552
- for (let [key, val] of Object.entries(defined || {})) {
553
- if (val?.defined_in_node_path == node_path) {
554
- delete defined[key]
555
- }
556
- }
557
- }
558
- }
559
- break
560
- case "if_then_else":
561
- if (input.condition) {
562
- if (Array.isArray(input?.passed_do)) {
563
- for (let do_item of input?.passed_do) {
564
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
565
- logs = [...logs, ...run_node_literal_res?.logs]
566
- defined = run_node_literal_res?.defined
567
- is_return = run_node_literal_res?.is_return
568
- if (is_return) {
569
- output = run_node_literal_res?.output
570
- break
571
- }
572
- }
573
- for (let [key, val] of Object.entries(defined || {})) {
574
- if (val?.defined_in_node_path == node_path) {
575
- delete defined[key]
576
- }
577
- }
578
- }
579
- } else {
580
- if (Array.isArray(input?.failed_do)) {
581
- for (let do_item of input?.failed_do) {
582
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
583
- logs = [...logs, ...run_node_literal_res?.logs]
584
- defined = run_node_literal_res?.defined
585
- is_return = run_node_literal_res?.is_return
586
- if (is_return) {
587
- output = run_node_literal_res?.output
588
- break
589
- }
590
- }
591
- for (let [key, val] of Object.entries(defined || {})) {
592
- if (val?.defined_in_node_path == node_path) {
593
- delete defined[key]
594
- }
595
- }
596
- }
597
- }
598
- break
599
- case "for_loop":
600
- let loop_over = input?.loop_over
601
- if (typeof loop_over === "object" && !Array.isArray(loop_over)) {
602
- loop_over = Object.entries(loop_over).map(([key, value]) => ({ key, value }))
603
- } else if (!isNaN(loop_over)) {
604
- loop_over = Array.from({ length: loop_over }, (_, i) => i)
605
- }
606
- if (Array.isArray(loop_over) && Array.isArray(input?.do)) {
607
- defined[node_path] = { defined_in_node_path: node_path, value: null }
608
- for (let value of loop_over) {
609
- defined[node_path].value = value
610
- for (let do_item of input?.do) {
611
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
612
- logs = [...logs, ...run_node_literal_res?.logs]
613
- defined = run_node_literal_res?.defined
614
- is_return = run_node_literal_res?.is_return
615
- if (is_return) {
616
- output = run_node_literal_res?.output
617
- break
618
- }
619
- }
620
- }
621
- for (let [key, val] of Object.entries(defined || {})) {
622
- if (val?.defined_in_node_path == node_path) {
623
- delete defined[key]
624
- }
625
- }
626
- }
627
- break
628
- case "return":
629
- is_return = true
630
- output = input
631
- break
632
- case "log":
633
- logs = [{ kind: input?.kind, label: input?.label, value: input?.value }]
634
- break
635
- case "define":
636
- defined[node_path] = { defined_in_node_path: parent_node_path, value: input?.value }
637
- break
638
- case "set_defined":
639
- if (defined.hasOwnProperty(input?.defined_node_path)) {
640
- defined[input?.defined_node_path].value = input?.value
641
- } else {
642
- logs = [
643
- ...logs,
644
- {
645
- kind: "error",
646
- label: "Runtime Error",
647
- value: `Variable, ${
648
- structure?.[input?.defined_function_node_path]?.input?.name ?? input?.defined_function_node_path
649
- }, not defined`,
650
- },
651
- ]
652
- }
653
- break
654
- case "get_defined":
655
- if (!defined.hasOwnProperty(input?.defined_node_path)) {
656
- logs = [
657
- ...logs,
658
- {
659
- kind: "error",
660
- label: "Runtime Error",
661
- value: `Variable, ${
662
- structure?.[input?.defined_function_node_path]?.input?.name ?? input?.defined_function_node_path
663
- }, not defined`,
664
- },
665
- ]
666
- } else {
667
- output = defined[input?.defined_node_path]?.value
668
- }
669
- break
670
- case "select_defined":
671
- output = input?.defined_node_path
672
- break
673
- case "set_defined_object_field":
674
- if (defined.hasOwnProperty(input?.defined_node_path)) {
675
- defined[input?.defined_node_path].value[input?.field_name] = input?.value
676
- } else {
677
- logs = [
678
- ...logs,
679
- {
680
- kind: "error",
681
- label: "Runtime Error",
682
- value: `Variable, ${
683
- structure?.[input?.defined_function_node_path]?.input?.name ?? input?.defined_function_node_path
684
- }, not defined`,
685
- },
686
- ]
687
- }
688
- break
689
- case "truthy_compare":
690
- if (input?.operator == "logical_or") {
691
- output = input?.a || input?.b
692
- } else if (input?.operator == "nullish_coalescing") {
693
- output = input?.a ?? input?.b
694
- } else if (input?.operator == "and") {
695
- output = input?.a && input?.b
696
- }
697
- break
698
- case "arithmetic_binary_operation":
699
- if (input?.operator === "add") {
700
- output = input?.a + input?.b
701
- } else if (input?.operator === "subtract") {
702
- output = input?.a - input?.b
703
- } else if (input?.operator === "multiply") {
704
- output = input?.a * input?.b
705
- } else if (input?.operator === "divide") {
706
- output = input?.a / input?.b
707
- } else if (input?.operator === "remainder") {
708
- output = input?.a % input?.b
709
- } else if (input?.operator === "exponent") {
710
- output = Math.pow(input?.a, input?.b)
711
- } else if (input?.operator === "min") {
712
- output = Math.min(input?.a, input?.b)
713
- } else if (input?.operator === "max") {
714
- output = Math.max(input?.a, input?.b)
715
- } else if (input?.operator === "rand_value_between") {
716
- output = Math.random() * (input?.b - input?.a) + input?.a //may make its own thing
717
- } else if (input?.operator === "to_precision") {
718
- output = Number(input?.a).toPrecision(input?.b)
719
- } else {
720
- output = null
721
- }
722
- break
723
- case "compare_numbers":
724
- if (input?.operator == "less_than") {
725
- output = input?.a < input?.b
726
- } else if (input?.operator == "less_than_or_equal") {
727
- output = input?.a <= input?.b
728
- } else if (input?.operator == "greater_than") {
729
- output = input?.a > input?.b
730
- } else if (input?.operator == "greater_than_or_equal") {
731
- output = input?.a >= input?.b
732
- } else if (input?.operator == "equal_to") {
733
- output = input?.a == input?.b
734
- } else if (input?.operator == "not_equal_to") {
735
- output = input?.a != input?.b
736
- }
737
- break
738
- case "compare_text":
739
- if (input?.operator === "equal_to") {
740
- output = input?.a == input?.b
741
- } else if (input?.operator === "not_equal_to") {
742
- output = input?.a !== input?.b
743
- } else if (input?.operator === "contains") {
744
- output = input?.a?.includes(input?.b)
745
- } else if (input?.operator === "does_not_contain") {
746
- output = !input?.a?.includes(input?.b)
747
- } else if (input?.operator === "starts_with") {
748
- output = input?.a?.startsWith(input?.b)
749
- } else if (input?.operator === "ends_with") {
750
- output = input?.a?.endsWith(input?.b)
751
- }
752
- break
753
- case "round":
754
- if (input?.operator === "nearest_int") {
755
- output = Math.round(input?.a)
756
- } else if (input?.operator === "floor_int") {
757
- output = Math.floor(input?.a)
758
- } else if (input?.operator === "ceil_int") {
759
- output = Math.ceil(input?.a)
760
- } else if (input?.operator === "absolute_value") {
761
- output = Math.abs(input?.a)
762
- }
763
- break
764
- case "parse":
765
- if (input?.operator === "to_int") {
766
- output = parseInt(input?.value)
767
- } else if (input?.operator === "to_float") {
768
- output = parseFloat(input?.value)
769
- } else if (input?.operator === "to_object") {
770
- output = JSON.parse(input?.value)
771
- } else if (input?.operator === "to_array") {
772
- output = Array.isArray(input?.value) ? input?.value : JSON.parse(input?.value)
773
- } else if (input?.operator === "to_boolean") {
774
- output = Boolean(input?.value)
775
- } else if (input?.operator === "to_account_id") {
776
- output = input?.value?.toString() // assuming account_id should be a string
777
- } else {
778
- output = null
779
- }
780
- break
781
- case "add_item_to_array":
782
- if (Array.isArray(input?.array)) {
783
- if (input?.where_to_add == "start") {
784
- output = [input?.value_to_add, ...input?.array]
785
- } else if (input?.where_to_add == "end") {
786
- output = [...input?.array, input?.value_to_add]
787
- } else {
788
- output = null
789
- }
790
- }
791
- break
792
- case "add_item_to_array_at_index":
793
- if (Array.isArray(input?.array) && typeof input?.where_to_add == "number") {
794
- output = [...input.array]
795
- output.splice(input.where_to_add, 0, input.value_to_add)
796
- }
797
- break
798
- case "remove_item_from_array":
799
- if (Array.isArray(input?.array)) {
800
- output = [...input.array]
801
- if (input.where_to_remove === "start") {
802
- output.shift()
803
- } else if (input.where_to_remove === "end") {
804
- output.pop()
805
- } else {
806
- output = null
807
- }
808
- } else {
809
- output = null
810
- }
811
- break
812
- case "remove_item_from_array_at_index":
813
- if (Array.isArray(input?.array)) {
814
- output = [...input.array].filter((_, index) => index !== input?.where_to_remove)
815
- }
816
- break
817
- case "concat_arrays":
818
- if (Array.isArray(input?.first_array) && Array.isArray(input?.second_array)) {
819
- output = [...input?.first_array, ...input?.second_array]
820
- }
821
- break
822
- case "local_compare":
823
- output = input?.a.localCompare(input?.b)
824
- break
825
- case "sort_array":
826
- if (Array.isArray(input?.array) && Array.isArray(input?.do)) {
827
- defined[node_path] = {
828
- defined_in_node_path: node_path,
829
- a: null,
830
- b: null,
831
- }
832
- input.array.sort((a, b) => {
833
- defined[node_path].a = a
834
- defined[node_path].b = b
835
- let sort_value = 0
836
- for (let do_item of input?.do) {
837
- let run_node_literal_res = run_node_literal(structure, do_item?.node_path, defined, node_path)
838
- logs = [...logs, ...run_node_literal_res?.logs]
839
- defined = run_node_literal_res?.defined
840
- if (run_node_literal_res?.is_return) {
841
- sort_value =
842
- run_node_literal_res?.output == true
843
- ? 1
844
- : run_node_literal_res?.output == false
845
- ? -1
846
- : run_node_literal_res?.output
847
- break
848
- }
849
- }
850
- return sort_value
851
- })
852
- output = input.array
853
- for (let [key, val] of Object.entries(defined || {})) {
854
- if (val?.defined_in_node_path == node_path) {
855
- delete defined[key]
856
- }
857
- }
858
- }
859
- break
860
- case "slice":
861
- if (typeof input?.value === "string" || Array.isArray(input?.value)) {
862
- output = input?.value.slice(input?.start_index, input?.end_index)
863
- }
864
- break
865
- case "join_array_of_text_items":
866
- if (Array.isArray(input)) {
867
- output = [...input].join()
868
- }
869
- break
870
- case "reverse_array":
871
- if (Array.isArray(input)) {
872
- output = [...input].reverse()
873
- }
874
- break
875
- case "join_array_of_text":
876
- if (Array.isArray(input?.array)) {
877
- output = [...input.array].join()
878
- }
879
- break
880
- case "sum_of_number_array":
881
- if (Array.isArray(input)) {
882
- output = input.reduce((acc, curr) => acc + curr, 0)
883
- }
884
- break
885
- case "average_of_number_array":
886
- if (Array.isArray(input)) {
887
- output = input.reduce((acc, curr) => acc + curr, 0) / input.length
888
- }
889
- break
890
- case "split_text":
891
- if (input?.text && input?.delimiter) {
892
- output = input?.text.split(input?.delimiter)
893
- }
894
- break
895
- case "replace_text":
896
- if (input?.base_text && input?.replace_text && input?.replacement_text) {
897
- output = input?.base_text.replace(input?.replace_text, input?.replacement_text)
898
- }
899
- break
900
- case "trim_text":
901
- if (input) {
902
- output = input?.trim()
903
- }
904
- break
905
- case "get_length":
906
- if (Array.isArray(input)) {
907
- output = input.length
908
- } else if (typeof input === "object") {
909
- output = Object.keys(input).length
910
- } else if (typeof input === "string") {
911
- output = input.length
912
- }
913
- break
914
- case "empty_array":
915
- output = []
916
- break
917
- case "empty_object":
918
- output = {}
919
- break
920
- case "null":
921
- output = null
922
- break
923
- case "email_address_literal":
924
- output = input
925
- break
926
- case "int_literal":
927
- output = input
928
- case "bigint_literal":
929
- output = input
930
- case "float_literal":
931
- output = input
932
- case "text_literal":
933
- output = input
934
- break
935
- case "checkbox_literal":
936
- output = !!input
937
- break
938
- case "time_literal":
939
- output = input
940
- break
941
- case "location_literal":
942
- output = input
943
- break
944
- case "color_literal":
945
- output = input
946
- break
947
- case "object_literal":
948
- console.log("object_literal", input)
949
- let obj = {}
950
- if (Array.isArray(input)) {
951
- for (let i = 0; i < input.length; i++) {
952
- for (let key of ["key", "value"]) {
953
- console.log("object_literal1", input?.[i][key])
954
- if (input?.[i][key]?.type) {
955
- let run_node_literal_res = run_node_literal(structure, input[i][key].input, defined, node_path)
956
- logs = [...logs, ...run_node_literal_res?.logs]
957
- input[i][key] = run_node_literal_res?.output
958
- }
959
- }
960
- obj[input[i]?.key] = input[i]?.value
961
- console.log("object_literal3", obj)
962
- }
963
- }
964
- output = obj
965
- break
966
- case "subscriber_id_literal":
967
- output = input
968
- break
969
- case "call_endpoint":
970
- // input = {
971
- // endpoint: , text
972
- // headers: , json
973
- // body:
974
- // }
975
- output = input
976
- break
977
- case "set_custom_field_on_subscriber":
978
- // input = {
979
- // subscriber_id: , text
980
- // custom_field_name:
981
- // value:
982
- // }
983
- break
984
- case "set_subscriber_tier":
985
- // input = {
986
- // subscriber_id: , text
987
- // tier:
988
- // }
989
- break
990
- case "send_email":
991
- // input = {
992
- // send_from_account_id: , text
993
- // email_template_id:
994
- // email_variables:
995
- // email_options:
996
- // }
997
- break
998
- default:
999
- output = input
1000
- break
1001
- }
1002
- return { logs, output, defined, is_return }
1003
- }
1004
-
1005
- function run_selected_config(input = {}) {
1006
- let selected_config = deep_copy(selected_version)
1007
- console.log("prerun structure", JSON.parse(JSON.stringify(selected_config)))
1008
- let init_literal = null //tbd
1009
- let wrapped_structure = {
1010
- ...selected_config,
1011
- call_main_function_00: {
1012
- node_type: "call_defined",
1013
- input: {
1014
- defined_function_node_path: "main_function_000000000",
1015
- input: init_literal,
1016
- },
1017
- },
1018
- }
1019
- let run_node_res = run_node_literal(wrapped_structure, "call_main_function_00", {
1020
- main_function_000000000: { defined_in_node_path: null },
1021
- })
1022
- console.log("run_node_res", run_node_res)
1023
- console_logs = run_node_res?.logs
1024
- console_result = run_node_res?.output
1025
- return run_node_res
1026
- }
1027
-
1028
- let update_function_is_loading = $state(false)
1029
- let update_function_success_trigger = $state(0)
1030
- let update_function_error_trigger = $state(0)
1031
- let update_function_error_message = $state(null)
1032
- async function update_function(account_id) {
1033
- console.log("update_function", account_id)
1034
- try {
1035
- update_function_is_loading = true
1036
- update_function_error_message = null
1037
- let update_function_res = await fetch(`/api/v1/accounts/${account_id}/functions/${id}`, {
1038
- method: "PUT",
1039
- body: JSON.stringify({
1040
- name: name,
1041
- description: description,
1042
- structure: selected_version,
1043
- }),
1044
- })
1045
- let update_function_res_body = await update_function_res.json()
1046
- console.log("update_function_res_body", update_function_res_body)
1047
- if (!update_function_res.ok) {
1048
- throw new Error(update_function_res_body?.message ?? "Error creating function.")
1049
- }
1050
- if (selected_version_index > 0) {
1051
- versions = versions.splice(selected_version_index)
1052
- selected_version_index = 0
1053
- }
1054
- name = update_function_res_body?.name
1055
- description = update_function_res_body?.description
1056
- db_epoch_updated = update_function_res_body?.db_epoch_updated
1057
- db_epoch_created = update_function_res_body?.db_epoch_created
1058
- update_function_success_trigger += 1
1059
- } catch (err) {
1060
- update_function_error_message = err
1061
- update_function_error_trigger += 1
1062
- } finally {
1063
- update_function_is_loading = false
1064
- }
1065
- }
1066
-
1067
- function init(config) {
1068
- init_function = config
1069
- id = config?.id
1070
- name = config?.name
1071
- description = config?.description
1072
- db_epoch_updated = config?.db_epoch_updated
1073
- db_epoch_created = config?.db_epoch_created
1074
- node_path = config?.node_path
1075
- if (config?.structure) {
1076
- versions = [config?.structure]
1077
- } else {
1078
- versions = [
1079
- {
1080
- type: "config",
1081
- value: {
1082
- parameters: {
1083
- type: "parameters_literal",
1084
- value: null,
1085
- },
1086
- is_do_collapsed: {
1087
- type: "arrow_toggle_literal",
1088
- value: false,
1089
- },
1090
- do: {
1091
- type: "stack_literal",
1092
- value: [],
1093
- },
1094
- },
1095
- },
1096
- ]
1097
- }
1098
- selected_version_index = 0
1099
- undo_version_button_manager = create_button_manager({
1100
- type: "soft",
1101
- is_no_wrap: true,
1102
- icon_sw: 40,
1103
- is_uniform: true,
1104
- support_icon: "undo_arrow",
1105
- is_disabled: versions.length < 2 || versions.length - selected_version_index < 2,
1106
- on_click: () => undo(),
1107
- })
1108
- redo_version_button_manager = create_button_manager({
1109
- type: "soft",
1110
- is_no_wrap: true,
1111
- icon_sw: 40,
1112
- is_uniform: true,
1113
- support_icon: "redo_arrow",
1114
- is_disabled: versions.length < 2 || versions.length - selected_version_index < 1,
1115
- on_click: () => redo(),
1116
- })
1117
- trashcan_icon_manager = create_icon_manager({
1118
- icon_id: "trashcan",
1119
- size: 8,
1120
- sw: 10,
1121
- color: "oklch(var(--l10-t) var(--c11) var(--h1))",
1122
- })
1123
- node_category_buttons = get_node_category_buttons()
1124
- set_root_node_prepped()
1125
- set_node_toolbox_items_prepped()
1126
- }
1127
- init(config)
1128
-
1129
- return {
1130
- get selected_category() {
1131
- return selected_category
1132
- },
1133
- get versions() {
1134
- return versions
1135
- },
1136
- get selected_version_index() {
1137
- return selected_version_index
1138
- },
1139
- get selected_version() {
1140
- return selected_version
1141
- },
1142
- get error_message() {
1143
- return error_message
1144
- },
1145
- get console_logs() {
1146
- return console_logs
1147
- },
1148
- get console_result() {
1149
- return console_result
1150
- },
1151
- get node_types() {
1152
- return function_types
1153
- },
1154
- get dragging_node() {
1155
- return dragging_node
1156
- },
1157
- get init_function() {
1158
- return init_function
1159
- },
1160
- get id() {
1161
- return id
1162
- },
1163
- get name() {
1164
- return name
1165
- },
1166
- get description() {
1167
- return description
1168
- },
1169
- get db_epoch_updated() {
1170
- return db_epoch_updated
1171
- },
1172
- get db_epoch_created() {
1173
- return db_epoch_created
1174
- },
1175
- get update_function_is_loading() {
1176
- return update_function_is_loading
1177
- },
1178
- get update_function_success_trigger() {
1179
- return update_function_success_trigger
1180
- },
1181
- get update_function_error_trigger() {
1182
- return update_function_error_trigger
1183
- },
1184
- get update_function_error_message() {
1185
- return update_function_error_message
1186
- },
1187
- get undo_version_button_manager() {
1188
- return undo_version_button_manager
1189
- },
1190
- get redo_version_button_manager() {
1191
- return redo_version_button_manager
1192
- },
1193
- get node_category_buttons() {
1194
- return node_category_buttons
1195
- },
1196
- get root_node_manager() {
1197
- return root_node_manager
1198
- },
1199
- get trashcan_icon_manager() {
1200
- return trashcan_icon_manager
1201
- },
1202
- get node_toolbox_items_prepped() {
1203
- return node_toolbox_items_prepped
1204
- },
1205
- update_function,
1206
- undo,
1207
- redo,
1208
- handle_node_drag_start,
1209
- handle_node_drag_end,
1210
- drag_or_select_node,
1211
- set_node_value,
1212
- get_defined_at_path,
1213
- run_node_literal,
1214
- }
1215
- }