@tmagic/editor 1.4.8 → 1.4.9

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 (143) hide show
  1. package/package.json +11 -11
  2. package/src/Editor.vue +211 -0
  3. package/src/components/CodeBlockEditor.vue +268 -0
  4. package/src/components/CodeParams.vue +60 -0
  5. package/src/components/ContentMenu.vue +193 -0
  6. package/src/components/FloatingBox.vue +178 -0
  7. package/src/components/Icon.vue +27 -0
  8. package/src/components/Resizer.vue +23 -0
  9. package/src/components/ScrollBar.vue +151 -0
  10. package/src/components/ScrollViewer.vue +128 -0
  11. package/src/components/SearchInput.vue +37 -0
  12. package/src/components/SplitView.vue +186 -0
  13. package/src/components/ToolButton.vue +138 -0
  14. package/src/components/Tree.vue +71 -0
  15. package/src/components/TreeNode.vue +150 -0
  16. package/src/editorProps.ts +117 -0
  17. package/src/fields/Code.vue +50 -0
  18. package/src/fields/CodeLink.vue +80 -0
  19. package/src/fields/CodeSelect.vue +132 -0
  20. package/src/fields/CodeSelectCol.vue +139 -0
  21. package/src/fields/DataSourceFieldSelect.vue +149 -0
  22. package/src/fields/DataSourceFields.vue +323 -0
  23. package/src/fields/DataSourceInput.vue +339 -0
  24. package/src/fields/DataSourceMethodSelect.vue +153 -0
  25. package/src/fields/DataSourceMethods.vue +104 -0
  26. package/src/fields/DataSourceMocks.vue +255 -0
  27. package/src/fields/DataSourceSelect.vue +98 -0
  28. package/src/fields/EventSelect.vue +375 -0
  29. package/src/fields/KeyValue.vue +137 -0
  30. package/src/fields/PageFragmentSelect.vue +63 -0
  31. package/src/fields/UISelect.vue +135 -0
  32. package/src/hooks/index.ts +24 -0
  33. package/src/hooks/use-code-block-edit.ts +83 -0
  34. package/src/hooks/use-data-source-edit.ts +46 -0
  35. package/src/hooks/use-data-source-method.ts +100 -0
  36. package/src/hooks/use-editor-content-height.ts +26 -0
  37. package/src/hooks/use-filter.ts +55 -0
  38. package/src/hooks/use-float-box.ts +76 -0
  39. package/src/hooks/use-getso.ts +35 -0
  40. package/src/hooks/use-next-float-box-position.ts +29 -0
  41. package/src/hooks/use-node-status.ts +48 -0
  42. package/src/hooks/use-stage.ts +129 -0
  43. package/src/hooks/use-window-rect.ts +20 -0
  44. package/src/icons/AppManageIcon.vue +15 -0
  45. package/src/icons/CenterIcon.vue +13 -0
  46. package/src/icons/CodeIcon.vue +28 -0
  47. package/src/icons/FolderMinusIcon.vue +22 -0
  48. package/src/icons/PinIcon.vue +25 -0
  49. package/src/icons/PinnedIcon.vue +25 -0
  50. package/src/index.ts +118 -0
  51. package/src/initService.ts +448 -0
  52. package/src/layouts/AddPageBox.vue +55 -0
  53. package/src/layouts/CodeEditor.vue +226 -0
  54. package/src/layouts/Framework.vue +163 -0
  55. package/src/layouts/NavMenu.vue +200 -0
  56. package/src/layouts/PropsPanel.vue +131 -0
  57. package/src/layouts/page-bar/AddButton.vue +48 -0
  58. package/src/layouts/page-bar/PageBar.vue +159 -0
  59. package/src/layouts/page-bar/PageBarScrollContainer.vue +156 -0
  60. package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
  61. package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
  62. package/src/layouts/sidebar/Sidebar.vue +278 -0
  63. package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
  64. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
  65. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
  66. package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
  67. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
  68. package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
  69. package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
  70. package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
  71. package/src/layouts/sidebar/layer/use-click.ts +113 -0
  72. package/src/layouts/sidebar/layer/use-drag.ts +167 -0
  73. package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
  74. package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
  75. package/src/layouts/workspace/Breadcrumb.vue +35 -0
  76. package/src/layouts/workspace/Workspace.vue +46 -0
  77. package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
  78. package/src/layouts/workspace/viewer/Stage.vue +246 -0
  79. package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
  80. package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
  81. package/src/services/BaseService.ts +230 -0
  82. package/src/services/codeBlock.ts +327 -0
  83. package/src/services/componentList.ts +58 -0
  84. package/src/services/dataSource.ts +216 -0
  85. package/src/services/dep.ts +152 -0
  86. package/src/services/editor.ts +1135 -0
  87. package/src/services/events.ts +93 -0
  88. package/src/services/history.ts +126 -0
  89. package/src/services/keybinding.ts +220 -0
  90. package/src/services/props.ts +286 -0
  91. package/src/services/stageOverlay.ts +212 -0
  92. package/src/services/storage.ts +155 -0
  93. package/src/services/ui.ts +156 -0
  94. package/src/shims-vue.d.ts +6 -0
  95. package/src/theme/breadcrumb.scss +6 -0
  96. package/src/theme/code-block.scss +6 -0
  97. package/src/theme/code-editor.scss +38 -0
  98. package/src/theme/common/var.scss +14 -0
  99. package/src/theme/component-list-panel.scss +95 -0
  100. package/src/theme/content-menu.scss +76 -0
  101. package/src/theme/data-source-field.scss +12 -0
  102. package/src/theme/data-source-fields.scss +13 -0
  103. package/src/theme/data-source-input.scss +18 -0
  104. package/src/theme/data-source-methods.scss +13 -0
  105. package/src/theme/data-source.scss +28 -0
  106. package/src/theme/event.scss +37 -0
  107. package/src/theme/floating-box.scss +32 -0
  108. package/src/theme/framework.scss +69 -0
  109. package/src/theme/icon.scss +12 -0
  110. package/src/theme/index.scss +10 -0
  111. package/src/theme/key-value.scss +20 -0
  112. package/src/theme/layer-panel.scss +26 -0
  113. package/src/theme/layout.scss +9 -0
  114. package/src/theme/nav-menu.scss +78 -0
  115. package/src/theme/page-bar.scss +89 -0
  116. package/src/theme/page-fragment-select.scss +14 -0
  117. package/src/theme/props-panel.scss +55 -0
  118. package/src/theme/resizer.scss +66 -0
  119. package/src/theme/ruler.scss +38 -0
  120. package/src/theme/search-input.scss +14 -0
  121. package/src/theme/sidebar.scss +79 -0
  122. package/src/theme/stage.scss +76 -0
  123. package/src/theme/theme.scss +27 -0
  124. package/src/theme/tree.scss +89 -0
  125. package/src/theme/workspace.scss +9 -0
  126. package/src/type.ts +749 -0
  127. package/src/utils/compose.ts +52 -0
  128. package/src/utils/config.ts +29 -0
  129. package/src/utils/content-menu.ts +95 -0
  130. package/src/utils/data-source/formConfigs/base.ts +30 -0
  131. package/src/utils/data-source/formConfigs/http.ts +60 -0
  132. package/src/utils/data-source/index.ts +243 -0
  133. package/src/utils/editor.ts +313 -0
  134. package/src/utils/idle-task.ts +72 -0
  135. package/src/utils/index.ts +24 -0
  136. package/src/utils/keybinding-config.ts +125 -0
  137. package/src/utils/logger.ts +47 -0
  138. package/src/utils/monaco-editor.ts +8 -0
  139. package/src/utils/operator.ts +106 -0
  140. package/src/utils/props.ts +504 -0
  141. package/src/utils/scroll-viewer.ts +165 -0
  142. package/src/utils/tree.ts +15 -0
  143. package/src/utils/undo-redo.ts +76 -0
