feat: enhance focusSelector logic by kangwei to #35417409

This commit is contained in:
alex.mm 2021-07-15 15:14:07 +08:00
parent a39010ae73
commit 81a2814f57
4 changed files with 16 additions and 37 deletions

View File

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

View File

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

View File

@ -1004,15 +1004,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
if (dropElement) {
return { container: dropElement };
}
const rootCanDropIn = this.componentMeta?.prototype?.options?.canDropIn;
if (
rootCanDropIn === undefined ||
rootCanDropIn === true ||
(typeof rootCanDropIn === 'function' && rootCanDropIn(node))
) {
return { container: this, ref };
}
// 假如最后找不到合适位置,返回 null 阻止继续插入节点
return null;
}

View File

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