2021-07-09 01:41:03 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Author: yehuozhili
|
|
|
|
|
|
* @Date: 2021-03-14 05:40:37
|
|
|
|
|
|
* @LastEditors: yehuozhili
|
2021-08-19 16:53:57 +08:00
|
|
|
|
* @LastEditTime: 2021-08-19 16:46:56
|
|
|
|
|
|
* @FilePath: \dooringx\packages\dooringx-lib\src\components\preview.tsx
|
2021-07-09 01:41:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
import Container from './container';
|
|
|
|
|
|
import React, { ReactElement, ReactNode, useEffect, useState } from 'react';
|
|
|
|
|
|
import UserConfig from '../config';
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Preview(props: PreviewProps): ReactElement {
|
2021-07-09 01:41:03 +08:00
|
|
|
|
const isEdit = props.config.getStoreChanger().isEdit();
|
|
|
|
|
|
/// 这里需要在渲染组件之前必须把所有config加载完成,否则会导致先运行的函数无法运行
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// 链接数据
|
|
|
|
|
|
props.config
|
|
|
|
|
|
.getDataCenter()
|
|
|
|
|
|
.initAddToDataMap(props.config.getStore().getData(), props.config.getStoreChanger());
|
|
|
|
|
|
// 链接事件
|
|
|
|
|
|
props.config
|
|
|
|
|
|
.getEventCenter()
|
|
|
|
|
|
.syncEventMap(props.config.getStore().getData(), props.config.getStoreChanger());
|
|
|
|
|
|
setTimeout(() => {
|
2021-07-10 19:35:06 +08:00
|
|
|
|
// 设置全局
|
|
|
|
|
|
const bodyColor = props.config.getStore().getData().globalState?.bodyColor;
|
|
|
|
|
|
if (bodyColor) {
|
|
|
|
|
|
document.body.style.backgroundColor = bodyColor;
|
|
|
|
|
|
}
|
2021-08-19 16:53:57 +08:00
|
|
|
|
if (props.completeFn) {
|
|
|
|
|
|
props.completeFn();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (props.loadingState === undefined) {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
2021-07-09 01:41:03 +08:00
|
|
|
|
});
|
2021-08-19 16:53:57 +08:00
|
|
|
|
}, [props, props.config]);
|
2021-07-09 01:41:03 +08:00
|
|
|
|
|
|
|
|
|
|
if (isEdit) {
|
|
|
|
|
|
// 正常情况不会走这
|
|
|
|
|
|
const state = props.config.getStoreChanger().getOrigin()!.now;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Container config={props.config} context="preview" state={state}></Container>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
} 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()}
|
|
|
|
|
|
></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;
|