@kayr/umbika-web-renderer 0.0.0-stage → 0.1.0

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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +46 -2
  3. package/THIRD_PARTY_LICENSES.txt +20 -0
  4. package/dist/browser/umbika.min.css +23 -0
  5. package/dist/browser/umbika.min.d.mts +437 -0
  6. package/dist/browser/umbika.min.d.ts +437 -0
  7. package/dist/browser/umbika.min.js +60 -0
  8. package/dist/browser/umbika.min.mjs +60 -0
  9. package/dist/casconda.d.ts +2 -0
  10. package/dist/casconda.d.ts.map +1 -0
  11. package/dist/contracts.d.ts +165 -0
  12. package/dist/contracts.d.ts.map +1 -0
  13. package/dist/dom-identity.d.ts +17 -0
  14. package/dist/dom-identity.d.ts.map +1 -0
  15. package/dist/field-presentation.d.ts +26 -0
  16. package/dist/field-presentation.d.ts.map +1 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/layout.d.ts +25 -0
  20. package/dist/layout.d.ts.map +1 -0
  21. package/dist/mount.d.ts +14 -0
  22. package/dist/mount.d.ts.map +1 -0
  23. package/dist/render-nodes.d.ts +18 -0
  24. package/dist/render-nodes.d.ts.map +1 -0
  25. package/dist/renderers/multi-select.d.ts +18 -0
  26. package/dist/renderers/multi-select.d.ts.map +1 -0
  27. package/dist/renderers/object.d.ts +6 -0
  28. package/dist/renderers/object.d.ts.map +1 -0
  29. package/dist/renderers/output.d.ts +6 -0
  30. package/dist/renderers/output.d.ts.map +1 -0
  31. package/dist/renderers/select.d.ts +6 -0
  32. package/dist/renderers/select.d.ts.map +1 -0
  33. package/dist/renderers/table.d.ts +6 -0
  34. package/dist/renderers/table.d.ts.map +1 -0
  35. package/dist/renderers/text.d.ts +8 -0
  36. package/dist/renderers/text.d.ts.map +1 -0
  37. package/dist/row-details.d.ts +89 -0
  38. package/dist/row-details.d.ts.map +1 -0
  39. package/dist/standalone/contracts.d.ts +77 -0
  40. package/dist/standalone/contracts.d.ts.map +1 -0
  41. package/dist/standalone/index.d.ts +7 -0
  42. package/dist/standalone/index.d.ts.map +1 -0
  43. package/dist/standalone/mount.d.ts +9 -0
  44. package/dist/standalone/mount.d.ts.map +1 -0
  45. package/dist/summary.d.ts +29 -0
  46. package/dist/summary.d.ts.map +1 -0
  47. package/dist/tags.d.ts +32 -0
  48. package/dist/tags.d.ts.map +1 -0
  49. package/package.json +78 -4
  50. package/src/casconda.js +10 -0
  51. package/src/contracts.js +79 -0
  52. package/src/dom-identity.js +31 -0
  53. package/src/field-presentation.js +71 -0
  54. package/src/index.js +9 -0
  55. package/src/layout.js +176 -0
  56. package/src/mount.js +569 -0
  57. package/src/render-nodes.js +77 -0
  58. package/src/renderers/multi-select.js +211 -0
  59. package/src/renderers/object.js +43 -0
  60. package/src/renderers/output.js +87 -0
  61. package/src/renderers/select.js +104 -0
  62. package/src/renderers/table.js +344 -0
  63. package/src/renderers/text.js +82 -0
  64. package/src/row-details.js +305 -0
  65. package/src/standalone/contracts.js +22 -0
  66. package/src/standalone/index.js +5 -0
  67. package/src/standalone/mount.js +128 -0
  68. package/src/summary.js +135 -0
  69. package/src/tags.js +69 -0
  70. package/styles/base.css +432 -0
  71. package/styles/standalone.css +2 -0
  72. package/styles/themes/default.css +99 -0
  73. package/vendor/LICENSE +21 -0
  74. package/vendor/README.md +33 -0
  75. package/vendor/casconda.min.d.mts +340 -0
  76. package/vendor/casconda.min.d.ts +340 -0
  77. package/vendor/casconda.min.mjs +1 -0
