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

View File

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

View File

@ -1,7 +1,6 @@
/* eslint-disable max-len */
import { isFormEvent } from '@alilc/lowcode-utils';
import { isFormEvent, isNodeSchema } from '@alilc/lowcode-utils';
import {
focusing,
insertChildren,
clipboard,
} from '@alilc/lowcode-designer';
@ -9,11 +8,60 @@ import {
IPublicModelPluginContext,
IPublicEnumTransformStage,
IPublicModelNode,
IPublicTypeNodeSchema,
} from '@alilc/lowcode-types';
import symbols from '../modules/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 */
function getNextForSelect(next: IPublicModelNode | null, head?: any, parent?: IPublicModelNode | null): any {
if (next) {
@ -108,11 +156,11 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
hotkey.bind('escape', (e: KeyboardEvent, action) => {
logger.info(`action ${action} is triggered`);
// const currentFocus = focusing.current;
if (canvas.isInLiveEditing) {
return;
}
const sel = focusing.focusDesigner?.currentDocument?.selection;
const sel = project.currentDocument?.selection;
if (isFormEvent(e) || !sel) {
return;
}
@ -168,15 +216,14 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
return;
}
// TODO
const designer = focusing.focusDesigner;
const doc = project?.currentDocument;
if (isFormEvent(e) || !designer || !doc) {
if (isFormEvent(e) || !doc) {
return;
}
/* istanbul ignore next */
clipboard.waitPasteData(e, ({ componentsTree }) => {
if (componentsTree) {
const { target, index } = designer.getSuitableInsertion(componentsTree) || {};
const { target, index } = getSuitableInsertion(ctx, componentsTree) || {};
if (!target) {
return;
}
@ -187,7 +234,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
const nodes = insertChildren(target, canAddComponentsTree, index);
if (nodes) {
doc.selection.selectAll(nodes.map((o) => o.id));
setTimeout(() => designer.activeTracker.track(nodes[0]), 10);
setTimeout(() => canvas.activeTracker?.track(nodes[0]), 10);
}
}
});