feat: enhance focusSelector logic

This commit is contained in:
kangwei 2020-12-23 23:00:38 +08:00
parent db2b6d5218
commit 8ae23d014f
3 changed files with 15 additions and 29 deletions

View File

@ -36,7 +36,6 @@ export interface DesignerProps {
suspensed?: boolean; suspensed?: boolean;
componentMetadatas?: ComponentMetadata[]; componentMetadatas?: ComponentMetadata[];
globalComponentActions?: ComponentAction[]; globalComponentActions?: ComponentAction[];
focusNodeSelector?: (rootNode: Node) => Node;
onMount?: (designer: Designer) => void; onMount?: (designer: Designer) => void;
onDragstart?: (e: LocateEvent) => void; onDragstart?: (e: LocateEvent) => void;
onDrag?: (e: LocateEvent) => void; onDrag?: (e: LocateEvent) => void;

View File

@ -98,9 +98,9 @@ export class DocumentModel {
if (this._drillDownNode) { if (this._drillDownNode) {
return this._drillDownNode; return this._drillDownNode;
} }
const selector = this.designer.get('focusNodeSelector'); const selector = this.designer.editor.get<((rootNode: RootNode) => Node) | null>('focusNodeSelector');
if (typeof selector === 'function') { if (selector && typeof selector === 'function') {
return selector(this.rootNode); return selector(this.rootNode!);
} }
return this.rootNode; return this.rootNode;
} }

View File

@ -4,26 +4,21 @@ import {
EditorConfig, EditorConfig,
PluginClassSet, PluginClassSet,
KeyType, KeyType,
GetOptions,
GetReturnType, GetReturnType,
HookConfig, HookConfig
} from '@ali/lowcode-types'; } from '@ali/lowcode-types';
import { IocContext, RegisterOptions } from './di';
import { globalLocale } from './intl'; import { globalLocale } from './intl';
import * as utils from './utils'; import * as utils from './utils';
import { obx } from './utils';
// import { tipHandler } from './widgets/tip/tip-handler'; // import { tipHandler } from './widgets/tip/tip-handler';
EventEmitter.defaultMaxListeners = 100; EventEmitter.defaultMaxListeners = 100;
const NOT_FOUND = Symbol.for('not_found');
export class Editor extends EventEmitter implements IEditor { export class Editor extends EventEmitter implements IEditor {
/** /**
* Ioc Container * Ioc Container
*/ */
private context = new IocContext({ @obx.val private context = new Map<KeyType, any>();
notFoundHandler: (/* type: KeyType */) => NOT_FOUND,
});
get locale() { get locale() {
return globalLocale.getLocale(); return globalLocale.getLocale();
@ -33,12 +28,8 @@ export class Editor extends EventEmitter implements IEditor {
private hooks: HookConfig[] = []; private hooks: HookConfig[] = [];
get<T = undefined, KeyOrType = any>(keyOrType: KeyOrType, opt?: GetOptions): GetReturnType<T, KeyOrType> | undefined { get<T = undefined, KeyOrType = any>(keyOrType: KeyOrType): GetReturnType<T, KeyOrType> | undefined {
const x = this.context.get<T, KeyOrType>(keyOrType, opt); return this.context.get(keyOrType as any);
if (x === NOT_FOUND) {
return undefined;
}
return x;
} }
has(keyOrType: KeyType): boolean { has(keyOrType: KeyType): boolean {
@ -46,17 +37,13 @@ export class Editor extends EventEmitter implements IEditor {
} }
set(key: KeyType, data: any): void { set(key: KeyType, data: any): void {
if (this.context.has(key)) { this.context.set(key, data);
this.context.replace(key, data, undefined, true);
} else {
this.context.register(data, key);
}
this.notifyGot(key); this.notifyGot(key);
} }
onceGot<T = undefined, KeyOrType extends KeyType = any>(keyOrType: KeyOrType): Promise<GetReturnType<T, KeyOrType>> { onceGot<T = undefined, KeyOrType extends KeyType = any>(keyOrType: KeyOrType): Promise<GetReturnType<T, KeyOrType>> {
const x = this.context.get<T, KeyOrType>(keyOrType); const x = this.context.get(keyOrType);
if (x !== NOT_FOUND) { if (x !== undefined) {
return Promise.resolve(x); return Promise.resolve(x);
} }
return new Promise((resolve) => { return new Promise((resolve) => {
@ -68,8 +55,8 @@ export class Editor extends EventEmitter implements IEditor {
keyOrType: KeyOrType, keyOrType: KeyOrType,
fn: (data: GetReturnType<T, KeyOrType>) => void, fn: (data: GetReturnType<T, KeyOrType>) => void,
): () => void { ): () => void {
const x = this.context.get<T, KeyOrType>(keyOrType); const x = this.context.get(keyOrType);
if (x !== NOT_FOUND) { if (x !== undefined) {
fn(x); fn(x);
return () => {}; return () => {};
} else { } else {
@ -80,8 +67,8 @@ export class Editor extends EventEmitter implements IEditor {
} }
} }
register(data: any, key?: KeyType, options?: RegisterOptions): void { register(data: any, key?: KeyType): void {
this.context.register(data, key, options); this.context.set(key || data, data);
this.notifyGot(key || data); this.notifyGot(key || data);
} }