@tmagic/editor 1.1.0-beta.7 → 1.1.0-beta.8

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.
@@ -761,10 +761,10 @@
761
761
  top: 0,
762
762
  left: 0
763
763
  });
764
- const getMiddleTop = (style = {}, parentNode, stage) => {
765
- let height = style.height || 0;
766
- if (!stage || typeof style.top !== "undefined" || !parentNode.style)
767
- return style.top;
764
+ const getMiddleTop = (node, parentNode, stage) => {
765
+ let height = node.style?.height || 0;
766
+ if (!stage || typeof node.style?.top !== "undefined" || !parentNode.style)
767
+ return node.style?.top;
768
768
  if (!utils.isNumber(height)) {
769
769
  height = 0;
770
770
  }
@@ -775,12 +775,11 @@
775
775
  }
776
776
  return (parentHeight - height) / 2;
777
777
  };
778
- const getInitPositionStyle = (style = {}, layout, parentNode, stage) => {
778
+ const getInitPositionStyle = (style = {}, layout) => {
779
779
  if (layout === Layout.ABSOLUTE) {
780
780
  return {
781
781
  ...style,
782
- position: "absolute",
783
- top: getMiddleTop(style, parentNode, stage)
782
+ position: "absolute"
784
783
  };
785
784
  }
786
785
  if (layout === Layout.RELATIVE) {
@@ -872,6 +871,16 @@
872
871
  }
873
872
  return config.style.left;
874
873
  };
874
+ const fixNodePosition = (config, parent, stage) => {
875
+ if (config.style?.position !== "absolute") {
876
+ return config.style;
877
+ }
878
+ return {
879
+ ...config.style || {},
880
+ top: getMiddleTop(config, parent, stage),
881
+ left: fixNodeLeft(config, parent, stage?.renderer.contentWindow?.document)
882
+ };
883
+ };
875
884
 
876
885
  class Props extends BaseService {
877
886
  state = vue.reactive({
@@ -1028,36 +1037,6 @@
1028
1037
  }));
1029
1038
  return pasteConfigs;
1030
1039
  };
1031
- const beforeAdd = async (addNode, parent) => {
1032
- const { type, inputEvent, ...config } = addNode;
1033
- const curNode = editorService.get("node");
1034
- let parentNode;
1035
- const isPage2 = type === schema.NodeType.PAGE;
1036
- if (isPage2) {
1037
- parentNode = editorService.get("root");
1038
- } else if (parent && typeof parent !== "function") {
1039
- parentNode = parent;
1040
- } else if (curNode.items) {
1041
- parentNode = curNode;
1042
- } else {
1043
- parentNode = editorService.getParentById(curNode.id, false);
1044
- }
1045
- if (!parentNode)
1046
- throw new Error("\u672A\u627E\u5230\u7236\u5143\u7D20");
1047
- const layout = await editorService.getLayout(vue.toRaw(parentNode), addNode);
1048
- const newNode = { ...vue.toRaw(await propsService.getPropsValue(type, config)) };
1049
- newNode.style = getInitPositionStyle(newNode.style, layout, parentNode, editorService.get("stage"));
1050
- if ((parentNode?.type === schema.NodeType.ROOT || curNode.type === schema.NodeType.ROOT) && newNode.type !== schema.NodeType.PAGE) {
1051
- throw new Error("app\u4E0B\u4E0D\u80FD\u6DFB\u52A0\u7EC4\u4EF6");
1052
- }
1053
- parentNode?.items?.push(newNode);
1054
- return {
1055
- parentNode,
1056
- newNode,
1057
- layout,
1058
- isPage: isPage2
1059
- };
1060
- };
1061
1040
  const getPositionInContainer = (position = {}, id) => {
1062
1041
  let { left = 0, top = 0 } = position;
1063
1042
  const parentEl = editorService.get("stage")?.renderer?.contentWindow?.document.getElementById(`${id}`);
@@ -1069,18 +1048,6 @@
1069
1048
  top
1070
1049
  };
1071
1050
  };
1072
- const notifyAddToStage = async (parentNode, newNode, layout) => {
1073
- const stage = editorService.get("stage");
1074
- const root = editorService.get("root");
1075
- await stage?.add({ config: lodashEs.cloneDeep(newNode), parent: lodashEs.cloneDeep(parentNode), root: lodashEs.cloneDeep(root) });
1076
- if (layout === Layout.ABSOLUTE) {
1077
- const fixedLeft = fixNodeLeft(newNode, parentNode, stage?.renderer.contentWindow?.document);
1078
- if (typeof fixedLeft !== "undefined" && newNode.style) {
1079
- newNode.style.left = fixedLeft;
1080
- await stage?.update({ config: lodashEs.cloneDeep(newNode), root: lodashEs.cloneDeep(root) });
1081
- }
1082
- }
1083
- };
1084
1051
  const beforeRemove = async (node) => {
1085
1052
  if (!node?.id)
1086
1053
  return;
@@ -1119,6 +1086,18 @@
1119
1086
  }
1120
1087
  return parent;
1121
1088
  };
