@tmagic/editor 1.2.3 → 1.2.4

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.
@@ -721,62 +721,51 @@
721
721
  }
722
722
  });
723
723
 
724
- var ColumnLayout = /* @__PURE__ */ ((ColumnLayout2) => {
725
- ColumnLayout2["LEFT"] = "left";
726
- ColumnLayout2["CENTER"] = "center";
727
- ColumnLayout2["RIGHT"] = "right";
728
- return ColumnLayout2;
729
- })(ColumnLayout || {});
730
- var LayerOffset = /* @__PURE__ */ ((LayerOffset2) => {
731
- LayerOffset2["TOP"] = "top";
732
- LayerOffset2["BOTTOM"] = "bottom";
733
- return LayerOffset2;
734
- })(LayerOffset || {});
735
- var Layout = /* @__PURE__ */ ((Layout2) => {
736
- Layout2["FLEX"] = "flex";
737
- Layout2["FIXED"] = "fixed";
738
- Layout2["RELATIVE"] = "relative";
739
- Layout2["ABSOLUTE"] = "absolute";
740
- return Layout2;
741
- })(Layout || {});
742
- var Keys = /* @__PURE__ */ ((Keys2) => {
743
- Keys2["ESCAPE"] = "Space";
744
- return Keys2;
745
- })(Keys || {});
746
- const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
747
- const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
748
- var CodeDeleteErrorType = /* @__PURE__ */ ((CodeDeleteErrorType2) => {
749
- CodeDeleteErrorType2["UNDELETEABLE"] = "undeleteable";
750
- CodeDeleteErrorType2["BIND"] = "bind";
751
- return CodeDeleteErrorType2;
752
- })(CodeDeleteErrorType || {});
753
- const CODE_DRAFT_STORAGE_KEY = "magicCodeDraft";
754
-
755
- const log = (...args) => {
756
- if (process.env.NODE_ENV === "development") {
757
- console.log("magic editor: ", ...args);
724
+ class UndoRedo {
725
+ elementList;
726
+ listCursor;
727
+ listMaxSize;
728
+ constructor(listMaxSize = 200) {
729
+ const minListMaxSize = 2;
730
+ this.elementList = [];
731
+ this.listCursor = 0;
732
+ this.listMaxSize = listMaxSize > minListMaxSize ? listMaxSize : minListMaxSize;
758
733
  }
759
- };
760
- const info = (...args) => {
761
- if (process.env.NODE_ENV === "development") {
762
- console.info("magic editor: ", ...args);
734
+ pushElement(element) {
735
+ this.elementList.splice(this.listCursor, this.elementList.length - this.listCursor, lodashEs.cloneDeep(element));
736
+ this.listCursor += 1;
737
+ if (this.elementList.length > this.listMaxSize) {
738
+ this.elementList.shift();
739
+ this.listCursor -= 1;
740
+ }
763
741
  }
764
- };
765
- const warn = (...args) => {
766
- if (process.env.NODE_ENV === "development") {
767
- console.warn("magic editor: ", ...args);
742
+ canUndo() {
743
+ return this.listCursor > 1;
768
744
  }
769
- };
770
- const debug = (...args) => {
771
- if (process.env.NODE_ENV === "development") {
772
- console.debug("magic editor: ", ...args);
745
+ undo() {
746
+ if (!this.canUndo()) {
747
+ return null;
748
+ }
749
+ this.listCursor -= 1;
750
+ return this.getCurrentElement();
773
751
  }
774
- };
775
- const error = (...args) => {
776
- if (process.env.NODE_ENV === "development") {
777
- console.error("magic editor: ", ...args);
752
+ canRedo() {
753
+ return this.elementList.length > this.listCursor;
778
754
  }
779
- };
755
+ redo() {
756
+ if (!this.canRedo()) {
757
+ return null;
758
+ }
759
+ this.listCursor += 1;
760
+ return this.getCurrentElement();
761
+ }
762
+ getCurrentElement() {
763
+ if (this.listCursor < 1) {
764
+ return null;
765
+ }
766
+ return lodashEs.cloneDeep(this.elementList[this.listCursor - 1]);
767
+ }
768
+ }
780
769
 
781
770
  const compose = (middleware) => {
782
771
  if (!Array.isArray(middleware))
@@ -900,272 +889,6 @@
900
889
  }
901
890
  }
902
891
 
903
- class CodeBlock extends BaseService {
904
- state = vue.reactive({
905
- isShowCodeEditor: false,
906
- codeDsl: null,
907
- id: "",
908
- editable: true,
909
- combineIds: [],
910
- undeletableList: [],
911
- relations: {}
912
- });
913
- constructor() {
914
- super([
915
- "setCodeDsl",
916
- "getCodeDsl",
917
- "getCodeContentById",
918
- "getCodeDslByIds",
919
- "getCurrentDsl",
920
- "setCodeDslById",
921
- "setCodeEditorShowStatus",
922
- "setEditStatus",
923
- "setCombineIds",
924
- "setUndeleteableList",
925
- "deleteCodeDslByIds"
926
- ]);
927
- }
928
- async setCodeDsl(codeDsl2) {
929
- this.state.codeDsl = codeDsl2;
930
- await editorService.setCodeDsl(this.state.codeDsl);
931
- info("[code-block]:code-dsl-change", this.state.codeDsl);
932
- this.emit("code-dsl-change", this.state.codeDsl);
933
- }
934
- async getCodeDsl(forceRefresh = false) {
935
- return this.getCodeDslSync(forceRefresh);
936
- }
937
- getCodeDslSync(forceRefresh = false) {
938
- if (!this.state.codeDsl || forceRefresh) {
939
- this.state.codeDsl = editorService.getCodeDslSync();
940
- }
941
- return this.state.codeDsl;
942
- }
943
- async getCodeContentById(id2) {
944
- if (!id2)
945
- return null;
946
- const totalCodeDsl = await this.getCodeDsl();
947
- if (!totalCodeDsl)
948
- return null;
949
- return totalCodeDsl[id2] ?? null;
950
- }
951
- async setCodeDslById(id, codeConfig) {
952
- let codeDsl = await this.getCodeDsl();
953
- const codeConfigProcessed = codeConfig;
954
- if (codeConfig.content) {
955
- codeConfigProcessed.content = eval(codeConfig.content);
956
- }
957
- if (!codeDsl) {
958
- codeDsl = {
959
- [id]: {
960
- ...codeConfigProcessed
961
- }
962
- };
963
- } else {
964
- const existContent = codeDsl[id] || {};
965
- codeDsl = {
966
- ...codeDsl,
967
- [id]: {
968
- ...existContent,
969
- ...codeConfigProcessed
970
- }
971
- };
972
- }
973
- await this.setCodeDsl(codeDsl);
974
- }
975
- async getCodeDslByIds(ids) {
976
- const codeDsl2 = await this.getCodeDsl();
977
- return lodashEs.pick(codeDsl2, ids);
978
- }
979
- async setCodeEditorShowStatus(status) {
980
- this.state.isShowCodeEditor = status;
981
- }
982
- getCodeEditorShowStatus() {
983
- return this.state.isShowCodeEditor;
984
- }
985
- setCodeEditorContent(status, id2) {
986
- if (!id2)
987
- return;
988
- this.setId(id2);
989
- this.state.isShowCodeEditor = status;
990
- }
991
- async getCurrentDsl() {
992
- return await this.getCodeContentById(this.state.id);
993
- }
994
- getEditStatus() {
995
- return this.state.editable;
996
- }
997
- async setEditStatus(status) {
998
- this.state.editable = status;
999
- }
1000
- setId(id2) {
1001
- if (!id2)
1002
- return;
1003
- this.state.id = id2;
1004
- }
1005
- getId() {
1006
- return this.state.id;
1007
- }
1008
- async setCombineIds(ids) {
1009
- this.state.combineIds = ids;
1010
- }
1011
- getCombineIds() {
1012
- return this.state.combineIds;
1013
- }
1014
- refreshCombineInfo() {
1015
- const root = editorService.get("root");
1016
- if (!root)
1017
- return null;
1018
- const relations = {};
1019
- this.recurseMNode(root, relations);
1020
- this.state.relations = relations;
1021
- return this.state.relations;
1022
- }
1023
- getCombineInfo() {
1024
- return this.state.relations;
1025
- }
1026
- getUndeletableList() {
1027
- return this.state.undeletableList;
1028
- }
1029
- async setUndeleteableList(codeIds) {
1030
- this.state.undeletableList = codeIds;
1031
- }
1032
- setCodeDraft(codeId, content) {
1033
- globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
1034
- }
1035
- getCodeDraft(codeId) {
1036
- return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
1037
- }
1038
- removeCodeDraft(codeId) {
1039
- globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
1040
- }
1041
- async deleteCodeDslByIds(codeIds) {
1042
- const currentDsl = await this.getCodeDsl();
1043
- const newDsl = lodashEs.omit(currentDsl, codeIds);
1044
- await this.setCodeDsl(newDsl);
1045
- return newDsl;
1046
- }
1047
- async getUniqueId() {
1048
- const newId = `code_${Math.random().toString(10).substring(2).substring(0, 4)}`;
1049
- const dsl = await this.getCodeDsl();
1050
- const existedIds = lodashEs.keys(dsl);
1051
- if (!existedIds.includes(newId))
1052
- return newId;
1053
- return await this.getUniqueId();
1054
- }
1055
- async deleteCompsInRelation(node) {
1056
- const codeDsl2 = lodashEs.cloneDeep(await this.getCodeDsl());
1057
- if (!codeDsl2)
1058
- return;
1059
- this.refreshRelationDeep(node, codeDsl2);
1060
- this.setCodeDsl(codeDsl2);
1061
- }
1062
- resetState() {
1063
- this.state.isShowCodeEditor = false;
1064
- this.state.codeDsl = null;
1065
- this.state.id = "";
1066
- this.state.editable = true;
1067
- this.state.combineIds = [];
1068
- this.state.undeletableList = [];
1069
- }
1070
- destroy() {
1071
- this.resetState();
1072
- this.removeAllListeners();
1073
- this.removeAllPlugins();
1074
- }
1075
- refreshRelationDeep(node, codeDsl2) {
1076
- if (!node.id)
1077
- return;
1078
- lodashEs.forIn(codeDsl2, (codeBlockContent) => {
1079
- const compsContent = codeBlockContent.comps || {};
1080
- codeBlockContent.comps = lodashEs.omit(compsContent, node.id);
1081
- });
1082
- if (!lodashEs.isEmpty(node.items)) {
1083
- node.items.forEach((item) => {
1084
- this.refreshRelationDeep(item, codeDsl2);
1085
- });
1086
- }
1087
- }
1088
- recurseMNode(node, relations) {
1089
- lodashEs.forIn(node, (value, key) => {
1090
- let unConfirmedValue = { id: node.id };
1091
- if (value?.hookType === schema.HookType.CODE && !lodashEs.isEmpty(value.hookData)) {
1092
- value.hookData.forEach((relationItem) => {
1093
- if (!relationItem.codeId)
1094
- return;
1095
- if (!relations[relationItem.codeId]) {
1096
- relations[relationItem.codeId] = {};
1097
- }
1098
- const codeItem = relations[relationItem.codeId];
1099
- if (lodashEs.isEmpty(codeItem[node.id])) {
1100
- codeItem[node.id] = [];
1101
- }
1102
- codeItem[node.id].push(key);
1103
- });
1104
- return;
1105
- }
1106
- if (typeof value === "object") {
1107
- unConfirmedValue = {
1108
- ...unConfirmedValue,
1109
- ...value
1110
- };
1111
- this.recurseMNode(unConfirmedValue, relations);
1112
- }
1113
- });
1114
- if (!lodashEs.isEmpty(node.items)) {
1115
- node.items.forEach((item) => {
1116
- this.recurseMNode(item, relations);
1117
- });
1118
- }
1119
- }
1120
- }
1121
- const codeBlockService = new CodeBlock();
1122
-
1123
- class UndoRedo {
1124
- elementList;
1125
- listCursor;
1126
- listMaxSize;
1127
- constructor(listMaxSize = 200) {
1128
- const minListMaxSize = 2;
1129
- this.elementList = [];
1130
- this.listCursor = 0;
1131
- this.listMaxSize = listMaxSize > minListMaxSize ? listMaxSize : minListMaxSize;
1132
- }
1133
- pushElement(element) {
1134
- this.elementList.splice(this.listCursor, this.elementList.length - this.listCursor, lodashEs.cloneDeep(element));
1135
- this.listCursor += 1;
1136
- if (this.elementList.length > this.listMaxSize) {
1137
- this.elementList.shift();
1138
- this.listCursor -= 1;
1139
- }
1140
- }
1141
- canUndo() {
1142
- return this.listCursor > 1;
1143
- }
1144
- undo() {
1145
- if (!this.canUndo()) {
1146
- return null;
1147
- }
1148
- this.listCursor -= 1;
1149
- return this.getCurrentElement();
1150
- }
1151
- canRedo() {
1152
- return this.elementList.length > this.listCursor;
1153
- }
1154
- redo() {
1155
- if (!this.canRedo()) {
1156
- return null;
1157
- }
1158
- this.listCursor += 1;
1159
- return this.getCurrentElement();
1160
- }
1161
- getCurrentElement() {
1162
- if (this.listCursor < 1) {
1163
- return null;
1164
- }
1165
- return lodashEs.cloneDeep(this.elementList[this.listCursor - 1]);
1166
- }
1167
- }
1168
-
1169
892
  class History extends BaseService {
1170
893
  state = vue.reactive({
1171
894
  pageSteps: {},
@@ -1334,6 +1057,37 @@
1334
1057
  }
1335
1058
  const storageService = new WebStorage();
1336
1059
 
1060
+ var ColumnLayout = /* @__PURE__ */ ((ColumnLayout2) => {
1061
+ ColumnLayout2["LEFT"] = "left";
1062
+ ColumnLayout2["CENTER"] = "center";
1063
+ ColumnLayout2["RIGHT"] = "right";
1064
+ return ColumnLayout2;
1065
+ })(ColumnLayout || {});
1066
+ var LayerOffset = /* @__PURE__ */ ((LayerOffset2) => {
1067
+ LayerOffset2["TOP"] = "top";
1068
+ LayerOffset2["BOTTOM"] = "bottom";
1069
+ return LayerOffset2;
1070
+ })(LayerOffset || {});
1071
+ var Layout = /* @__PURE__ */ ((Layout2) => {
1072
+ Layout2["FLEX"] = "flex";
1073
+ Layout2["FIXED"] = "fixed";
1074
+ Layout2["RELATIVE"] = "relative";
1075
+ Layout2["ABSOLUTE"] = "absolute";
1076
+ return Layout2;
1077
+ })(Layout || {});
1078
+ var Keys = /* @__PURE__ */ ((Keys2) => {
1079
+ Keys2["ESCAPE"] = "Space";
1080
+ return Keys2;
1081
+ })(Keys || {});
1082
+ const H_GUIDE_LINE_STORAGE_KEY = "$MagicStageHorizontalGuidelinesData";
1083
+ const V_GUIDE_LINE_STORAGE_KEY = "$MagicStageVerticalGuidelinesData";
1084
+ var CodeDeleteErrorType = /* @__PURE__ */ ((CodeDeleteErrorType2) => {
1085
+ CodeDeleteErrorType2["UNDELETEABLE"] = "undeleteable";
1086
+ CodeDeleteErrorType2["BIND"] = "bind";
1087
+ return CodeDeleteErrorType2;
1088
+ })(CodeDeleteErrorType || {});
1089
+ const CODE_DRAFT_STORAGE_KEY = "magicCodeDraft";
1090
+
1337
1091
  const COPY_STORAGE_KEY = "$MagicEditorCopyData";
1338
1092
  const getPageList = (app) => {
1339
1093
  if (app.items && Array.isArray(app.items)) {
@@ -1966,7 +1720,6 @@
1966
1720
  stage?.select(parent.id);
1967
1721
  }
1968
1722
  this.addModifiedNodeId(parent.id);
1969
- codeBlockService.deleteCompsInRelation(node);
1970
1723
  }
1971
1724
  async remove(nodeOrNodeList) {
1972
1725
  const nodes = Array.isArray(nodeOrNodeList) ? nodeOrNodeList : [nodeOrNodeList];
@@ -2034,7 +1787,6 @@
2034
1787
  const newNodes = await Promise.all(nodes.map((node) => this.doUpdate(node)));
2035
1788
  this.pushHistoryState();
2036
1789
  this.emit("update", newNodes);
2037
- codeBlockService.refreshCombineInfo();
2038
1790
  return Array.isArray(config) ? newNodes : newNodes[0];
2039
1791
  }
2040
1792
  async sort(id1, id2) {
@@ -2573,6 +2325,32 @@
2573
2325
  }
2574
2326
  ];
2575
2327
 
2328
+ const log = (...args) => {
2329
+ if (process.env.NODE_ENV === "development") {
2330
+ console.log("magic editor: ", ...args);
2331
+ }
2332
+ };
2333
+ const info = (...args) => {
2334
+ if (process.env.NODE_ENV === "development") {
2335
+ console.info("magic editor: ", ...args);
2336
+ }
2337
+ };
2338
+ const warn = (...args) => {
2339
+ if (process.env.NODE_ENV === "development") {
2340
+ console.warn("magic editor: ", ...args);
2341
+ }
2342
+ };
2343
+ const debug = (...args) => {
2344
+ if (process.env.NODE_ENV === "development") {
2345
+ console.debug("magic editor: ", ...args);
2346
+ }
2347
+ };
2348
+ const error = (...args) => {
2349
+ if (process.env.NODE_ENV === "development") {
2350
+ console.error("magic editor: ", ...args);
2351
+ }
2352
+ };
2353
+
2576
2354
  const state = vue.reactive({
2577
2355
  uiSelectMode: false,
2578
2356
  showSrc: false,
@@ -3753,15 +3531,20 @@
3753
3531
  const isShowCodeBlockEditor = vue.computed(() => services?.codeBlockService.getCodeEditorShowStatus() || false);
3754
3532
  const codeCombineInfo = vue.ref(null);
3755
3533
  const getBindCompsByCodeId = (codeId) => {
3756
- if (!codeCombineInfo.value || !codeCombineInfo.value[codeId])
3534
+ if (!codeCombineInfo.value)
3757
3535
  return [];
3758
- const bindCompsId = Object.keys(codeCombineInfo.value[codeId]);
3759
- return bindCompsId.map((compId) => ({
3760
- compId,
3761
- compName: getCompName(compId)
3762
- }));
3536
+ const bindsComp = [];
3537
+ lodashEs.forIn(codeCombineInfo.value, (codeIds, compId) => {
3538
+ if (codeIds.includes(codeId)) {
3539
+ bindsComp.push({
3540
+ compId,
3541
+ compName: getCompName(compId)
3542
+ });
3543
+ }
3544
+ });
3545
+ return bindsComp;
3763
3546
  };
3764
- const initList = async () => {
3547
+ const refreshCodeList = async () => {
3765
3548
  const codeDsl = lodashEs.cloneDeep(await services?.codeBlockService.getCodeDsl()) || null;
3766
3549
  codeCombineInfo.value = lodashEs.cloneDeep(services?.codeBlockService.getCombineInfo()) || null;
3767
3550
  if (!codeDsl || !codeCombineInfo.value)
@@ -3777,13 +3560,16 @@
3777
3560
  });
3778
3561
  });
3779
3562
  };
3563
+ vue.onMounted(() => {
3564
+ services?.codeBlockService.refreshAllRelations();
3565
+ refreshCodeList();
3566
+ });
3780
3567
  vue.watch(
3781
- [() => services?.codeBlockService.getCodeDslSync(), () => services?.codeBlockService.refreshCombineInfo()],
3568
+ [() => services?.codeBlockService.getCodeDslSync(), () => services?.codeBlockService.getCombineInfo()],
3782
3569
  () => {
3783
- initList();
3570
+ refreshCodeList();
3784
3571
  },
3785
3572
  {
3786
- immediate: true,
3787
3573
  deep: true
3788
3574
  }
3789
3575
  );
@@ -4681,7 +4467,7 @@
4681
4467
  default: vue.withCtx(() => [
4682
4468
  (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(vue.unref(sideBarItems), (config, index) => {
4683
4469
  return vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(uiComponent).component), {
4684
- key: index,
4470
+ key: config.$key || index,
4685
4471
  name: config.text
4686
4472
  }, {
4687
4473
  label: vue.withCtx(() => [
@@ -5816,6 +5602,231 @@
5816
5602
  }
5817
5603
  });
5818
5604
 
5605
+ class CodeBlock extends BaseService {
5606
+ state = vue.reactive({
5607
+ isShowCodeEditor: false,
5608
+ codeDsl: null,
5609
+ id: "",
5610
+ editable: true,
5611
+ combineIds: [],
5612
+ undeletableList: [],
5613
+ relations: {}
5614
+ });
5615
+ constructor() {
5616
+ super([
5617
+ "setCodeDsl",
5618
+ "getCodeDsl",
5619
+ "getCodeContentById",
5620
+ "getCodeDslByIds",
5621
+ "getCurrentDsl",
5622
+ "setCodeDslById",
5623
+ "setCodeEditorShowStatus",
5624
+ "setEditStatus",
5625
+ "setCombineIds",
5626
+ "setUndeleteableList",
5627
+ "deleteCodeDslByIds"
5628
+ ]);
5629
+ }
5630
+ async setCodeDsl(codeDsl2) {
5631
+ this.state.codeDsl = codeDsl2;
5632
+ await editorService.setCodeDsl(this.state.codeDsl);
5633
+ info("[code-block]:code-dsl-change", this.state.codeDsl);
5634
+ this.emit("code-dsl-change", this.state.codeDsl);
5635
+ }
5636
+ async getCodeDsl(forceRefresh = false) {
5637
+ return this.getCodeDslSync(forceRefresh);
5638
+ }
5639
+ getCodeDslSync(forceRefresh = false) {
5640
+ if (!this.state.codeDsl || forceRefresh) {
5641
+ this.state.codeDsl = editorService.getCodeDslSync();
5642
+ }
5643
+ return this.state.codeDsl;
5644
+ }
5645
+ async getCodeContentById(id2) {
5646
+ if (!id2)
5647
+ return null;
5648
+ const totalCodeDsl = await this.getCodeDsl();
5649
+ if (!totalCodeDsl)
5650
+ return null;
5651
+ return totalCodeDsl[id2] ?? null;
5652
+ }
5653
+ async setCodeDslById(id, codeConfig) {
5654
+ let codeDsl = await this.getCodeDsl();
5655
+ const codeConfigProcessed = codeConfig;
5656
+ if (codeConfig.content) {
5657
+ codeConfigProcessed.content = eval(codeConfig.content);
5658
+ }
5659
+ if (!codeDsl) {
5660
+ codeDsl = {
5661
+ [id]: {
5662
+ ...codeConfigProcessed
5663
+ }
5664
+ };
5665
+ } else {
5666
+ const existContent = codeDsl[id] || {};
5667
+ codeDsl = {
5668
+ ...codeDsl,
5669
+ [id]: {
5670
+ ...existContent,
5671
+ ...codeConfigProcessed
5672
+ }
5673
+ };
5674
+ }
5675
+ await this.setCodeDsl(codeDsl);
5676
+ }
5677
+ async getCodeDslByIds(ids) {
5678
+ const codeDsl2 = await this.getCodeDsl();
5679
+ return lodashEs.pick(codeDsl2, ids);
5680
+ }
5681
+ async setCodeEditorShowStatus(status) {
5682
+ this.state.isShowCodeEditor = status;
5683
+ }
5684
+ getCodeEditorShowStatus() {
5685
+ return this.state.isShowCodeEditor;
5686
+ }
5687
+ setCodeEditorContent(status, id2) {
5688
+ if (!id2)
5689
+ return;
5690
+ this.setId(id2);
5691
+ this.state.isShowCodeEditor = status;
5692
+ }
5693
+ async getCurrentDsl() {
5694
+ return await this.getCodeContentById(this.state.id);
5695
+ }
5696
+ getEditStatus() {
5697
+ return this.state.editable;
5698
+ }
5699
+ async setEditStatus(status) {
5700
+ this.state.editable = status;
5701
+ }
5702
+ setId(id2) {
5703
+ if (!id2)
5704
+ return;
5705
+ this.state.id = id2;
5706
+ }
5707
+ getId() {
5708
+ return this.state.id;
5709
+ }
5710
+ async setCombineIds(ids) {
5711
+ this.state.combineIds = ids;
5712
+ }
5713
+ getCombineIds() {
5714
+ return this.state.combineIds;
5715
+ }
5716
+ addCodeRelationListener() {
5717
+ editorService.on("update", (nodes) => {
5718
+ const relations = lodashEs.cloneDeep(this.state.relations);
5719
+ nodes.forEach((node) => {
5720
+ if (node?.id) {
5721
+ relations[node.id] = [];
5722
+ this.getNodeRelation(node, relations);
5723
+ }
5724
+ });
5725
+ this.state.relations = { ...relations };
5726
+ });
5727
+ editorService.on("remove", (nodes) => {
5728
+ nodes.forEach((node) => {
5729
+ this.state.relations = this.deleteNodeRelation(node, this.state.relations);
5730
+ });
5731
+ });
5732
+ }
5733
+ refreshAllRelations() {
5734
+ const root = editorService.get("root");
5735
+ if (!root)
5736
+ return;
5737
+ const relations = {};
5738
+ this.getNodeRelation(root, relations, true);
5739
+ this.state.relations = relations;
5740
+ }
5741
+ getCombineInfo() {
5742
+ return this.state.relations;
5743
+ }
5744
+ getUndeletableList() {
5745
+ return this.state.undeletableList;
5746
+ }
5747
+ async setUndeleteableList(codeIds) {
5748
+ this.state.undeletableList = codeIds;
5749
+ }
5750
+ setCodeDraft(codeId, content) {
5751
+ globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
5752
+ }
5753
+ getCodeDraft(codeId) {
5754
+ return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
5755
+ }
5756
+ removeCodeDraft(codeId) {
5757
+ globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
5758
+ }
5759
+ async deleteCodeDslByIds(codeIds) {
5760
+ const currentDsl = await this.getCodeDsl();
5761
+ const newDsl = lodashEs.omit(currentDsl, codeIds);
5762
+ await this.setCodeDsl(newDsl);
5763
+ return newDsl;
5764
+ }
5765
+ async getUniqueId() {
5766
+ const newId = `code_${Math.random().toString(10).substring(2).substring(0, 4)}`;
5767
+ const dsl = await this.getCodeDsl();
5768
+ const existedIds = lodashEs.keys(dsl);
5769
+ if (!existedIds.includes(newId))
5770
+ return newId;
5771
+ return await this.getUniqueId();
5772
+ }
5773
+ resetState() {
5774
+ this.state.isShowCodeEditor = false;
5775
+ this.state.codeDsl = null;
5776
+ this.state.id = "";
5777
+ this.state.editable = true;
5778
+ this.state.combineIds = [];
5779
+ this.state.undeletableList = [];
5780
+ }
5781
+ destroy() {
5782
+ this.resetState();
5783
+ this.removeAllListeners();
5784
+ this.removeAllPlugins();
5785
+ }
5786
+ getNodeRelation(node, relation, deep = false) {
5787
+ lodashEs.forIn(node, (value, key) => {
5788
+ if (value?.hookType === schema.HookType.CODE && !lodashEs.isEmpty(value.hookData)) {
5789
+ value.hookData.forEach((relationItem) => {
5790
+ if (relationItem.codeId) {
5791
+ relation[node.id] = lodashEs.union(relation[node.id], [relationItem.codeId]);
5792
+ }
5793
+ });
5794
+ return;
5795
+ }
5796
+ let isContinue = false;
5797
+ try {
5798
+ isContinue = key !== "items" && typeof value === "object" && JSON.stringify(value).includes("hookType");
5799
+ } catch (error) {
5800
+ console.error(error);
5801
+ }
5802
+ if (isContinue) {
5803
+ const unConfirmedValue = {
5804
+ id: node.id,
5805
+ ...value
5806
+ };
5807
+ this.getNodeRelation(unConfirmedValue, relation);
5808
+ }
5809
+ if (key === "items" && !lodashEs.isEmpty(value) && deep) {
5810
+ value.forEach((item) => {
5811
+ this.getNodeRelation(item, relation, deep);
5812
+ });
5813
+ }
5814
+ });
5815
+ }
5816
+ deleteNodeRelation(node, relations) {
5817
+ if (!node.id)
5818
+ return {};
5819
+ let newRelations = lodashEs.omit(relations, [node.id]);
5820
+ if (!lodashEs.isEmpty(node.items)) {
5821
+ node.items.forEach((item) => {
5822
+ newRelations = this.deleteNodeRelation(item, newRelations);
5823
+ });
5824
+ }
5825
+ return newRelations;
5826
+ }
5827
+ }
5828
+ const codeBlockService = new CodeBlock();
5829
+
5819
5830
  class ComponentList extends BaseService {
5820
5831
  state = vue.reactive({
5821
5832
  list: []
@@ -6050,6 +6061,7 @@
6050
6061
  disabledDragStart: props.disabledDragStart
6051
6062
  })
6052
6063
  );
6064
+ codeBlockService.addCodeRelationListener();
6053
6065
  return services;
6054
6066
  }
6055
6067
  });