@gravity-ui/page-constructor 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +2 -0
- package/build/cjs/blocks/Banner/schema.d.ts +63 -21
- package/build/cjs/blocks/CardLayout/schema.d.ts +10 -4
- package/build/cjs/blocks/Companies/schema.d.ts +5 -2
- package/build/cjs/blocks/ContentLayout/ContentLayout.js +3 -2
- package/build/cjs/blocks/ContentLayout/schema.d.ts +107 -61
- package/build/cjs/blocks/ContentLayout/schema.js +23 -18
- package/build/cjs/blocks/ExtendedFeatures/schema.d.ts +31 -4
- package/build/cjs/blocks/ExtendedFeatures/schema.js +2 -3
- package/build/cjs/blocks/FilterBlock/schema.d.ts +13 -5
- package/build/cjs/blocks/FilterBlock/schema.js +13 -2
- package/build/cjs/blocks/Header/schema.d.ts +92 -36
- package/build/cjs/blocks/Header/schema.js +1 -0
- package/build/cjs/blocks/HeaderSlider/schema.d.ts +48 -53
- package/build/cjs/blocks/HeaderSlider/schema.js +3 -1
- package/build/cjs/blocks/Icons/schema.d.ts +3 -1
- package/build/cjs/blocks/Icons/schema.js +2 -1
- package/build/cjs/blocks/Info/schema.d.ts +19 -8
- package/build/cjs/blocks/Map/schema.d.ts +17 -6
- package/build/cjs/blocks/Media/schema.d.ts +204 -152
- package/build/cjs/blocks/Media/schema.js +1 -1
- package/build/cjs/blocks/PromoFeaturesBlock/schema.d.ts +22 -8
- package/build/cjs/blocks/PromoFeaturesBlock/schema.js +1 -0
- package/build/cjs/blocks/Questions/schema.d.ts +7 -3
- package/build/cjs/blocks/Slider/schema.d.ts +13 -18
- package/build/cjs/blocks/Slider/schema.js +4 -5
- package/build/cjs/blocks/Table/schema.d.ts +5 -2
- package/build/cjs/blocks/Table/schema.js +12 -2
- package/build/cjs/blocks/Tabs/schema.d.ts +43 -16
- package/build/cjs/components/BlockBase/BlockBase.js +2 -2
- package/build/cjs/components/HeaderBreadcrumbs/HeaderBreadcrumbs.js +1 -1
- package/build/cjs/components/Image/schema.d.ts +1 -0
- package/build/cjs/components/Image/schema.js +3 -2
- package/build/cjs/containers/PageConstructor/components/ConstructorBlock/ConstructorBlock.js +1 -1
- package/build/cjs/containers/PageConstructor/components/ConstructorItem/ConstructorItem.js +1 -1
- package/build/cjs/customization/BlockDecoration.d.ts +1 -1
- package/build/cjs/customization/BlockDecoration.js +5 -6
- package/build/cjs/editor/{Components → components}/AddBlock/AddBlock.css +0 -7
- package/build/cjs/editor/components/AddBlock/AddBlock.d.ts +7 -0
- package/build/cjs/editor/{Components → components}/AddBlock/AddBlock.js +2 -2
- package/build/cjs/editor/components/BlockForm/BlockForm.d.ts +12 -0
- package/build/cjs/editor/components/BlockForm/BlockForm.js +26 -0
- package/build/cjs/editor/components/EditBlock/EditBlock.d.ts +13 -0
- package/build/cjs/editor/components/EditBlock/EditBlock.js +44 -0
- package/build/cjs/editor/components/ErrorBoundary/ErrorBoundary.css +27 -0
- package/build/cjs/editor/components/ErrorBoundary/ErrorBoundary.d.ts +13 -0
- package/build/cjs/editor/components/ErrorBoundary/ErrorBoundary.js +35 -0
- package/build/cjs/editor/components/ErrorBoundary/i18n/en.json +4 -0
- package/build/cjs/editor/components/ErrorBoundary/i18n/index.d.ts +2 -0
- package/build/cjs/editor/components/ErrorBoundary/i18n/index.js +8 -0
- package/build/cjs/editor/components/ErrorBoundary/i18n/ru.json +4 -0
- package/build/cjs/editor/components/PagePropsForm/PagePropsForm.d.ts +10 -0
- package/build/cjs/editor/components/PagePropsForm/PagePropsForm.js +17 -0
- package/build/cjs/editor/containers/Editor/Editor.css +28 -0
- package/build/cjs/editor/containers/Editor/Editor.d.ts +2 -0
- package/build/cjs/editor/containers/Editor/Editor.js +37 -0
- package/build/cjs/editor/containers/Form/Form.css +94 -0
- package/build/cjs/editor/containers/Form/Form.d.ts +11 -0
- package/build/cjs/editor/containers/Form/Form.js +47 -0
- package/build/cjs/editor/containers/Form/dynamic-form-custom.css +0 -0
- package/build/cjs/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.css +27 -0
- package/build/cjs/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.d.ts +19 -0
- package/build/cjs/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.js +72 -0
- package/build/cjs/editor/dynamic-forms-custom/config.d.ts +2 -0
- package/build/cjs/editor/dynamic-forms-custom/config.js +13 -0
- package/build/cjs/editor/dynamic-forms-custom/hooks/useOneOf.d.ts +12 -0
- package/build/cjs/editor/dynamic-forms-custom/hooks/useOneOf.js +75 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/detect.d.ts +9 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/detect.js +36 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/index.d.ts +28 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/index.js +188 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/types.d.ts +29 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/types.js +2 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/views.d.ts +37 -0
- package/build/cjs/editor/dynamic-forms-custom/parser/views.js +46 -0
- package/build/cjs/editor/hooks/useFormSpec.d.ts +2 -0
- package/build/cjs/editor/hooks/useFormSpec.js +13 -0
- package/build/cjs/editor/index.d.ts +1 -1
- package/build/cjs/editor/index.js +1 -1
- package/build/cjs/editor/store/index.d.ts +8 -11
- package/build/cjs/editor/store/index.js +61 -15
- package/build/cjs/editor/store/reducer.d.ts +17 -9
- package/build/cjs/editor/store/reducer.js +14 -31
- package/build/cjs/editor/store/utils.d.ts +1 -0
- package/build/cjs/editor/store/utils.js +3 -1
- package/build/cjs/editor/types/index.d.ts +11 -9
- package/build/cjs/editor/utils/index.d.ts +4 -3
- package/build/cjs/editor/utils/index.js +6 -4
- package/build/cjs/grid/Col/Col.d.ts +1 -1
- package/build/cjs/models/constructor-items/blocks.d.ts +13 -8
- package/build/cjs/models/constructor.d.ts +2 -2
- package/build/cjs/models/customization.d.ts +3 -8
- package/build/cjs/navigation/schema.d.ts +1 -0
- package/build/cjs/schema/constants.d.ts +908 -0
- package/build/cjs/schema/constants.js +42 -0
- package/build/cjs/schema/index.d.ts +9 -228
- package/build/cjs/schema/index.js +8 -46
- package/build/cjs/schema/validators/common.d.ts +71 -27
- package/build/cjs/schema/validators/common.js +50 -17
- package/build/cjs/sub-blocks/BackgroundCard/schema.d.ts +12 -5
- package/build/cjs/sub-blocks/BasicCard/schema.d.ts +8 -3
- package/build/cjs/sub-blocks/Content/schema.d.ts +14 -6
- package/build/cjs/sub-blocks/Content/schema.js +9 -1
- package/build/cjs/sub-blocks/LayoutItem/schema.d.ts +23 -9
- package/build/cjs/sub-blocks/MediaCard/schema.d.ts +16 -6
- package/build/cjs/sub-blocks/PriceDetailed/schema.d.ts +1 -0
- package/build/cjs/sub-blocks/PriceDetailed/schema.js +5 -1
- package/build/cjs/sub-blocks/Quote/schema.d.ts +6 -2
- package/build/esm/blocks/Banner/schema.d.ts +63 -21
- package/build/esm/blocks/CardLayout/schema.d.ts +10 -4
- package/build/esm/blocks/Companies/schema.d.ts +5 -2
- package/build/esm/blocks/ContentLayout/ContentLayout.js +3 -2
- package/build/esm/blocks/ContentLayout/schema.d.ts +107 -61
- package/build/esm/blocks/ContentLayout/schema.js +23 -18
- package/build/esm/blocks/ExtendedFeatures/schema.d.ts +31 -4
- package/build/esm/blocks/ExtendedFeatures/schema.js +3 -4
- package/build/esm/blocks/FilterBlock/schema.d.ts +13 -5
- package/build/esm/blocks/FilterBlock/schema.js +13 -2
- package/build/esm/blocks/Header/schema.d.ts +92 -36
- package/build/esm/blocks/Header/schema.js +1 -0
- package/build/esm/blocks/HeaderSlider/schema.d.ts +48 -53
- package/build/esm/blocks/HeaderSlider/schema.js +2 -1
- package/build/esm/blocks/Icons/schema.d.ts +3 -1
- package/build/esm/blocks/Icons/schema.js +2 -1
- package/build/esm/blocks/Info/schema.d.ts +19 -8
- package/build/esm/blocks/Map/schema.d.ts +17 -6
- package/build/esm/blocks/Media/schema.d.ts +204 -152
- package/build/esm/blocks/Media/schema.js +2 -2
- package/build/esm/blocks/PromoFeaturesBlock/schema.d.ts +22 -8
- package/build/esm/blocks/PromoFeaturesBlock/schema.js +1 -0
- package/build/esm/blocks/Questions/schema.d.ts +7 -3
- package/build/esm/blocks/Slider/schema.d.ts +13 -18
- package/build/esm/blocks/Slider/schema.js +4 -5
- package/build/esm/blocks/Table/schema.d.ts +5 -2
- package/build/esm/blocks/Table/schema.js +12 -2
- package/build/esm/blocks/Tabs/schema.d.ts +43 -16
- package/build/esm/components/BlockBase/BlockBase.js +2 -2
- package/build/esm/components/HeaderBreadcrumbs/HeaderBreadcrumbs.js +1 -1
- package/build/esm/components/Image/schema.d.ts +1 -0
- package/build/esm/components/Image/schema.js +3 -2
- package/build/esm/containers/PageConstructor/components/ConstructorBlock/ConstructorBlock.js +1 -1
- package/build/esm/containers/PageConstructor/components/ConstructorItem/ConstructorItem.js +1 -1
- package/build/esm/customization/BlockDecoration.d.ts +1 -1
- package/build/esm/customization/BlockDecoration.js +6 -6
- package/build/esm/editor/{Components → components}/AddBlock/AddBlock.css +0 -7
- package/build/esm/editor/components/AddBlock/AddBlock.d.ts +8 -0
- package/build/esm/editor/{Components → components}/AddBlock/AddBlock.js +2 -2
- package/build/esm/editor/components/BlockForm/BlockForm.d.ts +12 -0
- package/build/esm/editor/components/BlockForm/BlockForm.js +23 -0
- package/build/esm/editor/components/EditBlock/EditBlock.d.ts +14 -0
- package/build/esm/editor/components/EditBlock/EditBlock.js +41 -0
- package/build/esm/editor/components/ErrorBoundary/ErrorBoundary.css +27 -0
- package/build/esm/editor/components/ErrorBoundary/ErrorBoundary.d.ts +14 -0
- package/build/esm/editor/components/ErrorBoundary/ErrorBoundary.js +31 -0
- package/build/esm/editor/components/ErrorBoundary/i18n/en.json +4 -0
- package/build/esm/editor/components/ErrorBoundary/i18n/index.d.ts +2 -0
- package/build/esm/editor/components/ErrorBoundary/i18n/index.js +5 -0
- package/build/esm/editor/components/ErrorBoundary/i18n/ru.json +4 -0
- package/build/esm/editor/components/PagePropsForm/PagePropsForm.d.ts +10 -0
- package/build/esm/editor/components/PagePropsForm/PagePropsForm.js +13 -0
- package/build/esm/editor/containers/Editor/Editor.css +28 -0
- package/build/esm/editor/containers/Editor/Editor.d.ts +3 -0
- package/build/esm/editor/containers/Editor/Editor.js +34 -0
- package/build/esm/editor/containers/Form/Form.css +94 -0
- package/build/esm/editor/containers/Form/Form.d.ts +12 -0
- package/build/esm/editor/containers/Form/Form.js +45 -0
- package/build/esm/editor/containers/Form/dynamic-form-custom.css +0 -0
- package/build/esm/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.css +27 -0
- package/build/esm/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.d.ts +20 -0
- package/build/esm/editor/dynamic-forms-custom/components/OneOfCustom/OneOfCustom.js +68 -0
- package/build/esm/editor/dynamic-forms-custom/config.d.ts +2 -0
- package/build/esm/editor/dynamic-forms-custom/config.js +9 -0
- package/build/esm/editor/dynamic-forms-custom/hooks/useOneOf.d.ts +12 -0
- package/build/esm/editor/dynamic-forms-custom/hooks/useOneOf.js +69 -0
- package/build/esm/editor/dynamic-forms-custom/parser/detect.d.ts +9 -0
- package/build/esm/editor/dynamic-forms-custom/parser/detect.js +32 -0
- package/build/esm/editor/dynamic-forms-custom/parser/index.d.ts +28 -0
- package/build/esm/editor/dynamic-forms-custom/parser/index.js +186 -0
- package/build/esm/editor/dynamic-forms-custom/parser/types.d.ts +29 -0
- package/build/esm/editor/dynamic-forms-custom/parser/types.js +1 -0
- package/build/esm/editor/dynamic-forms-custom/parser/views.d.ts +37 -0
- package/build/esm/editor/dynamic-forms-custom/parser/views.js +39 -0
- package/build/esm/editor/hooks/useFormSpec.d.ts +2 -0
- package/build/esm/editor/hooks/useFormSpec.js +9 -0
- package/build/esm/editor/index.d.ts +1 -1
- package/build/esm/editor/index.js +1 -1
- package/build/esm/editor/store/index.d.ts +8 -11
- package/build/esm/editor/store/index.js +63 -17
- package/build/esm/editor/store/reducer.d.ts +17 -9
- package/build/esm/editor/store/reducer.js +12 -29
- package/build/esm/editor/store/utils.d.ts +1 -0
- package/build/esm/editor/store/utils.js +1 -0
- package/build/esm/editor/types/index.d.ts +11 -9
- package/build/esm/editor/utils/index.d.ts +4 -3
- package/build/esm/editor/utils/index.js +4 -3
- package/build/esm/grid/Col/Col.d.ts +1 -1
- package/build/esm/models/constructor-items/blocks.d.ts +13 -8
- package/build/esm/models/constructor.d.ts +2 -2
- package/build/esm/models/customization.d.ts +3 -8
- package/build/esm/navigation/schema.d.ts +1 -0
- package/build/esm/schema/constants.d.ts +908 -0
- package/build/esm/schema/constants.js +39 -0
- package/build/esm/schema/index.d.ts +9 -228
- package/build/esm/schema/index.js +4 -42
- package/build/esm/schema/validators/common.d.ts +71 -27
- package/build/esm/schema/validators/common.js +49 -16
- package/build/esm/sub-blocks/BackgroundCard/schema.d.ts +12 -5
- package/build/esm/sub-blocks/BasicCard/schema.d.ts +8 -3
- package/build/esm/sub-blocks/Content/schema.d.ts +14 -6
- package/build/esm/sub-blocks/Content/schema.js +9 -1
- package/build/esm/sub-blocks/LayoutItem/schema.d.ts +23 -9
- package/build/esm/sub-blocks/MediaCard/schema.d.ts +16 -6
- package/build/esm/sub-blocks/PriceDetailed/schema.d.ts +1 -0
- package/build/esm/sub-blocks/PriceDetailed/schema.js +5 -1
- package/build/esm/sub-blocks/Quote/schema.d.ts +6 -2
- package/package.json +5 -1
- package/server/models/constructor-items/blocks.d.ts +13 -8
- package/server/models/constructor.d.ts +2 -2
- package/server/models/customization.d.ts +3 -8
- package/build/cjs/editor/Components/AddBlock/AddBlock.d.ts +0 -7
- package/build/cjs/editor/Components/EditBlock/EditBlock.d.ts +0 -4
- package/build/cjs/editor/Components/EditBlock/EditBlock.js +0 -32
- package/build/cjs/editor/Containers/Editor.d.ts +0 -2
- package/build/cjs/editor/Containers/Editor.js +0 -24
- package/build/esm/editor/Components/AddBlock/AddBlock.d.ts +0 -8
- package/build/esm/editor/Components/EditBlock/EditBlock.d.ts +0 -5
- package/build/esm/editor/Components/EditBlock/EditBlock.js +0 -30
- package/build/esm/editor/Containers/Editor.d.ts +0 -2
- package/build/esm/editor/Containers/Editor.js +0 -20
- /package/build/cjs/editor/{Components → components}/EditBlock/EditBlock.css +0 -0
- /package/build/esm/editor/{Components → components}/EditBlock/EditBlock.css +0 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dynamicConfig = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const dynamic_forms_1 = require("@gravity-ui/dynamic-forms");
|
|
6
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
7
|
+
const OneOfCustom_1 = require("./components/OneOfCustom/OneOfCustom");
|
|
8
|
+
const getDynamicConfig = () => {
|
|
9
|
+
const dynamicConfig = lodash_1.default.cloneDeep(dynamic_forms_1.dynamicConfig);
|
|
10
|
+
dynamicConfig.object.inputs['oneof_custom'] = { Component: OneOfCustom_1.OneOfCustom, independent: true };
|
|
11
|
+
return dynamicConfig;
|
|
12
|
+
};
|
|
13
|
+
exports.dynamicConfig = getDynamicConfig();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ObjectIndependentInputProps, Spec, SpecTypes } from '@gravity-ui/dynamic-forms';
|
|
2
|
+
export interface UseOneOfParams {
|
|
3
|
+
props: ObjectIndependentInputProps;
|
|
4
|
+
onTogglerChange?: (value: string) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare const getSpecTypeDefaultValue: (type: SpecTypes) => {} | undefined;
|
|
7
|
+
export declare const useOneOf: ({ props, onTogglerChange }: UseOneOfParams) => {
|
|
8
|
+
oneOfValue: string;
|
|
9
|
+
specProperties: Record<string, Spec>;
|
|
10
|
+
toggler: JSX.Element;
|
|
11
|
+
togglerInput: JSX.Element;
|
|
12
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOneOf = exports.getSpecTypeDefaultValue = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = tslib_1.__importDefault(require("react"));
|
|
6
|
+
const dynamic_forms_1 = require("@gravity-ui/dynamic-forms");
|
|
7
|
+
const uikit_1 = require("@gravity-ui/uikit");
|
|
8
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
9
|
+
const MAX_TAB_TITLE_LENGTH = 20;
|
|
10
|
+
const getSpecTypeDefaultValue = (type) => {
|
|
11
|
+
switch (type) {
|
|
12
|
+
case dynamic_forms_1.SpecTypes.Array:
|
|
13
|
+
return [];
|
|
14
|
+
case dynamic_forms_1.SpecTypes.Boolean:
|
|
15
|
+
case dynamic_forms_1.SpecTypes.Number:
|
|
16
|
+
case dynamic_forms_1.SpecTypes.String:
|
|
17
|
+
return undefined;
|
|
18
|
+
default:
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.getSpecTypeDefaultValue = getSpecTypeDefaultValue;
|
|
23
|
+
const useOneOf = ({ props, onTogglerChange }) => {
|
|
24
|
+
const { name, input, spec, Layout } = props;
|
|
25
|
+
const { order, disabled, oneOfParams } = spec.viewSpec;
|
|
26
|
+
const specProperties = react_1.default.useMemo(() => (lodash_1.default.isObjectLike(spec.properties) ? spec.properties : {}), [spec.properties]);
|
|
27
|
+
const [oneOfValue, setOneOfValue] = react_1.default.useState(() => {
|
|
28
|
+
let valueKeys;
|
|
29
|
+
if (lodash_1.default.isObjectLike(input.value)) {
|
|
30
|
+
const keys = Object.keys(input.value);
|
|
31
|
+
if (keys.length) {
|
|
32
|
+
valueKeys = keys;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return (valueKeys || order || Object.keys(specProperties))[0];
|
|
36
|
+
});
|
|
37
|
+
const onOneOfChange = react_1.default.useCallback(([newValue]) => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (newValue !== oneOfValue) {
|
|
40
|
+
const specType = ((_a = specProperties[newValue]) === null || _a === void 0 ? void 0 : _a.type) || dynamic_forms_1.SpecTypes.Object;
|
|
41
|
+
input.onChange((0, exports.getSpecTypeDefaultValue)(specType));
|
|
42
|
+
setOneOfValue(newValue);
|
|
43
|
+
onTogglerChange === null || onTogglerChange === void 0 ? void 0 : onTogglerChange(newValue);
|
|
44
|
+
}
|
|
45
|
+
}, [setOneOfValue, input, oneOfValue, specProperties, onTogglerChange]);
|
|
46
|
+
const options = react_1.default.useMemo(() => (order || Object.keys(specProperties)).map((value) => {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
const title = ((_a = spec.description) === null || _a === void 0 ? void 0 : _a[value]) ||
|
|
49
|
+
((_b = specProperties[value]) === null || _b === void 0 ? void 0 : _b.viewSpec.layoutTitle) ||
|
|
50
|
+
value ||
|
|
51
|
+
'';
|
|
52
|
+
return {
|
|
53
|
+
value,
|
|
54
|
+
title,
|
|
55
|
+
content: title,
|
|
56
|
+
};
|
|
57
|
+
}), [spec.description, order, specProperties]);
|
|
58
|
+
const togglerInput = react_1.default.useMemo(() => {
|
|
59
|
+
if ((oneOfParams === null || oneOfParams === void 0 ? void 0 : oneOfParams.toggler) !== 'radio' &&
|
|
60
|
+
((oneOfParams === null || oneOfParams === void 0 ? void 0 : oneOfParams.toggler) === 'select' ||
|
|
61
|
+
options.length > 3 ||
|
|
62
|
+
lodash_1.default.some(options, ({ title }) => title.length > MAX_TAB_TITLE_LENGTH))) {
|
|
63
|
+
return (react_1.default.createElement(uikit_1.Select, { width: "max", value: [oneOfValue], onUpdate: onOneOfChange, options: options, disabled: disabled, filterable: options.length > 7, qa: name }));
|
|
64
|
+
}
|
|
65
|
+
return (react_1.default.createElement(uikit_1.RadioButton, { value: oneOfValue, onChange: (event) => onOneOfChange([event.target.value]), disabled: disabled, qa: name }, options.map(({ value, title }) => (react_1.default.createElement(uikit_1.RadioButton.Option, { key: value, value: value }, title)))));
|
|
66
|
+
}, [options, oneOfValue, onOneOfChange, name, oneOfParams === null || oneOfParams === void 0 ? void 0 : oneOfParams.toggler, disabled]);
|
|
67
|
+
const toggler = react_1.default.useMemo(() => {
|
|
68
|
+
if (Layout) {
|
|
69
|
+
return react_1.default.createElement(Layout, Object.assign({}, props), togglerInput);
|
|
70
|
+
}
|
|
71
|
+
return togglerInput;
|
|
72
|
+
}, [Layout, togglerInput, props]);
|
|
73
|
+
return { oneOfValue, specProperties, toggler, togglerInput };
|
|
74
|
+
};
|
|
75
|
+
exports.useOneOf = useOneOf;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.detectParserType = exports.ParserType = void 0;
|
|
4
|
+
const dynamic_forms_1 = require("@gravity-ui/dynamic-forms");
|
|
5
|
+
var ParserType;
|
|
6
|
+
(function (ParserType) {
|
|
7
|
+
ParserType["Object"] = "object";
|
|
8
|
+
ParserType["Array"] = "array";
|
|
9
|
+
ParserType["Children"] = "children";
|
|
10
|
+
ParserType["OneOf"] = "oneOf";
|
|
11
|
+
ParserType["Primitive"] = "primitive";
|
|
12
|
+
})(ParserType = exports.ParserType || (exports.ParserType = {}));
|
|
13
|
+
const isOneOf = (data) => 'oneOf' in data;
|
|
14
|
+
const isObject = (data) => 'properties' in data;
|
|
15
|
+
const isArray = (data) => 'type' in data && data.type === dynamic_forms_1.SpecTypes.Array;
|
|
16
|
+
const isChildren = (data) => 'type' in data &&
|
|
17
|
+
data.type === dynamic_forms_1.SpecTypes.Array &&
|
|
18
|
+
'items' in data &&
|
|
19
|
+
typeof data.items !== 'undefined' &&
|
|
20
|
+
'$ref' in data.items;
|
|
21
|
+
//detector applying order matters!
|
|
22
|
+
const ParserTypeDetectors = [
|
|
23
|
+
{ type: ParserType.OneOf, detector: isOneOf },
|
|
24
|
+
{ type: ParserType.Children, detector: isChildren },
|
|
25
|
+
{ type: ParserType.Object, detector: isObject },
|
|
26
|
+
{ type: ParserType.Array, detector: isArray },
|
|
27
|
+
];
|
|
28
|
+
const detectParserType = (data) => {
|
|
29
|
+
for (const { type, detector } of ParserTypeDetectors) {
|
|
30
|
+
if (detector(data)) {
|
|
31
|
+
return type;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return ParserType.Primitive;
|
|
35
|
+
};
|
|
36
|
+
exports.detectParserType = detectParserType;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Schema } from '../../../schema';
|
|
2
|
+
import { FormSpecs } from './types';
|
|
3
|
+
declare class FormSpecParser {
|
|
4
|
+
private schema;
|
|
5
|
+
private definitions;
|
|
6
|
+
/**
|
|
7
|
+
* Transforms page-constructor json schema into dynamic-forms specs for each block
|
|
8
|
+
*
|
|
9
|
+
* @param schema {Schema} - page-constructor json schema
|
|
10
|
+
* @returns {FormSpecs} - object containig dynamic-forms specs for each constructor block
|
|
11
|
+
*/
|
|
12
|
+
parse(schema: Schema): FormSpecs;
|
|
13
|
+
private getChildrenSpec;
|
|
14
|
+
private getRequiredProperties;
|
|
15
|
+
private childrenParser;
|
|
16
|
+
private oneOfParser;
|
|
17
|
+
private arrayParser;
|
|
18
|
+
private objectParser;
|
|
19
|
+
private primitiveParser;
|
|
20
|
+
private schemaParserMap;
|
|
21
|
+
private parseSchemaProperty;
|
|
22
|
+
private getBlocksSpec;
|
|
23
|
+
private getPageSpec;
|
|
24
|
+
private getFormSpec;
|
|
25
|
+
private init;
|
|
26
|
+
}
|
|
27
|
+
declare const _default: FormSpecParser;
|
|
28
|
+
export default _default;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
/* eslint-disable no-param-reassign */
|
|
5
|
+
/* eslint-disable no-not-accumulator-reassign/no-not-accumulator-reassign */
|
|
6
|
+
const dynamic_forms_1 = require("@gravity-ui/dynamic-forms");
|
|
7
|
+
const models_1 = require("../../../models");
|
|
8
|
+
const detect_1 = require("./detect");
|
|
9
|
+
const views_1 = require("./views");
|
|
10
|
+
class FormSpecParser {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.schema = {};
|
|
13
|
+
this.definitions = {};
|
|
14
|
+
this.getChildrenSpec = (data) => {
|
|
15
|
+
var _a, _b, _c;
|
|
16
|
+
const childrenType = (_c = (_b = (_a = data.items) === null || _a === void 0 ? void 0 : _a.$ref) === null || _b === void 0 ? void 0 : _b.split('/')) === null || _c === void 0 ? void 0 : _c.pop();
|
|
17
|
+
return this.definitions[childrenType];
|
|
18
|
+
};
|
|
19
|
+
this.getRequiredProperties = (data) => Array.isArray(data.required) ? data.required : [];
|
|
20
|
+
this.childrenParser = ({ data, name, required }) => {
|
|
21
|
+
const childSpec = this.getChildrenSpec(data);
|
|
22
|
+
const properties = childSpec &&
|
|
23
|
+
Object.entries(childSpec).reduce((parsedChildSpecProperties, [childName, childSchema]) => {
|
|
24
|
+
const childRequiredProperties = this.getRequiredProperties(childSchema);
|
|
25
|
+
const childProperies = childSchema.properties &&
|
|
26
|
+
Object.entries(childSchema.properties).reduce((parsedChildProperties, [childPropertyName, childPropertyData]) => {
|
|
27
|
+
parsedChildProperties[childPropertyName] = this.parseSchemaProperty({
|
|
28
|
+
data: childPropertyData,
|
|
29
|
+
name: childPropertyName,
|
|
30
|
+
required: childRequiredProperties.includes(childPropertyName),
|
|
31
|
+
});
|
|
32
|
+
return parsedChildProperties;
|
|
33
|
+
}, {});
|
|
34
|
+
const childJsonSchema = Object.assign(Object.assign({}, childSchema), { properties: Object.assign(Object.assign({}, childSchema.properties), { type: {
|
|
35
|
+
type: dynamic_forms_1.SpecTypes.String,
|
|
36
|
+
enum: [childName],
|
|
37
|
+
} }) });
|
|
38
|
+
parsedChildSpecProperties[childName] = {
|
|
39
|
+
type: dynamic_forms_1.SpecTypes.Array,
|
|
40
|
+
items: Object.assign(Object.assign({}, childSchema), { type: dynamic_forms_1.SpecTypes.Object, properties: Object.assign(Object.assign({}, childProperies), { type: {
|
|
41
|
+
type: dynamic_forms_1.SpecTypes.String,
|
|
42
|
+
defaultValue: childName,
|
|
43
|
+
viewSpec: {
|
|
44
|
+
type: 'hidden',
|
|
45
|
+
},
|
|
46
|
+
} }), viewSpec: childProperies &&
|
|
47
|
+
(0, views_1.getObjectViewSpec)({
|
|
48
|
+
properties: childProperies,
|
|
49
|
+
layoutTitle: childName,
|
|
50
|
+
}), __schema: childJsonSchema }),
|
|
51
|
+
required: false,
|
|
52
|
+
viewSpec: (0, views_1.getArrayViewSpec)({ layoutTitle: childName }),
|
|
53
|
+
__schema: {
|
|
54
|
+
type: dynamic_forms_1.SpecTypes.Array,
|
|
55
|
+
items: childJsonSchema,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
return parsedChildSpecProperties;
|
|
59
|
+
}, {});
|
|
60
|
+
return {
|
|
61
|
+
type: dynamic_forms_1.SpecTypes.Object,
|
|
62
|
+
properties,
|
|
63
|
+
viewSpec: {
|
|
64
|
+
type: 'oneof_custom',
|
|
65
|
+
layout: 'row',
|
|
66
|
+
layoutTitle: name,
|
|
67
|
+
oneOfParams: {
|
|
68
|
+
toggler: 'select',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
required,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
this.oneOfParser = ({ data, name, required }) => {
|
|
75
|
+
const requiredProperties = this.getRequiredProperties(data);
|
|
76
|
+
const properties = data.oneOf &&
|
|
77
|
+
data.oneOf.reduce((result, propertyData, index) => {
|
|
78
|
+
const propertyName = (propertyData === null || propertyData === void 0 ? void 0 : propertyData.optionName)
|
|
79
|
+
? propertyData === null || propertyData === void 0 ? void 0 : propertyData.optionName
|
|
80
|
+
: `${name}_${index}`;
|
|
81
|
+
result[propertyName] = this.parseSchemaProperty({
|
|
82
|
+
data: propertyData,
|
|
83
|
+
name: propertyName,
|
|
84
|
+
required: requiredProperties.includes(propertyName),
|
|
85
|
+
});
|
|
86
|
+
return result;
|
|
87
|
+
}, {});
|
|
88
|
+
return {
|
|
89
|
+
type: dynamic_forms_1.SpecTypes.Object,
|
|
90
|
+
properties,
|
|
91
|
+
required,
|
|
92
|
+
viewSpec: (0, views_1.getOneOfViewSpec)({ layoutTitle: name }),
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
this.arrayParser = ({ data, name }) => {
|
|
96
|
+
const items = data.items && this.parseSchemaProperty({ data: data.items, name });
|
|
97
|
+
return Object.assign(Object.assign({}, data), { type: dynamic_forms_1.SpecTypes.Array, items, viewSpec: (0, views_1.getArrayViewSpec)({ layoutTitle: name }) });
|
|
98
|
+
};
|
|
99
|
+
this.objectParser = ({ data, name, required }) => {
|
|
100
|
+
const requiredProperties = this.getRequiredProperties(data);
|
|
101
|
+
const properties = data.properties &&
|
|
102
|
+
Object.entries(data.properties).reduce((result, [propertyName, propertyData]) => {
|
|
103
|
+
result[propertyName] = this.parseSchemaProperty({
|
|
104
|
+
data: propertyData,
|
|
105
|
+
name: propertyName,
|
|
106
|
+
required: requiredProperties.includes(propertyName),
|
|
107
|
+
});
|
|
108
|
+
return result;
|
|
109
|
+
}, {});
|
|
110
|
+
return Object.assign(Object.assign({}, data), { type: dynamic_forms_1.SpecTypes.Object, properties, viewSpec: (0, views_1.getObjectViewSpec)({ properties, layoutTitle: name }), required });
|
|
111
|
+
};
|
|
112
|
+
this.primitiveParser = ({ data, name, required }) => {
|
|
113
|
+
return Object.assign(Object.assign({}, data), { type: data.type, required, defaultValue: data.default, viewSpec: (0, views_1.getPrimitiveViewSpec)({ layoutTitle: name, data }), validator: 'base' });
|
|
114
|
+
};
|
|
115
|
+
//eslint-disable-next-line @typescript-eslint/member-ordering
|
|
116
|
+
this.schemaParserMap = {
|
|
117
|
+
[detect_1.ParserType.Object]: this.objectParser,
|
|
118
|
+
[detect_1.ParserType.Array]: this.arrayParser,
|
|
119
|
+
[detect_1.ParserType.Primitive]: this.primitiveParser,
|
|
120
|
+
[detect_1.ParserType.OneOf]: this.oneOfParser,
|
|
121
|
+
[detect_1.ParserType.Children]: this.childrenParser,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Transforms page-constructor json schema into dynamic-forms specs for each block
|
|
126
|
+
*
|
|
127
|
+
* @param schema {Schema} - page-constructor json schema
|
|
128
|
+
* @returns {FormSpecs} - object containig dynamic-forms specs for each constructor block
|
|
129
|
+
*/
|
|
130
|
+
parse(schema) {
|
|
131
|
+
this.init(schema);
|
|
132
|
+
return this.getFormSpec();
|
|
133
|
+
}
|
|
134
|
+
parseSchemaProperty({ data, name, required }) {
|
|
135
|
+
const parserType = (0, detect_1.detectParserType)(data);
|
|
136
|
+
const parser = this.schemaParserMap[parserType];
|
|
137
|
+
return Object.assign(Object.assign({}, parser({ data, name, required })), {
|
|
138
|
+
//save json schema from constructor to compare with incoming intial data inside oneOf form fields
|
|
139
|
+
__schema: data });
|
|
140
|
+
}
|
|
141
|
+
getBlocksSpec() {
|
|
142
|
+
const blocks = this.definitions.children;
|
|
143
|
+
return Object.values(models_1.BlockType).reduce((result, blockName) => {
|
|
144
|
+
result[blockName] = this.parseSchemaProperty({
|
|
145
|
+
name: blockName,
|
|
146
|
+
data: Object.assign({}, blocks[blockName]),
|
|
147
|
+
required: true,
|
|
148
|
+
});
|
|
149
|
+
return result;
|
|
150
|
+
}, {});
|
|
151
|
+
}
|
|
152
|
+
getPageSpec() {
|
|
153
|
+
var _a;
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
155
|
+
const _b = ((_a = this.schema) === null || _a === void 0 ? void 0 : _a.properties) || {}, { blocks } = _b, pageSchema = tslib_1.__rest(_b, ["blocks"]);
|
|
156
|
+
const requiredProperties = this.getRequiredProperties(this.schema);
|
|
157
|
+
const properties = Object.entries(pageSchema).reduce((result, [propertyName, propertyData]) => {
|
|
158
|
+
result[propertyName] = this.parseSchemaProperty({
|
|
159
|
+
name: propertyName,
|
|
160
|
+
data: propertyData,
|
|
161
|
+
required: requiredProperties.includes(propertyName),
|
|
162
|
+
});
|
|
163
|
+
return result;
|
|
164
|
+
}, {});
|
|
165
|
+
return {
|
|
166
|
+
type: dynamic_forms_1.SpecTypes.Object,
|
|
167
|
+
properties,
|
|
168
|
+
viewSpec: (0, views_1.getObjectViewSpec)({ properties, layout: 'section' }),
|
|
169
|
+
required: true,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
getFormSpec() {
|
|
173
|
+
return {
|
|
174
|
+
blocks: this.getBlocksSpec(),
|
|
175
|
+
page: this.getPageSpec(),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
init(schema) {
|
|
179
|
+
this.schema = schema;
|
|
180
|
+
if (schema.definitions) {
|
|
181
|
+
this.definitions = Object.entries(schema.definitions).reduce((result, [childType, childSpec]) => {
|
|
182
|
+
result[childType] = childSpec === null || childSpec === void 0 ? void 0 : childSpec.selectCases;
|
|
183
|
+
return result;
|
|
184
|
+
}, {});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.default = new FormSpecParser();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Spec as DynamicFormSpec, ObjectSpec } from '@gravity-ui/dynamic-forms';
|
|
2
|
+
import { BlockType } from '../../../models';
|
|
3
|
+
import { Schema } from '../../../schema';
|
|
4
|
+
export type OneOfSpec = {
|
|
5
|
+
oneOf: DynamicFormSpec[];
|
|
6
|
+
viewSpec: ObjectSpec['viewSpec'];
|
|
7
|
+
};
|
|
8
|
+
export type CustomObjectSpec = Omit<ObjectSpec, 'properties'> & {
|
|
9
|
+
properties: Record<string, CustomSpec>;
|
|
10
|
+
};
|
|
11
|
+
export interface SpecCustomProps {
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
inputType?: string;
|
|
14
|
+
__schema?: Schema;
|
|
15
|
+
}
|
|
16
|
+
export type Spec = Exclude<DynamicFormSpec, 'ObjectSpec'> | CustomObjectSpec | OneOfSpec;
|
|
17
|
+
export type CustomSpec = Spec & SpecCustomProps;
|
|
18
|
+
export type BlocksSpec = Record<BlockType, CustomSpec>;
|
|
19
|
+
export type PageSpec = CustomSpec;
|
|
20
|
+
export interface FormSpecs {
|
|
21
|
+
blocks: BlocksSpec;
|
|
22
|
+
page: PageSpec;
|
|
23
|
+
}
|
|
24
|
+
export interface SchemaParserParams {
|
|
25
|
+
data: Schema;
|
|
26
|
+
name: string;
|
|
27
|
+
required?: Schema['required'];
|
|
28
|
+
}
|
|
29
|
+
export type SchemaParser = (params: SchemaParserParams) => CustomSpec;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema } from '../../../schema';
|
|
2
|
+
import { CustomObjectSpec, CustomSpec } from './types';
|
|
3
|
+
interface BaseParams {
|
|
4
|
+
layoutTitle?: string;
|
|
5
|
+
}
|
|
6
|
+
interface GetObjectViewSpecParams extends BaseParams, Partial<Extract<CustomSpec, CustomObjectSpec>> {
|
|
7
|
+
layout?: string;
|
|
8
|
+
}
|
|
9
|
+
type GetPrimitiveViewSpecParams = BaseParams & Schema;
|
|
10
|
+
export declare const getOneOfViewSpec: ({ layoutTitle }: BaseParams) => {
|
|
11
|
+
type: string;
|
|
12
|
+
layout: string;
|
|
13
|
+
layoutTitle: string | undefined;
|
|
14
|
+
oneOfParams: {
|
|
15
|
+
toggler: "select";
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare const getObjectViewSpec: ({ properties, disabled, layoutTitle, layout, }: GetObjectViewSpecParams) => {
|
|
19
|
+
layoutTitle: string | undefined;
|
|
20
|
+
type: string;
|
|
21
|
+
layout: string;
|
|
22
|
+
order: string[] | undefined;
|
|
23
|
+
disabled: boolean | undefined;
|
|
24
|
+
};
|
|
25
|
+
export declare const getArrayViewSpec: ({ layoutTitle }: BaseParams) => {
|
|
26
|
+
layoutTitle: string | undefined;
|
|
27
|
+
type: string;
|
|
28
|
+
layout: string;
|
|
29
|
+
layoutOpen: boolean;
|
|
30
|
+
itemLabel: string;
|
|
31
|
+
};
|
|
32
|
+
export declare const getPrimitiveViewSpec: ({ layoutTitle, ...data }: GetPrimitiveViewSpecParams) => {
|
|
33
|
+
layout: string;
|
|
34
|
+
type: any;
|
|
35
|
+
layoutTitle: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPrimitiveViewSpec = exports.getArrayViewSpec = exports.getObjectViewSpec = exports.getOneOfViewSpec = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const getOneOfViewSpec = ({ layoutTitle }) => {
|
|
6
|
+
return {
|
|
7
|
+
type: 'oneof_custom',
|
|
8
|
+
layout: 'row',
|
|
9
|
+
layoutTitle,
|
|
10
|
+
oneOfParams: {
|
|
11
|
+
toggler: 'select',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.getOneOfViewSpec = getOneOfViewSpec;
|
|
16
|
+
const getObjectViewSpec = ({ properties, disabled, layoutTitle, layout = 'accordeon', }) => {
|
|
17
|
+
return {
|
|
18
|
+
layoutTitle,
|
|
19
|
+
type: 'base',
|
|
20
|
+
layout,
|
|
21
|
+
order: properties && Object.keys(properties).sort(),
|
|
22
|
+
disabled,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
exports.getObjectViewSpec = getObjectViewSpec;
|
|
26
|
+
const getArrayViewSpec = ({ layoutTitle }) => ({
|
|
27
|
+
layoutTitle,
|
|
28
|
+
type: 'base',
|
|
29
|
+
layout: 'accordeon',
|
|
30
|
+
layoutOpen: true,
|
|
31
|
+
itemLabel: 'Add Item',
|
|
32
|
+
});
|
|
33
|
+
exports.getArrayViewSpec = getArrayViewSpec;
|
|
34
|
+
const getPrimitiveViewSpec = (_a) => {
|
|
35
|
+
var { layoutTitle } = _a, data = tslib_1.__rest(_a, ["layoutTitle"]);
|
|
36
|
+
let type = (data === null || data === void 0 ? void 0 : data.inputType) || 'base';
|
|
37
|
+
if (data && 'enum' in data && data.enum) {
|
|
38
|
+
type = 'select';
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
layout: 'row',
|
|
42
|
+
type,
|
|
43
|
+
layoutTitle,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.getPrimitiveViewSpec = getPrimitiveViewSpec;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const schema_1 = require("../../schema");
|
|
6
|
+
const parser_1 = tslib_1.__importDefault(require("../dynamic-forms-custom/parser"));
|
|
7
|
+
function useFormSpec(customSchema) {
|
|
8
|
+
return (0, react_1.useMemo)(() => {
|
|
9
|
+
const schema = (0, schema_1.generateDefaultSchema)(customSchema);
|
|
10
|
+
return parser_1.default.parse(schema);
|
|
11
|
+
}, [customSchema]);
|
|
12
|
+
}
|
|
13
|
+
exports.default = useFormSpec;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Editor } from './
|
|
1
|
+
export { Editor } from './containers/Editor/Editor';
|
|
2
2
|
export * from './types';
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Editor = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
var Editor_1 = require("./
|
|
5
|
+
var Editor_1 = require("./containers/Editor/Editor");
|
|
6
6
|
Object.defineProperty(exports, "Editor", { enumerable: true, get: function () { return Editor_1.Editor; } });
|
|
7
7
|
tslib_1.__exportStar(require("./types"), exports);
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { Block } from '../../models';
|
|
2
|
-
import { EditorProps } from '../types';
|
|
1
|
+
import { Block, BlockDecorationProps, PageContent } from '../../models';
|
|
2
|
+
import { EditBlockProps, EditorProps } from '../types';
|
|
3
3
|
export type EditorBlockId = number | string;
|
|
4
4
|
export declare function useEditorState({ content: intialContent, custom }: Omit<EditorProps, 'children'>): {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
onDelete: (id: EditorBlockId) => void;
|
|
10
|
-
onSelect: (id: EditorBlockId) => void;
|
|
11
|
-
onCopy: (index: number) => void;
|
|
12
|
-
onOrderChange: (oldIndex: number, newIndex: number) => void;
|
|
13
|
-
};
|
|
5
|
+
activeBlockIndex: number;
|
|
6
|
+
content: PageContent;
|
|
7
|
+
errorBoundaryState: number;
|
|
8
|
+
injectEditBlockProps: ({ type, index: relativeIndex, children, }: BlockDecorationProps) => EditBlockProps;
|
|
14
9
|
onAdd: (block: Block) => void;
|
|
10
|
+
onSelect: (index: number) => void;
|
|
11
|
+
onContentUpdate: (newContent: PageContent) => void;
|
|
15
12
|
};
|
|
@@ -4,29 +4,75 @@ exports.useEditorState = void 0;
|
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const models_1 = require("../../models");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
7
|
+
const EditBlock_1 = require("../components/EditBlock/EditBlock");
|
|
7
8
|
const reducer_1 = require("./reducer");
|
|
8
9
|
const utils_2 = require("./utils");
|
|
9
10
|
function useEditorState({ content: intialContent, custom }) {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
activeBlockId: 0,
|
|
14
|
-
orderedBlocksCount: (0, utils_1.getOrderedBlocks)(intialContent.blocks, headerBlockTypes).length,
|
|
11
|
+
const [{ activeBlockIndex, content, errorBoundaryState }, dispatch] = (0, react_1.useReducer)(reducer_1.reducer, {
|
|
12
|
+
activeBlockIndex: 0,
|
|
13
|
+
errorBoundaryState: 0,
|
|
15
14
|
content: (0, utils_2.addEditorProps)(intialContent),
|
|
16
15
|
});
|
|
17
16
|
return (0, react_1.useMemo)(() => {
|
|
17
|
+
const headerBlockTypes = [...models_1.HeaderBlockTypes, ...(0, utils_1.getCustomHeaderTypes)(custom)];
|
|
18
|
+
const contentHasHeader = Boolean((0, utils_1.getHeaderBlock)(content.blocks, headerBlockTypes));
|
|
19
|
+
const checkIsHeader = (type) => headerBlockTypes.includes(type);
|
|
20
|
+
const onAdd = (block) => {
|
|
21
|
+
const isHeader = checkIsHeader(block.type);
|
|
22
|
+
if (contentHasHeader && isHeader) {
|
|
23
|
+
//TODO: add warning that it should be only one header block
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// eslint-disable-next-line no-nested-ternary
|
|
27
|
+
const index = isHeader
|
|
28
|
+
? 0
|
|
29
|
+
: activeBlockIndex === -1
|
|
30
|
+
? content.blocks.length
|
|
31
|
+
: activeBlockIndex + 1;
|
|
32
|
+
dispatch({ type: reducer_1.ADD_BLOCK, payload: { block, index } });
|
|
33
|
+
};
|
|
34
|
+
const onSelect = (index) => dispatch({ type: reducer_1.SELECT_BLOCK, payload: index });
|
|
35
|
+
const onContentUpdate = (newContent) => dispatch({ type: reducer_1.UPDATE_CONTENT, payload: newContent });
|
|
36
|
+
const injectEditBlockProps = ({ type, index: relativeIndex = 0, children, }) => {
|
|
37
|
+
const orderedBlocksStartIndex = contentHasHeader ? 1 : 0;
|
|
38
|
+
const isHeader = checkIsHeader(type);
|
|
39
|
+
const index = isHeader ? 0 : relativeIndex + orderedBlocksStartIndex;
|
|
40
|
+
const isActive = activeBlockIndex === index;
|
|
41
|
+
const actions = {
|
|
42
|
+
[EditBlock_1.EditBlockControls.Delete]: () => dispatch({ type: reducer_1.DELETE_BLOCK, payload: index }),
|
|
43
|
+
};
|
|
44
|
+
if (!isHeader) {
|
|
45
|
+
actions[EditBlock_1.EditBlockControls.Copy] = () => dispatch({ type: reducer_1.COPY_BLOCK, payload: index });
|
|
46
|
+
if (index > orderedBlocksStartIndex) {
|
|
47
|
+
actions[EditBlock_1.EditBlockControls.Up] = () => dispatch({
|
|
48
|
+
type: reducer_1.ORDER_BLOCK,
|
|
49
|
+
payload: { oldIndex: index, newIndex: index - 1 },
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (index < content.blocks.length - 1) {
|
|
53
|
+
actions[EditBlock_1.EditBlockControls.Down] = () => dispatch({
|
|
54
|
+
type: reducer_1.ORDER_BLOCK,
|
|
55
|
+
payload: { oldIndex: index, newIndex: index + 1 },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
children,
|
|
61
|
+
isHeader,
|
|
62
|
+
isActive,
|
|
63
|
+
actions,
|
|
64
|
+
onSelect: () => onSelect(index),
|
|
65
|
+
};
|
|
66
|
+
};
|
|
18
67
|
return {
|
|
68
|
+
activeBlockIndex,
|
|
19
69
|
content,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
onCopy: (index) => dispatch({ type: reducer_1.COPY_BLOCK, payload: index }),
|
|
26
|
-
onOrderChange: (oldIndex, newIndex) => dispatch({ type: reducer_1.ORDER_BLOCK, payload: { oldIndex, newIndex } }),
|
|
27
|
-
},
|
|
28
|
-
onAdd: (block) => dispatch({ type: reducer_1.ADD_BLOCK, payload: block }),
|
|
70
|
+
errorBoundaryState,
|
|
71
|
+
injectEditBlockProps,
|
|
72
|
+
onAdd,
|
|
73
|
+
onSelect,
|
|
74
|
+
onContentUpdate,
|
|
29
75
|
};
|
|
30
|
-
}, [content,
|
|
76
|
+
}, [content, activeBlockIndex, errorBoundaryState, custom]);
|
|
31
77
|
}
|
|
32
78
|
exports.useEditorState = useEditorState;
|