@optiaxiom/proteus 0.1.16 → 0.1.18

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.
@@ -2,6 +2,7 @@
2
2
  import { createContext } from '@radix-ui/react-context';
3
3
 
4
4
  const [ProteusDocumentPathProvider, useProteusDocumentPathContext] = createContext("@optiaxiom/proteus/ProteusDocumentPath", {
5
+ mapIndices: [],
5
6
  path: ""
6
7
  });
7
8
 
@@ -14,6 +14,7 @@ function ProteusDocumentShell({
14
14
  defaultOpen = true,
15
15
  element,
16
16
  onDataChange,
17
+ onDownload,
17
18
  onInteraction,
18
19
  onMessage,
19
20
  onOpenChange,
@@ -28,24 +29,6 @@ function ProteusDocumentShell({
28
29
  setValid(formRef.current.checkValidity());
29
30
  }
30
31
  }, []);
31
- useEffect(() => {
32
- if (!element.blocking || !formRef.current)
33
- return;
34
- const walker = document.createTreeWalker(
35
- formRef.current,
36
- NodeFilter.SHOW_ELEMENT,
37
- {
38
- acceptNode: (node) => {
39
- if (node instanceof HTMLInputElement && node.type === "hidden")
40
- return NodeFilter.FILTER_SKIP;
41
- if (node.hidden)
42
- return NodeFilter.FILTER_SKIP;
43
- return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
44
- }
45
- }
46
- );
47
- walker.nextNode()?.focus();
48
- }, [element.blocking]);
49
32
  const [open, setOpen] = useControllableState({
50
33
  defaultProp: defaultOpen,
51
34
  onChange: onOpenChange,
@@ -68,20 +51,24 @@ function ProteusDocumentShell({
68
51
  } else if ("message" in event) {
69
52
  await onMessage?.(event.message);
70
53
  } else if (event.action === "download") {
54
+ const urls = [];
71
55
  if (typeof event.url === "string") {
72
- await downloadFile(event.url);
56
+ urls.push(event.url);
73
57
  } else if (Array.isArray(event.url)) {
74
- await Promise.all(
75
- event.url.map((u) => {
76
- if (typeof u !== "string") {
77
- throw new Error("Invalid URL in download array");
78
- }
79
- return downloadFile(u);
80
- })
81
- );
58
+ for (const u of event.url) {
59
+ if (typeof u !== "string") {
60
+ throw new Error("Invalid URL in download array");
61
+ }
62
+ urls.push(u);
63
+ }
82
64
  } else {
83
65
  throw new Error("Invalid URL for download action");
84
66
  }
67
+ if (onDownload) {
68
+ await onDownload(urls);
69
+ } else {
70
+ await Promise.all(urls.map((u) => downloadFile(u)));
71
+ }
85
72
  }
86
73
  }),
87
74
  readOnly,
@@ -4,10 +4,13 @@ import { ProteusElement } from '../proteus-element/ProteusElement.js';
4
4
  import { getProteusValue } from './getProteusValue.js';
5
5
  import { evaluateCondition } from './resolveProteusValue.js';
6
6
 
7
- function resolveProteusProp(value, data, parentPath) {
7
+ function resolveProteusProp(value, data, parentPath, mapIndices = []) {
8
8
  if (typeof value !== "object" || value === null) {
9
9
  return value;
10
10
  }
11
+ if ("$type" in value && value.$type === "MapIndex") {
12
+ return mapIndices.at(-1);
13
+ }
11
14
  if ("$type" in value && value.$type === "Value" && "path" in value && typeof value.path === "string") {
12
15
  return getProteusValue(
13
16
  data,
@@ -20,7 +23,7 @@ function resolveProteusProp(value, data, parentPath) {
20
23
  const resolved = {};
21
24
  let length = 0;
22
25
  for (const [k, v] of Object.entries(sources)) {
23
- const arr = resolveProteusProp(v, data, parentPath);
26
+ const arr = resolveProteusProp(v, data, parentPath, mapIndices);
24
27
  if (Array.isArray(arr)) {
25
28
  resolved[k] = arr;
26
29
  length = Math.max(length, arr.length);
@@ -37,12 +40,12 @@ function resolveProteusProp(value, data, parentPath) {
37
40
  if ("$type" in value && value.$type === "Show" && "when" in value && "children" in value) {
38
41
  const conditions = Array.isArray(value.when) ? value.when : [value.when];
39
42
  const shouldShow = conditions.every(
40
- (condition) => evaluateCondition(condition, data, parentPath)
43
+ (condition) => evaluateCondition(condition, data, parentPath, mapIndices)
41
44
  );
42
45
  if (!shouldShow) {
43
46
  return void 0;
44
47
  }
45
- return resolveProteusProp(value.children, data, parentPath);
48
+ return resolveProteusProp(value.children, data, parentPath, mapIndices);
46
49
  }
47
50
  return "$type" in value || Array.isArray(value) && value.some((v) => v && typeof v === "object" && "$type" in v) ? /* @__PURE__ */ jsx(ProteusElement, { element: value }) : value;
48
51
  }
@@ -1,62 +1,75 @@
1
1
  "use client";
2
2
  import { getProteusValue } from './getProteusValue.js';
3
3
 
4
- function evaluateCondition(condition, data, parentPath) {
4
+ function evaluateCondition(condition, data, parentPath, mapIndices = []) {
5
5
  if (!condition) {
6
6
  return true;
7
7
  }
8
8
  if ("and" in condition) {
9
9
  return condition.and.every(
10
- (cond) => evaluateCondition(cond, data, parentPath)
10
+ (cond) => evaluateCondition(cond, data, parentPath, mapIndices)
11
11
  );
12
12
  }
13
13
  if ("or" in condition) {
14
14
  return condition.or.some(
15
- (cond) => evaluateCondition(cond, data, parentPath)
15
+ (cond) => evaluateCondition(cond, data, parentPath, mapIndices)
16
16
  );
17
17
  }
18
18
  if ("!!" in condition) {
19
- const value = resolveProteusValue(condition["!!"], data, parentPath);
19
+ const value = resolveProteusValue(
20
+ condition["!!"],
21
+ data,
22
+ parentPath,
23
+ mapIndices
24
+ );
20
25
  return !!value;
21
26
  }
22
27
  if ("!" in condition) {
23
- const value = resolveProteusValue(condition["!"], data, parentPath);
28
+ const value = resolveProteusValue(
29
+ condition["!"],
30
+ data,
31
+ parentPath,
32
+ mapIndices
33
+ );
24
34
  return !value;
25
35
  }
26
36
  if ("==" in condition) {
27
37
  const [left, right] = condition["=="];
28
- return resolveProteusValue(left, data, parentPath) === resolveProteusValue(right, data, parentPath);
38
+ return resolveProteusValue(left, data, parentPath, mapIndices) === resolveProteusValue(right, data, parentPath, mapIndices);
29
39
  } else if ("!=" in condition) {
30
40
  const [left, right] = condition["!="];
31
- return resolveProteusValue(left, data, parentPath) !== resolveProteusValue(right, data, parentPath);
41
+ return resolveProteusValue(left, data, parentPath, mapIndices) !== resolveProteusValue(right, data, parentPath, mapIndices);
32
42
  } else if ("<" in condition) {
33
43
  const [left, right] = condition["<"];
34
- const leftVal = resolveProteusValue(left, data, parentPath);
35
- const rightVal = resolveProteusValue(right, data, parentPath);
44
+ const leftVal = resolveProteusValue(left, data, parentPath, mapIndices);
45
+ const rightVal = resolveProteusValue(right, data, parentPath, mapIndices);
36
46
  return typeof leftVal === "number" && typeof rightVal === "number" && leftVal < rightVal;
37
47
  } else if ("<=" in condition) {
38
48
  const [left, right] = condition["<="];
39
- const leftVal = resolveProteusValue(left, data, parentPath);
40
- const rightVal = resolveProteusValue(right, data, parentPath);
49
+ const leftVal = resolveProteusValue(left, data, parentPath, mapIndices);
50
+ const rightVal = resolveProteusValue(right, data, parentPath, mapIndices);
41
51
  return typeof leftVal === "number" && typeof rightVal === "number" && leftVal <= rightVal;
42
52
  } else if (">" in condition) {
43
53
  const [left, right] = condition[">"];
44
- const leftVal = resolveProteusValue(left, data, parentPath);
45
- const rightVal = resolveProteusValue(right, data, parentPath);
54
+ const leftVal = resolveProteusValue(left, data, parentPath, mapIndices);
55
+ const rightVal = resolveProteusValue(right, data, parentPath, mapIndices);
46
56
  return typeof leftVal === "number" && typeof rightVal === "number" && leftVal > rightVal;
47
57
  } else if (">=" in condition) {
48
58
  const [left, right] = condition[">="];
49
- const leftVal = resolveProteusValue(left, data, parentPath);
50
- const rightVal = resolveProteusValue(right, data, parentPath);
59
+ const leftVal = resolveProteusValue(left, data, parentPath, mapIndices);
60
+ const rightVal = resolveProteusValue(right, data, parentPath, mapIndices);
51
61
  return typeof leftVal === "number" && typeof rightVal === "number" && leftVal >= rightVal;
52
62
  }
53
63
  return false;
54
64
  }
55
- function resolveProteusValue(value, data, parentPath) {
65
+ function resolveProteusValue(value, data, parentPath, mapIndices = []) {
56
66
  if (typeof value !== "object" || value === null) {
57
67
  return value;
58
68
  }
59
69
  if ("$type" in value) {
70
+ if (value.$type === "MapIndex") {
71
+ return mapIndices.at(-1);
72
+ }
60
73
  if (value.$type === "Value" && "path" in value && typeof value.path === "string") {
61
74
  return getProteusValue(
62
75
  data,
@@ -75,10 +88,18 @@ function resolveProteusValue(value, data, parentPath) {
75
88
  }
76
89
  const resolvedPath = value.path.startsWith("/") ? value.path : `${parentPath}/${value.path}`;
77
90
  const items = array.map(
78
- (_, index) => resolveProteusValue(value.children, data, `${resolvedPath}/${index}`)
91
+ (_, index) => resolveProteusValue(value.children, data, `${resolvedPath}/${index}`, [
92
+ ...mapIndices,
93
+ index
94
+ ])
79
95
  );
80
96
  if ("separator" in value) {
81
- const sep = resolveProteusValue(value.separator, data, parentPath);
97
+ const sep = resolveProteusValue(
98
+ value.separator,
99
+ data,
100
+ parentPath,
101
+ mapIndices
102
+ );
82
103
  return items.join(typeof sep === "string" ? sep : "");
83
104
  }
84
105
  return items;
@@ -86,18 +107,20 @@ function resolveProteusValue(value, data, parentPath) {
86
107
  if (value.$type === "Show" && "when" in value && "children" in value) {
87
108
  const conditions = Array.isArray(value.when) ? value.when : [value.when];
88
109
  const shouldShow = conditions.every(
89
- (condition) => evaluateCondition(condition, data, parentPath)
110
+ (condition) => evaluateCondition(condition, data, parentPath, mapIndices)
90
111
  );
91
112
  if (!shouldShow) {
92
113
  return void 0;
93
114
  }
94
- return resolveProteusValue(value.children, data, parentPath);
115
+ return resolveProteusValue(value.children, data, parentPath, mapIndices);
95
116
  }
96
117
  if (value.$type === "Concat" && "children" in value) {
97
118
  if (!Array.isArray(value.children)) {
98
119
  return value;
99
120
  }
100
- return value.children.map((child) => resolveProteusValue(child, data, parentPath)).filter((v) => v !== void 0).join("");
121
+ return value.children.map(
122
+ (child) => resolveProteusValue(child, data, parentPath, mapIndices)
123
+ ).filter((v) => v !== void 0).join("");
101
124
  }
102
125
  }
103
126
  return value;
@@ -6,12 +6,12 @@ function useResolveProteusValues(props) {
6
6
  const { data } = useProteusDocumentContext(
7
7
  "@optiaxiom/react/useResolveProteusValues"
8
8
  );
9
- const { path: parentPath } = useProteusDocumentPathContext(
9
+ const { mapIndices, path: parentPath } = useProteusDocumentPathContext(
10
10
  "@optiaxiom/react/useResolveProteusValues"
11
11
  );
12
12
  const resolved = {};
13
13
  for (const [key, value] of Object.entries(props)) {
14
- resolved[key] = resolveProteusValue(value, data, parentPath);
14
+ resolved[key] = resolveProteusValue(value, data, parentPath, mapIndices);
15
15
  }
16
16
  return resolved;
17
17
  }
@@ -33,7 +33,7 @@ const ProteusElement = ({
33
33
  const { data, strict } = useProteusDocumentContext(
34
34
  "@optiaxiom/proteus/ProteusElement"
35
35
  );
36
- const { path: parentPath } = useProteusDocumentPathContext(
36
+ const { mapIndices, path: parentPath } = useProteusDocumentPathContext(
37
37
  "@optiaxiom/proteus/ProteusElement"
38
38
  );
39
39
  if (!elementProp) {
@@ -55,7 +55,7 @@ const ProteusElement = ({
55
55
  const { $type: _$type, ...rest } = obj;
56
56
  const resolved = {};
57
57
  for (const [key, value] of Object.entries(rest)) {
58
- resolved[key] = resolveProteusProp(value, data, parentPath);
58
+ resolved[key] = resolveProteusProp(value, data, parentPath, mapIndices);
59
59
  }
60
60
  return resolved;
61
61
  };
@@ -6,7 +6,7 @@ import { useProteusValue } from '../use-proteus-value/useProteusValue.js';
6
6
 
7
7
  function ProteusMap({ children, path, separator }) {
8
8
  const { strict } = useProteusDocumentContext("@optiaxiom/proteus/ProteusMap");
9
- const { path: parentPath } = useProteusDocumentPathContext(
9
+ const { mapIndices, path: parentPath } = useProteusDocumentPathContext(
10
10
  "@optiaxiom/proteus/ProteusMap"
11
11
  );
12
12
  const array = useProteusValue({ path });
@@ -21,6 +21,7 @@ function ProteusMap({ children, path, separator }) {
21
21
  return /* @__PURE__ */ jsx(Fragment, { children: array.map((_, index) => /* @__PURE__ */ jsxs(
22
22
  ProteusDocumentPathProvider,
23
23
  {
24
+ mapIndices: [...mapIndices, index],
24
25
  path: `${path.startsWith("/") ? path : `${parentPath}/${path}`}/${index}`,
25
26
  children: [
26
27
  index > 0 && separator,
@@ -6,12 +6,12 @@ import { evaluateCondition } from '../proteus-document/resolveProteusValue.js';
6
6
 
7
7
  function ProteusShow({ children, when }) {
8
8
  const { data } = useProteusDocumentContext("@optiaxiom/proteus/ProteusShow");
9
- const { path: parentPath } = useProteusDocumentPathContext(
9
+ const { mapIndices, path: parentPath } = useProteusDocumentPathContext(
10
10
  "@optiaxiom/proteus/ProteusShow"
11
11
  );
12
12
  const conditions = Array.isArray(when) ? when : [when];
13
13
  const shouldShow = conditions.every(
14
- (condition) => evaluateCondition(condition, data, parentPath)
14
+ (condition) => evaluateCondition(condition, data, parentPath, mapIndices)
15
15
  );
16
16
  if (!shouldShow) {
17
17
  return null;