@ovine/core
Version:
Build flexible admin system with json.
53 lines (52 loc) • 2.3 kB
JavaScript
import { isEmpty, cloneDeep, isObject } from 'lodash';
import React, { useMemo, useEffect } from 'react';
import { ThemeConsumer } from 'styled-components';
import { app } from "../../../app";
import { useAppContext } from "../../app/context";
import { storage } from "../../../constants";
import { getGlobal, setGlobal } from "../../../utils/store";
import AmisComponent from "./amis";
import { resolveLibSchema, wrapCss } from "./func";
// 源码 https://github.com/baidu/amis/blob/master/examples/components/App.jsx
export const Amis = (props) => {
const { schema: rawSchema, props: amisProps = {}, option = {} } = props;
const codeStore = getGlobal(storage.dev.code) || {};
const { enableRouteTabs, locale } = useAppContext();
// 改变固定的高度
// @ts-ignore
if (!amisProps.affixOffsetTop && getGlobal(storage.supportRouteTabs)) {
// @ts-ignore
amisProps.affixOffsetTop = enableRouteTabs ? 100 : 50;
}
useEffect(() => {
return () => {
if (codeStore.enable) {
setGlobal(storage.dev.code, {
enable: true,
schema: false,
});
}
};
}, []);
const envSchema = useMemo(() => {
if (!rawSchema || !isObject(rawSchema) || isEmpty(rawSchema)) {
return {
type: 'html',
html: '请传入有效的 schema',
};
}
// Avoid modify rawSchema of props
const schema = cloneDeep(Object.assign(Object.assign({}, rawSchema), {
// Merge amis global definitions
constants: app.amis.constants, definitions: Object.assign(Object.assign({}, app.amis.definitions), rawSchema.definitions) }));
const libSchema = resolveLibSchema(wrapCss(schema));
if (codeStore.enable && schema.type === 'page') {
setGlobal(storage.dev.code, {
enable: true,
schema: libSchema,
});
}
return libSchema;
}, [rawSchema]);
return (React.createElement(ThemeConsumer, null, (theme) => (React.createElement(AmisComponent, { key: theme.name, option: Object.assign(Object.assign({}, option), { theme: theme.name, locale }), schema: envSchema, props: amisProps }))));
};