@tmagic/editor 1.4.8 → 1.4.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.
- package/dist/style.css +26 -0
- package/dist/tmagic-editor.js +3213 -2820
- package/dist/tmagic-editor.umd.cjs +3237 -2839
- package/package.json +14 -12
- package/src/Editor.vue +212 -0
- package/src/components/CodeBlockEditor.vue +268 -0
- package/src/components/CodeParams.vue +60 -0
- package/src/components/ContentMenu.vue +193 -0
- package/src/components/FloatingBox.vue +178 -0
- package/src/components/Icon.vue +27 -0
- package/src/components/Resizer.vue +23 -0
- package/src/components/ScrollBar.vue +151 -0
- package/src/components/ScrollViewer.vue +128 -0
- package/src/components/SearchInput.vue +37 -0
- package/src/components/SplitView.vue +186 -0
- package/src/components/ToolButton.vue +138 -0
- package/src/components/Tree.vue +71 -0
- package/src/components/TreeNode.vue +150 -0
- package/src/editorProps.ts +120 -0
- package/src/fields/Code.vue +50 -0
- package/src/fields/CodeLink.vue +80 -0
- package/src/fields/CodeSelect.vue +132 -0
- package/src/fields/CodeSelectCol.vue +139 -0
- package/src/fields/CondOpSelect.vue +87 -0
- package/src/fields/DataSourceFieldSelect/FieldSelect.vue +152 -0
- package/src/fields/DataSourceFieldSelect/Index.vue +168 -0
- package/src/fields/DataSourceFields.vue +329 -0
- package/src/fields/DataSourceInput.vue +339 -0
- package/src/fields/DataSourceMethodSelect.vue +153 -0
- package/src/fields/DataSourceMethods.vue +104 -0
- package/src/fields/DataSourceMocks.vue +255 -0
- package/src/fields/DataSourceSelect.vue +98 -0
- package/src/fields/DisplayConds.vue +145 -0
- package/src/fields/EventSelect.vue +375 -0
- package/src/fields/KeyValue.vue +137 -0
- package/src/fields/PageFragmentSelect.vue +63 -0
- package/src/fields/UISelect.vue +135 -0
- package/src/hooks/index.ts +24 -0
- package/src/hooks/use-code-block-edit.ts +83 -0
- package/src/hooks/use-data-source-edit.ts +46 -0
- package/src/hooks/use-data-source-method.ts +100 -0
- package/src/hooks/use-editor-content-height.ts +26 -0
- package/src/hooks/use-filter.ts +55 -0
- package/src/hooks/use-float-box.ts +76 -0
- package/src/hooks/use-getso.ts +35 -0
- package/src/hooks/use-next-float-box-position.ts +29 -0
- package/src/hooks/use-node-status.ts +48 -0
- package/src/hooks/use-stage.ts +129 -0
- package/src/hooks/use-window-rect.ts +20 -0
- package/src/icons/AppManageIcon.vue +15 -0
- package/src/icons/CenterIcon.vue +13 -0
- package/src/icons/CodeIcon.vue +28 -0
- package/src/icons/FolderMinusIcon.vue +22 -0
- package/src/icons/PinIcon.vue +25 -0
- package/src/icons/PinnedIcon.vue +25 -0
- package/src/index.ts +124 -0
- package/src/initService.ts +448 -0
- package/src/layouts/AddPageBox.vue +55 -0
- package/src/layouts/CodeEditor.vue +226 -0
- package/src/layouts/Framework.vue +165 -0
- package/src/layouts/NavMenu.vue +200 -0
- package/src/layouts/PropsPanel.vue +131 -0
- package/src/layouts/page-bar/AddButton.vue +48 -0
- package/src/layouts/page-bar/PageBar.vue +165 -0
- package/src/layouts/page-bar/PageBarScrollContainer.vue +192 -0
- package/src/layouts/page-bar/PageList.vue +55 -0
- package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
- package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
- package/src/layouts/sidebar/Sidebar.vue +278 -0
- package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
- package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
- package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
- package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
- package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
- package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
- package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
- package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
- package/src/layouts/sidebar/layer/use-click.ts +113 -0
- package/src/layouts/sidebar/layer/use-drag.ts +167 -0
- package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
- package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
- package/src/layouts/workspace/Breadcrumb.vue +35 -0
- package/src/layouts/workspace/Workspace.vue +46 -0
- package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
- package/src/layouts/workspace/viewer/Stage.vue +246 -0
- package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
- package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
- package/src/services/BaseService.ts +230 -0
- package/src/services/codeBlock.ts +327 -0
- package/src/services/componentList.ts +58 -0
- package/src/services/dataSource.ts +216 -0
- package/src/services/dep.ts +152 -0
- package/src/services/editor.ts +1135 -0
- package/src/services/events.ts +93 -0
- package/src/services/history.ts +126 -0
- package/src/services/keybinding.ts +220 -0
- package/src/services/props.ts +286 -0
- package/src/services/stageOverlay.ts +212 -0
- package/src/services/storage.ts +155 -0
- package/src/services/ui.ts +157 -0
- package/src/shims-vue.d.ts +6 -0
- package/src/theme/breadcrumb.scss +6 -0
- package/src/theme/code-block.scss +6 -0
- package/src/theme/code-editor.scss +38 -0
- package/src/theme/common/var.scss +14 -0
- package/src/theme/component-list-panel.scss +95 -0
- package/src/theme/content-menu.scss +76 -0
- package/src/theme/data-source-field-select.scss +16 -0
- package/src/theme/data-source-field.scss +12 -0
- package/src/theme/data-source-fields.scss +13 -0
- package/src/theme/data-source-input.scss +18 -0
- package/src/theme/data-source-methods.scss +13 -0
- package/src/theme/data-source.scss +28 -0
- package/src/theme/event.scss +37 -0
- package/src/theme/floating-box.scss +32 -0
- package/src/theme/framework.scss +69 -0
- package/src/theme/icon.scss +12 -0
- package/src/theme/index.scss +10 -0
- package/src/theme/key-value.scss +20 -0
- package/src/theme/layer-panel.scss +26 -0
- package/src/theme/layout.scss +9 -0
- package/src/theme/nav-menu.scss +78 -0
- package/src/theme/page-bar.scss +102 -0
- package/src/theme/page-fragment-select.scss +14 -0
- package/src/theme/props-panel.scss +55 -0
- package/src/theme/resizer.scss +66 -0
- package/src/theme/ruler.scss +38 -0
- package/src/theme/search-input.scss +14 -0
- package/src/theme/sidebar.scss +79 -0
- package/src/theme/stage.scss +76 -0
- package/src/theme/theme.scss +28 -0
- package/src/theme/tree.scss +89 -0
- package/src/theme/workspace.scss +9 -0
- package/src/type.ts +780 -0
- package/src/utils/compose.ts +52 -0
- package/src/utils/config.ts +29 -0
- package/src/utils/content-menu.ts +95 -0
- package/src/utils/data-source/formConfigs/base.ts +30 -0
- package/src/utils/data-source/formConfigs/http.ts +60 -0
- package/src/utils/data-source/index.ts +249 -0
- package/src/utils/editor.ts +313 -0
- package/src/utils/idle-task.ts +72 -0
- package/src/utils/index.ts +24 -0
- package/src/utils/keybinding-config.ts +125 -0
- package/src/utils/logger.ts +47 -0
- package/src/utils/monaco-editor.ts +8 -0
- package/src/utils/operator.ts +106 -0
- package/src/utils/props.ts +407 -0
- package/src/utils/scroll-viewer.ts +165 -0
- package/src/utils/tree.ts +15 -0
- package/src/utils/undo-redo.ts +76 -0
- package/types/editorProps.d.ts +3 -1
- package/types/fields/CondOpSelect.vue.d.ts +17 -0
- package/types/fields/DataSourceFieldSelect/FieldSelect.vue.d.ts +36 -0
- package/types/fields/{DataSourceFieldSelect.vue.d.ts → DataSourceFieldSelect/Index.vue.d.ts} +1 -1
- package/types/fields/DisplayConds.vue.d.ts +32 -0
- package/types/index.d.ts +3 -1
- package/types/layouts/Framework.vue.d.ts +3 -1
- package/types/layouts/PropsPanel.vue.d.ts +21 -21
- package/types/layouts/page-bar/PageBar.vue.d.ts +6 -0
- package/types/layouts/page-bar/PageBarScrollContainer.vue.d.ts +3 -0
- package/types/layouts/page-bar/PageList.vue.d.ts +25 -0
- package/types/services/ui.d.ts +1 -0
- package/types/type.d.ts +30 -4
- package/types/utils/data-source/index.d.ts +1 -0
- package/types/utils/props.d.ts +12 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<ScrollViewer
|
|
3
|
+
class="m-editor-stage"
|
|
4
|
+
ref="stageWrap"
|
|
5
|
+
tabindex="-1"
|
|
6
|
+
:width="stageRect?.width"
|
|
7
|
+
:height="stageRect?.height"
|
|
8
|
+
:wrap-width="stageContainerRect?.width"
|
|
9
|
+
:wrap-height="stageContainerRect?.height"
|
|
10
|
+
:zoom="zoom"
|
|
11
|
+
:correction-scroll-size="{
|
|
12
|
+
width: 60,
|
|
13
|
+
height: 50,
|
|
14
|
+
}"
|
|
15
|
+
@click="stageWrap?.container?.focus()"
|
|
16
|
+
>
|
|
17
|
+
<div
|
|
18
|
+
class="m-editor-stage-container"
|
|
19
|
+
ref="stageContainer"
|
|
20
|
+
:style="`transform: scale(${zoom});`"
|
|
21
|
+
@contextmenu="contextmenuHandler"
|
|
22
|
+
@drop="dropHandler"
|
|
23
|
+
@dragover="dragoverHandler"
|
|
24
|
+
></div>
|
|
25
|
+
|
|
26
|
+
<NodeListMenu></NodeListMenu>
|
|
27
|
+
|
|
28
|
+
<template #content>
|
|
29
|
+
<StageOverlay v-if="!disabledStageOverlay"></StageOverlay>
|
|
30
|
+
|
|
31
|
+
<Teleport to="body">
|
|
32
|
+
<ViewerMenu
|
|
33
|
+
ref="menu"
|
|
34
|
+
:is-multi-select="isMultiSelect"
|
|
35
|
+
:stage-content-menu="stageContentMenu"
|
|
36
|
+
:custom-content-menu="customContentMenu"
|
|
37
|
+
></ViewerMenu>
|
|
38
|
+
</Teleport>
|
|
39
|
+
</template>
|
|
40
|
+
</ScrollViewer>
|
|
41
|
+
</template>
|
|
42
|
+
|
|
43
|
+
<script lang="ts" setup>
|
|
44
|
+
import { computed, inject, markRaw, nextTick, onBeforeUnmount, onMounted, ref, toRaw, watch, watchEffect } from 'vue';
|
|
45
|
+
import { cloneDeep } from 'lodash-es';
|
|
46
|
+
|
|
47
|
+
import type { MApp, MContainer } from '@tmagic/schema';
|
|
48
|
+
import StageCore, { getOffset, Runtime } from '@tmagic/stage';
|
|
49
|
+
import { calcValueByFontsize } from '@tmagic/utils';
|
|
50
|
+
|
|
51
|
+
import ScrollViewer from '@editor/components/ScrollViewer.vue';
|
|
52
|
+
import { useStage } from '@editor/hooks/use-stage';
|
|
53
|
+
import { DragType, Layout, type MenuButton, type MenuComponent, type Services, type StageOptions } from '@editor/type';
|
|
54
|
+
import { getConfig } from '@editor/utils/config';
|
|
55
|
+
import { KeyBindingContainerKey } from '@editor/utils/keybinding-config';
|
|
56
|
+
|
|
57
|
+
import NodeListMenu from './NodeListMenu.vue';
|
|
58
|
+
import StageOverlay from './StageOverlay.vue';
|
|
59
|
+
import ViewerMenu from './ViewerMenu.vue';
|
|
60
|
+
|
|
61
|
+
defineOptions({
|
|
62
|
+
name: 'MEditorStage',
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
withDefaults(
|
|
66
|
+
defineProps<{
|
|
67
|
+
stageContentMenu: (MenuButton | MenuComponent)[];
|
|
68
|
+
disabledStageOverlay?: boolean;
|
|
69
|
+
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
|
|
70
|
+
}>(),
|
|
71
|
+
{
|
|
72
|
+
disabledStageOverlay: false,
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
let stage: StageCore | null = null;
|
|
77
|
+
let runtime: Runtime | null = null;
|
|
78
|
+
|
|
79
|
+
const services = inject<Services>('services');
|
|
80
|
+
const stageOptions = inject<StageOptions>('stageOptions');
|
|
81
|
+
|
|
82
|
+
const stageWrap = ref<InstanceType<typeof ScrollViewer>>();
|
|
83
|
+
const stageContainer = ref<HTMLDivElement>();
|
|
84
|
+
const menu = ref<InstanceType<typeof ViewerMenu>>();
|
|
85
|
+
|
|
86
|
+
const nodes = computed(() => services?.editorService.get('nodes') || []);
|
|
87
|
+
const isMultiSelect = computed(() => nodes.value.length > 1);
|
|
88
|
+
const stageRect = computed(() => services?.uiService.get('stageRect'));
|
|
89
|
+
const stageContainerRect = computed(() => services?.uiService.get('stageContainerRect'));
|
|
90
|
+
const root = computed(() => services?.editorService.get('root'));
|
|
91
|
+
const page = computed(() => services?.editorService.get('page'));
|
|
92
|
+
const zoom = computed(() => services?.uiService.get('zoom') || 1);
|
|
93
|
+
const node = computed(() => services?.editorService.get('node'));
|
|
94
|
+
|
|
95
|
+
watchEffect(() => {
|
|
96
|
+
if (stage || !page.value) return;
|
|
97
|
+
|
|
98
|
+
if (!stageContainer.value) return;
|
|
99
|
+
if (!(stageOptions?.runtimeUrl || stageOptions?.render) || !root.value) return;
|
|
100
|
+
|
|
101
|
+
stage = useStage(stageOptions);
|
|
102
|
+
|
|
103
|
+
stage.on('select', () => {
|
|
104
|
+
stageWrap.value?.container?.focus();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
services?.editorService.set('stage', markRaw(stage));
|
|
108
|
+
|
|
109
|
+
stage.mount(stageContainer.value);
|
|
110
|
+
|
|
111
|
+
if (!node.value?.id) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
stage.on('runtime-ready', (rt) => {
|
|
116
|
+
runtime = rt;
|
|
117
|
+
// toRaw返回的值是一个引用而非快照,需要cloneDeep
|
|
118
|
+
root.value && runtime?.updateRootConfig?.(cloneDeep(toRaw(root.value)));
|
|
119
|
+
page.value?.id && runtime?.updatePageId?.(page.value.id);
|
|
120
|
+
setTimeout(() => {
|
|
121
|
+
node.value && stage?.select(toRaw(node.value.id));
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
watch(zoom, (zoom) => {
|
|
127
|
+
if (!stage || !zoom) return;
|
|
128
|
+
stage.setZoom(zoom);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
watch(page, (page) => {
|
|
132
|
+
if (runtime && page) {
|
|
133
|
+
services?.editorService.set('stageLoading', true);
|
|
134
|
+
runtime.updatePageId?.(page.id);
|
|
135
|
+
nextTick(() => {
|
|
136
|
+
stage?.select(page.id);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const rootChangeHandler = (root: MApp) => {
|
|
142
|
+
if (runtime && root) {
|
|
143
|
+
runtime.updateRootConfig?.(cloneDeep(toRaw(root)));
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
services?.editorService.on('root-change', rootChangeHandler);
|
|
148
|
+
|
|
149
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
150
|
+
for (const { contentRect } of entries) {
|
|
151
|
+
services?.uiService.set('stageContainerRect', {
|
|
152
|
+
width: contentRect.width,
|
|
153
|
+
height: contentRect.height,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
onMounted(() => {
|
|
159
|
+
if (stageWrap.value?.container) {
|
|
160
|
+
resizeObserver.observe(stageWrap.value.container);
|
|
161
|
+
services?.keybindingService.registerEl(KeyBindingContainerKey.STAGE, stageWrap.value.container);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
onBeforeUnmount(() => {
|
|
166
|
+
stage?.destroy();
|
|
167
|
+
resizeObserver.disconnect();
|
|
168
|
+
services?.editorService.set('stage', null);
|
|
169
|
+
services?.keybindingService.unregisterEl('stage');
|
|
170
|
+
services?.editorService.off('root-change', rootChangeHandler);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const parseDSL = getConfig('parseDSL');
|
|
174
|
+
|
|
175
|
+
const contextmenuHandler = (e: MouseEvent) => {
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
menu.value?.show(e);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const dragoverHandler = (e: DragEvent) => {
|
|
181
|
+
if (!e.dataTransfer) return;
|
|
182
|
+
e.preventDefault();
|
|
183
|
+
e.dataTransfer.dropEffect = 'move';
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const dropHandler = async (e: DragEvent) => {
|
|
187
|
+
if (!e.dataTransfer) return;
|
|
188
|
+
|
|
189
|
+
const data = e.dataTransfer.getData('text/json');
|
|
190
|
+
|
|
191
|
+
if (!data) return;
|
|
192
|
+
|
|
193
|
+
const config = parseDSL(`(${data})`);
|
|
194
|
+
|
|
195
|
+
if (!config || config.dragType !== DragType.COMPONENT_LIST) return;
|
|
196
|
+
|
|
197
|
+
e.preventDefault();
|
|
198
|
+
|
|
199
|
+
const doc = stage?.renderer.contentWindow?.document;
|
|
200
|
+
const parentEl: HTMLElement | null | undefined = doc?.querySelector(`.${stageOptions?.containerHighlightClassName}`);
|
|
201
|
+
|
|
202
|
+
let parent: MContainer | undefined | null = page.value;
|
|
203
|
+
if (parentEl) {
|
|
204
|
+
parent = services?.editorService.getNodeById(parentEl.id, false) as MContainer;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (parent && stageContainer.value && stage) {
|
|
208
|
+
const layout = await services?.editorService.getLayout(parent);
|
|
209
|
+
|
|
210
|
+
const containerRect = stageContainer.value.getBoundingClientRect();
|
|
211
|
+
const { scrollTop, scrollLeft } = stage.mask;
|
|
212
|
+
const { style = {} } = config.data;
|
|
213
|
+
|
|
214
|
+
let top = 0;
|
|
215
|
+
let left = 0;
|
|
216
|
+
let position = 'relative';
|
|
217
|
+
|
|
218
|
+
if (style.position === 'fixed') {
|
|
219
|
+
position = 'fixed';
|
|
220
|
+
top = e.clientY - containerRect.top;
|
|
221
|
+
left = e.clientX - containerRect.left;
|
|
222
|
+
} else if (layout === Layout.ABSOLUTE) {
|
|
223
|
+
position = 'absolute';
|
|
224
|
+
top = e.clientY - containerRect.top + scrollTop;
|
|
225
|
+
left = e.clientX - containerRect.left + scrollLeft;
|
|
226
|
+
|
|
227
|
+
if (parentEl) {
|
|
228
|
+
const { left: parentLeft, top: parentTop } = getOffset(parentEl);
|
|
229
|
+
left = left - parentLeft * zoom.value;
|
|
230
|
+
top = top - parentTop * zoom.value;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
config.data.style = {
|
|
235
|
+
...style,
|
|
236
|
+
position,
|
|
237
|
+
top: calcValueByFontsize(doc, top / zoom.value),
|
|
238
|
+
left: calcValueByFontsize(doc, left / zoom.value),
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
config.data.inputEvent = e;
|
|
242
|
+
|
|
243
|
+
services?.editorService.add(config.data, parent);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
</script>
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div v-if="stageOverlayVisible" class="m-editor-stage-overlay" @click="closeOverlayHandler">
|
|
3
|
+
<TMagicIcon class="m-editor-stage-overlay-close" :size="'20'" @click="closeOverlayHandler"
|
|
4
|
+
><CloseBold
|
|
5
|
+
/></TMagicIcon>
|
|
6
|
+
<div ref="stageOverlay" class="m-editor-stage-overlay-container" :style="style" @click.stop></div>
|
|
7
|
+
</div>
|
|
8
|
+
</template>
|
|
9
|
+
|
|
10
|
+
<script setup lang="ts">
|
|
11
|
+
import { computed, inject, ref, watch } from 'vue';
|
|
12
|
+
import { CloseBold } from '@element-plus/icons-vue';
|
|
13
|
+
|
|
14
|
+
import { TMagicIcon } from '@tmagic/design';
|
|
15
|
+
|
|
16
|
+
import type { Services, StageOptions } from '@editor/type';
|
|
17
|
+
|
|
18
|
+
const services = inject<Services>('services');
|
|
19
|
+
|
|
20
|
+
const stageOptions = inject<StageOptions>('stageOptions');
|
|
21
|
+
|
|
22
|
+
const stageOverlay = ref<HTMLDivElement>();
|
|
23
|
+
|
|
24
|
+
const stageOverlayVisible = computed(() => services?.stageOverlayService.get('stageOverlayVisible'));
|
|
25
|
+
const wrapWidth = computed(() => services?.stageOverlayService.get('wrapWidth') || 0);
|
|
26
|
+
const wrapHeight = computed(() => services?.stageOverlayService.get('wrapHeight') || 0);
|
|
27
|
+
const stage = computed(() => services?.editorService.get('stage'));
|
|
28
|
+
|
|
29
|
+
const style = computed(() => ({
|
|
30
|
+
width: `${wrapWidth.value}px`,
|
|
31
|
+
height: `${wrapHeight.value}px`,
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
watch(stage, (stage) => {
|
|
35
|
+
if (stage) {
|
|
36
|
+
stage.on('dblclick', async (event: MouseEvent) => {
|
|
37
|
+
const el = await stage.actionManager.getElementFromPoint(event);
|
|
38
|
+
services?.stageOverlayService.openOverlay(el);
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
services?.stageOverlayService.closeOverlay();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
watch(stageOverlay, (stageOverlay) => {
|
|
46
|
+
if (!services) return;
|
|
47
|
+
|
|
48
|
+
const subStage = services.stageOverlayService.createStage(stageOptions);
|
|
49
|
+
services?.stageOverlayService.set('stage', subStage);
|
|
50
|
+
|
|
51
|
+
if (stageOverlay && subStage) {
|
|
52
|
+
subStage.mount(stageOverlay);
|
|
53
|
+
|
|
54
|
+
const { mask, renderer } = subStage;
|
|
55
|
+
|
|
56
|
+
const { contentWindow } = renderer;
|
|
57
|
+
mask.showRule(false);
|
|
58
|
+
|
|
59
|
+
services?.stageOverlayService.updateOverlay();
|
|
60
|
+
|
|
61
|
+
contentWindow?.magic.onRuntimeReady({});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const closeOverlayHandler = () => {
|
|
66
|
+
services?.stageOverlayService.closeOverlay();
|
|
67
|
+
};
|
|
68
|
+
</script>
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<ContentMenu :menu-data="menuData" ref="menu"></ContentMenu>
|
|
3
|
+
</template>
|
|
4
|
+
|
|
5
|
+
<script lang="ts" setup>
|
|
6
|
+
import { computed, inject, markRaw, ref, watch } from 'vue';
|
|
7
|
+
import { Bottom, Top } from '@element-plus/icons-vue';
|
|
8
|
+
|
|
9
|
+
import { NodeType } from '@tmagic/schema';
|
|
10
|
+
import { isPage, isPageFragment } from '@tmagic/utils';
|
|
11
|
+
|
|
12
|
+
import ContentMenu from '@editor/components/ContentMenu.vue';
|
|
13
|
+
import CenterIcon from '@editor/icons/CenterIcon.vue';
|
|
14
|
+
import { LayerOffset, Layout, MenuButton, MenuComponent, Services } from '@editor/type';
|
|
15
|
+
import { useCopyMenu, useDeleteMenu, useMoveToMenu, usePasteMenu } from '@editor/utils/content-menu';
|
|
16
|
+
|
|
17
|
+
defineOptions({
|
|
18
|
+
name: 'MEditorViewerMenu',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const props = withDefaults(
|
|
22
|
+
defineProps<{
|
|
23
|
+
isMultiSelect?: boolean;
|
|
24
|
+
stageContentMenu: (MenuButton | MenuComponent)[];
|
|
25
|
+
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
|
|
26
|
+
}>(),
|
|
27
|
+
{
|
|
28
|
+
isMultiSelect: false,
|
|
29
|
+
stageContentMenu: () => [],
|
|
30
|
+
customContentMenu: (menus: (MenuButton | MenuComponent)[]) => menus,
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const services = inject<Services>('services');
|
|
35
|
+
const editorService = services?.editorService;
|
|
36
|
+
const menu = ref<InstanceType<typeof ContentMenu>>();
|
|
37
|
+
const canCenter = ref(false);
|
|
38
|
+
|
|
39
|
+
const node = computed(() => editorService?.get('node'));
|
|
40
|
+
const nodes = computed(() => editorService?.get('nodes'));
|
|
41
|
+
const parent = computed(() => editorService?.get('parent'));
|
|
42
|
+
|
|
43
|
+
const menuData = computed<(MenuButton | MenuComponent)[]>(() =>
|
|
44
|
+
props.customContentMenu(
|
|
45
|
+
[
|
|
46
|
+
{
|
|
47
|
+
type: 'button',
|
|
48
|
+
text: '水平居中',
|
|
49
|
+
icon: markRaw(CenterIcon),
|
|
50
|
+
display: () => canCenter.value,
|
|
51
|
+
handler: () => {
|
|
52
|
+
if (!nodes.value) return;
|
|
53
|
+
editorService?.alignCenter(nodes.value);
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
useCopyMenu(),
|
|
57
|
+
usePasteMenu(menu),
|
|
58
|
+
{
|
|
59
|
+
type: 'divider',
|
|
60
|
+
direction: 'horizontal',
|
|
61
|
+
display: () => {
|
|
62
|
+
if (!node.value) return false;
|
|
63
|
+
return !isPage(node.value) && !isPageFragment(node.value);
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
type: 'button',
|
|
68
|
+
text: '上移一层',
|
|
69
|
+
icon: markRaw(Top),
|
|
70
|
+
display: () => !isPage(node.value) && !isPageFragment(node.value) && !props.isMultiSelect,
|
|
71
|
+
handler: () => {
|
|
72
|
+
editorService?.moveLayer(1);
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
type: 'button',
|
|
77
|
+
text: '下移一层',
|
|
78
|
+
icon: markRaw(Bottom),
|
|
79
|
+
display: () => !isPage(node.value) && !isPageFragment(node.value) && !props.isMultiSelect,
|
|
80
|
+
handler: () => {
|
|
81
|
+
editorService?.moveLayer(-1);
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: 'button',
|
|
86
|
+
text: '置顶',
|
|
87
|
+
icon: markRaw(Top),
|
|
88
|
+
display: () => !isPage(node.value) && !isPageFragment(node.value) && !props.isMultiSelect,
|
|
89
|
+
handler: () => {
|
|
90
|
+
editorService?.moveLayer(LayerOffset.TOP);
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
type: 'button',
|
|
95
|
+
text: '置底',
|
|
96
|
+
icon: markRaw(Bottom),
|
|
97
|
+
display: () => !isPage(node.value) && !isPageFragment(node.value) && !props.isMultiSelect,
|
|
98
|
+
handler: () => {
|
|
99
|
+
editorService?.moveLayer(LayerOffset.BOTTOM);
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
useMoveToMenu(services),
|
|
103
|
+
{
|
|
104
|
+
type: 'divider',
|
|
105
|
+
direction: 'horizontal',
|
|
106
|
+
display: () => !isPage(node.value) && !isPageFragment(node.value) && !props.isMultiSelect,
|
|
107
|
+
},
|
|
108
|
+
useDeleteMenu(),
|
|
109
|
+
{
|
|
110
|
+
type: 'divider',
|
|
111
|
+
direction: 'horizontal',
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
type: 'button',
|
|
115
|
+
text: '清空参考线',
|
|
116
|
+
handler: () => {
|
|
117
|
+
editorService?.get('stage')?.clearGuides();
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
...props.stageContentMenu,
|
|
121
|
+
],
|
|
122
|
+
'viewer',
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
watch(
|
|
127
|
+
parent,
|
|
128
|
+
async () => {
|
|
129
|
+
if (!parent.value || !editorService) return (canCenter.value = false);
|
|
130
|
+
const layout = await editorService.getLayout(parent.value);
|
|
131
|
+
const isLayoutConform = [Layout.ABSOLUTE, Layout.FIXED].includes(layout);
|
|
132
|
+
const isTypeConform = nodes.value?.every(
|
|
133
|
+
(selectedNode) => ![NodeType.ROOT, NodeType.PAGE, 'pop'].includes(`${selectedNode?.type}`),
|
|
134
|
+
);
|
|
135
|
+
canCenter.value = isLayoutConform && !!isTypeConform;
|
|
136
|
+
},
|
|
137
|
+
{ immediate: true },
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const show = (e: MouseEvent) => {
|
|
141
|
+
menu.value?.show(e);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
defineExpose({ show });
|
|
145
|
+
</script>
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making TMagicEditor available.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 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 { EventEmitter } from 'events';
|
|
20
|
+
|
|
21
|
+
import { compose } from '@editor/utils/compose';
|
|
22
|
+
|
|
23
|
+
const methodName = (prefix: string, name: string) => `${prefix}${name[0].toUpperCase()}${name.substring(1)}`;
|
|
24
|
+
|
|
25
|
+
const isError = (error: any): boolean => Object.prototype.toString.call(error) === '[object Error]';
|
|
26
|
+
|
|
27
|
+
const doAction = (
|
|
28
|
+
args: any[],
|
|
29
|
+
scope: any,
|
|
30
|
+
sourceMethod: any,
|
|
31
|
+
beforeMethodName: string,
|
|
32
|
+
afterMethodName: string,
|
|
33
|
+
fn: (args: any[], next?: Function | undefined) => void,
|
|
34
|
+
) => {
|
|
35
|
+
try {
|
|
36
|
+
let beforeArgs = args;
|
|
37
|
+
|
|
38
|
+
for (const beforeMethod of scope.pluginOptionsList[beforeMethodName]) {
|
|
39
|
+
beforeArgs = beforeMethod(...beforeArgs) || [];
|
|
40
|
+
|
|
41
|
+
if (isError(beforeArgs)) throw beforeArgs;
|
|
42
|
+
|
|
43
|
+
if (!Array.isArray(beforeArgs)) {
|
|
44
|
+
beforeArgs = [beforeArgs];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let returnValue: any = fn(beforeArgs, sourceMethod.bind(scope));
|
|
49
|
+
|
|
50
|
+
for (const afterMethod of scope.pluginOptionsList[afterMethodName]) {
|
|
51
|
+
returnValue = afterMethod(returnValue, ...beforeArgs);
|
|
52
|
+
|
|
53
|
+
if (isError(returnValue)) throw returnValue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return returnValue;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const doAsyncAction = async (
|
|
63
|
+
args: any[],
|
|
64
|
+
scope: any,
|
|
65
|
+
sourceMethod: any,
|
|
66
|
+
beforeMethodName: string,
|
|
67
|
+
afterMethodName: string,
|
|
68
|
+
fn: (args: any[], next?: Function | undefined) => Promise<void> | void,
|
|
69
|
+
) => {
|
|
70
|
+
try {
|
|
71
|
+
let beforeArgs = args;
|
|
72
|
+
|
|
73
|
+
for (const beforeMethod of scope.pluginOptionsList[beforeMethodName]) {
|
|
74
|
+
beforeArgs = (await beforeMethod(...beforeArgs)) || [];
|
|
75
|
+
|
|
76
|
+
if (isError(beforeArgs)) throw beforeArgs;
|
|
77
|
+
|
|
78
|
+
if (!Array.isArray(beforeArgs)) {
|
|
79
|
+
beforeArgs = [beforeArgs];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let returnValue: any = await fn(beforeArgs, sourceMethod.bind(scope));
|
|
84
|
+
|
|
85
|
+
for (const afterMethod of scope.pluginOptionsList[afterMethodName]) {
|
|
86
|
+
returnValue = await afterMethod(returnValue, ...beforeArgs);
|
|
87
|
+
|
|
88
|
+
if (isError(returnValue)) throw returnValue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return returnValue;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 提供两种方式对Class进行扩展
|
|
99
|
+
* 方法1:
|
|
100
|
+
* 给Class中的每个方法都添加before after两个钩子
|
|
101
|
+
* 给Class添加一个usePlugin方法,use方法可以传入一个包含before或者after方法的对象
|
|
102
|
+
*
|
|
103
|
+
* 例如:
|
|
104
|
+
* Class EditorService extends BaseService {
|
|
105
|
+
* constructor() {
|
|
106
|
+
* super([ { name: 'add', isAsync: true },]);
|
|
107
|
+
* }
|
|
108
|
+
* add(value) { return result; }
|
|
109
|
+
* };
|
|
110
|
+
*
|
|
111
|
+
* const editorService = new EditorService();
|
|
112
|
+
*
|
|
113
|
+
* editorService.usePlugin({
|
|
114
|
+
* beforeAdd(value) { return [value] },
|
|
115
|
+
* afterAdd(result, value) { return result },
|
|
116
|
+
* });
|
|
117
|
+
*
|
|
118
|
+
* editorService.add(); 最终会变成 () => {
|
|
119
|
+
* editorService.beforeAdd();
|
|
120
|
+
* editorService.add();
|
|
121
|
+
* editorService.afterAdd();
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* 调用时的参数会透传到before方法的参数中, 然后before的return 会作为原方法的参数和after的参数,after第一个参数则是原方法的return值;
|
|
125
|
+
* 如需终止后续方法调用可以return new Error();
|
|
126
|
+
*
|
|
127
|
+
* 方法2:
|
|
128
|
+
* 给Class中的每个方法都添加中间件
|
|
129
|
+
* 给Class添加一个use方法,use方法可以传入一个包含源对象方法名作为key值的对象
|
|
130
|
+
*
|
|
131
|
+
* 例如:
|
|
132
|
+
* Class EditorService extends BaseService {
|
|
133
|
+
* constructor() {
|
|
134
|
+
* super([ { name: 'add', isAsync: true },]);
|
|
135
|
+
* }
|
|
136
|
+
* add(value) { return result; }
|
|
137
|
+
* };
|
|
138
|
+
*
|
|
139
|
+
* const editorService = new EditorService();
|
|
140
|
+
* editorService.use({
|
|
141
|
+
* add(value, next) { console.log(value); next() },
|
|
142
|
+
* });
|
|
143
|
+
*/
|
|
144
|
+
export default class extends EventEmitter {
|
|
145
|
+
private pluginOptionsList: Record<string, Function[]> = {};
|
|
146
|
+
private middleware: Record<string, Function[]> = {};
|
|
147
|
+
private taskList: (() => Promise<void>)[] = [];
|
|
148
|
+
private doingTask = false;
|
|
149
|
+
|
|
150
|
+
constructor(methods: { name: string; isAsync: boolean }[] = [], serialMethods: string[] = []) {
|
|
151
|
+
super();
|
|
152
|
+
|
|
153
|
+
methods.forEach(({ name: propertyName, isAsync }) => {
|
|
154
|
+
const scope = this as any;
|
|
155
|
+
|
|
156
|
+
const sourceMethod = scope[propertyName];
|
|
157
|
+
|
|
158
|
+
const beforeMethodName = methodName('before', propertyName);
|
|
159
|
+
const afterMethodName = methodName('after', propertyName);
|
|
160
|
+
|
|
161
|
+
this.pluginOptionsList[beforeMethodName] = [];
|
|
162
|
+
this.pluginOptionsList[afterMethodName] = [];
|
|
163
|
+
this.middleware[propertyName] = [];
|
|
164
|
+
|
|
165
|
+
const fn = compose(this.middleware[propertyName], isAsync);
|
|
166
|
+
Object.defineProperty(scope, propertyName, {
|
|
167
|
+
value: isAsync
|
|
168
|
+
? async (...args: any[]) => {
|
|
169
|
+
if (!serialMethods.includes(propertyName)) {
|
|
170
|
+
return doAsyncAction(args, scope, sourceMethod, beforeMethodName, afterMethodName, fn);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 由于async await,所以会出现函数执行到await时让出线程,导致执行顺序出错,例如调用了select(1) -> update -> select(2),这个时候就有可能出现update了2;
|
|
174
|
+
// 这里保证函数调用严格按顺序执行;
|
|
175
|
+
const promise = new Promise<any>((resolve, reject) => {
|
|
176
|
+
this.taskList.push(async () => {
|
|
177
|
+
try {
|
|
178
|
+
const value = await doAsyncAction(args, scope, sourceMethod, beforeMethodName, afterMethodName, fn);
|
|
179
|
+
resolve(value);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
reject(e);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (!this.doingTask) {
|
|
187
|
+
this.doTask();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return promise;
|
|
191
|
+
}
|
|
192
|
+
: (...args: any[]) => doAction(args, scope, sourceMethod, beforeMethodName, afterMethodName, fn),
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @deprecated 请使用usePlugin代替
|
|
199
|
+
*/
|
|
200
|
+
public use(options: Record<string, Function>) {
|
|
201
|
+
Object.entries(options).forEach(([methodName, method]: [string, Function]) => {
|
|
202
|
+
if (typeof method === 'function') this.middleware[methodName].push(method);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public usePlugin(options: Record<string, Function>) {
|
|
207
|
+
Object.entries(options).forEach(([methodName, method]: [string, Function]) => {
|
|
208
|
+
if (typeof method === 'function') this.pluginOptionsList[methodName].push(method);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public removeAllPlugins() {
|
|
213
|
+
Object.keys(this.pluginOptionsList).forEach((key) => {
|
|
214
|
+
this.pluginOptionsList[key] = [];
|
|
215
|
+
});
|
|
216
|
+
Object.keys(this.middleware).forEach((key) => {
|
|
217
|
+
this.middleware[key] = [];
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private async doTask() {
|
|
222
|
+
this.doingTask = true;
|
|
223
|
+
let task = this.taskList.shift();
|
|
224
|
+
while (task) {
|
|
225
|
+
await task();
|
|
226
|
+
task = this.taskList.shift();
|
|
227
|
+
}
|
|
228
|
+
this.doingTask = false;
|
|
229
|
+
}
|
|
230
|
+
}
|