Skip to content

Commit

Permalink
refactor: add isInLiveEditing api to canvas for refactoring of hotkey…
Browse files Browse the repository at this point in the history
… plugin as a standalone one
  • Loading branch information
JackLian authored and liujuping committed Jan 13, 2023
1 parent 4fd3af1 commit f25babe
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 59 deletions.
37 changes: 16 additions & 21 deletions docs/docs/api/canvas.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: cavas - 画布 API
title: canvas - 画布 API
sidebar_position: 12
---

Expand All @@ -17,30 +17,25 @@ sidebar_position: 12

获取拖拽操作对象的实例

```typescript
/**
* 获取拖拽操作对象的实例
* get dragon instance, you can use this to obtain draging related abilities and lifecycle hooks
* @since v1.1.0
*/
get dragon(): IPublicModelDragon | null;
```
关联模型 [IPublicModelDragon](./model/dragon)
`@type {IPublicModelDragon | null}`


相关类型:[IPublicModelDragon](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/dragon.ts)

### activeTracker

获取活动追踪器实例

```typescript
/**
* 获取活动追踪器实例
* get activeTracker instance, which is a singleton running in engine.
* it tracks document`s current focusing node/node[], and notify it`s subscribers that when
* focusing node/node[] changed.
* @since v1.1.0
*/
get activeTracker(): IPublicModelActiveTracker | null;
```
`@type {IPublicModelActiveTracker | null}`

相关类型:[IPublicModelActiveTracker](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/active-tracker.ts)

### isInLiveEditing

是否处于 LiveEditing 状态

`@type {boolean}`


## 方法

