@easy-editor/plugin-hotkey 0.0.1-alpha.0

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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright © 2024-PRESENT JinSo <https://github.com/JinSooo>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # @easy-editor/plugin-hotkey
2
+
3
+ Hotkey plugin for EasyEditor.
4
+
5
+ ## Hotkeys
6
+
7
+ | Action | Shortcut |
8
+ |--------|----------|
9
+ | Undo | ⌘ + Z / Ctrl + Z |
10
+ | Redo | ⌘ + Y / Ctrl + Y |
11
+ | Lock/Unlock | ⌘ + Shift + L / Ctrl + Shift + L |
12
+ | Show/Hide | ⌘ + Shift + H / Ctrl + Shift + H |
13
+ | Copy | ⌘ + C / Ctrl + C |
14
+ | Paste | ⌘ + V / Ctrl + V |
15
+ | Cut | ⌘ + X / Ctrl + X |
16
+ | Delete | Backspace / Delete |
17
+ | Clear Selection | Esc |
18
+
@@ -0,0 +1,173 @@
1
+ 'use strict';
2
+
3
+ var core = require('@easy-editor/core');
4
+
5
+ const HOTKEY_MAP = {
6
+ HISTORY_UNDO: ['command+z', 'ctrl+z'],
7
+ HISTORY_REDO: ['command+y', 'ctrl+y'],
8
+ LOCK_UNLOCK: ['command+shift+l', 'ctrl+shift+l'],
9
+ SHOW_HIDE: ['command+shift+h', 'ctrl+shift+h'],
10
+ COPY: ['command+c', 'ctrl+c'],
11
+ PASTE: ['command+v', 'ctrl+v'],
12
+ CUT: ['command+x', 'ctrl+x'],
13
+ DELETE: ['backspace', 'del'],
14
+ CLEAR_SELECTION: ['esc']
15
+ };
16
+
17
+ const isFormEvent = e => {
18
+ const t = e.target;
19
+ if (!t) {
20
+ return false;
21
+ }
22
+ if (t.form || /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) {
23
+ return true;
24
+ }
25
+ if (t instanceof HTMLElement && /write/.test(window.getComputedStyle(t).getPropertyValue('-webkit-user-modify'))) {
26
+ return true;
27
+ }
28
+ return false;
29
+ };
30
+
31
+ const HotkeyPlugin = () => {
32
+ return {
33
+ name: 'HotkeyPlugin',
34
+ deps: [],
35
+ init(ctx) {
36
+ const {
37
+ hotkey,
38
+ project,
39
+ logger,
40
+ designer
41
+ } = ctx;
42
+ hotkey.bind(HOTKEY_MAP.HISTORY_UNDO, () => {
43
+ const currentHistory = project.currentDocument?.history;
44
+ if (currentHistory?.isUndoable()) {
45
+ currentHistory?.back();
46
+ } else {
47
+ logger.log('No operations to undo');
48
+ }
49
+ });
50
+ hotkey.bind(HOTKEY_MAP.HISTORY_REDO, () => {
51
+ const currentHistory = project.currentDocument?.history;
52
+ if (currentHistory?.isRedoable()) {
53
+ currentHistory?.forward();
54
+ } else {
55
+ logger.log('No operations to redo');
56
+ }
57
+ });
58
+ hotkey.bind(HOTKEY_MAP.LOCK_UNLOCK, () => {
59
+ const selection = designer.selection;
60
+ const doc = project.currentDocument;
61
+ for (const nodeId of selection.selected) {
62
+ const node = doc?.getNode(nodeId);
63
+ if (node?.isLocked) {
64
+ node.lock(false);
65
+ logger.log('Lock');
66
+ } else {
67
+ node?.lock(true);
68
+ logger.log('Unlock');
69
+ }
70
+ }
71
+ selection.clear();
72
+ });
73
+ hotkey.bind(HOTKEY_MAP.SHOW_HIDE, () => {
74
+ const selection = designer.selection;
75
+ const doc = project.currentDocument;
76
+ for (const nodeId of selection.selected) {
77
+ const node = doc?.getNode(nodeId);
78
+ if (node?.isHidden) {
79
+ node.hide(false);
80
+ logger.log('Show');
81
+ } else {
82
+ node?.hide(true);
83
+ logger.log('Hide');
84
+ }
85
+ }
86
+ selection.clear();
87
+ });
88
+ hotkey.bind(HOTKEY_MAP.COPY, e => {
89
+ const doc = project.currentDocument;
90
+ if (isFormEvent(e) || !doc) {
91
+ return;
92
+ }
93
+ const selected = designer.selection.getTopNodes(false);
94
+ if (!selected || selected.length < 1) {
95
+ return;
96
+ }
97
+ const componentsTree = selected.map(item => item?.export(core.TRANSFORM_STAGE.CLONE));
98
+ const data = {
99
+ type: 'NodeSchema',
100
+ componentsMap: {},
101
+ componentsTree
102
+ };
103
+ core.clipboard.setData(data);
104
+ });
105
+ hotkey.bind(HOTKEY_MAP.CUT, e => {
106
+ const doc = project.currentDocument;
107
+ if (isFormEvent(e) || !doc) {
108
+ return;
109
+ }
110
+ const selection = designer.selection;
111
+ const selected = selection.getTopNodes(false);
112
+ if (!selected || selected.length < 1) {
113
+ return;
114
+ }
115
+ const componentsTree = selected.map(item => item?.export(core.TRANSFORM_STAGE.CLONE));
116
+ const data = {
117
+ type: 'NodeSchema',
118
+ componentsMap: {},
119
+ componentsTree
120
+ };
121
+ core.clipboard.setData(data);
122
+ for (const node of selected) {
123
+ node?.parent?.select();
124
+ node.remove();
125
+ }
126
+ selection.clear();
127
+ });
128
+ hotkey.bind(HOTKEY_MAP.PASTE, e => {
129
+ const doc = project.currentDocument;
130
+ const selection = designer.selection;
131
+ if (isFormEvent(e) || !doc) {
132
+ return;
133
+ }
134
+ core.clipboard.waitPasteData(e, ({
135
+ componentsTree
136
+ }) => {
137
+ if (componentsTree) {
138
+ const target = doc?.rootNode;
139
+ if (!target) {
140
+ return;
141
+ }
142
+ const nodes = core.insertChildren(target, componentsTree);
143
+ if (nodes) {
144
+ selection.selectAll(nodes.map(o => o.id));
145
+ }
146
+ }
147
+ });
148
+ });
149
+ hotkey.bind(HOTKEY_MAP.DELETE, e => {
150
+ const doc = project.currentDocument;
151
+ const selection = designer.selection;
152
+ if (isFormEvent(e) || !doc) {
153
+ return;
154
+ }
155
+ const nodes = selection.getTopNodes();
156
+ for (const node of nodes) {
157
+ node && doc?.removeNode(node);
158
+ }
159
+ selection.clear();
160
+ });
161
+ hotkey.bind(HOTKEY_MAP.CLEAR_SELECTION, e => {
162
+ const selection = designer.selection;
163
+ if (isFormEvent(e) || !selection) {
164
+ return;
165
+ }
166
+ selection.clear();
167
+ });
168
+ }
169
+ };
170
+ };
171
+
172
+ module.exports = HotkeyPlugin;
173
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/utils.ts","../../src/index.ts"],"sourcesContent":["export const HOTKEY_MAP = {\n /**\n * 历史记录回退\n */\n HISTORY_UNDO: ['command+z', 'ctrl+z'],\n\n /**\n * 历史记录前进\n */\n HISTORY_REDO: ['command+y', 'ctrl+y'],\n\n /**\n * 锁定/解锁\n */\n LOCK_UNLOCK: ['command+shift+l', 'ctrl+shift+l'],\n\n /**\n * 显示/隐藏\n */\n SHOW_HIDE: ['command+shift+h', 'ctrl+shift+h'],\n\n /**\n * 复制\n */\n COPY: ['command+c', 'ctrl+c'],\n\n /**\n * 粘贴\n */\n PASTE: ['command+v', 'ctrl+v'],\n\n /**\n * 剪切\n */\n CUT: ['command+x', 'ctrl+x'],\n\n /**\n * 删除\n */\n DELETE: ['backspace', 'del'],\n\n /**\n * 取消选中\n */\n CLEAR_SELECTION: ['esc'],\n}\n","/**\n * 检查事件是否由表单元素触发\n */\nexport const isFormEvent = (e: KeyboardEvent | MouseEvent) => {\n const t = e.target as HTMLFormElement\n if (!t) {\n return false\n }\n\n if (t.form || /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) {\n return true\n }\n if (t instanceof HTMLElement && /write/.test(window.getComputedStyle(t).getPropertyValue('-webkit-user-modify'))) {\n return true\n }\n return false\n}\n","import { type PluginCreator, TRANSFORM_STAGE, clipboard, insertChildren } from '@easy-editor/core'\nimport { HOTKEY_MAP } from './const'\nimport { isFormEvent } from './utils'\n\nconst HotkeyPlugin: PluginCreator = () => {\n return {\n name: 'HotkeyPlugin',\n deps: [],\n init(ctx) {\n const { hotkey, project, logger, designer } = ctx\n\n hotkey.bind(HOTKEY_MAP.HISTORY_UNDO, () => {\n const currentHistory = project.currentDocument?.history\n if (currentHistory?.isUndoable()) {\n currentHistory?.back()\n } else {\n logger.log('No operations to undo')\n }\n })\n\n hotkey.bind(HOTKEY_MAP.HISTORY_REDO, () => {\n const currentHistory = project.currentDocument?.history\n if (currentHistory?.isRedoable()) {\n currentHistory?.forward()\n } else {\n logger.log('No operations to redo')\n }\n })\n\n hotkey.bind(HOTKEY_MAP.LOCK_UNLOCK, () => {\n const selection = designer.selection\n const doc = project.currentDocument\n\n for (const nodeId of selection.selected) {\n const node = doc?.getNode(nodeId)\n if (node?.isLocked) {\n node.lock(false)\n logger.log('Lock')\n } else {\n node?.lock(true)\n logger.log('Unlock')\n }\n }\n\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.SHOW_HIDE, () => {\n const selection = designer.selection\n const doc = project.currentDocument\n\n for (const nodeId of selection.selected) {\n const node = doc?.getNode(nodeId)\n if (node?.isHidden) {\n node.hide(false)\n logger.log('Show')\n } else {\n node?.hide(true)\n logger.log('Hide')\n }\n }\n\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.COPY, e => {\n const doc = project.currentDocument\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const selected = designer.selection.getTopNodes(false)\n if (!selected || selected.length < 1) {\n return\n }\n\n const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE))\n const data = { type: 'NodeSchema', componentsMap: {}, componentsTree }\n\n clipboard.setData(data)\n })\n\n hotkey.bind(HOTKEY_MAP.CUT, e => {\n const doc = project.currentDocument\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const selection = designer.selection\n const selected = selection.getTopNodes(false)\n if (!selected || selected.length < 1) {\n return\n }\n\n const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE))\n const data = { type: 'NodeSchema', componentsMap: {}, componentsTree }\n\n clipboard.setData(data)\n\n for (const node of selected) {\n node?.parent?.select()\n node.remove()\n }\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.PASTE, e => {\n const doc = project.currentDocument\n const selection = designer.selection\n\n if (isFormEvent(e) || !doc) {\n return\n }\n\n clipboard.waitPasteData(e, ({ componentsTree }) => {\n if (componentsTree) {\n const target = doc?.rootNode\n\n if (!target) {\n return\n }\n\n const nodes = insertChildren(target, componentsTree)\n if (nodes) {\n selection.selectAll(nodes.map(o => o.id))\n }\n }\n })\n })\n\n hotkey.bind(HOTKEY_MAP.DELETE, e => {\n const doc = project.currentDocument\n const selection = designer.selection\n\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const nodes = selection.getTopNodes()\n for (const node of nodes) {\n node && doc?.removeNode(node)\n }\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.CLEAR_SELECTION, e => {\n const selection = designer.selection\n\n if (isFormEvent(e) || !selection) {\n return\n }\n\n selection.clear()\n })\n },\n }\n}\n\nexport default HotkeyPlugin\n"],"names":["HOTKEY_MAP","HISTORY_UNDO","HISTORY_REDO","LOCK_UNLOCK","SHOW_HIDE","COPY","PASTE","CUT","DELETE","CLEAR_SELECTION","isFormEvent","e","t","target","form","test","tagName","HTMLElement","window","getComputedStyle","getPropertyValue","HotkeyPlugin","name","deps","init","ctx","hotkey","project","logger","designer","bind","currentHistory","currentDocument","history","isUndoable","back","log","isRedoable","forward","selection","doc","nodeId","selected","node","getNode","isLocked","lock","clear","isHidden","hide","getTopNodes","length","componentsTree","map","item","export","TRANSFORM_STAGE","CLONE","data","type","componentsMap","clipboard","setData","parent","select","remove","waitPasteData","rootNode","nodes","insertChildren","selectAll","o","id","removeNode"],"mappings":";;;;AAAO,MAAMA,UAAU,GAAG;AAIxBC,EAAAA,YAAY,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAKrCC,EAAAA,YAAY,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAKrCC,EAAAA,WAAW,EAAE,CAAC,iBAAiB,EAAE,cAAc,CAAC;AAKhDC,EAAAA,SAAS,EAAE,CAAC,iBAAiB,EAAE,cAAc,CAAC;AAK9CC,EAAAA,IAAI,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAK7BC,EAAAA,KAAK,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAK9BC,EAAAA,GAAG,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAK5BC,EAAAA,MAAM,EAAE,CAAC,WAAW,EAAE,KAAK,CAAC;EAK5BC,eAAe,EAAE,CAAC,KAAK;AACzB,CAAC;;AC1CM,MAAMC,WAAW,GAAIC,CAA6B,IAAK;AAC5D,EAAA,MAAMC,CAAC,GAAGD,CAAC,CAACE,MAAyB;EACrC,IAAI,CAACD,CAAC,EAAE;AACN,IAAA,OAAO,KAAK;AACd;AAEA,EAAA,IAAIA,CAAC,CAACE,IAAI,IAAI,2BAA2B,CAACC,IAAI,CAACH,CAAC,CAACI,OAAO,CAAC,EAAE;AACzD,IAAA,OAAO,IAAI;AACb;EACA,IAAIJ,CAAC,YAAYK,WAAW,IAAI,OAAO,CAACF,IAAI,CAACG,MAAM,CAACC,gBAAgB,CAACP,CAAC,CAAC,CAACQ,gBAAgB,CAAC,qBAAqB,CAAC,CAAC,EAAE;AAChH,IAAA,OAAO,IAAI;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,GAAG,EAAEiC,UAAU,CAAC9B,IAAI,CAAC;AAC/B;QACAJ,SAAS,CAACQ,KAAK,EAAE;AACnB,OAAC,CAAC;MAEFrB,MAAM,CAACI,IAAI,CAAC9B,UAAU,CAACS,eAAe,EAAEE,CAAC,IAAI;AAC3C,QAAA,MAAM4B,SAAS,GAAGV,QAAQ,CAACU,SAAS;AAEpC,QAAA,IAAI7B,WAAW,CAACC,CAAC,CAAC,IAAI,CAAC4B,SAAS,EAAE;AAChC,UAAA;AACF;QAEAA,SAAS,CAACQ,KAAK,EAAE;AACnB,OAAC,CAAC;AACJ;GACD;AACH;;;;"}
@@ -0,0 +1,38 @@
1
+ export declare const HOTKEY_MAP: {
2
+ /**
3
+ * 历史记录回退
4
+ */
5
+ HISTORY_UNDO: string[];
6
+ /**
7
+ * 历史记录前进
8
+ */
9
+ HISTORY_REDO: string[];
10
+ /**
11
+ * 锁定/解锁
12
+ */
13
+ LOCK_UNLOCK: string[];
14
+ /**
15
+ * 显示/隐藏
16
+ */
17
+ SHOW_HIDE: string[];
18
+ /**
19
+ * 复制
20
+ */
21
+ COPY: string[];
22
+ /**
23
+ * 粘贴
24
+ */
25
+ PASTE: string[];
26
+ /**
27
+ * 剪切
28
+ */
29
+ CUT: string[];
30
+ /**
31
+ * 删除
32
+ */
33
+ DELETE: string[];
34
+ /**
35
+ * 取消选中
36
+ */
37
+ CLEAR_SELECTION: string[];
38
+ };
@@ -0,0 +1,171 @@
1
+ import { TRANSFORM_STAGE, clipboard, insertChildren } from '@easy-editor/core';
2
+
3
+ const HOTKEY_MAP = {
4
+ HISTORY_UNDO: ['command+z', 'ctrl+z'],
5
+ HISTORY_REDO: ['command+y', 'ctrl+y'],
6
+ LOCK_UNLOCK: ['command+shift+l', 'ctrl+shift+l'],
7
+ SHOW_HIDE: ['command+shift+h', 'ctrl+shift+h'],
8
+ COPY: ['command+c', 'ctrl+c'],
9
+ PASTE: ['command+v', 'ctrl+v'],
10
+ CUT: ['command+x', 'ctrl+x'],
11
+ DELETE: ['backspace', 'del'],
12
+ CLEAR_SELECTION: ['esc']
13
+ };
14
+
15
+ const isFormEvent = e => {
16
+ const t = e.target;
17
+ if (!t) {
18
+ return false;
19
+ }
20
+ if (t.form || /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) {
21
+ return true;
22
+ }
23
+ if (t instanceof HTMLElement && /write/.test(window.getComputedStyle(t).getPropertyValue('-webkit-user-modify'))) {
24
+ return true;
25
+ }
26
+ return false;
27
+ };
28
+
29
+ const HotkeyPlugin = () => {
30
+ return {
31
+ name: 'HotkeyPlugin',
32
+ deps: [],
33
+ init(ctx) {
34
+ const {
35
+ hotkey,
36
+ project,
37
+ logger,
38
+ designer
39
+ } = ctx;
40
+ hotkey.bind(HOTKEY_MAP.HISTORY_UNDO, () => {
41
+ const currentHistory = project.currentDocument?.history;
42
+ if (currentHistory?.isUndoable()) {
43
+ currentHistory?.back();
44
+ } else {
45
+ logger.log('No operations to undo');
46
+ }
47
+ });
48
+ hotkey.bind(HOTKEY_MAP.HISTORY_REDO, () => {
49
+ const currentHistory = project.currentDocument?.history;
50
+ if (currentHistory?.isRedoable()) {
51
+ currentHistory?.forward();
52
+ } else {
53
+ logger.log('No operations to redo');
54
+ }
55
+ });
56
+ hotkey.bind(HOTKEY_MAP.LOCK_UNLOCK, () => {
57
+ const selection = designer.selection;
58
+ const doc = project.currentDocument;
59
+ for (const nodeId of selection.selected) {
60
+ const node = doc?.getNode(nodeId);
61
+ if (node?.isLocked) {
62
+ node.lock(false);
63
+ logger.log('Lock');
64
+ } else {
65
+ node?.lock(true);
66
+ logger.log('Unlock');
67
+ }
68
+ }
69
+ selection.clear();
70
+ });
71
+ hotkey.bind(HOTKEY_MAP.SHOW_HIDE, () => {
72
+ const selection = designer.selection;
73
+ const doc = project.currentDocument;
74
+ for (const nodeId of selection.selected) {
75
+ const node = doc?.getNode(nodeId);
76
+ if (node?.isHidden) {
77
+ node.hide(false);
78
+ logger.log('Show');
79
+ } else {
80
+ node?.hide(true);
81
+ logger.log('Hide');
82
+ }
83
+ }
84
+ selection.clear();
85
+ });
86
+ hotkey.bind(HOTKEY_MAP.COPY, e => {
87
+ const doc = project.currentDocument;
88
+ if (isFormEvent(e) || !doc) {
89
+ return;
90
+ }
91
+ const selected = designer.selection.getTopNodes(false);
92
+ if (!selected || selected.length < 1) {
93
+ return;
94
+ }
95
+ const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE));
96
+ const data = {
97
+ type: 'NodeSchema',
98
+ componentsMap: {},
99
+ componentsTree
100
+ };
101
+ clipboard.setData(data);
102
+ });
103
+ hotkey.bind(HOTKEY_MAP.CUT, e => {
104
+ const doc = project.currentDocument;
105
+ if (isFormEvent(e) || !doc) {
106
+ return;
107
+ }
108
+ const selection = designer.selection;
109
+ const selected = selection.getTopNodes(false);
110
+ if (!selected || selected.length < 1) {
111
+ return;
112
+ }
113
+ const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE));
114
+ const data = {
115
+ type: 'NodeSchema',
116
+ componentsMap: {},
117
+ componentsTree
118
+ };
119
+ clipboard.setData(data);
120
+ for (const node of selected) {
121
+ node?.parent?.select();
122
+ node.remove();
123
+ }
124
+ selection.clear();
125
+ });
126
+ hotkey.bind(HOTKEY_MAP.PASTE, e => {
127
+ const doc = project.currentDocument;
128
+ const selection = designer.selection;
129
+ if (isFormEvent(e) || !doc) {
130
+ return;
131
+ }
132
+ clipboard.waitPasteData(e, ({
133
+ componentsTree
134
+ }) => {
135
+ if (componentsTree) {
136
+ const target = doc?.rootNode;
137
+ if (!target) {
138
+ return;
139
+ }
140
+ const nodes = insertChildren(target, componentsTree);
141
+ if (nodes) {
142
+ selection.selectAll(nodes.map(o => o.id));
143
+ }
144
+ }
145
+ });
146
+ });
147
+ hotkey.bind(HOTKEY_MAP.DELETE, e => {
148
+ const doc = project.currentDocument;
149
+ const selection = designer.selection;
150
+ if (isFormEvent(e) || !doc) {
151
+ return;
152
+ }
153
+ const nodes = selection.getTopNodes();
154
+ for (const node of nodes) {
155
+ node && doc?.removeNode(node);
156
+ }
157
+ selection.clear();
158
+ });
159
+ hotkey.bind(HOTKEY_MAP.CLEAR_SELECTION, e => {
160
+ const selection = designer.selection;
161
+ if (isFormEvent(e) || !selection) {
162
+ return;
163
+ }
164
+ selection.clear();
165
+ });
166
+ }
167
+ };
168
+ };
169
+
170
+ export { HotkeyPlugin as default };
171
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/const.ts","../../src/utils.ts","../../src/index.ts"],"sourcesContent":["export const HOTKEY_MAP = {\n /**\n * 历史记录回退\n */\n HISTORY_UNDO: ['command+z', 'ctrl+z'],\n\n /**\n * 历史记录前进\n */\n HISTORY_REDO: ['command+y', 'ctrl+y'],\n\n /**\n * 锁定/解锁\n */\n LOCK_UNLOCK: ['command+shift+l', 'ctrl+shift+l'],\n\n /**\n * 显示/隐藏\n */\n SHOW_HIDE: ['command+shift+h', 'ctrl+shift+h'],\n\n /**\n * 复制\n */\n COPY: ['command+c', 'ctrl+c'],\n\n /**\n * 粘贴\n */\n PASTE: ['command+v', 'ctrl+v'],\n\n /**\n * 剪切\n */\n CUT: ['command+x', 'ctrl+x'],\n\n /**\n * 删除\n */\n DELETE: ['backspace', 'del'],\n\n /**\n * 取消选中\n */\n CLEAR_SELECTION: ['esc'],\n}\n","/**\n * 检查事件是否由表单元素触发\n */\nexport const isFormEvent = (e: KeyboardEvent | MouseEvent) => {\n const t = e.target as HTMLFormElement\n if (!t) {\n return false\n }\n\n if (t.form || /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) {\n return true\n }\n if (t instanceof HTMLElement && /write/.test(window.getComputedStyle(t).getPropertyValue('-webkit-user-modify'))) {\n return true\n }\n return false\n}\n","import { type PluginCreator, TRANSFORM_STAGE, clipboard, insertChildren } from '@easy-editor/core'\nimport { HOTKEY_MAP } from './const'\nimport { isFormEvent } from './utils'\n\nconst HotkeyPlugin: PluginCreator = () => {\n return {\n name: 'HotkeyPlugin',\n deps: [],\n init(ctx) {\n const { hotkey, project, logger, designer } = ctx\n\n hotkey.bind(HOTKEY_MAP.HISTORY_UNDO, () => {\n const currentHistory = project.currentDocument?.history\n if (currentHistory?.isUndoable()) {\n currentHistory?.back()\n } else {\n logger.log('No operations to undo')\n }\n })\n\n hotkey.bind(HOTKEY_MAP.HISTORY_REDO, () => {\n const currentHistory = project.currentDocument?.history\n if (currentHistory?.isRedoable()) {\n currentHistory?.forward()\n } else {\n logger.log('No operations to redo')\n }\n })\n\n hotkey.bind(HOTKEY_MAP.LOCK_UNLOCK, () => {\n const selection = designer.selection\n const doc = project.currentDocument\n\n for (const nodeId of selection.selected) {\n const node = doc?.getNode(nodeId)\n if (node?.isLocked) {\n node.lock(false)\n logger.log('Lock')\n } else {\n node?.lock(true)\n logger.log('Unlock')\n }\n }\n\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.SHOW_HIDE, () => {\n const selection = designer.selection\n const doc = project.currentDocument\n\n for (const nodeId of selection.selected) {\n const node = doc?.getNode(nodeId)\n if (node?.isHidden) {\n node.hide(false)\n logger.log('Show')\n } else {\n node?.hide(true)\n logger.log('Hide')\n }\n }\n\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.COPY, e => {\n const doc = project.currentDocument\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const selected = designer.selection.getTopNodes(false)\n if (!selected || selected.length < 1) {\n return\n }\n\n const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE))\n const data = { type: 'NodeSchema', componentsMap: {}, componentsTree }\n\n clipboard.setData(data)\n })\n\n hotkey.bind(HOTKEY_MAP.CUT, e => {\n const doc = project.currentDocument\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const selection = designer.selection\n const selected = selection.getTopNodes(false)\n if (!selected || selected.length < 1) {\n return\n }\n\n const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE))\n const data = { type: 'NodeSchema', componentsMap: {}, componentsTree }\n\n clipboard.setData(data)\n\n for (const node of selected) {\n node?.parent?.select()\n node.remove()\n }\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.PASTE, e => {\n const doc = project.currentDocument\n const selection = designer.selection\n\n if (isFormEvent(e) || !doc) {\n return\n }\n\n clipboard.waitPasteData(e, ({ componentsTree }) => {\n if (componentsTree) {\n const target = doc?.rootNode\n\n if (!target) {\n return\n }\n\n const nodes = insertChildren(target, componentsTree)\n if (nodes) {\n selection.selectAll(nodes.map(o => o.id))\n }\n }\n })\n })\n\n hotkey.bind(HOTKEY_MAP.DELETE, e => {\n const doc = project.currentDocument\n const selection = designer.selection\n\n if (isFormEvent(e) || !doc) {\n return\n }\n\n const nodes = selection.getTopNodes()\n for (const node of nodes) {\n node && doc?.removeNode(node)\n }\n selection.clear()\n })\n\n hotkey.bind(HOTKEY_MAP.CLEAR_SELECTION, e => {\n const selection = designer.selection\n\n if (isFormEvent(e) || !selection) {\n return\n }\n\n selection.clear()\n })\n },\n }\n}\n\nexport default HotkeyPlugin\n"],"names":["HOTKEY_MAP","HISTORY_UNDO","HISTORY_REDO","LOCK_UNLOCK","SHOW_HIDE","COPY","PASTE","CUT","DELETE","CLEAR_SELECTION","isFormEvent","e","t","target","form","test","tagName","HTMLElement","window","getComputedStyle","getPropertyValue","HotkeyPlugin","name","deps","init","ctx","hotkey","project","logger","designer","bind","currentHistory","currentDocument","history","isUndoable","back","log","isRedoable","forward","selection","doc","nodeId","selected","node","getNode","isLocked","lock","clear","isHidden","hide","getTopNodes","length","componentsTree","map","item","export","TRANSFORM_STAGE","CLONE","data","type","componentsMap","clipboard","setData","parent","select","remove","waitPasteData","rootNode","nodes","insertChildren","selectAll","o","id","removeNode"],"mappings":";;AAAO,MAAMA,UAAU,GAAG;AAIxBC,EAAAA,YAAY,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AAKrCC,EAAAA,YAAY,EAAE,CAAC,WAAW,EAAE,QAAQ,CAAC;AA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;"}
@@ -0,0 +1,3 @@
1
+ import { type PluginCreator } from '@easy-editor/core';
2
+ declare const HotkeyPlugin: PluginCreator;
3
+ export default HotkeyPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,170 @@
1
+ import { TRANSFORM_STAGE, clipboard, insertChildren } from '@easy-editor/core';
2
+
3
+ const HOTKEY_MAP = {
4
+ HISTORY_UNDO: ['command+z', 'ctrl+z'],
5
+ HISTORY_REDO: ['command+y', 'ctrl+y'],
6
+ LOCK_UNLOCK: ['command+shift+l', 'ctrl+shift+l'],
7
+ SHOW_HIDE: ['command+shift+h', 'ctrl+shift+h'],
8
+ COPY: ['command+c', 'ctrl+c'],
9
+ PASTE: ['command+v', 'ctrl+v'],
10
+ CUT: ['command+x', 'ctrl+x'],
11
+ DELETE: ['backspace', 'del'],
12
+ CLEAR_SELECTION: ['esc']
13
+ };
14
+
15
+ const isFormEvent = e => {
16
+ const t = e.target;
17
+ if (!t) {
18
+ return false;
19
+ }
20
+ if (t.form || /^(INPUT|SELECT|TEXTAREA)$/.test(t.tagName)) {
21
+ return true;
22
+ }
23
+ if (t instanceof HTMLElement && /write/.test(window.getComputedStyle(t).getPropertyValue('-webkit-user-modify'))) {
24
+ return true;
25
+ }
26
+ return false;
27
+ };
28
+
29
+ const HotkeyPlugin = () => {
30
+ return {
31
+ name: 'HotkeyPlugin',
32
+ deps: [],
33
+ init(ctx) {
34
+ const {
35
+ hotkey,
36
+ project,
37
+ logger,
38
+ designer
39
+ } = ctx;
40
+ hotkey.bind(HOTKEY_MAP.HISTORY_UNDO, () => {
41
+ const currentHistory = project.currentDocument?.history;
42
+ if (currentHistory?.isUndoable()) {
43
+ currentHistory?.back();
44
+ } else {
45
+ logger.log('No operations to undo');
46
+ }
47
+ });
48
+ hotkey.bind(HOTKEY_MAP.HISTORY_REDO, () => {
49
+ const currentHistory = project.currentDocument?.history;
50
+ if (currentHistory?.isRedoable()) {
51
+ currentHistory?.forward();
52
+ } else {
53
+ logger.log('No operations to redo');
54
+ }
55
+ });
56
+ hotkey.bind(HOTKEY_MAP.LOCK_UNLOCK, () => {
57
+ const selection = designer.selection;
58
+ const doc = project.currentDocument;
59
+ for (const nodeId of selection.selected) {
60
+ const node = doc?.getNode(nodeId);
61
+ if (node?.isLocked) {
62
+ node.lock(false);
63
+ logger.log('Lock');
64
+ } else {
65
+ node?.lock(true);
66
+ logger.log('Unlock');
67
+ }
68
+ }
69
+ selection.clear();
70
+ });
71
+ hotkey.bind(HOTKEY_MAP.SHOW_HIDE, () => {
72
+ const selection = designer.selection;
73
+ const doc = project.currentDocument;
74
+ for (const nodeId of selection.selected) {
75
+ const node = doc?.getNode(nodeId);
76
+ if (node?.isHidden) {
77
+ node.hide(false);
78
+ logger.log('Show');
79
+ } else {
80
+ node?.hide(true);
81
+ logger.log('Hide');
82
+ }
83
+ }
84
+ selection.clear();
85
+ });
86
+ hotkey.bind(HOTKEY_MAP.COPY, e => {
87
+ const doc = project.currentDocument;
88
+ if (isFormEvent(e) || !doc) {
89
+ return;
90
+ }
91
+ const selected = designer.selection.getTopNodes(false);
92
+ if (!selected || selected.length < 1) {
93
+ return;
94
+ }
95
+ const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE));
96
+ const data = {
97
+ type: 'NodeSchema',
98
+ componentsMap: {},
99
+ componentsTree
100
+ };
101
+ clipboard.setData(data);
102
+ });
103
+ hotkey.bind(HOTKEY_MAP.CUT, e => {
104
+ const doc = project.currentDocument;
105
+ if (isFormEvent(e) || !doc) {
106
+ return;
107
+ }
108
+ const selection = designer.selection;
109
+ const selected = selection.getTopNodes(false);
110
+ if (!selected || selected.length < 1) {
111
+ return;
112
+ }
113
+ const componentsTree = selected.map(item => item?.export(TRANSFORM_STAGE.CLONE));
114
+ const data = {
115
+ type: 'NodeSchema',
116
+ componentsMap: {},
117
+ componentsTree
118
+ };
119
+ clipboard.setData(data);
120
+ for (const node of selected) {
121
+ node?.parent?.select();
122
+ node.remove();
123
+ }
124
+ selection.clear();
125
+ });
126
+ hotkey.bind(HOTKEY_MAP.PASTE, e => {
127
+ const doc = project.currentDocument;
128
+ const selection = designer.selection;
129
+ if (isFormEvent(e) || !doc) {
130
+ return;
131
+ }
132
+ clipboard.waitPasteData(e, ({
133
+ componentsTree
134
+ }) => {
135
+ if (componentsTree) {
136
+ const target = doc?.rootNode;
137
+ if (!target) {
138
+ return;
139
+ }
140
+ const nodes = insertChildren(target, componentsTree);
141
+ if (nodes) {
142
+ selection.selectAll(nodes.map(o => o.id));
143
+ }
144
+ }
145
+ });
146
+ });
147
+ hotkey.bind(HOTKEY_MAP.DELETE, e => {
148
+ const doc = project.currentDocument;
149
+ const selection = designer.selection;
150
+ if (isFormEvent(e) || !doc) {
151
+ return;
152
+ }
153
+ const nodes = selection.getTopNodes();
154
+ for (const node of nodes) {
155
+ node && doc?.removeNode(node);
156
+ }
157
+ selection.clear();
158
+ });
159
+ hotkey.bind(HOTKEY_MAP.CLEAR_SELECTION, e => {
160
+ const selection = designer.selection;
161
+ if (isFormEvent(e) || !selection) {
162
+ return;
163
+ }
164
+ selection.clear();
165
+ });
166
+ }
167
+ };
168
+ };
169
+
170
+ export { HotkeyPlugin as default };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 检查事件是否由表单元素触发
3
+ */
4
+ export declare const isFormEvent: (e: KeyboardEvent | MouseEvent) => boolean;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@easy-editor/plugin-hotkey",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "Hotkey plugin for EasyEditor.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "files": [
8
+ "dist",
9
+ "LICENSE",
10
+ "README.md"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "homepage": "https://github.com/Easy-Editor/EasyEditor",
16
+ "license": "MIT",
17
+ "author": "JinSo <kimjinso@qq.com>",
18
+ "keywords": [
19
+ "@easy-editor",
20
+ "easyeditor",
21
+ "low-code",
22
+ "editor",
23
+ "engine",
24
+ "plugin",
25
+ "dashboard"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/Easy-Editor/EasyEditor"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/Easy-Editor/EasyEditor/issues"
33
+ },
34
+ "peerDependencies": {
35
+ "@easy-editor/core": "0.0.3-alpha.2"
36
+ },
37
+ "scripts": {
38
+ "dev": "deno run --watch ./src/index.ts",
39
+ "format": "biome format --write .",
40
+ "lint": "biome check .",
41
+ "build": "npm-run-all -nl build:*",
42
+ "build:clean": "rimraf dist/",
43
+ "build:js": "rollup -c",
44
+ "types": "npm-run-all -nl types:*",
45
+ "types:src": "tsc --project tsconfig.build.json",
46
+ "test-types": "tsc --project tsconfig.test.json"
47
+ },
48
+ "types": "dist/index.d.ts",
49
+ "typings": "dist/index.d.ts",
50
+ "module": "dist/index.js",
51
+ "exports": {
52
+ ".": {
53
+ "import": "./dist/index.js",
54
+ "require": "./dist/cjs/index.js",
55
+ "default": "./dist/index.js"
56
+ }
57
+ }
58
+ }