refactor: remove dependecy of designer.focusing for refactoring of hotkey plugin as a standalone one

This commit is contained in:
JackLian 2023-01-16 10:37:56 +08:00 committed by 刘菊萍(絮黎)
parent 8c7f57a120
commit 0b0015c368
5 changed files with 63 additions and 28 deletions

View File

@ -30,7 +30,6 @@ import { ActiveTracker, IActiveTracker } from './active-tracker';
import { Detecting } from './detecting'; import { Detecting } from './detecting';
import { DropLocation } from './location'; import { DropLocation } from './location';
import { OffsetObserver, createOffsetObserver } from './offset-observer'; import { OffsetObserver, createOffsetObserver } from './offset-observer';
import { focusing } from './focusing';
import { SettingTopEntry } from './setting'; import { SettingTopEntry } from './setting';
import { BemToolsManager } from '../builtin-simulator/bem-tools/manager'; import { BemToolsManager } from '../builtin-simulator/bem-tools/manager';
import { ComponentActions } from '../component-actions'; import { ComponentActions } from '../component-actions';
@ -241,9 +240,6 @@ export class Designer implements IDesigner {
this.postEvent('init', this); this.postEvent('init', this);
this.setupSelection(); this.setupSelection();
setupHistory(); setupHistory();
// TODO: 先简单实现,后期通过焦点赋值
focusing.focusDesigner = this;
} }
setupSelection = () => { setupSelection = () => {
@ -341,6 +337,7 @@ export class Designer implements IDesigner {
/** /**
* *
* @deprecated
*/ */
getSuitableInsertion( getSuitableInsertion(
insertNode?: INode | IPublicTypeNodeSchema | IPublicTypeNodeSchema[], insertNode?: INode | IPublicTypeNodeSchema | IPublicTypeNodeSchema[],

View File

@ -1,8 +0,0 @@
import { Designer } from './designer';
// TODO: use focus-tracker replace
class Focusing {
focusDesigner?: Designer;
}
export const focusing = new Focusing();

View File

@ -7,6 +7,5 @@ export * from './offset-observer';
export * from './scroller'; export * from './scroller';
export * from './setting'; export * from './setting';
export * from './active-tracker'; export * from './active-tracker';
export * from './focusing';
export * from '../document'; export * from '../document';
export * from './clipboard'; export * from './clipboard';

View File

@ -1396,11 +1396,11 @@ export function comparePosition(node1: Node, node2: Node): PositionNO {
export function insertChild( export function insertChild(
container: INode, container: INode,
thing: Node | IPublicTypeNodeData, thing: INode | IPublicTypeNodeData,
at?: number | null, at?: number | null,
copy?: boolean, copy?: boolean,
): Node { ): INode {
let node: Node; let node: INode;
if (isNode(thing) && (copy || thing.isSlot())) { if (isNode(thing) && (copy || thing.isSlot())) {
thing = thing.export(IPublicEnumTransformStage.Clone); thing = thing.export(IPublicEnumTransformStage.Clone);
} }
@ -1410,20 +1410,20 @@ export function insertChild(
node = container.document.createNode(thing); node = container.document.createNode(thing);
} }
container.children.internalInsert(node, at); container.children.insert(node, at);
return node; return node;
} }
export function insertChildren( export function insertChildren(
container: INode, container: INode,
nodes: Node[] | IPublicTypeNodeData[], nodes: INode[] | IPublicTypeNodeData[],
at?: number | null, at?: number | null,
copy?: boolean, copy?: boolean,
): Node[] { ): INode[] {
let index = at; let index = at;
let node: any; let node: any;
const results: Node[] = []; const results: INode[] = [];
// eslint-disable-next-line no-cond-assign // eslint-disable-next-line no-cond-assign
while ((node = nodes.pop())) { while ((node = nodes.pop())) {
node = insertChild(container, node, index, copy); node = insertChild(container, node, index, copy);

View File

@ -1,7 +1,6 @@
/* eslint-disable max-len */ /* eslint-disable max-len */
import { isFormEvent } from '@alilc/lowcode-utils'; import { isFormEvent, isNodeSchema } from '@alilc/lowcode-utils';
import { import {
focusing,
insertChildren, insertChildren,
clipboard, clipboard,
} from '@alilc/lowcode-designer'; } from '@alilc/lowcode-designer';
@ -9,11 +8,60 @@ import {
IPublicModelPluginContext, IPublicModelPluginContext,
IPublicEnumTransformStage, IPublicEnumTransformStage,
IPublicModelNode, IPublicModelNode,
IPublicTypeNodeSchema,
} from '@alilc/lowcode-types'; } from '@alilc/lowcode-types';
import symbols from '../modules/symbols'; import symbols from '../modules/symbols';
const { nodeSymbol, documentSymbol } = symbols; const { nodeSymbol, documentSymbol } = symbols;
/**
*
*/
function getSuitableInsertion(
pluginContext: IPublicModelPluginContext,
insertNode?: IPublicModelNode | IPublicTypeNodeSchema | IPublicTypeNodeSchema[],
): { target: IPublicModelNode; index?: number } | null {
const { project, material } = pluginContext;
const activeDoc = project.currentDocument;
if (!activeDoc) {
return null;
}
if (
Array.isArray(insertNode) &&
isNodeSchema(insertNode[0]) &&
material.getComponentMeta(insertNode[0].componentName)?.isModal
) {
if (!activeDoc.root) {
return null;
}
return {
target: activeDoc.root,
};
}
const focusNode = activeDoc.focusNode!;
const nodes = activeDoc.selection.getNodes();
const refNode = nodes.find((item) => focusNode.contains(item));
let target;
let index: number | undefined;
if (!refNode || refNode === focusNode) {
target = focusNode;
} else if (refNode.componentMeta?.isContainer) {
target = refNode;
} else {
// FIXME!!, parent maybe null
target = refNode.parent!;
index = refNode.index + 1;
}
if (target && insertNode && !target.componentMeta?.checkNestingDown(target, insertNode)) {
return null;
}
return { target, index };
}
/* istanbul ignore next */ /* istanbul ignore next */
function getNextForSelect(next: IPublicModelNode | null, head?: any, parent?: IPublicModelNode | null): any { function getNextForSelect(next: IPublicModelNode | null, head?: any, parent?: IPublicModelNode | null): any {
if (next) { if (next) {
@ -108,11 +156,11 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
hotkey.bind('escape', (e: KeyboardEvent, action) => { hotkey.bind('escape', (e: KeyboardEvent, action) => {
logger.info(`action ${action} is triggered`); logger.info(`action ${action} is triggered`);
// const currentFocus = focusing.current;
if (canvas.isInLiveEditing) { if (canvas.isInLiveEditing) {
return; return;
} }
const sel = focusing.focusDesigner?.currentDocument?.selection; const sel = project.currentDocument?.selection;
if (isFormEvent(e) || !sel) { if (isFormEvent(e) || !sel) {
return; return;
} }
@ -168,15 +216,14 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
return; return;
} }
// TODO // TODO
const designer = focusing.focusDesigner;
const doc = project?.currentDocument; const doc = project?.currentDocument;
if (isFormEvent(e) || !designer || !doc) { if (isFormEvent(e) || !doc) {
return; return;
} }
/* istanbul ignore next */ /* istanbul ignore next */
clipboard.waitPasteData(e, ({ componentsTree }) => { clipboard.waitPasteData(e, ({ componentsTree }) => {
if (componentsTree) { if (componentsTree) {
const { target, index } = designer.getSuitableInsertion(componentsTree) || {}; const { target, index } = getSuitableInsertion(ctx, componentsTree) || {};
if (!target) { if (!target) {
return; return;
} }
@ -187,7 +234,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
const nodes = insertChildren(target, canAddComponentsTree, index); const nodes = insertChildren(target, canAddComponentsTree, index);
if (nodes) { if (nodes) {
doc.selection.selectAll(nodes.map((o) => o.id)); doc.selection.selectAll(nodes.map((o) => o.id));
setTimeout(() => designer.activeTracker.track(nodes[0]), 10); setTimeout(() => canvas.activeTracker?.track(nodes[0]), 10);
} }
} }
}); });