update 0.9.5

This commit is contained in:
hufeixiong
2021-11-02 12:48:08 +08:00
parent ab67e095b4
commit b854f9f275
9 changed files with 139 additions and 42 deletions

View File

@@ -1,5 +1,11 @@
## changelog
## 0.9.5
preview组件scripts加载由并发变为线性。
编辑模式下使用远程组件会储存当前画布状态。
## 0.9.4
修复preview组件不能解除loading的bug

View File

@@ -4,6 +4,12 @@ order: 1
---
## changelog
## 0.9.5
preview组件scripts加载由并发变为线性。
编辑模式下使用远程组件会储存当前画布状态。
## 0.9.4
修复preview组件不能解除loading的bug

View File

@@ -14,12 +14,12 @@ import {
ContainerWrapper,
Control,
} from 'dooringx-lib';
import { InsertRowBelowOutlined } from '@ant-design/icons';
import { InsertRowBelowOutlined, UploadOutlined } from '@ant-design/icons';
import { useContext, useState } from 'react';
import { configContext, LocaleContext } from '@/layouts';
import { useCallback } from 'react';
import { PREVIEWSTATE } from '@/constant';
import { Button, Input, Popover } from 'antd';
import { Button, Input, message, Modal, Popover, Upload } from 'antd';
import { localeKey } from '../../../dooringx-lib/dist/locale';
import { LeftRegistComponentMapItem } from 'dooringx-lib/dist/core/crossDrag';
@@ -48,6 +48,19 @@ export default function IndexPage() {
const [state] = useStoreState(config, subscribeFn, everyFn);
const [value, setValue] = useState('');
const [open, setOpen] = useState(false);
const createAndDownloadFile = (fileName: string) => {
const aTag = document.createElement('a');
const res = config.getStore().getData();
const JSONres = JSON.stringify(res);
const blob = new Blob([JSONres]);
aTag.download = fileName;
const url = URL.createObjectURL(blob);
aTag.href = url;
aTag.click();
URL.revokeObjectURL(url);
};
return (
<div {...innerContainerDragUp(config)}>
@@ -91,7 +104,54 @@ export default function IndexPage() {
>
</Button>
<Button
onClick={() => {
createAndDownloadFile('dooring.json');
}}
>
json
</Button>
<Button
onClick={() => {
setOpen(true);
}}
>
json
</Button>
</div>
<Modal
visible={open}
onCancel={() => setOpen(false)}
onOk={() => setOpen(false)}
title={'import json'}
>
<Upload
name="file"
maxCount={1}
onChange={(e) => {
if (e.file.status === 'done') {
const file = e.file.originFileObj;
if (file) {
let reader = new FileReader();
reader.addEventListener('loadend', function () {
try {
let res = JSON.parse(reader.result as string);
console.log(res, '返回结果数据');
config.getStore().resetToInitData([res]);
setOpen(false);
} catch {
message.error('json解析格式有误');
}
});
reader.readAsText(file);
}
}
}}
>
<Button icon={<UploadOutlined />}>&nbsp; </Button>
</Upload>
</Modal>
<div
style={{
display: 'flex',

View File

@@ -1,5 +1,5 @@
{
"version": "0.9.4",
"version": "0.9.5",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/dooringx-lib.esm.js",

View File

@@ -68,8 +68,10 @@ function Preview(props: PreviewProps): ReactElement {
// 加载 script
const scripts = props.config.getStore().getData().globalState['script'];
if (scripts && Array.isArray(scripts) && scripts.length > 0) {
const fn = async () => {
const allp = scripts.map((v) => {
return new Promise((res) => {
return () =>
new Promise<number>((res) => {
const s = document.createElement('script');
s.src = v;
document.body.appendChild(s);
@@ -77,18 +79,27 @@ function Preview(props: PreviewProps): ReactElement {
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);
};
});
});
Promise.all(allp)
.then(() => {
const allplen = allp.length;
for (let i = 0; i < allplen; i++) {
try {
await allp[i]();
} catch {
console.error('script load fail:', scripts[i]);
}
}
finalFn();
})
.catch(() => {
finalFn();
});
};
fn();
} else {
finalFn();
}

View File

@@ -592,29 +592,41 @@ export class UserConfig {
this.addCoRegistMap(leftProps);
}
scriptRegistComponentMulti(
items: ComponentItemFactory[],
leftProps: LeftRegistComponentMapItem[]
) {
items.forEach((item) => {
this.registComponent(item);
});
const obj = {} as InitConfig;
obj.leftAllRegistMap = leftProps;
this.initConfig = userConfigMerge(this.initConfig, obj);
this.init();
this.store.forceUpdate();
}
// foreach可能导致问题
// scriptRegistComponentMulti(
// items: ComponentItemFactory[],
// leftProps: LeftRegistComponentMapItem[]
// ) {
// items.forEach((item) => {
// this.registComponent(item);
// });
// const obj = {} as InitConfig;
// obj.leftAllRegistMap = leftProps;
// this.initConfig = userConfigMerge(this.initConfig, obj);
// this.init();
// this.store.forceUpdate();
// }
/**
*
* 分类信息需要单独存储后加载
* @param {string} src url地址
* @param {Partial<LeftRegistComponentMapItem>} leftProps 分类
* @param {Function} [callback] 回调
* @return {*}
* @memberof UserConfig
*/
scriptSingleLoad(
src: string,
leftProps: Partial<LeftRegistComponentMapItem>,
callback?: Function
) {
let isEdit = this.storeChanger.isEdit();
let globalState = this.store.getData().globalState;
let storeData = this.store.getData();
let globalState = storeData.globalState;
if (isEdit) {
globalState = this.storeChanger.getOrigin()!.now.globalState;
storeData = this.storeChanger.getOrigin()!.now;
globalState = storeData.globalState;
}
if (globalState['script'].includes(src)) {
console.error(src + 'scripts have been loaded');
@@ -643,6 +655,8 @@ export class UserConfig {
globalState = this.storeChanger.getOrigin()!.now.globalState;
}
globalState['script'].push(src);
storeData.globalState = globalState;
this.store.resetToInitData([storeData], true);
this.store.forceUpdate();
this.scriptLoading = false;
if (callback) {

View File

@@ -45,9 +45,9 @@ class Store {
}
/**
*
* 注意重置需要注册事件
* 重置需要注册事件
* @param {IStoreData[]} initData
* @param {boolean} [check=false]
* @param {boolean} [check=false] 检查编辑弹窗状态
* @memberof Store
*/
resetToInitData(initData: IStoreData[], check = false) {

View File

@@ -1,6 +1,6 @@
{
"name": "dooringx-plugin-template",
"version": "0.9.4",
"version": "0.9.5",
"description": "> TODO: description",
"author": "yehuozhili <673632758@qq.com>",
"homepage": "https://github.com/H5-Dooring/dooringx#readme",

View File

@@ -40,7 +40,7 @@
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-url": "^6.1.0",
"@svgr/rollup": "^5.5.0",
"dooringx-lib": "^0.9.4",
"dooringx-lib": "^0.9.5",
"postcss": "^8.3.6",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.1",