@yimoka/designable-playground 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/demo/components/Alert/index.d.ts +4 -0
- package/dist/demo/components/Alert/index.js +41 -0
- package/dist/demo/components/ArrayCards/index.d.ts +17 -0
- package/dist/demo/components/ArrayCards/index.js +62 -0
- package/dist/demo/components/ArrayList/index.d.ts +25 -0
- package/dist/demo/components/ArrayList/index.js +143 -0
- package/dist/demo/components/ArrayTable/index.d.ts +39 -0
- package/dist/demo/components/ArrayTable/index.js +247 -0
- package/dist/demo/components/AutoComplete/index.d.ts +4 -0
- package/dist/demo/components/AutoComplete/index.js +52 -0
- package/dist/demo/components/Avatar/index.d.ts +4 -0
- package/dist/demo/components/Avatar/index.js +34 -0
- package/dist/demo/components/Badge/index.d.ts +4 -0
- package/dist/demo/components/Badge/index.js +35 -0
- package/dist/demo/components/Button/index.d.ts +4 -0
- package/dist/demo/components/Button/index.js +86 -0
- package/dist/demo/components/Card/index.d.ts +4 -0
- package/dist/demo/components/Card/index.js +72 -0
- package/dist/demo/components/Carousel/index.d.ts +4 -0
- package/dist/demo/components/Carousel/index.js +33 -0
- package/dist/demo/components/Cascader/index.d.ts +4 -0
- package/dist/demo/components/Cascader/index.js +60 -0
- package/dist/demo/components/Checkbox/index.d.ts +8 -0
- package/dist/demo/components/Checkbox/index.js +55 -0
- package/dist/demo/components/ColorPicker/index.d.ts +4 -0
- package/dist/demo/components/ColorPicker/index.js +38 -0
- package/dist/demo/components/DatePicker/index.d.ts +4 -0
- package/dist/demo/components/DatePicker/index.js +61 -0
- package/dist/demo/components/DateRangePicker/index.d.ts +4 -0
- package/dist/demo/components/DateRangePicker/index.js +58 -0
- package/dist/demo/components/Divider/index.d.ts +4 -0
- package/dist/demo/components/Divider/index.js +39 -0
- package/dist/demo/components/Empty/index.d.ts +4 -0
- package/dist/demo/components/Empty/index.js +29 -0
- package/dist/demo/components/Flex/index.d.ts +4 -0
- package/dist/demo/components/Flex/index.js +51 -0
- package/dist/demo/components/Form/index.d.ts +21 -0
- package/dist/demo/components/Form/index.js +116 -0
- package/dist/demo/components/FormCollapse/index.d.ts +28 -0
- package/dist/demo/components/FormCollapse/index.js +164 -0
- package/dist/demo/components/FormGrid/index.d.ts +22 -0
- package/dist/demo/components/FormGrid/index.js +134 -0
- package/dist/demo/components/FormItem/index.d.ts +14 -0
- package/dist/demo/components/FormItem/index.js +33 -0
- package/dist/demo/components/FormLayout/index.d.ts +26 -0
- package/dist/demo/components/FormLayout/index.js +260 -0
- package/dist/demo/components/FormTab/index.d.ts +15 -0
- package/dist/demo/components/FormTab/index.js +186 -0
- package/dist/demo/components/GridCol/index.d.ts +4 -0
- package/dist/demo/components/GridCol/index.js +53 -0
- package/dist/demo/components/GridRow/index.d.ts +4 -0
- package/dist/demo/components/GridRow/index.js +70 -0
- package/dist/demo/components/HTMLContent/index.d.ts +4 -0
- package/dist/demo/components/HTMLContent/index.js +34 -0
- package/dist/demo/components/I18nEditor/index.d.ts +8 -0
- package/dist/demo/components/I18nEditor/index.js +64 -0
- package/dist/demo/components/Image/index.d.ts +4 -0
- package/dist/demo/components/Image/index.js +36 -0
- package/dist/demo/components/Input/index.d.ts +4 -0
- package/dist/demo/components/Input/index.js +36 -0
- package/dist/demo/components/InputSearch/index.d.ts +4 -0
- package/dist/demo/components/InputSearch/index.js +40 -0
- package/dist/demo/components/Link/index.d.ts +4 -0
- package/dist/demo/components/Link/index.js +37 -0
- package/dist/demo/components/Mentions/index.d.ts +4 -0
- package/dist/demo/components/Mentions/index.js +41 -0
- package/dist/demo/components/NumberPicker/index.d.ts +6 -0
- package/dist/demo/components/NumberPicker/index.js +63 -0
- package/dist/demo/components/ObjectContainer/index.d.ts +14 -0
- package/dist/demo/components/ObjectContainer/index.js +46 -0
- package/dist/demo/components/Password/index.d.ts +6 -0
- package/dist/demo/components/Password/index.js +44 -0
- package/dist/demo/components/QRCode/index.d.ts +4 -0
- package/dist/demo/components/QRCode/index.js +34 -0
- package/dist/demo/components/Radio/index.d.ts +6 -0
- package/dist/demo/components/Radio/index.js +49 -0
- package/dist/demo/components/Rate/index.d.ts +6 -0
- package/dist/demo/components/Rate/index.js +43 -0
- package/dist/demo/components/Search/index.d.ts +4 -0
- package/dist/demo/components/Search/index.js +42 -0
- package/dist/demo/components/SearchSelect/index.d.ts +4 -0
- package/dist/demo/components/SearchSelect/index.js +53 -0
- package/dist/demo/components/Select/index.d.ts +4 -0
- package/dist/demo/components/Select/index.js +59 -0
- package/dist/demo/components/Slider/index.d.ts +6 -0
- package/dist/demo/components/Slider/index.js +61 -0
- package/dist/demo/components/Space/index.d.ts +9 -0
- package/dist/demo/components/Space/index.js +61 -0
- package/dist/demo/components/Statistic/index.d.ts +4 -0
- package/dist/demo/components/Statistic/index.js +33 -0
- package/dist/demo/components/Switch/index.d.ts +4 -0
- package/dist/demo/components/Switch/index.js +49 -0
- package/dist/demo/components/Tabs/index.d.ts +10 -0
- package/dist/demo/components/Tabs/index.js +181 -0
- package/dist/demo/components/Tag/index.d.ts +4 -0
- package/dist/demo/components/Tag/index.js +33 -0
- package/dist/demo/components/Text/index.d.ts +11 -0
- package/dist/demo/components/Text/index.js +64 -0
- package/dist/demo/components/TextArea/index.d.ts +6 -0
- package/dist/demo/components/TextArea/index.js +43 -0
- package/dist/demo/components/TimePicker/index.d.ts +4 -0
- package/dist/demo/components/TimePicker/index.js +69 -0
- package/dist/demo/components/TimeRangePicker/index.d.ts +4 -0
- package/dist/demo/components/TimeRangePicker/index.js +68 -0
- package/dist/demo/components/Transfer/index.d.ts +6 -0
- package/dist/demo/components/Transfer/index.js +59 -0
- package/dist/demo/components/TreeSelect/index.d.ts +4 -0
- package/dist/demo/components/TreeSelect/index.js +63 -0
- package/dist/demo/components/Upload/index.d.ts +8 -0
- package/dist/demo/components/Upload/index.js +64 -0
- package/dist/demo/components/index.d.ts +55 -0
- package/dist/demo/components/index.js +55 -0
- package/dist/demo/components/shared.d.ts +143 -0
- package/dist/demo/components/shared.js +315 -0
- package/dist/src/Playground.d.ts +3 -0
- package/dist/src/Playground.js +304 -0
- package/dist/src/antd/AntdPlayground.d.ts +16 -0
- package/dist/src/antd/AntdPlayground.js +136 -0
- package/dist/src/antd/index.d.ts +1 -0
- package/dist/src/antd/index.js +1 -0
- package/dist/src/context.d.ts +4 -0
- package/dist/src/context.js +13 -0
- package/dist/src/examples/index.d.ts +267 -0
- package/dist/src/examples/index.js +824 -0
- package/dist/src/hooks/index.d.ts +4 -0
- package/dist/src/hooks/index.js +4 -0
- package/dist/src/hooks/remoteSchemaAdapter.d.ts +53 -0
- package/dist/src/hooks/remoteSchemaAdapter.js +262 -0
- package/dist/src/hooks/usePlayground.d.ts +38 -0
- package/dist/src/hooks/usePlayground.js +642 -0
- package/dist/src/hooks/usePlaygroundRemotePersistence.d.ts +135 -0
- package/dist/src/hooks/usePlaygroundRemotePersistence.js +736 -0
- package/dist/src/hooks/useSchemaEditorDraft.d.ts +4 -0
- package/dist/src/hooks/useSchemaEditorDraft.js +44 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.js +14 -0
- package/dist/src/layouts/PlaygroundLayout/ErrorBoundaries.d.ts +59 -0
- package/dist/src/layouts/PlaygroundLayout/ErrorBoundaries.js +161 -0
- package/dist/src/layouts/PlaygroundLayout/index.d.ts +7 -0
- package/dist/src/layouts/PlaygroundLayout/index.js +443 -0
- package/dist/src/layouts/index.d.ts +1 -0
- package/dist/src/layouts/index.js +1 -0
- package/dist/src/panels/CompositePanel/index.d.ts +6 -0
- package/dist/src/panels/CompositePanel/index.js +139 -0
- package/dist/src/panels/ToolbarPanel/index.d.ts +3 -0
- package/dist/src/panels/ToolbarPanel/index.js +24 -0
- package/dist/src/panels/index.d.ts +2 -0
- package/dist/src/panels/index.js +2 -0
- package/dist/src/presets/antd.d.ts +19 -0
- package/dist/src/presets/antd.js +112 -0
- package/dist/src/presets/index.d.ts +4 -0
- package/dist/src/presets/index.js +3 -0
- package/dist/src/presets/runtime-array-helpers.d.ts +8 -0
- package/dist/src/presets/runtime-array-helpers.js +147 -0
- package/dist/src/presets/runtime-component-props.d.ts +3 -0
- package/dist/src/presets/runtime-component-props.js +5 -0
- package/dist/src/presets/runtime-components.d.ts +5 -0
- package/dist/src/presets/runtime-components.js +419 -0
- package/dist/src/presets/runtime-form-components.d.ts +9 -0
- package/dist/src/presets/runtime-form-components.js +94 -0
- package/dist/src/presets/scenario-catalog.d.ts +6 -0
- package/dist/src/presets/scenario-catalog.js +128 -0
- package/dist/src/presets/scenarios.d.ts +52 -0
- package/dist/src/presets/scenarios.js +603 -0
- package/dist/src/runtime/actions.d.ts +19 -0
- package/dist/src/runtime/actions.js +307 -0
- package/dist/src/runtime/contracts.d.ts +149 -0
- package/dist/src/runtime/contracts.js +1 -0
- package/dist/src/runtime/document-bindings.d.ts +3 -0
- package/dist/src/runtime/document-bindings.js +101 -0
- package/dist/src/runtime/document-core.d.ts +57 -0
- package/dist/src/runtime/document-core.js +379 -0
- package/dist/src/runtime/document-entity.d.ts +12 -0
- package/dist/src/runtime/document-entity.js +351 -0
- package/dist/src/runtime/document-expressions.d.ts +35 -0
- package/dist/src/runtime/document-expressions.js +346 -0
- package/dist/src/runtime/document-security.d.ts +34 -0
- package/dist/src/runtime/document-security.js +538 -0
- package/dist/src/runtime/document.d.ts +16 -0
- package/dist/src/runtime/document.js +212 -0
- package/dist/src/runtime/environment.d.ts +16 -0
- package/dist/src/runtime/environment.js +76 -0
- package/dist/src/runtime/i18n-schema.d.ts +36 -0
- package/dist/src/runtime/i18n-schema.js +338 -0
- package/dist/src/runtime/i18n.d.ts +25 -0
- package/dist/src/runtime/i18n.js +447 -0
- package/dist/src/runtime/index.d.ts +8 -0
- package/dist/src/runtime/index.js +8 -0
- package/dist/src/runtime/navigation.d.ts +5 -0
- package/dist/src/runtime/navigation.js +43 -0
- package/dist/src/runtime/playground.d.ts +30 -0
- package/dist/src/runtime/playground.js +243 -0
- package/dist/src/runtime/preview-boundaries.d.ts +13 -0
- package/dist/src/runtime/preview-boundaries.js +43 -0
- package/dist/src/runtime/preview.d.ts +44 -0
- package/dist/src/runtime/preview.js +574 -0
- package/dist/src/runtime/settings/RuntimeJsonEditor.d.ts +10 -0
- package/dist/src/runtime/settings/RuntimeJsonEditor.js +21 -0
- package/dist/src/runtime/settings/RuntimeStoreEntitySettings.d.ts +15 -0
- package/dist/src/runtime/settings/RuntimeStoreEntitySettings.js +65 -0
- package/dist/src/runtime/templates.d.ts +1560 -0
- package/dist/src/runtime/templates.js +508 -0
- package/dist/src/runtime/website-contact-template.d.ts +1 -0
- package/dist/src/runtime/website-contact-template.js +66 -0
- package/dist/src/runtime/website-template-shared.d.ts +14 -0
- package/dist/src/runtime/website-template-shared.js +210 -0
- package/dist/src/runtime/website-templates.d.ts +5 -0
- package/dist/src/runtime/website-templates.js +976 -0
- package/dist/src/shared/clipboard.d.ts +4 -0
- package/dist/src/shared/clipboard.js +56 -0
- package/dist/src/shared/componentRegistry.d.ts +133 -0
- package/dist/src/shared/componentRegistry.js +381 -0
- package/dist/src/shared/formilyCompatibility.d.ts +8 -0
- package/dist/src/shared/formilyCompatibility.js +40 -0
- package/dist/src/shared/index.d.ts +6 -0
- package/dist/src/shared/index.js +6 -0
- package/dist/src/shared/productionAcceptance.d.ts +63 -0
- package/dist/src/shared/productionAcceptance.js +201 -0
- package/dist/src/shared/schema-options.d.ts +16 -0
- package/dist/src/shared/schema-options.js +138 -0
- package/dist/src/shared/schema.d.ts +63 -0
- package/dist/src/shared/schema.js +627 -0
- package/dist/src/shared/styleSecurity.d.ts +2 -0
- package/dist/src/shared/styleSecurity.js +35 -0
- package/dist/src/transformers/formily.d.ts +34 -0
- package/dist/src/transformers/formily.js +943 -0
- package/dist/src/transformers/index.d.ts +1 -0
- package/dist/src/transformers/index.js +1 -0
- package/dist/src/types.d.ts +186 -0
- package/dist/src/types.js +1 -0
- package/dist/src/widgets/ActionsWidget/index.d.ts +3 -0
- package/dist/src/widgets/ActionsWidget/index.js +10 -0
- package/dist/src/widgets/ComponentTreeWidget/index.d.ts +3 -0
- package/dist/src/widgets/ComponentTreeWidget/index.js +333 -0
- package/dist/src/widgets/NodeActionsWidget/index.d.ts +2 -0
- package/dist/src/widgets/NodeActionsWidget/index.js +125 -0
- package/dist/src/widgets/OutlineTreeWidget/index.d.ts +3 -0
- package/dist/src/widgets/OutlineTreeWidget/index.js +432 -0
- package/dist/src/widgets/OutlineTreeWidget/shared.d.ts +32 -0
- package/dist/src/widgets/OutlineTreeWidget/shared.js +79 -0
- package/dist/src/widgets/PreviewWidget/index.d.ts +2 -0
- package/dist/src/widgets/PreviewWidget/index.js +1566 -0
- package/dist/src/widgets/ProductionReadinessWidget/index.d.ts +18 -0
- package/dist/src/widgets/ProductionReadinessWidget/index.js +356 -0
- package/dist/src/widgets/RemotePersistenceStatusWidget/index.d.ts +18 -0
- package/dist/src/widgets/RemotePersistenceStatusWidget/index.js +438 -0
- package/dist/src/widgets/RemoteVersionHistoryWidget/index.d.ts +39 -0
- package/dist/src/widgets/RemoteVersionHistoryWidget/index.js +475 -0
- package/dist/src/widgets/ResourceWidget/index.d.ts +3 -0
- package/dist/src/widgets/ResourceWidget/index.js +491 -0
- package/dist/src/widgets/SchemaCodeView/index.d.ts +7 -0
- package/dist/src/widgets/SchemaCodeView/index.js +225 -0
- package/dist/src/widgets/SchemaEditorWidget/index.d.ts +10 -0
- package/dist/src/widgets/SchemaEditorWidget/index.js +484 -0
- package/dist/src/widgets/SettingsPanelWidget/index.d.ts +3 -0
- package/dist/src/widgets/SettingsPanelWidget/index.js +190 -0
- package/dist/src/widgets/StorageActionsWidget/index.d.ts +2 -0
- package/dist/src/widgets/StorageActionsWidget/index.js +284 -0
- package/dist/src/widgets/index.d.ts +13 -0
- package/dist/src/widgets/index.js +13 -0
- package/package.json +100 -4
|
@@ -0,0 +1,1566 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { observer } from '@formily/reactive-react';
|
|
3
|
+
import { DesignerComponentsContext, TreeNodeContext, usePrefix, useTree, } from '@yimoka/designable-react';
|
|
4
|
+
import cls from 'classnames';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { usePlaygroundContext } from "../../context.js";
|
|
7
|
+
import { FORMILY_SCHEMA_PROP_NAMES, INTERNAL_DESIGNABLE_SCHEMA_PROP_NAMES, isComponentSchemaPropName, RENDER_ONLY_SCHEMA_PROP_NAMES, } from "../../shared/schema.js";
|
|
8
|
+
import { hasUnsafeStyleValue } from "../../shared/styleSecurity.js";
|
|
9
|
+
const DESIGNER_PROP_NAMES = [
|
|
10
|
+
'data-designer-source-id',
|
|
11
|
+
'data-designer-node-id',
|
|
12
|
+
'data-designer-outline-node-id',
|
|
13
|
+
'data-designer-node-helpers-id',
|
|
14
|
+
'data-designer-node-handler',
|
|
15
|
+
'data-designer-node-resize-handler',
|
|
16
|
+
'data-designer-node-translate-handler',
|
|
17
|
+
'data-content-editable',
|
|
18
|
+
'data-content-editable-node-id',
|
|
19
|
+
'data-click-stop-propagation',
|
|
20
|
+
];
|
|
21
|
+
const toRenderableComponent = (component) => component;
|
|
22
|
+
const PREVIEW_UNSAFE_PROP_NAMES = new Set([
|
|
23
|
+
'dangerouslySetInnerHTML',
|
|
24
|
+
]);
|
|
25
|
+
const PREVIEW_UNSAFE_STYLE_PROP_NAMES = new Set([
|
|
26
|
+
'inset',
|
|
27
|
+
'insetBlock',
|
|
28
|
+
'insetBlockEnd',
|
|
29
|
+
'insetBlockStart',
|
|
30
|
+
'insetInline',
|
|
31
|
+
'insetInlineEnd',
|
|
32
|
+
'insetInlineStart',
|
|
33
|
+
'top',
|
|
34
|
+
'right',
|
|
35
|
+
'bottom',
|
|
36
|
+
'left',
|
|
37
|
+
'zIndex',
|
|
38
|
+
]);
|
|
39
|
+
const PREVIEW_ESCAPE_POSITION_VALUES = new Set([
|
|
40
|
+
'fixed',
|
|
41
|
+
'sticky',
|
|
42
|
+
]);
|
|
43
|
+
const OPTION_COMPONENT_NAMES = new Set([
|
|
44
|
+
'AutoComplete',
|
|
45
|
+
'Cascader',
|
|
46
|
+
'Checkbox',
|
|
47
|
+
'Mentions',
|
|
48
|
+
'Radio',
|
|
49
|
+
'Search',
|
|
50
|
+
'SearchSelect',
|
|
51
|
+
'Select',
|
|
52
|
+
'TreeSelect',
|
|
53
|
+
'Transfer',
|
|
54
|
+
]);
|
|
55
|
+
const DATA_SOURCE_LOADING_COMPONENT_NAMES = new Set([
|
|
56
|
+
'Mentions',
|
|
57
|
+
'Select',
|
|
58
|
+
]);
|
|
59
|
+
const PREVIEW_ROOT_STYLE = {
|
|
60
|
+
background: '#f4f7fb',
|
|
61
|
+
boxSizing: 'border-box',
|
|
62
|
+
height: '100%',
|
|
63
|
+
minHeight: 0,
|
|
64
|
+
overflow: 'auto',
|
|
65
|
+
padding: 24,
|
|
66
|
+
width: '100%',
|
|
67
|
+
};
|
|
68
|
+
const PREVIEW_CONTENT_STYLE = {
|
|
69
|
+
background: '#ffffff',
|
|
70
|
+
border: '1px solid #dfe7f1',
|
|
71
|
+
borderRadius: 8,
|
|
72
|
+
boxShadow: '0 10px 28px rgba(15, 23, 42, 0.08)',
|
|
73
|
+
boxSizing: 'border-box',
|
|
74
|
+
margin: '0 auto',
|
|
75
|
+
maxWidth: 1040,
|
|
76
|
+
minHeight: 360,
|
|
77
|
+
padding: 24,
|
|
78
|
+
width: '100%',
|
|
79
|
+
};
|
|
80
|
+
const PreviewRuntimeContext = React.createContext({
|
|
81
|
+
reactiveFieldNames: new Set(),
|
|
82
|
+
setValue: () => undefined,
|
|
83
|
+
targetReactionSchemas: {},
|
|
84
|
+
targetReactionStates: {},
|
|
85
|
+
values: {},
|
|
86
|
+
});
|
|
87
|
+
const removeDesignerProps = (props) => {
|
|
88
|
+
const nextProps = { ...props };
|
|
89
|
+
DESIGNER_PROP_NAMES.forEach((name) => {
|
|
90
|
+
delete nextProps[name];
|
|
91
|
+
});
|
|
92
|
+
return nextProps;
|
|
93
|
+
};
|
|
94
|
+
const isEventHandlerPropName = (key) => /^on[A-Z]/.test(key);
|
|
95
|
+
const isRecord = (value) => (typeof value === 'object' && value !== null && !Array.isArray(value));
|
|
96
|
+
const UNSAFE_OBJECT_KEYS = new Set([
|
|
97
|
+
'__proto__',
|
|
98
|
+
'constructor',
|
|
99
|
+
'prototype',
|
|
100
|
+
]);
|
|
101
|
+
const isSafeObjectKey = (key) => !UNSAFE_OBJECT_KEYS.has(key);
|
|
102
|
+
const getObjectPathSegments = (path) => (path.split('.').map(key => key.trim())
|
|
103
|
+
.filter(Boolean));
|
|
104
|
+
const isSafeObjectPath = (path) => {
|
|
105
|
+
const keys = getObjectPathSegments(path);
|
|
106
|
+
return keys.length > 0 && keys.every(isSafeObjectKey);
|
|
107
|
+
};
|
|
108
|
+
const createPreviewRecord = () => Object.create(null);
|
|
109
|
+
const isSchemaOnlyPropName = (key, componentName) => (!isComponentSchemaPropName(componentName, key)
|
|
110
|
+
&& (FORMILY_SCHEMA_PROP_NAMES.has(key)
|
|
111
|
+
|| RENDER_ONLY_SCHEMA_PROP_NAMES.has(key)
|
|
112
|
+
|| INTERNAL_DESIGNABLE_SCHEMA_PROP_NAMES.has(key)
|
|
113
|
+
|| key.startsWith('x-')));
|
|
114
|
+
const isSafeDataSourceUrl = (url) => {
|
|
115
|
+
const value = url.trim();
|
|
116
|
+
if (!value || value.startsWith('//'))
|
|
117
|
+
return false;
|
|
118
|
+
if (/^https?:\/\//i.test(value))
|
|
119
|
+
return true;
|
|
120
|
+
if (value.startsWith('/') || value.startsWith('./') || value.startsWith('../'))
|
|
121
|
+
return true;
|
|
122
|
+
return false;
|
|
123
|
+
};
|
|
124
|
+
const sanitizePreviewStyle = (style) => {
|
|
125
|
+
if (!isRecord(style))
|
|
126
|
+
return undefined;
|
|
127
|
+
const nextStyle = { ...style };
|
|
128
|
+
const position = typeof nextStyle.position === 'string'
|
|
129
|
+
? nextStyle.position.trim().toLowerCase()
|
|
130
|
+
: undefined;
|
|
131
|
+
if (position && PREVIEW_ESCAPE_POSITION_VALUES.has(position)) {
|
|
132
|
+
delete nextStyle.position;
|
|
133
|
+
}
|
|
134
|
+
PREVIEW_UNSAFE_STYLE_PROP_NAMES.forEach((name) => {
|
|
135
|
+
delete nextStyle[name];
|
|
136
|
+
});
|
|
137
|
+
Object.keys(nextStyle).forEach((name) => {
|
|
138
|
+
const value = nextStyle[name];
|
|
139
|
+
if (typeof value === 'string' && hasUnsafeStyleValue(value)) {
|
|
140
|
+
delete nextStyle[name];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return Object.keys(nextStyle).length ? nextStyle : undefined;
|
|
144
|
+
};
|
|
145
|
+
const sanitizePreviewProps = (props, componentName) => {
|
|
146
|
+
const nextProps = removeDesignerProps(props);
|
|
147
|
+
Object.keys(nextProps).forEach((key) => {
|
|
148
|
+
if (!isSafeObjectKey(key)) {
|
|
149
|
+
delete nextProps[key];
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (isSchemaOnlyPropName(key, componentName)) {
|
|
153
|
+
delete nextProps[key];
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (PREVIEW_UNSAFE_PROP_NAMES.has(key)) {
|
|
157
|
+
delete nextProps[key];
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (isEventHandlerPropName(key)) {
|
|
161
|
+
delete nextProps[key];
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (key === 'style') {
|
|
165
|
+
const style = sanitizePreviewStyle(nextProps[key]);
|
|
166
|
+
if (style) {
|
|
167
|
+
nextProps[key] = style;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
delete nextProps[key];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return nextProps;
|
|
175
|
+
};
|
|
176
|
+
const sanitizePreviewDataSourceValue = (value) => {
|
|
177
|
+
if (Array.isArray(value))
|
|
178
|
+
return value.map(sanitizePreviewDataSourceValue);
|
|
179
|
+
if (!isRecord(value))
|
|
180
|
+
return value;
|
|
181
|
+
const result = createPreviewRecord();
|
|
182
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
183
|
+
if (!isSafeObjectKey(key))
|
|
184
|
+
return;
|
|
185
|
+
const safeItem = sanitizePreviewDataSourceValue(item);
|
|
186
|
+
if (isRecord(safeItem) && Object.keys(safeItem).length === 0)
|
|
187
|
+
return;
|
|
188
|
+
result[key] = safeItem;
|
|
189
|
+
});
|
|
190
|
+
return result;
|
|
191
|
+
};
|
|
192
|
+
const getSafeDataSourceStringProp = (dataSource, key, fallback) => {
|
|
193
|
+
const rawValue = dataSource[key];
|
|
194
|
+
const value = typeof rawValue === 'string' && rawValue.trim()
|
|
195
|
+
? rawValue
|
|
196
|
+
: fallback;
|
|
197
|
+
return isSafeObjectKey(value) ? value : fallback;
|
|
198
|
+
};
|
|
199
|
+
const sanitizePreviewDataSourceEntry = (key, item) => {
|
|
200
|
+
if (!isSafeObjectKey(key) || item === undefined || item === null)
|
|
201
|
+
return {};
|
|
202
|
+
if (typeof item === 'string') {
|
|
203
|
+
const trimmedValue = item.trim();
|
|
204
|
+
if (!trimmedValue)
|
|
205
|
+
return {};
|
|
206
|
+
if (key === 'url' && !isSafeDataSourceUrl(trimmedValue)) {
|
|
207
|
+
return { unsafeUrl: true };
|
|
208
|
+
}
|
|
209
|
+
return { value: trimmedValue };
|
|
210
|
+
}
|
|
211
|
+
const safeItem = sanitizePreviewDataSourceValue(item);
|
|
212
|
+
if (isRecord(safeItem) && Object.keys(safeItem).length === 0)
|
|
213
|
+
return {};
|
|
214
|
+
return { value: safeItem };
|
|
215
|
+
};
|
|
216
|
+
const sanitizePreviewDataSource = (value) => {
|
|
217
|
+
if (!isRecord(value))
|
|
218
|
+
return undefined;
|
|
219
|
+
let hasUnsafeUrl = false;
|
|
220
|
+
const safeDataSource = createPreviewRecord();
|
|
221
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
222
|
+
const safeEntry = sanitizePreviewDataSourceEntry(key, item);
|
|
223
|
+
if (safeEntry.unsafeUrl) {
|
|
224
|
+
hasUnsafeUrl = true;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (safeEntry.value !== undefined) {
|
|
228
|
+
safeDataSource[key] = safeEntry.value;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
if (hasUnsafeUrl)
|
|
232
|
+
return undefined;
|
|
233
|
+
const keys = Object.keys(safeDataSource);
|
|
234
|
+
if (keys.length === 1 && safeDataSource.method === 'GET')
|
|
235
|
+
return undefined;
|
|
236
|
+
return keys.length ? safeDataSource : undefined;
|
|
237
|
+
};
|
|
238
|
+
const getSchemaComponentProps = (schema, componentName) => {
|
|
239
|
+
const props = isRecord(schema['x-component-props'])
|
|
240
|
+
? { ...schema['x-component-props'] }
|
|
241
|
+
: {};
|
|
242
|
+
Object.entries(schema).forEach(([key, value]) => {
|
|
243
|
+
if (props[key] === undefined && isComponentSchemaPropName(componentName, key)) {
|
|
244
|
+
props[key] = value;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
if (isRecord(schema.style)) {
|
|
248
|
+
props.style = {
|
|
249
|
+
...schema.style,
|
|
250
|
+
...(isRecord(props.style) ? props.style : {}),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
return sanitizePreviewProps(props, componentName);
|
|
254
|
+
};
|
|
255
|
+
const getErrorMessage = (error) => {
|
|
256
|
+
if (error instanceof Error)
|
|
257
|
+
return error.message;
|
|
258
|
+
if (typeof error === 'string' && error.trim())
|
|
259
|
+
return error;
|
|
260
|
+
return '预览渲染失败';
|
|
261
|
+
};
|
|
262
|
+
const MissingComponentNotice = ({ children, componentName }) => (_jsxs("div", { "data-preview-missing-component": componentName, style: {
|
|
263
|
+
background: '#fff7e6',
|
|
264
|
+
border: '1px dashed #ffc069',
|
|
265
|
+
borderRadius: 6,
|
|
266
|
+
boxSizing: 'border-box',
|
|
267
|
+
color: '#ad6800',
|
|
268
|
+
fontSize: 12,
|
|
269
|
+
lineHeight: 1.5,
|
|
270
|
+
margin: '4px 0',
|
|
271
|
+
padding: '7px 9px',
|
|
272
|
+
}, children: [_jsxs("div", { style: { fontWeight: 600 }, children: ["\u672A\u6CE8\u518C\u7EC4\u4EF6\uFF1A", componentName] }), children ? (_jsx("div", { style: { marginTop: 6 }, children: children })) : null] }));
|
|
273
|
+
const PreviewNodeErrorNotice = ({ componentName, error }) => (_jsxs("div", { "data-preview-node-error": componentName || 'unknown', role: "alert", style: {
|
|
274
|
+
background: '#fff1f0',
|
|
275
|
+
border: '1px dashed #ff7875',
|
|
276
|
+
borderRadius: 6,
|
|
277
|
+
boxSizing: 'border-box',
|
|
278
|
+
color: '#a8071a',
|
|
279
|
+
fontSize: 12,
|
|
280
|
+
lineHeight: 1.5,
|
|
281
|
+
margin: '4px 0',
|
|
282
|
+
padding: '7px 9px',
|
|
283
|
+
}, children: [_jsxs("div", { style: { fontWeight: 600 }, children: ["\u7EC4\u4EF6\u9884\u89C8\u5931\u8D25", componentName ? `:${componentName}` : ''] }), _jsx("div", { style: { marginTop: 6 }, children: getErrorMessage(error) })] }));
|
|
284
|
+
const stringifyOptionLabel = (value) => {
|
|
285
|
+
if (value === undefined || value === null)
|
|
286
|
+
return '';
|
|
287
|
+
if (typeof value === 'string')
|
|
288
|
+
return value;
|
|
289
|
+
return JSON.stringify(value);
|
|
290
|
+
};
|
|
291
|
+
const getSchemaOptionChildren = (item) => {
|
|
292
|
+
if (!Array.isArray(item.children))
|
|
293
|
+
return undefined;
|
|
294
|
+
return item.children
|
|
295
|
+
.map((child, childIndex) => normalizeSchemaOption(child, childIndex))
|
|
296
|
+
.filter((child) => Boolean(child));
|
|
297
|
+
};
|
|
298
|
+
const getSchemaOptionExtras = (item) => {
|
|
299
|
+
const children = getSchemaOptionChildren(item);
|
|
300
|
+
return {
|
|
301
|
+
...(children ? { children } : {}),
|
|
302
|
+
...(item.disabled === true ? { disabled: true } : {}),
|
|
303
|
+
...(item.description !== undefined ? { description: item.description } : {}),
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
const getSchemaOptionValue = (item) => (item.value ?? item.const ?? item.key);
|
|
307
|
+
const getSchemaOptionLabel = (item, index, value, labels) => (item.label ?? item.title ?? labels?.[index] ?? stringifyOptionLabel(value));
|
|
308
|
+
const normalizeSchemaOption = (item, index, labels) => {
|
|
309
|
+
if (!isRecord(item)) {
|
|
310
|
+
return {
|
|
311
|
+
label: labels?.[index] ?? stringifyOptionLabel(item),
|
|
312
|
+
value: item,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
const value = getSchemaOptionValue(item);
|
|
316
|
+
if (value === undefined)
|
|
317
|
+
return undefined;
|
|
318
|
+
return {
|
|
319
|
+
...getSchemaOptionExtras(item),
|
|
320
|
+
label: getSchemaOptionLabel(item, index, value, labels),
|
|
321
|
+
value,
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
const getOptionsFromEnumRecord = (source) => {
|
|
325
|
+
const options = Object.entries(source).map(([value, label]) => ({
|
|
326
|
+
label: isRecord(label)
|
|
327
|
+
? label.label ?? label.title ?? stringifyOptionLabel(value)
|
|
328
|
+
: label ?? stringifyOptionLabel(value),
|
|
329
|
+
value,
|
|
330
|
+
...(isRecord(label) && label.disabled === true ? { disabled: true } : {}),
|
|
331
|
+
...(isRecord(label) && label.description !== undefined ? { description: label.description } : {}),
|
|
332
|
+
}));
|
|
333
|
+
return options.length ? options : undefined;
|
|
334
|
+
};
|
|
335
|
+
const getOptionsFromEnum = (schema) => {
|
|
336
|
+
const schemaEnum = schema.enum;
|
|
337
|
+
if (isRecord(schemaEnum))
|
|
338
|
+
return getOptionsFromEnumRecord(schemaEnum);
|
|
339
|
+
if (!Array.isArray(schemaEnum))
|
|
340
|
+
return undefined;
|
|
341
|
+
const labels = Array.isArray(schema.enumNames) ? schema.enumNames : undefined;
|
|
342
|
+
const options = schemaEnum
|
|
343
|
+
.map((item, index) => normalizeSchemaOption(item, index, labels))
|
|
344
|
+
.filter((item) => Boolean(item));
|
|
345
|
+
return options.length ? options : undefined;
|
|
346
|
+
};
|
|
347
|
+
const getOptionsFromXEnum = (schema) => {
|
|
348
|
+
const xEnum = schema['x-enum'];
|
|
349
|
+
if (isRecord(xEnum))
|
|
350
|
+
return getOptionsFromEnumRecord(xEnum);
|
|
351
|
+
if (!Array.isArray(xEnum))
|
|
352
|
+
return undefined;
|
|
353
|
+
const options = xEnum
|
|
354
|
+
.map((item, index) => normalizeSchemaOption(item, index))
|
|
355
|
+
.filter((item) => Boolean(item));
|
|
356
|
+
return options.length ? options : undefined;
|
|
357
|
+
};
|
|
358
|
+
const getOptionsFromOneOf = (schema) => {
|
|
359
|
+
if (!Array.isArray(schema.oneOf))
|
|
360
|
+
return undefined;
|
|
361
|
+
const options = schema.oneOf
|
|
362
|
+
.filter(isRecord)
|
|
363
|
+
.map((item) => {
|
|
364
|
+
const value = item.const ?? item.value;
|
|
365
|
+
if (value === undefined)
|
|
366
|
+
return undefined;
|
|
367
|
+
return {
|
|
368
|
+
...(item.disabled === true ? { disabled: true } : {}),
|
|
369
|
+
...(item.description !== undefined ? { description: item.description } : {}),
|
|
370
|
+
label: item.title ?? item.label ?? stringifyOptionLabel(value),
|
|
371
|
+
value,
|
|
372
|
+
};
|
|
373
|
+
})
|
|
374
|
+
.filter((item) => Boolean(item));
|
|
375
|
+
return options.length ? options : undefined;
|
|
376
|
+
};
|
|
377
|
+
const getOptionsFromSchema = (componentName, schema) => {
|
|
378
|
+
if (!componentName || !OPTION_COMPONENT_NAMES.has(componentName))
|
|
379
|
+
return undefined;
|
|
380
|
+
return getOptionsFromOneOf(schema) ?? getOptionsFromXEnum(schema) ?? getOptionsFromEnum(schema);
|
|
381
|
+
};
|
|
382
|
+
const getOptionPropName = (componentName) => {
|
|
383
|
+
if (componentName === 'TreeSelect')
|
|
384
|
+
return 'treeData';
|
|
385
|
+
if (componentName === 'Transfer')
|
|
386
|
+
return 'dataSource';
|
|
387
|
+
return 'options';
|
|
388
|
+
};
|
|
389
|
+
const hasExplicitOptionProp = (componentName, props) => (props.options !== undefined
|
|
390
|
+
|| (getOptionPropName(componentName) === 'treeData' && props.treeData !== undefined)
|
|
391
|
+
|| (getOptionPropName(componentName) === 'dataSource' && props.dataSource !== undefined));
|
|
392
|
+
const toTreeSelectData = (options) => (options.map(option => ({
|
|
393
|
+
...(option.disabled === true ? { disabled: true } : {}),
|
|
394
|
+
...(option.children?.length ? { children: toTreeSelectData(option.children) } : {}),
|
|
395
|
+
title: option.label,
|
|
396
|
+
value: option.value,
|
|
397
|
+
})));
|
|
398
|
+
const toTransferDataSource = (options) => (options.map(option => ({
|
|
399
|
+
...(option.disabled === true ? { disabled: true } : {}),
|
|
400
|
+
key: typeof option.value === 'string' || typeof option.value === 'number'
|
|
401
|
+
? option.value
|
|
402
|
+
: String(option.value),
|
|
403
|
+
title: option.label,
|
|
404
|
+
})));
|
|
405
|
+
const toOptionProps = (componentName, options) => {
|
|
406
|
+
const propName = getOptionPropName(componentName);
|
|
407
|
+
if (propName === 'treeData')
|
|
408
|
+
return { treeData: toTreeSelectData(options) };
|
|
409
|
+
if (propName === 'dataSource')
|
|
410
|
+
return { dataSource: toTransferDataSource(options) };
|
|
411
|
+
return { options };
|
|
412
|
+
};
|
|
413
|
+
const withSchemaDerivedOptions = (componentName, schema, props) => {
|
|
414
|
+
if (hasExplicitOptionProp(componentName, props))
|
|
415
|
+
return props;
|
|
416
|
+
const options = getOptionsFromSchema(componentName, schema);
|
|
417
|
+
return options ? { ...props, ...toOptionProps(componentName, options) } : props;
|
|
418
|
+
};
|
|
419
|
+
const getValueByPath = (value, path) => {
|
|
420
|
+
if (typeof path !== 'string' || !path.trim())
|
|
421
|
+
return value;
|
|
422
|
+
const keys = getObjectPathSegments(path);
|
|
423
|
+
if (!keys.length || !keys.every(isSafeObjectKey))
|
|
424
|
+
return undefined;
|
|
425
|
+
return keys.reduce((current, key) => {
|
|
426
|
+
if (!isRecord(current))
|
|
427
|
+
return undefined;
|
|
428
|
+
return current[key];
|
|
429
|
+
}, value);
|
|
430
|
+
};
|
|
431
|
+
const normalizeDataSourceItems = (source, dataSource) => {
|
|
432
|
+
const labelKey = getSafeDataSourceStringProp(dataSource, 'label', 'label');
|
|
433
|
+
const valueKey = getSafeDataSourceStringProp(dataSource, 'value', 'value');
|
|
434
|
+
const getDataSourceOptionExtras = (item) => ({
|
|
435
|
+
...(Array.isArray(item.children)
|
|
436
|
+
? { children: normalizeDataSourceItems(item.children, dataSource) }
|
|
437
|
+
: {}),
|
|
438
|
+
...(item.disabled === true ? { disabled: true } : {}),
|
|
439
|
+
...(item.description !== undefined ? { description: item.description } : {}),
|
|
440
|
+
});
|
|
441
|
+
const options = source.map((item) => {
|
|
442
|
+
if (!isRecord(item)) {
|
|
443
|
+
return {
|
|
444
|
+
label: stringifyOptionLabel(item),
|
|
445
|
+
value: item,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
const value = item[valueKey] ?? item.value ?? item.id;
|
|
449
|
+
if (value === undefined)
|
|
450
|
+
return undefined;
|
|
451
|
+
return {
|
|
452
|
+
...getDataSourceOptionExtras(item),
|
|
453
|
+
label: item[labelKey] ?? item.label ?? item.title ?? stringifyOptionLabel(value),
|
|
454
|
+
value,
|
|
455
|
+
};
|
|
456
|
+
}).filter((item) => Boolean(item));
|
|
457
|
+
return options.length ? options : undefined;
|
|
458
|
+
};
|
|
459
|
+
const getDataSourceArrayByPath = (response, dataSource) => {
|
|
460
|
+
const { dataPath } = dataSource;
|
|
461
|
+
if (typeof dataPath !== 'string' || !dataPath.trim())
|
|
462
|
+
return undefined;
|
|
463
|
+
const data = getValueByPath(response, dataPath);
|
|
464
|
+
if (Array.isArray(data))
|
|
465
|
+
return data;
|
|
466
|
+
if (isRecord(data) && Array.isArray(data.data))
|
|
467
|
+
return data.data;
|
|
468
|
+
return undefined;
|
|
469
|
+
};
|
|
470
|
+
const getDefaultDataSourceArray = (response) => {
|
|
471
|
+
if (Array.isArray(response))
|
|
472
|
+
return response;
|
|
473
|
+
if (isRecord(response) && Array.isArray(response.data))
|
|
474
|
+
return response.data;
|
|
475
|
+
return undefined;
|
|
476
|
+
};
|
|
477
|
+
const normalizeDataSourceOptions = (response, dataSource) => {
|
|
478
|
+
const hasDataPath = typeof dataSource.dataPath === 'string' && dataSource.dataPath.trim().length > 0;
|
|
479
|
+
const source = hasDataPath
|
|
480
|
+
? getDataSourceArrayByPath(response, dataSource)
|
|
481
|
+
: getDefaultDataSourceArray(response);
|
|
482
|
+
return source ? normalizeDataSourceItems(source, dataSource) : undefined;
|
|
483
|
+
};
|
|
484
|
+
const usePreviewDataSourceProps = (componentName, schema, props, enabled = true) => {
|
|
485
|
+
const { previewDataSourceLoader } = usePlaygroundContext();
|
|
486
|
+
const dataSource = schema['x-data'];
|
|
487
|
+
const safeDataSource = React.useMemo(() => sanitizePreviewDataSource(dataSource), [dataSource]);
|
|
488
|
+
const hasExplicitOptions = props.options !== undefined
|
|
489
|
+
|| (getOptionPropName(componentName) === 'treeData' && props.treeData !== undefined)
|
|
490
|
+
|| (getOptionPropName(componentName) === 'dataSource' && props.dataSource !== undefined);
|
|
491
|
+
const [state, setState] = React.useState({});
|
|
492
|
+
const requestKey = React.useMemo(() => ({
|
|
493
|
+
componentName,
|
|
494
|
+
previewDataSourceLoader,
|
|
495
|
+
safeDataSource,
|
|
496
|
+
schema,
|
|
497
|
+
}), [componentName, previewDataSourceLoader, safeDataSource, schema]);
|
|
498
|
+
const canExposeDataSourceState = enabled
|
|
499
|
+
&& !isSchemaHidden(schema)
|
|
500
|
+
&& Boolean(previewDataSourceLoader)
|
|
501
|
+
&& Boolean(componentName)
|
|
502
|
+
&& OPTION_COMPONENT_NAMES.has(componentName ?? '')
|
|
503
|
+
&& !hasExplicitOptions
|
|
504
|
+
&& Boolean(safeDataSource);
|
|
505
|
+
React.useEffect(() => {
|
|
506
|
+
if (!enabled
|
|
507
|
+
|| isSchemaHidden(schema)
|
|
508
|
+
|| !previewDataSourceLoader
|
|
509
|
+
|| !componentName
|
|
510
|
+
|| !OPTION_COMPONENT_NAMES.has(componentName)
|
|
511
|
+
|| hasExplicitOptions
|
|
512
|
+
|| !safeDataSource) {
|
|
513
|
+
return undefined;
|
|
514
|
+
}
|
|
515
|
+
let disposed = false;
|
|
516
|
+
let response;
|
|
517
|
+
try {
|
|
518
|
+
response = previewDataSourceLoader({
|
|
519
|
+
componentName,
|
|
520
|
+
dataSource: safeDataSource,
|
|
521
|
+
schema,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
catch (error) {
|
|
525
|
+
void Promise.resolve().then(() => {
|
|
526
|
+
if (disposed)
|
|
527
|
+
return;
|
|
528
|
+
setState({
|
|
529
|
+
error: getErrorMessage(error),
|
|
530
|
+
requestKey,
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
return () => {
|
|
534
|
+
disposed = true;
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
void Promise.resolve(response)
|
|
538
|
+
.then((nextResponse) => {
|
|
539
|
+
if (disposed)
|
|
540
|
+
return;
|
|
541
|
+
setState({
|
|
542
|
+
options: normalizeDataSourceOptions(nextResponse, safeDataSource),
|
|
543
|
+
requestKey,
|
|
544
|
+
});
|
|
545
|
+
})
|
|
546
|
+
.catch((error) => {
|
|
547
|
+
if (disposed)
|
|
548
|
+
return;
|
|
549
|
+
setState({
|
|
550
|
+
error: getErrorMessage(error),
|
|
551
|
+
requestKey,
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
return () => {
|
|
555
|
+
disposed = true;
|
|
556
|
+
};
|
|
557
|
+
}, [
|
|
558
|
+
componentName,
|
|
559
|
+
enabled,
|
|
560
|
+
hasExplicitOptions,
|
|
561
|
+
previewDataSourceLoader,
|
|
562
|
+
requestKey,
|
|
563
|
+
safeDataSource,
|
|
564
|
+
schema,
|
|
565
|
+
]);
|
|
566
|
+
if (!canExposeDataSourceState)
|
|
567
|
+
return {};
|
|
568
|
+
const isCurrentRequest = state.requestKey === requestKey;
|
|
569
|
+
const canExposeLoading = componentName
|
|
570
|
+
? DATA_SOURCE_LOADING_COMPONENT_NAMES.has(componentName)
|
|
571
|
+
: false;
|
|
572
|
+
return {
|
|
573
|
+
...(!isCurrentRequest && canExposeLoading ? { loading: true } : {}),
|
|
574
|
+
...(isCurrentRequest && state.error ? {
|
|
575
|
+
'data-preview-data-source-error': state.error,
|
|
576
|
+
status: 'error',
|
|
577
|
+
} : {}),
|
|
578
|
+
...(isCurrentRequest && state.options ? toOptionProps(componentName, state.options) : {}),
|
|
579
|
+
};
|
|
580
|
+
};
|
|
581
|
+
const isSchemaHidden = (schema) => (schema['x-hidden'] === true
|
|
582
|
+
|| schema['x-visible'] === false
|
|
583
|
+
|| schema['x-display'] === 'hidden'
|
|
584
|
+
|| schema['x-display'] === 'none'
|
|
585
|
+
|| isStaticReactionHidden(schema));
|
|
586
|
+
const getReactionList = (schema) => {
|
|
587
|
+
const reactions = schema['x-reactions'];
|
|
588
|
+
if (Array.isArray(reactions))
|
|
589
|
+
return reactions.filter(isRecord);
|
|
590
|
+
return isRecord(reactions) ? [reactions] : [];
|
|
591
|
+
};
|
|
592
|
+
const getDependencyNames = (reaction) => {
|
|
593
|
+
const { dependencies } = reaction;
|
|
594
|
+
if (!Array.isArray(dependencies))
|
|
595
|
+
return [];
|
|
596
|
+
return dependencies.filter((item) => (typeof item === 'string'
|
|
597
|
+
&& isSafeObjectPath(item)));
|
|
598
|
+
};
|
|
599
|
+
const hasReactionTarget = (reaction) => (typeof reaction.target === 'string' && reaction.target.trim().length > 0);
|
|
600
|
+
const getReactionTargetName = (reaction) => (hasReactionTarget(reaction) && isSafeObjectPath(reaction.target)
|
|
601
|
+
? reaction.target.trim()
|
|
602
|
+
: undefined);
|
|
603
|
+
const parseLiteral = (value) => {
|
|
604
|
+
const trimmed = value.trim();
|
|
605
|
+
if (trimmed === 'true')
|
|
606
|
+
return true;
|
|
607
|
+
if (trimmed === 'false')
|
|
608
|
+
return false;
|
|
609
|
+
if (trimmed === 'null')
|
|
610
|
+
return null;
|
|
611
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed))
|
|
612
|
+
return Number(trimmed);
|
|
613
|
+
const quoted = trimmed.match(/^(['"])(.*)\1$/);
|
|
614
|
+
return quoted ? quoted[2] : undefined;
|
|
615
|
+
};
|
|
616
|
+
const getDependencyValueByToken = (deps, token) => {
|
|
617
|
+
const match = token.trim().match(/^\$deps\[(\d+)\]$/);
|
|
618
|
+
if (!match)
|
|
619
|
+
return undefined;
|
|
620
|
+
return deps[Number(match[1])];
|
|
621
|
+
};
|
|
622
|
+
const getReactionDependencyValue = (values, name) => {
|
|
623
|
+
if (Object.prototype.hasOwnProperty.call(values, name)) {
|
|
624
|
+
return values[name];
|
|
625
|
+
}
|
|
626
|
+
return getValueByPath(values, name);
|
|
627
|
+
};
|
|
628
|
+
const getReactionDependencyValues = (values, dependencyNames) => (dependencyNames.map(name => getReactionDependencyValue(values, name)));
|
|
629
|
+
const getReactionExpressionTokenValue = (deps, token, selfValue) => {
|
|
630
|
+
const trimmed = token.trim();
|
|
631
|
+
if (trimmed === '$self.value')
|
|
632
|
+
return selfValue;
|
|
633
|
+
return getDependencyValueByToken(deps, trimmed);
|
|
634
|
+
};
|
|
635
|
+
const reactionNumberComparators = {
|
|
636
|
+
'<': (left, right) => left < right,
|
|
637
|
+
'<=': (left, right) => left <= right,
|
|
638
|
+
'>': (left, right) => left > right,
|
|
639
|
+
'>=': (left, right) => left >= right,
|
|
640
|
+
};
|
|
641
|
+
const compareReactionExpressionValues = (left, operator, right) => {
|
|
642
|
+
if (operator === '!==' || operator === '!=')
|
|
643
|
+
return left !== right;
|
|
644
|
+
const numberComparator = reactionNumberComparators[operator];
|
|
645
|
+
if (!numberComparator)
|
|
646
|
+
return left === right;
|
|
647
|
+
if (typeof left !== 'number' || typeof right !== 'number')
|
|
648
|
+
return undefined;
|
|
649
|
+
return numberComparator(left, right);
|
|
650
|
+
};
|
|
651
|
+
const evaluateReactionComparison = (expression, deps, selfValue) => {
|
|
652
|
+
const comparison = expression.match(/^(\$deps\[\d+\]|\$self\.value)\s*(===|!==|==|!=|>=|<=|>|<)\s*(.+)$/);
|
|
653
|
+
if (!comparison)
|
|
654
|
+
return { matched: false };
|
|
655
|
+
const left = getReactionExpressionTokenValue(deps, comparison[1], selfValue);
|
|
656
|
+
const right = parseLiteral(comparison[3]);
|
|
657
|
+
if (right === undefined)
|
|
658
|
+
return { matched: true, value: undefined };
|
|
659
|
+
return {
|
|
660
|
+
matched: true,
|
|
661
|
+
value: compareReactionExpressionValues(left, comparison[2], right),
|
|
662
|
+
};
|
|
663
|
+
};
|
|
664
|
+
const evaluateReactionExpression = (value, deps, selfValue) => {
|
|
665
|
+
if (typeof value !== 'string')
|
|
666
|
+
return value;
|
|
667
|
+
const match = value.trim().match(/^\{\{\s*(.*?)\s*\}\}$/);
|
|
668
|
+
if (!match)
|
|
669
|
+
return value;
|
|
670
|
+
const expression = match[1].trim();
|
|
671
|
+
if (!expression)
|
|
672
|
+
return undefined;
|
|
673
|
+
if (expression.startsWith('!')) {
|
|
674
|
+
const dependencyValue = getReactionExpressionTokenValue(deps, expression.slice(1), selfValue);
|
|
675
|
+
return dependencyValue === undefined ? undefined : !dependencyValue;
|
|
676
|
+
}
|
|
677
|
+
const comparison = evaluateReactionComparison(expression, deps, selfValue);
|
|
678
|
+
if (comparison.matched)
|
|
679
|
+
return comparison.value;
|
|
680
|
+
const literal = parseLiteral(expression);
|
|
681
|
+
if (literal !== undefined)
|
|
682
|
+
return literal;
|
|
683
|
+
return getReactionExpressionTokenValue(deps, expression, selfValue);
|
|
684
|
+
};
|
|
685
|
+
const getReactionStateBranch = (reaction, deps, selfValue) => {
|
|
686
|
+
const hasWhen = Object.prototype.hasOwnProperty.call(reaction, 'when');
|
|
687
|
+
const branch = hasWhen && !evaluateReactionExpression(reaction.when, deps, selfValue)
|
|
688
|
+
? reaction.otherwise
|
|
689
|
+
: reaction.fulfill;
|
|
690
|
+
if (!isRecord(branch) || !isRecord(branch.state))
|
|
691
|
+
return undefined;
|
|
692
|
+
return branch.state;
|
|
693
|
+
};
|
|
694
|
+
const evaluateReactionState = (state, deps, selfValue) => {
|
|
695
|
+
const nextState = {};
|
|
696
|
+
Object.entries(state).forEach(([key, value]) => {
|
|
697
|
+
const evaluated = evaluateReactionExpression(value, deps, selfValue);
|
|
698
|
+
if (evaluated !== undefined) {
|
|
699
|
+
nextState[key] = evaluated;
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
return Object.keys(nextState).length ? nextState : undefined;
|
|
703
|
+
};
|
|
704
|
+
const evaluateReactionSchemaValue = (value, deps, selfValue) => {
|
|
705
|
+
if (Array.isArray(value)) {
|
|
706
|
+
return value.map(item => evaluateReactionSchemaValue(item, deps, selfValue));
|
|
707
|
+
}
|
|
708
|
+
if (isRecord(value)) {
|
|
709
|
+
return Object.fromEntries(Object.entries(value)
|
|
710
|
+
.filter(([key]) => isSafeObjectKey(key))
|
|
711
|
+
.map(([key, item]) => [
|
|
712
|
+
key,
|
|
713
|
+
evaluateReactionSchemaValue(item, deps, selfValue),
|
|
714
|
+
]));
|
|
715
|
+
}
|
|
716
|
+
return evaluateReactionExpression(value, deps, selfValue);
|
|
717
|
+
};
|
|
718
|
+
const evaluateReactionSchema = (schemaPatch, deps, selfValue) => {
|
|
719
|
+
const evaluated = evaluateReactionSchemaValue(schemaPatch, deps, selfValue);
|
|
720
|
+
return isRecord(evaluated) && Object.keys(evaluated).length ? evaluated : undefined;
|
|
721
|
+
};
|
|
722
|
+
const getReactionSchemaBranch = (reaction, deps, selfValue) => {
|
|
723
|
+
const hasWhen = Object.prototype.hasOwnProperty.call(reaction, 'when');
|
|
724
|
+
const branch = hasWhen && !evaluateReactionExpression(reaction.when, deps, selfValue)
|
|
725
|
+
? reaction.otherwise
|
|
726
|
+
: reaction.fulfill;
|
|
727
|
+
if (!isRecord(branch) || !isRecord(branch.schema))
|
|
728
|
+
return undefined;
|
|
729
|
+
return branch.schema;
|
|
730
|
+
};
|
|
731
|
+
const getStaticReactionStates = (schema) => (getReactionList(schema)
|
|
732
|
+
.filter(reaction => !hasReactionTarget(reaction) && getDependencyNames(reaction).length === 0)
|
|
733
|
+
.map(reaction => getReactionStateBranch(reaction, []))
|
|
734
|
+
.filter((state) => Boolean(state)));
|
|
735
|
+
const getDynamicReactionStates = (schema, values) => (getReactionList(schema)
|
|
736
|
+
.map((reaction) => {
|
|
737
|
+
if (hasReactionTarget(reaction))
|
|
738
|
+
return undefined;
|
|
739
|
+
const dependencyNames = getDependencyNames(reaction);
|
|
740
|
+
if (!dependencyNames.length)
|
|
741
|
+
return undefined;
|
|
742
|
+
const deps = getReactionDependencyValues(values, dependencyNames);
|
|
743
|
+
const state = getReactionStateBranch(reaction, deps);
|
|
744
|
+
return state ? evaluateReactionState(state, deps) : undefined;
|
|
745
|
+
})
|
|
746
|
+
.filter((state) => Boolean(state)));
|
|
747
|
+
const getStaticReactionSchemas = (schema) => (getReactionList(schema)
|
|
748
|
+
.filter(reaction => !hasReactionTarget(reaction) && getDependencyNames(reaction).length === 0)
|
|
749
|
+
.map(reaction => getReactionSchemaBranch(reaction, []))
|
|
750
|
+
.filter((schemaPatch) => Boolean(schemaPatch)));
|
|
751
|
+
const getDynamicReactionSchemas = (schema, values) => (getReactionList(schema)
|
|
752
|
+
.map((reaction) => {
|
|
753
|
+
if (hasReactionTarget(reaction))
|
|
754
|
+
return undefined;
|
|
755
|
+
const dependencyNames = getDependencyNames(reaction);
|
|
756
|
+
if (!dependencyNames.length)
|
|
757
|
+
return undefined;
|
|
758
|
+
const deps = getReactionDependencyValues(values, dependencyNames);
|
|
759
|
+
const schemaPatch = getReactionSchemaBranch(reaction, deps);
|
|
760
|
+
return schemaPatch ? evaluateReactionSchema(schemaPatch, deps) : undefined;
|
|
761
|
+
})
|
|
762
|
+
.filter((schemaPatch) => Boolean(schemaPatch)));
|
|
763
|
+
const clonePreviewValue = (value) => {
|
|
764
|
+
if (Array.isArray(value))
|
|
765
|
+
return value.map(clonePreviewValue);
|
|
766
|
+
if (isRecord(value)) {
|
|
767
|
+
return Object.fromEntries(Object.entries(value)
|
|
768
|
+
.filter(([key]) => isSafeObjectKey(key))
|
|
769
|
+
.map(([key, item]) => [key, clonePreviewValue(item)]));
|
|
770
|
+
}
|
|
771
|
+
return value;
|
|
772
|
+
};
|
|
773
|
+
const mergePreviewSchemaPatch = (target, patch) => {
|
|
774
|
+
const result = { ...target };
|
|
775
|
+
Object.entries(patch).forEach(([key, value]) => {
|
|
776
|
+
if (!isSafeObjectKey(key))
|
|
777
|
+
return;
|
|
778
|
+
const current = result[key];
|
|
779
|
+
if (isRecord(current) && isRecord(value)) {
|
|
780
|
+
result[key] = mergePreviewSchemaPatch(current, value);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
result[key] = clonePreviewValue(value);
|
|
784
|
+
});
|
|
785
|
+
return result;
|
|
786
|
+
};
|
|
787
|
+
const applyReactionSchemaPatches = (schema, patches) => (patches.length
|
|
788
|
+
? patches.reduce((current, patch) => mergePreviewSchemaPatch(current, patch), clonePreviewValue(schema))
|
|
789
|
+
: schema);
|
|
790
|
+
const getSchemaItemEntriesForRuntime = (items) => {
|
|
791
|
+
if (Array.isArray(items)) {
|
|
792
|
+
return items
|
|
793
|
+
.filter(isRecord)
|
|
794
|
+
.map((item, index) => [`items_${index}`, item]);
|
|
795
|
+
}
|
|
796
|
+
if (isRecord(items))
|
|
797
|
+
return [['items', items]];
|
|
798
|
+
return [];
|
|
799
|
+
};
|
|
800
|
+
const getSchemaChildEntriesForRuntime = (schema) => {
|
|
801
|
+
const propertyEntries = isRecord(schema.properties)
|
|
802
|
+
? Object.entries(schema.properties).filter((entry) => isRecord(entry[1]))
|
|
803
|
+
: [];
|
|
804
|
+
const itemEntries = getSchemaItemEntriesForRuntime(schema.items);
|
|
805
|
+
return [
|
|
806
|
+
...itemEntries,
|
|
807
|
+
...propertyEntries,
|
|
808
|
+
];
|
|
809
|
+
};
|
|
810
|
+
const collectTargetReactionStates = (schema, values) => {
|
|
811
|
+
const targetStates = createPreviewRecord();
|
|
812
|
+
const addTargetState = (targetName, state) => {
|
|
813
|
+
if (!isSafeObjectKey(targetName))
|
|
814
|
+
return;
|
|
815
|
+
targetStates[targetName] = [
|
|
816
|
+
...(targetStates[targetName] ?? []),
|
|
817
|
+
state,
|
|
818
|
+
];
|
|
819
|
+
};
|
|
820
|
+
const visit = (node, key, parentPath) => {
|
|
821
|
+
const schemaPath = key ? [parentPath, key].filter(Boolean).join('.') : parentPath;
|
|
822
|
+
const selfValue = getPreviewValueByKeys(values, getPreviewCanonicalValueKeys(node, key, schemaPath));
|
|
823
|
+
getReactionList(node).forEach((reaction) => {
|
|
824
|
+
const targetName = getReactionTargetName(reaction);
|
|
825
|
+
if (!targetName)
|
|
826
|
+
return;
|
|
827
|
+
const dependencyNames = getDependencyNames(reaction);
|
|
828
|
+
const deps = getReactionDependencyValues(values, dependencyNames);
|
|
829
|
+
const state = getReactionStateBranch(reaction, deps, selfValue);
|
|
830
|
+
const evaluatedState = state ? evaluateReactionState(state, deps, selfValue) : undefined;
|
|
831
|
+
if (evaluatedState) {
|
|
832
|
+
addTargetState(targetName, evaluatedState);
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
getSchemaChildEntriesForRuntime(node).forEach(([childKey, child]) => {
|
|
836
|
+
visit(child, childKey, schemaPath);
|
|
837
|
+
});
|
|
838
|
+
};
|
|
839
|
+
getSchemaChildEntriesForRuntime(schema).forEach(([key, child]) => {
|
|
840
|
+
visit(child, key);
|
|
841
|
+
});
|
|
842
|
+
return targetStates;
|
|
843
|
+
};
|
|
844
|
+
const collectTargetReactionSchemas = (schema, values) => {
|
|
845
|
+
const targetSchemas = createPreviewRecord();
|
|
846
|
+
const addTargetSchema = (targetName, schemaPatch) => {
|
|
847
|
+
if (!isSafeObjectKey(targetName))
|
|
848
|
+
return;
|
|
849
|
+
targetSchemas[targetName] = [
|
|
850
|
+
...(targetSchemas[targetName] ?? []),
|
|
851
|
+
schemaPatch,
|
|
852
|
+
];
|
|
853
|
+
};
|
|
854
|
+
const visit = (node, key, parentPath) => {
|
|
855
|
+
const schemaPath = key ? [parentPath, key].filter(Boolean).join('.') : parentPath;
|
|
856
|
+
const selfValue = getPreviewValueByKeys(values, getPreviewCanonicalValueKeys(node, key, schemaPath));
|
|
857
|
+
getReactionList(node).forEach((reaction) => {
|
|
858
|
+
const targetName = getReactionTargetName(reaction);
|
|
859
|
+
if (!targetName)
|
|
860
|
+
return;
|
|
861
|
+
const dependencyNames = getDependencyNames(reaction);
|
|
862
|
+
const deps = getReactionDependencyValues(values, dependencyNames);
|
|
863
|
+
const schemaPatch = getReactionSchemaBranch(reaction, deps, selfValue);
|
|
864
|
+
const evaluatedSchema = schemaPatch ? evaluateReactionSchema(schemaPatch, deps, selfValue) : undefined;
|
|
865
|
+
if (evaluatedSchema) {
|
|
866
|
+
addTargetSchema(targetName, evaluatedSchema);
|
|
867
|
+
}
|
|
868
|
+
});
|
|
869
|
+
getSchemaChildEntriesForRuntime(node).forEach(([childKey, child]) => {
|
|
870
|
+
visit(child, childKey, schemaPath);
|
|
871
|
+
});
|
|
872
|
+
};
|
|
873
|
+
getSchemaChildEntriesForRuntime(schema).forEach(([key, child]) => {
|
|
874
|
+
visit(child, key);
|
|
875
|
+
});
|
|
876
|
+
return targetSchemas;
|
|
877
|
+
};
|
|
878
|
+
const isStaticReactionHidden = (schema) => (getStaticReactionStates(schema).some(state => (state.hidden === true
|
|
879
|
+
|| state.visible === false
|
|
880
|
+
|| state.display === 'hidden'
|
|
881
|
+
|| state.display === 'none')));
|
|
882
|
+
const isReactionStateHidden = (states) => (states.some(state => (state.hidden === true
|
|
883
|
+
|| state.visible === false
|
|
884
|
+
|| state.display === 'hidden'
|
|
885
|
+
|| state.display === 'none')));
|
|
886
|
+
const getStaticSchemaStateProps = (schema, extraStates = []) => {
|
|
887
|
+
const stateProps = {};
|
|
888
|
+
const pattern = schema['x-pattern'];
|
|
889
|
+
const reactionStates = [
|
|
890
|
+
...getStaticReactionStates(schema),
|
|
891
|
+
...extraStates,
|
|
892
|
+
];
|
|
893
|
+
if (schema['x-disabled'] === true || pattern === 'disabled') {
|
|
894
|
+
stateProps.disabled = true;
|
|
895
|
+
}
|
|
896
|
+
if (schema['x-read-only'] === true || schema.readOnly === true || pattern === 'readOnly') {
|
|
897
|
+
stateProps.readOnly = true;
|
|
898
|
+
}
|
|
899
|
+
if (schema['x-read-pretty'] === true || pattern === 'readPretty') {
|
|
900
|
+
stateProps.readPretty = true;
|
|
901
|
+
}
|
|
902
|
+
reactionStates.forEach((state) => {
|
|
903
|
+
if (state.disabled === true || state.pattern === 'disabled') {
|
|
904
|
+
stateProps.disabled = true;
|
|
905
|
+
}
|
|
906
|
+
if (state.readOnly === true || state.pattern === 'readOnly') {
|
|
907
|
+
stateProps.readOnly = true;
|
|
908
|
+
}
|
|
909
|
+
if (state.readPretty === true || state.pattern === 'readPretty') {
|
|
910
|
+
stateProps.readPretty = true;
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
return stateProps;
|
|
914
|
+
};
|
|
915
|
+
const getStaticDefaultValueProps = (componentName, schema, props) => {
|
|
916
|
+
if (!Object.prototype.hasOwnProperty.call(schema, 'default'))
|
|
917
|
+
return {};
|
|
918
|
+
if (componentName === 'Switch' || (componentName === 'Checkbox' && props.options === undefined)) {
|
|
919
|
+
if (props.checked !== undefined || props.defaultChecked !== undefined)
|
|
920
|
+
return {};
|
|
921
|
+
return { defaultChecked: Boolean(schema.default) };
|
|
922
|
+
}
|
|
923
|
+
if (props.value !== undefined || props.defaultValue !== undefined)
|
|
924
|
+
return {};
|
|
925
|
+
return { defaultValue: schema.default };
|
|
926
|
+
};
|
|
927
|
+
const getPreviewFieldName = (schema, schemaKey) => {
|
|
928
|
+
const componentProps = isRecord(schema['x-component-props'])
|
|
929
|
+
? schema['x-component-props']
|
|
930
|
+
: {};
|
|
931
|
+
const name = componentProps.name ?? schema.name ?? schemaKey;
|
|
932
|
+
return typeof name === 'string' && name.trim() ? name.trim() : undefined;
|
|
933
|
+
};
|
|
934
|
+
const getPreviewTargetKeys = (schema, schemaKey, schemaPath) => {
|
|
935
|
+
const keys = new Set();
|
|
936
|
+
const fieldName = getPreviewFieldName(schema, schemaKey);
|
|
937
|
+
if (fieldName && isSafeObjectPath(fieldName))
|
|
938
|
+
keys.add(fieldName);
|
|
939
|
+
if (schemaKey && isSafeObjectKey(schemaKey))
|
|
940
|
+
keys.add(schemaKey);
|
|
941
|
+
if (schemaPath && isSafeObjectPath(schemaPath))
|
|
942
|
+
keys.add(schemaPath);
|
|
943
|
+
return [...keys];
|
|
944
|
+
};
|
|
945
|
+
const getPreviewCanonicalValueKeys = (schema, schemaKey, schemaPath) => {
|
|
946
|
+
const keys = new Set();
|
|
947
|
+
const componentProps = isRecord(schema['x-component-props'])
|
|
948
|
+
? schema['x-component-props']
|
|
949
|
+
: {};
|
|
950
|
+
const explicitName = componentProps.name ?? schema.name;
|
|
951
|
+
if (typeof explicitName === 'string' && explicitName.trim() && isSafeObjectPath(explicitName.trim())) {
|
|
952
|
+
keys.add(explicitName.trim());
|
|
953
|
+
}
|
|
954
|
+
if (schemaPath && isSafeObjectPath(schemaPath))
|
|
955
|
+
keys.add(schemaPath);
|
|
956
|
+
if (schemaKey && isSafeObjectKey(schemaKey))
|
|
957
|
+
keys.add(schemaKey);
|
|
958
|
+
getPreviewTargetKeys(schema, schemaKey, schemaPath).forEach(key => keys.add(key));
|
|
959
|
+
return [...keys];
|
|
960
|
+
};
|
|
961
|
+
const getPreviewValueByKeys = (values, keys) => {
|
|
962
|
+
for (const key of keys) {
|
|
963
|
+
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
|
964
|
+
return values[key];
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
for (const key of keys) {
|
|
968
|
+
const value = getValueByPath(values, key);
|
|
969
|
+
if (value !== undefined)
|
|
970
|
+
return value;
|
|
971
|
+
}
|
|
972
|
+
return undefined;
|
|
973
|
+
};
|
|
974
|
+
const getValueFromChangeArgs = (args) => {
|
|
975
|
+
const first = args[0];
|
|
976
|
+
if (isRecord(first)
|
|
977
|
+
&& isRecord(first.target)
|
|
978
|
+
&& ('checked' in first.target || 'value' in first.target)) {
|
|
979
|
+
const targetType = typeof first.target.type === 'string'
|
|
980
|
+
? first.target.type.toLowerCase()
|
|
981
|
+
: undefined;
|
|
982
|
+
if (targetType === 'checkbox' || targetType === 'radio') {
|
|
983
|
+
return first.target.checked;
|
|
984
|
+
}
|
|
985
|
+
return first.target.value ?? first.target.checked;
|
|
986
|
+
}
|
|
987
|
+
return first;
|
|
988
|
+
};
|
|
989
|
+
const withPreviewValueTracking = (props, fieldKeys, runtime) => {
|
|
990
|
+
const reactiveKeys = fieldKeys.filter(key => runtime.reactiveFieldNames.has(key));
|
|
991
|
+
if (!reactiveKeys.length)
|
|
992
|
+
return props;
|
|
993
|
+
return {
|
|
994
|
+
...props,
|
|
995
|
+
onChange: (...args) => {
|
|
996
|
+
const value = getValueFromChangeArgs(args);
|
|
997
|
+
reactiveKeys.forEach(key => runtime.setValue(key, value));
|
|
998
|
+
},
|
|
999
|
+
};
|
|
1000
|
+
};
|
|
1001
|
+
const collectReactiveFieldNames = (schema) => {
|
|
1002
|
+
const names = new Set();
|
|
1003
|
+
const visit = (node, key, parentPath) => {
|
|
1004
|
+
const schemaPath = key ? [parentPath, key].filter(Boolean).join('.') : parentPath;
|
|
1005
|
+
getReactionList(node).forEach((reaction) => {
|
|
1006
|
+
getDependencyNames(reaction)
|
|
1007
|
+
.forEach(name => names.add(name));
|
|
1008
|
+
if (getReactionTargetName(reaction)) {
|
|
1009
|
+
getPreviewTargetKeys(node, key, schemaPath).forEach(name => names.add(name));
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
1012
|
+
getSchemaChildEntriesForRuntime(node).forEach(([childKey, child]) => {
|
|
1013
|
+
visit(child, childKey, schemaPath);
|
|
1014
|
+
});
|
|
1015
|
+
};
|
|
1016
|
+
getSchemaChildEntriesForRuntime(schema).forEach(([key, child]) => {
|
|
1017
|
+
visit(child, key);
|
|
1018
|
+
});
|
|
1019
|
+
return names;
|
|
1020
|
+
};
|
|
1021
|
+
const collectPreviewInitialValues = (schema) => {
|
|
1022
|
+
const values = createPreviewRecord();
|
|
1023
|
+
const visit = (node, key, parentPath) => {
|
|
1024
|
+
const schemaPath = key ? [parentPath, key].filter(Boolean).join('.') : parentPath;
|
|
1025
|
+
if (Object.prototype.hasOwnProperty.call(node, 'default')) {
|
|
1026
|
+
getPreviewTargetKeys(node, key, schemaPath).forEach((fieldName) => {
|
|
1027
|
+
values[fieldName] = node.default;
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
getSchemaChildEntriesForRuntime(node).forEach(([childKey, child]) => {
|
|
1031
|
+
visit(child, childKey, schemaPath);
|
|
1032
|
+
});
|
|
1033
|
+
};
|
|
1034
|
+
getSchemaChildEntriesForRuntime(schema).forEach(([key, child]) => {
|
|
1035
|
+
visit(child, key);
|
|
1036
|
+
});
|
|
1037
|
+
return values;
|
|
1038
|
+
};
|
|
1039
|
+
const hasStaticRequiredValidator = (schema) => {
|
|
1040
|
+
const validator = schema['x-validator'];
|
|
1041
|
+
if (!Array.isArray(validator))
|
|
1042
|
+
return false;
|
|
1043
|
+
return validator.some(item => (isRecord(item)
|
|
1044
|
+
&& item.required === true));
|
|
1045
|
+
};
|
|
1046
|
+
const getReactionRequiredState = (states) => {
|
|
1047
|
+
let requiredState;
|
|
1048
|
+
states.forEach((state) => {
|
|
1049
|
+
if (typeof state.required === 'boolean') {
|
|
1050
|
+
requiredState = state.required;
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
return requiredState;
|
|
1054
|
+
};
|
|
1055
|
+
const isSchemaRequired = (schema, reactionStates = [], inheritedRequired = false) => {
|
|
1056
|
+
const reactionRequired = getReactionRequiredState(reactionStates);
|
|
1057
|
+
return reactionRequired ?? (schema.required === true || inheritedRequired || hasStaticRequiredValidator(schema));
|
|
1058
|
+
};
|
|
1059
|
+
const getRequiredNames = (schema) => (Array.isArray(schema.required)
|
|
1060
|
+
? schema.required.filter((item) => typeof item === 'string' && item.trim().length > 0)
|
|
1061
|
+
: []);
|
|
1062
|
+
const toValidatorPattern = (value) => {
|
|
1063
|
+
if (value instanceof RegExp)
|
|
1064
|
+
return value;
|
|
1065
|
+
if (typeof value !== 'string' || !value.trim())
|
|
1066
|
+
return undefined;
|
|
1067
|
+
try {
|
|
1068
|
+
return new RegExp(value);
|
|
1069
|
+
}
|
|
1070
|
+
catch {
|
|
1071
|
+
return undefined;
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
const getPreviewValidatorTextFields = (rule) => ({
|
|
1075
|
+
...(typeof rule.message === 'string' && rule.message.trim() ? { message: rule.message } : {}),
|
|
1076
|
+
...(typeof rule.format === 'string' && rule.format.trim() ? { type: rule.format } : {}),
|
|
1077
|
+
});
|
|
1078
|
+
const getPreviewValidatorRangeFields = (rule) => ({
|
|
1079
|
+
...(typeof rule.len === 'number' ? { len: rule.len } : {}),
|
|
1080
|
+
...(typeof rule.min === 'number' ? { min: rule.min } : {}),
|
|
1081
|
+
...(typeof rule.max === 'number' ? { max: rule.max } : {}),
|
|
1082
|
+
});
|
|
1083
|
+
const getPreviewValidatorPrimitiveFields = (rule) => ({
|
|
1084
|
+
...(rule.required === true ? { required: true } : {}),
|
|
1085
|
+
...getPreviewValidatorTextFields(rule),
|
|
1086
|
+
...getPreviewValidatorRangeFields(rule),
|
|
1087
|
+
...(rule.whitespace === true ? { whitespace: true } : {}),
|
|
1088
|
+
...(Array.isArray(rule.enum) ? { enum: rule.enum } : {}),
|
|
1089
|
+
});
|
|
1090
|
+
const getPreviewValidatorTriggerField = (rule) => (typeof rule.triggerType === 'string' && rule.triggerType.trim()
|
|
1091
|
+
? { validateTrigger: rule.triggerType }
|
|
1092
|
+
: {});
|
|
1093
|
+
const getPreviewValidatorPatternField = (rule) => {
|
|
1094
|
+
const pattern = toValidatorPattern(rule.pattern);
|
|
1095
|
+
return pattern ? { pattern } : {};
|
|
1096
|
+
};
|
|
1097
|
+
const toPreviewValidatorRule = (rule) => {
|
|
1098
|
+
const nextRule = {
|
|
1099
|
+
...getPreviewValidatorPrimitiveFields(rule),
|
|
1100
|
+
...getPreviewValidatorTriggerField(rule),
|
|
1101
|
+
...getPreviewValidatorPatternField(rule),
|
|
1102
|
+
};
|
|
1103
|
+
return Object.keys(nextRule).length ? nextRule : undefined;
|
|
1104
|
+
};
|
|
1105
|
+
const getSchemaValidatorList = (validator) => {
|
|
1106
|
+
if (Array.isArray(validator))
|
|
1107
|
+
return validator;
|
|
1108
|
+
if (typeof validator === 'string' && validator.trim())
|
|
1109
|
+
return [{ format: validator }];
|
|
1110
|
+
if (isRecord(validator))
|
|
1111
|
+
return [validator];
|
|
1112
|
+
return [];
|
|
1113
|
+
};
|
|
1114
|
+
const getPreviewValidatorRules = (schema, reactionStates = [], inheritedRequired = false) => {
|
|
1115
|
+
const validators = getSchemaValidatorList(schema['x-validator']);
|
|
1116
|
+
const rules = validators
|
|
1117
|
+
.filter(isRecord)
|
|
1118
|
+
.map(toPreviewValidatorRule)
|
|
1119
|
+
.filter((rule) => Boolean(rule));
|
|
1120
|
+
const reactionRequired = getReactionRequiredState(reactionStates);
|
|
1121
|
+
const resolvedRequired = reactionRequired ?? (schema.required === true || inheritedRequired);
|
|
1122
|
+
const nextRules = reactionRequired === false
|
|
1123
|
+
? rules.filter(rule => rule.required !== true)
|
|
1124
|
+
: [...rules];
|
|
1125
|
+
if (resolvedRequired === true && !nextRules.some(rule => rule.required === true)) {
|
|
1126
|
+
nextRules.unshift({ required: true });
|
|
1127
|
+
}
|
|
1128
|
+
return nextRules.length ? nextRules : undefined;
|
|
1129
|
+
};
|
|
1130
|
+
const shouldPassSchemaTitleToComponent = (schema) => (schema.title !== undefined
|
|
1131
|
+
&& typeof schema['x-decorator'] !== 'string'
|
|
1132
|
+
&& (schema.type === 'void'
|
|
1133
|
+
|| isRecord(schema.properties)
|
|
1134
|
+
|| schema.items !== undefined));
|
|
1135
|
+
const TAB_COMPONENT_NAMES = new Set([
|
|
1136
|
+
'Tabs',
|
|
1137
|
+
'FormTab',
|
|
1138
|
+
]);
|
|
1139
|
+
const COLLAPSE_COMPONENT_NAMES = new Set([
|
|
1140
|
+
'FormCollapse',
|
|
1141
|
+
]);
|
|
1142
|
+
const ARRAY_TABLE_COMPONENT_NAMES = new Set([
|
|
1143
|
+
'ArrayTable',
|
|
1144
|
+
]);
|
|
1145
|
+
const isFormilySchemaDocument = (schema) => (isRecord(schema)
|
|
1146
|
+
&& schema.type === 'object'
|
|
1147
|
+
&& isRecord(schema.properties));
|
|
1148
|
+
const isFormilySchemaPackage = (schema) => (isRecord(schema)
|
|
1149
|
+
&& isFormilySchemaDocument(schema.schema));
|
|
1150
|
+
const sortSchemaEntries = (properties) => (Object.entries(properties)
|
|
1151
|
+
.map((entry, declarationIndex) => ({ declarationIndex, entry }))
|
|
1152
|
+
.sort((before, after) => {
|
|
1153
|
+
const beforeIndex = before.entry[1]['x-index'];
|
|
1154
|
+
const afterIndex = after.entry[1]['x-index'];
|
|
1155
|
+
if (typeof beforeIndex === 'number' && typeof afterIndex === 'number') {
|
|
1156
|
+
return beforeIndex - afterIndex || before.declarationIndex - after.declarationIndex;
|
|
1157
|
+
}
|
|
1158
|
+
return before.declarationIndex - after.declarationIndex;
|
|
1159
|
+
})
|
|
1160
|
+
.map(item => item.entry));
|
|
1161
|
+
const getSchemaItems = (schema) => {
|
|
1162
|
+
const { items } = schema;
|
|
1163
|
+
if (Array.isArray(items)) {
|
|
1164
|
+
return items
|
|
1165
|
+
.filter(isRecord)
|
|
1166
|
+
.map((item, index) => [`items_${index}`, item]);
|
|
1167
|
+
}
|
|
1168
|
+
if (isRecord(items)) {
|
|
1169
|
+
return [['items', items]];
|
|
1170
|
+
}
|
|
1171
|
+
return [];
|
|
1172
|
+
};
|
|
1173
|
+
const schemaContainsComponent = (schema, componentName) => {
|
|
1174
|
+
if (schema['x-component'] === componentName)
|
|
1175
|
+
return true;
|
|
1176
|
+
const propertyEntries = isRecord(schema.properties)
|
|
1177
|
+
? Object.values(schema.properties).filter(isRecord)
|
|
1178
|
+
: [];
|
|
1179
|
+
const itemEntries = getSchemaItems(schema).map(([, item]) => item);
|
|
1180
|
+
return [
|
|
1181
|
+
...propertyEntries,
|
|
1182
|
+
...itemEntries,
|
|
1183
|
+
].some(child => schemaContainsComponent(child, componentName));
|
|
1184
|
+
};
|
|
1185
|
+
const PreviewError = ({ className, error, prefix, style, }) => (_jsx("div", { className: cls(prefix, className), style: style, children: _jsxs("div", { className: `${prefix}-error`, role: "alert", children: ["\u9884\u89C8\u9519\u8BEF\uFF1A", getErrorMessage(error)] }) }));
|
|
1186
|
+
const PreviewFrame = ({ children, className, prefix, style, ...props }) => (_jsx("div", { "data-testid": "designable-preview", ...props, className: cls(prefix, className), style: {
|
|
1187
|
+
...PREVIEW_ROOT_STYLE,
|
|
1188
|
+
...style,
|
|
1189
|
+
}, children: _jsx("div", { className: `${prefix}-content`, style: PREVIEW_CONTENT_STYLE, children: children }) }));
|
|
1190
|
+
class PreviewErrorBoundary extends React.Component {
|
|
1191
|
+
static getDerivedStateFromError(error) {
|
|
1192
|
+
return { error };
|
|
1193
|
+
}
|
|
1194
|
+
state = { error: null };
|
|
1195
|
+
componentDidUpdate(prevProps) {
|
|
1196
|
+
if (prevProps.resetKey !== this.props.resetKey && this.state.error) {
|
|
1197
|
+
this.setState({ error: null });
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
render() {
|
|
1201
|
+
if (this.state.error) {
|
|
1202
|
+
return (_jsx(PreviewError, { className: this.props.className, error: this.state.error, prefix: this.props.prefix, style: this.props.style }));
|
|
1203
|
+
}
|
|
1204
|
+
return this.props.children;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
class PreviewNodeErrorBoundary extends React.Component {
|
|
1208
|
+
static getDerivedStateFromError(error) {
|
|
1209
|
+
return { error };
|
|
1210
|
+
}
|
|
1211
|
+
state = { error: null };
|
|
1212
|
+
componentDidUpdate(prevProps) {
|
|
1213
|
+
if (prevProps.resetKey !== this.props.resetKey && this.state.error) {
|
|
1214
|
+
this.setState({ error: null });
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
render() {
|
|
1218
|
+
if (this.state.error) {
|
|
1219
|
+
return (_jsx(PreviewNodeErrorNotice, { componentName: this.props.componentName, error: this.state.error }));
|
|
1220
|
+
}
|
|
1221
|
+
return this.props.children;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
const getPreviewResetKey = (value) => {
|
|
1225
|
+
try {
|
|
1226
|
+
return JSON.stringify(value);
|
|
1227
|
+
}
|
|
1228
|
+
catch {
|
|
1229
|
+
return undefined;
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
const getTreePreviewResetKey = (tree) => {
|
|
1233
|
+
try {
|
|
1234
|
+
return getPreviewResetKey(tree.serialize());
|
|
1235
|
+
}
|
|
1236
|
+
catch {
|
|
1237
|
+
return `${tree.id}:${tree.children?.length ?? 0}`;
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
const renderPreviewTreeNodeContent = (component, node, props, children) => {
|
|
1241
|
+
if (component) {
|
|
1242
|
+
return React.createElement(component, props, ...children);
|
|
1243
|
+
}
|
|
1244
|
+
if (node.componentName && node.depth > 0) {
|
|
1245
|
+
return (_jsx(MissingComponentNotice, { componentName: node.componentName, children: children }));
|
|
1246
|
+
}
|
|
1247
|
+
return _jsx(_Fragment, { children: children });
|
|
1248
|
+
};
|
|
1249
|
+
const getPreviewTreeMergedProps = (node) => {
|
|
1250
|
+
const mergedProps = {
|
|
1251
|
+
...(node.designerProps?.defaultProps || {}),
|
|
1252
|
+
...(node.props ?? {}),
|
|
1253
|
+
...(node.designerProps?.getComponentProps?.(node) || {}),
|
|
1254
|
+
};
|
|
1255
|
+
if (node.depth === 0) {
|
|
1256
|
+
delete mergedProps.style;
|
|
1257
|
+
}
|
|
1258
|
+
return mergedProps;
|
|
1259
|
+
};
|
|
1260
|
+
const shouldRenderPreviewTreeNode = (node, nodeProps) => !node.hidden && !isSchemaHidden(nodeProps);
|
|
1261
|
+
const getPreviewTreeNodeChildren = (node) => {
|
|
1262
|
+
if (node.designerProps?.selfRenderChildren)
|
|
1263
|
+
return [];
|
|
1264
|
+
return node.children?.map(child => (_jsx(PreviewTreeNodeWidget, { node: child }, child.id))) || [];
|
|
1265
|
+
};
|
|
1266
|
+
const getPreviewTreeRenderProps = (node, nodeProps, basePreviewProps, dataSourceProps) => {
|
|
1267
|
+
const valueProps = {
|
|
1268
|
+
...basePreviewProps,
|
|
1269
|
+
...dataSourceProps,
|
|
1270
|
+
};
|
|
1271
|
+
return {
|
|
1272
|
+
...valueProps,
|
|
1273
|
+
...getStaticSchemaStateProps(nodeProps),
|
|
1274
|
+
...getStaticDefaultValueProps(node.componentName, nodeProps, valueProps),
|
|
1275
|
+
};
|
|
1276
|
+
};
|
|
1277
|
+
const PreviewTreeNodeWidget = observer(({ node }) => {
|
|
1278
|
+
const { components } = usePlaygroundContext();
|
|
1279
|
+
const nodeProps = node.props ?? {};
|
|
1280
|
+
const component = node.componentName ? toRenderableComponent(components[node.componentName]) : undefined;
|
|
1281
|
+
const mergedProps = getPreviewTreeMergedProps(node);
|
|
1282
|
+
const basePreviewProps = withSchemaDerivedOptions(node.componentName, nodeProps, sanitizePreviewProps(mergedProps, node.componentName));
|
|
1283
|
+
const dataSourceProps = usePreviewDataSourceProps(node.componentName, nodeProps, basePreviewProps, !node.hidden && !isSchemaHidden(nodeProps));
|
|
1284
|
+
if (!shouldRenderPreviewTreeNode(node, nodeProps))
|
|
1285
|
+
return null;
|
|
1286
|
+
const children = getPreviewTreeNodeChildren(node);
|
|
1287
|
+
const props = getPreviewTreeRenderProps(node, nodeProps, basePreviewProps, dataSourceProps);
|
|
1288
|
+
const content = renderPreviewTreeNodeContent(component, node, props, children);
|
|
1289
|
+
return (_jsx(TreeNodeContext.Provider, { value: node, children: _jsx(PreviewNodeErrorBoundary, { componentName: node.componentName, resetKey: getTreePreviewResetKey(node), children: content }) }));
|
|
1290
|
+
});
|
|
1291
|
+
PreviewTreeNodeWidget.displayName = 'PreviewTreeNodeWidget';
|
|
1292
|
+
const getPreviewSchemaComponentName = (schema) => (typeof schema['x-component'] === 'string' ? schema['x-component'] : undefined);
|
|
1293
|
+
const getPreviewSchemaDecoratorName = (schema) => (typeof schema['x-decorator'] === 'string' ? schema['x-decorator'] : undefined);
|
|
1294
|
+
const getPreviewSchemaDecoratorProps = (schema) => (isRecord(schema['x-decorator-props'])
|
|
1295
|
+
? sanitizePreviewProps(schema['x-decorator-props'])
|
|
1296
|
+
: {});
|
|
1297
|
+
const getPreviewSchemaPropertyEntries = (schema) => (isRecord(schema.properties)
|
|
1298
|
+
? sortSchemaEntries(schema.properties)
|
|
1299
|
+
: []);
|
|
1300
|
+
const getPreviewSchemaChildPath = (schemaPath, key) => ([schemaPath, key].filter(Boolean).join('.'));
|
|
1301
|
+
const renderPreviewSchemaChildNode = (context, key, child) => (_jsx(PreviewSchemaNodeWidget, { components: context.components, parentRequiredNames: context.requiredNames, schema: child, schemaKey: key, schemaPath: getPreviewSchemaChildPath(context.schemaPath, key) }, key));
|
|
1302
|
+
const renderPreviewSchemaBoundary = (context, content) => (_jsx(PreviewNodeErrorBoundary, { componentName: context.decoratorName ?? context.componentName, resetKey: context.nodeResetKey, children: content }));
|
|
1303
|
+
const renderPreviewSchemaWithDecorator = (context, content) => {
|
|
1304
|
+
const { decoratorComponent, decoratorProps, dynamicReactionStates, effectiveSchema, inheritedRequired, } = context;
|
|
1305
|
+
if (!decoratorComponent)
|
|
1306
|
+
return content;
|
|
1307
|
+
return React.createElement(decoratorComponent, {
|
|
1308
|
+
...decoratorProps,
|
|
1309
|
+
description: decoratorProps.description ?? effectiveSchema.description,
|
|
1310
|
+
required: decoratorProps.required ?? (isSchemaRequired(effectiveSchema, dynamicReactionStates, inheritedRequired) ? true : undefined),
|
|
1311
|
+
rules: decoratorProps.rules ?? getPreviewValidatorRules(effectiveSchema, dynamicReactionStates, inheritedRequired),
|
|
1312
|
+
title: decoratorProps.title ?? effectiveSchema.title,
|
|
1313
|
+
}, content);
|
|
1314
|
+
};
|
|
1315
|
+
const renderMissingPreviewSchemaComponent = (context) => {
|
|
1316
|
+
const { children, componentName } = context;
|
|
1317
|
+
const content = componentName
|
|
1318
|
+
? (_jsx(MissingComponentNotice, { componentName: componentName, children: children }))
|
|
1319
|
+
: _jsx(_Fragment, { children: children });
|
|
1320
|
+
return renderPreviewSchemaBoundary(context, renderPreviewSchemaWithDecorator(context, content));
|
|
1321
|
+
};
|
|
1322
|
+
const getPreviewSchemaChildComponentProps = (child) => (isRecord(child['x-component-props'])
|
|
1323
|
+
? sanitizePreviewProps(child['x-component-props'])
|
|
1324
|
+
: {});
|
|
1325
|
+
const getPreviewSchemaTabItems = (context) => (context.propertyEntries.map(([key, child], index) => {
|
|
1326
|
+
const childProps = getPreviewSchemaChildComponentProps(child);
|
|
1327
|
+
return {
|
|
1328
|
+
key: String(childProps.name ?? childProps.tabKey ?? key),
|
|
1329
|
+
label: childProps.title ?? childProps.tab ?? child.title ?? `标签 ${index + 1}`,
|
|
1330
|
+
children: renderPreviewSchemaChildNode(context, key, child),
|
|
1331
|
+
};
|
|
1332
|
+
}));
|
|
1333
|
+
const getPreviewSchemaCollapseItems = (context) => (context.propertyEntries.map(([key, child], index) => {
|
|
1334
|
+
const childProps = getPreviewSchemaChildComponentProps(child);
|
|
1335
|
+
return {
|
|
1336
|
+
key: String(childProps.name ?? key),
|
|
1337
|
+
label: childProps.header ?? childProps.title ?? child.title ?? `分组 ${index + 1}`,
|
|
1338
|
+
extra: childProps.extra,
|
|
1339
|
+
collapsible: childProps.collapsible,
|
|
1340
|
+
children: renderPreviewSchemaChildNode(context, key, child),
|
|
1341
|
+
};
|
|
1342
|
+
}));
|
|
1343
|
+
const getPreviewSchemaTableColumns = (context, itemSchema) => {
|
|
1344
|
+
const columnEntries = getPreviewSchemaPropertyEntries(itemSchema);
|
|
1345
|
+
const parentRequiredNames = getRequiredNames(itemSchema);
|
|
1346
|
+
return columnEntries.map(([key, child], index) => {
|
|
1347
|
+
const childProps = getPreviewSchemaChildComponentProps(child);
|
|
1348
|
+
return {
|
|
1349
|
+
key,
|
|
1350
|
+
title: childProps.title ?? child.title ?? `列 ${index + 1}`,
|
|
1351
|
+
align: childProps.align,
|
|
1352
|
+
width: childProps.width,
|
|
1353
|
+
child: (_jsx(PreviewSchemaNodeWidget, { components: context.components, parentRequiredNames: parentRequiredNames, schema: child, schemaKey: key, schemaPath: getPreviewSchemaChildPath(context.schemaPath, key) }, key)),
|
|
1354
|
+
};
|
|
1355
|
+
});
|
|
1356
|
+
};
|
|
1357
|
+
const renderPreviewSchemaWithItems = (context, extraProps) => {
|
|
1358
|
+
if (!context.schemaComponent)
|
|
1359
|
+
return undefined;
|
|
1360
|
+
return renderPreviewSchemaBoundary(context, renderPreviewSchemaWithDecorator(context, React.createElement(context.schemaComponent, { ...context.trackedProps, ...extraProps })));
|
|
1361
|
+
};
|
|
1362
|
+
const renderPreviewSchemaSpecialComponent = (context) => {
|
|
1363
|
+
const { componentName, itemEntries, propertyEntries } = context;
|
|
1364
|
+
if (componentName && TAB_COMPONENT_NAMES.has(componentName) && propertyEntries.length) {
|
|
1365
|
+
return renderPreviewSchemaWithItems(context, { items: getPreviewSchemaTabItems(context) });
|
|
1366
|
+
}
|
|
1367
|
+
if (componentName && COLLAPSE_COMPONENT_NAMES.has(componentName) && propertyEntries.length) {
|
|
1368
|
+
return renderPreviewSchemaWithItems(context, { items: getPreviewSchemaCollapseItems(context) });
|
|
1369
|
+
}
|
|
1370
|
+
if (componentName && ARRAY_TABLE_COMPONENT_NAMES.has(componentName) && itemEntries.length) {
|
|
1371
|
+
return renderPreviewSchemaWithItems(context, {
|
|
1372
|
+
columns: getPreviewSchemaTableColumns(context, itemEntries[0][1]),
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
return undefined;
|
|
1376
|
+
};
|
|
1377
|
+
const renderPreviewSchemaDefaultComponent = (context) => {
|
|
1378
|
+
if (!context.schemaComponent)
|
|
1379
|
+
return renderMissingPreviewSchemaComponent(context);
|
|
1380
|
+
const specialContent = renderPreviewSchemaSpecialComponent(context);
|
|
1381
|
+
if (specialContent)
|
|
1382
|
+
return specialContent;
|
|
1383
|
+
return renderPreviewSchemaBoundary(context, renderPreviewSchemaWithDecorator(context, React.createElement(context.schemaComponent, context.trackedProps, ...context.children)));
|
|
1384
|
+
};
|
|
1385
|
+
const PreviewSchemaNodeWidget = ({ components, parentRequiredNames = [], schema, schemaKey, schemaPath }) => {
|
|
1386
|
+
const runtime = React.useContext(PreviewRuntimeContext);
|
|
1387
|
+
const inheritedRequired = Boolean(schemaKey && parentRequiredNames.includes(schemaKey));
|
|
1388
|
+
const dynamicReactionStates = [
|
|
1389
|
+
...getDynamicReactionStates(schema, runtime.values),
|
|
1390
|
+
...getPreviewTargetKeys(schema, schemaKey, schemaPath).flatMap(key => (runtime.targetReactionStates[key] ?? [])),
|
|
1391
|
+
];
|
|
1392
|
+
const effectiveSchema = applyReactionSchemaPatches(schema, [
|
|
1393
|
+
...getStaticReactionSchemas(schema),
|
|
1394
|
+
...getDynamicReactionSchemas(schema, runtime.values),
|
|
1395
|
+
...getPreviewTargetKeys(schema, schemaKey, schemaPath).flatMap(key => (runtime.targetReactionSchemas[key] ?? [])),
|
|
1396
|
+
]);
|
|
1397
|
+
const schemaHidden = isSchemaHidden(effectiveSchema) || isReactionStateHidden(dynamicReactionStates);
|
|
1398
|
+
const componentName = getPreviewSchemaComponentName(effectiveSchema);
|
|
1399
|
+
const schemaComponent = componentName ? toRenderableComponent(components[componentName]) : undefined;
|
|
1400
|
+
const componentProps = getSchemaComponentProps(effectiveSchema, componentName);
|
|
1401
|
+
const baseProps = withSchemaDerivedOptions(componentName, effectiveSchema, componentProps);
|
|
1402
|
+
const dataSourceProps = usePreviewDataSourceProps(componentName, effectiveSchema, baseProps, !schemaHidden);
|
|
1403
|
+
const nodeResetKey = getPreviewResetKey({
|
|
1404
|
+
schema: effectiveSchema,
|
|
1405
|
+
states: dynamicReactionStates,
|
|
1406
|
+
});
|
|
1407
|
+
if (schemaHidden)
|
|
1408
|
+
return null;
|
|
1409
|
+
const props = {
|
|
1410
|
+
...baseProps,
|
|
1411
|
+
...dataSourceProps,
|
|
1412
|
+
...getStaticSchemaStateProps(effectiveSchema, dynamicReactionStates),
|
|
1413
|
+
};
|
|
1414
|
+
Object.assign(props, getStaticDefaultValueProps(componentName, effectiveSchema, props));
|
|
1415
|
+
const fieldKeys = getPreviewTargetKeys(schema, schemaKey, schemaPath);
|
|
1416
|
+
const trackedProps = withPreviewValueTracking(props, fieldKeys, runtime);
|
|
1417
|
+
if (shouldPassSchemaTitleToComponent(effectiveSchema)) {
|
|
1418
|
+
trackedProps.title = effectiveSchema.title;
|
|
1419
|
+
}
|
|
1420
|
+
const decoratorName = getPreviewSchemaDecoratorName(effectiveSchema);
|
|
1421
|
+
const decoratorComponent = decoratorName ? toRenderableComponent(components[decoratorName]) : undefined;
|
|
1422
|
+
const decoratorProps = getPreviewSchemaDecoratorProps(effectiveSchema);
|
|
1423
|
+
const propertyEntries = getPreviewSchemaPropertyEntries(effectiveSchema);
|
|
1424
|
+
const itemEntries = getSchemaItems(effectiveSchema);
|
|
1425
|
+
const childEntries = [
|
|
1426
|
+
...itemEntries,
|
|
1427
|
+
...propertyEntries,
|
|
1428
|
+
];
|
|
1429
|
+
const requiredNames = getRequiredNames(effectiveSchema);
|
|
1430
|
+
const children = childEntries.map(([key, child]) => (renderPreviewSchemaChildNode({ components, requiredNames, schemaPath }, key, child)));
|
|
1431
|
+
return renderPreviewSchemaDefaultComponent({
|
|
1432
|
+
children,
|
|
1433
|
+
componentName,
|
|
1434
|
+
components,
|
|
1435
|
+
decoratorComponent,
|
|
1436
|
+
decoratorName,
|
|
1437
|
+
decoratorProps,
|
|
1438
|
+
dynamicReactionStates,
|
|
1439
|
+
effectiveSchema,
|
|
1440
|
+
inheritedRequired,
|
|
1441
|
+
itemEntries,
|
|
1442
|
+
nodeResetKey,
|
|
1443
|
+
propertyEntries,
|
|
1444
|
+
requiredNames,
|
|
1445
|
+
schemaComponent,
|
|
1446
|
+
schemaPath,
|
|
1447
|
+
trackedProps,
|
|
1448
|
+
});
|
|
1449
|
+
};
|
|
1450
|
+
PreviewSchemaNodeWidget.displayName = 'PreviewSchemaNodeWidget';
|
|
1451
|
+
const usePreviewRuntime = (schema) => {
|
|
1452
|
+
const reactiveFieldNames = React.useMemo(() => collectReactiveFieldNames(schema), [schema]);
|
|
1453
|
+
const initialValues = React.useMemo(() => collectPreviewInitialValues(schema), [schema]);
|
|
1454
|
+
const [runtimeState, setRuntimeState] = React.useState(() => ({ schema, values: initialValues }));
|
|
1455
|
+
const values = runtimeState.schema === schema ? runtimeState.values : initialValues;
|
|
1456
|
+
const targetReactionStates = React.useMemo(() => (collectTargetReactionStates(schema, values)), [schema, values]);
|
|
1457
|
+
const targetReactionSchemas = React.useMemo(() => (collectTargetReactionSchemas(schema, values)), [schema, values]);
|
|
1458
|
+
return React.useMemo(() => ({
|
|
1459
|
+
reactiveFieldNames,
|
|
1460
|
+
setValue: (name, value) => {
|
|
1461
|
+
if (!isSafeObjectPath(name))
|
|
1462
|
+
return;
|
|
1463
|
+
setRuntimeState((current) => {
|
|
1464
|
+
const currentValues = current.schema === schema ? current.values : initialValues;
|
|
1465
|
+
if (current.schema === schema && Object.is(currentValues[name], value))
|
|
1466
|
+
return current;
|
|
1467
|
+
return {
|
|
1468
|
+
schema,
|
|
1469
|
+
values: Object.is(currentValues[name], value)
|
|
1470
|
+
? currentValues
|
|
1471
|
+
: { ...currentValues, [name]: value },
|
|
1472
|
+
};
|
|
1473
|
+
});
|
|
1474
|
+
},
|
|
1475
|
+
targetReactionSchemas,
|
|
1476
|
+
targetReactionStates,
|
|
1477
|
+
values,
|
|
1478
|
+
}), [initialValues, reactiveFieldNames, schema, targetReactionSchemas, targetReactionStates, values]);
|
|
1479
|
+
};
|
|
1480
|
+
const PreviewSchemaWidget = ({ components, schema }) => {
|
|
1481
|
+
const runtime = usePreviewRuntime(schema);
|
|
1482
|
+
const requiredNames = getRequiredNames(schema);
|
|
1483
|
+
const children = sortSchemaEntries(schema.properties).map(([key, child]) => (_jsx(PreviewSchemaNodeWidget, { components: components, parentRequiredNames: requiredNames, schema: child, schemaKey: key, schemaPath: key }, key)));
|
|
1484
|
+
const handleSubmitCapture = (event) => {
|
|
1485
|
+
event.preventDefault();
|
|
1486
|
+
};
|
|
1487
|
+
const rootComponentName = typeof schema['x-component'] === 'string'
|
|
1488
|
+
? schema['x-component']
|
|
1489
|
+
: undefined;
|
|
1490
|
+
const rootComponent = rootComponentName ? toRenderableComponent(components[rootComponentName]) : undefined;
|
|
1491
|
+
const hasFormComponent = schemaContainsComponent(schema, 'Form');
|
|
1492
|
+
const rootProps = getSchemaComponentProps(schema, rootComponentName);
|
|
1493
|
+
if (rootComponentName) {
|
|
1494
|
+
const rootContent = rootComponent
|
|
1495
|
+
? React.createElement(rootComponent, rootProps, ...children)
|
|
1496
|
+
: (_jsx(MissingComponentNotice, { componentName: rootComponentName, children: children }));
|
|
1497
|
+
const content = (_jsx(PreviewNodeErrorBoundary, { componentName: rootComponentName, resetKey: getPreviewResetKey(schema), children: rootContent }));
|
|
1498
|
+
return (_jsx(PreviewRuntimeContext.Provider, { value: runtime, children: rootComponentName === 'Form' || hasFormComponent ? (_jsx("div", { onSubmitCapture: handleSubmitCapture, children: content })) : (_jsx("form", { onSubmitCapture: handleSubmitCapture, children: content })) }));
|
|
1499
|
+
}
|
|
1500
|
+
if (hasFormComponent) {
|
|
1501
|
+
return (_jsx(PreviewRuntimeContext.Provider, { value: runtime, children: _jsx("div", { onSubmitCapture: handleSubmitCapture, children: children }) }));
|
|
1502
|
+
}
|
|
1503
|
+
return (_jsx(PreviewRuntimeContext.Provider, { value: runtime, children: _jsx("form", { onSubmitCapture: handleSubmitCapture, children: children }) }));
|
|
1504
|
+
};
|
|
1505
|
+
PreviewSchemaWidget.displayName = 'PreviewSchemaWidget';
|
|
1506
|
+
const PreviewSchemaPackageWidget = ({ components, form, schema, }) => {
|
|
1507
|
+
const formComponent = toRenderableComponent(components.Form);
|
|
1508
|
+
const runtime = usePreviewRuntime(schema);
|
|
1509
|
+
const requiredNames = getRequiredNames(schema);
|
|
1510
|
+
const children = sortSchemaEntries(schema.properties).map(([key, child]) => (_jsx(PreviewSchemaNodeWidget, { components: components, parentRequiredNames: requiredNames, schema: child, schemaKey: key, schemaPath: key }, key)));
|
|
1511
|
+
const handleSubmitCapture = (event) => {
|
|
1512
|
+
event.preventDefault();
|
|
1513
|
+
};
|
|
1514
|
+
const rootComponentName = typeof schema['x-component'] === 'string'
|
|
1515
|
+
? schema['x-component']
|
|
1516
|
+
: undefined;
|
|
1517
|
+
const rootComponent = rootComponentName && rootComponentName !== 'Form'
|
|
1518
|
+
? toRenderableComponent(components[rootComponentName])
|
|
1519
|
+
: undefined;
|
|
1520
|
+
const rootProps = getSchemaComponentProps(schema, rootComponentName);
|
|
1521
|
+
const schemaContent = rootComponentName && rootComponentName !== 'Form'
|
|
1522
|
+
? (_jsx(PreviewNodeErrorBoundary, { componentName: rootComponentName, resetKey: getPreviewResetKey(schema), children: rootComponent
|
|
1523
|
+
? React.createElement(rootComponent, rootProps, ...children)
|
|
1524
|
+
: (_jsx(MissingComponentNotice, { componentName: rootComponentName, children: children })) }))
|
|
1525
|
+
: children;
|
|
1526
|
+
if (!formComponent) {
|
|
1527
|
+
return (_jsx(PreviewSchemaWidget, { components: components, schema: schema }));
|
|
1528
|
+
}
|
|
1529
|
+
return (_jsx(PreviewRuntimeContext.Provider, { value: runtime, children: _jsx("div", { onSubmitCapture: handleSubmitCapture, children: _jsx(PreviewNodeErrorBoundary, { componentName: "Form", resetKey: getPreviewResetKey({ form, schema }), children: React.createElement(formComponent, sanitizePreviewProps(form ?? {}), schemaContent) }) }) }));
|
|
1530
|
+
};
|
|
1531
|
+
PreviewSchemaPackageWidget.displayName = 'PreviewSchemaPackageWidget';
|
|
1532
|
+
export const PreviewWidget = observer((props) => {
|
|
1533
|
+
const { components, transformer, PreviewRenderer: previewRenderer } = usePlaygroundContext();
|
|
1534
|
+
const tree = useTree();
|
|
1535
|
+
const prefix = usePrefix('preview');
|
|
1536
|
+
if (!tree) {
|
|
1537
|
+
return (_jsx(PreviewFrame, { ...props, prefix: prefix, children: _jsx("div", { className: `${prefix}-empty`, children: "\u6682\u65E0\u9884\u89C8\u5185\u5BB9" }) }));
|
|
1538
|
+
}
|
|
1539
|
+
if (previewRenderer) {
|
|
1540
|
+
let schema;
|
|
1541
|
+
try {
|
|
1542
|
+
schema = transformer ? transformer.toSchema(tree) : tree.serialize();
|
|
1543
|
+
}
|
|
1544
|
+
catch (error) {
|
|
1545
|
+
return (_jsx(PreviewError, { className: props.className, error: error, prefix: prefix, style: props.style }));
|
|
1546
|
+
}
|
|
1547
|
+
return (_jsx(PreviewErrorBoundary, { className: props.className, prefix: prefix, resetKey: getPreviewResetKey(schema), style: props.style, children: _jsx(PreviewFrame, { ...props, prefix: prefix, children: React.createElement(previewRenderer, { schema, tree }) }) }));
|
|
1548
|
+
}
|
|
1549
|
+
if (transformer) {
|
|
1550
|
+
let schema;
|
|
1551
|
+
try {
|
|
1552
|
+
schema = transformer.toSchema(tree);
|
|
1553
|
+
}
|
|
1554
|
+
catch (error) {
|
|
1555
|
+
return (_jsx(PreviewError, { className: props.className, error: error, prefix: prefix, style: props.style }));
|
|
1556
|
+
}
|
|
1557
|
+
if (isFormilySchemaDocument(schema)) {
|
|
1558
|
+
return (_jsx(PreviewErrorBoundary, { className: props.className, prefix: prefix, resetKey: getPreviewResetKey(schema), style: props.style, children: _jsx(PreviewFrame, { ...props, prefix: prefix, children: _jsx(PreviewSchemaWidget, { components: components, schema: schema }) }) }));
|
|
1559
|
+
}
|
|
1560
|
+
if (isFormilySchemaPackage(schema)) {
|
|
1561
|
+
return (_jsx(PreviewErrorBoundary, { className: props.className, prefix: prefix, resetKey: getPreviewResetKey(schema), style: props.style, children: _jsx(PreviewFrame, { ...props, prefix: prefix, children: _jsx(PreviewSchemaPackageWidget, { components: components, form: schema.form, schema: schema.schema }) }) }));
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return (_jsx(PreviewErrorBoundary, { className: props.className, prefix: prefix, resetKey: getTreePreviewResetKey(tree), style: props.style, children: _jsx(PreviewFrame, { ...props, prefix: prefix, children: _jsx(DesignerComponentsContext.Provider, { value: components, children: _jsx(PreviewTreeNodeWidget, { node: tree }) }) }) }));
|
|
1565
|
+
});
|
|
1566
|
+
PreviewWidget.displayName = 'PreviewWidget';
|