@arkitektio/bsx 2.1.0 → 2.2.1

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 (32) hide show
  1. package/dist/catalogs/index.d.ts +24 -24
  2. package/dist/catalogs/parts/data.d.ts +1 -1
  3. package/dist/catalogs/parts/feedback.d.ts +3 -3
  4. package/dist/catalogs/parts/forms.d.ts +4 -4
  5. package/dist/catalogs/parts/icons.d.ts +2 -2
  6. package/dist/catalogs/parts/index.d.ts +24 -24
  7. package/dist/catalogs/parts/navigation.d.ts +4 -4
  8. package/dist/catalogs/parts/overlays.d.ts +11 -11
  9. package/dist/catalogs/shadcn.d.ts +11 -11
  10. package/dist/catalogs/ui.d.ts +13 -13
  11. package/dist/renderer/bloks/control.d.ts.map +1 -1
  12. package/dist/renderer/bloks/control.js +14 -2
  13. package/dist/renderer/bloks/control.js.map +1 -1
  14. package/dist/renderer/bloks/data.d.ts +1 -1
  15. package/dist/renderer/bloks/feedback.d.ts +3 -3
  16. package/dist/renderer/bloks/forms.d.ts +4 -4
  17. package/dist/renderer/bloks/icons.d.ts +1 -1
  18. package/dist/renderer/bloks/index.d.ts +24 -24
  19. package/dist/renderer/bloks/navigation.d.ts +4 -4
  20. package/dist/renderer/bloks/overlays.d.ts +11 -11
  21. package/dist/renderer/functions/v2/index.d.ts.map +1 -1
  22. package/dist/renderer/functions/v2/index.js +48 -0
  23. package/dist/renderer/functions/v2/index.js.map +1 -1
  24. package/dist/shadcn.d.ts +24 -24
  25. package/dist/spec/vocabulary.json +27 -0
  26. package/dist/spec/vocabulary.json.js +27 -0
  27. package/package.json +1 -1
  28. package/src/renderer/bloks/control.tsx +18 -1
  29. package/src/renderer/bloks/flow.test.tsx +59 -1
  30. package/src/renderer/functions/v2/functions.test.ts +60 -1
  31. package/src/renderer/functions/v2/index.ts +48 -0
  32. package/src/spec/vocabulary.json +27 -0
@@ -4,14 +4,18 @@ import {
4
4
  BlokScopeProvider,
5
5
  BlokSuspenseContext,
6
6
  createBlokComponent,
7
+ isWritableScopedPath,
8
+ resolveScopedPath,
7
9
  useBlok,
8
10
  useBlokDrawContext,
11
+ useBlokScope,
9
12
  useResolvedProp,
10
13
  useScopedResolver,
11
14
  useSuspenseReport,
12
15
  useValue,
13
16
  type BlokComponentNode,
14
17
  type BlokSourceStatus,
18
+ type BlokScope,
15
19
  type BlokSuspenseReporter,
16
20
  } from '../runtime';
17
21
  import {useBlokRunHost} from './run/host';
@@ -108,6 +112,18 @@ export const foreachItemIds = (id: string, count: number, keys: readonly unknown
108
112
  });
109
113
  };
110
114
 
