Skip to content

Commit f25babe

Browse files
JackLianliujuping
authored andcommitted
refactor: add isInLiveEditing api to canvas for refactoring of hotkey plugin as a standalone one
1 parent 4fd3af1 commit f25babe

File tree

5 files changed

+46
-59
lines changed

5 files changed

+46
-59
lines changed

docs/docs/api/canvas.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: cavas - 画布 API
2+
title: canvas - 画布 API
33
sidebar_position: 12
44
---
55

@@ -17,30 +17,25 @@ sidebar_position: 12
1717

1818
获取拖拽操作对象的实例
1919

20-
```typescript
21-
/**
22-
* 获取拖拽操作对象的实例
23-
* get dragon instance, you can use this to obtain draging related abilities and lifecycle hooks
24-
* @since v1.1.0
25-
*/
26-
get dragon(): IPublicModelDragon | null;
27-
```
28-
关联模型 [IPublicModelDragon](./model/dragon)
20+
`@type {IPublicModelDragon | null}`
21+
22+
23+
相关类型:[IPublicModelDragon](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/dragon.ts)
2924

3025
### activeTracker
3126

3227
获取活动追踪器实例
3328

34-
```typescript
35-
/**
36-
* 获取活动追踪器实例
37-
* get activeTracker instance, which is a singleton running in engine.
38-
* it tracks document`s current focusing node/node[], and notify it`s subscribers that when
39-
* focusing node/node[] changed.
40-
* @since v1.1.0
41-
*/
42-
get activeTracker(): IPublicModelActiveTracker | null;
43-
```
29+
`@type {IPublicModelActiveTracker | null}`
30+
31+
相关类型:[IPublicModelActiveTracker](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/active-tracker.ts)
32+
33+
### isInLiveEditing
34+
35+
是否处于 LiveEditing 状态
36+
37+
`@type {boolean}`
38+
4439

4540
## 方法
4641

@@ -83,4 +78,4 @@ createScroller(scrollable: IPublicModelScrollable): IPublicModelScroller;
8378
* @since v1.1.0
8479
*/
8580
createScrollTarget(shell: HTMLDivElement): IPublicModelScrollTarget;
86-
```
81+
```

