@tmagic/editor 1.4.8 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -11
- package/src/Editor.vue +211 -0
- package/src/components/CodeBlockEditor.vue +268 -0
- package/src/components/CodeParams.vue +60 -0
- package/src/components/ContentMenu.vue +193 -0
- package/src/components/FloatingBox.vue +178 -0
- package/src/components/Icon.vue +27 -0
- package/src/components/Resizer.vue +23 -0
- package/src/components/ScrollBar.vue +151 -0
- package/src/components/ScrollViewer.vue +128 -0
- package/src/components/SearchInput.vue +37 -0
- package/src/components/SplitView.vue +186 -0
- package/src/components/ToolButton.vue +138 -0
- package/src/components/Tree.vue +71 -0
- package/src/components/TreeNode.vue +150 -0
- package/src/editorProps.ts +117 -0
- package/src/fields/Code.vue +50 -0
- package/src/fields/CodeLink.vue +80 -0
- package/src/fields/CodeSelect.vue +132 -0
- package/src/fields/CodeSelectCol.vue +139 -0
- package/src/fields/DataSourceFieldSelect.vue +149 -0
- package/src/fields/DataSourceFields.vue +323 -0
- package/src/fields/DataSourceInput.vue +339 -0
- package/src/fields/DataSourceMethodSelect.vue +153 -0
- package/src/fields/DataSourceMethods.vue +104 -0
- package/src/fields/DataSourceMocks.vue +255 -0
- package/src/fields/DataSourceSelect.vue +98 -0
- package/src/fields/EventSelect.vue +375 -0
- package/src/fields/KeyValue.vue +137 -0
- package/src/fields/PageFragmentSelect.vue +63 -0
- package/src/fields/UISelect.vue +135 -0
- package/src/hooks/index.ts +24 -0
- package/src/hooks/use-code-block-edit.ts +83 -0
- package/src/hooks/use-data-source-edit.ts +46 -0
- package/src/hooks/use-data-source-method.ts +100 -0
- package/src/hooks/use-editor-content-height.ts +26 -0
- package/src/hooks/use-filter.ts +55 -0
- package/src/hooks/use-float-box.ts +76 -0
- package/src/hooks/use-getso.ts +35 -0
- package/src/hooks/use-next-float-box-position.ts +29 -0
- package/src/hooks/use-node-status.ts +48 -0
- package/src/hooks/use-stage.ts +129 -0
- package/src/hooks/use-window-rect.ts +20 -0
- package/src/icons/AppManageIcon.vue +15 -0
- package/src/icons/CenterIcon.vue +13 -0
- package/src/icons/CodeIcon.vue +28 -0
- package/src/icons/FolderMinusIcon.vue +22 -0
- package/src/icons/PinIcon.vue +25 -0
- package/src/icons/PinnedIcon.vue +25 -0
- package/src/index.ts +118 -0
- package/src/initService.ts +448 -0
- package/src/layouts/AddPageBox.vue +55 -0
- package/src/layouts/CodeEditor.vue +226 -0
- package/src/layouts/Framework.vue +163 -0
- package/src/layouts/NavMenu.vue +200 -0
- package/src/layouts/PropsPanel.vue +131 -0
- package/src/layouts/page-bar/AddButton.vue +48 -0
- package/src/layouts/page-bar/PageBar.vue +159 -0
- package/src/layouts/page-bar/PageBarScrollContainer.vue +156 -0
- package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
- package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
- package/src/layouts/sidebar/Sidebar.vue +278 -0
- package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
- package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
- package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
- package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
- package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
- package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
- package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
- package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
- package/src/layouts/sidebar/layer/use-click.ts +113 -0
- package/src/layouts/sidebar/layer/use-drag.ts +167 -0
- package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
- package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
- package/src/layouts/workspace/Breadcrumb.vue +35 -0
- package/src/layouts/workspace/Workspace.vue +46 -0
- package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
- package/src/layouts/workspace/viewer/Stage.vue +246 -0
- package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
- package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
- package/src/services/BaseService.ts +230 -0
- package/src/services/codeBlock.ts +327 -0
- package/src/services/componentList.ts +58 -0
- package/src/services/dataSource.ts +216 -0
- package/src/services/dep.ts +152 -0
- package/src/services/editor.ts +1135 -0
- package/src/services/events.ts +93 -0
- package/src/services/history.ts +126 -0
- package/src/services/keybinding.ts +220 -0
- package/src/services/props.ts +286 -0
- package/src/services/stageOverlay.ts +212 -0
- package/src/services/storage.ts +155 -0
- package/src/services/ui.ts +156 -0
- package/src/shims-vue.d.ts +6 -0
- package/src/theme/breadcrumb.scss +6 -0
- package/src/theme/code-block.scss +6 -0
- package/src/theme/code-editor.scss +38 -0
- package/src/theme/common/var.scss +14 -0
- package/src/theme/component-list-panel.scss +95 -0
- package/src/theme/content-menu.scss +76 -0
- package/src/theme/data-source-field.scss +12 -0
- package/src/theme/data-source-fields.scss +13 -0
- package/src/theme/data-source-input.scss +18 -0
- package/src/theme/data-source-methods.scss +13 -0
- package/src/theme/data-source.scss +28 -0
- package/src/theme/event.scss +37 -0
- package/src/theme/floating-box.scss +32 -0
- package/src/theme/framework.scss +69 -0
- package/src/theme/icon.scss +12 -0
- package/src/theme/index.scss +10 -0
- package/src/theme/key-value.scss +20 -0
- package/src/theme/layer-panel.scss +26 -0
- package/src/theme/layout.scss +9 -0
- package/src/theme/nav-menu.scss +78 -0
- package/src/theme/page-bar.scss +89 -0
- package/src/theme/page-fragment-select.scss +14 -0
- package/src/theme/props-panel.scss +55 -0
- package/src/theme/resizer.scss +66 -0
- package/src/theme/ruler.scss +38 -0
- package/src/theme/search-input.scss +14 -0
- package/src/theme/sidebar.scss +79 -0
- package/src/theme/stage.scss +76 -0
- package/src/theme/theme.scss +27 -0
- package/src/theme/tree.scss +89 -0
- package/src/theme/workspace.scss +9 -0
- package/src/type.ts +749 -0
- package/src/utils/compose.ts +52 -0
- package/src/utils/config.ts +29 -0
- package/src/utils/content-menu.ts +95 -0
- package/src/utils/data-source/formConfigs/base.ts +30 -0
- package/src/utils/data-source/formConfigs/http.ts +60 -0
- package/src/utils/data-source/index.ts +243 -0
- package/src/utils/editor.ts +313 -0
- package/src/utils/idle-task.ts +72 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/keybinding-config.ts +125 -0
- package/src/utils/logger.ts +47 -0
- package/src/utils/monaco-editor.ts +8 -0
- package/src/utils/operator.ts +106 -0
- package/src/utils/props.ts +504 -0
- package/src/utils/scroll-viewer.ts +165 -0
- package/src/utils/tree.ts +15 -0
- package/src/utils/undo-redo.ts +76 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { reactive } from 'vue';
|
|
20
|
+
import { cloneDeep } from 'lodash-es';
|
|
21
|
+
|
|
22
|
+
import { DEFAULT_EVENTS, DEFAULT_METHODS, EventOption } from '@tmagic/core';
|
|
23
|
+
import { toLine } from '@tmagic/utils';
|
|
24
|
+
|
|
25
|
+
import type { ComponentGroup } from '@editor/type';
|
|
26
|
+
|
|
27
|
+
import BaseService from './BaseService';
|
|
28
|
+
|
|
29
|
+
let eventMap: Record<string, EventOption[]> = reactive({});
|
|
30
|
+
let methodMap: Record<string, EventOption[]> = reactive({});
|
|
31
|
+
|
|
32
|
+
class Events extends BaseService {
|
|
33
|
+
constructor() {
|
|
34
|
+
super([]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public init(componentGroupList: ComponentGroup[]) {
|
|
38
|
+
componentGroupList.forEach((group) => {
|
|
39
|
+
group.items.forEach((element) => {
|
|
40
|
+
const type = toLine(element.type);
|
|
41
|
+
if (!this.getEvent(type)) {
|
|
42
|
+
this.setEvent(type, DEFAULT_EVENTS);
|
|
43
|
+
}
|
|
44
|
+
if (!this.getMethod(type)) {
|
|
45
|
+
this.setMethod(type, DEFAULT_METHODS);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public setEvents(events: Record<string, EventOption[]>) {
|
|
52
|
+
Object.keys(events).forEach((type: string) => {
|
|
53
|
+
this.setEvent(toLine(type), events[type] || []);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public setEvent(type: string, events: EventOption[]) {
|
|
58
|
+
eventMap[toLine(type)] = [...DEFAULT_EVENTS, ...events];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public getEvent(type: string): EventOption[] {
|
|
62
|
+
return cloneDeep(eventMap[toLine(type)] || DEFAULT_EVENTS);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public setMethods(methods: Record<string, EventOption[]>) {
|
|
66
|
+
Object.keys(methods).forEach((type: string) => {
|
|
67
|
+
this.setMethod(toLine(type), methods[type] || []);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public setMethod(type: string, method: EventOption[]) {
|
|
72
|
+
methodMap[toLine(type)] = [...DEFAULT_METHODS, ...method];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getMethod(type: string) {
|
|
76
|
+
return cloneDeep(methodMap[toLine(type)] || DEFAULT_METHODS);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public resetState() {
|
|
80
|
+
eventMap = reactive({});
|
|
81
|
+
methodMap = reactive({});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public destroy() {
|
|
85
|
+
this.resetState();
|
|
86
|
+
this.removeAllListeners();
|
|
87
|
+
this.removeAllPlugins();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type EventsService = Events;
|
|
92
|
+
|
|
93
|
+
export default new Events();
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { reactive } from 'vue';
|
|
20
|
+
|
|
21
|
+
import type { MPage, MPageFragment } from '@tmagic/schema';
|
|
22
|
+
|
|
23
|
+
import type { HistoryState, StepValue } from '@editor/type';
|
|
24
|
+
import { UndoRedo } from '@editor/utils/undo-redo';
|
|
25
|
+
|
|
26
|
+
import BaseService from './BaseService';
|
|
27
|
+
|
|
28
|
+
class History extends BaseService {
|
|
29
|
+
public state = reactive<HistoryState>({
|
|
30
|
+
pageSteps: {},
|
|
31
|
+
pageId: undefined,
|
|
32
|
+
canRedo: false,
|
|
33
|
+
canUndo: false,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
constructor() {
|
|
37
|
+
super([]);
|
|
38
|
+
|
|
39
|
+
this.on('change', this.setCanUndoRedo);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public reset() {
|
|
43
|
+
this.state.pageSteps = {};
|
|
44
|
+
this.resetPage();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public resetPage() {
|
|
48
|
+
this.state.pageId = undefined;
|
|
49
|
+
this.state.canRedo = false;
|
|
50
|
+
this.state.canUndo = false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public changePage(page: MPage | MPageFragment): void {
|
|
54
|
+
if (!page) return;
|
|
55
|
+
|
|
56
|
+
this.state.pageId = page.id;
|
|
57
|
+
|
|
58
|
+
if (!this.state.pageSteps[this.state.pageId]) {
|
|
59
|
+
const undoRedo = new UndoRedo<StepValue>();
|
|
60
|
+
|
|
61
|
+
undoRedo.pushElement({
|
|
62
|
+
data: page,
|
|
63
|
+
modifiedNodeIds: new Map(),
|
|
64
|
+
nodeId: page.id,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
this.state.pageSteps[this.state.pageId] = undoRedo;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.setCanUndoRedo();
|
|
71
|
+
|
|
72
|
+
this.emit('page-change', this.state.pageSteps[this.state.pageId]);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public resetState(): void {
|
|
76
|
+
this.state.pageId = undefined;
|
|
77
|
+
this.state.pageSteps = {};
|
|
78
|
+
this.state.canRedo = false;
|
|
79
|
+
this.state.canUndo = false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public push(state: StepValue): StepValue | null {
|
|
83
|
+
const undoRedo = this.getUndoRedo();
|
|
84
|
+
if (!undoRedo) return null;
|
|
85
|
+
undoRedo.pushElement(state);
|
|
86
|
+
this.emit('change', state);
|
|
87
|
+
return state;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public undo(): StepValue | null {
|
|
91
|
+
const undoRedo = this.getUndoRedo();
|
|
92
|
+
if (!undoRedo) return null;
|
|
93
|
+
const state = undoRedo.undo();
|
|
94
|
+
this.emit('change', state);
|
|
95
|
+
return state;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public redo(): StepValue | null {
|
|
99
|
+
const undoRedo = this.getUndoRedo();
|
|
100
|
+
if (!undoRedo) return null;
|
|
101
|
+
const state = undoRedo.redo();
|
|
102
|
+
this.emit('change', state);
|
|
103
|
+
return state;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public destroy(): void {
|
|
107
|
+
this.resetState();
|
|
108
|
+
this.removeAllListeners();
|
|
109
|
+
this.removeAllPlugins();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private getUndoRedo() {
|
|
113
|
+
if (!this.state.pageId) return null;
|
|
114
|
+
return this.state.pageSteps[this.state.pageId];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private setCanUndoRedo(): void {
|
|
118
|
+
const undoRedo = this.getUndoRedo();
|
|
119
|
+
this.state.canRedo = undoRedo?.canRedo() || false;
|
|
120
|
+
this.state.canUndo = undoRedo?.canUndo() || false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type HistoryService = History;
|
|
125
|
+
|
|
126
|
+
export default new History();
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import KeyController, { KeyControllerEvent } from 'keycon';
|
|
2
|
+
|
|
3
|
+
import { isPage, isPageFragment } from '@tmagic/utils';
|
|
4
|
+
|
|
5
|
+
import { KeyBindingCacheItem, KeyBindingCommand, KeyBindingItem } from '@editor/type';
|
|
6
|
+
|
|
7
|
+
import BaseService from './BaseService';
|
|
8
|
+
import editorService from './editor';
|
|
9
|
+
import uiService from './ui';
|
|
10
|
+
|
|
11
|
+
class Keybinding extends BaseService {
|
|
12
|
+
public ctrlKey = /mac os x/.test(navigator.userAgent.toLowerCase()) ? 'meta' : 'ctrl';
|
|
13
|
+
|
|
14
|
+
private controllers = new Map<string, KeyController>();
|
|
15
|
+
|
|
16
|
+
private bindingList: KeyBindingCacheItem[] = [];
|
|
17
|
+
|
|
18
|
+
private commands: Record<KeyBindingCommand | string, (e: KeyboardEvent) => void | Promise<void>> = {
|
|
19
|
+
[KeyBindingCommand.DELETE_NODE]: () => {
|
|
20
|
+
const nodes = editorService.get('nodes');
|
|
21
|
+
|
|
22
|
+
if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
|
|
23
|
+
editorService.remove(nodes);
|
|
24
|
+
},
|
|
25
|
+
[KeyBindingCommand.COPY_NODE]: () => {
|
|
26
|
+
const nodes = editorService.get('nodes');
|
|
27
|
+
nodes && editorService.copy(nodes);
|
|
28
|
+
},
|
|
29
|
+
[KeyBindingCommand.CUT_NODE]: () => {
|
|
30
|
+
const nodes = editorService.get('nodes');
|
|
31
|
+
|
|
32
|
+
if (!nodes || isPage(nodes[0]) || isPageFragment(nodes[0])) return;
|
|
33
|
+
editorService.copy(nodes);
|
|
34
|
+
editorService.remove(nodes);
|
|
35
|
+
},
|
|
36
|
+
[KeyBindingCommand.PASTE_NODE]: () => {
|
|
37
|
+
const nodes = editorService.get('nodes');
|
|
38
|
+
nodes && editorService.paste({ offsetX: 10, offsetY: 10 });
|
|
39
|
+
},
|
|
40
|
+
[KeyBindingCommand.UNDO]: () => {
|
|
41
|
+
editorService.undo();
|
|
42
|
+
},
|
|
43
|
+
[KeyBindingCommand.REDO]: () => {
|
|
44
|
+
editorService.redo();
|
|
45
|
+
},
|
|
46
|
+
[KeyBindingCommand.ZOOM_IN]: () => {
|
|
47
|
+
uiService.zoom(0.1);
|
|
48
|
+
},
|
|
49
|
+
[KeyBindingCommand.ZOOM_OUT]: () => {
|
|
50
|
+
uiService.zoom(-0.1);
|
|
51
|
+
},
|
|
52
|
+
[KeyBindingCommand.ZOOM_RESET]: () => {
|
|
53
|
+
uiService.set('zoom', 1);
|
|
54
|
+
},
|
|
55
|
+
[KeyBindingCommand.ZOOM_FIT]: async () => {
|
|
56
|
+
uiService.set('zoom', await uiService.calcZoom());
|
|
57
|
+
},
|
|
58
|
+
[KeyBindingCommand.MOVE_UP_1]: () => {
|
|
59
|
+
editorService.move(0, -1);
|
|
60
|
+
},
|
|
61
|
+
[KeyBindingCommand.MOVE_DOWN_1]: () => {
|
|
62
|
+
editorService.move(0, 1);
|
|
63
|
+
},
|
|
64
|
+
[KeyBindingCommand.MOVE_LEFT_1]: () => {
|
|
65
|
+
editorService.move(-1, 0);
|
|
66
|
+
},
|
|
67
|
+
[KeyBindingCommand.MOVE_RIGHT_1]: () => {
|
|
68
|
+
editorService.move(1, 0);
|
|
69
|
+
},
|
|
70
|
+
[KeyBindingCommand.MOVE_UP_10]: () => {
|
|
71
|
+
editorService.move(0, -10);
|
|
72
|
+
},
|
|
73
|
+
[KeyBindingCommand.MOVE_DOWN_10]: () => {
|
|
74
|
+
editorService.move(0, 10);
|
|
75
|
+
},
|
|
76
|
+
[KeyBindingCommand.MOVE_LEFT_10]: () => {
|
|
77
|
+
editorService.move(-10, 0);
|
|
78
|
+
},
|
|
79
|
+
[KeyBindingCommand.MOVE_RIGHT_10]: () => {
|
|
80
|
+
editorService.move(10, 0);
|
|
81
|
+
},
|
|
82
|
+
[KeyBindingCommand.SWITCH_NODE]: () => {
|
|
83
|
+
editorService.selectNextNode();
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
public registerCommand(command: string, handler: (e: KeyboardEvent) => void | Promise<void>) {
|
|
88
|
+
this.commands[command] = handler;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @deprecated
|
|
93
|
+
*/
|
|
94
|
+
public registeCommand(command: string, handler: (e: KeyboardEvent) => void | Promise<void>) {
|
|
95
|
+
this.registerCommand(command, handler);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public unregisterCommand(command: string) {
|
|
99
|
+
delete this.commands[command];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @deprecated
|
|
104
|
+
*/
|
|
105
|
+
public unregisteCommand(command: string) {
|
|
106
|
+
this.unregisterCommand(command);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public registerEl(name: string, el?: HTMLElement) {
|
|
110
|
+
if (name !== 'global' && !el) {
|
|
111
|
+
throw new Error('只有name为global可以不传el');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const keycon = new KeyController(el);
|
|
115
|
+
this.controllers.set(name, keycon);
|
|
116
|
+
this.bind(name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @deprecated
|
|
121
|
+
*/
|
|
122
|
+
public registeEl(name: string, el?: HTMLElement) {
|
|
123
|
+
this.registerEl(name, el);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public unregisterEl(name: string) {
|
|
127
|
+
this.controllers.get(name)?.destroy();
|
|
128
|
+
this.controllers.delete(name);
|
|
129
|
+
this.bindingList.forEach((item) => {
|
|
130
|
+
item.bound = false;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @deprecated
|
|
136
|
+
*/
|
|
137
|
+
public unregisteEl(name: string) {
|
|
138
|
+
this.unregisterEl(name);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public register(maps: KeyBindingItem[]) {
|
|
142
|
+
for (const keybindingItem of maps) {
|
|
143
|
+
const { command, keybinding, when } = keybindingItem;
|
|
144
|
+
|
|
145
|
+
for (const [type = '', eventType = 'keydown'] of when) {
|
|
146
|
+
const cacheItem: KeyBindingCacheItem = { type, command, keybinding, eventType, bound: false };
|
|
147
|
+
|
|
148
|
+
this.bindingList.push(cacheItem);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.bind();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @deprecated
|
|
157
|
+
*/
|
|
158
|
+
public registe(map: KeyBindingItem[]) {
|
|
159
|
+
this.register(map);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public reset() {
|
|
163
|
+
this.controllers.forEach((keycon) => {
|
|
164
|
+
keycon.destroy();
|
|
165
|
+
});
|
|
166
|
+
this.controllers.clear();
|
|
167
|
+
this.bindingList = [];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public destroy() {
|
|
171
|
+
this.reset();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private bind(name?: string) {
|
|
175
|
+
for (const item of this.bindingList) {
|
|
176
|
+
const { type, eventType, command, keybinding, bound } = item;
|
|
177
|
+
|
|
178
|
+
if (name && name !== type) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (bound) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const keycon = this.controllers.get(type);
|
|
187
|
+
if (!keycon) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const handler = (e: KeyControllerEvent) => {
|
|
192
|
+
e.inputEvent.preventDefault();
|
|
193
|
+
|
|
194
|
+
this.commands[command]?.(e.inputEvent);
|
|
195
|
+
};
|
|
196
|
+
this.getKeyconKeys(keybinding).forEach((keys) => {
|
|
197
|
+
if (keys[0]) {
|
|
198
|
+
keycon[eventType](keys, handler);
|
|
199
|
+
} else {
|
|
200
|
+
keycon[eventType](handler);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
item.bound = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private getKeyconKeys(keybinding: string | string[] = '') {
|
|
209
|
+
const splitKey = (key: string) => key.split('+').map((k) => (k === 'ctrl' ? this.ctrlKey : k));
|
|
210
|
+
|
|
211
|
+
if (Array.isArray(keybinding)) {
|
|
212
|
+
return keybinding.map((key) => splitKey(key));
|
|
213
|
+
}
|
|
214
|
+
return [splitKey(keybinding)];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export type KeybindingService = Keybinding;
|
|
219
|
+
|
|
220
|
+
export default new Keybinding();
|