@tmagic/editor 1.1.6 → 1.2.0-beta.10

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.
Files changed (82) hide show
  1. package/dist/style.css +191 -8
  2. package/dist/tmagic-editor.mjs +2104 -1027
  3. package/dist/tmagic-editor.mjs.map +1 -1
  4. package/dist/tmagic-editor.umd.js +2136 -1053
  5. package/dist/tmagic-editor.umd.js.map +1 -1
  6. package/package.json +11 -10
  7. package/src/Editor.vue +27 -12
  8. package/src/components/ContentMenu.vue +4 -4
  9. package/src/components/Icon.vue +7 -5
  10. package/src/{layouts → components}/Layout.vue +15 -5
  11. package/src/components/ScrollBar.vue +1 -1
  12. package/src/components/ScrollViewer.vue +10 -1
  13. package/src/components/ToolButton.vue +30 -15
  14. package/src/fields/Code.vue +5 -2
  15. package/src/fields/CodeLink.vue +20 -12
  16. package/src/fields/CodeSelect.vue +131 -0
  17. package/src/fields/UISelect.vue +9 -8
  18. package/src/index.ts +7 -2
  19. package/src/layouts/AddPageBox.vue +13 -20
  20. package/src/layouts/CodeEditor.vue +106 -120
  21. package/src/layouts/Framework.vue +62 -40
  22. package/src/layouts/NavMenu.vue +1 -1
  23. package/src/layouts/PropsPanel.vue +5 -10
  24. package/src/layouts/Resizer.vue +1 -1
  25. package/src/layouts/sidebar/ComponentListPanel.vue +15 -13
  26. package/src/layouts/sidebar/LayerMenu.vue +89 -92
  27. package/src/layouts/sidebar/LayerPanel.vue +273 -178
  28. package/src/layouts/sidebar/Sidebar.vue +129 -37
  29. package/src/layouts/sidebar/code-block/CodeBlockEditor.vue +169 -0
  30. package/src/layouts/sidebar/code-block/CodeBlockList.vue +267 -0
  31. package/src/layouts/workspace/Breadcrumb.vue +32 -0
  32. package/src/layouts/workspace/PageBar.vue +12 -11
  33. package/src/layouts/workspace/PageBarScrollContainer.vue +1 -1
  34. package/src/layouts/workspace/Stage.vue +15 -7
  35. package/src/layouts/workspace/ViewerMenu.vue +8 -7
  36. package/src/layouts/workspace/Workspace.vue +10 -3
  37. package/src/services/codeBlock.ts +415 -0
  38. package/src/services/editor.ts +35 -5
  39. package/src/theme/breadcrumb.scss +6 -0
  40. package/src/theme/code-block.scss +184 -0
  41. package/src/theme/component-list-panel.scss +3 -7
  42. package/src/theme/index.scss +1 -13
  43. package/src/theme/layer-panel.scss +7 -2
  44. package/src/theme/layout.scss +5 -0
  45. package/src/theme/theme.scss +16 -0
  46. package/src/type.ts +88 -1
  47. package/src/utils/props.ts +9 -3
  48. package/src/utils/scroll-viewer.ts +18 -2
  49. package/src/utils/stage.ts +7 -0
  50. package/types/Editor.vue.d.ts +2 -2
  51. package/types/components/ContentMenu.vue.d.ts +1 -3
  52. package/types/components/Icon.vue.d.ts +1 -3
  53. package/types/{layouts → components}/Layout.vue.d.ts +0 -0
  54. package/types/components/ScrollBar.vue.d.ts +1 -3
  55. package/types/components/ScrollViewer.vue.d.ts +45 -1
  56. package/types/components/ToolButton.vue.d.ts +1 -3
  57. package/types/fields/Code.vue.d.ts +9 -3
  58. package/types/fields/CodeLink.vue.d.ts +5 -3
  59. package/types/fields/CodeSelect.vue.d.ts +94 -0
  60. package/types/fields/UISelect.vue.d.ts +1 -3
  61. package/types/index.d.ts +4 -0
  62. package/types/layouts/AddPageBox.vue.d.ts +43 -3
  63. package/types/layouts/CodeEditor.vue.d.ts +158 -45
  64. package/types/layouts/NavMenu.vue.d.ts +1 -3
  65. package/types/layouts/sidebar/LayerMenu.vue.d.ts +31 -63
  66. package/types/layouts/sidebar/LayerPanel.vue.d.ts +38 -1074
  67. package/types/layouts/sidebar/Sidebar.vue.d.ts +119 -17
  68. package/types/layouts/sidebar/code-block/CodeBlockEditor.vue.d.ts +50 -0
  69. package/types/layouts/sidebar/code-block/CodeBlockList.vue.d.ts +73 -0
  70. package/types/layouts/workspace/Breadcrumb.vue.d.ts +44 -0
  71. package/types/layouts/workspace/Stage.vue.d.ts +23 -7
  72. package/types/layouts/workspace/ViewerMenu.vue.d.ts +6 -3
  73. package/types/layouts/workspace/Workspace.vue.d.ts +23 -5
  74. package/types/services/codeBlock.d.ts +155 -0
  75. package/types/services/editor.d.ts +12 -1
  76. package/types/services/props.d.ts +12 -0
  77. package/types/services/ui.d.ts +1 -1
  78. package/types/type.d.ts +78 -1
  79. package/types/utils/props.d.ts +12 -0
  80. package/types/utils/scroll-viewer.d.ts +5 -0
  81. package/src/layouts/sidebar/TabPane.vue +0 -99
  82. package/types/layouts/sidebar/TabPane.vue.d.ts +0 -14