packages/designer/tests/designer/builtin-hotkey.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,18 @@ import {
77
import { Designer } from '../../src/designer/designer';
88
import formSchema from '../fixtures/schema/form';
99
import { fireEvent } from '@testing-library/react';
10-
import { isInLiveEditing, builtinHotkey } from '../../../engine/src/inner-plugins/builtin-hotkey';
10+
import { builtinHotkey } from '../../../engine/src/inner-plugins/builtin-hotkey';
1111
import { shellModelFactory } from '../../../engine/src/modules/shell-model-factory';
1212
import { ILowCodePluginContextPrivate, LowCodePluginManager } from '@alilc/lowcode-designer';
1313
import { IPublicApiPlugins } from '@alilc/lowcode-types';
14-
import { Logger, Project } from '@alilc/lowcode-shell';
14+
import { Logger, Project, Canvas } from '@alilc/lowcode-shell';
1515
import { Workspace } from '@alilc/lowcode-workspace';
1616

1717
const editor = new Editor();
1818
const workspace = new Workspace();
1919

2020
let designer: Designer;
2121

22-
describe('error scenarios', () => {
23-
it('edtior not registered', () => {
24-
expect(isInLiveEditing()).toBeUndefined();
25-
});
26-
});
27-
2822
// keyCode 对应表:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
2923
// hotkey 模块底层用的 keyCode,所以还不能用 key / code 测试
3024
describe('快捷键测试', () => {
@@ -40,6 +34,7 @@ describe('快捷键测试', () => {
4034
context.hotkey = hotkey;
4135
context.logger = logger;
4236
context.project = project;
37+
context.canvas = new Canvas(editor);
4338
}
4439
};
4540
pluginManager = new LowCodePluginManager(contextApiAssembler).toProxy();

packages/engine/src/inner-plugins/builtin-hotkey.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-disable max-len */
2-
import { Editor, globalContext } from '@alilc/lowcode-editor-core';
32
import { isFormEvent } from '@alilc/lowcode-utils';
43
import {
54
focusing,
@@ -12,22 +11,8 @@ import {
1211
IPublicModelNode,
1312
} from '@alilc/lowcode-types';
1413
import symbols from '../modules/symbols';
15-
const { nodeSymbol, documentSymbol } = symbols;
16-
17-
export function isInLiveEditing() {
18-
const workspace = globalContext.has('workspace') && globalContext.get('workspace');
19-
if (workspace?.isActive) {
20-
return Boolean(
21-
workspace.window.editor.get('designer')?.project?.simulator?.liveEditing?.editing,
22-
);
23-
}
2414

25-
if (globalContext.has(Editor)) {
26-
return Boolean(
27-
globalContext.get(Editor).get('designer')?.project?.simulator?.liveEditing?.editing,
28-
);
29-
}
30-
}
15+
const { nodeSymbol, documentSymbol } = symbols;
3116

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

103-
if (isInLiveEditing()) {
88+
if (canvas.isInLiveEditing) {
10489
return;
10590
}
10691
// TODO: use focus-tracker
@@ -124,7 +109,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
124109
hotkey.bind('escape', (e: KeyboardEvent, action) => {
125110
logger.info(`action ${action} is triggered`);
126111
// const currentFocus = focusing.current;
127-
if (isInLiveEditing()) {
112+
if (canvas.isInLiveEditing) {
128113
return;
129114
}
130115
const sel = focusing.focusDesigner?.currentDocument?.selection;
@@ -140,7 +125,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
140125
// command + c copy command + x cut
141126
hotkey.bind(['command+c', 'ctrl+c', 'command+x', 'ctrl+x'], (e, action) => {
142127
logger.info(`action ${action} is triggered`);
143-
if (isInLiveEditing()) {
128+
if (canvas.isInLiveEditing) {
144129
return;
145130
}
146131
const doc = project.currentDocument;
@@ -179,10 +164,9 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
179164
// command + v paste
180165
hotkey.bind(['command+v', 'ctrl+v'], (e, action) => {
181166
logger.info(`action ${action} is triggered`);
182-
if (isInLiveEditing()) {
167+
if (canvas.isInLiveEditing) {
183168
return;
184169
}
185-
if (isInLiveEditing()) return;
186170
// TODO
187171
const designer = focusing.focusDesigner;
188172
const doc = project?.currentDocument;
@@ -212,7 +196,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
212196
// command + z undo
213197
hotkey.bind(['command+z', 'ctrl+z'], (e, action) => {
214198
logger.info(`action ${action} is triggered`);
215-
if (isInLiveEditing()) {
199+
if (canvas.isInLiveEditing) {
216200
return;
217201
}
218202
const history = project.currentDocument?.history;
@@ -230,7 +214,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
230214
// command + shift + z redo
231215
hotkey.bind(['command+y', 'ctrl+y', 'command+shift+z'], (e, action) => {
232216
logger.info(`action ${action} is triggered`);
233-
if (isInLiveEditing()) {
217+
if (canvas.isInLiveEditing) {
234218
return;
235219
}
236220
const history = project.currentDocument?.history;
@@ -247,7 +231,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
247231
// sibling selection
248232
hotkey.bind(['left', 'right'], (e, action) => {
249233
logger.info(`action ${action} is triggered`);
250-
if (isInLiveEditing()) {
234+
if (canvas.isInLiveEditing) {
251235
return;
252236
}
253237
const doc = project.currentDocument;
@@ -266,7 +250,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
266250

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

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

326310
hotkey.bind(['option+up'], (e, action) => {
327311
logger.info(`action ${action} is triggered`);
328-
if (isInLiveEditing()) {
312+
if (canvas.isInLiveEditing) {
329313
return;
330314
}
331315
const doc = project.currentDocument;
@@ -367,7 +351,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
367351

368352
hotkey.bind(['option+down'], (e, action) => {
369353
logger.info(`action ${action} is triggered`);
370-
if (isInLiveEditing()) {
354+
if (canvas.isInLiveEditing) {
371355
return;
372356
}
373357
const doc = project.getCurrentDocument();

packages/shell/src/api/canvas.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class Canvas implements IPublicApiCanvas {
3636
return activeTracker;
3737
}
3838

39+
get isInLiveEditing(): boolean {
40+
return Boolean(this[editorSymbol].get('designer')?.project?.simulator?.liveEditing?.editing);
41+
}
42+
3943
constructor(editor: IPublicModelEditor, readonly workspaceMode: boolean = false) {
4044
this[editorSymbol] = editor;
4145
}

packages/types/src/shell/api/canvas.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { IPublicModelDragon, IPublicModelDropLocation, IPublicModelScrollTarget, IPublicModelScrollable, IPublicModelScroller, IPublicModelActiveTracker } from '../model';
22
import { IPublicTypeLocationData } from '../type';
33

4+
/**
5+
* @since v1.1.0
6+
*/
47
export interface IPublicApiCanvas {
58

6-
79
/**
810
* 创一个滚动控制器 Scroller,赋予一个视图滚动的基本能力,
911
* a Scroller is a controller that gives a view (IPublicModelScrollable) the ability scrolling
@@ -45,4 +47,11 @@ export interface IPublicApiCanvas {
4547
* @since v1.1.0
4648
*/
4749
get activeTracker(): IPublicModelActiveTracker | null;
50+
51+
/**
52+
* 是否处于 LiveEditing 状态
53+
* check if canvas is in liveEditing state
54+
* @since v1.1.0
55+
*/
56+
get isInLiveEditing(): boolean;
4857
}

0 commit comments

Comments
 (0)