@next-core/editor-bricks-helper 0.40.1 → 0.42.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/dist/index.esm.js CHANGED
@@ -4,11 +4,11 @@ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
4
  import { sortBy, clamp, isEmpty, cloneDeep, upperFirst, omit } from 'lodash';
5
5
  import { hasOwnProperty, isBrickNode, normalizeBuilderNode, JsonStorage, isRouteNode } from '@next-core/brick-utils';
6
6
  export { isBrickNode, isCustomTemplateNode, isRouteNode, isSnippetNode } from '@next-core/brick-utils';
7
+ import yaml from 'js-yaml';
7
8
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
8
9
  import { useDrag, useDrop, DndProvider } from 'react-dnd';
9
10
  import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
10
11
  import { developHelper, getRuntime, BrickAsComponent, property, BrickWrapper, UpdatingElement } from '@next-core/brick-kit';
11
- import yaml from 'js-yaml';
12
12
  import _decorate from '@babel/runtime/helpers/decorate';
13
13
  import ReactDOM from 'react-dom';
14
14
  import { HTML5Backend } from 'react-dnd-html5-backend';
@@ -658,6 +658,63 @@ function isParentExpandableTemplate(nodes, parentUid) {
658
658
  return nodes.find(node => node.$$uid === parentUid).$$isExpandableTemplate;
659
659
  }
660
660
 
661
+ var jsonFieldsInBrick = ["properties", "events", "lifeCycle", "params", "if", "transform"];
662
+ var yamlFieldsInBrick = ["permissionsPreCheck", "transformFrom"];
663
+ var ignoredFieldsInBrick = ["brick", "template", "portal", "slots", "id", "children", "instanceId"];
664
+ // The reverse operation of *normalize*.
665
+ // https://github.com/easyops-cn/next-basics/blob/5a6710d567821bcb4a0c71e22d55212193d8b0cb/bricks/next-builder/src/shared/storyboard/buildStoryboard.ts#L394
666
+ function reverseNormalize(brickConf, ctx) {
667
+ return Object.fromEntries(Object.entries(brickConf).map(_ref => {
668
+ var [key, value] = _ref;
669
+ return value === undefined || ignoredFieldsInBrick.includes(key) ? undefined : jsonFieldsInBrick.includes(key) ? [key, JSON.stringify(value)] : yamlFieldsInBrick.includes(key) ? [key, yaml.safeDump(value)] : [key, value];
670
+ }).filter(Boolean).concat(Object.entries(ctx.nodeData), [["brick", ctx.nodeData.type === "template" ? brickConf.template : brickConf.brick], ["portal", ctx.isPortalCanvas || brickConf.portal]]));
671
+ }
672
+
673
+ function getSnippetNodeDetail(_ref) {
674
+ var {
675
+ parent,
676
+ parentUid,
677
+ mountPoint,
678
+ nodeUid,
679
+ brickConf,
680
+ isPortalCanvas,
681
+ sort
682
+ } = _ref;
683
+ var type = brickConf.template ? "template" : brickConf.bg ? "provider" : "brick";
684
+ return {
685
+ nodeUid,
686
+ parentUid,
687
+ nodeData: reverseNormalize(brickConf, {
688
+ isPortalCanvas,
689
+ nodeData: {
690
+ type,
691
+ parent,
692
+ mountPoint,
693
+ sort
694
+ }
695
+ }),
696
+ children: brickConf.slots ? Object.entries(brickConf.slots).flatMap(_ref2 => {
697
+ var [mountPoint, slotConf] = _ref2;
698
+ return slotConf.bricks.map(childBrickConf => ({
699
+ childBrickConf,
700
+ mountPoint
701
+ }));
702
+ }).map((_ref3, index) => {
703
+ var {
704
+ childBrickConf,
705
+ mountPoint
706
+ } = _ref3;
707
+ return getSnippetNodeDetail({
708
+ parentUid: nodeUid,
709
+ mountPoint,
710
+ nodeUid: getUniqueNodeId(),
711
+ brickConf: childBrickConf,
712
+ sort: index
713
+ });
714
+ }) : []
715
+ };
716
+ }
717
+
661
718
  var BuilderInternalEventType;
662
719
 