115
+ /**
116
+ * The absolute path of a foreach's list, or undefined when it has none.
117
+ *
118
+ * An alias is taken as absolute, so the items of a nested loop (`items={row.cells}`)
119
+ * must be addressed past the enclosing `row`. A list on an item handed down by value
120
+ * has no address at all, so its items are handed down by value too.
121
+ */
122
+ const absoluteItemsPath = (path: string | null | undefined, scope: BlokScope): string | undefined => {
123
+ const trimmed = path?.replace(/[/.]+$/, '');
124
+ return trimmed && isWritableScopedPath(trimmed, scope) ? resolveScopedPath(trimmed, scope) : undefined;
125
+ };
126
+
111
127
  export const Foreach = createBlokComponent(
112
128
  {name: 'foreach', acceptsChildren: true, placement: {inheritsContext: true}, schema: foreachSchema},
113
129
  ({buildChild, component, schema}) => {
@@ -116,7 +132,8 @@ export const Foreach = createBlokComponent(
116
132
  const scopeName = blok['let'].prop?.declares_value ?? (typeof blok['let'].prop?.static_value === 'string' ? blok['let'].prop.static_value : '');
117
133
  const resolve = useScopedResolver();
118
134
  const childIds = React.useMemo(() => component.children?.map(child => child.id) ?? [], [component]);
119
- const itemsPath = blok.items.prop?.dynamic_value?.path?.replace(/[/.]+$/, '');
135
+ const scope = useBlokScope();
136
+ const itemsPath = absoluteItemsPath(blok.items.prop?.dynamic_value?.path, scope);
120
137
  const keyProp = blok.key.prop;
121
138
 
122
139
  if (!Array.isArray(items) || childIds.length === 0 || !scopeName) {
@@ -1,5 +1,5 @@
1
1
  // @vitest-environment jsdom
2
- import {act, cleanup, screen} from '@testing-library/react';
2
+ import {act, cleanup, fireEvent, screen} from '@testing-library/react';
3
3
  import {afterEach, describe, expect, it} from 'vitest';
4
4
  import {foreachItemIds} from './control';
5
5
  import {renderBsx, testCatalog} from './testing';
@@ -85,6 +85,64 @@ describe('foreach', () => {
85
85
  expect([...document.querySelectorAll('p')].map(element => element.textContent)).toEqual(['one', 'two', 'three']);
86
86
  });
87
87
 
88
+ describe('inside another foreach', () => {
89
+ const rows = [
90
+ {id: 'r1', cells: [{id: 'c1', name: 'One'}, {id: 'c2', name: 'Two'}]},
91
+ {id: 'r2', cells: [{id: 'c3', name: 'Three'}]},
92
+ ];
93
+ const nested = (items: string) =>
94
+ `<Card><foreach items={${items}} let="row" key={row.id}><foreach items={row.cells} let="cell" key={cell.id}><Text>{cell.name}</Text></foreach></foreach></Card>`;
95
+ const texts = () => [...document.querySelectorAll('p')].map(element => element.textContent);
96
+
97
+ it('reads the fields of items of a list on the outer item', () => {
98
+ render(nested('rows'), {rows});
99
+ expect(texts()).toEqual(['One', 'Two', 'Three']);
100
+ });
101
+
102
+ it('reads them when the outer list is on the agent', () => {
103
+ render(nested('self.rows'), {deps: {self: {rows}}});
104
+ expect(texts()).toEqual(['One', 'Two', 'Three']);
105
+ });
106
+
107
+ it('reads them when the outer items came by value', () => {
108
+ render(nested('list.reverse(rows)'), {rows});
109
+ expect(texts()).toEqual(['Three', 'One', 'Two']);
110
+ });
111
+
112
+ it('reads them through a third level', () => {
113
+ const deep = [{id: 'a', rows: [{id: 'b', cells: [{id: 'c', name: 'Deep'}]}]}];
114
+ render(
115
+ '<Card><foreach items={outer} let="o" key={o.id}><foreach items={o.rows} let="r" key={r.id}><foreach items={r.cells} let="c" key={c.id}><Text>{c.name}</Text></foreach></foreach></foreach></Card>',
116
+ {outer: deep},
117
+ );
118
+ expect(texts()).toEqual(['Deep']);
119
+ });
120
+
121
+ it('keys its items by their own field, not by a missing one', () => {
122
+ const {store} = render(nested('rows'), {rows});
123
+ const before = document.querySelectorAll('p')[1];
124
+ act(() => store.getState().setRuntimeValue('rows/0/cells', [rows[0].cells[1], rows[0].cells[0]]));
125
+ expect(texts()).toEqual(['Two', 'One', 'Three']);
126
+ expect(document.querySelectorAll('p')[0]).toBe(before);
127
+ });
128
+
129
+ it('follows the live data', () => {
130
+ const {store} = render(nested('rows'), {rows});
131
+ act(() => store.getState().setRuntimeValue('rows/1/cells/0/name', 'Changed'));
132
+ expect(texts()).toEqual(['One', 'Two', 'Changed']);
133
+ });
134
+
135
+ it('writes a bound control to the real list', () => {
136
+ const {store} = render(
137
+ '<Card><foreach items={rows} let="row" key={row.id}><foreach items={row.cells} let="cell" key={cell.id}><Input bind={cell.name} /></foreach></foreach></Card>',
138
+ {rows},
139
+ );
140
+ fireEvent.change(screen.getAllByRole('textbox')[2], {target: {value: 'Written'}});
141
+ const model = store.getState().dataModel as {rows: typeof rows};
142
+ expect(model.rows[1].cells[0].name).toBe('Written');
143
+ });
144
+ });
145
+
88
146
  it('names its items <id>@<key>, or <id>@#<n> without key', () => {
89
147
  expect(foreachItemIds('Card[0]/foreach[0]', 2, null)).toEqual(['Card[0]/foreach[0]@#0', 'Card[0]/foreach[0]@#1']);
90
148
  expect(foreachItemIds('f', 3, ['a', 'b', 'a'])).toEqual(['f@a', 'f@b', 'f@a#1']);
@@ -18,7 +18,7 @@ describe('coverage', () => {
18
18
  it('implements every pure vocabulary function, and nothing else', () => {
19
19
  const declared = [...loadVocabulary().functions.values()].filter(fn => !fn.effect).map(fn => fn.name).sort();
20
20
  expect([...v2Functions.keys()].sort()).toEqual(declared);
21
- expect(declared).toHaveLength(80);
21
+ expect(declared).toHaveLength(107);
22
22
  expect(undeclaredImplementations()).toEqual([]);
23
23
  });
24
24
 
@@ -53,6 +53,37 @@ describe('the null table', () => {
53
53
  ['math.div', {a: 1, b: 0}, null],
54
54
  ['math.div', {a: null, b: 2}, null],
55
55
  ['math.mod', {a: 1, b: 0}, null],
56
+ ['math.pow', {a: null, b: 2}, null],
57
+ ['math.pow', {a: -8, b: 0.5}, null],
58
+ ['math.pow', {a: 10, b: 400}, null],
59
+ ['math.sqrt', {value: -1}, null],
60
+ ['math.cbrt', {value: null}, null],
61
+ ['math.exp', {value: 1000}, null],
62
+ ['math.log', {value: 0}, null],
63
+ ['math.log', {value: null}, null],
64
+ ['math.log', {value: 8, base: 1}, null],
65
+ ['math.log2', {value: -1}, null],
66
+ ['math.log10', {value: 0}, null],
67
+ ['math.sin', {value: null}, null],
68
+ ['math.cos', {value: null}, null],
69
+ ['math.tan', {value: null}, null],
70
+ ['math.asin', {value: 2}, null],
71
+ ['math.acos', {value: 2}, null],
72
+ ['math.atan', {value: null}, null],
73
+ ['math.atan2', {y: null, x: 1}, null],
74
+ ['math.sinh', {value: 1000}, null],
75
+ ['math.cosh', {value: 1000}, null],
76
+ ['math.tanh', {value: null}, null],
77
+ ['math.asinh', {value: null}, null],
78
+ ['math.acosh', {value: 0}, null],
79
+ ['math.atanh', {value: 1}, null],
80
+ ['math.degrees', {value: null}, null],
81
+ ['math.radians', {value: null}, null],
82
+ ['math.sign', {value: null}, null],
83
+ ['math.trunc', {value: null}, null],
84
+ ['math.hypot', {values: []}, null],
85
+ ['math.pi', {}, Math.PI],
86
+ ['math.e', {}, Math.E],
56
87
  ['math.neg', {a: null}, null],
57
88
  ['math.abs', {value: null}, null],
58
89
  ['math.round', {value: null}, null],
@@ -160,6 +191,34 @@ describe('semantics', () => {
160
191
  expect(call('math.clamp', {value: 12, min: 0, max: 10})).toBe(10);
161
192
  expect(call('math.max', {values: [1, 7, 3]})).toBe(7);
162
193
  expect(call('math.mod', {a: 7, b: 3})).toBe(1);
194
+ expect(call('math.pow', {a: 2, b: 10})).toBe(1024);
195
+ expect(call('math.sqrt', {value: 9})).toBe(3);
196
+ expect(call('math.cbrt', {value: 27})).toBe(3);
197
+ expect(call('math.exp', {value: 0})).toBe(1);
198
+ expect(call('math.log', {value: Math.E})).toBe(1);
199
+ expect(call('math.log', {value: 8, base: 2})).toBeCloseTo(3);
200
+ expect(call('math.log2', {value: 8})).toBe(3);
201
+ expect(call('math.log10', {value: 1000})).toBe(3);
202
+ expect(call('math.sin', {value: 0})).toBe(0);
203
+ expect(call('math.cos', {value: 0})).toBe(1);
204
+ expect(call('math.tan', {value: 0})).toBe(0);
205
+ expect(call('math.asin', {value: 1})).toBeCloseTo(Math.PI / 2);
206
+ expect(call('math.acos', {value: 1})).toBe(0);
207
+ expect(call('math.atan', {value: 1})).toBeCloseTo(Math.PI / 4);
208
+ expect(call('math.atan2', {y: 1, x: 1})).toBeCloseTo(Math.PI / 4);
209
+ expect(call('math.sinh', {value: 0})).toBe(0);
210
+ expect(call('math.cosh', {value: 0})).toBe(1);
211
+ expect(call('math.tanh', {value: 0})).toBe(0);
212
+ expect(call('math.asinh', {value: 0})).toBe(0);
213
+ expect(call('math.acosh', {value: 1})).toBe(0);
214
+ expect(call('math.atanh', {value: 0})).toBe(0);
215
+ expect(call('math.degrees', {value: Math.PI})).toBe(180);
216
+ expect(call('math.radians', {value: 180})).toBe(Math.PI);
217
+ expect(call('math.sign', {value: -3})).toBe(-1);
218
+ expect(call('math.trunc', {value: -1.5})).toBe(-1);
219
+ expect(call('math.hypot', {values: [3, 4]})).toBe(5);
220
+ expect(call('math.pi', {})).toBe(Math.PI);
221
+ expect(call('math.e', {})).toBe(Math.E);
163
222
  });
164
223
 
165
224
  it('works on strings', () => {
@@ -259,6 +259,14 @@ const mathUnary =
259
259
  return value === null ? null : operate(value);
260
260
  };
261
261
 
262
+ /** The classic functions: what is not a finite number (`sqrt(-1)`, `log(0)`) is null. */
263
+ const finite = (value: number): number | null => (Number.isFinite(value) ? value : null);
264
+
265
+ const mathReal = (operate: (value: number) => number): Impl => args => {
266
+ const value = num(args.value);
267
+ return value === null ? null : finite(operate(value));
268
+ };
269
+
262
270
  const strUnary =
263
271
  (operate: (value: string) => unknown): Impl =>
264
272
  args => {
@@ -333,6 +341,7 @@ const IMPLEMENTATIONS: Readonly<Partial<Record<string, Impl>>> = {
333
341
  'math.mul': mathBinary((a, b) => a * b),
334
342
  'math.div': mathBinary((a, b) => (b === 0 ? null : a / b)),
335
343
  'math.mod': mathBinary((a, b) => (b === 0 ? null : a % b)),
344
+ 'math.pow': mathBinary((a, b) => finite(a ** b)),
336
345
  'math.neg': mathUnary('a', value => -value || 0),
337
346
  'math.abs': mathUnary('value', Math.abs),
338
347
  'math.round': args => {
@@ -356,6 +365,45 @@ const IMPLEMENTATIONS: Readonly<Partial<Record<string, Impl>>> = {
356
365
  return values.length ? Math.max(...values) : null;
357
366
  },
358
367
  'math.sum': args => numbers(args.values).reduce((total, value) => total + value, 0),
368
+ 'math.sqrt': mathReal(Math.sqrt),
369
+ 'math.cbrt': mathReal(Math.cbrt),
370
+ 'math.exp': mathReal(Math.exp),
371
+ 'math.log': args => {
372
+ const value = num(args.value);
373
+ if (value === null) return null;
374
+ if (args.base === null || args.base === undefined) return finite(Math.log(value));
375
+ const base = num(args.base);
376
+ return base === null ? null : finite(Math.log(value) / Math.log(base));
377
+ },
378
+ 'math.log2': mathReal(Math.log2),
379
+ 'math.log10': mathReal(Math.log10),
380
+ 'math.sin': mathReal(Math.sin),
381
+ 'math.cos': mathReal(Math.cos),
382
+ 'math.tan': mathReal(Math.tan),
383
+ 'math.asin': mathReal(Math.asin),
384
+ 'math.acos': mathReal(Math.acos),
385
+ 'math.atan': mathReal(Math.atan),
386
+ 'math.atan2': args => {
387
+ const y = num(args.y);
388
+ const x = num(args.x);
389
+ return y === null || x === null ? null : finite(Math.atan2(y, x));
390
+ },
391
+ 'math.sinh': mathReal(Math.sinh),
392
+ 'math.cosh': mathReal(Math.cosh),
393
+ 'math.tanh': mathReal(Math.tanh),
394
+ 'math.asinh': mathReal(Math.asinh),
395
+ 'math.acosh': mathReal(Math.acosh),
396
+ 'math.atanh': mathReal(Math.atanh),
397
+ 'math.degrees': mathReal(value => (value * 180) / Math.PI),
398
+ 'math.radians': mathReal(value => (value * Math.PI) / 180),
399
+ 'math.sign': mathReal(value => Math.sign(value) || 0),
400
+ 'math.trunc': mathReal(value => Math.trunc(value) || 0),
401
+ 'math.hypot': args => {
402
+ const values = numbers(args.values);
403
+ return values.length ? finite(Math.hypot(...values)) : null;
404
+ },
405
+ 'math.pi': () => Math.PI,
406
+ 'math.e': () => Math.E,
359
407
 
360
408
  // str
361
409
  'str.upper': strUnary(value => value.toUpperCase()),
@@ -160,6 +160,7 @@
160
160
  "math.mul": {"params": [["a", "FLOAT"], ["b", "FLOAT"]], "returns": "FLOAT", "null": "null"},
161
161
  "math.div": {"params": [["a", "FLOAT"], ["b", "FLOAT"]], "returns": "FLOAT", "null": "null; division by zero is null"},
162
162
  "math.mod": {"params": [["a", "FLOAT"], ["b", "FLOAT"]], "returns": "FLOAT", "null": "null; modulo zero is null"},
163
+ "math.pow": {"params": [["a", "FLOAT"], ["b", "FLOAT"]], "returns": "FLOAT", "description": "a to the power of b.", "null": "null; a result that is not a finite number is null"},
163
164
  "math.neg": {"params": [["a", "FLOAT"]], "returns": "FLOAT", "null": "null"},
164
165
  "math.abs": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "null": "null"},
165
166
  "math.round": {"params": [["value", "FLOAT"], ["digits", "INT", false]], "returns": "FLOAT", "description": "Half away from zero, to `digits` decimals (0).", "null": "null"},
@@ -169,6 +170,32 @@
169
170
  "math.min": {"params": [["values", "LIST"]], "returns": "FLOAT", "null": "nulls are skipped; null when none is left"},
170
171
  "math.max": {"params": [["values", "LIST"]], "returns": "FLOAT", "null": "nulls are skipped; null when none is left"},
171
172
  "math.sum": {"params": [["values", "LIST"]], "returns": "FLOAT", "null": "nulls are skipped; 0 when none is left"},
173
+ "math.sqrt": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The square root.", "null": "null; a result that is not a finite number is null"},
174
+ "math.cbrt": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The cube root.", "null": "null; a result that is not a finite number is null"},
175
+ "math.exp": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "e to the power of value.", "null": "null; a result that is not a finite number is null"},
176
+ "math.log": {"params": [["value", "FLOAT"], ["base", "FLOAT", false]], "returns": "FLOAT", "description": "The logarithm to `base` (e).", "null": "null; a result that is not a finite number is null"},
177
+ "math.log2": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The base 2 logarithm.", "null": "null; a result that is not a finite number is null"},
178
+ "math.log10": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The base 10 logarithm.", "null": "null; a result that is not a finite number is null"},
179
+ "math.sin": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The sine of an angle in radians.", "null": "null; a result that is not a finite number is null"},
180
+ "math.cos": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The cosine of an angle in radians.", "null": "null; a result that is not a finite number is null"},
181
+ "math.tan": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The tangent of an angle in radians.", "null": "null; a result that is not a finite number is null"},
182
+ "math.asin": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The arcsine, in radians.", "null": "null; a result that is not a finite number is null"},
183
+ "math.acos": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The arccosine, in radians.", "null": "null; a result that is not a finite number is null"},
184
+ "math.atan": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The arctangent, in radians.", "null": "null; a result that is not a finite number is null"},
185
+ "math.atan2": {"params": [["y", "FLOAT"], ["x", "FLOAT"]], "returns": "FLOAT", "description": "The angle of the point (x, y), in radians.", "null": "null; a result that is not a finite number is null"},
186
+ "math.sinh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The hyperbolic sine.", "null": "null; a result that is not a finite number is null"},
187
+ "math.cosh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The hyperbolic cosine.", "null": "null; a result that is not a finite number is null"},
188
+ "math.tanh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The hyperbolic tangent.", "null": "null; a result that is not a finite number is null"},
189
+ "math.asinh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The inverse hyperbolic sine.", "null": "null; a result that is not a finite number is null"},
190
+ "math.acosh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The inverse hyperbolic cosine.", "null": "null; a result that is not a finite number is null"},
191
+ "math.atanh": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The inverse hyperbolic tangent.", "null": "null; a result that is not a finite number is null"},
192
+ "math.degrees": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "Radians as degrees.", "null": "null; a result that is not a finite number is null"},
193
+ "math.radians": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "Degrees as radians.", "null": "null; a result that is not a finite number is null"},
194
+ "math.sign": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "-1, 0 or 1.", "null": "null; a result that is not a finite number is null"},
195
+ "math.trunc": {"params": [["value", "FLOAT"]], "returns": "FLOAT", "description": "The integer part, towards zero.", "null": "null; a result that is not a finite number is null"},
196
+ "math.hypot": {"params": [["values", "LIST"]], "returns": "FLOAT", "description": "The square root of the sum of squares.", "null": "nulls are skipped; null when none is left"},
197
+ "math.pi": {"params": [], "returns": "FLOAT", "description": "The constant pi.", "null": "takes no arguments"},
198
+ "math.e": {"params": [], "returns": "FLOAT", "description": "The constant e.", "null": "takes no arguments"},
172
199
  "str.upper": {"params": [["value", "STRING"]], "returns": "STRING", "null": "null"},
173
200
  "str.lower": {"params": [["value", "STRING"]], "returns": "STRING", "null": "null"},
174
201
  "str.trim": {"params": [["value", "STRING"]], "returns": "STRING", "null": "null"},