@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,736 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { getPlaygroundProductionReadinessPhaseSummary, } from "../shared/componentRegistry.js";
|
|
3
|
+
import { getPlaygroundProductionAcceptancePhaseSummary, } from "../shared/productionAcceptance.js";
|
|
4
|
+
import { stringifySchemaForExport, } from "../shared/schema.js";
|
|
5
|
+
export const PLAYGROUND_REMOTE_LOAD_RESULT = Symbol('playground-remote-load-result');
|
|
6
|
+
export function createPlaygroundRemotePersistenceLoadResult(schema, commitLoadedRemoteSnapshot, discardLoadedRemoteSnapshot) {
|
|
7
|
+
return {
|
|
8
|
+
commitLoadedRemoteSnapshot,
|
|
9
|
+
discardLoadedRemoteSnapshot,
|
|
10
|
+
schema,
|
|
11
|
+
[PLAYGROUND_REMOTE_LOAD_RESULT]: true,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export const isPlaygroundRemotePersistenceLoadResult = (value) => {
|
|
15
|
+
if (typeof value !== 'object' || value === null)
|
|
16
|
+
return false;
|
|
17
|
+
return value[PLAYGROUND_REMOTE_LOAD_RESULT] === true;
|
|
18
|
+
};
|
|
19
|
+
const DEFAULT_BEFORE_UNLOAD_MESSAGE = '设计内容尚未保存,离开后可能丢失未保存变更。';
|
|
20
|
+
const toError = (message, reason = 'unknown') => (Object.assign(new Error(message), { reason }));
|
|
21
|
+
const attachErrorReason = (error, reason) => (isRecord(error)
|
|
22
|
+
? Object.assign(error, { reason })
|
|
23
|
+
: Object.assign(new Error(String(error)), { reason }));
|
|
24
|
+
const createValidationError = (validation, messagePrefix = 'Schema 校验失败', issues = validation.issues) => Object.assign(new Error(`${messagePrefix}:${issues.map(issue => issue.message).join(';')}`), {
|
|
25
|
+
issues,
|
|
26
|
+
reason: 'validation',
|
|
27
|
+
validation,
|
|
28
|
+
});
|
|
29
|
+
const createProductionReadinessError = (phaseSummary) => Object.assign(new Error([
|
|
30
|
+
`Playground 生产准入未通过:${phaseSummary.decision.summary}`,
|
|
31
|
+
...phaseSummary.decision.blockingMessages.slice(0, 5).map(message => `- ${message}`),
|
|
32
|
+
].join('\n')), {
|
|
33
|
+
decision: phaseSummary.decision,
|
|
34
|
+
phaseSummary,
|
|
35
|
+
reason: 'production-readiness',
|
|
36
|
+
});
|
|
37
|
+
const createProductionAcceptanceError = (acceptanceSnapshot, acceptancePhaseSummary) => Object.assign(new Error([
|
|
38
|
+
`Playground 生产试点验收未通过:${acceptanceSnapshot.summary}`,
|
|
39
|
+
...acceptanceSnapshot.cases
|
|
40
|
+
.filter(item => item.status !== 'ready')
|
|
41
|
+
.slice(0, 5)
|
|
42
|
+
.map(item => `- ${item.name}: ${item.reasons.join(';') || '未通过生产试点验收'}`),
|
|
43
|
+
].join('\n')), {
|
|
44
|
+
acceptancePhaseSummary,
|
|
45
|
+
acceptanceSnapshot,
|
|
46
|
+
reason: 'production-acceptance',
|
|
47
|
+
});
|
|
48
|
+
const actionPermissionLabels = {
|
|
49
|
+
load: '远程加载',
|
|
50
|
+
publish: '远程发布',
|
|
51
|
+
save: '远程保存',
|
|
52
|
+
};
|
|
53
|
+
const createPermissionDeniedError = (action) => Object.assign(new Error(`当前没有${actionPermissionLabels[action]}权限。`), {
|
|
54
|
+
action,
|
|
55
|
+
reason: 'permission-denied',
|
|
56
|
+
});
|
|
57
|
+
const assertValidDesignForRemoteSave = (api, shouldValidate, blockWarnings) => {
|
|
58
|
+
if (!shouldValidate)
|
|
59
|
+
return undefined;
|
|
60
|
+
const validation = api.validateDesign();
|
|
61
|
+
if (!validation.valid) {
|
|
62
|
+
throw createValidationError(validation);
|
|
63
|
+
}
|
|
64
|
+
const warningIssues = validation.issues.filter(issue => issue.severity === 'warning');
|
|
65
|
+
if (blockWarnings && warningIssues.length > 0) {
|
|
66
|
+
throw createValidationError(validation, 'Schema 发布校验未通过', warningIssues);
|
|
67
|
+
}
|
|
68
|
+
return validation;
|
|
69
|
+
};
|
|
70
|
+
const exportSchemaTextForRemoteSave = (api) => {
|
|
71
|
+
const schemaText = api.tryExportDesign();
|
|
72
|
+
if (!schemaText) {
|
|
73
|
+
const lastError = api.getLastError?.()?.error;
|
|
74
|
+
throw lastError
|
|
75
|
+
? attachErrorReason(lastError, 'export')
|
|
76
|
+
: toError('当前 Schema 导出失败,无法远程保存', 'export');
|
|
77
|
+
}
|
|
78
|
+
return schemaText;
|
|
79
|
+
};
|
|
80
|
+
const createProductionReadinessMetadata = ({ productionReadinessDecision, productionReadinessPhaseSummary, }) => ({
|
|
81
|
+
productionCanGrayRelease: productionReadinessDecision?.canGrayRelease,
|
|
82
|
+
productionCanPublish: productionReadinessDecision?.canPublish,
|
|
83
|
+
productionReadinessActionCount: productionReadinessPhaseSummary?.requiredActions.length,
|
|
84
|
+
productionReadinessPhase: productionReadinessPhaseSummary?.phase,
|
|
85
|
+
productionReadinessPhaseTitle: productionReadinessPhaseSummary?.title,
|
|
86
|
+
productionReadinessStatus: productionReadinessDecision?.status,
|
|
87
|
+
productionReadinessSummary: productionReadinessDecision?.summary,
|
|
88
|
+
});
|
|
89
|
+
const createProductionAcceptanceMetadata = ({ productionAcceptancePhaseSummary, productionAcceptanceSnapshot, }) => ({
|
|
90
|
+
productionAcceptanceActionCount: productionAcceptancePhaseSummary?.requiredActions.length,
|
|
91
|
+
productionAcceptanceBlockedCaseCount: productionAcceptanceSnapshot?.blockedCaseCount,
|
|
92
|
+
productionAcceptanceCaseCount: productionAcceptanceSnapshot?.totalCaseCount,
|
|
93
|
+
productionAcceptancePhase: productionAcceptancePhaseSummary?.phase,
|
|
94
|
+
productionAcceptancePhaseTitle: productionAcceptancePhaseSummary?.title,
|
|
95
|
+
productionAcceptanceStatus: productionAcceptanceSnapshot?.status,
|
|
96
|
+
productionAcceptanceSummary: productionAcceptanceSnapshot?.summary,
|
|
97
|
+
productionCanPilot: productionAcceptanceSnapshot?.canPilot,
|
|
98
|
+
productionCanProductionRelease: productionAcceptanceSnapshot?.canProductionRelease,
|
|
99
|
+
});
|
|
100
|
+
const createSaveMetadata = ({ api, dirty, lastChangeType, mode = 'draft', productionAcceptancePhaseSummary, productionAcceptanceSnapshot, productionReadinessDecision, productionReadinessPhaseSummary, schema, schemaText, source = 'manual', validation, }) => {
|
|
101
|
+
const validationIssues = validation?.issues ?? [];
|
|
102
|
+
const validationErrorCount = validationIssues.filter(issue => issue.severity === 'error').length;
|
|
103
|
+
const validationWarningCount = validationIssues.filter(issue => issue.severity === 'warning').length;
|
|
104
|
+
return {
|
|
105
|
+
dirty,
|
|
106
|
+
lastChangeType,
|
|
107
|
+
mode,
|
|
108
|
+
schemaContextVersion: 1,
|
|
109
|
+
schemaFingerprint: createRemoteSaveSchemaFingerprint(schema),
|
|
110
|
+
schemaTextLength: schemaText.length,
|
|
111
|
+
source,
|
|
112
|
+
validationErrorCount,
|
|
113
|
+
validationValid: validation?.valid,
|
|
114
|
+
validationWarningCount,
|
|
115
|
+
...createProductionReadinessMetadata({
|
|
116
|
+
productionReadinessDecision,
|
|
117
|
+
productionReadinessPhaseSummary,
|
|
118
|
+
}),
|
|
119
|
+
...createProductionAcceptanceMetadata({
|
|
120
|
+
productionAcceptancePhaseSummary,
|
|
121
|
+
productionAcceptanceSnapshot,
|
|
122
|
+
}),
|
|
123
|
+
viewType: api.getViewType(),
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
const createRemoteSaveSchemaFingerprint = (schema) => {
|
|
127
|
+
try {
|
|
128
|
+
return stringifySchemaForExport(schema, 0);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const getProductionReadinessPhaseSummaryForRemoteSave = ({ api, blockProductionReadinessWarningsBeforePublish, getProductionReadinessReport, mode = 'draft', schema, schemaText, validation, }) => {
|
|
135
|
+
const report = getProductionReadinessReport?.({
|
|
136
|
+
api,
|
|
137
|
+
mode,
|
|
138
|
+
schema,
|
|
139
|
+
schemaText,
|
|
140
|
+
validation,
|
|
141
|
+
});
|
|
142
|
+
if (!report)
|
|
143
|
+
return undefined;
|
|
144
|
+
const phaseSummary = getPlaygroundProductionReadinessPhaseSummary(report, {
|
|
145
|
+
blockWarnings: blockProductionReadinessWarningsBeforePublish,
|
|
146
|
+
});
|
|
147
|
+
const { decision } = phaseSummary;
|
|
148
|
+
if (mode === 'publish' && !decision.canPublish) {
|
|
149
|
+
throw createProductionReadinessError(phaseSummary);
|
|
150
|
+
}
|
|
151
|
+
return phaseSummary;
|
|
152
|
+
};
|
|
153
|
+
const getProductionAcceptanceForRemoteSave = ({ api, blockProductionAcceptanceBeforePublish, getProductionAcceptanceSnapshot, mode = 'draft', productionReadinessDecision, productionReadinessPhaseSummary, schema, schemaText, validation, }) => {
|
|
154
|
+
const acceptanceSnapshot = getProductionAcceptanceSnapshot?.({
|
|
155
|
+
api,
|
|
156
|
+
mode,
|
|
157
|
+
productionReadinessDecision,
|
|
158
|
+
productionReadinessPhaseSummary,
|
|
159
|
+
schema,
|
|
160
|
+
schemaText,
|
|
161
|
+
validation,
|
|
162
|
+
});
|
|
163
|
+
if (!acceptanceSnapshot)
|
|
164
|
+
return {};
|
|
165
|
+
const acceptancePhaseSummary = getPlaygroundProductionAcceptancePhaseSummary(acceptanceSnapshot);
|
|
166
|
+
if (mode === 'publish'
|
|
167
|
+
&& blockProductionAcceptanceBeforePublish
|
|
168
|
+
&& !acceptanceSnapshot.canProductionRelease) {
|
|
169
|
+
throw createProductionAcceptanceError(acceptanceSnapshot, acceptancePhaseSummary);
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
productionAcceptancePhaseSummary: acceptancePhaseSummary,
|
|
173
|
+
productionAcceptanceSnapshot: acceptanceSnapshot,
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const prepareRemoteSaveContext = ({ api, blockProductionAcceptanceBeforePublish, blockWarningsBeforePublish, blockProductionReadinessWarningsBeforePublish, getProductionAcceptanceSnapshot, getProductionReadinessReport, options, validateBeforePublish, validateBeforeSave, }) => {
|
|
177
|
+
const validation = assertValidDesignForRemoteSave(api, shouldValidateRemoteSave({
|
|
178
|
+
mode: options?.mode,
|
|
179
|
+
validateBeforePublish,
|
|
180
|
+
validateBeforeSave,
|
|
181
|
+
}), shouldBlockWarningsBeforeRemoteSave({
|
|
182
|
+
blockWarningsBeforePublish,
|
|
183
|
+
mode: options?.mode,
|
|
184
|
+
validateBeforePublish,
|
|
185
|
+
}));
|
|
186
|
+
const schemaText = exportSchemaTextForRemoteSave(api);
|
|
187
|
+
const schema = api.getSchema();
|
|
188
|
+
const dirty = api.isDirty();
|
|
189
|
+
const lastChangeType = api.getLastChangeType();
|
|
190
|
+
const productionReadinessPhaseSummary = getProductionReadinessPhaseSummaryForRemoteSave({
|
|
191
|
+
api,
|
|
192
|
+
blockProductionReadinessWarningsBeforePublish,
|
|
193
|
+
getProductionReadinessReport,
|
|
194
|
+
mode: options?.mode,
|
|
195
|
+
schema,
|
|
196
|
+
schemaText,
|
|
197
|
+
validation,
|
|
198
|
+
});
|
|
199
|
+
const productionReadinessDecision = productionReadinessPhaseSummary?.decision;
|
|
200
|
+
const { productionAcceptancePhaseSummary, productionAcceptanceSnapshot, } = getProductionAcceptanceForRemoteSave({
|
|
201
|
+
api,
|
|
202
|
+
blockProductionAcceptanceBeforePublish,
|
|
203
|
+
getProductionAcceptanceSnapshot,
|
|
204
|
+
mode: options?.mode,
|
|
205
|
+
productionReadinessDecision,
|
|
206
|
+
productionReadinessPhaseSummary,
|
|
207
|
+
schema,
|
|
208
|
+
schemaText,
|
|
209
|
+
validation,
|
|
210
|
+
});
|
|
211
|
+
const metadata = createSaveMetadata({
|
|
212
|
+
api,
|
|
213
|
+
dirty,
|
|
214
|
+
lastChangeType,
|
|
215
|
+
mode: options?.mode,
|
|
216
|
+
productionAcceptancePhaseSummary,
|
|
217
|
+
productionAcceptanceSnapshot,
|
|
218
|
+
productionReadinessDecision,
|
|
219
|
+
productionReadinessPhaseSummary,
|
|
220
|
+
schema,
|
|
221
|
+
schemaText,
|
|
222
|
+
source: options?.source,
|
|
223
|
+
validation,
|
|
224
|
+
});
|
|
225
|
+
return {
|
|
226
|
+
dirty,
|
|
227
|
+
lastChangeType,
|
|
228
|
+
metadata,
|
|
229
|
+
productionAcceptancePhaseSummary,
|
|
230
|
+
productionAcceptanceSnapshot,
|
|
231
|
+
productionReadinessDecision,
|
|
232
|
+
productionReadinessPhaseSummary,
|
|
233
|
+
schema,
|
|
234
|
+
schemaText,
|
|
235
|
+
validation,
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
const shouldValidateRemoteSave = ({ mode, validateBeforePublish, validateBeforeSave, }) => (validateBeforeSave || (mode === 'publish' && validateBeforePublish));
|
|
239
|
+
const shouldBlockWarningsBeforeRemoteSave = ({ blockWarningsBeforePublish, mode, validateBeforePublish, }) => (mode === 'publish'
|
|
240
|
+
&& validateBeforePublish
|
|
241
|
+
&& blockWarningsBeforePublish);
|
|
242
|
+
const isRecord = (value) => (typeof value === 'object' && value !== null);
|
|
243
|
+
const defaultIsConflictError = (error) => {
|
|
244
|
+
if (!isRecord(error))
|
|
245
|
+
return false;
|
|
246
|
+
return error.status === 409
|
|
247
|
+
|| error.statusCode === 409
|
|
248
|
+
|| error.code === 'CONFLICT'
|
|
249
|
+
|| error.code === 'REMOTE_SCHEMA_CONFLICT'
|
|
250
|
+
|| error.code === 'SCHEMA_REVISION_CONFLICT'
|
|
251
|
+
|| error.code === 'SCHEMA_VERSION_CONFLICT'
|
|
252
|
+
|| error.reason === 'schema-version-conflict';
|
|
253
|
+
};
|
|
254
|
+
const knownErrorReasons = new Set([
|
|
255
|
+
'baseline-sync',
|
|
256
|
+
'conflict',
|
|
257
|
+
'export',
|
|
258
|
+
'load-protected-dirty',
|
|
259
|
+
'permission-denied',
|
|
260
|
+
'production-acceptance',
|
|
261
|
+
'production-readiness',
|
|
262
|
+
'remote',
|
|
263
|
+
'save-stale',
|
|
264
|
+
'unknown',
|
|
265
|
+
'validation',
|
|
266
|
+
]);
|
|
267
|
+
const getExplicitErrorReason = (error) => {
|
|
268
|
+
if (!isRecord(error) || typeof error.reason !== 'string')
|
|
269
|
+
return undefined;
|
|
270
|
+
return knownErrorReasons.has(error.reason)
|
|
271
|
+
? error.reason
|
|
272
|
+
: undefined;
|
|
273
|
+
};
|
|
274
|
+
const getRemotePersistenceErrorReason = ({ error, isConflictError, type, }) => {
|
|
275
|
+
if (type === 'save' && isConflictError(error))
|
|
276
|
+
return 'conflict';
|
|
277
|
+
return getExplicitErrorReason(error) ?? (type === 'save' ? 'remote' : 'unknown');
|
|
278
|
+
};
|
|
279
|
+
const withDefault = (value, defaultValue) => (value === undefined ? defaultValue : value);
|
|
280
|
+
const normalizeRemoteActionPermissions = (permissions) => ({
|
|
281
|
+
canLoad: withDefault(permissions?.canLoad, true),
|
|
282
|
+
canPublish: withDefault(permissions?.canPublish, true),
|
|
283
|
+
canSave: withDefault(permissions?.canSave, true),
|
|
284
|
+
});
|
|
285
|
+
const canLoadRemoteSchema = (permissions) => permissions.canLoad;
|
|
286
|
+
const canSaveRemoteSchema = (permissions, mode = 'draft') => (mode === 'publish' ? permissions.canPublish : permissions.canSave);
|
|
287
|
+
const normalizeRemotePersistenceOptions = (options) => {
|
|
288
|
+
const blockWarningsBeforePublish = withDefault(options.blockWarningsBeforePublish, false);
|
|
289
|
+
return {
|
|
290
|
+
actionPermissions: normalizeRemoteActionPermissions(options.actionPermissions),
|
|
291
|
+
api: options.api,
|
|
292
|
+
autoLoad: withDefault(options.autoLoad, true),
|
|
293
|
+
autoSaveDelayMs: withDefault(options.autoSaveDelayMs, false),
|
|
294
|
+
beforeUnloadMessage: withDefault(options.beforeUnloadMessage, DEFAULT_BEFORE_UNLOAD_MESSAGE),
|
|
295
|
+
blockProductionReadinessWarningsBeforePublish: (withDefault(options.blockProductionReadinessWarningsBeforePublish, blockWarningsBeforePublish)),
|
|
296
|
+
blockProductionAcceptanceBeforePublish: withDefault(options.blockProductionAcceptanceBeforePublish, true),
|
|
297
|
+
blockWarningsBeforePublish,
|
|
298
|
+
confirmBeforeUnload: withDefault(options.confirmBeforeUnload, true),
|
|
299
|
+
commitLoadedRemoteSnapshot: options.commitLoadedRemoteSnapshot,
|
|
300
|
+
contextKey: options.contextKey,
|
|
301
|
+
getProductionAcceptanceSnapshot: options.getProductionAcceptanceSnapshot,
|
|
302
|
+
getProductionReadinessReport: options.getProductionReadinessReport,
|
|
303
|
+
isConflictError: withDefault(options.isConflictError, defaultIsConflictError),
|
|
304
|
+
loadSchema: options.loadSchema,
|
|
305
|
+
onError: options.onError,
|
|
306
|
+
protectDirtyOnLoad: withDefault(options.protectDirtyOnLoad, true),
|
|
307
|
+
saveSchema: options.saveSchema,
|
|
308
|
+
validateBeforePublish: withDefault(options.validateBeforePublish, true),
|
|
309
|
+
validateBeforeSave: withDefault(options.validateBeforeSave, true),
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
export const usePlaygroundRemotePersistence = (options) => {
|
|
313
|
+
const { actionPermissions, api, autoLoad, autoSaveDelayMs, beforeUnloadMessage, blockProductionAcceptanceBeforePublish, blockProductionReadinessWarningsBeforePublish, blockWarningsBeforePublish, confirmBeforeUnload, commitLoadedRemoteSnapshot, contextKey, getProductionAcceptanceSnapshot, getProductionReadinessReport, isConflictError, loadSchema, onError, protectDirtyOnLoad, saveSchema, validateBeforePublish, validateBeforeSave, } = normalizeRemotePersistenceOptions(options);
|
|
314
|
+
const apiRef = useRef(api ?? undefined);
|
|
315
|
+
const saveRemoteSchemaRef = useRef(undefined);
|
|
316
|
+
const lastAutoSaveRevisionRef = useRef(0);
|
|
317
|
+
const activeLoadedEnvelopeRef = useRef(undefined);
|
|
318
|
+
const activeAppliedLoadSettlementRef = useRef(undefined);
|
|
319
|
+
const contextKeyRef = useRef(contextKey);
|
|
320
|
+
const mountedRef = useRef(true);
|
|
321
|
+
const loadedApiRef = useRef(undefined);
|
|
322
|
+
const operationIdRef = useRef(0);
|
|
323
|
+
const pendingOperationsRef = useRef(new Map());
|
|
324
|
+
const [status, setStatus] = useState('idle');
|
|
325
|
+
const [error, setError] = useState();
|
|
326
|
+
const [lastErrorAction, setLastErrorAction] = useState();
|
|
327
|
+
const [lastErrorReason, setLastErrorReason] = useState();
|
|
328
|
+
const [lastLoadedAt, setLastLoadedAt] = useState();
|
|
329
|
+
const [lastSavedAt, setLastSavedAt] = useState();
|
|
330
|
+
const [lastSaveMetadata, setLastSaveMetadata] = useState();
|
|
331
|
+
const [lastSaveValidation, setLastSaveValidation] = useState();
|
|
332
|
+
const [lastSaveAttemptMode, setLastSaveAttemptMode] = useState();
|
|
333
|
+
const [dirty, setDirty] = useState(false);
|
|
334
|
+
const [schemaChangeRevision, setSchemaChangeRevision] = useState(0);
|
|
335
|
+
const [lastChangeType, setLastChangeType] = useState();
|
|
336
|
+
const discardActiveLoadedEnvelope = useCallback(() => {
|
|
337
|
+
activeLoadedEnvelopeRef.current?.discardLoadedRemoteSnapshot?.();
|
|
338
|
+
activeLoadedEnvelopeRef.current = undefined;
|
|
339
|
+
}, []);
|
|
340
|
+
useEffect(() => {
|
|
341
|
+
mountedRef.current = true;
|
|
342
|
+
return () => {
|
|
343
|
+
discardActiveLoadedEnvelope();
|
|
344
|
+
mountedRef.current = false;
|
|
345
|
+
};
|
|
346
|
+
}, [discardActiveLoadedEnvelope]);
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
const nextApi = api ?? undefined;
|
|
349
|
+
if (apiRef.current === nextApi)
|
|
350
|
+
return;
|
|
351
|
+
discardActiveLoadedEnvelope();
|
|
352
|
+
activeAppliedLoadSettlementRef.current = undefined;
|
|
353
|
+
apiRef.current = nextApi;
|
|
354
|
+
operationIdRef.current += 1;
|
|
355
|
+
pendingOperationsRef.current.clear();
|
|
356
|
+
loadedApiRef.current = undefined;
|
|
357
|
+
lastAutoSaveRevisionRef.current = -1;
|
|
358
|
+
setStatus('idle');
|
|
359
|
+
setError(undefined);
|
|
360
|
+
setLastErrorAction(undefined);
|
|
361
|
+
setLastErrorReason(undefined);
|
|
362
|
+
setDirty(nextApi?.isDirty() ?? false);
|
|
363
|
+
setLastChangeType(nextApi?.getLastChangeType());
|
|
364
|
+
}, [api, discardActiveLoadedEnvelope]);
|
|
365
|
+
useEffect(() => {
|
|
366
|
+
if (Object.is(contextKeyRef.current, contextKey))
|
|
367
|
+
return;
|
|
368
|
+
discardActiveLoadedEnvelope();
|
|
369
|
+
activeAppliedLoadSettlementRef.current = undefined;
|
|
370
|
+
contextKeyRef.current = contextKey;
|
|
371
|
+
operationIdRef.current += 1;
|
|
372
|
+
pendingOperationsRef.current.clear();
|
|
373
|
+
loadedApiRef.current = undefined;
|
|
374
|
+
lastAutoSaveRevisionRef.current = -1;
|
|
375
|
+
setStatus('idle');
|
|
376
|
+
setError(undefined);
|
|
377
|
+
setLastErrorAction(undefined);
|
|
378
|
+
setLastErrorReason(undefined);
|
|
379
|
+
setDirty(apiRef.current?.isDirty() ?? false);
|
|
380
|
+
setLastChangeType(apiRef.current?.getLastChangeType());
|
|
381
|
+
}, [contextKey, discardActiveLoadedEnvelope]);
|
|
382
|
+
const setFailed = useCallback((nextError, type) => {
|
|
383
|
+
if (!mountedRef.current)
|
|
384
|
+
return;
|
|
385
|
+
const reason = getRemotePersistenceErrorReason({
|
|
386
|
+
error: nextError,
|
|
387
|
+
isConflictError,
|
|
388
|
+
type,
|
|
389
|
+
});
|
|
390
|
+
setError(nextError);
|
|
391
|
+
setLastErrorAction(type);
|
|
392
|
+
setLastErrorReason(reason);
|
|
393
|
+
setStatus(type === 'save' && isConflictError(nextError) ? 'conflict' : 'error');
|
|
394
|
+
onError?.(nextError, { type, api: apiRef.current, reason });
|
|
395
|
+
}, [isConflictError, onError]);
|
|
396
|
+
const isActiveOperation = useCallback((operationId, nextApi) => (mountedRef.current && operationIdRef.current === operationId && apiRef.current === nextApi), []);
|
|
397
|
+
const beginRemoteOperation = useCallback((nextApi) => {
|
|
398
|
+
const operationId = operationIdRef.current + 1;
|
|
399
|
+
operationIdRef.current = operationId;
|
|
400
|
+
pendingOperationsRef.current.set(operationId, nextApi);
|
|
401
|
+
return operationId;
|
|
402
|
+
}, []);
|
|
403
|
+
const finishRemoteOperation = useCallback((operationId, nextApi, restorePendingOperation) => {
|
|
404
|
+
pendingOperationsRef.current.delete(operationId);
|
|
405
|
+
if (!restorePendingOperation || operationIdRef.current !== operationId)
|
|
406
|
+
return;
|
|
407
|
+
const previousOperationId = [...pendingOperationsRef.current.entries()]
|
|
408
|
+
.filter(([, pendingApi]) => pendingApi === nextApi)
|
|
409
|
+
.reduce((latestId, [pendingId]) => Math.max(latestId, pendingId), 0);
|
|
410
|
+
if (previousOperationId > 0)
|
|
411
|
+
operationIdRef.current = previousOperationId;
|
|
412
|
+
}, []);
|
|
413
|
+
const syncDirtySnapshot = useCallback((nextApi) => {
|
|
414
|
+
setDirty(nextApi.isDirty());
|
|
415
|
+
setLastChangeType(nextApi.getLastChangeType());
|
|
416
|
+
}, []);
|
|
417
|
+
const handleSchemaChange = useCallback((_schema, context) => {
|
|
418
|
+
setDirty(context.dirty);
|
|
419
|
+
if (context.dirty) {
|
|
420
|
+
setSchemaChangeRevision(revision => revision + 1);
|
|
421
|
+
setLastSaveValidation(undefined);
|
|
422
|
+
}
|
|
423
|
+
setLastChangeType(context.lastChangeType ?? context.type);
|
|
424
|
+
}, []);
|
|
425
|
+
const applyRemoteSchema = useCallback((nextApi, schema, options, onSchemaApplied) => {
|
|
426
|
+
if (!options?.force && protectDirtyOnLoad && nextApi.isDirty()) {
|
|
427
|
+
throw toError('远程 Schema 已返回,但当前画布存在未保存变更,已拒绝覆盖。', 'load-protected-dirty');
|
|
428
|
+
}
|
|
429
|
+
if (schema !== null && schema !== undefined) {
|
|
430
|
+
if (!nextApi.importDesign(schema)) {
|
|
431
|
+
throw toError('远程 Schema 加载失败');
|
|
432
|
+
}
|
|
433
|
+
onSchemaApplied?.();
|
|
434
|
+
}
|
|
435
|
+
if (!nextApi.markSaved()) {
|
|
436
|
+
throw toError('远程 Schema 已加载,但保存基线同步失败', 'baseline-sync');
|
|
437
|
+
}
|
|
438
|
+
}, [protectDirtyOnLoad]);
|
|
439
|
+
const markSavedIfSchemaUnchanged = useCallback((nextApi, schemaText) => {
|
|
440
|
+
if (nextApi.tryExportDesign() !== schemaText) {
|
|
441
|
+
throw toError('远程保存已完成,但保存期间画布已产生新变更,已保留未保存状态。', 'save-stale');
|
|
442
|
+
}
|
|
443
|
+
if (!nextApi.markSaved()) {
|
|
444
|
+
throw toError('远程保存成功,但保存基线同步失败', 'baseline-sync');
|
|
445
|
+
}
|
|
446
|
+
}, []);
|
|
447
|
+
const loadRemoteSchema = useCallback(async (options) => {
|
|
448
|
+
const currentApi = apiRef.current;
|
|
449
|
+
if (!currentApi || !loadSchema)
|
|
450
|
+
return false;
|
|
451
|
+
if (!canLoadRemoteSchema(actionPermissions)) {
|
|
452
|
+
setFailed(createPermissionDeniedError('load'), 'load');
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
discardActiveLoadedEnvelope();
|
|
456
|
+
const operationId = beginRemoteOperation(currentApi);
|
|
457
|
+
let schemaApplied = false;
|
|
458
|
+
let loadSucceeded = false;
|
|
459
|
+
let loadedEnvelope;
|
|
460
|
+
let resolveAppliedLoadSettlement;
|
|
461
|
+
const appliedLoadSettlement = new Promise((resolve) => {
|
|
462
|
+
resolveAppliedLoadSettlement = resolve;
|
|
463
|
+
});
|
|
464
|
+
setError(undefined);
|
|
465
|
+
setLastErrorAction(undefined);
|
|
466
|
+
setLastErrorReason(undefined);
|
|
467
|
+
setStatus('loading');
|
|
468
|
+
try {
|
|
469
|
+
const loadedResult = await loadSchema();
|
|
470
|
+
loadedEnvelope = isPlaygroundRemotePersistenceLoadResult(loadedResult)
|
|
471
|
+
? loadedResult
|
|
472
|
+
: undefined;
|
|
473
|
+
activeLoadedEnvelopeRef.current = loadedEnvelope;
|
|
474
|
+
const schema = isPlaygroundRemotePersistenceLoadResult(loadedResult)
|
|
475
|
+
? loadedResult.schema
|
|
476
|
+
: loadedResult;
|
|
477
|
+
if (!isActiveOperation(operationId, currentApi)) {
|
|
478
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
applyRemoteSchema(currentApi, schema, options, () => {
|
|
482
|
+
schemaApplied = true;
|
|
483
|
+
activeAppliedLoadSettlementRef.current = {
|
|
484
|
+
api: currentApi,
|
|
485
|
+
contextKey: contextKeyRef.current,
|
|
486
|
+
settlement: appliedLoadSettlement,
|
|
487
|
+
};
|
|
488
|
+
});
|
|
489
|
+
await commitLoadedRemoteSnapshot?.(schema);
|
|
490
|
+
await loadedEnvelope?.commitLoadedRemoteSnapshot?.();
|
|
491
|
+
if (!isActiveOperation(operationId, currentApi)) {
|
|
492
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
496
|
+
if (mountedRef.current) {
|
|
497
|
+
setError(undefined);
|
|
498
|
+
setLastErrorAction(undefined);
|
|
499
|
+
setLastErrorReason(undefined);
|
|
500
|
+
setStatus('loaded');
|
|
501
|
+
setLastLoadedAt(new Date());
|
|
502
|
+
loadedApiRef.current = currentApi;
|
|
503
|
+
syncDirtySnapshot(currentApi);
|
|
504
|
+
}
|
|
505
|
+
loadSucceeded = true;
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
catch (nextError) {
|
|
509
|
+
if (!isActiveOperation(operationId, currentApi)) {
|
|
510
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
finishRemoteOperation(operationId, currentApi, !schemaApplied);
|
|
514
|
+
setFailed(nextError, 'load');
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
finally {
|
|
518
|
+
resolveAppliedLoadSettlement(loadSucceeded);
|
|
519
|
+
if (activeAppliedLoadSettlementRef.current?.settlement === appliedLoadSettlement) {
|
|
520
|
+
activeAppliedLoadSettlementRef.current = undefined;
|
|
521
|
+
}
|
|
522
|
+
loadedEnvelope?.discardLoadedRemoteSnapshot?.();
|
|
523
|
+
if (activeLoadedEnvelopeRef.current === loadedEnvelope) {
|
|
524
|
+
activeLoadedEnvelopeRef.current = undefined;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}, [
|
|
528
|
+
actionPermissions,
|
|
529
|
+
applyRemoteSchema,
|
|
530
|
+
beginRemoteOperation,
|
|
531
|
+
commitLoadedRemoteSnapshot,
|
|
532
|
+
discardActiveLoadedEnvelope,
|
|
533
|
+
finishRemoteOperation,
|
|
534
|
+
isActiveOperation,
|
|
535
|
+
loadSchema,
|
|
536
|
+
setFailed,
|
|
537
|
+
syncDirtySnapshot,
|
|
538
|
+
]);
|
|
539
|
+
const saveRemoteSchema = useCallback(async (options) => {
|
|
540
|
+
const currentApi = apiRef.current;
|
|
541
|
+
if (!currentApi || !saveSchema)
|
|
542
|
+
return false;
|
|
543
|
+
const saveMode = options?.mode ?? 'draft';
|
|
544
|
+
if (!canSaveRemoteSchema(actionPermissions, saveMode)) {
|
|
545
|
+
setLastSaveAttemptMode(saveMode);
|
|
546
|
+
setFailed(createPermissionDeniedError(saveMode === 'publish' ? 'publish' : 'save'), 'save');
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
const activeAppliedLoadSettlement = activeAppliedLoadSettlementRef.current;
|
|
550
|
+
if (activeAppliedLoadSettlement?.api === currentApi
|
|
551
|
+
&& Object.is(activeAppliedLoadSettlement.contextKey, contextKeyRef.current)) {
|
|
552
|
+
const loadSucceeded = await activeAppliedLoadSettlement.settlement;
|
|
553
|
+
if (!loadSucceeded || !mountedRef.current || apiRef.current !== currentApi)
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
else if (activeAppliedLoadSettlement) {
|
|
557
|
+
activeAppliedLoadSettlementRef.current = undefined;
|
|
558
|
+
}
|
|
559
|
+
discardActiveLoadedEnvelope();
|
|
560
|
+
const operationId = beginRemoteOperation(currentApi);
|
|
561
|
+
setError(undefined);
|
|
562
|
+
setLastErrorAction(undefined);
|
|
563
|
+
setLastErrorReason(undefined);
|
|
564
|
+
setLastSaveValidation(undefined);
|
|
565
|
+
setLastSaveAttemptMode(saveMode);
|
|
566
|
+
setStatus('saving');
|
|
567
|
+
try {
|
|
568
|
+
const { dirty: dirtyBeforeSave, lastChangeType: lastChangeTypeBeforeSave, metadata, productionAcceptancePhaseSummary, productionAcceptanceSnapshot, productionReadinessDecision, productionReadinessPhaseSummary, schema, schemaText, validation, } = prepareRemoteSaveContext({
|
|
569
|
+
api: currentApi,
|
|
570
|
+
blockProductionAcceptanceBeforePublish,
|
|
571
|
+
blockProductionReadinessWarningsBeforePublish,
|
|
572
|
+
blockWarningsBeforePublish,
|
|
573
|
+
getProductionAcceptanceSnapshot,
|
|
574
|
+
getProductionReadinessReport,
|
|
575
|
+
options,
|
|
576
|
+
validateBeforePublish,
|
|
577
|
+
validateBeforeSave,
|
|
578
|
+
});
|
|
579
|
+
await saveSchema(schema, {
|
|
580
|
+
api: currentApi,
|
|
581
|
+
dirty: dirtyBeforeSave,
|
|
582
|
+
lastChangeType: lastChangeTypeBeforeSave,
|
|
583
|
+
metadata,
|
|
584
|
+
productionAcceptancePhaseSummary,
|
|
585
|
+
productionAcceptanceSnapshot,
|
|
586
|
+
productionReadinessDecision,
|
|
587
|
+
productionReadinessPhaseSummary,
|
|
588
|
+
schema,
|
|
589
|
+
schemaText,
|
|
590
|
+
validation,
|
|
591
|
+
});
|
|
592
|
+
if (!isActiveOperation(operationId, currentApi)) {
|
|
593
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
markSavedIfSchemaUnchanged(currentApi, schemaText);
|
|
597
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
598
|
+
if (mountedRef.current) {
|
|
599
|
+
setError(undefined);
|
|
600
|
+
setLastErrorAction(undefined);
|
|
601
|
+
setLastErrorReason(undefined);
|
|
602
|
+
setStatus('saved');
|
|
603
|
+
setLastSavedAt(new Date());
|
|
604
|
+
setLastSaveMetadata(metadata);
|
|
605
|
+
setLastSaveValidation(validation);
|
|
606
|
+
syncDirtySnapshot(currentApi);
|
|
607
|
+
}
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
catch (nextError) {
|
|
611
|
+
if (!isActiveOperation(operationId, currentApi)) {
|
|
612
|
+
finishRemoteOperation(operationId, currentApi, false);
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
finishRemoteOperation(operationId, currentApi, true);
|
|
616
|
+
setFailed(nextError, 'save');
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
}, [
|
|
620
|
+
actionPermissions,
|
|
621
|
+
beginRemoteOperation,
|
|
622
|
+
discardActiveLoadedEnvelope,
|
|
623
|
+
finishRemoteOperation,
|
|
624
|
+
isActiveOperation,
|
|
625
|
+
markSavedIfSchemaUnchanged,
|
|
626
|
+
saveSchema,
|
|
627
|
+
setFailed,
|
|
628
|
+
syncDirtySnapshot,
|
|
629
|
+
blockProductionReadinessWarningsBeforePublish,
|
|
630
|
+
blockProductionAcceptanceBeforePublish,
|
|
631
|
+
blockWarningsBeforePublish,
|
|
632
|
+
getProductionAcceptanceSnapshot,
|
|
633
|
+
getProductionReadinessReport,
|
|
634
|
+
validateBeforePublish,
|
|
635
|
+
validateBeforeSave,
|
|
636
|
+
]);
|
|
637
|
+
useEffect(() => {
|
|
638
|
+
saveRemoteSchemaRef.current = saveRemoteSchema;
|
|
639
|
+
}, [saveRemoteSchema]);
|
|
640
|
+
useEffect(() => {
|
|
641
|
+
if (!autoLoad
|
|
642
|
+
|| !api
|
|
643
|
+
|| !loadSchema
|
|
644
|
+
|| !canLoadRemoteSchema(actionPermissions)
|
|
645
|
+
|| loadedApiRef.current === api)
|
|
646
|
+
return undefined;
|
|
647
|
+
const timer = window.setTimeout(() => {
|
|
648
|
+
void loadRemoteSchema();
|
|
649
|
+
}, 0);
|
|
650
|
+
return () => {
|
|
651
|
+
window.clearTimeout(timer);
|
|
652
|
+
};
|
|
653
|
+
}, [actionPermissions, api, autoLoad, contextKey, loadRemoteSchema, loadSchema]);
|
|
654
|
+
useEffect(() => {
|
|
655
|
+
if (!confirmBeforeUnload)
|
|
656
|
+
return undefined;
|
|
657
|
+
const handleBeforeUnload = (event) => {
|
|
658
|
+
const currentApi = apiRef.current;
|
|
659
|
+
if (!currentApi?.isDirty())
|
|
660
|
+
return undefined;
|
|
661
|
+
event.preventDefault();
|
|
662
|
+
Object.defineProperty(event, 'returnValue', {
|
|
663
|
+
configurable: true,
|
|
664
|
+
value: beforeUnloadMessage,
|
|
665
|
+
});
|
|
666
|
+
return beforeUnloadMessage;
|
|
667
|
+
};
|
|
668
|
+
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
669
|
+
return () => {
|
|
670
|
+
window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
671
|
+
};
|
|
672
|
+
}, [beforeUnloadMessage, confirmBeforeUnload]);
|
|
673
|
+
useEffect(() => {
|
|
674
|
+
if (autoSaveDelayMs === false
|
|
675
|
+
|| !dirty
|
|
676
|
+
|| !api
|
|
677
|
+
|| !saveSchema
|
|
678
|
+
|| !canSaveRemoteSchema(actionPermissions)
|
|
679
|
+
|| status === 'loading'
|
|
680
|
+
|| status === 'saving') {
|
|
681
|
+
return undefined;
|
|
682
|
+
}
|
|
683
|
+
if (lastAutoSaveRevisionRef.current === schemaChangeRevision) {
|
|
684
|
+
return undefined;
|
|
685
|
+
}
|
|
686
|
+
const delay = Math.max(0, autoSaveDelayMs);
|
|
687
|
+
const timer = window.setTimeout(() => {
|
|
688
|
+
lastAutoSaveRevisionRef.current = schemaChangeRevision;
|
|
689
|
+
void saveRemoteSchemaRef.current?.({ source: 'auto' });
|
|
690
|
+
}, delay);
|
|
691
|
+
return () => {
|
|
692
|
+
window.clearTimeout(timer);
|
|
693
|
+
};
|
|
694
|
+
}, [actionPermissions, api, autoSaveDelayMs, dirty, saveSchema, schemaChangeRevision, status]);
|
|
695
|
+
return useMemo(() => ({
|
|
696
|
+
api: api ?? undefined,
|
|
697
|
+
canSave: Boolean(api
|
|
698
|
+
&& saveSchema
|
|
699
|
+
&& (canSaveRemoteSchema(actionPermissions, 'draft') || canSaveRemoteSchema(actionPermissions, 'publish'))),
|
|
700
|
+
conflicted: status === 'conflict',
|
|
701
|
+
dirty,
|
|
702
|
+
error,
|
|
703
|
+
lastErrorAction,
|
|
704
|
+
lastErrorReason,
|
|
705
|
+
handleSchemaChange,
|
|
706
|
+
lastLoadedAt,
|
|
707
|
+
lastChangeType,
|
|
708
|
+
lastSaveAttemptMode,
|
|
709
|
+
lastSaveMetadata,
|
|
710
|
+
lastSaveValidation,
|
|
711
|
+
lastSavedAt,
|
|
712
|
+
loadRemoteSchema,
|
|
713
|
+
loading: status === 'loading',
|
|
714
|
+
saveRemoteSchema,
|
|
715
|
+
saving: status === 'saving',
|
|
716
|
+
status,
|
|
717
|
+
}), [
|
|
718
|
+
api,
|
|
719
|
+
actionPermissions,
|
|
720
|
+
dirty,
|
|
721
|
+
error,
|
|
722
|
+
handleSchemaChange,
|
|
723
|
+
lastErrorAction,
|
|
724
|
+
lastErrorReason,
|
|
725
|
+
lastLoadedAt,
|
|
726
|
+
lastChangeType,
|
|
727
|
+
lastSaveAttemptMode,
|
|
728
|
+
lastSaveMetadata,
|
|
729
|
+
lastSaveValidation,
|
|
730
|
+
lastSavedAt,
|
|
731
|
+
loadRemoteSchema,
|
|
732
|
+
saveRemoteSchema,
|
|
733
|
+
saveSchema,
|
|
734
|
+
status,
|
|
735
|
+
]);
|
|
736
|
+
};
|