@@ -9,47 +9,48 @@
9
9
  >
10
10
  <div class="m-editor-page-bar-title">
11
11
  <slot name="page-bar-title" :page="item">
12
- <el-tooltip effect="dark" placement="top-start" :content="item.name">
12
+ <TMagicTooltip effect="dark" placement="top-start" :content="item.name">
13
13
  <span>{{ item.name || item.id }}</span>
14
- </el-tooltip>
14
+ </TMagicTooltip>
15
15
  </slot>
16
16
  </div>
17
17
 
18
- <el-popover popper-class="page-bar-popover" placement="top" :width="160" trigger="hover">
18
+ <TMagicPopover popper-class="page-bar-popover" placement="top" :width="160" trigger="hover">
19
19
  <div>
20
20
  <slot name="page-bar-popover" :page="item">
21
- <tool-button
21
+ <ToolButton
22
22
  :data="{
23
23
  type: 'button',
24
24
  text: '复制',
25
25
  icon: DocumentCopy,
26
26
  handler: () => copy(item),
27
27
  }"
28
- ></tool-button>
29
- <tool-button
28
+ ></ToolButton>
29
+ <ToolButton
30
30
  :data="{
31
31
  type: 'button',
32
32
  text: '删除',
33
33
  icon: Delete,
34
34
  handler: () => remove(item),
35
35
  }"
36
- ></tool-button>
36
+ ></ToolButton>
37
37
  </slot>
38
38
  </div>
39
39
  <template #reference>
40
- <el-icon class="m-editor-page-bar-menu-icon">
40
+ <TMagicIcon class="m-editor-page-bar-menu-icon">
41
41
  <CaretBottom></CaretBottom>
42
- </el-icon>
42
+ </TMagicIcon>
43
43
  </template>
44
- </el-popover>
44
+ </TMagicPopover>
45
45
  </div>
46
46
  </PageBarScrollContainer>
47
47
  </template>
48
48
 
49
- <script lang="ts" setup>
49
+ <script lang="ts" setup name="MEditorPageBar">
50
50
  import { computed, inject } from 'vue';
51
51
  import { CaretBottom, Delete, DocumentCopy } from '@element-plus/icons-vue';
52
52
 
53
+ import { TMagicIcon, TMagicPopover, TMagicTooltip } from '@tmagic/design';
53
54
  import type { MApp, MPage } from '@tmagic/schema';
54
55
 
55
56
  import ToolButton from '../../components/ToolButton.vue';
@@ -26,7 +26,7 @@
26
26
  </div>
27
27
  </template>
28
28
 
29
- <script setup lang="ts">
29
+ <script setup lang="ts" name="MEditorPageBarScrollContainer">
30
30
  import { computed, inject, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from 'vue';
31
31
  import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue';
32
32
 
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <scroll-viewer
2
+ <ScrollViewer
3
3
  class="m-editor-stage"
4
4
  ref="stageWrap"
5
5
  :width="stageRect?.width"
@@ -7,6 +7,10 @@
7
7
  :wrap-width="stageContainerRect?.width"
8
8
  :wrap-height="stageContainerRect?.height"
9
9
  :zoom="zoom"
10
+ :correction-scroll-size="{
11
+ width: 60,
12
+ height: 50,
13
+ }"
10
14
  >
