@tmagic/editor 1.0.0-beta.11 → 1.0.0-beta.12

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # [文档](https://tencent.github.io/tmagic-editor/docs/)
@@ -4,10 +4,11 @@ import * as monaco from 'monaco-editor';
4
4
  import { Plus, Edit, ArrowDown, Grid, ScaleToOriginal, ZoomOut, ZoomIn, Right, Back, Delete, Files, Coin, ArrowLeftBold, ArrowRightBold, CaretBottom } from '@element-plus/icons';
5
5
  import { NodeType } from '@tmagic/schema';
6
6
  import { cloneDeep, random, mergeWith, throttle } from 'lodash-es';
7
- import { toLine, isPop, getNodePath } from '@tmagic/utils';
7
+ import { toLine, isPop, getNodePath, isNumber, isPage } from '@tmagic/utils';
8
8
  import { EventEmitter as EventEmitter$2 } from 'events';
9
9
  import { DEFAULT_EVENTS, DEFAULT_METHODS } from '@tmagic/core';
10
10
  import { ElMessage } from 'element-plus';
11
+ import KeyController from 'keycon';
11
12
  import StageCore from '@tmagic/stage';
12
13
 
13
14
  var _export_sfc = (sfc, props) => {
@@ -609,7 +610,7 @@ class Props extends BaseService {
609
610
  setPropsValue(type, value) {
610
611
  this.state.propsValueMap[type] = value;
611
612
  }
612
- async getPropsValue(type) {
613
+ async getPropsValue(type, defaultValue = {}) {
613
614
  if (type === "area") {
614
615
  const value = await this.getPropsValue("button");
615
616
  value.className = "action-area";
@@ -621,6 +622,7 @@ class Props extends BaseService {
621
622
  }
622
623
  return cloneDeep({
623
624
  ...getDefaultPropsValue(type),
625
+ ...defaultValue,
624
626
  ...this.state.propsValueMap[type] || {}
625
627
  });
626
628
  }
@@ -682,7 +684,7 @@ const updatePopId = (oldId, popId, pageConfig) => {
682
684
  };
683
685
  const setNewItemId = (config, parent) => {
684
686
  const oldId = config.id;
685
- config.id = generateId(config.type);
687
+ config.id = generateId(config.type || "component");
686
688
  config.name = `${config.name?.replace(/_(\d+)$/, "")}_${config.id}`;
687
689
  if (isPop(config) && parent?.type === NodeType.PAGE) {
688
690
  updatePopId(oldId, config.id, parent);
@@ -705,11 +707,25 @@ const toRelative = (node) => {
705
707
  };
706
708
  return node;
707
709
  };
708
- const initPosition = (node, layout) => {
710
+ const setTop2Middle = (node, parentNode, stage) => {
711
+ const style = node.style || {};
712
+ const height = style.height || 0;
713
+ if (!stage || style.top || !parentNode.style || !isNumber(height))
714
+ return style;
715
+ const { height: parentHeight } = parentNode.style;
716
+ if (isPage(parentNode)) {
717
+ const { scrollTop = 0, wrapperHeight } = stage.mask;
718
+ style.top = (wrapperHeight - height) / 2 + scrollTop;
719
+ } else {
720
+ style.top = (parentHeight - height) / 2;
721
+ }
722
+ return style;
723
+ };
724
+ const initPosition = (node, layout, parentNode, stage) => {
709
725
  if (layout === Layout.ABSOLUTE) {
710
726
  node.style = {
711
727
  position: "absolute",
712
- ...node.style || {}
728
+ ...setTop2Middle(node, parentNode, stage)
713
729
  };
714
730
  return node;
715
731
  }
@@ -756,7 +772,9 @@ const Fixed2Other = async (node, root, getLayout) => {
756
772
  const cur = path.pop();
757
773
  const offset = {
758
774
  left: cur?.style?.left || 0,
759
- top: cur?.style?.top || 0
775
+ top: cur?.style?.top || 0,
776
+ right: "",
777
+ bottom: ""
760
778
  };
761
779
  path.forEach((value) => {
762
780
  offset.left = offset.left - globalThis.parseFloat(value.style?.left || 0);
@@ -901,7 +919,7 @@ class Editor$1 extends BaseService {
901
919
  if (!parentNode)
902
920
  throw new Error("\u672A\u627E\u5230\u7236\u5143\u7D20");
903
921
  const layout = await this.getLayout(parentNode);
904
- const newNode = initPosition({ ...toRaw(await propsService.getPropsValue(type)), ...config2 }, layout);
922
+ const newNode = initPosition({ ...toRaw(await propsService.getPropsValue(type, config2)) }, layout, parentNode, this.get("stage"));
905
923
  if ((parentNode?.type === NodeType.ROOT || curNode.type === NodeType.ROOT) && newNode.type !== NodeType.PAGE) {
906
924
  throw new Error("app\u4E0B\u4E0D\u80FD\u6DFB\u52A0\u7EC4\u4EF6");
907
925
  }
@@ -1326,7 +1344,7 @@ const fillConfig = (config = []) => [
1326
1344
  type: "select",
1327
1345
  options: (mForm, { model }) => {
1328
1346
  const node = editorService.getNodeById(model.to);
1329
- if (!node)
1347
+ if (!node?.type)
1330
1348
  return [];
1331
1349
  return eventsService.getMethod(node.type).map((option) => ({
1332
1350
  text: option.label,
@@ -3049,9 +3067,9 @@ const _sfc_main$a = defineComponent({
3049
3067
  curFormConfig.value = [];
3050
3068
  return;
3051
3069
  }
3052
- values.value = node.value;
3053
3070
  const type = node.value.type || (node.value.items ? "container" : "text");
3054
3071
  curFormConfig.value = await services?.propsService.getPropsConfig(type) || [];
3072
+ values.value = node.value;
3055
3073
  };
3056
3074
  watchEffect(init);
3057
3075
  services?.propsService.on("props-configs-change", init);
@@ -3109,11 +3127,11 @@ const _sfc_main$9 = defineComponent({
3109
3127
  searchText,
3110
3128
  collapseValue,
3111
3129
  list,
3112
- appendComponent({ text, type, ...config }) {
3130
+ appendComponent({ text, type, data = {} }) {
3113
3131
  services?.editorService.add({
3114
3132
  name: text,
3115
3133
  type,
3116
- ...config
3134
+ ...data
3117
3135
  });
3118
3136
  }
3119
3137
  };
@@ -3203,7 +3221,8 @@ const _sfc_main$8 = defineComponent({
3203
3221
  append(config) {
3204
3222
  services?.editorService.add({
3205
3223
  name: config.text,
3206
- type: config.type
3224
+ type: config.type,
3225
+ ...config.data || {}
3207
3226
  });
3208
3227
  },
3209
3228
  remove() {
@@ -4348,12 +4367,48 @@ const _sfc_main$1 = defineComponent({
4348
4367
  },
4349
4368
  setup() {
4350
4369
  const services = inject("services");
4370
+ const workspace = ref();
4371
+ const node = computed(() => services?.editorService.get("node"));
4372
+ let keycon;
4373
+ const mouseenterHandler = () => {
4374
+ workspace.value?.focus();
4375
+ };
4376
+ const mouseleaveHandler = () => {
4377
+ workspace.value?.blur();
4378
+ };
4379
+ onMounted(() => {
4380
+ workspace.value?.addEventListener("mouseenter", mouseenterHandler);
4381
+ workspace.value?.addEventListener("mouseleave", mouseleaveHandler);
4382
+ keycon = new KeyController(workspace.value);
4383
+ keycon.keyup("delete", () => {
4384
+ node.value && services?.editorService.remove(node.value);
4385
+ }).keyup(["ctrl", "c"], () => {
4386
+ node.value && services?.editorService.copy(node.value);
4387
+ }).keyup(["ctrl", "v"], () => {
4388
+ node.value && services?.editorService.paste();
4389
+ }).keyup(["ctrl", "x"], () => {
4390
+ if (node.value && services) {
4391
+ services.editorService.copy(node.value);
4392
+ services.editorService.remove(node.value);
4393
+ }
4394
+ });
4395
+ });
4396
+ onUnmounted(() => {
4397
+ workspace.value?.removeEventListener("mouseenter", mouseenterHandler);
4398
+ workspace.value?.removeEventListener("mouseleave", mouseleaveHandler);
4399
+ keycon.destroy();
4400
+ });
4351
4401
  return {
4402
+ workspace,
4352
4403
  page: computed(() => services?.editorService.get("page"))
4353
4404
  };
4354
4405
  }
4355
4406
  });
4356
- const _hoisted_1 = { class: "m-editor-workspace" };
4407
+ const _hoisted_1 = {
4408
+ class: "m-editor-workspace",
4409
+ tabindex: "1",
4410
+ ref: "workspace"
4411
+ };
4357
4412
  function _sfc_render$1(_ctx, _cache, $props, $setup, $data, $options) {
4358
4413
  const _component_magic_stage = resolveComponent("magic-stage");
4359
4414
  const _component_page_bar = resolveComponent("page-bar");
@@ -4375,7 +4430,7 @@ function _sfc_render$1(_ctx, _cache, $props, $setup, $data, $options) {
4375
4430
  ]),
4376
4431
  _: 3
4377
4432
  })
4378
- ]);
4433
+ ], 512);
4379
4434
  }
4380
4435
  var Workspace = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["render", _sfc_render$1]]);
4381
4436