663
720
  (function (BuilderInternalEventType) {
@@ -814,7 +871,8 @@ class BuilderDataManager {
814
871
  nodeUid,
815
872
  parentUid,
816
873
  nodeUids,
817
- nodeData
874
+ nodeData,
875
+ sort
818
876
  } = detail;
819
877
  var {
820
878
  nodes: addNodes,
@@ -825,7 +883,7 @@ class BuilderDataManager {
825
883
  parent: parentUid,
826
884
  child: nodeUid,
827
885
  mountPoint: nodeData.mountPoint,
828
- sort: undefined,
886
+ sort: sort !== null && sort !== void 0 ? sort : undefined,
829
887
  $$isTemplateDelegated: isParentExpandableTemplate(nodes, parentUid)
830
888
  }).concat(addEdges);
831
889
  var newData = {
@@ -1063,30 +1121,35 @@ class BuilderDataManager {
1063
1121
  this.reorder(parentUid, orderedEdges);
1064
1122
  }
1065
1123
 
1066
- workbenchTreeNodeMove(detail) {
1067
- var {
1068
- rootId,
1069
- nodes,
1070
- edges,
1071
- wrapperNode
1072
- } = this.data;
1124
+ getDragInfo(_ref3) {
1125
+ var _dragEdge$mountPoint;
1126
+
1073
1127
  var {
1128
+ nodeData,
1074
1129
  dragNodeUid,
1075
1130
  dragOverNodeUid,
1076
- dragParentNodeUid,
1077
1131
  dragStatus
1078
- } = detail;
1132
+ } = _ref3;
1133
+ var {
1134
+ rootId,
1135
+ nodes,
1136
+ edges
1137
+ } = this.data;
1079
1138
  var isDragRoot = dragOverNodeUid === rootId;
1080
- var nodeData = nodes.find(item => item.$$uid === dragNodeUid);
1081
- var dragEdge = edges.find(item => item.child === dragNodeUid);
1139
+ /*
1140
+ * 如果没有id, 则为新增状态, 没有edge, 否则为移动状态
1141
+ */
1142
+
1143
+ var isAdd = nodeData.id;
1144
+ var dragEdge = !isAdd ? edges.find(item => item.child === dragNodeUid) : null;
1082
1145
  var dragOverEdge = edges.find(item => item.child === dragOverNodeUid);
1083
1146
  /**
1084
1147
  * 如果是根节点, 则mountPoint强制等于 bricks
1085
- * 如果是属于拖动进某个节点中, 则使用原mountPoint
1148
+ * 如果是属于拖动进某个节点中, 则使用原mountPoint(新增节点为content)
1086
1149
  * 其他情况, 使用被拖拽节点的mountPoint
1087
1150
  */
1088
1151
 
1089
- var mountPoint = isDragRoot ? "bricks" : dragStatus === "inside" ? dragEdge.mountPoint : dragOverEdge.mountPoint;
1152
+ var mountPoint = isDragRoot ? "bricks" : dragStatus === "inside" ? (_dragEdge$mountPoint = dragEdge === null || dragEdge === void 0 ? void 0 : dragEdge.mountPoint) !== null && _dragEdge$mountPoint !== void 0 ? _dragEdge$mountPoint : "content" : dragOverEdge.mountPoint;
1090
1153
  var parentEdge = edges.find(item => item.child === dragOverNodeUid);
1091
1154
  /**
1092
1155
  * 如果是根节点, parentUid强制等于rootId
@@ -1109,31 +1172,141 @@ class BuilderDataManager {
1109
1172
 
1110
1173
  sortNodeIds.push(nodeData.id);
1111
1174
  } else if (dragStatus === "top" || dragStatus === "bottom") {
1112
- var _dragEdge = edges.find(edge => edge.child === dragNodeUid);
1175
+ var _dragEdge$child;
1113
1176
 
1114
1177
  var overIndex = sortUids.findIndex(item => item === dragOverNodeUid);
1115
1178
  sortIndex = dragStatus === "top" ? overIndex : overIndex + 1; // 排序修正
1116
1179
 
1117
1180
  sortNodeIds.splice(sortIndex, 0, nodeData.id);
1118
- sortUids.splice(sortIndex, 0, _dragEdge.child);
1181
+ sortUids.splice(sortIndex, 0, (_dragEdge$child = dragEdge === null || dragEdge === void 0 ? void 0 : dragEdge.child) !== null && _dragEdge$child !== void 0 ? _dragEdge$child : null);
1119
1182
  }
1120
1183
 
1184
+ return {
1185
+ parentUid,
1186
+ mountPoint,
1187
+ sortIndex,
1188
+ parnetNodeData,
1189
+ sortUids,
1190
+ sortNodeIds
1191
+ };
1192
+ }
1193
+
1194
+ workbenchNodeAdd(detail) {
1195
+ var {
1196
+ rootId,
1197
+ nodes
1198
+ } = this.data;
1199
+ var {
1200
+ nodeData,
1201
+ dragOverNodeInstanceId,
1202
+ dragStatus
1203
+ } = detail;
1204
+
1205
+ if (nodeData.instanceId) ; else {
1206
+ var _nodes$find;
1207
+
1208
+ // insert
1209
+ var newNodeUid = getUniqueNodeId();
1210
+ var isRoot = dragOverNodeInstanceId === "#main-mount-point";
1211
+ var parentInstanceId = isRoot ? nodes.find(item => item.$$uid === rootId).instanceId : dragOverNodeInstanceId;
1212
+ var dragOverNodeUid = (_nodes$find = nodes.find(item => isRoot ? item.$$uid === rootId : item.instanceId === dragOverNodeInstanceId)) === null || _nodes$find === void 0 ? void 0 : _nodes$find.$$uid;
1213
+ var {
1214
+ parentUid,
1215
+ mountPoint,
1216
+ sortIndex,
1217
+ sortUids: nodeUids,
1218
+ sortNodeIds: nodeIds
1219
+ } = this.getDragInfo({
1220
+ nodeData: {
1221
+ id: null
1222
+ },
1223
+ dragNodeUid: newNodeUid,
1224
+ dragOverNodeUid,
1225
+ dragStatus
1226
+ });
1227
+ nodeData.parent = nodes.find(item => item.$$uid === parentUid).instanceId;
1228
+ nodeData.mountPoint = mountPoint;
1229
+
1230
+ if (nodeData.bricks) {
1231
+ // snippet
1232
+ this.snippetApply({
1233
+ parentUid,
1234
+ nodeDetails: nodeData.bricks.map(brickConf => getSnippetNodeDetail({
1235
+ parent: parentInstanceId,
1236
+ parentUid: parentUid,
1237
+ mountPoint: mountPoint,
1238
+ nodeUid: newNodeUid,
1239
+ brickConf: brickConf,
1240
+ isPortalCanvas: false
1241
+ })),
1242
+ nodeIds,
1243
+ nodeUids
1244
+ });
1245
+ return;
1246
+ }
1247
+
1248
+ this.runAddNodeAction({
1249
+ nodeUid: newNodeUid,
1250
+ parentUid,
1251
+ nodeUids,
1252
+ nodeIds,
1253
+ nodeData,
1254
+ sort: sortIndex
1255
+ });
1256
+ this.eventTarget.dispatchEvent(new CustomEvent(BuilderInternalEventType.NODE_ADD, {
1257
+ detail: {
1258
+ nodeUid: newNodeUid,
1259
+ parentUid,
1260
+ nodeUids,
1261
+ nodeIds,
1262
+ nodeData
1263
+ }
1264
+ }));
1265
+ }
1266
+ }
1267
+
1268
+ workbenchTreeNodeMove(detail) {
1269
+ var {
1270
+ rootId,
1271
+ nodes,
1272
+ edges,
1273
+ wrapperNode
1274
+ } = this.data;
1275
+ var {
1276
+ dragNodeUid,
1277
+ dragOverNodeUid,
1278
+ dragStatus
1279
+ } = detail;
1280
+ var nodeData = nodes.find(item => item.$$uid === dragNodeUid);
1281
+ var {
1282
+ parentUid,
1283
+ parnetNodeData,
1284
+ mountPoint,
1285
+ sortIndex,
1286
+ sortUids: nodeUids,
1287
+ sortNodeIds: nodeIds
1288
+ } = this.getDragInfo({
1289
+ nodeData,
1290
+ dragNodeUid,
1291
+ dragOverNodeUid,
1292
+ dragStatus
1293
+ });
1121
1294
  var newData = {
1122
1295
  rootId,
1123
1296
  nodes,
1124
1297
  edges: edges.filter(edge => edge.child !== dragNodeUid).concat({
1125
- parent: dragParentNodeUid,
1298
+ parent: parentUid,
1126
1299
  child: dragNodeUid,
1127
1300
  mountPoint: mountPoint,
1128
1301
  sort: sortIndex,
1129
- $$isTemplateDelegated: isParentExpandableTemplate(nodes, dragParentNodeUid)
1302
+ $$isTemplateDelegated: isParentExpandableTemplate(nodes, parentUid)
1130
1303
  }),
1131
1304
  wrapperNode
1132
1305
  };
1133
1306
  this.data = _objectSpread(_objectSpread({}, newData), {}, {
1134
1307
  edges: reorderBuilderEdges(newData, {
1135
1308
  parentUid,
1136
- nodeUids: sortUids
1309
+ nodeUids
1137
1310
  })
1138
1311
  });
1139
1312
  this.triggerDataChange();
@@ -1141,7 +1314,7 @@ class BuilderDataManager {
1141
1314
  detail: {
1142
1315
  nodeUid: dragNodeUid,
1143
1316
  nodeInstanceId: nodeData.instanceId,
1144
- nodeIds: sortNodeIds,
1317
+ nodeIds,
1145
1318
  nodeData: {
1146
1319
  parent: parnetNodeData.instanceId,
1147
1320
  mountPoint: mountPoint
@@ -1154,10 +1327,10 @@ class BuilderDataManager {
1154
1327
  */
1155
1328
 
1156
1329
 
1157
- moveMountPoint(_ref3, mountPoint, direction) {
1330
+ moveMountPoint(_ref4, mountPoint, direction) {
1158
1331
  var {
1159
1332
  $$uid: parentUid
1160
- } = _ref3;
1333
+ } = _ref4;
1161
1334
  var {
1162
1335
  relatedEdges,
1163
1336
  mountPoints
@@ -2252,63 +2425,6 @@ function getSortedIdsAfterDropped(droppingInfo) {
2252
2425
  };
2253
2426
  }
2254
2427
 
2255
- var jsonFieldsInBrick = ["properties", "events", "lifeCycle", "params", "if", "transform"];
2256
- var yamlFieldsInBrick = ["permissionsPreCheck", "transformFrom"];
2257
- var ignoredFieldsInBrick = ["brick", "template", "portal", "slots", "id", "children", "instanceId"];
2258
- // The reverse operation of *normalize*.
2259
- // https://github.com/easyops-cn/next-basics/blob/5a6710d567821bcb4a0c71e22d55212193d8b0cb/bricks/next-builder/src/shared/storyboard/buildStoryboard.ts#L394
2260
- function reverseNormalize(brickConf, ctx) {
2261
- return Object.fromEntries(Object.entries(brickConf).map(_ref => {
2262
- var [key, value] = _ref;
2263
- return value === undefined || ignoredFieldsInBrick.includes(key) ? undefined : jsonFieldsInBrick.includes(key) ? [key, JSON.stringify(value)] : yamlFieldsInBrick.includes(key) ? [key, yaml.safeDump(value)] : [key, value];
2264
- }).filter(Boolean).concat(Object.entries(ctx.nodeData), [["brick", ctx.nodeData.type === "template" ? brickConf.template : brickConf.brick], ["portal", ctx.isPortalCanvas || brickConf.portal]]));
2265
- }
2266
-
2267
- function getSnippetNodeDetail(_ref) {
2268
- var {
2269
- parent,
2270
- parentUid,
2271
- mountPoint,
2272
- nodeUid,
2273
- brickConf,
2274
- isPortalCanvas,
2275
- sort
2276
- } = _ref;
2277
- var type = brickConf.template ? "template" : brickConf.bg ? "provider" : "brick";
2278
- return {
2279
- nodeUid,
2280
- parentUid,
2281
- nodeData: reverseNormalize(brickConf, {
2282
- isPortalCanvas,
2283
- nodeData: {
2284
- type,
2285
- parent,
2286
- mountPoint,
2287
- sort
2288
- }
2289
- }),
2290
- children: brickConf.slots ? Object.entries(brickConf.slots).flatMap(_ref2 => {
2291
- var [mountPoint, slotConf] = _ref2;
2292
- return slotConf.bricks.map(childBrickConf => ({
2293
- childBrickConf,
2294
- mountPoint
2295
- }));
2296
- }).map((_ref3, index) => {
2297
- var {
2298
- childBrickConf,
2299
- mountPoint
2300
- } = _ref3;
2301
- return getSnippetNodeDetail({
2302
- parentUid: nodeUid,
2303
- mountPoint,
2304
- nodeUid: getUniqueNodeId(),
2305
- brickConf: childBrickConf,
2306
- sort: index
2307
- });
2308
- }) : []
2309
- };
2310
- }
2311
-
2312
2428
  function processDrop(_ref) {
2313
2429
  var {
2314
2430
  manager,