11
15
  <div
12
16
  class="m-editor-stage-container"
@@ -16,13 +20,13 @@
16
20
  @drop="dropHandler"
17
21
  @dragover="dragoverHandler"
18
22
  ></div>
19
- <teleport to="body">
20
- <viewer-menu ref="menu" :is-multi-select="isMultiSelect"></viewer-menu>
21
- </teleport>
22
- </scroll-viewer>
23
+ <Teleport to="body">
24
+ <ViewerMenu ref="menu" :is-multi-select="isMultiSelect" :stage-content-menu="stageContentMenu"></ViewerMenu>
25
+ </Teleport>
26
+ </ScrollViewer>
23
27
  </template>
24
28
 
25
- <script lang="ts" setup>
29
+ <script lang="ts" setup name="MEditorStage">
26
30
  import { computed, inject, markRaw, onMounted, onUnmounted, ref, toRaw, watch, watchEffect } from 'vue';
27
31
  import { cloneDeep } from 'lodash-es';
28
32
 
@@ -30,11 +34,15 @@ import type { MApp, MContainer, MNode, MPage } from '@tmagic/schema';
30
34
  import StageCore, { calcValueByFontsize, getOffset, Runtime } from '@tmagic/stage';
31
35
 
32
36
  import ScrollViewer from '../../components/ScrollViewer.vue';
33
- import { Layout, Services, StageOptions, StageRect } from '../../type';
37
+ import { Layout, MenuButton, MenuComponent, Services, StageOptions, StageRect } from '../../type';
34
38
  import { useStage } from '../../utils/stage';
35
39
 
36
40
  import ViewerMenu from './ViewerMenu.vue';
37
41
 
42
+ defineProps<{
43
+ stageContentMenu: (MenuButton | MenuComponent)[];
44
+ }>();
45
+
38
46
  let stage: StageCore | null = null;
39
47
  let runtime: Runtime | null = null;
40
48
 
@@ -2,8 +2,8 @@
2
2
  <content-menu :menu-data="menuData" ref="menu"></content-menu>
3
3
  </template>
4
4
 
5
- <script lang="ts" setup>
6
- import { computed, inject, markRaw, reactive, ref, watch } from 'vue';
5
+ <script lang="ts" setup name="MEditorViewerMenu">
6
+ import { computed, inject, markRaw, ref, watch } from 'vue';
7
7
  import { Bottom, Delete, DocumentCopy, Top } from '@element-plus/icons-vue';
8
8
 
9
9
  import { MNode, NodeType } from '@tmagic/schema';
@@ -15,7 +15,10 @@ import storageService from '../../services/storage';
15
15
  import { LayerOffset, Layout, MenuButton, MenuComponent, Services } from '../../type';
16
16
  import { COPY_STORAGE_KEY } from '../../utils/editor';
17
17
 
18
- const props = withDefaults(defineProps<{ isMultiSelect?: boolean }>(), { isMultiSelect: false });
18
+ const props = withDefaults(
19
+ defineProps<{ isMultiSelect?: boolean; stageContentMenu: (MenuButton | MenuComponent)[] }>(),
20
+ { isMultiSelect: false },
21
+ );
19
22
 
20
23
  const services = inject<Services>('services');
21
24
  const editorService = services?.editorService;
@@ -28,9 +31,7 @@ const nodes = computed(() => editorService?.get<MNode[]>('nodes'));
28
31
  const parent = computed(() => editorService?.get('parent'));
29
32
  const stage = computed(() => editorService?.get<StageCore>('stage'));
30
33
 
