diff --git a/packages/editor-core/src/config.ts b/packages/editor-core/src/config.ts index a766252f5..d5da2308d 100644 --- a/packages/editor-core/src/config.ts +++ b/packages/editor-core/src/config.ts @@ -232,19 +232,39 @@ export class EngineConfig { this.config = config || {}; } + /** + * 判断指定 key 是否有值 + * @param key + * @returns + */ has(key: string): boolean { return this.config[key] !== undefined; } + /** + * 获取指定 key 的值 + * @param key + * @param defaultValue + * @returns + */ get(key: string, defaultValue?: any): any { return lodashGet(this.config, key, defaultValue); } + /** + * 设置指定 key 的值 + * @param key + * @param value + */ set(key: string, value: any) { this.config[key] = value; this.notifyGot(key); } + /** + * 批量设值,set 的对象版本 + * @param config + */ setConfig(config: { [key: string]: any }) { if (config) { Object.keys(config).forEach((key) => { @@ -281,6 +301,12 @@ export class EngineConfig { } } + /** + * 获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值 + * 注:此函数返回 Promise 实例,只会执行(fullfill)一次 + * @param key + * @returns + */ onceGot(key: string): Promise { const val = this.config[key]; if (val !== undefined) { @@ -291,6 +317,12 @@ export class EngineConfig { }); } + /** + * 获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用 + * @param key + * @param fn + * @returns + */ onGot(key: string, fn: (data: any) => void): () => void { const val = this.config?.[key]; if (val !== undefined) { diff --git a/packages/shell/src/node.ts b/packages/shell/src/node.ts index bb270b9c6..27b8a62fa 100644 --- a/packages/shell/src/node.ts +++ b/packages/shell/src/node.ts @@ -348,7 +348,7 @@ export default class Node { * @param useMutator */ insertBefore(node: Node, ref?: Node | undefined, useMutator?: boolean) { - this[nodeSymbol].insertBefore(node[nodeSymbol], ref?.[nodeSymbol], useMutator); + this[nodeSymbol].insertBefore(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator); } /** @@ -358,7 +358,7 @@ export default class Node { * @param useMutator */ insertAfter(node: Node, ref?: Node | undefined, useMutator?: boolean) { - this[nodeSymbol].insertAfter(node[nodeSymbol], ref?.[nodeSymbol], useMutator); + this[nodeSymbol].insertAfter(node[nodeSymbol] || node, ref?.[nodeSymbol], useMutator); } /**