@@ -0,0 +1,71 @@
1
+ import { _fieldDomId } from "./dom-identity.js";
2
+ import { rx } from "./casconda.js";
3
+ import { frag, label, legend, li, p, ul } from "./tags.js";
4
+
5
+ /**
6
+ * @param {string} instanceId
7
+ * @param {import("@kayr/umbika-core").ActiveField} node
8
+ * @param {import("./contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
9
+ */
10
+ export function _fieldPresentation(instanceId, node, resource) {
11
+ const control = _fieldDomId(instanceId, "control", node.path);
12
+ const description = _fieldDomId(instanceId, "description", node.path);
13
+ const errors = _fieldDomId(instanceId, "errors", node.path);
14
+
15
+ return {
16
+ control,
17
+ describedBy: `${description} ${errors}`,
18
+ label: label(
19
+ { class: "umbika-field__label", for: control },
20
+ rx.text(
21
+ resource,
22
+ (
23
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ current,
24
+ ) => (current && "label" in current ? current.label : ""),
25
+ ),
26
+ ),
27
+ legend: legend(
28
+ { class: "umbika-field__label" },
29
+ rx.text(
30
+ resource,
31
+ (
32
+ /** @type {import("@kayr/umbika-core").ActiveFieldNode | undefined} */ current,
33
+ ) => (current && "label" in current ? current.label : ""),
34
+ ),
35
+ ),
36
+ descriptionId: description,
37
+ errors: _renderErrors(errors, resource),
38
+ };
39
+ }
40
+
41
+ /**
42
+ * @param {string} id
43
+ * @param {import("./contracts.js").KeyedItemResource<import("@kayr/umbika-core").ActiveFieldNode>} resource
44
+ */
45
+ export function _renderErrors(id, resource) {
46
+ return ul(
47
+ {
48
+ id,
49
+ class: "umbika-field__errors",
50
+ "aria-live": "polite",
51
+ watch: resource,
52
+ },
53
+ () =>
54
+ frag(...(resource.get()?.errors ?? []).map((error) => li(error.message))),
55
+ );
56
+ }
57
+
58
+ /**
59
+ * @param {string} id
60
+ * @param {string} guidance
61
+ */
62
+ export function _fieldDescription(id, guidance) {
63
+ return p({ id, class: "umbika-field__description" }, guidance);
64
+ }
65
+
66
+ /** @param {unknown} value */
67
+ export function _displayValue(value) {
68
+ return value === null || value === undefined || value === ""
69
+ ? "—"
70
+ : String(value);
71
+ }
package/src/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export { mountWebForm } from "./mount.js";
2
+
3
+ /** @typedef {import("./contracts.js").MountedWebForm} MountedWebForm */
4
+ /** @typedef {import("./contracts.js").MountWebFormOptions} MountWebFormOptions */
5
+ /** @typedef {import("./contracts.js").WebFormLayout} WebFormLayout */
6
+ /** @typedef {import("./contracts.js").WebTableLayout} WebTableLayout */
7
+ /** @typedef {import("./contracts.js").WebTableColumnLayout} WebTableColumnLayout */
8
+ /** @typedef {import("./contracts.js").WebTableMode} WebTableMode */
9
+ /** @typedef {import("./contracts.js").WebTableColumnAlignment} WebTableColumnAlignment */
package/src/layout.js ADDED
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Copy and validate renderer-owned repeat presentation at the mount boundary.
3
+ *
4
+ * @param {unknown} layout
5
+ * @param {readonly import("@kayr/umbika-core").FormNode[]} schema
6
+ * @returns {import("./contracts.js").OwnedWebFormLayout}
7
+ */
8
+ export function _copyWebFormLayout(layout, schema) {
9
+ if (layout === undefined) return Object.freeze({ tables: Object.freeze([]) });
10
+ if (!_isRecord(layout)) throw new TypeError("layout must be an object");
11
+ if (layout.tables !== undefined && !Array.isArray(layout.tables)) {
12
+ throw new TypeError("layout.tables must be an array");
13
+ }
14
+
15
+ const seen = new Set();
16
+ const tables = /** @type {unknown[]} */ (layout.tables ?? []).map(
17
+ (entry, index) => {
18
+ const name = `layout table ${index + 1}`;
19
+ if (!_isRecord(entry)) throw new TypeError(`${name} must be an object`);
20
+ const path = _copyPath(entry.path, `${name} path`);
21
+ const key = _definitionPathKey(path);
22
+ if (seen.has(key))
23
+ throw new TypeError(`${name} duplicates ${_show(path)}`);
24
+ seen.add(key);
25
+
26
+ const table = _schemaNodeAt(schema, path, `${name} path`);
27
+ if (table.type !== "table") {
28
+ throw new TypeError(
29
+ `${name} path ${_show(path)} must identify a table`,
30
+ );
31
+ }
32
+ if (entry.mode !== "cards" && entry.mode !== "table") {
33
+ throw new TypeError(`${name} mode must be "cards" or "table"`);
34
+ }
35
+
36
+ const columns = _copyColumns(entry.columns, table.children, name);
37
+ return Object.freeze({ path, mode: entry.mode, columns });
38
+ },
39
+ );
40
+
41
+ return Object.freeze({ tables: Object.freeze(tables) });
42
+ }
43
+
44
+ /**
45
+ * Validate the mount-scoped theme token. Strings cross the ownership boundary
46
+ * by value, so the returned token cannot be changed through a caller alias.
47
+ *
48
+ * @param {unknown} theme
49
+ */
50
+ export function _copyWebFormTheme(theme) {
51
+ if (theme === undefined) return "default";
52
+ if (typeof theme !== "string" || !/^[A-Za-z][A-Za-z0-9_-]*$/.test(theme)) {
53
+ throw new TypeError(
54
+ "theme must start with a letter and contain only letters, numbers, hyphens, or underscores",
55
+ );
56
+ }
57
+ return theme;
58
+ }
59
+
60
+ /**
61
+ * @param {import("./contracts.js").OwnedWebFormLayout} layout
62
+ * @param {readonly string[]} definitionPath
63
+ */
64
+ export function _tableLayoutAt(layout, definitionPath) {
65
+ const key = _definitionPathKey(definitionPath);
66
+ return layout.tables.find((entry) => _definitionPathKey(entry.path) === key);
67
+ }
68
+
69
+ /**
70
+ * @param {unknown} columns
71
+ * @param {readonly import("@kayr/umbika-core").FormNode[]} rowSchema
72
+ * @param {string} tableName
73
+ */
74
+ function _copyColumns(columns, rowSchema, tableName) {
75
+ if (columns === undefined) return undefined;
76
+ if (!Array.isArray(columns)) {
77
+ throw new TypeError(`${tableName} columns must be an array`);
78
+ }
79
+ if (columns.length === 0 || columns.length > 4) {
80
+ throw new TypeError(
81
+ `${tableName} columns must contain one to four entries`,
82
+ );
83
+ }
84
+
85
+ const seen = new Set();
86
+ return Object.freeze(
87
+ columns.map((entry, index) => {
88
+ const name = `${tableName} column ${index + 1}`;
89
+ if (!_isRecord(entry)) throw new TypeError(`${name} must be an object`);
90
+ const path = _copyPath(entry.path, `${name} path`);
91
+ const key = _definitionPathKey(path);
92
+ if (seen.has(key))
93
+ throw new TypeError(`${name} duplicates ${_show(path)}`);
94
+ seen.add(key);
95
+
96
+ const field = _schemaNodeAt(rowSchema, path, `${name} path`);
97
+ if (field.type === "object" || field.type === "table") {
98
+ throw new TypeError(`${name} must identify a summary field`);
99
+ }
100
+ if (
101
+ entry.label !== undefined &&
102
+ (typeof entry.label !== "string" || entry.label.trim() === "")
103
+ ) {
104
+ throw new TypeError(`${name} label must be a non-empty string`);
105
+ }
106
+ if (
107
+ entry.align !== undefined &&
108
+ entry.align !== "start" &&
109
+ entry.align !== "center" &&
110
+ entry.align !== "end"
111
+ ) {
112
+ throw new TypeError(
113
+ `${name} align must be "start", "center", or "end"`,
114
+ );
115
+ }
116
+
117
+ return Object.freeze({
118
+ key,
119
+ path,
120
+ label: entry.label ?? field.label,
121
+ align: entry.align ?? "start",
122
+ });
123
+ }),
124
+ );
125
+ }
126
+
127
+ /** @param {readonly import("@kayr/umbika-core").FormNode[]} nodes @param {readonly string[]} path @param {string} name */
128
+ function _schemaNodeAt(nodes, path, name) {
129
+ let siblings = nodes;
130
+ /** @type {import("@kayr/umbika-core").FormNode | undefined} */
131
+ let matched;
132
+ for (const segment of path) {
133
+ matched = siblings.find((node) => node.id === segment);
134
+ if (!matched) throw new TypeError(`${name} ${_show(path)} does not exist`);
135
+ siblings =
136
+ matched.type === "object" || matched.type === "table"
137
+ ? matched.children
138
+ : [];
139
+ }
140
+ return /** @type {import("@kayr/umbika-core").FormNode} */ (matched);
141
+ }
142
+
143
+ /** @param {unknown} value @param {string} name @returns {import("@kayr/umbika-core").FieldPath} */
144
+ function _copyPath(value, name) {
145
+ if (
146
+ !Array.isArray(value) ||
147
+ value.length === 0 ||
148
+ value.some(
149
+ (segment) => typeof segment !== "string" || segment.trim() === "",
150
+ )
151
+ ) {
152
+ throw new TypeError(
153
+ `${name} must be a non-empty path of non-empty strings`,
154
+ );
155
+ }
156
+ return Object.freeze(
157
+ /** @type {import("@kayr/umbika-core").FieldPath} */ (
158
+ /** @type {unknown} */ ([...value])
159
+ ),
160
+ );
161
+ }
162
+
163
+ /** @param {readonly string[]} path */
164
+ function _definitionPathKey(path) {
165
+ return path.map((segment) => `${segment.length}:${segment}`).join("|");
166
+ }
167
+
168
+ /** @param {readonly string[]} path */
169
+ function _show(path) {
170
+ return JSON.stringify(path);
171
+ }
172
+
173
+ /** @param {unknown} value @returns {value is Record<string, any>} */
174
+ function _isRecord(value) {
175
+ return typeof value === "object" && value !== null && !Array.isArray(value);
176
+ }