@elastic/isomer-sdk 0.1.0 → 0.3.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.
- package/NOTICE.txt +0 -286
- package/README.md +13 -2
- package/THIRD_PARTY_LICENSES.md +1 -885
- package/dist/author/authored_fields.d.ts +93 -18
- package/dist/author/authored_fields.d.ts.map +1 -1
- package/dist/author/authored_fields.js +63 -43
- package/dist/author/authored_fields.js.map +1 -1
- package/dist/author/authoring_input.d.ts +7 -0
- package/dist/author/authoring_input.d.ts.map +1 -0
- package/dist/author/authoring_input.js +66 -0
- package/dist/author/authoring_input.js.map +1 -0
- package/dist/author/authoring_model.d.ts +33 -0
- package/dist/author/authoring_model.d.ts.map +1 -0
- package/dist/author/authoring_model.js +142 -0
- package/dist/author/authoring_model.js.map +1 -0
- package/dist/author/declarations.d.ts +13 -0
- package/dist/author/declarations.d.ts.map +1 -0
- package/dist/author/declarations.js +288 -0
- package/dist/author/declarations.js.map +1 -0
- package/dist/author/index.d.ts +4 -1
- package/dist/author/index.d.ts.map +1 -1
- package/dist/author/index.js +4 -1
- package/dist/author/index.js.map +1 -1
- package/dist/author/jsx_shim.d.ts +50 -30
- package/dist/author/jsx_shim.d.ts.map +1 -1
- package/dist/author/jsx_shim.js +146 -138
- package/dist/author/jsx_shim.js.map +1 -1
- package/dist/author/print_jsx.d.ts +19 -0
- package/dist/author/print_jsx.d.ts.map +1 -0
- package/dist/author/print_jsx.js +372 -0
- package/dist/author/print_jsx.js.map +1 -0
- package/dist/author/prompt.d.ts +2 -2
- package/dist/author/prompt.d.ts.map +1 -1
- package/dist/author/prompt.js +7 -2
- package/dist/author/prompt.js.map +1 -1
- package/dist/author/schema_to_typescript.d.ts +37 -0
- package/dist/author/schema_to_typescript.d.ts.map +1 -0
- package/dist/author/schema_to_typescript.js +289 -0
- package/dist/author/schema_to_typescript.js.map +1 -0
- package/dist/cjs/author/authored_fields.js +68 -47
- package/dist/cjs/author/authored_fields.js.map +1 -1
- package/dist/cjs/author/authoring_input.js +70 -0
- package/dist/cjs/author/authoring_input.js.map +1 -0
- package/dist/cjs/author/authoring_model.js +149 -0
- package/dist/cjs/author/authoring_model.js.map +1 -0
- package/dist/cjs/author/declarations.js +292 -0
- package/dist/cjs/author/declarations.js.map +1 -0
- package/dist/cjs/author/index.js +10 -1
- package/dist/cjs/author/index.js.map +1 -1
- package/dist/cjs/author/jsx_shim.js +148 -140
- package/dist/cjs/author/jsx_shim.js.map +1 -1
- package/dist/cjs/author/print_jsx.js +378 -0
- package/dist/cjs/author/print_jsx.js.map +1 -0
- package/dist/cjs/author/prompt.js +7 -2
- package/dist/cjs/author/prompt.js.map +1 -1
- package/dist/cjs/author/schema_to_typescript.js +298 -0
- package/dist/cjs/author/schema_to_typescript.js.map +1 -0
- package/dist/cjs/composition/body_node_base.js +11 -4
- package/dist/cjs/composition/body_node_base.js.map +1 -1
- package/dist/cjs/composition/error.js +2 -0
- package/dist/cjs/composition/error.js.map +1 -1
- package/dist/cjs/composition/index.js +2 -1
- package/dist/cjs/composition/index.js.map +1 -1
- package/dist/cjs/composition/structured_value.js.map +1 -1
- package/dist/cjs/define/frame.js +2 -3
- package/dist/cjs/define/frame.js.map +1 -1
- package/dist/cjs/define/index.js +7 -1
- package/dist/cjs/define/index.js.map +1 -1
- package/dist/cjs/define/primitive_example.js +26 -0
- package/dist/cjs/define/primitive_example.js.map +1 -0
- package/dist/cjs/define/primitive_icon.js +17 -0
- package/dist/cjs/define/primitive_icon.js.map +1 -0
- package/dist/cjs/define/primitive_module.js +24 -7
- package/dist/cjs/define/primitive_module.js.map +1 -1
- package/dist/cjs/define/slack_assets.js.map +1 -1
- package/dist/cjs/entries/index.js +8 -2
- package/dist/cjs/entries/index.js.map +1 -1
- package/dist/cjs/entries/markdown.js +2 -1
- package/dist/cjs/entries/markdown.js.map +1 -1
- package/dist/cjs/entries/slack.js +1 -2
- package/dist/cjs/entries/slack.js.map +1 -1
- package/dist/cjs/pack/capabilities.js +17 -11
- package/dist/cjs/pack/capabilities.js.map +1 -1
- package/dist/cjs/pack/compose.js +4 -1
- package/dist/cjs/pack/compose.js.map +1 -1
- package/dist/cjs/pack/index.js +2 -1
- package/dist/cjs/pack/index.js.map +1 -1
- package/dist/cjs/pack/primitive_pack.js +27 -5
- package/dist/cjs/pack/primitive_pack.js.map +1 -1
- package/dist/cjs/render/anchors.js +1 -1
- package/dist/cjs/render/anchors.js.map +1 -1
- package/dist/cjs/render/format_number.js +2 -1
- package/dist/cjs/render/format_number.js.map +1 -1
- package/dist/cjs/render/html/envelope.js +12 -6
- package/dist/cjs/render/html/envelope.js.map +1 -1
- package/dist/cjs/render/layout_check.js.map +1 -1
- package/dist/cjs/render/markdown/builder.js +18 -8
- package/dist/cjs/render/markdown/builder.js.map +1 -1
- package/dist/cjs/render/markdown/format.js +70 -30
- package/dist/cjs/render/markdown/format.js.map +1 -1
- package/dist/cjs/render/markdown/host_tags.js +67 -0
- package/dist/cjs/render/markdown/host_tags.js.map +1 -0
- package/dist/cjs/render/markdown/index.js +3 -1
- package/dist/cjs/render/markdown/index.js.map +1 -1
- package/dist/cjs/render/markdown/split.js +325 -0
- package/dist/cjs/render/markdown/split.js.map +1 -0
- package/dist/cjs/render/primitive_dispatch.js +11 -25
- package/dist/cjs/render/primitive_dispatch.js.map +1 -1
- package/dist/cjs/render/react/content.js +3 -1
- package/dist/cjs/render/react/content.js.map +1 -1
- package/dist/cjs/render/slack/format.js +1 -601
- package/dist/cjs/render/slack/format.js.map +1 -1
- package/dist/cjs/render/slack/index.js +1 -2
- package/dist/cjs/render/slack/index.js.map +1 -1
- package/dist/cjs/render/slack/markdown_content.js +172 -115
- package/dist/cjs/render/slack/markdown_content.js.map +1 -1
- package/dist/cjs/testing/conformance.js +40 -24
- package/dist/cjs/testing/conformance.js.map +1 -1
- package/dist/cjs/testing/icon.js +248 -0
- package/dist/cjs/testing/icon.js.map +1 -0
- package/dist/cjs/testing/index.js +3 -1
- package/dist/cjs/testing/index.js.map +1 -1
- package/dist/cjs/testing/registration.js +5 -3
- package/dist/cjs/testing/registration.js.map +1 -1
- package/dist/cjs/validate/authoring_schema.js +17 -1
- package/dist/cjs/validate/authoring_schema.js.map +1 -1
- package/dist/cjs/validate/index.js +2 -1
- package/dist/cjs/validate/index.js.map +1 -1
- package/dist/cjs/validate/json_schema.js +10 -3
- package/dist/cjs/validate/json_schema.js.map +1 -1
- package/dist/cjs/validate/url.js +2 -1
- package/dist/cjs/validate/url.js.map +1 -1
- package/dist/cjs/validate/validation.js +2 -45
- package/dist/cjs/validate/validation.js.map +1 -1
- package/dist/composition/body_node_base.d.ts +7 -6
- package/dist/composition/body_node_base.d.ts.map +1 -1
- package/dist/composition/body_node_base.js +10 -3
- package/dist/composition/body_node_base.js.map +1 -1
- package/dist/composition/error.d.ts +2 -0
- package/dist/composition/error.d.ts.map +1 -1
- package/dist/composition/error.js +2 -0
- package/dist/composition/error.js.map +1 -1
- package/dist/composition/index.d.ts +1 -1
- package/dist/composition/index.d.ts.map +1 -1
- package/dist/composition/index.js +1 -1
- package/dist/composition/index.js.map +1 -1
- package/dist/composition/structured_value.d.ts +3 -3
- package/dist/composition/structured_value.d.ts.map +1 -1
- package/dist/composition/structured_value.js.map +1 -1
- package/dist/define/frame.d.ts +9 -20
- package/dist/define/frame.d.ts.map +1 -1
- package/dist/define/frame.js +2 -3
- package/dist/define/frame.js.map +1 -1
- package/dist/define/index.d.ts +3 -1
- package/dist/define/index.d.ts.map +1 -1
- package/dist/define/index.js +3 -1
- package/dist/define/index.js.map +1 -1
- package/dist/define/primitive_example.d.ts +30 -0
- package/dist/define/primitive_example.d.ts.map +1 -0
- package/dist/define/primitive_example.js +21 -0
- package/dist/define/primitive_example.js.map +1 -0
- package/dist/define/primitive_icon.d.ts +11 -0
- package/dist/define/primitive_icon.d.ts.map +1 -0
- package/dist/define/primitive_icon.js +14 -0
- package/dist/define/primitive_icon.js.map +1 -0
- package/dist/define/primitive_module.d.ts +45 -24
- package/dist/define/primitive_module.d.ts.map +1 -1
- package/dist/define/primitive_module.js +24 -7
- package/dist/define/primitive_module.js.map +1 -1
- package/dist/define/slack_assets.d.ts +1 -1
- package/dist/define/slack_assets.d.ts.map +1 -1
- package/dist/define/slack_assets.js.map +1 -1
- package/dist/entries/index.d.ts +3 -3
- package/dist/entries/index.d.ts.map +1 -1
- package/dist/entries/index.js +3 -3
- package/dist/entries/index.js.map +1 -1
- package/dist/entries/markdown.d.ts +1 -1
- package/dist/entries/markdown.d.ts.map +1 -1
- package/dist/entries/markdown.js +1 -1
- package/dist/entries/markdown.js.map +1 -1
- package/dist/entries/slack.d.ts +1 -1
- package/dist/entries/slack.d.ts.map +1 -1
- package/dist/entries/slack.js +1 -1
- package/dist/entries/slack.js.map +1 -1
- package/dist/pack/capabilities.d.ts +24 -10
- package/dist/pack/capabilities.d.ts.map +1 -1
- package/dist/pack/capabilities.js +17 -11
- package/dist/pack/capabilities.js.map +1 -1
- package/dist/pack/compose.d.ts +4 -1
- package/dist/pack/compose.d.ts.map +1 -1
- package/dist/pack/compose.js +4 -1
- package/dist/pack/compose.js.map +1 -1
- package/dist/pack/index.d.ts +2 -2
- package/dist/pack/index.d.ts.map +1 -1
- package/dist/pack/index.js +2 -2
- package/dist/pack/index.js.map +1 -1
- package/dist/pack/primitive_pack.d.ts +14 -3
- package/dist/pack/primitive_pack.d.ts.map +1 -1
- package/dist/pack/primitive_pack.js +25 -4
- package/dist/pack/primitive_pack.js.map +1 -1
- package/dist/render/anchors.d.ts +2 -2
- package/dist/render/anchors.d.ts.map +1 -1
- package/dist/render/anchors.js +1 -1
- package/dist/render/anchors.js.map +1 -1
- package/dist/render/format_number.d.ts.map +1 -1
- package/dist/render/format_number.js +2 -1
- package/dist/render/format_number.js.map +1 -1
- package/dist/render/html/envelope.d.ts +3 -3
- package/dist/render/html/envelope.js +12 -6
- package/dist/render/html/envelope.js.map +1 -1
- package/dist/render/layout_check.d.ts +1 -1
- package/dist/render/layout_check.d.ts.map +1 -1
- package/dist/render/layout_check.js.map +1 -1
- package/dist/render/markdown/builder.d.ts +5 -4
- package/dist/render/markdown/builder.d.ts.map +1 -1
- package/dist/render/markdown/builder.js +16 -7
- package/dist/render/markdown/builder.js.map +1 -1
- package/dist/render/markdown/format.d.ts +9 -0
- package/dist/render/markdown/format.d.ts.map +1 -1
- package/dist/render/markdown/format.js +67 -31
- package/dist/render/markdown/format.js.map +1 -1
- package/dist/render/markdown/host_tags.d.ts +14 -0
- package/dist/render/markdown/host_tags.d.ts.map +1 -0
- package/dist/render/markdown/host_tags.js +63 -0
- package/dist/render/markdown/host_tags.js.map +1 -0
- package/dist/render/markdown/index.d.ts +1 -0
- package/dist/render/markdown/index.d.ts.map +1 -1
- package/dist/render/markdown/index.js +1 -0
- package/dist/render/markdown/index.js.map +1 -1
- package/dist/render/markdown/split.d.ts +32 -0
- package/dist/render/markdown/split.d.ts.map +1 -0
- package/dist/render/markdown/split.js +321 -0
- package/dist/render/markdown/split.js.map +1 -0
- package/dist/render/primitive_dispatch.d.ts +6 -6
- package/dist/render/primitive_dispatch.d.ts.map +1 -1
- package/dist/render/primitive_dispatch.js +12 -26
- package/dist/render/primitive_dispatch.js.map +1 -1
- package/dist/render/react/content.d.ts +1 -1
- package/dist/render/react/content.d.ts.map +1 -1
- package/dist/render/react/content.js +3 -1
- package/dist/render/react/content.js.map +1 -1
- package/dist/render/slack/format.d.ts +0 -25
- package/dist/render/slack/format.d.ts.map +1 -1
- package/dist/render/slack/format.js +1 -599
- package/dist/render/slack/format.js.map +1 -1
- package/dist/render/slack/index.d.ts +1 -1
- package/dist/render/slack/index.d.ts.map +1 -1
- package/dist/render/slack/index.js +1 -1
- package/dist/render/slack/index.js.map +1 -1
- package/dist/render/slack/markdown_content.d.ts +10 -4
- package/dist/render/slack/markdown_content.d.ts.map +1 -1
- package/dist/render/slack/markdown_content.js +169 -113
- package/dist/render/slack/markdown_content.js.map +1 -1
- package/dist/testing/conformance.d.ts +19 -14
- package/dist/testing/conformance.d.ts.map +1 -1
- package/dist/testing/conformance.js +40 -24
- package/dist/testing/conformance.js.map +1 -1
- package/dist/testing/icon.d.ts +11 -0
- package/dist/testing/icon.d.ts.map +1 -0
- package/dist/testing/icon.js +244 -0
- package/dist/testing/icon.js.map +1 -0
- package/dist/testing/index.d.ts +2 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -0
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/registration.d.ts +1 -1
- package/dist/testing/registration.d.ts.map +1 -1
- package/dist/testing/registration.js +6 -4
- package/dist/testing/registration.js.map +1 -1
- package/dist/validate/authoring_schema.d.ts +6 -0
- package/dist/validate/authoring_schema.d.ts.map +1 -1
- package/dist/validate/authoring_schema.js +15 -0
- package/dist/validate/authoring_schema.js.map +1 -1
- package/dist/validate/index.d.ts +1 -1
- package/dist/validate/index.d.ts.map +1 -1
- package/dist/validate/index.js +1 -1
- package/dist/validate/index.js.map +1 -1
- package/dist/validate/json_schema.d.ts.map +1 -1
- package/dist/validate/json_schema.js +10 -3
- package/dist/validate/json_schema.js.map +1 -1
- package/dist/validate/url.d.ts.map +1 -1
- package/dist/validate/url.js +2 -1
- package/dist/validate/url.js.map +1 -1
- package/dist/validate/validation.d.ts +1 -11
- package/dist/validate/validation.d.ts.map +1 -1
- package/dist/validate/validation.js +3 -46
- package/dist/validate/validation.js.map +1 -1
- package/package.json +3 -2
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* 2.0.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.readAuthoredSpec = exports.fromTextChildren = exports.authoredText = exports.authoredProps = exports.authoredChild = void 0;
|
|
9
|
+
exports.readAuthoredSpec = exports.fromNodeChildren = exports.fromTextChildren = exports.authoredToItem = exports.authoredNodes = exports.authoredText = exports.authoredProps = exports.authoredChild = void 0;
|
|
10
10
|
exports.fromChildren = fromChildren;
|
|
11
11
|
const error_1 = require("../composition/error");
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Member name of the child component a schema field is filled from.
|
|
14
14
|
*
|
|
15
15
|
* `Symbol.for`: ESM and CJS builds must agree, same as `authorType`.
|
|
16
16
|
*/
|
|
@@ -19,23 +19,32 @@ exports.authoredChild = Symbol.for('elastic.isomer.authored_child');
|
|
|
19
19
|
exports.authoredProps = Symbol.for('elastic.isomer.authored_props');
|
|
20
20
|
/** Set when a field is filled from text children. */
|
|
21
21
|
exports.authoredText = Symbol.for('elastic.isomer.authored_text');
|
|
22
|
-
|
|
22
|
+
/** Set when a field is filled from primitive elements no other field claims. */
|
|
23
|
+
exports.authoredNodes = Symbol.for('elastic.isomer.authored_nodes');
|
|
24
|
+
/** Holds a {@link fromChildren} brand's `toItem`. */
|
|
25
|
+
exports.authoredToItem = Symbol.for('elastic.isomer.authored_to_item');
|
|
23
26
|
const authoredTextField = Symbol.for('elastic.isomer.authored_text_field');
|
|
24
27
|
const authoredCollapse = Symbol.for('elastic.isomer.authored_collapse');
|
|
28
|
+
const authoredDedent = Symbol.for('elastic.isomer.authored_dedent');
|
|
29
|
+
const authoredPropsSchema = Symbol.for('elastic.isomer.authored_props_schema');
|
|
25
30
|
const BRANDS = [
|
|
26
31
|
exports.authoredChild,
|
|
27
32
|
authoredTextField,
|
|
28
|
-
authoredToItem,
|
|
33
|
+
exports.authoredToItem,
|
|
29
34
|
exports.authoredText,
|
|
35
|
+
exports.authoredNodes,
|
|
30
36
|
authoredCollapse,
|
|
37
|
+
authoredDedent,
|
|
38
|
+
authoredPropsSchema,
|
|
31
39
|
];
|
|
32
40
|
// Applies a complete branding. Branding an instance again is allowed only with
|
|
33
41
|
// the same configuration, absent options included, since both fields share it.
|
|
34
42
|
const brand = (caller, schema, config) => {
|
|
35
43
|
const record = schema;
|
|
36
44
|
if (BRANDS.some((key) => Object.hasOwn(schema, key))) {
|
|
37
|
-
if (BRANDS.every((key) => record[key] === config[key]))
|
|
45
|
+
if (BRANDS.every((key) => record[key] === config[key])) {
|
|
38
46
|
return;
|
|
47
|
+
}
|
|
39
48
|
throw new error_1.IsomerError(error_1.ISOMER_ERROR_CODES.AUTHORED_SCHEMA_REUSED, `${caller}: this schema instance is already branded another way; pass each field its own schema.`);
|
|
40
49
|
}
|
|
41
50
|
for (const key of BRANDS) {
|
|
@@ -47,27 +56,42 @@ const brand = (caller, schema, config) => {
|
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
};
|
|
50
|
-
function fromChildren(
|
|
59
|
+
function fromChildren(name, schema, options) {
|
|
51
60
|
brand('fromChildren', schema, {
|
|
52
|
-
[exports.authoredChild]:
|
|
61
|
+
[exports.authoredChild]: name,
|
|
53
62
|
[authoredTextField]: options?.text,
|
|
54
|
-
[authoredToItem]: options?.toItem,
|
|
63
|
+
[exports.authoredToItem]: options?.toItem,
|
|
64
|
+
[authoredPropsSchema]: options?.propsSchema,
|
|
55
65
|
});
|
|
56
66
|
return schema;
|
|
57
67
|
}
|
|
58
68
|
/**
|
|
59
69
|
* Brands `schema` as text children.
|
|
60
70
|
*
|
|
61
|
-
* Whitespace collapses unless `collapseWhitespace` is `false`.
|
|
71
|
+
* Whitespace collapses unless `collapseWhitespace` is `false`. `dedent` keeps
|
|
72
|
+
* it and reads the text as source, as {@link textFromChildren} does.
|
|
62
73
|
*/
|
|
63
74
|
const fromTextChildren = (schema, options) => {
|
|
64
75
|
brand('fromTextChildren', schema, {
|
|
65
76
|
[exports.authoredText]: true,
|
|
66
|
-
[authoredCollapse]: options?.collapseWhitespace === false
|
|
77
|
+
[authoredCollapse]: options?.collapseWhitespace === false || options?.dedent
|
|
78
|
+
? false
|
|
79
|
+
: undefined,
|
|
80
|
+
[authoredDedent]: options?.dedent ? true : undefined,
|
|
67
81
|
});
|
|
68
82
|
return schema;
|
|
69
83
|
};
|
|
70
84
|
exports.fromTextChildren = fromTextChildren;
|
|
85
|
+
/**
|
|
86
|
+
* Brands `schema`, a node or an array of nodes, as the primitive elements among
|
|
87
|
+
* the children that no other field claims. Use it for nodes the tree walk does
|
|
88
|
+
* not visit; a walked child slot fills from children already.
|
|
89
|
+
*/
|
|
90
|
+
const fromNodeChildren = (schema) => {
|
|
91
|
+
brand('fromNodeChildren', schema, { [exports.authoredNodes]: true });
|
|
92
|
+
return schema;
|
|
93
|
+
};
|
|
94
|
+
exports.fromNodeChildren = fromNodeChildren;
|
|
71
95
|
const zodDefType = (schema) => schema._zod?.def?.type;
|
|
72
96
|
const WRAPPER_TYPES = new Set(['optional', 'nullable', 'default', 'readonly']);
|
|
73
97
|
const innerSchema = (schema) => {
|
|
@@ -85,8 +109,9 @@ const brandHolder = (schema, key) => {
|
|
|
85
109
|
// Whether any wrapper layer of `schema` accepts `undefined`.
|
|
86
110
|
const optionalThrough = (schema) => {
|
|
87
111
|
for (let current = schema; current; current = innerSchema(current)) {
|
|
88
|
-
if (isOptionalSchema(current))
|
|
112
|
+
if (isOptionalSchema(current)) {
|
|
89
113
|
return true;
|
|
114
|
+
}
|
|
90
115
|
}
|
|
91
116
|
return false;
|
|
92
117
|
};
|
|
@@ -102,50 +127,33 @@ const isOptionalSchema = (schema) => {
|
|
|
102
127
|
const type = zodDefType(schema);
|
|
103
128
|
return type === 'optional' || type === 'default';
|
|
104
129
|
};
|
|
105
|
-
const schemaSignature = (schema, seen = new Set()) => {
|
|
106
|
-
if (seen.has(schema)) {
|
|
107
|
-
return 'cycle';
|
|
108
|
-
}
|
|
109
|
-
seen.add(schema);
|
|
110
|
-
const type = zodDefType(schema) ?? '?';
|
|
111
|
-
if (type === 'object' && 'shape' in schema) {
|
|
112
|
-
const shape = schema.shape;
|
|
113
|
-
const fields = Object.keys(shape)
|
|
114
|
-
.sort()
|
|
115
|
-
.map((key) => `${key}:${schemaSignature(shape[key], seen)}`);
|
|
116
|
-
return `{${fields.join(',')}}`;
|
|
117
|
-
}
|
|
118
|
-
if (type === 'array' && 'element' in schema) {
|
|
119
|
-
return `[${schemaSignature(schema.element, seen)}]`;
|
|
120
|
-
}
|
|
121
|
-
const inner = innerSchema(schema);
|
|
122
|
-
if (inner) {
|
|
123
|
-
return `${type}(${schemaSignature(inner, seen)})`;
|
|
124
|
-
}
|
|
125
|
-
return type;
|
|
126
|
-
};
|
|
127
130
|
const arrayElement = (schema) => {
|
|
128
131
|
const inner = unwrapAll(schema);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return inner;
|
|
132
|
+
return zodDefType(inner) === 'array' && 'element' in inner
|
|
133
|
+
? inner.element
|
|
134
|
+
: undefined;
|
|
133
135
|
};
|
|
134
136
|
const readChild = (field, schema) => {
|
|
135
137
|
const holder = brandHolder(schema, exports.authoredChild);
|
|
136
|
-
const
|
|
137
|
-
if (!holder || typeof
|
|
138
|
+
const name = holder?.[exports.authoredChild];
|
|
139
|
+
if (!holder || typeof name !== 'string') {
|
|
138
140
|
return undefined;
|
|
139
141
|
}
|
|
140
142
|
const textField = holder[authoredTextField];
|
|
141
|
-
const toItem = holder[authoredToItem];
|
|
143
|
+
const toItem = holder[exports.authoredToItem];
|
|
144
|
+
const propsSchema = holder[authoredPropsSchema];
|
|
145
|
+
const element = arrayElement(holder);
|
|
146
|
+
const itemSchema = element ?? unwrapAll(holder);
|
|
142
147
|
const child = {
|
|
143
148
|
field,
|
|
144
|
-
|
|
145
|
-
itemSchema
|
|
149
|
+
name,
|
|
150
|
+
itemSchema,
|
|
151
|
+
array: element !== undefined,
|
|
146
152
|
optional: optionalThrough(schema),
|
|
147
|
-
signature: schemaSignature(arrayElement(holder)),
|
|
148
153
|
};
|
|
154
|
+
if (propsSchema) {
|
|
155
|
+
child.propsSchema = propsSchema;
|
|
156
|
+
}
|
|
149
157
|
if (typeof textField === 'string') {
|
|
150
158
|
child.textField = textField;
|
|
151
159
|
}
|
|
@@ -159,19 +167,27 @@ const readText = (field, schema) => {
|
|
|
159
167
|
if (!holder) {
|
|
160
168
|
return undefined;
|
|
161
169
|
}
|
|
162
|
-
|
|
170
|
+
const text = {
|
|
163
171
|
field,
|
|
164
172
|
collapseWhitespace: holder[authoredCollapse] !== false,
|
|
165
173
|
optional: optionalThrough(schema),
|
|
166
174
|
};
|
|
175
|
+
if (holder[authoredDedent] === true) {
|
|
176
|
+
text.dedent = true;
|
|
177
|
+
}
|
|
178
|
+
return text;
|
|
167
179
|
};
|
|
168
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* The branded fields on an object schema, top level only: read a child's own
|
|
182
|
+
* brands from its {@link AuthoredChildField.itemSchema}.
|
|
183
|
+
*/
|
|
169
184
|
const readAuthoredSpec = (schema) => {
|
|
170
185
|
if (zodDefType(schema) !== 'object' || !('shape' in schema)) {
|
|
171
|
-
return { children: [], text: [] };
|
|
186
|
+
return { children: [], text: [], nodes: [] };
|
|
172
187
|
}
|
|
173
188
|
const children = [];
|
|
174
189
|
const text = [];
|
|
190
|
+
const nodes = [];
|
|
175
191
|
for (const [field, fieldSchema] of Object.entries(schema.shape)) {
|
|
176
192
|
const child = readChild(field, fieldSchema);
|
|
177
193
|
if (child) {
|
|
@@ -181,9 +197,14 @@ const readAuthoredSpec = (schema) => {
|
|
|
181
197
|
const textField = readText(field, fieldSchema);
|
|
182
198
|
if (textField) {
|
|
183
199
|
text.push(textField);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const holder = brandHolder(fieldSchema, exports.authoredNodes);
|
|
203
|
+
if (holder) {
|
|
204
|
+
nodes.push({ field, array: arrayElement(holder) !== undefined });
|
|
184
205
|
}
|
|
185
206
|
}
|
|
186
|
-
return { children, text };
|
|
207
|
+
return { children, text, nodes };
|
|
187
208
|
};
|
|
188
209
|
exports.readAuthoredSpec = readAuthoredSpec;
|
|
189
210
|
//# sourceMappingURL=authored_fields.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authored_fields.js","sourceRoot":"","sources":["../../../src/author/authored_fields.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAyIH,oCAcC;AAlJD,gDAAuE;AAGvE;;;;GAIG;AACU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AAG,CAAC,MAAe,EAAgB,EAAE;IAChE,IAAI,UAAU,CAAC,MAAM,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,OAAO,IAAI,MAAM,CAAC,EAAE,CAAC;QAC5D,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IACpC,CAAC;IACD,MAAM,QAAQ,GAAyB,EAAE,CAAC;IAC1C,MAAM,IAAI,GAAwB,EAAE,CAAC;IACrC,KAAK,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,IAAI,MAAM,CAAC,OAAO,CAC/C,MAAM,CAAC,KAAgC,CACxC,EAAE,CAAC;QACF,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QAC5C,IAAI,KAAK,EAAE,CAAC;YACV,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,SAAS;QACX,CAAC;QACD,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QAC/C,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACvB,CAAC;IACH,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAC5B,CAAC,CAAC;AApBW,QAAA,gBAAgB,oBAoB3B","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport type { ReactNode } from 'react';\nimport type { ZodType } from 'zod';\n\nimport { ISOMER_ERROR_CODES, IsomerError } from '../composition/error';\nimport type { PrimitiveNode } from '../define/primitive_module';\n\n/**\n * Child-element type a schema field is filled from.\n *\n * `Symbol.for`: ESM and CJS builds must agree, same as `authorType`.\n */\nexport const authoredChild = Symbol.for('elastic.isomer.authored_child');\n\n/** Phantom props type for the child component. Not set at runtime. */\nexport const authoredProps = Symbol.for('elastic.isomer.authored_props');\n\n/** Set when a field is filled from text children. */\nexport const authoredText = Symbol.for('elastic.isomer.authored_text');\n\nconst authoredToItem = Symbol.for('elastic.isomer.authored_to_item');\nconst authoredTextField = Symbol.for('elastic.isomer.authored_text_field');\nconst authoredCollapse = Symbol.for('elastic.isomer.authored_collapse');\n\n/**\n * A field filled from child elements of `TName`.\n *\n * Phantom: `z.infer` still reads the underlying schema. The shim reads `TName`\n * and `TProps` off the field type, and the symbols off the schema value.\n */\nexport interface AuthoredChildBrand<TName extends string, TProps> {\n readonly [authoredChild]: TName;\n readonly [authoredProps]: TProps;\n}\n\n/** A field filled from text children. */\nexport interface AuthoredTextBrand {\n readonly [authoredText]: true;\n}\n\n/** What {@link fromChildren}'s `toItem` receives. */\nexport interface AuthorChildContext {\n /** Nested elements, parsed as body nodes. */\n parseChildren: (children: ReactNode) => PrimitiveNode[];\n}\n\ntype ArrayItem<TSchema extends ZodType> =\n NonNullable<zOutput<TSchema>> extends readonly (infer Item)[]\n ? Item\n : NonNullable<zOutput<TSchema>>;\n\ntype zOutput<TSchema> = TSchema extends { _zod: { output: infer Output } }\n ? Output\n : never;\n\n/** `TText` is optional on the child props because leftover text fills it. */\ntype ChildProps<TItem, TText extends string | undefined> = [TText] extends [\n string,\n]\n ? Omit<TItem, TText> &\n Partial<Pick<TItem, Extract<TText, keyof TItem>>> & {\n children?: ReactNode;\n }\n : TItem & { children?: ReactNode };\n\nconst BRANDS = [\n authoredChild,\n authoredTextField,\n authoredToItem,\n authoredText,\n authoredCollapse,\n] as const;\n\n// Applies a complete branding. Branding an instance again is allowed only with\n// the same configuration, absent options included, since both fields share it.\nconst brand = (\n caller: 'fromChildren' | 'fromTextChildren',\n schema: object,\n config: Partial<Record<(typeof BRANDS)[number], unknown>>\n): void => {\n const record = schema as Record<symbol, unknown>;\n if (BRANDS.some((key) => Object.hasOwn(schema, key))) {\n if (BRANDS.every((key) => record[key] === config[key])) return;\n throw new IsomerError(\n ISOMER_ERROR_CODES.AUTHORED_SCHEMA_REUSED,\n `${caller}: this schema instance is already branded another way; pass each field its own schema.`\n );\n }\n for (const key of BRANDS) {\n if (config[key] !== undefined) {\n Object.defineProperty(schema, key, {\n value: config[key],\n enumerable: false,\n });\n }\n }\n};\n\n/**\n * Brands `schema` as JSX children of `childType`.\n *\n * `options.text` copies leftover text onto that field. `options.toItem` replaces\n * the default prop copy; its props annotation is the child component's props.\n * The brand is read through `.optional()`, `.nullable()`, `.default()`, and\n * `.readonly()`; any other method clones without it, so call it on `schema`\n * before passing it in.\n */\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n>(\n childType: TName,\n schema: TSchema\n): TSchema &\n AuthoredChildBrand<TName, ChildProps<ArrayItem<TSchema>, undefined>>;\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n const TText extends string,\n>(\n childType: TName,\n schema: TSchema,\n options: { text: TText }\n): TSchema & AuthoredChildBrand<TName, ChildProps<ArrayItem<TSchema>, TText>>;\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n TProps,\n>(\n childType: TName,\n schema: TSchema,\n options: {\n text?: string;\n toItem(props: TProps, context: AuthorChildContext): unknown;\n }\n): TSchema & AuthoredChildBrand<TName, TProps>;\nexport function fromChildren(\n childType: string,\n schema: ZodType,\n options?: {\n text?: string;\n toItem?: (this: void, props: never, context: AuthorChildContext) => unknown;\n }\n): ZodType {\n brand('fromChildren', schema, {\n [authoredChild]: childType,\n [authoredTextField]: options?.text,\n [authoredToItem]: options?.toItem,\n });\n return schema;\n}\n\n/**\n * Brands `schema` as text children.\n *\n * Whitespace collapses unless `collapseWhitespace` is `false`.\n */\nexport const fromTextChildren = <TSchema extends ZodType>(\n schema: TSchema,\n options?: { collapseWhitespace?: boolean }\n): TSchema & AuthoredTextBrand => {\n brand('fromTextChildren', schema, {\n [authoredText]: true,\n [authoredCollapse]:\n options?.collapseWhitespace === false ? false : undefined,\n });\n return schema as TSchema & AuthoredTextBrand;\n};\n\n/** One {@link fromChildren} field as the shim reads it. */\nexport interface AuthoredChildField {\n field: string;\n childType: string;\n /** Field leftover text children fill. */\n textField?: string;\n /** Replaces the default prop copy. */\n toItem?: (props: object, context: AuthorChildContext) => unknown;\n /** The array element schema, or the field schema itself when it is not an array. */\n itemSchema: ZodType;\n /** Whether the field accepts `undefined`, so children may be omitted. */\n optional: boolean;\n /** Structural fingerprint of `itemSchema`; every field branding one child type must share it. */\n signature: string;\n}\n\n/** One {@link fromTextChildren} field as the shim reads it. */\nexport interface AuthoredTextField {\n field: string;\n /** `true` unless the brand was given `collapseWhitespace: false`. */\n collapseWhitespace: boolean;\n /** Whether the field accepts `undefined`, so text children may be omitted. */\n optional: boolean;\n}\n\n/** The branded fields found on one object schema. */\nexport interface AuthoredSpec {\n children: AuthoredChildField[];\n text: AuthoredTextField[];\n}\n\nconst zodDefType = (schema: object): string | undefined =>\n (schema as { _zod?: { def?: { type?: string } } })._zod?.def?.type;\n\nconst WRAPPER_TYPES = new Set(['optional', 'nullable', 'default', 'readonly']);\n\ninterface WrapperDef {\n type?: string;\n innerType?: ZodType;\n}\n\nconst innerSchema = (schema: ZodType): ZodType | undefined => {\n const def: WrapperDef | undefined = (\n schema as { _zod?: { def?: WrapperDef } }\n )._zod?.def;\n return def?.type && WRAPPER_TYPES.has(def.type) ? def.innerType : undefined;\n};\n\n// The schema holding `brand`: `schema` or one it wraps.\nconst brandHolder = (schema: ZodType, key: symbol): ZodType | undefined => {\n let current: ZodType | undefined = schema;\n while (current && !Object.hasOwn(current, key)) {\n current = innerSchema(current);\n }\n return current;\n};\n\n// Whether any wrapper layer of `schema` accepts `undefined`.\nconst optionalThrough = (schema: ZodType): boolean => {\n for (\n let current: ZodType | undefined = schema;\n current;\n current = innerSchema(current)\n ) {\n if (isOptionalSchema(current)) return true;\n }\n return false;\n};\n\nconst unwrapAll = (schema: ZodType): ZodType => {\n let current = schema;\n for (let inner = innerSchema(current); inner; inner = innerSchema(current)) {\n current = inner;\n }\n return current;\n};\n\n/** `undefined` is a valid value, so children may be omitted. */\nconst isOptionalSchema = (schema: ZodType): boolean => {\n const type = zodDefType(schema);\n return type === 'optional' || type === 'default';\n};\n\nconst schemaSignature = (\n schema: ZodType,\n seen: Set<object> = new Set()\n): string => {\n if (seen.has(schema)) {\n return 'cycle';\n }\n seen.add(schema);\n const type = zodDefType(schema) ?? '?';\n if (type === 'object' && 'shape' in schema) {\n const shape = schema.shape as Record<string, ZodType>;\n const fields = Object.keys(shape)\n .sort()\n .map((key) => `${key}:${schemaSignature(shape[key] as ZodType, seen)}`);\n return `{${fields.join(',')}}`;\n }\n if (type === 'array' && 'element' in schema) {\n return `[${schemaSignature((schema as { element: ZodType }).element, seen)}]`;\n }\n const inner = innerSchema(schema);\n if (inner) {\n return `${type}(${schemaSignature(inner, seen)})`;\n }\n return type;\n};\n\nconst arrayElement = (schema: ZodType): ZodType => {\n const inner = unwrapAll(schema);\n if (zodDefType(inner) === 'array' && 'element' in inner) {\n return (inner as { element: ZodType }).element;\n }\n return inner;\n};\n\nconst readChild = (\n field: string,\n schema: ZodType\n): AuthoredChildField | undefined => {\n const holder = brandHolder(schema, authoredChild);\n const childType = (holder as { [authoredChild]?: unknown } | undefined)?.[\n authoredChild\n ];\n if (!holder || typeof childType !== 'string') {\n return undefined;\n }\n const textField = (holder as { [authoredTextField]?: unknown })[\n authoredTextField\n ];\n const toItem = (holder as { [authoredToItem]?: unknown })[authoredToItem];\n const child: AuthoredChildField = {\n field,\n childType,\n itemSchema: arrayElement(holder),\n optional: optionalThrough(schema),\n signature: schemaSignature(arrayElement(holder)),\n };\n if (typeof textField === 'string') {\n child.textField = textField;\n }\n if (typeof toItem === 'function') {\n child.toItem = toItem as (\n props: object,\n context: AuthorChildContext\n ) => unknown;\n }\n return child;\n};\n\nconst readText = (\n field: string,\n schema: ZodType\n): AuthoredTextField | undefined => {\n const holder = brandHolder(schema, authoredText);\n if (!holder) {\n return undefined;\n }\n return {\n field,\n collapseWhitespace:\n (holder as { [authoredCollapse]?: unknown })[authoredCollapse] !== false,\n optional: optionalThrough(schema),\n };\n};\n\n/** Top-level branded fields on an object schema. Nested brands stay on the item schema. */\nexport const readAuthoredSpec = (schema: ZodType): AuthoredSpec => {\n if (zodDefType(schema) !== 'object' || !('shape' in schema)) {\n return { children: [], text: [] };\n }\n const children: AuthoredChildField[] = [];\n const text: AuthoredTextField[] = [];\n for (const [field, fieldSchema] of Object.entries(\n schema.shape as Record<string, ZodType>\n )) {\n const child = readChild(field, fieldSchema);\n if (child) {\n children.push(child);\n continue;\n }\n const textField = readText(field, fieldSchema);\n if (textField) {\n text.push(textField);\n }\n }\n return { children, text };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"authored_fields.js","sourceRoot":"","sources":["../../../src/author/authored_fields.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAyOH,oCAgBC;AApPD,gDAAuE;AAGvE;;;;GAIG;AACU,QAAA,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;AAEzE,sEAAsE;AACzD,QAAA,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;AAEzE,qDAAqD;AACxC,QAAA,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;AAEvE,gFAAgF;AACnE,QAAA,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,+BAA+B,CAAC,CAAC;AAEzE,qDAAqD;AACxC,QAAA,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;AAC5E,MAAM,iBAAiB,GAAG,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;AAC3E,MAAM,gBAAgB,GAAG,MAAM,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;AACxE,MAAM,cAAc,GAAG,MAAM,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;AACpE,MAAM,mBAAmB,GAAG,MAAM,CAA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AM,SAAS,GAAG,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QAC/C,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACrB,SAAS;QACX,CAAC;QACD,MAAM,MAAM,GAAG,WAAW,CAAC,WAAW,EAAE,qBAAa,CAAC,CAAC;QACvD,IAAI,MAAM,EAAE,CAAC;YACX,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACnC,CAAC,CAAC;AA1BW,QAAA,gBAAgB,oBA0B3B","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport type { ReactNode } from 'react';\nimport type { ZodType } from 'zod';\n\nimport { ISOMER_ERROR_CODES, IsomerError } from '../composition/error';\nimport type { PrimitiveNode } from '../define/primitive_module';\n\n/**\n * Member name of the child component a schema field is filled from.\n *\n * `Symbol.for`: ESM and CJS builds must agree, same as `authorType`.\n */\nexport const authoredChild = Symbol.for('elastic.isomer.authored_child');\n\n/** Phantom props type for the child component. Not set at runtime. */\nexport const authoredProps = Symbol.for('elastic.isomer.authored_props');\n\n/** Set when a field is filled from text children. */\nexport const authoredText = Symbol.for('elastic.isomer.authored_text');\n\n/** Set when a field is filled from primitive elements no other field claims. */\nexport const authoredNodes = Symbol.for('elastic.isomer.authored_nodes');\n\n/** Holds a {@link fromChildren} brand's `toItem`. */\nexport const authoredToItem = Symbol.for('elastic.isomer.authored_to_item');\nconst authoredTextField = Symbol.for('elastic.isomer.authored_text_field');\nconst authoredCollapse = Symbol.for('elastic.isomer.authored_collapse');\nconst authoredDedent = Symbol.for('elastic.isomer.authored_dedent');\nconst authoredPropsSchema = Symbol.for('elastic.isomer.authored_props_schema');\n\n/**\n * A field filled from child elements of `TName`.\n *\n * Phantom: `z.infer` still reads the underlying schema. The shim reads `TName`\n * and `TProps` off the field type, and the symbols off the schema value.\n */\nexport interface AuthoredChildBrand<TName extends string, TProps> {\n readonly [authoredChild]: TName;\n readonly [authoredProps]: TProps;\n}\n\n/** A child field whose `toItem` builds each item, so the shim fills none of the item's own brands. */\nexport interface AuthoredToItemBrand {\n readonly [authoredToItem]: (\n props: never,\n context: AuthorChildContext\n ) => unknown;\n}\n\n/** A field filled from text children. */\nexport interface AuthoredTextBrand {\n readonly [authoredText]: true;\n}\n\n/** A field filled from primitive elements, like a child slot the tree walk does not visit. */\nexport interface AuthoredNodesBrand {\n readonly [authoredNodes]: true;\n}\n\n/** What {@link fromChildren}'s `toItem` receives. */\nexport interface AuthorChildContext {\n /** Nested elements, parsed as body nodes. */\n parseChildren: (children: ReactNode) => PrimitiveNode[];\n}\n\ntype ArrayItem<TSchema extends ZodType> =\n NonNullable<zOutput<TSchema>> extends readonly (infer Item)[]\n ? Item\n : NonNullable<zOutput<TSchema>>;\n\ntype zInput<TSchema> = TSchema extends { _zod: { input: infer Input } }\n ? Input\n : never;\n\ntype zOutput<TSchema> = TSchema extends { _zod: { output: infer Output } }\n ? Output\n : never;\n\ntype WrapperOf<TInner> = {\n _zod: {\n def: {\n type: 'optional' | 'nullable' | 'default' | 'readonly';\n innerType: TInner;\n };\n };\n};\n\ntype Unwrapped<TSchema> =\n TSchema extends WrapperOf<infer Inner> ? Unwrapped<Inner> : TSchema;\n\n/** Whether `F`, or a wrapper layer of it, carries a {@link fromChildren} brand. */\ntype IsChildBranded<F> =\n F extends AuthoredChildBrand<string, unknown>\n ? true\n : F extends WrapperOf<infer Inner>\n ? IsChildBranded<Inner>\n : false;\n\n/** A branded field's item schema: the array element through any wrapper, or the unwrapped schema. */\nexport type ItemSchema<TSchema> =\n Unwrapped<TSchema> extends { element: infer Element }\n ? Element\n : Unwrapped<TSchema>;\n\n/** Item fields a nested {@link fromChildren} brand fills from child elements. */\ntype ChildBrandedKeys<TItemSchema> = TItemSchema extends {\n shape: infer Shape;\n}\n ? string extends keyof Shape\n ? never\n : {\n [K in keyof Shape]: IsChildBranded<Shape[K]> extends true ? K : never;\n }[keyof Shape]\n : never;\n\n/** `TOptional` is optional on the child props because text or child elements fill it. */\ntype ChildProps<TItem, TOptional extends PropertyKey> = [TOptional] extends [\n never,\n]\n ? TItem & { children?: ReactNode }\n : Omit<TItem, TOptional> &\n Partial<Pick<TItem, Extract<TOptional, keyof TItem>>> & {\n children?: ReactNode;\n };\n\nconst BRANDS = [\n authoredChild,\n authoredTextField,\n authoredToItem,\n authoredText,\n authoredNodes,\n authoredCollapse,\n authoredDedent,\n authoredPropsSchema,\n] as const;\n\n// Applies a complete branding. Branding an instance again is allowed only with\n// the same configuration, absent options included, since both fields share it.\nconst brand = (\n caller: 'fromChildren' | 'fromNodeChildren' | 'fromTextChildren',\n schema: object,\n config: Partial<Record<(typeof BRANDS)[number], unknown>>\n): void => {\n const record = schema as Record<symbol, unknown>;\n if (BRANDS.some((key) => Object.hasOwn(schema, key))) {\n if (BRANDS.every((key) => record[key] === config[key])) {\n return;\n }\n throw new IsomerError(\n ISOMER_ERROR_CODES.AUTHORED_SCHEMA_REUSED,\n `${caller}: this schema instance is already branded another way; pass each field its own schema.`\n );\n }\n for (const key of BRANDS) {\n if (config[key] !== undefined) {\n Object.defineProperty(schema, key, {\n value: config[key],\n enumerable: false,\n });\n }\n }\n};\n\n/**\n * Brands `schema` as JSX children of the member `name`, capitalized on the\n * primitive's component: `fromChildren('item', …)` on `slideList` is\n * `<SlideList.Item>`. Names are unique across a primitive's child tree.\n *\n * `options.text` copies leftover text onto that field. `options.toItem` replaces\n * the default prop copy; its props annotation is the child component's props.\n * The brand is read through `.optional()`, `.nullable()`, `.default()`, and\n * `.readonly()`; any other method clones without it, so call it on `schema`\n * before passing it in.\n */\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n>(\n name: TName,\n schema: TSchema\n): TSchema &\n AuthoredChildBrand<\n TName,\n ChildProps<ArrayItem<TSchema>, ChildBrandedKeys<ItemSchema<TSchema>>>\n >;\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n const TText extends string,\n>(\n name: TName,\n schema: TSchema,\n options: { text: TText; toItem?: never }\n): TSchema &\n AuthoredChildBrand<\n TName,\n ChildProps<\n ArrayItem<TSchema>,\n TText | ChildBrandedKeys<ItemSchema<TSchema>>\n >\n >;\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n TPropsSchema extends ZodType,\n>(\n name: TName,\n schema: TSchema,\n options: {\n text?: string;\n propsSchema: TPropsSchema;\n toItem(\n props: zInput<TPropsSchema> & { children?: ReactNode },\n context: AuthorChildContext\n ): unknown;\n }\n): TSchema &\n AuthoredChildBrand<TName, zInput<TPropsSchema> & { children?: ReactNode }> &\n AuthoredToItemBrand;\nexport function fromChildren<\n const TName extends string,\n TSchema extends ZodType,\n TProps,\n>(\n name: TName,\n schema: TSchema,\n options: {\n text?: string;\n toItem(props: TProps, context: AuthorChildContext): unknown;\n propsSchema?: never;\n }\n): TSchema & AuthoredChildBrand<TName, TProps> & AuthoredToItemBrand;\nexport function fromChildren(\n name: string,\n schema: ZodType,\n options?: {\n text?: string;\n toItem?: (this: void, props: never, context: AuthorChildContext) => unknown;\n propsSchema?: ZodType;\n }\n): ZodType {\n brand('fromChildren', schema, {\n [authoredChild]: name,\n [authoredTextField]: options?.text,\n [authoredToItem]: options?.toItem,\n [authoredPropsSchema]: options?.propsSchema,\n });\n return schema;\n}\n\n/**\n * Brands `schema` as text children.\n *\n * Whitespace collapses unless `collapseWhitespace` is `false`. `dedent` keeps\n * it and reads the text as source, as {@link textFromChildren} does.\n */\nexport const fromTextChildren = <TSchema extends ZodType>(\n schema: TSchema,\n options?: { collapseWhitespace?: boolean; dedent?: boolean }\n): TSchema & AuthoredTextBrand => {\n brand('fromTextChildren', schema, {\n [authoredText]: true,\n [authoredCollapse]:\n options?.collapseWhitespace === false || options?.dedent\n ? false\n : undefined,\n [authoredDedent]: options?.dedent ? true : undefined,\n });\n return schema as TSchema & AuthoredTextBrand;\n};\n\n/**\n * Brands `schema`, a node or an array of nodes, as the primitive elements among\n * the children that no other field claims. Use it for nodes the tree walk does\n * not visit; a walked child slot fills from children already.\n */\nexport const fromNodeChildren = <TSchema extends ZodType>(\n schema: TSchema\n): TSchema & AuthoredNodesBrand => {\n brand('fromNodeChildren', schema, { [authoredNodes]: true });\n return schema as TSchema & AuthoredNodesBrand;\n};\n\n/** One {@link fromChildren} field as the shim reads it. */\nexport interface AuthoredChildField {\n field: string;\n /** The member name `fromChildren` was given, before capitalizing. */\n name: string;\n /** Field leftover text children fill. */\n textField?: string;\n /** Replaces the default prop copy. */\n toItem?: (props: object, context: AuthorChildContext) => unknown;\n /** The array element schema, or the field schema itself when it is not an array. */\n itemSchema: ZodType;\n /** Input props for a custom `toItem`, excluding JSX children. */\n propsSchema?: ZodType;\n /** Whether the field holds an array of items rather than one item. */\n array: boolean;\n /** Whether the field accepts `undefined`, so children may be omitted. */\n optional: boolean;\n}\n\n/** One {@link fromTextChildren} field as the shim reads it. */\nexport interface AuthoredTextField {\n field: string;\n /** `true` unless the brand was given `collapseWhitespace: false` or `dedent`. */\n collapseWhitespace: boolean;\n /** Set when the brand was given `dedent`. */\n dedent?: true;\n /** Whether the field accepts `undefined`, so text children may be omitted. */\n optional: boolean;\n}\n\n/** One {@link fromNodeChildren} field as the shim reads it. */\nexport interface AuthoredNodesField {\n field: string;\n /** Whether the field holds an array of nodes rather than one. */\n array: boolean;\n}\n\n/** The branded fields found on one object schema. */\nexport interface AuthoredSpec {\n children: AuthoredChildField[];\n text: AuthoredTextField[];\n /** Absent on a spec built by hand; read as none. */\n nodes?: AuthoredNodesField[];\n}\n\nconst zodDefType = (schema: object): string | undefined =>\n (schema as { _zod?: { def?: { type?: string } } })._zod?.def?.type;\n\nconst WRAPPER_TYPES = new Set(['optional', 'nullable', 'default', 'readonly']);\n\ninterface WrapperDef {\n type?: string;\n innerType?: ZodType;\n}\n\nconst innerSchema = (schema: ZodType): ZodType | undefined => {\n const def: WrapperDef | undefined = (\n schema as { _zod?: { def?: WrapperDef } }\n )._zod?.def;\n return def?.type && WRAPPER_TYPES.has(def.type) ? def.innerType : undefined;\n};\n\n// The schema holding `brand`: `schema` or one it wraps.\nconst brandHolder = (schema: ZodType, key: symbol): ZodType | undefined => {\n let current: ZodType | undefined = schema;\n while (current && !Object.hasOwn(current, key)) {\n current = innerSchema(current);\n }\n return current;\n};\n\n// Whether any wrapper layer of `schema` accepts `undefined`.\nconst optionalThrough = (schema: ZodType): boolean => {\n for (\n let current: ZodType | undefined = schema;\n current;\n current = innerSchema(current)\n ) {\n if (isOptionalSchema(current)) {\n return true;\n }\n }\n return false;\n};\n\nconst unwrapAll = (schema: ZodType): ZodType => {\n let current = schema;\n for (let inner = innerSchema(current); inner; inner = innerSchema(current)) {\n current = inner;\n }\n return current;\n};\n\n/** `undefined` is a valid value, so children may be omitted. */\nconst isOptionalSchema = (schema: ZodType): boolean => {\n const type = zodDefType(schema);\n return type === 'optional' || type === 'default';\n};\n\nconst arrayElement = (schema: ZodType): ZodType | undefined => {\n const inner = unwrapAll(schema);\n return zodDefType(inner) === 'array' && 'element' in inner\n ? (inner as { element: ZodType }).element\n : undefined;\n};\n\nconst readChild = (\n field: string,\n schema: ZodType\n): AuthoredChildField | undefined => {\n const holder = brandHolder(schema, authoredChild);\n const name = (holder as { [authoredChild]?: unknown } | undefined)?.[\n authoredChild\n ];\n if (!holder || typeof name !== 'string') {\n return undefined;\n }\n const textField = (holder as { [authoredTextField]?: unknown })[\n authoredTextField\n ];\n const toItem = (holder as { [authoredToItem]?: unknown })[authoredToItem];\n const propsSchema = (holder as { [authoredPropsSchema]?: ZodType })[\n authoredPropsSchema\n ];\n const element = arrayElement(holder);\n const itemSchema = element ?? unwrapAll(holder);\n const child: AuthoredChildField = {\n field,\n name,\n itemSchema,\n array: element !== undefined,\n optional: optionalThrough(schema),\n };\n if (propsSchema) {\n child.propsSchema = propsSchema;\n }\n if (typeof textField === 'string') {\n child.textField = textField;\n }\n if (typeof toItem === 'function') {\n child.toItem = toItem as (\n props: object,\n context: AuthorChildContext\n ) => unknown;\n }\n return child;\n};\n\nconst readText = (\n field: string,\n schema: ZodType\n): AuthoredTextField | undefined => {\n const holder = brandHolder(schema, authoredText);\n if (!holder) {\n return undefined;\n }\n const text: AuthoredTextField = {\n field,\n collapseWhitespace:\n (holder as { [authoredCollapse]?: unknown })[authoredCollapse] !== false,\n optional: optionalThrough(schema),\n };\n if ((holder as { [authoredDedent]?: unknown })[authoredDedent] === true) {\n text.dedent = true;\n }\n return text;\n};\n\n/**\n * The branded fields on an object schema, top level only: read a child's own\n * brands from its {@link AuthoredChildField.itemSchema}.\n */\nexport const readAuthoredSpec = (schema: ZodType): AuthoredSpec => {\n if (zodDefType(schema) !== 'object' || !('shape' in schema)) {\n return { children: [], text: [], nodes: [] };\n }\n const children: AuthoredChildField[] = [];\n const text: AuthoredTextField[] = [];\n const nodes: AuthoredNodesField[] = [];\n for (const [field, fieldSchema] of Object.entries(\n schema.shape as Record<string, ZodType>\n )) {\n const child = readChild(field, fieldSchema);\n if (child) {\n children.push(child);\n continue;\n }\n const textField = readText(field, fieldSchema);\n if (textField) {\n text.push(textField);\n continue;\n }\n const holder = brandHolder(fieldSchema, authoredNodes);\n if (holder) {\n nodes.push({ field, array: arrayElement(holder) !== undefined });\n }\n }\n return { children, text, nodes };\n};\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
4
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
5
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
6
|
+
* 2.0.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.prepareAuthoringInput = void 0;
|
|
10
|
+
const zod_1 = require("zod");
|
|
11
|
+
const isSchema = (value) => typeof value === 'object' &&
|
|
12
|
+
value !== null &&
|
|
13
|
+
'_zod' in value &&
|
|
14
|
+
typeof value._zod?.def?.type === 'string';
|
|
15
|
+
/** Input-schema copy without executable defaults or catch values. */
|
|
16
|
+
const prepareAuthoringInput = (schema) => {
|
|
17
|
+
const copies = new Map();
|
|
18
|
+
const metadata = zod_1.z.registry();
|
|
19
|
+
const copyValue = (value) => {
|
|
20
|
+
if (isSchema(value)) {
|
|
21
|
+
return copySchema(value);
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(value)) {
|
|
24
|
+
return value.map(copyValue);
|
|
25
|
+
}
|
|
26
|
+
if (typeof value !== 'object' || value === null) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
const prototype = Object.getPrototypeOf(value);
|
|
30
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
return Object.fromEntries(Object.entries(value).map(([key, member]) => [key, copyValue(member)]));
|
|
34
|
+
};
|
|
35
|
+
const copySchema = (source) => {
|
|
36
|
+
const cached = copies.get(source);
|
|
37
|
+
if (cached) {
|
|
38
|
+
return cached;
|
|
39
|
+
}
|
|
40
|
+
let result;
|
|
41
|
+
copies.set(source, zod_1.z.lazy(() => result));
|
|
42
|
+
const { def } = source._zod;
|
|
43
|
+
if (def.type === 'default' ||
|
|
44
|
+
def.type === 'prefault' ||
|
|
45
|
+
def.type === 'catch') {
|
|
46
|
+
const { innerType } = def;
|
|
47
|
+
const inner = copySchema(innerType);
|
|
48
|
+
result =
|
|
49
|
+
def.type === 'catch'
|
|
50
|
+
? zod_1.z.clone(inner, inner._zod.def)
|
|
51
|
+
: zod_1.z.optional(inner);
|
|
52
|
+
}
|
|
53
|
+
else if (def.type === 'lazy') {
|
|
54
|
+
const { getter } = def;
|
|
55
|
+
result = zod_1.z.lazy(() => copySchema(getter()));
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
result = zod_1.z.clone(source, copyValue(def));
|
|
59
|
+
}
|
|
60
|
+
copies.set(source, result);
|
|
61
|
+
const meta = zod_1.z.globalRegistry.get(source);
|
|
62
|
+
if (meta) {
|
|
63
|
+
metadata.add(result, meta);
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
return { schema: copySchema(schema), metadata };
|
|
68
|
+
};
|
|
69
|
+
exports.prepareAuthoringInput = prepareAuthoringInput;
|
|
70
|
+
//# sourceMappingURL=authoring_input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authoring_input.js","sourceRoot":"","sources":["../../../src/author/authoring_input.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6BAAsC;AAEtC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoB,EAAE,CACpD,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,MAAM,IAAI,KAAK;IACf,OAAQ,KAAiB,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,KAAK,QAAQ,CAAC;AAEzD,qEAAqE;AAC9D,MAAM,qBAAqB,GAAG,CAAC,MAAe,EAAE,EAAE;IACvD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC3C,MAAM,QAAQ,GAAG,OAAC,CAAC,QAAQ,EAAgB,CAAC;IAC5C,MAAM,SAAS,GAAG,CAAC,KAAc,EAAW,EAAE;QAC5C,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACpB,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC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,CAAC,IAAI,CAAC,GAAG,CAAC;oBAChC,CAAC,CAAC,OAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC/B,MAAM,EAAE,MAAM,EAAE,GAAG,GAA8C,CAAC;YAClE,MAAM,GAAG,OAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QAC9C,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,OAAC,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAe,CAAC,CAAC;QACzD,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC3B,MAAM,IAAI,GAAG,OAAC,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC1C,IAAI,IAAI,EAAE,CAAC;YACT,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IACF,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC;AAClD,CAAC,CAAC;AAzDW,QAAA,qBAAqB,yBAyDhC","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport { z, type ZodType } from 'zod';\n\nconst isSchema = (value: unknown): value is ZodType =>\n typeof value === 'object' &&\n value !== null &&\n '_zod' in value &&\n typeof (value as ZodType)._zod?.def?.type === 'string';\n\n/** Input-schema copy without executable defaults or catch values. */\nexport const prepareAuthoringInput = (schema: ZodType) => {\n const copies = new Map<ZodType, ZodType>();\n const metadata = z.registry<z.GlobalMeta>();\n const copyValue = (value: unknown): unknown => {\n if (isSchema(value)) {\n return copySchema(value);\n }\n if (Array.isArray(value)) {\n return value.map(copyValue);\n }\n if (typeof value !== 'object' || value === null) {\n return value;\n }\n const prototype: unknown = Object.getPrototypeOf(value);\n if (prototype !== Object.prototype && prototype !== null) {\n return value;\n }\n return Object.fromEntries(\n Object.entries(value).map(([key, member]) => [key, copyValue(member)])\n );\n };\n const copySchema = (source: ZodType): ZodType => {\n const cached = copies.get(source);\n if (cached) {\n return cached;\n }\n let result: ZodType;\n copies.set(\n source,\n z.lazy(() => result)\n );\n const { def } = source._zod;\n if (\n def.type === 'default' ||\n def.type === 'prefault' ||\n def.type === 'catch'\n ) {\n const { innerType } = def as { type: string; innerType: ZodType };\n const inner = copySchema(innerType);\n result =\n def.type === 'catch'\n ? z.clone(inner, inner._zod.def)\n : z.optional(inner);\n } else if (def.type === 'lazy') {\n const { getter } = def as { type: string; getter: () => ZodType };\n result = z.lazy(() => copySchema(getter()));\n } else {\n result = z.clone(source, copyValue(def) as typeof def);\n }\n copies.set(source, result);\n const meta = z.globalRegistry.get(source);\n if (meta) {\n metadata.add(result, meta);\n }\n return result;\n };\n return { schema: copySchema(schema), metadata };\n};\n"]}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
4
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
5
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
6
|
+
* 2.0.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.capitalize = exports.inferChildSlots = exports.createAuthoringModel = exports.memberType = void 0;
|
|
10
|
+
const error_1 = require("../composition/error");
|
|
11
|
+
const authored_fields_1 = require("./authored_fields");
|
|
12
|
+
/** The {@link AuthoringChild.authorType} of `field`'s elements under the primitive `owner`. */
|
|
13
|
+
const memberType = (owner, { name }) => `${owner}.${(0, exports.capitalize)(name)}`;
|
|
14
|
+
exports.memberType = memberType;
|
|
15
|
+
const createAuthoringModel = (primitives) => {
|
|
16
|
+
const authoredByType = new Map();
|
|
17
|
+
const membersByType = new Map();
|
|
18
|
+
const collectMembers = (type, spec) => {
|
|
19
|
+
const members = new Map();
|
|
20
|
+
const visited = new Set();
|
|
21
|
+
const collect = (fields, path) => {
|
|
22
|
+
const siblings = new Set();
|
|
23
|
+
for (const field of fields) {
|
|
24
|
+
const member = (0, exports.capitalize)(field.name);
|
|
25
|
+
const prior = members.get(member);
|
|
26
|
+
if (prior &&
|
|
27
|
+
(!isSameChild(prior.field, field) || siblings.has(member))) {
|
|
28
|
+
throw new error_1.IsomerError('DUPLICATE_AUTHORED_CHILD', `buildJsxShim: "${prior.field.field}" and "${field.field}" both become <${(0, exports.capitalize)(type)}.${member}>`);
|
|
29
|
+
}
|
|
30
|
+
siblings.add(member);
|
|
31
|
+
const nextPath = [...path, field];
|
|
32
|
+
if (!prior) {
|
|
33
|
+
members.set(member, {
|
|
34
|
+
field,
|
|
35
|
+
primitiveType: type,
|
|
36
|
+
member,
|
|
37
|
+
authorType: (0, exports.memberType)(type, field),
|
|
38
|
+
path: nextPath,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (field.toItem || visited.has(field.itemSchema)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
visited.add(field.itemSchema);
|
|
45
|
+
collect((0, authored_fields_1.readAuthoredSpec)(field.itemSchema).children, nextPath);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
collect(spec.children, []);
|
|
49
|
+
if (members.size > 0) {
|
|
50
|
+
membersByType.set(type, [...members.values()]);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
for (const { type, schema } of primitives) {
|
|
54
|
+
if (!isZodType(schema)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const spec = (0, authored_fields_1.readAuthoredSpec)(schema);
|
|
58
|
+
if (spec.children.length === 0 &&
|
|
59
|
+
spec.text.length === 0 &&
|
|
60
|
+
!spec.nodes?.length) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
authoredByType.set(type, spec);
|
|
64
|
+
collectMembers(type, spec);
|
|
65
|
+
}
|
|
66
|
+
const typeByName = new Map([['Composition', 'view']]);
|
|
67
|
+
for (const { type } of primitives) {
|
|
68
|
+
const name = (0, exports.capitalize)(type);
|
|
69
|
+
const earlier = typeByName.get(name);
|
|
70
|
+
if (earlier !== undefined && earlier !== type) {
|
|
71
|
+
throw new error_1.IsomerError('DUPLICATE_PRIMITIVE_TYPE', `buildJsxShim: "${earlier}" and "${type}" both become the component ${name}`);
|
|
72
|
+
}
|
|
73
|
+
typeByName.set(name, type);
|
|
74
|
+
}
|
|
75
|
+
const holderByType = new Map(primitives.map(({ type }) => [type, `the primitive "${type}"`]));
|
|
76
|
+
for (const { authorType, primitiveType, member } of [
|
|
77
|
+
...membersByType.values(),
|
|
78
|
+
].flat()) {
|
|
79
|
+
const holder = `the member ${member} of "${primitiveType}"`;
|
|
80
|
+
const earlier = holderByType.get(authorType);
|
|
81
|
+
if (earlier !== undefined) {
|
|
82
|
+
throw new error_1.IsomerError('DUPLICATE_PRIMITIVE_TYPE', `buildJsxShim: ${earlier} and ${holder} share the type "${authorType}"`);
|
|
83
|
+
}
|
|
84
|
+
holderByType.set(authorType, holder);
|
|
85
|
+
}
|
|
86
|
+
const childSlotsByType = new Map(primitives.map(({ type, children }) => {
|
|
87
|
+
const walked = (0, exports.inferChildSlots)(children);
|
|
88
|
+
const nodes = (authoredByType.get(type)?.nodes ?? []).filter(({ field }) => !walked.some((slot) => slot.field === field));
|
|
89
|
+
return [
|
|
90
|
+
type,
|
|
91
|
+
[...walked, ...nodes.map(({ array, field }) => ({ array, field }))],
|
|
92
|
+
];
|
|
93
|
+
}));
|
|
94
|
+
return { authoredByType, membersByType, childSlotsByType };
|
|
95
|
+
};
|
|
96
|
+
exports.createAuthoringModel = createAuthoringModel;
|
|
97
|
+
const isSameChild = (a, b) => a.itemSchema === b.itemSchema &&
|
|
98
|
+
a.propsSchema === b.propsSchema &&
|
|
99
|
+
a.toItem === b.toItem &&
|
|
100
|
+
a.textField === b.textField;
|
|
101
|
+
const isZodType = (schema) => typeof schema === 'object' && schema !== null && '_zod' in schema;
|
|
102
|
+
/**
|
|
103
|
+
* Reads {@link PrimitiveDefinition.children} against a probe node so JSX can
|
|
104
|
+
* fill the same fields a tree walk would visit. Paths like `body[0]` are array
|
|
105
|
+
* slots; a bare `header` is a single nested node.
|
|
106
|
+
*/
|
|
107
|
+
const inferChildSlots = (children) => {
|
|
108
|
+
if (typeof children !== 'function') {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
const probeNode = { type: '__probe__' };
|
|
112
|
+
const node = new Proxy({}, {
|
|
113
|
+
get(_target, prop) {
|
|
114
|
+
if (prop === 'type') {
|
|
115
|
+
return probeNode.type;
|
|
116
|
+
}
|
|
117
|
+
return [probeNode];
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
const refs = children(node);
|
|
122
|
+
if (!Array.isArray(refs)) {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
const byField = new Map();
|
|
126
|
+
for (const ref of refs) {
|
|
127
|
+
if (!isChildRef(ref)) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const field = /^[A-Za-z_]\w*/.exec(ref.path)?.[0];
|
|
131
|
+
if (!field) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
byField.set(field, byField.get(field) === true || /\[\d+\]/.test(ref.path));
|
|
135
|
+
}
|
|
136
|
+
return [...byField.entries()].map(([field, array]) => ({ array, field }));
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
exports.inferChildSlots = inferChildSlots;
|
|
143
|
+
const isChildRef = (value) => typeof value === 'object' &&
|
|
144
|
+
value !== null &&
|
|
145
|
+
'path' in value &&
|
|
146
|
+
typeof value.path === 'string';
|
|
147
|
+
const capitalize = (value) => `${value.slice(0, 1).toUpperCase()}${value.slice(1)}`;
|
|
148
|
+
exports.capitalize = capitalize;
|
|
149
|
+
//# sourceMappingURL=authoring_model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authoring_model.js","sourceRoot":"","sources":["../../../src/author/authoring_model.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAIH,gDAAmD;AAGnD,uDAI2B;AAiB3B,+FAA+F;AACxF,MAAM,UAAU,GAAG,CACxB,KAAa,EACb,EAAE,IAAI,EAAoC,EAClC,EAAE,CAAC,GAAG,KAAK,IAAI,IAAA,kBAAU,EAAC,IAAI,CAAC,EAAE,CAAC;AAH/B,QAAA,UAAU,cAGqB;AAErC,MAAM,oBAAoB,GAAG,CAClC,UAA6E,EAC7E,EAAE;IACF,MAAM,cAAc,GAAG,IAAI,GAAG,EAAwB,CAAC;IACvD,MAAM,aAAa,GAAG,IAAI,GAAG,EAA4B,CAAC;IAC1D,MAAM,cAAc,GAAG,CAAC,IAAY,EAAE,IAAkB,EAAQ,EAAE;QAChE,MAAM,OAAO,GAAG,IAAI,GAAG,EAA0B,CAAC;QAClD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAW,CAAC;QACnC,MAAM,OAAO,GAAG,CACd,MAAqC,EACrC,IAAmC,EAC7B,EAAE;YACR,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;YACnC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACtC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;gBAClC,IACE,KAAK;oBACL,CAAC,CAAC,WAAW,CAAC,KAAK,CAA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AM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAClD,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,SAAS;YACX,CAAC;YACD,OAAO,CAAC,GAAG,CACT,KAAK,EACL,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CACxD,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAC5E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC,CAAC;AAvCW,QAAA,eAAe,mBAuC1B;AAEF,MAAM,UAAU,GAAG,CAAC,KAAc,EAA6B,EAAE,CAC/D,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,MAAM,IAAI,KAAK;IACf,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;AAE1B,MAAM,UAAU,GAAG,CAAC,KAAa,EAAU,EAAE,CAClD,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAD3C,QAAA,UAAU,cACiC","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport type { ZodType } from 'zod';\n\nimport { IsomerError } from '../composition/error';\nimport type { PrimitiveNode } from '../define/primitive_module';\n\nimport {\n type AuthoredChildField,\n type AuthoredSpec,\n readAuthoredSpec,\n} from './authored_fields';\n\nexport interface ChildSlot {\n array: boolean;\n field: string;\n}\n\nexport interface AuthoringChild {\n field: AuthoredChildField;\n primitiveType: string;\n /** Property name on the primitive's component, e.g. `Item`. */\n member: string;\n /** What the member's elements carry as {@link authorType}, e.g. `slideList.Item`. */\n authorType: string;\n path: readonly AuthoredChildField[];\n}\n\n/** The {@link AuthoringChild.authorType} of `field`'s elements under the primitive `owner`. */\nexport const memberType = (\n owner: string,\n { name }: Pick<AuthoredChildField, 'name'>\n): string => `${owner}.${capitalize(name)}`;\n\nexport const createAuthoringModel = (\n primitives: readonly { type: string; schema?: unknown; children?: unknown }[]\n) => {\n const authoredByType = new Map<string, AuthoredSpec>();\n const membersByType = new Map<string, AuthoringChild[]>();\n const collectMembers = (type: string, spec: AuthoredSpec): void => {\n const members = new Map<string, AuthoringChild>();\n const visited = new Set<ZodType>();\n const collect = (\n fields: readonly AuthoredChildField[],\n path: readonly AuthoredChildField[]\n ): void => {\n const siblings = new Set<string>();\n for (const field of fields) {\n const member = capitalize(field.name);\n const prior = members.get(member);\n if (\n prior &&\n (!isSameChild(prior.field, field) || siblings.has(member))\n ) {\n throw new IsomerError(\n 'DUPLICATE_AUTHORED_CHILD',\n `buildJsxShim: \"${prior.field.field}\" and \"${field.field}\" both become <${capitalize(type)}.${member}>`\n );\n }\n siblings.add(member);\n const nextPath = [...path, field];\n if (!prior) {\n members.set(member, {\n field,\n primitiveType: type,\n member,\n authorType: memberType(type, field),\n path: nextPath,\n });\n }\n if (field.toItem || visited.has(field.itemSchema)) {\n continue;\n }\n visited.add(field.itemSchema);\n collect(readAuthoredSpec(field.itemSchema).children, nextPath);\n }\n };\n collect(spec.children, []);\n if (members.size > 0) {\n membersByType.set(type, [...members.values()]);\n }\n };\n for (const { type, schema } of primitives) {\n if (!isZodType(schema)) {\n continue;\n }\n const spec = readAuthoredSpec(schema);\n if (\n spec.children.length === 0 &&\n spec.text.length === 0 &&\n !spec.nodes?.length\n ) {\n continue;\n }\n authoredByType.set(type, spec);\n collectMembers(type, spec);\n }\n const typeByName = new Map([['Composition', 'view']]);\n for (const { type } of primitives) {\n const name = capitalize(type);\n const earlier = typeByName.get(name);\n if (earlier !== undefined && earlier !== type) {\n throw new IsomerError(\n 'DUPLICATE_PRIMITIVE_TYPE',\n `buildJsxShim: \"${earlier}\" and \"${type}\" both become the component ${name}`\n );\n }\n typeByName.set(name, type);\n }\n const holderByType = new Map(\n primitives.map(({ type }) => [type, `the primitive \"${type}\"`])\n );\n for (const { authorType, primitiveType, member } of [\n ...membersByType.values(),\n ].flat()) {\n const holder = `the member ${member} of \"${primitiveType}\"`;\n const earlier = holderByType.get(authorType);\n if (earlier !== undefined) {\n throw new IsomerError(\n 'DUPLICATE_PRIMITIVE_TYPE',\n `buildJsxShim: ${earlier} and ${holder} share the type \"${authorType}\"`\n );\n }\n holderByType.set(authorType, holder);\n }\n const childSlotsByType = new Map(\n primitives.map(({ type, children }) => {\n const walked = inferChildSlots(children);\n const nodes = (authoredByType.get(type)?.nodes ?? []).filter(\n ({ field }) => !walked.some((slot) => slot.field === field)\n );\n return [\n type,\n [...walked, ...nodes.map(({ array, field }) => ({ array, field }))],\n ];\n })\n );\n return { authoredByType, membersByType, childSlotsByType };\n};\n\nconst isSameChild = (a: AuthoredChildField, b: AuthoredChildField): boolean =>\n a.itemSchema === b.itemSchema &&\n a.propsSchema === b.propsSchema &&\n a.toItem === b.toItem &&\n a.textField === b.textField;\n\nconst isZodType = (schema: unknown): schema is ZodType =>\n typeof schema === 'object' && schema !== null && '_zod' in schema;\n\n/**\n * Reads {@link PrimitiveDefinition.children} against a probe node so JSX can\n * fill the same fields a tree walk would visit. Paths like `body[0]` are array\n * slots; a bare `header` is a single nested node.\n */\nexport const inferChildSlots = (children: unknown): readonly ChildSlot[] => {\n if (typeof children !== 'function') {\n return [];\n }\n const probeNode: PrimitiveNode = { type: '__probe__' };\n const node = new Proxy(\n {},\n {\n get(_target, prop) {\n if (prop === 'type') {\n return probeNode.type;\n }\n return [probeNode];\n },\n }\n );\n try {\n const refs = (children as (value: unknown) => unknown)(node);\n if (!Array.isArray(refs)) {\n return [];\n }\n const byField = new Map<string, boolean>();\n for (const ref of refs) {\n if (!isChildRef(ref)) {\n continue;\n }\n const field = /^[A-Za-z_]\\w*/.exec(ref.path)?.[0];\n if (!field) {\n continue;\n }\n byField.set(\n field,\n byField.get(field) === true || /\\[\\d+\\]/.test(ref.path)\n );\n }\n return [...byField.entries()].map(([field, array]) => ({ array, field }));\n } catch {\n return [];\n }\n};\n\nconst isChildRef = (value: unknown): value is { path: string } =>\n typeof value === 'object' &&\n value !== null &&\n 'path' in value &&\n typeof value.path === 'string';\n\nexport const capitalize = (value: string): string =>\n `${value.slice(0, 1).toUpperCase()}${value.slice(1)}`;\n"]}
|