@tmagic/editor 1.2.3 → 1.2.5

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) {
@@ -2273,6 +2025,7 @@
2273
2025
  await this.select(value.nodeId);
2274
2026
  this.get("stage")?.select(value.nodeId);
2275
2027
  }, 0);
2028
+ this.emit("history-change", value.data);
2276
2029
  }
2277
2030
  async toggleFixedPosition(dist, src, root) {
2278
2031
  const newConfig = lodashEs.cloneDeep(dist);
@@ -2573,6 +2326,32 @@
2573
2326
  }
2574
2327
  ];
2575
2328
 
2329
+ const log = (...args) => {
2330
+ if (process.env.NODE_ENV === "development") {
2331
+ console.log("magic editor: ", ...args);
2332
+ }
2333
+ };
2334
+ const info = (...args) => {
2335
+ if (process.env.NODE_ENV === "development") {
2336
+ console.info("magic editor: ", ...args);
2337
+ }
2338
+ };
2339
+ const warn = (...args) => {
2340
+ if (process.env.NODE_ENV === "development") {
2341
+ console.warn("magic editor: ", ...args);
2342
+ }
2343
+ };
2344
+ const debug = (...args) => {
2345
+ if (process.env.NODE_ENV === "development") {
2346
+ console.debug("magic editor: ", ...args);
2347
+ }
2348
+ };
2349
+ const error = (...args) => {
2350
+ if (process.env.NODE_ENV === "development") {
2351
+ console.error("magic editor: ", ...args);
2352
+ }
2353
+ };
2354
+
2576
2355
  const state = vue.reactive({
2577
2356
  uiSelectMode: false,
2578
2357
  showSrc: false,
@@ -3753,15 +3532,20 @@
3753
3532
  const isShowCodeBlockEditor = vue.computed(() => services?.codeBlockService.getCodeEditorShowStatus() || false);
3754
3533
  const codeCombineInfo = vue.ref(null);
3755
3534
  const getBindCompsByCodeId = (codeId) => {
3756
- if (!codeCombineInfo.value || !codeCombineInfo.value[codeId])
3535
+ if (!codeCombineInfo.value)
3757
3536
  return [];
3758
- const bindCompsId = Object.keys(codeCombineInfo.value[codeId]);
3759
- return bindCompsId.map((compId) => ({
3760
- compId,
3761
- compName: getCompName(compId)
3762
- }));
3537
+ const bindsComp = [];
3538
+ lodashEs.forIn(codeCombineInfo.value, (codeIds, compId) => {
3539
+ if (codeIds.includes(codeId)) {
3540
+ bindsComp.push({
3541
+ compId,
3542
+ compName: getCompName(compId)
3543
+ });
3544
+ }
3545
+ });
3546
+ return bindsComp;
3763
3547
  };
3764
- const initList = async () => {
3548
+ const refreshCodeList = async () => {
3765
3549
  const codeDsl = lodashEs.cloneDeep(await services?.codeBlockService.getCodeDsl()) || null;
3766
3550
  codeCombineInfo.value = lodashEs.cloneDeep(services?.codeBlockService.getCombineInfo()) || null;
3767
3551
  if (!codeDsl || !codeCombineInfo.value)
@@ -3778,12 +3562,18 @@
3778
3562
  });
3779
3563
  };
3780
3564
  vue.watch(
3781
- [() => services?.codeBlockService.getCodeDslSync(), () => services?.codeBlockService.refreshCombineInfo()],
3565
+ () => services?.editorService.get("root"),
3566
+ () => {
3567
+ services?.codeBlockService.refreshAllRelations();
3568
+ refreshCodeList();
3569
+ }
3570
+ );
3571
+ vue.watch(
3572
+ [() => services?.codeBlockService.getCodeDslSync(), () => services?.codeBlockService.getCombineInfo()],
3782
3573
  () => {
3783
- initList();
3574
+ refreshCodeList();
3784
3575
  },
3785
3576
  {
3786
- immediate: true,
3787
3577
  deep: true
3788
3578
  }
3789
3579
  );
@@ -4681,7 +4471,7 @@
4681
4471
  default: vue.withCtx(() => [
4682
4472
  (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(vue.unref(sideBarItems), (config, index) => {
4683
4473
  return vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(vue.unref(uiComponent).component), {
4684
- key: index,
4474
+ key: config.$key || index,
4685
4475
  name: config.text
4686
4476
  }, {
4687
4477
  label: vue.withCtx(() => [
@@ -5816,6 +5606,236 @@
5816
5606
  }
5817
5607
  });
5818
5608
 
5609
+ class CodeBlock extends BaseService {
5610
+ state = vue.reactive({
5611
+ isShowCodeEditor: false,
5612
+ codeDsl: null,
5613
+ id: "",
5614
+ editable: true,
5615
+ combineIds: [],
5616
+ undeletableList: [],
5617
+ relations: {}
5618
+ });
5619
+ constructor() {
5620
+ super([
5621
+ "setCodeDsl",
5622
+ "getCodeDsl",
5623
+ "getCodeContentById",
5624
+ "getCodeDslByIds",
5625
+ "getCurrentDsl",
5626
+ "setCodeDslById",
5627
+ "setCodeEditorShowStatus",
5628
+ "setEditStatus",
5629
+ "setCombineIds",
5630
+ "setUndeleteableList",
5631
+ "deleteCodeDslByIds"
5632
+ ]);
5633
+ }
5634
+ async setCodeDsl(codeDsl2) {
5635
+ this.state.codeDsl = codeDsl2;
5636
+ await editorService.setCodeDsl(this.state.codeDsl);
5637
+ info("[code-block]:code-dsl-change", this.state.codeDsl);
5638
+ this.emit("code-dsl-change", this.state.codeDsl);
5639
+ }
5640
+ async getCodeDsl(forceRefresh = false) {
5641
+ return this.getCodeDslSync(forceRefresh);
5642
+ }
5643
+ getCodeDslSync(forceRefresh = false) {
5644
+ if (!this.state.codeDsl || forceRefresh) {
5645
+ this.state.codeDsl = editorService.getCodeDslSync();
5646
+ }
5647
+ return this.state.codeDsl;
5648
+ }
5649
+ async getCodeContentById(id2) {
5650
+ if (!id2)
5651
+ return null;
5652
+ const totalCodeDsl = await this.getCodeDsl();
5653
+ if (!totalCodeDsl)
5654
+ return null;
5655
+ return totalCodeDsl[id2] ?? null;
5656
+ }
5657
+ async setCodeDslById(id, codeConfig) {
5658
+ let codeDsl = await this.getCodeDsl();
5659
+ const codeConfigProcessed = codeConfig;
5660
+ if (codeConfig.content) {
5661
+ codeConfigProcessed.content = eval(codeConfig.content);
5662
+ }
5663
+ if (!codeDsl) {
5664
+ codeDsl = {
5665
+ [id]: {
5666
+ ...codeConfigProcessed
5667
+ }
5668
+ };
5669
+ } else {
5670
+ const existContent = codeDsl[id] || {};
5671
+ codeDsl = {
5672
+ ...codeDsl,
5673
+ [id]: {
5674
+ ...existContent,
5675
+ ...codeConfigProcessed
5676
+ }
5677
+ };
5678
+ }
5679
+ await this.setCodeDsl(codeDsl);
5680
+ }
5681
+ async getCodeDslByIds(ids) {
5682
+ const codeDsl2 = await this.getCodeDsl();
5683
+ return lodashEs.pick(codeDsl2, ids);
5684
+ }
5685
+ async setCodeEditorShowStatus(status) {
5686
+ this.state.isShowCodeEditor = status;
5687
+ }
5688
+ getCodeEditorShowStatus() {
5689
+ return this.state.isShowCodeEditor;
5690
+ }
5691
+ setCodeEditorContent(status, id2) {
5692
+ if (!id2)
5693
+ return;
5694
+ this.setId(id2);
5695
+ this.state.isShowCodeEditor = status;
5696
+ }
5697
+ async getCurrentDsl() {
5698
+ return await this.getCodeContentById(this.state.id);
5699
+ }
5700
+ getEditStatus() {
5701
+ return this.state.editable;
5702
+ }
5703
+ async setEditStatus(status) {
5704
+ this.state.editable = status;
5705
+ }
5706
+ setId(id2) {
5707
+ if (!id2)
5708
+ return;
5709
+ this.state.id = id2;
5710
+ }
5711
+ getId() {
5712
+ return this.state.id;
5713
+ }
5714
+ async setCombineIds(ids) {
5715
+ this.state.combineIds = ids;
5716
+ }
5717
+ getCombineIds() {
5718
+ return this.state.combineIds;
5719
+ }
5720
+ addCodeRelationListener() {
5721
+ editorService.on("update", (nodes) => {
5722
+ const relations = lodashEs.cloneDeep(this.state.relations);
5723
+ nodes.forEach((node) => {
5724
+ if (node?.id) {
5725
+ relations[node.id] = [];
5726
+ this.getNodeRelation(node, relations);
5727
+ }
5728
+ });
5729
+ this.state.relations = { ...relations };
5730
+ });
5731
+ editorService.on("remove", (nodes) => {
5732
+ nodes.forEach((node) => {
5733
+ this.state.relations = this.deleteNodeRelation(node, this.state.relations);
5734
+ });
5735
+ });
5736
+ editorService.on("history-change", (page) => {
5737
+ const relations = {};
5738
+ this.getNodeRelation(page, relations, true);
5739
+ this.state.relations = relations;
5740
+ });
5741
+ }
5742
+ refreshAllRelations() {
5743
+ const root = editorService.get("root");
5744
+ if (!root)
5745
+ return;
5746
+ const relations = {};
5747
+ this.getNodeRelation(root, relations, true);
5748
+ this.state.relations = relations;
5749
+ }
5750
+ getCombineInfo() {
5751
+ return this.state.relations;
5752
+ }
5753
+ getUndeletableList() {
5754
+ return this.state.undeletableList;
5755
+ }
5756
+ async setUndeleteableList(codeIds) {
5757
+ this.state.undeletableList = codeIds;
5758
+ }
5759
+ setCodeDraft(codeId, content) {
5760
+ globalThis.localStorage.setItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`, content);
5761
+ }
5762
+ getCodeDraft(codeId) {
5763
+ return globalThis.localStorage.getItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
5764
+ }
5765
+ removeCodeDraft(codeId) {
5766
+ globalThis.localStorage.removeItem(`${CODE_DRAFT_STORAGE_KEY}_${codeId}`);
5767
+ }
5768
+ async deleteCodeDslByIds(codeIds) {
5769
+ const currentDsl = await this.getCodeDsl();
5770
+ const newDsl = lodashEs.omit(currentDsl, codeIds);
5771
+ await this.setCodeDsl(newDsl);
5772
+ return newDsl;
5773
+ }
5774
+ async getUniqueId() {
5775
+ const newId = `code_${Math.random().toString(10).substring(2).substring(0, 4)}`;
5776
+ const dsl = await this.getCodeDsl();
5777
+ const existedIds = lodashEs.keys(dsl);
5778
+ if (!existedIds.includes(newId))
5779
+ return newId;
5780
+ return await this.getUniqueId();
5781
+ }
5782
+ resetState() {
5783
+ this.state.isShowCodeEditor = false;
5784
+ this.state.codeDsl = null;
5785
+ this.state.id = "";
5786
+ this.state.editable = true;
5787
+ this.state.combineIds = [];
5788
+ this.state.undeletableList = [];
5789
+ }
5790
+ destroy() {
5791
+ this.resetState();
5792
+ this.removeAllListeners();
5793
+ this.removeAllPlugins();
5794
+ }
5795
+ getNodeRelation(node, relation, deep = false) {
5796
+ lodashEs.forIn(node, (value, key) => {
5797
+ if (value?.hookType === schema.HookType.CODE && !lodashEs.isEmpty(value.hookData)) {
5798
+ value.hookData.forEach((relationItem) => {
5799
+ if (relationItem.codeId) {
5800
+ relation[node.id] = lodashEs.union(relation[node.id], [relationItem.codeId]);
5801
+ }
5802
+ });
5803
+ return;
5804
+ }
5805
+ let isContinue = false;
5806
+ try {
5807
+ isContinue = key !== "items" && typeof value === "object" && JSON.stringify(value).includes("hookType");
5808
+ } catch (error) {
5809
+ console.error(error);
5810
+ }
5811
+ if (isContinue) {
5812
+ const unConfirmedValue = {
5813
+ id: node.id,
5814
+ ...value
5815
+ };
5816
+ this.getNodeRelation(unConfirmedValue, relation);
5817
+ }
5818
+ if (key === "items" && !lodashEs.isEmpty(value) && deep) {
5819
+ value.forEach((item) => {
5820
+ this.getNodeRelation(item, relation, deep);
5821
+ });
5822
+ }
5823
+ });
5824
+ }
5825
+ deleteNodeRelation(node, relations) {
5826
+ if (!node.id)
5827
+ return {};
5828
+ let newRelations = lodashEs.omit(relations, [node.id]);
5829
+ if (!lodashEs.isEmpty(node.items)) {
5830
+ node.items.forEach((item) => {
5831
+ newRelations = this.deleteNodeRelation(item, newRelations);
5832
+ });
5833
+ }
5834
+ return newRelations;
5835
+ }
5836
+ }
5837
+ const codeBlockService = new CodeBlock();
5838
+
5819
5839
  class ComponentList extends BaseService {
5820
5840
  state = vue.reactive({
5821
5841
  list: []
@@ -6050,6 +6070,7 @@
6050
6070
  disabledDragStart: props.disabledDragStart
6051
6071
  })
6052
6072
  );
6073
+ codeBlockService.addCodeRelationListener();
6053
6074
  return services;
6054
6075
  }
6055
6076
  });