1089
+ const getAddParent = (addNode) => {
1090
+ const curNode = editorService.get("node");
1091
+ let parentNode;
1092
+ if (!Array.isArray(addNode) && utils.isPage(addNode)) {
1093
+ parentNode = editorService.get("root");
1094
+ } else if (curNode.items) {
1095
+ parentNode = curNode;
1096
+ } else {
1097
+ parentNode = editorService.getParentById(curNode.id, false);
1098
+ }
1099
+ return parentNode;
1100
+ };
1122
1101
 
1123
1102
  class Editor$1 extends BaseService {
1124
1103
  state = vue.reactive({
@@ -1137,6 +1116,7 @@
1137
1116
  super([
1138
1117
  "getLayout",
1139
1118
  "select",
1119
+ "doAdd",
1140
1120
  "add",
1141
1121
  "remove",
1142
1122
  "update",
@@ -1271,36 +1251,52 @@
1271
1251
  });
1272
1252
  this.set("nodes", nodes);
1273
1253
  }
1274
- async multiAdd(configs) {
1254
+ async doAdd(node, parent) {
1255
+ const root = this.get("root");
1256
+ const curNode = this.get("node");
1275
1257
  const stage = this.get("stage");
1276
- const newNodes = await Promise.all(configs.map(async (configItem) => {
1277
- const { parentNode, newNode, layout } = await beforeAdd(configItem);
1278
- await notifyAddToStage(parentNode, newNode, layout);
1279
- return newNode;
1280
- }));
1281
- const newNodeIds = newNodes.map((node) => node.id);
1282
- this.addModifiedNodeId(newNodeIds.join("-"));
1283
- this.pushHistoryState();
1284
- stage?.multiSelect(newNodeIds);
1285
- this.emit("multiAdd", newNodes);
1286
- return newNodes;
1258
+ if ((parent?.type === schema.NodeType.ROOT || curNode.type === schema.NodeType.ROOT) && node.type !== schema.NodeType.PAGE) {
1259
+ throw new Error("app\u4E0B\u4E0D\u80FD\u6DFB\u52A0\u7EC4\u4EF6");
1260
+ }
1261
+ const layout = await this.getLayout(vue.toRaw(parent), node);
1262
+ node.style = getInitPositionStyle(node.style, layout);
1263
+ await stage?.add({ config: lodashEs.cloneDeep(node), parent: lodashEs.cloneDeep(parent), root: lodashEs.cloneDeep(root) });
1264
+ node.style = fixNodePosition(node, parent, stage);
1265
+ await stage?.update({ config: lodashEs.cloneDeep(node), root: lodashEs.cloneDeep(root) });
1266
+ parent?.items?.push(node);
1267
+ this.addModifiedNodeId(node.id);
1268
+ return node;
1287
1269
  }
1288
1270
  async add(addNode, parent) {
1289
1271
  const stage = this.get("stage");
1290
- const { parentNode, newNode, layout, isPage: isPage2 } = await beforeAdd(addNode, parent);
1291
- await notifyAddToStage(parentNode, newNode, layout);
1292
- await this.select(newNode);
1293
- this.addModifiedNodeId(newNode.id);
1294
- if (!isPage2) {
1295
- this.pushHistoryState();
1272
+ const parentNode = parent && typeof parent !== "function" ? parent : getAddParent(addNode);
1273
+ if (!parentNode)
1274
+ throw new Error("\u672A\u627E\u5230\u7236\u5143\u7D20");
1275
+ const addNodes = [];
1276
+ if (!Array.isArray(addNode)) {
1277
+ const { type, inputEvent, ...config } = addNode;
1278
+ if (!type)
1279
+ throw new Error("\u7EC4\u4EF6\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A");
1280
+ addNodes.push({ ...vue.toRaw(await propsService.getPropsValue(type, config)) });
1281
+ } else {
1282
+ addNodes.push(...addNode);
1296
1283
  }
1297
- if (isPage2) {
1298
- this.state.pageLength += 1;
1284
+ const newNodes = await Promise.all(addNodes.map((node) => this.doAdd(node, parentNode)));
1285
+ if (newNodes.length > 1) {
1286
+ const newNodeIds = newNodes.map((node) => node.id);
1287
+ stage?.multiSelect(newNodeIds);
1288
+ await this.multiSelect(newNodeIds);
1299
1289
  } else {
1300
- stage?.select(newNode.id);
1290
+ await this.select(newNodes[0]);
1291
+ if (utils.isPage(newNodes[0])) {
1292
+ this.state.pageLength += 1;
1293
+ } else {
1294
+ stage?.select(newNodes[0].id);
1295
+ }
1301
1296
  }
1302
- this.emit("add", newNode);
1303
- return newNode;
1297
+ this.pushHistoryState();
1298
+ this.emit("add", newNodes);
1299
+ return newNodes.length > 1 ? newNodes[0] : newNodes;
1304
1300
  }
1305
1301
  async remove(nodeOrNodeList) {
1306
1302
  if (Array.isArray(nodeOrNodeList)) {
@@ -1392,10 +1388,10 @@
1392
1388
  }
1393
1389
  async paste(position = {}) {
1394
1390
  const config = await storageService.getItem(COPY_STORAGE_KEY);
1395
- if (!config)
1391
+ if (!Array.isArray(config))
1396
1392
  return;
1397
1393
  const pasteConfigs = await beforePaste(position, config);
1398
- return await this.multiAdd(pasteConfigs);
1394
+ return this.add(pasteConfigs);
1399
1395
  }
1400
1396
  async alignCenter(config) {
1401
1397
  const parent = this.get("parent");
@@ -1458,7 +1454,7 @@
1458
1454
  return srcValue;
1459
1455
  }
1460
1456
  });
1461
- newConfig.style = getInitPositionStyle(newConfig.style, layout, target, stage);
1457
+ newConfig.style = getInitPositionStyle(newConfig.style, layout);
1462
1458
  target.items.push(newConfig);
1463
1459
  await stage.select(targetId);
1464
1460
  await stage.update({ config: lodashEs.cloneDeep(target), root });
@@ -1484,7 +1480,7 @@
1484
1480
  const node = vue.toRaw(this.get("node"));
1485
1481
  if (!node || utils.isPage(node))
1486
1482
  return;
1487
- const { style, id } = node;
1483
+ const { style, id, type } = node;
1488
1484
  if (!style || style.position !== "absolute")
1489
1485
  return;
1490
1486
  if (top && !utils.isNumber(style.top))
@@ -1493,6 +1489,7 @@
1493
1489
  return;
1494
1490
  this.update({
1495
1491
  id,
1492
+ type,
1496
1493
  style: {
1497
1494
  ...style,
1498
1495
  left: Number(style.left) + left,
@@ -2057,10 +2054,12 @@
2057
2054
  vue.createVNode(_component_edit)
2058
2055
  ]),
2059
2056
  _: 1
2060
- })) : typeof _ctx.icon === "string" && _ctx.icon.startsWith("http") ? (vue.openBlock(), vue.createElementBlock("img", {
2061
- key: 1,
2062
- src: _ctx.icon
2063
- }, null, 8, _hoisted_1$8)) : typeof _ctx.icon === "string" ? (vue.openBlock(), vue.createElementBlock("i", {
2057
+ })) : typeof _ctx.icon === "string" && _ctx.icon.startsWith("http") ? (vue.openBlock(), vue.createBlock(_component_el_icon, { key: 1 }, {
2058
+ default: vue.withCtx(() => [
2059
+ vue.createElementVNode("img", { src: _ctx.icon }, null, 8, _hoisted_1$8)
2060
+ ]),
2061
+ _: 1
2062
+ })) : typeof _ctx.icon === "string" ? (vue.openBlock(), vue.createElementBlock("i", {
2064
2063
  key: 2,
2065
2064
  class: vue.normalizeClass(_ctx.icon)
2066
2065
  }, null, 2)) : (vue.openBlock(), vue.createBlock(_component_el_icon, { key: 3 }, {
@@ -3723,10 +3722,6 @@
3723
3722
  },
3724
3723
  ...stageContentMenu
3725
3724
  ]);
3726
- vue.onMounted(async () => {
3727
- const data = await storageService.getItem(COPY_STORAGE_KEY);
3728
- canPaste.value = data !== "undefined" && !!data;
3729
- });
3730
3725
  vue.watch(parent, async () => {
3731
3726
  if (!parent.value || !editorService)
3732
3727
  return canCenter.value = false;
@@ -3735,8 +3730,10 @@
3735
3730
  const isTypeConform = nodes.value?.every((selectedNode) => ![schema.NodeType.ROOT, schema.NodeType.PAGE, "pop"].includes(`${selectedNode?.type}`));
3736
3731
  canCenter.value = isLayoutConform && !!isTypeConform;
3737
3732
  }, { immediate: true });
3738
- const show = (e) => {
3733
+ const show = async (e) => {
3739
3734
  menu.value?.show(e);
3735
+ const data = await storageService.getItem(COPY_STORAGE_KEY);
3736
+ canPaste.value = data !== "undefined" && !!data;
3740
3737
  };
3741
3738
  expose({ show });
3742
3739
  return (_ctx, _cache) => {
@@ -3984,7 +3981,7 @@
3984
3981
  nodes.value && services?.editorService.copy(nodes.value);
3985
3982
  }).keydown([ctrl, "v"], (e) => {
3986
3983
  e.inputEvent.preventDefault();
3987
- nodes.value && services?.editorService.paste();
3984
+ nodes.value && services?.editorService.paste({ offsetX: 10, offsetY: 10 });
3988
3985
  }).keydown([ctrl, "x"], (e) => {
3989
3986
  e.inputEvent.preventDefault();
3990
3987
  if (!nodes.value || utils.isPage(nodes.value[0]))
@@ -4464,6 +4461,7 @@
4464
4461
  exports.eventsService = eventsService;
4465
4462
  exports.fillConfig = fillConfig;
4466
4463
  exports.fixNodeLeft = fixNodeLeft;
4464
+ exports.fixNodePosition = fixNodePosition;
4467
4465
  exports.generatePageName = generatePageName;
4468
4466
  exports.generatePageNameByApp = generatePageNameByApp;
4469
4467
  exports.getConfig = getConfig;