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; 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

@ -97,9 +97,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

@ -1004,15 +1004,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
if (dropElement) { if (dropElement) {
return { container: 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; return null;
} }

View File

@ -4,25 +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();
@ -32,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 {
@ -45,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) => {
@ -67,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 {
@ -79,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);
} }