31
- const stageContentMenu = inject<(MenuButton | MenuComponent)[]>('stageContentMenu', []);
32
-
33
- const menuData = reactive<(MenuButton | MenuComponent)[]>([
34
+ const menuData = computed<(MenuButton | MenuComponent)[]>(() => [
34
35
  {
35
36
  type: 'button',
36
37
  text: '水平居中',
@@ -130,7 +131,7 @@ const menuData = reactive<(MenuButton | MenuComponent)[]>([
130
131
  editorService?.get<StageCore>('stage').clearGuides();
131
132
  },
132
133
  },
133
- ...stageContentMenu,
134
+ ...props.stageContentMenu,
134
135
  ]);
135
136
 
136
137
  watch(
@@ -1,7 +1,9 @@
1
1
  <template>
2
2
  <div class="m-editor-workspace" tabindex="-1" ref="workspace">
3
+ <Breadcrumb></Breadcrumb>
4
+
3
5
  <slot name="stage">
4
- <MagicStage :key="page?.id"></MagicStage>
6
+ <MagicStage :key="page?.id" :stage-content-menu="stageContentMenu"></MagicStage>
5
7
  </slot>
6
8
 
7
9
  <slot name="workspace-content"></slot>
@@ -13,18 +15,23 @@
13
15
  </div>
14
16
  </template>
15
17
 
16
- <script lang="ts" setup>
18
+ <script lang="ts" setup name="MEditorWorkspace">
17
19
  import { computed, inject, onMounted, onUnmounted, ref } from 'vue';
18
20
  import KeyController from 'keycon';
19
21
 
20
22
  import type { MNode, MPage } from '@tmagic/schema';
21
23
  import { isPage } from '@tmagic/utils';
22
24
 
23
- import type { Services } from '../../type';
25
+ import type { MenuButton, MenuComponent, Services } from '../../type';
24
26
 
27
+ import Breadcrumb from './Breadcrumb.vue';
25
28
  import PageBar from './PageBar.vue';
26
29
  import MagicStage from './Stage.vue';
27
30
 
31
+ defineProps<{
32
+ stageContentMenu: (MenuButton | MenuComponent)[];
33
+ }>();
34
+
28
35
  const services = inject<Services>('services');
29
36
  const workspace = ref<HTMLDivElement>();
30
37
  const nodes = computed(() => services?.editorService.get<MNode[]>('nodes'));
@@ -0,0 +1,415 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making TMagicEditor available.
3
+ *
4
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import { reactive } from 'vue';
20
+ import { cloneDeep, forIn, isEmpty, keys, omit, pick } from 'lodash-es';
21
+
22
+ import { Id, MNode } from '@tmagic/schema';
23
+
24
+ import editorService from '../services/editor';
25
+ import type { CodeBlockContent, CodeBlockDSL, CodeState } from '../type';
26
+ import { CodeEditorMode, CodeSelectOp } from '../type';
27
+ import { error, info } from '../utils/logger';
28
+
29
+ import BaseService from './BaseService';
30
+
31
+ class CodeBlock extends BaseService {
32
+ private state = reactive<CodeState>({
33
+ isShowCodeEditor: false,
34
+ codeDsl: null,
35
+ id: '',
36
+ editable: true,
37
+ mode: CodeEditorMode.EDITOR,
38
+ combineIds: [],
39
+ undeletableList: [],
40
+ });
41
+
42
+ constructor() {
43
+ super([
44
+ 'setCodeDsl',
45
+ 'getCodeDsl',
46
+ 'getCodeContentById',
47
+ 'getCodeDslByIds',
48
+ 'getCurrentDsl',
49
+ 'setCodeDslById',
50
+ 'setCodeEditorShowStatus',
51
+ 'setEditStatus',
52
+ 'setMode',
53
+ 'setCombineIds',
54
+ 'setUndeleteableList',
55
+ 'deleteCodeDslByIds',
56
+ ]);
57
+ }
58
+
59
+ /**
60
+ * 设置活动的代码块dsl数据源
61
+ * @param {CodeBlockDSL} codeDsl 代码DSL
62
+ * @returns {void}
63
+ */
64
+ public async setCodeDsl(codeDsl: CodeBlockDSL): Promise<void> {
65
+ this.state.codeDsl = codeDsl;
66
+ await editorService.setCodeDsl(this.state.codeDsl);
67
+ info('[code-block]:code-dsl-change', this.state.codeDsl);
68
+ this.emit('code-dsl-change', this.state.codeDsl);
69
+ }
70
+
71
+ /**
72
+ * 获取活动的代码块dsl数据源(默认从dsl中的codeBlocks字段读取)
73
+ * @param {boolean} forceRefresh 是否强制从活动dsl拉取刷新
74
+ * @returns {CodeBlockDSL | null}
75
+ */
76
+ public async getCodeDsl(forceRefresh = false): Promise<CodeBlockDSL | null> {
77
+ if (!this.state.codeDsl || forceRefresh) {
78
+ this.state.codeDsl = await editorService.getCodeDsl();
79
+ }
80
+ return this.state.codeDsl;
81
+ }
82
+
83
+ /**
84
+ * 根据代码块id获取代码块内容
85
+ * @param {string} id 代码块id
86
+ * @returns {CodeBlockContent | null}
87
+ */
88
+ public async getCodeContentById(id: string): Promise<CodeBlockContent | null> {
89
+ if (!id) return null;
90
+ const totalCodeDsl = await this.getCodeDsl();
91
+ if (!totalCodeDsl) return null;
92
+ return totalCodeDsl[id] ?? null;
93
+ }
94
+
95
+ /**
96
+ * 设置代码块ID和代码内容到源dsl
97
+ * @param {string} id 代码块id
98
+ * @param {CodeBlockContent} codeConfig 代码块内容配置信息
99
+ * @returns {void}
100
+ */
101
+ public async setCodeDslById(id: string, codeConfig: CodeBlockContent): Promise<void> {
102
+ let codeDsl = await this.getCodeDsl();
103
+ if (!codeDsl) {
104
+ // dsl中无代码块字段
105
+ codeDsl = {
106
+ [id]: codeConfig,
107
+ };
108
+ } else {
109
+ const existContent = codeDsl[id] || {};
110
+ codeDsl = {
111
+ ...codeDsl,
112
+ [id]: {
113
+ ...existContent,
114
+ ...codeConfig,
115
+ },
116
+ };
117
+ }
118
+ await this.setCodeDsl(codeDsl);
119
+ }
120
+
121
+ /**
122
+ * 根据代码块id数组获取代码dsl
123
+ * @param {string[]} ids 代码块id数组
124
+ * @returns {CodeBlockDSL}
125
+ */
126
+ public async getCodeDslByIds(ids: string[]): Promise<CodeBlockDSL> {
127
+ const codeDsl = await this.getCodeDsl();
128
+ return pick(codeDsl, ids) as CodeBlockDSL;
129
+ }
130
+
131
+ /**
132
+ * 设置代码编辑面板展示状态
133
+ * @param {boolean} status 是否展示代码编辑面板
134
+ * @returns {void}
135
+ */
136
+ public async setCodeEditorShowStatus(status: boolean): Promise<void> {
137
+ this.state.isShowCodeEditor = status;
138
+ }
139
+
140
+ /**
141
+ * 获取代码编辑面板展示状态
142
+ * @returns {boolean} 是否展示代码编辑面板
143
+ */
144
+ public getCodeEditorShowStatus(): boolean {
145
+ return this.state.isShowCodeEditor;
146
+ }
147
+
148
+ /**
149
+ * 设置代码编辑面板展示状态及展示内容
150
+ * @param {boolean} status 是否展示代码编辑面板
151
+ * @param {string} id 代码块id
152
+ * @returns {void}
153
+ */
154
+ public setCodeEditorContent(status: boolean, id: string): void {
155
+ if (!id) return;
156
+ this.setId(id);
157
+ this.state.isShowCodeEditor = status;
158
+ }
159
+
160
+ /**
161
+ * 获取当前选中的代码块内容
162
+ * @returns {CodeBlockContent | null}
163
+ */
164
+ public async getCurrentDsl() {
165
+ return await this.getCodeContentById(this.state.id);
166
+ }
167
+
168
+ /**
169
+ * 获取编辑状态
170
+ * @returns {boolean} 是否可编辑
171
+ */
172
+ public getEditStatus(): boolean {
173
+ return this.state.editable;
174
+ }
175
+
176
+ /**
177
+ * 设置编辑状态
178
+ * @param {boolean} 是否可编辑
179
+ * @returns {void}
180
+ */
181
+ public async setEditStatus(status: boolean): Promise<void> {
182
+ this.state.editable = status;
183
+ }
184
+
185
+ /**
186
+ * 设置当前选中的代码块ID
187
+ * @param {string} id 代码块id
188
+ * @returns {void}
189
+ */
190
+ public setId(id: string) {
191
+ if (!id) return;
192
+ this.state.id = id;
193
+ }
194
+
195
+ /**
196
+ * 获取当前选中的代码块ID
197
+ * @returns {string} id 代码块id
198
+ */
199
+ public getId(): string {
200
+ return this.state.id;
201
+ }
202
+
203
+ /**
204
+ * 获取当前模式
205
+ * @returns {CodeEditorMode}
206
+ */
207
+ public getMode(): CodeEditorMode {
208
+ return this.state.mode;
209
+ }
210
+
211
+ /**
212
+ * 设置当前模式
213
+ * @param {CodeEditorMode} mode 模式
214
+ * @returns {void}
215
+ */
216
+ public async setMode(mode: CodeEditorMode): Promise<void> {
217
+ this.state.mode = mode;
218
+ }
219
+
220
+ /**
221
+ * 设置当前选中组件已关联绑定的代码块id数组
222
+ * @param {string[]} ids 代码块id数组
223
+ * @returns {void}
224
+ */
225
+ public async setCombineIds(ids: string[]): Promise<void> {
226
+ this.state.combineIds = ids;
227
+ }
228
+
229
+ /**
230
+ * 获取当前选中组件已关联绑定的代码块id数组
231
+ * @returns {string[]}
232
+ */
233
+ public getCombineIds(): string[] {
234
+ return this.state.combineIds;
235
+ }
236
+
237
+ /**
238
+ * 设置绑定关系
239
+ * @param {Id} 组件id
240
+ * @param {string[]} diffCodeIds 代码块id数组
241
+ * @param {CodeSelectOp} opFlag 操作类型
242
+ * @param {string} hook 代码块挂载hook名称
243
+ * @returns {void}
244
+ */
245
+ public async setCombineRelation(compId: Id, diffCodeIds: string[], opFlag: CodeSelectOp, hook: string) {
246
+ const codeDsl = cloneDeep(await this.getCodeDsl());
247
+ if (!codeDsl) return;
248
+ if (opFlag === CodeSelectOp.DELETE) {
249
+ try {
250
+ diffCodeIds.forEach((codeId) => {
251
+ const compsContent = codeDsl[codeId].comps;
252
+ const index = compsContent?.[compId].findIndex((item) => item === hook);
253
+ if (typeof index !== 'undefined' && index !== -1) {
254
+ compsContent?.[compId].splice(index, 1);
255
+ }
256
+ });
257
+ } catch (e) {
258
+ error(e);
259
+ throw new Error('解绑代码块失败');
260
+ }
261
+ } else if (opFlag === CodeSelectOp.ADD) {
262
+ try {
263
+ diffCodeIds.forEach((codeId) => {
264
+ const compsContent = codeDsl[codeId].comps;
265
+ const existHooks = compsContent?.[compId];
266
+ if (isEmpty(existHooks)) {
267
+ // comps属性不存在,或者comps为空:新增
268
+ codeDsl[codeId].comps = {
269
+ ...(codeDsl[codeId].comps || {}),
270
+ [compId]: [hook],
271
+ };
272
+ } else {
273
+ // 往已有的关系中添加hook
274
+ existHooks?.push(hook);
275
+ }
276
+ });
277
+ } catch (e) {
278
+ error(e);
279
+ throw new Error('绑定代码块失败');
280
+ }
281
+ } else if (opFlag === CodeSelectOp.CHANGE) {
282
+ // 单选修改
283
+ forIn(codeDsl, (codeBlockContent, codeId) => {
284
+ if (codeId === diffCodeIds[0]) {
285
+ // 增加
286
+ codeBlockContent.comps = {
287
+ ...(codeBlockContent?.comps || {}),
288
+ [compId]: [hook],
289
+ };
290
+ } else if (isEmpty(diffCodeIds) || codeId !== diffCodeIds[0]) {
291
+ // 清空或者移除之前的选项
292
+ const compHooks = codeBlockContent?.comps?.[compId];
293
+ // continue
294
+ if (!compHooks) return true;
295
+ const index = compHooks.findIndex((hookName) => hookName === hook);
296
+ if (index !== -1) {
297
+ compHooks.splice(index, 1);
298
+ // break
299
+ return false;
300
+ }
301
+ }
302
+ });
303
+ }
304
+ this.setCodeDsl(codeDsl);
305
+ }
306
+
307
+ /**
308
+ * 获取不可删除列表
309
+ * @returns {string[]}
310
+ */
311
+ public getUndeletableList(): string[] {
312
+ return this.state.undeletableList;
313
+ }
314
+
315
+ /**
316
+ * 设置不可删除列表:为业务逻辑预留的不可删除的代码块列表,由业务逻辑维护(如代码块上线后不可删除)
317
+ * @param {string[]} codeIds 代码块id数组
318
+ * @returns {void}
319
+ */
320
+ public async setUndeleteableList(codeIds: string[]): Promise<void> {
321
+ this.state.undeletableList = codeIds;
322
+ }
323
+
324
+ /**
325
+ * 在dsl数据源中删除指定id的代码块
326
+ * @param {string[]} codeIds 需要删除的代码块id数组
327
+ * @returns {CodeBlockDSL} 删除后的code dsl
328
+ */
329
+ public async deleteCodeDslByIds(codeIds: string[]): Promise<CodeBlockDSL> {
330
+ const currentDsl = await this.getCodeDsl();
331
+ const newDsl = omit(currentDsl, codeIds);
332
+ await this.setCodeDsl(newDsl);
333
+ return newDsl;
334
+ }
335
+
336
+ /**
337
+ * 生成代码块唯一id
338
+ * @returns {string} 代码块唯一id
339
+ */
340
+ public async getUniqueId(): Promise<string> {
341
+ const newId = `code_${Math.random().toString(10).substring(2).substring(0, 4)}`;
342
+ // 判断是否重复
343
+ const dsl = await this.getCodeDsl();
344
+ const existedIds = keys(dsl);
345
+ if (!existedIds.includes(newId)) return newId;
346
+ return await this.getUniqueId();
347
+ }
348
+
349
+ /**
350
+ * 一对一解除绑定关系 功能隐藏,暂时注释
351
+ * @param {string} compId 组件id
352
+ * @param {string} codeId 代码块id
353
+ * @param {string[]} codeHooks 代码块挂载hook名称
354
+ * @returns {boolean} 结果
355
+ */
356
+ // public async unbind(compId: Id, codeId: string, codeHooks: string[]): Promise<boolean> {
357
+ // const nodeInfo = editorService.getNodeById(compId);
358
+ // if (!nodeInfo) return false;
359
+ // // 更新node节点信息
360
+ // codeHooks.forEach((hook) => {
361
+ // if (!isEmpty(nodeInfo[hook])) {
362
+ // const newHookInfo = nodeInfo[hook].filter((item: string) => item !== codeId);
363
+ // nodeInfo[hook] = newHookInfo;
364
+ // editorService.update(nodeInfo);
365
+ // }
366
+ // });
367
+ // // 更新绑定关系
368
+ // const oldRelation = await this.getCompRelation();
369
+ // const oldCodeIds = oldRelation[compId];
370
+ // // 不能直接splice修改原数组,会导致绑定关系更新错乱
371
+ // const newCodeIds = oldCodeIds.filter((item) => item !== codeId);
372
+ // this.setCompRelation(compId, newCodeIds);
373
+ // return true;
374
+ // }
375
+
376
+ /**
377
+ * 通过组件id解除绑定关系(删除组件)
378
+ * @param {MNode} compId 组件节点
379
+ * @returns void
380
+ */
381
+ public async deleteCompsInRelation(node: MNode) {
382
+ const codeDsl = cloneDeep(await this.getCodeDsl());
383
+ if (!codeDsl) return;
384
+ this.recurseNodes(node, codeDsl);
385
+ this.setCodeDsl(codeDsl);
386
+ }
387
+
388
+ public destroy() {
389
+ this.state.isShowCodeEditor = false;
390
+ this.state.codeDsl = null;
391
+ this.state.id = '';
392
+ this.state.editable = true;
393
+ this.state.mode = CodeEditorMode.EDITOR;
394
+ this.state.combineIds = [];
395
+ this.state.undeletableList = [];
396
+ }
397
+
398
+ // 删除组件时 如果是容器 需要遍历删除其包含节点的绑定信息
399
+ private recurseNodes(node: MNode, codeDsl: CodeBlockDSL) {
400
+ if (!node.id) return;
401
+ forIn(codeDsl, (codeBlockContent) => {
402
+ const compsContent = codeBlockContent.comps || {};
403
+ codeBlockContent.comps = omit(compsContent, node.id);
404
+ });
405
+ if (!isEmpty(node.items)) {
406
+ node.items.forEach((item: MNode) => {
407
+ this.recurseNodes(item, codeDsl);
408
+ });
409
+ }
410
+ }
411
+ }
412
+
413
+ export type CodeBlockService = CodeBlock;
414
+
415
+ export default new CodeBlock();