@@ -0,0 +1,131 @@
1
+ <template>
2
+ <div class="m-editor-props-panel" v-show="nodes.length === 1">
3
+ <slot name="props-panel-header"></slot>
4
+ <MForm
5
+ ref="configForm"
6
+ :class="propsPanelSize"
7
+ :popper-class="`m-editor-props-panel-popper ${propsPanelSize}`"
8
+ :size="propsPanelSize"
9
+ :init-values="values"
10
+ :config="curFormConfig"
11
+ :extend-state="extendState"
12
+ @change="submit"
13
+ @error="errorHandler"
14
+ ></MForm>
15
+
16
+ <TMagicButton
17
+ v-if="!disabledShowSrc"
18
+ class="m-editor-props-panel-src-icon"
19
+ circle
20
+ size="large"
21
+ title="源码"
22
+ :type="showSrc ? 'primary' : ''"
23
+ @click="showSrc = !showSrc"
24
+ >
25
+ <MIcon :icon="DocumentIcon"></MIcon>
26
+ </TMagicButton>
27
+
28
+ <CodeEditor
29
+ v-if="showSrc"
30
+ class="m-editor-props-panel-src-code"
31
+ :height="`${editorContentHeight}px`"
32
+ :init-values="values"
33
+ :options="codeOptions"
34
+ :parse="true"
35
+ @save="saveCode"
36
+ ></CodeEditor>
37
+ </div>
38
+ </template>
39
+
40
+ <script lang="ts" setup>
41
+ import { computed, getCurrentInstance, inject, onBeforeUnmount, onMounted, ref, watchEffect } from 'vue';
42
+ import { Document as DocumentIcon } from '@element-plus/icons-vue';
43
+
44
+ import { TMagicButton } from '@tmagic/design';
45
+ import type { FormState, FormValue } from '@tmagic/form';
46
+ import { MForm } from '@tmagic/form';
47
+ import type { MNode } from '@tmagic/schema';
48
+
49
+ import MIcon from '@editor/components/Icon.vue';
50
+ import { useEditorContentHeight } from '@editor/hooks/use-editor-content-height';
51
+ import type { PropsPanelSlots, Services } from '@editor/type';
52
+
53
+ import CodeEditor from './CodeEditor.vue';
54
+
55
+ defineSlots<PropsPanelSlots>();
56
+
57
+ defineOptions({
58
+ name: 'MEditorPropsPanel',
59
+ });
60
+
61
+ defineProps<{
62
+ disabledShowSrc?: boolean;
63
+ extendState?: (state: FormState) => Record<string, any> | Promise<Record<string, any>>;
64
+ }>();
65
+
66
+ const codeOptions = inject('codeOptions', {});
67
+
68
+ const emit = defineEmits(['mounted', 'submit-error', 'form-error']);
69
+
70
+ const showSrc = ref(false);
71
+
72
+ const internalInstance = getCurrentInstance();
73
+ const values = ref<FormValue>({});
74
+ const configForm = ref<InstanceType<typeof MForm>>();
75
+ // ts类型应该是FormConfig, 但是打包时会出错,所以暂时用any
76
+ const curFormConfig = ref<any>([]);
77
+ const services = inject<Services>('services');
78
+ const node = computed(() => services?.editorService.get('node'));
79
+ const nodes = computed(() => services?.editorService.get('nodes') || []);
80
+ const propsPanelSize = computed(() => services?.uiService.get('propsPanelSize') || 'small');
81
+ const stage = computed(() => services?.editorService.get('stage'));
82
+
83
+ const { height: editorContentHeight } = useEditorContentHeight();
84
+
85
+ const init = async () => {
86
+ if (!node.value) {
87
+ curFormConfig.value = [];
88
+ return;
89
+ }
90
+
91
+ const type = node.value.type || (node.value.items ? 'container' : 'text');
92
+ curFormConfig.value = (await services?.propsService.getPropsConfig(type)) || [];
93
+ values.value = node.value;
94
+ };
95
+
96
+ watchEffect(init);
97
+ services?.propsService.on('props-configs-change', init);
98
+
99
+ onMounted(() => {
100
+ emit('mounted', internalInstance);
101
+ });
102
+
103
+ onBeforeUnmount(() => {
104
+ services?.propsService.off('props-configs-change', init);
105
+ });
106
+
107
+ watchEffect(() => {
108
+ if (configForm.value && stage.value) {
109
+ configForm.value.formState.stage = stage.value;
110
+ }
111
+ });
112
+
113
+ const submit = async () => {
114
+ try {
115
+ const values = await configForm.value?.submitForm();
116
+ services?.editorService.update(values);
117
+ } catch (e: any) {
118
+ emit('submit-error', e);
119
+ }
120
+ };
121
+
122
+ const errorHandler = (e: any) => {
123
+ emit('form-error', e);
124
+ };
125
+
126
+ const saveCode = (values: MNode) => {
127
+ services?.editorService.update(values);
128
+ };
129
+
130
+ defineExpose({ configForm, submit });
131
+ </script>
@@ -0,0 +1,48 @@
1
+ <template>
2
+ <div
3
+ v-if="showAddPageButton"
4
+ id="m-editor-page-bar-add-icon"
5
+ class="m-editor-page-bar-item m-editor-page-bar-item-icon"
6
+ @click="addPage"
7
+ >
8
+ <Icon :icon="Plus"></Icon>
9
+ </div>
10
+ <div v-else style="width: 21px"></div>
11
+ </template>
12
+
13
+ <script setup lang="ts">
14
+ import { computed, inject, toRaw } from 'vue';
15
+ import { Plus } from '@element-plus/icons-vue';
16
+
17
+ import { NodeType } from '@tmagic/schema';
18
+
19
+ import Icon from '@editor/components/Icon.vue';
20
+ import type { Services } from '@editor/type';
21
+ import { generatePageNameByApp } from '@editor/utils/editor';
22
+
23
+ defineOptions({
24
+ name: 'MEditorPageBarAddButton',
25
+ });
26
+
27
+ const props = defineProps<{
28
+ type: NodeType.PAGE | NodeType.PAGE_FRAGMENT;
29
+ }>();
30
+
31
+ const services = inject<Services>('services');
32
+ const uiService = services?.uiService;
33
+ const editorService = services?.editorService;
34
+
35
+ const showAddPageButton = computed(() => uiService?.get('showAddPageButton'));
36
+
37
+ const addPage = () => {
38
+ if (!editorService) return;
39
+ const root = toRaw(editorService.get('root'));
40
+ if (!root) throw new Error('root 不能为空');
41
+ const pageConfig = {
42
+ type: props.type,
43
+ name: generatePageNameByApp(root, props.type),
44
+ items: [],
45
+ };
46
+ editorService.add(pageConfig);
47
+ };
48
+ </script>
@@ -0,0 +1,159 @@
1
+ <template>
2
+ <div class="m-editor-page-bar-tabs">
3
+ <SwitchTypeButton v-if="!disabledPageFragment" v-model="active" />
4
+
5
+ <PageBarScrollContainer :type="active">
6
+ <template #prepend>
7
+ <AddButton :type="active"></AddButton>
8
+ </template>
9
+
10
+ <div
11
+ v-for="item in list"
12
+ class="m-editor-page-bar-item"
13
+ :key="item.id"
14
+ :class="{ active: page?.id === item.id }"
15
+ @click="switchPage(item.id)"
16
+ >
17
+ <div class="m-editor-page-bar-title">
18
+ <slot name="page-bar-title" :page="item">
19
+ <span :title="item.name">{{ item.name || item.id }}</span>
20
+ </slot>
21
+ </div>
22
+
23
+ <TMagicPopover popper-class="page-bar-popover" placement="top" :width="160" trigger="hover">
24
+ <div>
25
+ <slot name="page-bar-popover" :page="item">
26
+ <ToolButton
27
+ :data="{
28
+ type: 'button',
29
+ text: '复制',
30
+ icon: DocumentCopy,
31
+ handler: () => copy(item),
32
+ }"
33
+ ></ToolButton>
34
+ <ToolButton
35
+ :data="{
36
+ type: 'button',
37
+ text: '删除',
38
+ icon: Delete,
39
+ handler: () => remove(item),
40
+ }"
41
+ ></ToolButton>
42
+ </slot>
43
+ </div>
44
+ <template #reference>
45
+ <TMagicIcon class="m-editor-page-bar-menu-icon">
46
+ <CaretBottom></CaretBottom>
47
+ </TMagicIcon>
48
+ </template>
49
+ </TMagicPopover>
50
+ </div>
51
+ </PageBarScrollContainer>
52
+ </div>
53
+ </template>
54
+
55
+ <script lang="ts" setup>
56
+ import { computed, inject, ref, watch } from 'vue';
57
+ import { CaretBottom, Delete, DocumentCopy } from '@element-plus/icons-vue';
58
+
59
+ import { TMagicIcon, TMagicPopover } from '@tmagic/design';
60
+ import { Id, type MPage, type MPageFragment, NodeType } from '@tmagic/schema';
61
+ import { isPage, isPageFragment } from '@tmagic/utils';
62
+
63
+ import ToolButton from '@editor/components/ToolButton.vue';
64
+ import type { Services } from '@editor/type';
65
+ import { getPageFragmentList, getPageList } from '@editor/utils';
66
+
67
+ import AddButton from './AddButton.vue';
68
+ import PageBarScrollContainer from './PageBarScrollContainer.vue';
69
+ import SwitchTypeButton from './SwitchTypeButton.vue';
70
+
71
+ defineOptions({
72
+ name: 'MEditorPageBar',
73
+ });
74
+
75
+ defineProps<{
76
+ disabledPageFragment: boolean;
77
+ }>();
78
+
79
+ const active = ref<NodeType.PAGE | NodeType.PAGE_FRAGMENT>(NodeType.PAGE);
80
+
81
+ const services = inject<Services>('services');
82
+ const editorService = services?.editorService;
83
+
84
+ const root = computed(() => editorService?.get('root'));
85
+ const page = computed(() => editorService?.get('page'));
86
+ const pageList = computed(() => getPageList(root.value));
87
+ const pageFragmentList = computed(() => getPageFragmentList(root.value));
88
+
89
+ const list = computed(() => (active.value === NodeType.PAGE ? pageList.value : pageFragmentList.value));
90
+
91
+ const activePage = ref<Id>('');
92
+ const activePageFragment = ref<Id>('');
93
+
94
+ watch(
95
+ page,
96
+ (page) => {
97
+ if (!page) {
98
+ if (active.value === NodeType.PAGE) {
99
+ activePage.value = '';
100
+ }
101
+ if (active.value === NodeType.PAGE_FRAGMENT) {
102
+ activePageFragment.value = '';
103
+ }
104
+ return;
105
+ }
106
+
107
+ if (isPage(page)) {
108
+ activePage.value = page?.id;
109
+ if (active.value !== NodeType.PAGE) {
110
+ active.value = NodeType.PAGE;
111
+ }
112
+ } else if (isPageFragment(page)) {
113
+ activePageFragment.value = page?.id;
114
+ if (active.value !== NodeType.PAGE_FRAGMENT) {
115
+ active.value = NodeType.PAGE_FRAGMENT;
116
+ }
117
+ }
118
+ },
119
+ {
120
+ immediate: true,
121
+ },
122
+ );
123
+
124
+ watch(active, (active) => {
125
+ if (active === NodeType.PAGE) {
126
+ if (!activePage.value && !pageList.value.length) {
127
+ editorService?.selectRoot();
128
+ return;
129
+ }
130
+ switchPage(activePage.value);
131
+ return;
132
+ }
133
+
134
+ if (active === NodeType.PAGE_FRAGMENT) {
135
+ // 之前没有选中过页面片并且当前没有页面片
136
+ if (!activePageFragment.value && !pageFragmentList.value.length) {
137
+ editorService?.selectRoot();
138
+ return;
139
+ }
140
+ switchPage(activePageFragment.value || pageFragmentList.value[0].id);
141
+ }
142
+ });
143
+
144
+ const switchPage = (id: Id) => {
145
+ editorService?.select(id);
146
+ };
147
+
148
+ const copy = (node: MPage | MPageFragment) => {
149
+ node && editorService?.copy(node);
150
+ editorService?.paste({
151
+ left: 0,
152
+ top: 0,
153
+ });
154
+ };
155
+
156
+ const remove = (node: MPage | MPageFragment) => {
157
+ editorService?.remove(node);
158
+ };
159
+ </script>
@@ -0,0 +1,156 @@
1
+ <template>
2
+ <div class="m-editor-page-bar" ref="pageBar">
3
+ <slot name="prepend"></slot>
4
+
5
+ <div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('left')">
6
+ <Icon :icon="ArrowLeftBold"></Icon>
7
+ </div>
8
+
9
+ <div
10
+ v-if="(type === NodeType.PAGE && pageLength) || (type === NodeType.PAGE_FRAGMENT && pageFragmentLength)"
11
+ class="m-editor-page-bar-items"
12
+ ref="itemsContainer"
13
+ :style="`width: ${itemsContainerWidth}px`"
14
+ >
15
+ <slot></slot>
16
+ </div>
17
+
18
+ <div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('right')">
19
+ <Icon :icon="ArrowRightBold"></Icon>
20
+ </div>
21
+ </div>
22
+ </template>
23
+
24
+ <script setup lang="ts">
25
+ import {
26
+ computed,
27
+ type ComputedRef,
28
+ inject,
29
+ nextTick,
30
+ onBeforeUnmount,
31
+ onMounted,
32
+ ref,
33
+ watch,
34
+ type WatchStopHandle,
35
+ } from 'vue';
36
+ import { ArrowLeftBold, ArrowRightBold } from '@element-plus/icons-vue';
37
+
38
+ import { NodeType } from '@tmagic/schema';
39
+
40
+ import Icon from '@editor/components/Icon.vue';
41
+ import type { Services } from '@editor/type';
42
+
43
+ defineOptions({
44
+ name: 'MEditorPageBarScrollContainer',
45
+ });
46
+
47
+ const props = defineProps<{
48
+ type: NodeType.PAGE | NodeType.PAGE_FRAGMENT;
49
+ }>();
50
+
51
+ const services = inject<Services>('services');
52
+ const editorService = services?.editorService;
53
+ const uiService = services?.uiService;
54
+
55
+ const itemsContainer = ref<HTMLDivElement>();
56
+ const canScroll = ref(false);
57
+
58
+ const showAddPageButton = computed(() => uiService?.get('showAddPageButton'));
59
+
60
+ const itemsContainerWidth = ref(0);
61
+
62
+ const setCanScroll = () => {
63
+ // 减去新增、左移、右移三个按钮的宽度
64
+ // 37 = icon width 16 + padding 10 * 2 + border-right 1
65
+ itemsContainerWidth.value = (pageBar.value?.clientWidth || 0) - 37 * 2 - (showAddPageButton.value ? 37 : 21);
66
+
67
+ nextTick(() => {
68
+ if (itemsContainer.value) {
69
+ canScroll.value = itemsContainer.value.scrollWidth - itemsContainerWidth.value > 1;
70
+ }
71
+ });
72
+ };
73
+
74
+ const resizeObserver = new ResizeObserver(() => {
75
+ setCanScroll();
76
+ });
77
+
78
+ const pageBar = ref<HTMLDivElement>();
79
+ onMounted(() => {
80
+ pageBar.value && resizeObserver.observe(pageBar.value);
81
+ });
82
+
83
+ onBeforeUnmount(() => {
84
+ resizeObserver.disconnect();
85
+ });
86
+
87
+ let translateLeft = 0;
88
+
89
+ const scroll = (type: 'left' | 'right' | 'start' | 'end') => {
90
+ if (!itemsContainer.value) return;
91
+
92
+ const maxScrollLeft = itemsContainer.value.scrollWidth - itemsContainerWidth.value;
93
+
94
+ if (type === 'left') {
95
+ translateLeft += 100;
96
+
97
+ if (translateLeft > 0) {
98
+ translateLeft = 0;
99
+ }
100
+ } else if (type === 'right') {
101
+ translateLeft -= 100;
102
+
103
+ if (-translateLeft > maxScrollLeft) {
104
+ translateLeft = -maxScrollLeft;
105
+ }
106
+ } else if (type === 'start') {
107
+ translateLeft = 0;
108
+ } else if (type === 'end') {
109
+ translateLeft = -maxScrollLeft;
110
+ }
111
+
112
+ itemsContainer.value.style.transform = `translate(${translateLeft}px, 0px)`;
113
+ };
114
+
115
+ const pageLength = computed(() => editorService?.get('pageLength') || 0);
116
+ const pageFragmentLength = computed(() => editorService?.get('pageFragmentLength') || 0);
117
+
118
+ const crateWatchLength = (length: ComputedRef<number>) =>
119
+ watch(
120
+ length,
121
+ (length = 0, preLength = 0) => {
122
+ setTimeout(() => {
123
+ setCanScroll();
124
+ if (length < preLength) {
125
+ scroll('start');
126
+ } else {
127
+ scroll('end');
128
+ }
129
+ });
130
+ },
131
+ {
132
+ immediate: true,
133
+ },
134
+ );
135
+
136
+ let unWatchPageLength: WatchStopHandle | null;
137
+ let unWatchPageFragmentLength: WatchStopHandle | null;
138
+
139
+ watch(
140
+ () => props.type,
141
+ (type) => {
142
+ if (type === NodeType.PAGE) {
143
+ unWatchPageFragmentLength?.();
144
+ unWatchPageFragmentLength = null;
145
+ unWatchPageLength = crateWatchLength(pageLength);
146
+ } else {
147
+ unWatchPageLength?.();
148
+ unWatchPageLength = null;
149
+ unWatchPageFragmentLength = crateWatchLength(pageFragmentLength);
150
+ }
151
+ },
152
+ {
153
+ immediate: true,
154
+ },
155
+ );
156
+ </script>
@@ -0,0 +1,45 @@
1
+ <template>
2
+ <TMagicButton
3
+ v-for="item in data"
4
+ class="m-editor-page-bar-switch-type-button"
5
+ size="small"
6
+ :key="item.type"
7
+ link
8
+ :class="{ active: modelValue === item.type }"
9
+ :type="modelValue === item.type ? 'primary' : ''"
10
+ @click="clickHandler(item.type)"
11
+ >{{ item.text }}</TMagicButton
12
+ >
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { TMagicButton } from '@tmagic/design';
17
+ import { NodeType } from '@tmagic/schema';
18
+
19
+ defineOptions({
20
+ name: 'MEditorPageBarSwitchTypeButton',
21
+ });
22
+
23
+ defineProps<{
24
+ modelValue: NodeType.PAGE | NodeType.PAGE_FRAGMENT;
25
+ }>();
26
+
27
+ const data: { type: NodeType.PAGE | NodeType.PAGE_FRAGMENT; text: string }[] = [
28
+ {
29
+ type: NodeType.PAGE,
30
+ text: '页面',
31
+ },
32
+ {
33
+ type: NodeType.PAGE_FRAGMENT,
34
+ text: '页面片',
35
+ },
36
+ ];
37
+
38
+ const emit = defineEmits<{
39
+ 'update:modelValue': [value: NodeType.PAGE | NodeType.PAGE_FRAGMENT];
40
+ }>();
41
+
42
+ const clickHandler = (value: NodeType.PAGE | NodeType.PAGE_FRAGMENT) => {
43
+ emit('update:modelValue', value);
44
+ };
45
+ </script>
@@ -0,0 +1,134 @@
1
+ <template>
2
+ <TMagicScrollbar>
3
+ <slot name="component-list-panel-header"></slot>
4
+
5
+ <TMagicCollapse class="ui-component-panel" :model-value="collapseValue">
6
+ <SearchInput @search="filterTextChangeHandler"></SearchInput>
7
+ <template v-for="(group, index) in list">
8
+ <TMagicCollapseItem v-if="group.items && group.items.length" :key="index" :name="`${index}`">
9
+ <template #title><MIcon :icon="Grid"></MIcon>{{ group.title }}</template>
10
+ <div
11
+ v-for="item in group.items"
12
+ class="component-item"
13
+ draggable="true"
14
+ :key="item.type"
15
+ @click="appendComponent(item)"
16
+ @dragstart="dragstartHandler(item, $event)"
17
+ @dragend="dragendHandler"
18
+ @drag="dragHandler"
19
+ >
20
+ <slot name="component-list-item" :component="item">
21
+ <TMagicTooltip placement="right" :disabled="!Boolean(item.desc)" :content="item.desc">
22
+ <MIcon :icon="item.icon"></MIcon>
23
+ </TMagicTooltip>
24
+ <span :title="item.text">{{ item.text }}</span>
25
+ </slot>
26
+ </div>
27
+ </TMagicCollapseItem>
28
+ </template>
29
+ </TMagicCollapse>
30
+ </TMagicScrollbar>
31
+ </template>
32
+
33
+ <script lang="ts" setup>
34
+ import { computed, inject, ref } from 'vue';
35
+ import { Grid } from '@element-plus/icons-vue';
36
+ import serialize from 'serialize-javascript';
37
+
38
+ import { TMagicCollapse, TMagicCollapseItem, TMagicScrollbar, TMagicTooltip } from '@tmagic/design';
39
+ import { removeClassNameByClassName } from '@tmagic/utils';
40
+
41
+ import MIcon from '@editor/components/Icon.vue';
42
+ import SearchInput from '@editor/components/SearchInput.vue';
43
+ import {
44
+ type ComponentGroup,
45
+ type ComponentItem,
46
+ ComponentListPanelSlots,
47
+ DragType,
48
+ type Services,
49
+ type StageOptions,
50
+ } from '@editor/type';
51
+
52
+ defineSlots<ComponentListPanelSlots>();
53
+
54
+ defineOptions({
55
+ name: 'MEditorComponentListPanel',
56
+ });
57
+
58
+ const searchText = ref('');
59
+
60
+ const filterTextChangeHandler = (v: string) => {
61
+ searchText.value = v;
62
+ };
63
+
64
+ const services = inject<Services>('services');
65
+ const stageOptions = inject<StageOptions>('stageOptions');
66
+
67
+ const stage = computed(() => services?.editorService.get('stage'));
68
+ const list = computed(() =>
69
+ services?.componentListService.getList().map((group: ComponentGroup) => ({
70
+ ...group,
71
+ items: group.items.filter((item: ComponentItem) => item.text.includes(searchText.value)),
72
+ })),
73
+ );
74
+ const collapseValue = computed(() =>
75
+ Array(list.value?.length)
76
+ .fill(1)
77
+ .map((x, i) => `${i}`),
78
+ );
79
+
80
+ let timeout: NodeJS.Timeout | undefined;
81
+ let clientX: number;
82
+ let clientY: number;
83
+
84
+ const appendComponent = ({ text, type, data = {} }: ComponentItem): void => {
85
+ services?.editorService.add({
86
+ name: text,
87
+ type,
88
+ ...data,
89
+ });
90
+ };
91
+
92
+ const dragstartHandler = ({ text, type, data = {} }: ComponentItem, e: DragEvent) => {
93
+ e.dataTransfer?.setData(
94
+ 'text/json',
95
+ serialize({
96
+ dragType: DragType.COMPONENT_LIST,
97
+ data: {
98
+ name: text,
99
+ type,
100
+ ...data,
101
+ },
102
+ }),
103
+ );
104
+ };
105
+
106
+ const dragendHandler = () => {
107
+ if (timeout) {
108
+ globalThis.clearTimeout(timeout);
109
+ timeout = undefined;
110
+ }
111
+ const doc = stage.value?.renderer.getDocument();
112
+ if (doc && stageOptions?.containerHighlightClassName) {
113
+ removeClassNameByClassName(doc, stageOptions.containerHighlightClassName);
114
+ }
115
+ clientX = 0;
116
+ clientY = 0;
117
+ };
118
+
119
+ const dragHandler = (e: DragEvent) => {
120
+ if (e.clientX !== clientX || e.clientY !== clientY) {
121
+ clientX = e.clientX;
122
+ clientY = e.clientY;
123
+ if (timeout) {
124
+ globalThis.clearTimeout(timeout);
125
+ timeout = undefined;
126
+ }
127
+ return;
128
+ }
129
+
130
+ if (timeout || !stage.value) return;
131
+
132
+ timeout = stage.value.delayedMarkContainer(e);
133
+ };
134
+ </script>