@tmagic/editor 1.3.0-alpha.6 → 1.3.0-alpha.7

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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue'), require('serialize-javascript'), require('lodash-es'), require('@tmagic/design'), require('@tmagic/schema'), require('@element-plus/icons-vue'), require('@tmagic/form'), require('@tmagic/table'), require('monaco-editor'), require('gesto'), require('@tmagic/utils'), require('keycon'), require('@tmagic/stage'), require('events'), require('@tmagic/core')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'vue', 'serialize-javascript', 'lodash-es', '@tmagic/design', '@tmagic/schema', '@element-plus/icons-vue', '@tmagic/form', '@tmagic/table', 'monaco-editor', 'gesto', '@tmagic/utils', 'keycon', '@tmagic/stage', 'events', '@tmagic/core'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TMagicEditor = {}, global.Vue, global.serialize, global.lodashEs, global.design, global.schema, global.iconsVue, global.form, global.table, global.monaco, global.Gesto, global.utils, global.KeyController, global.StageCore, global.events, global.core));
5
- })(this, (function (exports, vue, serialize, lodashEs, design, schema, iconsVue, form, table, monaco, Gesto, utils, KeyController, StageCore, events, core) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue'), require('serialize-javascript'), require('lodash-es'), require('@tmagic/design'), require('@tmagic/schema'), require('@element-plus/icons-vue'), require('@tmagic/form'), require('@tmagic/table'), require('monaco-editor'), require('gesto'), require('@tmagic/utils'), require('@tmagic/stage'), require('events'), require('@tmagic/core'), require('keycon')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'vue', 'serialize-javascript', 'lodash-es', '@tmagic/design', '@tmagic/schema', '@element-plus/icons-vue', '@tmagic/form', '@tmagic/table', 'monaco-editor', 'gesto', '@tmagic/utils', '@tmagic/stage', 'events', '@tmagic/core', 'keycon'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TMagicEditor = {}, global.Vue, global.serialize, global.lodashEs, global.design, global.schema, global.iconsVue, global.form, global.table, global.monaco, global.Gesto, global.utils, global.StageCore, global.events, global.core, global.KeyController));
5
+ })(this, (function (exports, vue, serialize, lodashEs, design, schema, iconsVue, form, table, monaco, Gesto, utils, StageCore, events, core, KeyController) { 'use strict';
6
6
 
7
7
  function _interopNamespaceDefault(e) {
8
8
  const n = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
@@ -56,6 +56,12 @@
56
56
  }
57
57
  });
58
58
 
59
+ let $TMAGIC_EDITOR = {};
60
+ const setConfig = (option) => {
61
+ $TMAGIC_EDITOR = option;
62
+ };
63
+ const getConfig = (key) => $TMAGIC_EDITOR[key];
64
+
59
65
  const _sfc_main$D = /* @__PURE__ */ vue.defineComponent({
60
66
  ...{
61
67
  name: "MEditorCodeLink"
@@ -111,7 +117,8 @@
111
117
  if (!props.name || !props.model)
112
118
  return;
113
119
  try {
114
- props.model[props.name] = eval(`(${v[props.name]})`);
120
+ const parseDSL = getConfig("parseDSL");
121
+ props.model[props.name] = parseDSL(`(${v[props.name]})`);
115
122
  emit("change", props.model[props.name]);
116
123
  } catch (e) {
117
124
  console.error(e);
@@ -1356,12 +1363,6 @@
1356
1363
  }
1357
1364
  });
1358
1365
 
1359
- let $TMAGIC_EDITOR = {};
1360
- const setConfig = (option) => {
1361
- $TMAGIC_EDITOR = option;
1362
- };
1363
- const getConfig = (key) => $TMAGIC_EDITOR[key];
1364
-
1365
1366
  const _sfc_main$t = /* @__PURE__ */ vue.defineComponent({
1366
1367
  ...{
1367
1368
  name: "MEditorResizer"
@@ -1777,6 +1778,28 @@
1777
1778
  return CodeDeleteErrorType2;
1778
1779
  })(CodeDeleteErrorType || {});
1779
1780
  const CODE_DRAFT_STORAGE_KEY = "magicCodeDraft";
1781
+ var KeyBindingCommand = /* @__PURE__ */ ((KeyBindingCommand2) => {
1782
+ KeyBindingCommand2["COPY_NODE"] = "tmagic-system-copy-node";
1783
+ KeyBindingCommand2["PASTE_NODE"] = "tmagic-system-paste-node";
1784
+ KeyBindingCommand2["DELETE_NODE"] = "tmagic-system-delete-node";
1785
+ KeyBindingCommand2["CUT_NODE"] = "tmagic-system-cut-node";
1786
+ KeyBindingCommand2["UNDO"] = "tmagic-system-undo";
1787
+ KeyBindingCommand2["REDO"] = "tmagic-system-redo";
1788
+ KeyBindingCommand2["ZOOM_IN"] = "tmagic-system-zoom-in";
1789
+ KeyBindingCommand2["ZOOM_OUT"] = "tmagic-system-zoom-out";
1790
+ KeyBindingCommand2["ZOOM_RESET"] = "tmagic-system-zoom-reset";
1791
+ KeyBindingCommand2["ZOOM_FIT"] = "tmagic-system-zoom-fit";
1792
+ KeyBindingCommand2["MOVE_UP_1"] = "tmagic-system-move-up-1";
1793
+ KeyBindingCommand2["MOVE_DOWN_1"] = "tmagic-system-move-down-1";
1794
+ KeyBindingCommand2["MOVE_LEFT_1"] = "tmagic-system-move-left-1";
1795
+ KeyBindingCommand2["MOVE_RIGHT_1"] = "tmagic-system-move-right-1";
1796
+ KeyBindingCommand2["MOVE_UP_10"] = "tmagic-system-move-up-10";
1797
+ KeyBindingCommand2["MOVE_DOWN_10"] = "tmagic-system-move-down-10";
1798
+ KeyBindingCommand2["MOVE_LEFT_10"] = "tmagic-system-move-left-10";
1799
+ KeyBindingCommand2["MOVE_RIGHT_10"] = "tmagic-system-move-right-10";
1800
+ KeyBindingCommand2["SWITCH_NODE"] = "tmagic-system-switch-node";
1801
+ return KeyBindingCommand2;
1802
+ })(KeyBindingCommand || {});
1780
1803
 
1781
1804
  const COPY_STORAGE_KEY = "$MagicEditorCopyData";
1782
1805
  const getPageList = (app) => {
@@ -2223,8 +2246,8 @@
2223
2246
  * 清理,支持storageService.usePlugin
2224
2247
  */
2225
2248
  async clear() {
2226
- const storage2 = await this.getStorage();
2227
- storage2.clear();
2249
+ const storage = await this.getStorage();
2250
+ storage.clear();
2228
2251
  }
2229
2252
  /**
2230
2253
  * 获取存储项,支持storageService.usePlugin
@@ -2238,7 +2261,7 @@
2238
2261
  return null;
2239
2262
  switch (protocol) {
2240
2263
  case "object" /* OBJECT */:
2241
- return eval(`(${item})`);
2264
+ return getConfig("parseDSL")(`(${item})`);
2242
2265
  case "json" /* JSON */:
2243
2266
  return JSON.parse(item);
2244
2267
  case "number" /* NUMBER */:
@@ -2253,49 +2276,49 @@
2253
2276
  * 获取指定索引位置的key
2254
2277
  */
2255
2278
  async key(index) {
2256
- const storage2 = await this.getStorage();
2257
- return storage2.key(index);
2279
+ const storage = await this.getStorage();
2280
+ return storage.key(index);
2258
2281
  }
2259
2282
  /**
2260
2283
  * 移除存储项,支持storageService.usePlugin
2261
2284
  */
2262
- async removeItem(key2, options2 = {}) {
2263
- const [storage2, namespace2] = await Promise.all([this.getStorage(), this.getNamespace()]);
2264
- storage2.removeItem(`${options2.namespace || namespace2}:${key2}`);
2285
+ async removeItem(key, options = {}) {
2286
+ const [storage, namespace] = await Promise.all([this.getStorage(), this.getNamespace()]);
2287
+ storage.removeItem(`${options.namespace || namespace}:${key}`);
2265
2288
  }
2266
2289
  /**
2267
2290
  * 设置存储项,支持storageService.usePlugin
2268
2291
  */
2269
- async setItem(key2, value, options2 = {}) {
2270
- const [storage2, namespace2] = await Promise.all([this.getStorage(), this.getNamespace()]);
2271
- let item2 = value;
2272
- const protocol2 = options2.protocol ? `${options2.protocol}:` : "";
2292
+ async setItem(key, value, options = {}) {
2293
+ const [storage, namespace] = await Promise.all([this.getStorage(), this.getNamespace()]);
2294
+ let item = value;
2295
+ const protocol = options.protocol ? `${options.protocol}:` : "";
2273
2296
  if (typeof value === "string" /* STRING */ || typeof value === "number" /* NUMBER */) {
2274
- item2 = `${protocol2}${value}`;
2297
+ item = `${protocol}${value}`;
2275
2298
  } else {
2276
- item2 = `${protocol2}${serialize(value)}`;
2299
+ item = `${protocol}${serialize(value)}`;
2277
2300
  }
2278
- storage2.setItem(`${options2.namespace || namespace2}:${key2}`, item2);
2301
+ storage.setItem(`${options.namespace || namespace}:${key}`, item);
2279
2302
  }
2280
2303
  destroy() {
2281
2304
  this.removeAllListeners();
2282
2305
  this.removeAllPlugins();
2283
2306
  }
2284
2307
  getValueAndProtocol(value) {
2285
- let protocol2 = "";
2308
+ let protocol = "";
2286
2309
  if (value === null) {
2287
2310
  return {
2288
2311
  item: value,
2289
- protocol: protocol2
2312
+ protocol
2290
2313
  };
2291
2314
  }
2292
- const item2 = value.replace(new RegExp(`^(${Object.values(Protocol).join("|")})(:)(.+)`), (_$0, $1, _$2, $3) => {
2293
- protocol2 = $1;
2315
+ const item = value.replace(new RegExp(`^(${Object.values(Protocol).join("|")})(:)(.+)`), (_$0, $1, _$2, $3) => {
2316
+ protocol = $1;
2294
2317
  return $3;
2295
2318
  });
2296
2319
  return {
2297
- protocol: protocol2,
2298
- item: item2
2320
+ protocol,
2321
+ item
2299
2322
  };
2300
2323
  }
2301
2324
  }
@@ -3497,7 +3520,8 @@
3497
3520
  };
3498
3521
  const saveCode = (value) => {
3499
3522
  try {
3500
- editorService?.set("root", eval(value));
3523
+ const parseDSL = getConfig("parseDSL");
3524
+ editorService?.set("root", parseDSL(value));
3501
3525
  } catch (e) {
3502
3526
  console.error(e);
3503
3527
  }
@@ -4227,6 +4251,7 @@
4227
4251
  },
4228
4252
  setup(__props, { expose: __expose }) {
4229
4253
  const props = __props;
4254
+ vue.provide("mForm", {});
4230
4255
  const defaultParamColConfig = {
4231
4256
  type: "row",
4232
4257
  label: "参数类型",
@@ -4295,29 +4320,29 @@
4295
4320
  initTableModel();
4296
4321
  const beforeSave = (codeValue) => {
4297
4322
  try {
4298
- eval(codeValue);
4323
+ getConfig("parseDSL")(codeValue);
4299
4324
  return true;
4300
4325
  } catch (e) {
4301
4326
  design.tMagicMessage.error(e.stack);
4302
4327
  return false;
4303
4328
  }
4304
4329
  };
4305
- const saveCode = async (codeValue2) => {
4330
+ const saveCode = async (codeValue) => {
4306
4331
  if (!props.editable)
4307
4332
  return;
4308
- evalRes.value = beforeSave(codeValue2);
4333
+ evalRes.value = beforeSave(codeValue);
4309
4334
  if (evalRes.value) {
4310
4335
  await services?.codeBlockService.setCodeDslById(props.id, {
4311
4336
  name: codeName.value,
4312
- content: codeValue2,
4337
+ content: codeValue,
4313
4338
  params: tableModel.value?.params || []
4314
4339
  });
4315
4340
  design.tMagicMessage.success("代码成功保存到本地");
4316
4341
  services?.codeBlockService.removeCodeDraft(props.id);
4317
4342
  }
4318
4343
  };
4319
- const saveAndClose = async (codeValue2) => {
4320
- await saveCode(codeValue2);
4344
+ const saveAndClose = async (codeValue) => {
4345
+ await saveCode(codeValue);
4321
4346
  if (evalRes.value) {
4322
4347
  close();
4323
4348
  }
@@ -5086,6 +5111,7 @@
5086
5111
  left: "0",
5087
5112
  top: "0"
5088
5113
  });
5114
+ const contains = (el) => menu.value?.contains(el) || subMenu.value?.contains(el);
5089
5115
  const hide = () => {
5090
5116
  if (!visible.value)
5091
5117
  return;
@@ -5098,7 +5124,7 @@
5098
5124
  if (!visible.value || !target) {
5099
5125
  return;
5100
5126
  }
5101
- if (menu.value?.contains(target) || subMenu.value?.$el?.contains(target)) {
5127
+ if (contains(target)) {
5102
5128
  return;
5103
5129
  }
5104
5130
  hide();
@@ -5125,13 +5151,15 @@
5125
5151
  if (typeof item !== "object" || !menuItem.items?.length) {
5126
5152
  return;
5127
5153
  }
5128
- subMenuData.value = menuItem.items;
5129
- if (menu.value) {
5130
- subMenu.value.show({
5131
- clientX: menu.value.offsetLeft + menu.value.clientWidth,
5132
- clientY: menu.value.offsetTop
5133
- });
5134
- }
5154
+ subMenuData.value = menuItem.items || [];
5155
+ setTimeout(() => {
5156
+ if (menu.value) {
5157
+ subMenu.value?.show({
5158
+ clientX: menu.value.offsetLeft + menu.value.clientWidth,
5159
+ clientY: menu.value.offsetTop
5160
+ });
5161
+ }
5162
+ }, 0);
5135
5163
  };
5136
5164
  vue.onMounted(() => {
5137
5165
  if (props.isSubMenu)
@@ -5144,8 +5172,10 @@
5144
5172
  globalThis.removeEventListener("mousedown", hideHandler, true);
5145
5173
  });
5146
5174
  __expose({
5175
+ menu,
5147
5176
  hide,
5148
- show
5177
+ show,
5178
+ contains
5149
5179
  });
5150
5180
  return (_ctx, _cache) => {
5151
5181
  const _component_content_menu = vue.resolveComponent("content-menu", true);
@@ -5168,14 +5198,15 @@
5168
5198
  }), 128))
5169
5199
  ]),
5170
5200
  (vue.openBlock(), vue.createBlock(vue.Teleport, { to: "body" }, [
5171
- vue.createVNode(_component_content_menu, {
5201
+ subMenuData.value.length ? (vue.openBlock(), vue.createBlock(_component_content_menu, {
5202
+ key: 0,
5172
5203
  class: "sub-menu",
5173
5204
  ref_key: "subMenu",
5174
5205
  ref: subMenu,
5175
5206
  "menu-data": subMenuData.value,
5176
5207
  "is-sub-menu": true,
5177
5208
  onHide: hide
5178
- }, null, 8, ["menu-data"])
5209
+ }, null, 8, ["menu-data"])) : vue.createCommentVNode("", true)
5179
5210
  ]))
5180
5211
  ], 4)), [
5181
5212
  [vue.vShow, visible.value]
@@ -5435,38 +5466,61 @@
5435
5466
  const windowBlurHandler = () => {
5436
5467
  isCtrlKeyDown.value = false;
5437
5468
  };
5438
- const globalKeyupHandler = () => {
5439
- if (document.activeElement !== tree.value?.$el) {
5469
+ keybindingService?.registeCommand("layer-panel-not-ctrl-keydown", (e) => {
5470
+ if (e.key !== keybindingService.ctrlKey) {
5440
5471
  isCtrlKeyDown.value = false;
5441
5472
  }
5442
- };
5443
- let keycon;
5444
- vue.onMounted(() => {
5445
- editorService?.on("remove", editorServiceRemoveHandler);
5473
+ });
5474
+ keybindingService?.registeCommand("layer-panel-ctrl-keydown", () => {
5475
+ isCtrlKeyDown.value = true;
5476
+ });
5477
+ keybindingService?.registeCommand("layer-panel-ctrl-keyup", () => {
5478
+ isCtrlKeyDown.value = false;
5479
+ });
5480
+ keybindingService?.registeCommand("layer-panel-global-keydwon", () => {
5481
+ if (!tree.value?.$el.contains(document.activeElement)) {
5482
+ isCtrlKeyDown.value = false;
5483
+ }
5484
+ });
5485
+ keybindingService?.registe([
5486
+ {
5487
+ command: "layer-panel-not-ctrl-keydown",
5488
+ when: [["layer-panel", "keydown"]]
5489
+ },
5490
+ {
5491
+ command: "layer-panel-ctrl-keydown",
5492
+ keybinding: "ctrl",
5493
+ when: [["layer-panel", "keydown"]]
5494
+ },
5495
+ {
5496
+ command: "layer-panel-ctrl-keyup",
5497
+ keybinding: "ctrl",
5498
+ when: [["layer-panel", "keyup"]]
5499
+ },
5500
+ {
5501
+ command: "layer-panel-global-keydwon",
5502
+ keybinding: "ctrl",
5503
+ when: [["global", "keydown"]]
5504
+ }
5505
+ ]);
5506
+ vue.watch(tree, () => {
5446
5507
  if (tree.value?.$el) {
5447
- keybindingService?.on("keyup", globalKeyupHandler);
5448
- keycon = new KeyController(tree.value.$el);
5449
- const isMac = /mac os x/.test(navigator.userAgent.toLowerCase());
5450
- const ctrl = isMac ? "meta" : "ctrl";
5451
- keycon.keydown((e) => {
5452
- if (e.key !== ctrl) {
5453
- isCtrlKeyDown.value = false;
5454
- }
5455
- }).keydown(ctrl, () => {
5456
- isCtrlKeyDown.value = true;
5457
- }).keyup(ctrl, () => {
5458
- isCtrlKeyDown.value = false;
5459
- });
5508
+ keybindingService?.registeEl("layer-panel", tree.value.$el);
5460
5509
  tree.value.$el.addEventListener("blur", windowBlurHandler);
5510
+ } else {
5511
+ keybindingService?.unregisteEl("layer-panel");
5461
5512
  }
5513
+ });
5514
+ vue.onMounted(() => {
5515
+ editorService?.on("remove", editorServiceRemoveHandler);
5462
5516
  globalThis.addEventListener("blur", windowBlurHandler);
5463
5517
  });
5518
+ vue.onBeforeUnmount(() => {
5519
+ tree.value?.$el.removeEventListener("blur", windowBlurHandler);
5520
+ });
5464
5521
  vue.onUnmounted(() => {
5465
- keycon.destroy();
5466
5522
  editorService?.off("remove", editorServiceRemoveHandler);
5467
- keybindingService?.off("keyup", globalKeyupHandler);
5468
5523
  globalThis.removeEventListener("blur", windowBlurHandler);
5469
- tree.value?.$el.removeEventListener("blur", windowBlurHandler);
5470
5524
  });
5471
5525
  const highlightHandler = lodashEs.throttle((data) => {
5472
5526
  highlight(data);
@@ -6510,6 +6564,9 @@
6510
6564
  if (!(stageOptions?.runtimeUrl || stageOptions?.render) || !root.value)
6511
6565
  return;
6512
6566
  stage = useStage(stageOptions);
6567
+ stage.on("select", () => {
6568
+ stageWrap.value?.container?.focus();
6569
+ });
6513
6570
  services?.editorService.set("stage", vue.markRaw(stage));
6514
6571
  stage?.mount(stageContainer.value);
6515
6572
  if (!node.value?.id)
@@ -6550,21 +6607,25 @@
6550
6607
  }
6551
6608
  });
6552
6609
  vue.onMounted(() => {
6553
- stageWrap.value?.container && resizeObserver.observe(stageWrap.value.container);
6610
+ if (stageWrap.value?.container) {
6611
+ resizeObserver.observe(stageWrap.value.container);
6612
+ services?.keybindingService.registeEl("stage", stageWrap.value.container);
6613
+ }
6554
6614
  });
6555
6615
  vue.onUnmounted(() => {
6556
6616
  stage?.destroy();
6557
6617
  resizeObserver.disconnect();
6558
6618
  services?.editorService.set("stage", null);
6619
+ services?.keybindingService.unregisteEl("stage");
6559
6620
  });
6560
- const contextmenuHandler = (e2) => {
6561
- e2.preventDefault();
6562
- menu.value?.show(e2);
6621
+ const contextmenuHandler = (e) => {
6622
+ e.preventDefault();
6623
+ menu.value?.show(e);
6563
6624
  };
6564
- const dragoverHandler = (e2) => {
6565
- e2.preventDefault();
6566
- if (e2.dataTransfer) {
6567
- e2.dataTransfer.dropEffect = "move";
6625
+ const dragoverHandler = (e) => {
6626
+ e.preventDefault();
6627
+ if (e.dataTransfer) {
6628
+ e.dataTransfer.dropEffect = "move";
6568
6629
  }
6569
6630
  };
6570
6631
  const dropHandler = async (e) => {
@@ -6576,7 +6637,8 @@
6576
6637
  parent = services?.editorService.getNodeById(parentEl.id, false);
6577
6638
  }
6578
6639
  if (e.dataTransfer && parent && stageContainer.value && stage) {
6579
- const config = eval(`(${e.dataTransfer.getData("data")})`);
6640
+ const parseDSL = getConfig("parseDSL");
6641
+ const config = parseDSL(`(${e.dataTransfer.getData("data")})`);
6580
6642
  const layout = await services?.editorService.getLayout(parent);
6581
6643
  const containerRect = stageContainer.value.getBoundingClientRect();
6582
6644
  const { scrollTop, scrollLeft } = stage.mask;
@@ -6609,6 +6671,7 @@
6609
6671
  class: "m-editor-stage",
6610
6672
  ref_key: "stageWrap",
6611
6673
  ref: stageWrap,
6674
+ tabindex: "-1",
6612
6675
  width: stageRect.value?.width,
6613
6676
  height: stageRect.value?.height,
6614
6677
  "wrap-width": stageContainerRect.value?.width,
@@ -6644,10 +6707,7 @@
6644
6707
  }
6645
6708
  });
6646
6709
 
6647
- const _hoisted_1 = {
6648
- class: "m-editor-workspace",
6649
- tabindex: "-1"
6650
- };
6710
+ const _hoisted_1 = { class: "m-editor-workspace" };
6651
6711
  const _sfc_main$1 = /* @__PURE__ */ vue.defineComponent({
6652
6712
  ...{
6653
6713
  name: "MEditorWorkspace"
@@ -6707,8 +6767,8 @@
6707
6767
  * @param {CodeBlockDSL} codeDsl 代码DSL
6708
6768
  * @returns {void}
6709
6769
  */
6710
- async setCodeDsl(codeDsl2) {
6711
- this.state.codeDsl = codeDsl2;
6770
+ async setCodeDsl(codeDsl) {
6771
+ this.state.codeDsl = codeDsl;
6712
6772
  this.emit("code-dsl-change", this.state.codeDsl);
6713
6773
  }
6714
6774
  /**
@@ -6725,13 +6785,13 @@
6725
6785
  * @param {Id} id 代码块id
6726
6786
  * @returns {CodeBlockContent | null}
6727
6787
  */
6728
- getCodeContentById(id2) {
6729
- if (!id2)
6788
+ getCodeContentById(id) {
6789
+ if (!id)
6730
6790
  return null;
6731
6791
  const totalCodeDsl = this.getCodeDsl();
6732
6792
  if (!totalCodeDsl)
6733
6793
  return null;
6734
- return totalCodeDsl[id2] ?? null;
6794
+ return totalCodeDsl[id] ?? null;
6735
6795
  }
6736
6796
  /**
6737
6797
  * 设置代码块ID和代码内容到源dsl
@@ -6746,7 +6806,8 @@
6746
6806
  }
6747
6807
  const codeConfigProcessed = codeConfig;
6748
6808
  if (codeConfig.content) {
6749
- codeConfigProcessed.content = eval(codeConfig.content);
6809
+ const parseDSL = getConfig("parseDSL");
6810
+ codeConfigProcessed.content = parseDSL(codeConfig.content);
6750
6811
  }
6751
6812
  const existContent = codeDsl[id] || {};
6752
6813
  codeDsl[id] = {
@@ -6761,8 +6822,8 @@
6761
6822
  * @returns {CodeBlockDSL}
6762
6823
  */
6763
6824
  getCodeDslByIds(ids) {
6764
- const codeDsl2 = this.getCodeDsl();
6765
- return lodashEs.pick(codeDsl2, ids);
6825
+ const codeDsl = this.getCodeDsl();
6826
+ return lodashEs.pick(codeDsl, ids);
6766
6827
  }
6767
6828
  /**
6768
6829
  * 设置代码编辑面板展示状态
@@ -6785,10 +6846,10 @@
6785
6846
  * @param {Id} id 代码块id
6786
6847
  * @returns {void}
6787
6848
  */
6788
- setCodeEditorContent(status, id2) {
6789
- if (!id2)
6849
+ setCodeEditorContent(status, id) {
6850
+ if (!id)
6790
6851
  return;
6791
- this.setId(id2);
6852
+ this.setId(id);
6792
6853
  this.state.isShowCodeEditor = status;
6793
6854
  }
6794
6855
  /**
@@ -6818,10 +6879,10 @@
6818
6879
  * @param {Id} id 代码块id
6819
6880
  * @returns {void}
6820
6881
  */
6821
- setId(id2) {
6822
- if (!id2)
6882
+ setId(id) {
6883
+ if (!id)
6823
6884
  return;
6824
- this.state.id = id2;
6885
+ this.state.id = id;
6825
6886
  }
6826
6887
  /**
6827
6888
  * 获取当前选中的代码块ID
@@ -6886,9 +6947,9 @@
6886
6947
  const currentDsl = await this.getCodeDsl();
6887
6948
  if (!currentDsl)
6888
6949
  return;
6889
- codeIds.forEach((id2) => {
6890
- delete currentDsl[id2];
6891
- this.emit("remove", id2);
6950
+ codeIds.forEach((id) => {
6951
+ delete currentDsl[id];
6952
+ this.emit("remove", id);
6892
6953
  });
6893
6954
  }
6894
6955
  /**
@@ -7403,176 +7464,274 @@
7403
7464
  const eventsService = new Events();
7404
7465
 
7405
7466
  class Keybinding extends BaseService {
7406
- keycon = new KeyController();
7407
7467
  ctrlKey = /mac os x/.test(navigator.userAgent.toLowerCase()) ? "meta" : "ctrl";
7408
- constructor() {
7409
- super();
7410
- this.keycon.keyup((e) => {
7411
- this.emit("keyup", e.inputEvent);
7412
- }).keyup("delete", (e) => {
7413
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7414
- return;
7415
- }
7416
- e.inputEvent.preventDefault();
7417
- this.removeNode();
7418
- }).keyup("backspace", (e) => {
7419
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7420
- return;
7421
- }
7422
- e.inputEvent.preventDefault();
7423
- this.removeNode();
7424
- }).keydown([this.ctrlKey, "c"], (e) => {
7425
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7426
- return;
7427
- }
7428
- e.inputEvent.preventDefault();
7468
+ controllers = /* @__PURE__ */ new Map();
7469
+ bindingList = [];
7470
+ commands = {
7471
+ [KeyBindingCommand.DELETE_NODE]: () => {
7429
7472
  const nodes = editorService.get("nodes");
7430
- nodes && editorService.copy(nodes);
7431
- }).keydown([this.ctrlKey, "v"], (e) => {
7432
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7473
+ if (!nodes || utils.isPage(nodes[0]))
7433
7474
  return;
7434
- }
7435
- e.inputEvent.preventDefault();
7475
+ editorService.remove(nodes);
7476
+ },
7477
+ [KeyBindingCommand.COPY_NODE]: () => {
7436
7478
  const nodes = editorService.get("nodes");
7437
- nodes && editorService.paste({ offsetX: 10, offsetY: 10 });
7438
- }).keydown([this.ctrlKey, "x"], (e) => {
7439
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7440
- return;
7441
- }
7442
- e.inputEvent.preventDefault();
7443
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7444
- return;
7445
- }
7479
+ nodes && editorService.copy(nodes);
7480
+ },
7481
+ [KeyBindingCommand.CUT_NODE]: () => {
7446
7482
  const nodes = editorService.get("nodes");
7447
7483
  if (!nodes || utils.isPage(nodes[0]))
7448
7484
  return;
7449
7485
  editorService.copy(nodes);
7450
7486
  editorService.remove(nodes);
7451
- }).keydown([this.ctrlKey, "z"], (e) => {
7452
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7453
- return;
7454
- }
7455
- e.inputEvent.preventDefault();
7487
+ },
7488
+ [KeyBindingCommand.PASTE_NODE]: () => {
7489
+ const nodes = editorService.get("nodes");
7490
+ nodes && editorService.paste({ offsetX: 10, offsetY: 10 });
7491
+ },
7492
+ [KeyBindingCommand.UNDO]: () => {
7456
7493
  editorService.undo();
7457
- }).keydown([this.ctrlKey, "shift", "z"], (e) => {
7458
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7459
- return;
7460
- }
7461
- e.inputEvent.preventDefault();
7494
+ },
7495
+ [KeyBindingCommand.REDO]: () => {
7462
7496
  editorService.redo();
7463
- }).keydown("up", (e) => {
7464
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7465
- return;
7466
- }
7467
- e.inputEvent.preventDefault();
7497
+ },
7498
+ [KeyBindingCommand.ZOOM_IN]: () => {
7499
+ uiService.zoom(0.1);
7500
+ },
7501
+ [KeyBindingCommand.ZOOM_OUT]: () => {
7502
+ uiService.zoom(-0.1);
7503
+ },
7504
+ [KeyBindingCommand.ZOOM_RESET]: () => {
7505
+ uiService.set("zoom", 1);
7506
+ },
7507
+ [KeyBindingCommand.ZOOM_FIT]: async () => {
7508
+ uiService.set("zoom", await uiService.calcZoom());
7509
+ },
7510
+ [KeyBindingCommand.MOVE_UP_1]: () => {
7468
7511
  editorService.move(0, -1);
7469
- }).keydown("down", (e) => {
7470
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7471
- return;
7472
- }
7473
- e.inputEvent.preventDefault();
7512
+ },
7513
+ [KeyBindingCommand.MOVE_DOWN_1]: () => {
7474
7514
  editorService.move(0, 1);
7475
- }).keydown("left", (e) => {
7476
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7477
- return;
7478
- }
7479
- e.inputEvent.preventDefault();
7515
+ },
7516
+ [KeyBindingCommand.MOVE_LEFT_1]: () => {
7480
7517
  editorService.move(-1, 0);
7481
- }).keydown("right", (e) => {
7482
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7483
- return;
7484
- }
7485
- e.inputEvent.preventDefault();
7518
+ },
7519
+ [KeyBindingCommand.MOVE_RIGHT_1]: () => {
7486
7520
  editorService.move(1, 0);
7487
- }).keydown([this.ctrlKey, "up"], (e) => {
7488
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7489
- return;
7490
- }
7491
- e.inputEvent.preventDefault();
7521
+ },
7522
+ [KeyBindingCommand.MOVE_UP_10]: () => {
7492
7523
  editorService.move(0, -10);
7493
- }).keydown([this.ctrlKey, "down"], (e) => {
7494
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7495
- return;
7496
- }
7497
- e.inputEvent.preventDefault();
7524
+ },
7525
+ [KeyBindingCommand.MOVE_DOWN_10]: () => {
7498
7526
  editorService.move(0, 10);
7499
- }).keydown([this.ctrlKey, "left"], (e) => {
7500
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7501
- return;
7502
- }
7503
- e.inputEvent.preventDefault();
7527
+ },
7528
+ [KeyBindingCommand.MOVE_LEFT_10]: () => {
7504
7529
  editorService.move(-10, 0);
7505
- }).keydown([this.ctrlKey, "right"], (e) => {
7506
- e.inputEvent.preventDefault();
7530
+ },
7531
+ [KeyBindingCommand.MOVE_RIGHT_10]: () => {
7507
7532
  editorService.move(10, 0);
7508
- }).keydown("tab", (e) => {
7509
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7510
- return;
7511
- }
7512
- e.inputEvent.preventDefault();
7533
+ },
7534
+ [KeyBindingCommand.SWITCH_NODE]: () => {
7513
7535
  editorService.selectNextNode();
7514
- }).keydown([this.ctrlKey, "tab"], (e) => {
7515
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7516
- return;
7517
- }
7518
- e.inputEvent.preventDefault();
7519
- editorService.selectNextPage();
7520
- }).keydown([this.ctrlKey, "="], (e) => {
7521
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7522
- return;
7523
- }
7524
- e.inputEvent.preventDefault();
7525
- uiService.zoom(0.1);
7526
- }).keydown([this.ctrlKey, "numpadplus"], (e) => {
7527
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7528
- return;
7529
- }
7530
- e.inputEvent.preventDefault();
7531
- uiService.zoom(0.1);
7532
- }).keydown([this.ctrlKey, "-"], (e) => {
7533
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7534
- return;
7535
- }
7536
- e.inputEvent.preventDefault();
7537
- uiService.zoom(-0.1);
7538
- }).keydown([this.ctrlKey, "numpad-"], (e) => {
7539
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7540
- return;
7541
- }
7542
- e.inputEvent.preventDefault();
7543
- uiService.zoom(-0.1);
7544
- }).keydown([this.ctrlKey, "0"], async (e) => {
7545
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7546
- return;
7547
- }
7548
- e.inputEvent.preventDefault();
7549
- uiService.set("zoom", await uiService.calcZoom());
7550
- }).keydown([this.ctrlKey, "1"], (e) => {
7551
- if (this.isDisabledKeyEvent(e.inputEvent.target)) {
7552
- return;
7536
+ }
7537
+ };
7538
+ registeCommand(command, handler) {
7539
+ this.commands[command] = handler;
7540
+ }
7541
+ unregisteCommand(command) {
7542
+ delete this.commands[command];
7543
+ }
7544
+ registeEl(name, el) {
7545
+ if (name !== "global" && !el) {
7546
+ throw new Error("只有name为global可以不传el");
7547
+ }
7548
+ const keycon = new KeyController(el);
7549
+ this.controllers.set(name, keycon);
7550
+ this.bind(name);
7551
+ }
7552
+ unregisteEl(name) {
7553
+ this.controllers.get(name)?.destroy();
7554
+ this.controllers.delete(name);
7555
+ this.bindingList.forEach((item) => {
7556
+ item.binded = false;
7557
+ });
7558
+ }
7559
+ registe(maps) {
7560
+ for (const keybindingItem of maps) {
7561
+ const { command, keybinding, when } = keybindingItem;
7562
+ for (const [type = "", eventType = "keydown"] of when) {
7563
+ const cacheItem = { type, command, keybinding, eventType, binded: false };
7564
+ this.bindingList.push(cacheItem);
7553
7565
  }
7554
- e.inputEvent.preventDefault();
7555
- uiService.set("zoom", 1);
7566
+ }
7567
+ this.bind();
7568
+ }
7569
+ reset() {
7570
+ this.controllers.forEach((keycon) => {
7571
+ keycon.destroy();
7556
7572
  });
7573
+ this.controllers.clear();
7574
+ this.bindingList = [];
7557
7575
  }
7558
7576
  destroy() {
7559
- this.keycon.destroy();
7577
+ this.reset();
7560
7578
  }
7561
- isDisabledKeyEvent(node) {
7562
- const el = node;
7563
- if (!el)
7564
- return false;
7565
- return el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.isContentEditable;
7579
+ bind(name) {
7580
+ for (const item of this.bindingList) {
7581
+ const { type, eventType, command, keybinding, binded } = item;
7582
+ if (name && name !== type) {
7583
+ continue;
7584
+ }
7585
+ if (binded) {
7586
+ continue;
7587
+ }
7588
+ const keycon = this.controllers.get(type);
7589
+ if (!keycon) {
7590
+ continue;
7591
+ }
7592
+ const handler = (e) => {
7593
+ e.inputEvent.preventDefault();
7594
+ this.commands[command]?.(e.inputEvent);
7595
+ };
7596
+ this.getKeyconKeys(keybinding).forEach((keys) => {
7597
+ if (keys[0]) {
7598
+ keycon[eventType](keys, handler);
7599
+ } else {
7600
+ keycon[eventType](handler);
7601
+ }
7602
+ });
7603
+ item.binded = true;
7604
+ }
7566
7605
  }
7567
- removeNode() {
7568
- const nodes = editorService.get("nodes");
7569
- if (!nodes || utils.isPage(nodes[0]))
7570
- return;
7571
- editorService.remove(nodes);
7606
+ getKeyconKeys(keybinding = "") {
7607
+ const splitKey = (key) => key.split("+").map((k) => k === "ctrl" ? this.ctrlKey : k);
7608
+ if (Array.isArray(keybinding)) {
7609
+ return keybinding.map((key) => splitKey(key));
7610
+ }
7611
+ return [splitKey(keybinding)];
7572
7612
  }
7573
7613
  }
7574
7614
  const keybindingService = new Keybinding();
7575
7615
 
7616
+ const keybindingConfig = [
7617
+ {
7618
+ command: KeyBindingCommand.DELETE_NODE,
7619
+ keybinding: ["delete", "backspace"],
7620
+ when: [
7621
+ ["stage", "keyup"],
7622
+ ["layer-panel", "keydown"]
7623
+ ]
7624
+ },
7625
+ {
7626
+ command: KeyBindingCommand.COPY_NODE,
7627
+ keybinding: "ctrl+c",
7628
+ when: [
7629
+ ["stage", "keydown"],
7630
+ ["layer-panel", "keydown"]
7631
+ ]
7632
+ },
7633
+ {
7634
+ command: KeyBindingCommand.PASTE_NODE,
7635
+ keybinding: "ctrl+v",
7636
+ when: [
7637
+ ["stage", "keydown"],
7638
+ ["layer-panel", "keydown"]
7639
+ ]
7640
+ },
7641
+ {
7642
+ command: KeyBindingCommand.CUT_NODE,
7643
+ keybinding: "ctrl+x",
7644
+ when: [
7645
+ ["stage", "keydown"],
7646
+ ["layer-panel", "keydown"]
7647
+ ]
7648
+ },
7649
+ {
7650
+ command: KeyBindingCommand.UNDO,
7651
+ keybinding: "ctrl+z",
7652
+ when: [
7653
+ ["stage", "keydown"],
7654
+ ["layer-panel", "keydown"]
7655
+ ]
7656
+ },
7657
+ {
7658
+ command: KeyBindingCommand.REDO,
7659
+ keybinding: "ctrl+shift+z",
7660
+ when: [
7661
+ ["stage", "keydown"],
7662
+ ["layer-panel", "keydown"]
7663
+ ]
7664
+ },
7665
+ {
7666
+ command: KeyBindingCommand.MOVE_UP_1,
7667
+ keybinding: "up",
7668
+ when: [["stage", "keydown"]]
7669
+ },
7670
+ {
7671
+ command: KeyBindingCommand.MOVE_DOWN_1,
7672
+ keybinding: "down",
7673
+ when: [["stage", "keydown"]]
7674
+ },
7675
+ {
7676
+ command: KeyBindingCommand.MOVE_LEFT_1,
7677
+ keybinding: "left",
7678
+ when: [["stage", "keydown"]]
7679
+ },
7680
+ {
7681
+ command: KeyBindingCommand.MOVE_RIGHT_1,
7682
+ keybinding: "right",
7683
+ when: [["stage", "keydown"]]
7684
+ },
7685
+ {
7686
+ command: KeyBindingCommand.MOVE_UP_10,
7687
+ keybinding: "ctrl+up",
7688
+ when: [["stage", "keydown"]]
7689
+ },
7690
+ {
7691
+ command: KeyBindingCommand.MOVE_DOWN_10,
7692
+ keybinding: "ctrl+down",
7693
+ when: [["stage", "keydown"]]
7694
+ },
7695
+ {
7696
+ command: KeyBindingCommand.MOVE_LEFT_10,
7697
+ keybinding: "ctrl+left",
7698
+ when: [["stage", "keydown"]]
7699
+ },
7700
+ {
7701
+ command: KeyBindingCommand.MOVE_RIGHT_10,
7702
+ keybinding: "ctrl+right",
7703
+ when: [["stage", "keydown"]]
7704
+ },
7705
+ {
7706
+ command: KeyBindingCommand.SWITCH_NODE,
7707
+ keybinding: "tab",
7708
+ when: [
7709
+ ["stage", "keydown"],
7710
+ ["layer-panel", "keydown"]
7711
+ ]
7712
+ },
7713
+ {
7714
+ command: KeyBindingCommand.ZOOM_IN,
7715
+ keybinding: ["ctrl+=", "ctrl+numpadplus"],
7716
+ when: [["stage", "keydown"]]
7717
+ },
7718
+ {
7719
+ command: KeyBindingCommand.ZOOM_OUT,
7720
+ keybinding: ["ctrl+-", "ctrl+numpad-"],
7721
+ when: [["stage", "keydown"]]
7722
+ },
7723
+ {
7724
+ command: KeyBindingCommand.ZOOM_FIT,
7725
+ keybinding: "ctrl+0",
7726
+ when: [["stage", "keydown"]]
7727
+ },
7728
+ {
7729
+ command: KeyBindingCommand.ZOOM_RESET,
7730
+ keybinding: "ctrl+1",
7731
+ when: [["stage", "keydown"]]
7732
+ }
7733
+ ];
7734
+
7576
7735
  const editorProps = {
7577
7736
  /** 页面初始值 */
7578
7737
  modelValue: {
@@ -7700,7 +7859,8 @@
7700
7859
  propsService,
7701
7860
  eventsService,
7702
7861
  uiService,
7703
- codeBlockService
7862
+ codeBlockService,
7863
+ keybindingService
7704
7864
  }) => {
7705
7865
  vue.watch(
7706
7866
  () => props.modelValue,
@@ -7769,6 +7929,7 @@
7769
7929
  uiService.resetState();
7770
7930
  componentListService.resetState();
7771
7931
  codeBlockService.resetState();
7932
+ keybindingService.reset();
7772
7933
  });
7773
7934
  };
7774
7935
  const initServiceEvents = (props, emit, { editorService, codeBlockService, dataSourceService, depService }) => {
@@ -7965,6 +8126,8 @@
7965
8126
  };
7966
8127
  initServiceEvents(props, emit, services);
7967
8128
  initServiceState(props, services);
8129
+ keybindingService.registe(keybindingConfig);
8130
+ keybindingService.registeEl("global");
7968
8131
  vue.provide("services", services);
7969
8132
  vue.provide("codeOptions", props.codeOptions);
7970
8133
  vue.provide(
@@ -8101,7 +8264,8 @@
8101
8264
  const index$1 = '';
8102
8265
 
8103
8266
  const defaultInstallOpt = {
8104
- // @todo, 自定义图片上传方法等编辑器依赖的外部选项
8267
+ // eslint-disable-next-line no-eval
8268
+ parseDSL: (dsl) => eval(dsl)
8105
8269
  };
8106
8270
  const index = {
8107
8271
  install: (app, opt) => {
@@ -8137,6 +8301,7 @@
8137
8301
  exports.Fixed2Other = Fixed2Other;
8138
8302
  exports.H_GUIDE_LINE_STORAGE_KEY = H_GUIDE_LINE_STORAGE_KEY;
8139
8303
  exports.Icon = _sfc_main$B;
8304
+ exports.KeyBindingCommand = KeyBindingCommand;
8140
8305
  exports.KeyValue = _sfc_main$w;
8141
8306
  exports.Keys = Keys;
8142
8307
  exports.LayerOffset = LayerOffset;