Expand Down Expand Up @@ -83,4 +78,4 @@ createScroller(scrollable: IPublicModelScrollable): IPublicModelScroller;
* @since v1.1.0
*/
createScrollTarget(shell: HTMLDivElement): IPublicModelScrollTarget;
```
```
11 changes: 3 additions & 8 deletions packages/designer/tests/designer/builtin-hotkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@ import {
import { Designer } from '../../src/designer/designer';
import formSchema from '../fixtures/schema/form';
import { fireEvent } from '@testing-library/react';
import { isInLiveEditing, builtinHotkey } from '../../../engine/src/inner-plugins/builtin-hotkey';
import { builtinHotkey } from '../../../engine/src/inner-plugins/builtin-hotkey';
import { shellModelFactory } from '../../../engine/src/modules/shell-model-factory';
import { ILowCodePluginContextPrivate, LowCodePluginManager } from '@alilc/lowcode-designer';
import { IPublicApiPlugins } from '@alilc/lowcode-types';
import { Logger, Project } from '@alilc/lowcode-shell';
import { Logger, Project, Canvas } from '@alilc/lowcode-shell';
import { Workspace } from '@alilc/lowcode-workspace';

const editor = new Editor();
const workspace = new Workspace();

let designer: Designer;

describe('error scenarios', () => {
it('edtior not registered', () => {
expect(isInLiveEditing()).toBeUndefined();
});
});

// keyCode 对应表:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
// hotkey 模块底层用的 keyCode,所以还不能用 key / code 测试
describe('快捷键测试', () => {
Expand All @@ -40,6 +34,7 @@ describe('快捷键测试', () => {
context.hotkey = hotkey;
context.logger = logger;
context.project = project;
context.canvas = new Canvas(editor);
}
};
pluginManager = new LowCodePluginManager(contextApiAssembler).toProxy();
Expand Down
42 changes: 13 additions & 29 deletions packages/engine/src/inner-plugins/builtin-hotkey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable max-len */
import { Editor, globalContext } from '@alilc/lowcode-editor-core';
import { isFormEvent } from '@alilc/lowcode-utils';
import {
focusing,
Expand All @@ -12,22 +11,8 @@ import {
IPublicModelNode,
} from '@alilc/lowcode-types';
import symbols from '../modules/symbols';
const { nodeSymbol, documentSymbol } = symbols;

export function isInLiveEditing() {
const workspace = globalContext.has('workspace') && globalContext.get('workspace');
if (workspace?.isActive) {
return Boolean(
workspace.window.editor.get('designer')?.project?.simulator?.liveEditing?.editing,
);
}

if (globalContext.has(Editor)) {
return Boolean(
globalContext.get(Editor).get('designer')?.project?.simulator?.liveEditing?.editing,
);
}
}
const { nodeSymbol, documentSymbol } = symbols;

/* istanbul ignore next */
function getNextForSelect(next: IPublicModelNode | null, head?: any, parent?: IPublicModelNode | null): any {
Expand Down Expand Up @@ -95,12 +80,12 @@ function getPrevForSelect(prev: IPublicModelNode | null, head?: any, parent?: IP
export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
return {
init() {
const { hotkey, project, logger } = ctx;
const { hotkey, project, logger, canvas } = ctx;
// hotkey binding
hotkey.bind(['backspace', 'del'], (e: KeyboardEvent, action) => {
logger.info(`action ${action} is triggered`);

if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
// TODO: use focus-tracker
Expand All @@ -124,7 +109,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
hotkey.bind('escape', (e: KeyboardEvent, action) => {
logger.info(`action ${action} is triggered`);
// const currentFocus = focusing.current;
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const sel = focusing.focusDesigner?.currentDocument?.selection;
Expand All @@ -140,7 +125,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
// command + c copy command + x cut
hotkey.bind(['command+c', 'ctrl+c', 'command+x', 'ctrl+x'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.currentDocument;
Expand Down Expand Up @@ -179,10 +164,9 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
// command + v paste
hotkey.bind(['command+v', 'ctrl+v'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
if (isInLiveEditing()) return;
// TODO
const designer = focusing.focusDesigner;
const doc = project?.currentDocument;
Expand Down Expand Up @@ -212,7 +196,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
// command + z undo
hotkey.bind(['command+z', 'ctrl+z'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const history = project.currentDocument?.history;
Expand All @@ -230,7 +214,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
// command + shift + z redo
hotkey.bind(['command+y', 'ctrl+y', 'command+shift+z'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const history = project.currentDocument?.history;
Expand All @@ -247,7 +231,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
// sibling selection
hotkey.bind(['left', 'right'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.currentDocument;
Expand All @@ -266,7 +250,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {

hotkey.bind(['up', 'down'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.currentDocument;
Expand All @@ -291,7 +275,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {

hotkey.bind(['option+left', 'option+right'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.currentDocument;
Expand Down Expand Up @@ -325,7 +309,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {

hotkey.bind(['option+up'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.currentDocument;
Expand Down Expand Up @@ -367,7 +351,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {

hotkey.bind(['option+down'], (e, action) => {
logger.info(`action ${action} is triggered`);
if (isInLiveEditing()) {
if (canvas.isInLiveEditing) {
return;
}
const doc = project.getCurrentDocument();
Expand Down
4 changes: 4 additions & 0 deletions packages/shell/src/api/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class Canvas implements IPublicApiCanvas {
return activeTracker;
}

get isInLiveEditing(): boolean {
return Boolean(this[editorSymbol].get('designer')?.project?.simulator?.liveEditing?.editing);
}

constructor(editor: IPublicModelEditor, readonly workspaceMode: boolean = false) {
this[editorSymbol] = editor;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/types/src/shell/api/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { IPublicModelDragon, IPublicModelDropLocation, IPublicModelScrollTarget, IPublicModelScrollable, IPublicModelScroller, IPublicModelActiveTracker } from '../model';
import { IPublicTypeLocationData } from '../type';

/**
* @since v1.1.0
*/
export interface IPublicApiCanvas {


/**
* 创一个滚动控制器 Scroller,赋予一个视图滚动的基本能力,
* a Scroller is a controller that gives a view (IPublicModelScrollable) the ability scrolling
Expand Down Expand Up @@ -45,4 +47,11 @@ export interface IPublicApiCanvas {
* @since v1.1.0
*/
get activeTracker(): IPublicModelActiveTracker | null;

/**
* 是否处于 LiveEditing 状态
* check if canvas is in liveEditing state
* @since v1.1.0
*/
get isInLiveEditing(): boolean;
}

0 comments on commit f25babe

Please sign in to comment.