Files
dooring/packages/dooringx-lib/src/components/preview.tsx

166 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-07-09 01:41:03 +08:00
/*
* @Author: yehuozhili
* @Date: 2021-03-14 05:40:37
* @LastEditors: yehuozhili
2022-04-24 00:38:03 +08:00
* @LastEditTime: 2022-04-23 18:01:55
2021-08-19 16:53:57 +08:00
* @FilePath: \dooringx\packages\dooringx-lib\src\components\preview.tsx
2021-07-09 01:41:03 +08:00
*/
import Container from './container';
2022-01-21 14:29:53 +08:00
import React, { ReactElement, ReactNode, useEffect, useState, CSSProperties } from 'react';
2021-07-09 01:41:03 +08:00
import UserConfig from '../config';
2021-09-28 22:36:03 +08:00
import { ComponentItemFactory } from '..';
2021-07-09 01:41:03 +08:00
2021-08-19 16:53:57 +08:00
export interface PreviewProps {
config: UserConfig;
/**
*
* loading node
* @type {ReactNode}
* @memberof PreviewProps
*/
loadText?: ReactNode;
/**
*
*
* @type {boolean}
* @memberof PreviewProps
*/
loadingState?: boolean;
/**
*
*
* @type {Function}
* @memberof PreviewProps
*/
completeFn?: Function;
2022-01-26 18:04:46 +08:00
/**
2022-01-21 14:29:53 +08:00
*
*
* @type {CSSProperties}
* @memberof PreviewProps
*/
2022-01-26 18:04:46 +08:00
previewContainerStyle?: CSSProperties;
2021-08-19 16:53:57 +08:00
}
function Preview(props: PreviewProps): ReactElement {
2022-04-24 00:38:03 +08:00
const isEdit = props.config.getStore().isEdit();
2021-07-09 01:41:03 +08:00
/// 这里需要在渲染组件之前必须把所有config加载完成否则会导致先运行的函数无法运行
const [loading, setLoading] = useState(true);
useEffect(() => {
2021-09-28 22:36:03 +08:00
const finalFn = () => {
2022-02-15 18:02:32 +08:00
props.config.created();
props.config.createdFn.forEach((v) => v());
2021-09-28 22:36:03 +08:00
setTimeout(() => {
2021-10-10 00:57:02 +08:00
// 链接数据
2022-04-24 00:38:03 +08:00
props.config.getDataCenter().initAddToDataMap(props.config.getStore().getData());
2021-10-10 00:57:02 +08:00
// 链接事件
props.config
.getEventCenter()
2022-04-24 00:38:03 +08:00
.syncEventMap(props.config.getStore().getData(), props.config.getStore());
2021-10-10 00:57:02 +08:00
2021-09-28 22:36:03 +08:00
// 设置全局
2022-04-01 14:34:10 +08:00
const global = props.config.getStore().getData().globalState;
const bodyColor = global?.bodyColor;
2022-04-07 00:11:31 +08:00
const title = global?.title;
if (title) {
document.title = title;
}
2021-09-28 22:36:03 +08:00
if (bodyColor) {
document.body.style.backgroundColor = bodyColor;
}
2022-04-01 14:34:10 +08:00
const customAnimate = global?.customAnimate;
if (customAnimate && Array.isArray(customAnimate)) {
// 插入自定义动画
props.config.animateFactory.fromArrInsertKeyFrame(customAnimate);
}
2021-09-28 22:36:03 +08:00
if (props.completeFn) {
props.completeFn();
}
2022-02-15 18:02:32 +08:00
props.config.beforeOnMounted();
props.config.beforeOnMountedFn.forEach((v) => v());
2021-09-28 22:36:03 +08:00
if (props.loadingState === undefined) {
setLoading(false);
}
});
};
// 加载 script
const scripts = props.config.getStore().getData().globalState['script'];
if (scripts && Array.isArray(scripts) && scripts.length > 0) {
2021-11-02 12:48:08 +08:00
const fn = async () => {
const allp = scripts.map((v) => {
return () =>
new Promise<number>((res) => {
const s = document.createElement('script');
s.src = v;
document.body.appendChild(s);
s.onload = () => {
const item = window[
props.config.SCRIPTGLOBALNAME as any
] as unknown as ComponentItemFactory;
try {
props.config.registComponent(item);
} catch {
console.error('read item fail:', v);
}
document.body.removeChild(s);
res(0);
};
});
2021-09-28 22:36:03 +08:00
});
2021-11-02 12:48:08 +08:00
const allplen = allp.length;
for (let i = 0; i < allplen; i++) {
try {
await allp[i]();
} catch {
console.error('script load fail:', scripts[i]);
}
}
finalFn();
};
fn();
2021-10-10 00:57:02 +08:00
} else {
finalFn();
2021-09-28 22:36:03 +08:00
}
2021-08-19 16:53:57 +08:00
}, [props, props.config]);
2021-07-09 01:41:03 +08:00
if (isEdit) {
// 正常情况不会走这
2022-04-24 00:38:03 +08:00
const store = props.config.getStore();
const data = store.getData();
store.changeModaltoNormal(data);
2021-07-09 01:41:03 +08:00
return (
<>
2022-04-24 00:38:03 +08:00
<Container config={props.config} context="preview" state={store.getData()}></Container>
2021-07-09 01:41:03 +08:00
</>
);
} else {
2021-08-19 16:53:57 +08:00
const loadingNode = <div>{props.loadText ? props.loadText : 'loading'}</div>;
const container = (
<>
<Container
config={props.config}
context="preview"
state={props.config.getStore().getData()}
2022-01-26 18:04:46 +08:00
previewContainerStyle={props.previewContainerStyle}
2021-08-19 16:53:57 +08:00
></Container>
</>
);
if (props.loadingState === undefined) {
if (loading) {
return loadingNode;
} else {
return container;
}
2021-07-09 01:41:03 +08:00
} else {
2021-08-19 16:53:57 +08:00
if (props.loadingState) {
return loadingNode;
} else {
return container;
}
2021-07-09 01:41:03 +08:00
}
}
}
export default Preview;