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,5 @@
{
"version": "0.9.4",
"version": "0.9.5",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/dooringx-lib.esm.js",

View File

@@ -68,27 +68,38 @@ function Preview(props: PreviewProps): ReactElement {
// 加载 script
const scripts = props.config.getStore().getData().globalState['script'];
if (scripts && Array.isArray(scripts) && scripts.length > 0) {
const allp = scripts.map((v) => {
return new Promise((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;
props.config.registComponent(item);
res(0);
};
});
});
Promise.all(allp)
.then(() => {
finalFn();
})
.catch(() => {
finalFn();
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);
};
});
});
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();
} 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) {