Skip to content

Commit 2501fe7

Browse files
lichunnchilingling
andauthored
refactor: optimize layout module logic (#600)
* feat:add layout * feat: 调整layout导入 * feat: 把DesignCanvas移到canvas * feat: 暂时不改entry * fix: 添加注释 * feat:layout部分逻辑调整 * fix:调整AI图标 * fix:调整App引用 * feat:抽取Layout全局配置组件 * fix: sync newest change --------- Co-authored-by: chilingling <michaelchiling1024@gmail.com>
1 parent 5bc3872 commit 2501fe7

File tree

7 files changed

+221
-230
lines changed

7 files changed

+221
-230
lines changed

packages/design-core/src/App.vue

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,72 @@
33
</template>
44

55
<script>
6-
import { getMergeRegistry } from '@opentiny/tiny-engine-meta-register'
6+
import { watch, onUnmounted } from 'vue'
7+
import {
8+
getMergeRegistry,
9+
getMetaApi,
10+
useModal,
11+
useApp,
12+
useNotify,
13+
useResource,
14+
useCanvas
15+
} from '@opentiny/tiny-engine-meta-register'
16+
import { isVsCodeEnv } from '@opentiny/tiny-engine-common/js/environments'
17+
import { useBroadcastChannel } from '@vueuse/core'
18+
import { constants } from '@opentiny/tiny-engine-utils'
19+
20+
const { BROADCAST_CHANNEL } = constants
21+
const { message } = useModal()
722
823
export default {
924
setup() {
1025
const registry = getMergeRegistry()
26+
const materialsApi = getMetaApi('engine.plugins.materials')
27+
const blockApi = getMetaApi('engine.plugins.blockmanage')
28+
29+
// 此处接收画布内部的错误和警告提示
30+
const { data } = useBroadcastChannel({ name: BROADCAST_CHANNEL.Notify })
31+
32+
watch(data, (options) => useNotify(options))
33+
34+
if (isVsCodeEnv) {
35+
const appId = useApp().appInfoState.selectedId
36+
materialsApi
37+
.fetchGroups(appId)
38+
.then((groups) => {
39+
const blocks = []
40+
groups.forEach((group) => {
41+
blocks.push(...group.blocks)
42+
})
43+
blockApi.requestInitBlocks(blocks)
44+
})
45+
.catch((error) => {
46+
message({ message: error.message, status: 'error' })
47+
})
48+
}
49+
50+
const handlePopStateEvent = () => {
51+
useResource().handlePopStateEvent()
52+
}
53+
54+
window.addEventListener('popstate', handlePopStateEvent)
55+
56+
onUnmounted(() => {
57+
window.removeEventListener('popstate', handlePopStateEvent)
58+
})
59+
60+
watch(
61+
useCanvas().isCanvasApiReady,
62+
(ready) => {
63+
if (ready) {
64+
useResource().fetchResource()
65+
}
66+
},
67+
{
68+
immediate: true
69+
}
70+
)
71+
1172
return {
1273
registry
1374
}

packages/design-core/src/init.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ import { injectGlobalComponents, setGlobalMonacoEditorTheme, Modal, Notify } fro
1818
import { initHttp } from '@opentiny/tiny-engine-http'
1919
import TinyThemeTool from '@opentiny/vue-theme/theme-tool'
2020
import { tinySmbTheme } from '@opentiny/vue-theme/theme' // SMB 主题
21-
import { defineEntry, mergeRegistry, getMergeMeta, initHook, HOOK_NAME } from '@opentiny/tiny-engine-meta-register'
21+
import {
22+
defineEntry,
23+
mergeRegistry,
24+
getMergeMeta,
25+
initHook,
26+
HOOK_NAME,
27+
useEditorInfo
28+
} from '@opentiny/tiny-engine-meta-register'
2229
import App from './App.vue'
2330
import defaultRegistry from '../registry.js'
2431
import { registerConfigurators } from './registerConfigurators'
@@ -77,4 +84,5 @@ export const init = ({ selector = '#app', registry = defaultRegistry, lifeCycles
7784

7885
app.mount(selector)
7986
appMounted?.({ app })
87+
useEditorInfo().getUserInfo()
8088
}

packages/layout/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import component from './src/Main.vue'
22
import metaData from './meta'
33
import { LayoutService } from './src/composable'
4+
import designSmbConfig from '@opentiny/vue-design-smb'
5+
import { ConfigProvider as TinyConfigProvider } from '@opentiny/vue'
46

57
export default {
68
...metaData,
79
component,
10+
options: {
11+
configProvider: TinyConfigProvider,
12+
configProviderDesign: designSmbConfig
13+
},
814
metas: [LayoutService]
915
}
1016

packages/layout/src/DesignPlugins.vue

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@
4949
</span>
5050
</div>
5151
</li>
52-
<li
53-
v-if="state.independence"
54-
:key="state.bottomNavLists.length + 1"
55-
:class="['list-item']"
56-
:title="state.independence.title"
57-
@click="openAIRobot"
58-
>
59-
<div>
60-
<span class="item-icon">
61-
<img class="chatgpt-icon" src="../assets/AI.png" />
62-
</span>
63-
</div>
64-
</li>
6552
</ul>
6653
</div>
6754

@@ -82,14 +69,6 @@
8269
</keep-alive>
8370
</div>
8471
</div>
85-
<!-- AI 悬浮窗 -->
86-
<Teleport to="body">
87-
<div v-if="robotVisible" class="robot-dialog">
88-
<keep-alive>
89-
<component :is="robotComponent" @close-chat="robotVisible = false"></component>
90-
</keep-alive>
91-
</div>
92-
</Teleport>
9372
</template>
9473

9574
<script>
@@ -118,8 +97,6 @@ export default {
11897
const components = {}
11998
const iconComponents = {}
12099
const pluginRef = ref(null)
121-
const robotVisible = ref(false)
122-
const robotComponent = ref(null)
123100
const { isTemporaryPage } = usePage()
124101
125102
const {
@@ -139,24 +116,16 @@ export default {
139116
}
140117
})
141118
142-
const completed = ref(false)
143-
144119
const state = reactive({
145120
prevIdex: -2,
146121
topNavLists: props.plugins.filter((item) => item.align === 'top'),
147122
bottomNavLists: props.plugins.filter((item) => item.align === 'bottom'),
148123
independence: props.plugins.find((item) => item.align === 'independence')
149124
})
150125
151-
const doCompleted = () => {
152-
if (!completed.value) {
153-
completed.value = true
154-
useLayout().closePlugin()
155-
}
156-
}
157-
158126
const clickMenu = ({ item, index }) => {
159-
if (item.id === PLUGIN_NAME.EditorHelp) return
127+
if (item.id === PLUGIN_NAME.EditorHelp || item.id === PLUGIN_NAME.Robot) return
128+
160129
state.prevIdex = index
161130
162131
// 切换插件与关闭插件时确认
@@ -177,6 +146,7 @@ export default {
177146
})
178147
}
179148
}
149+
180150
watch(isTemporaryPage, () => {
181151
if (isTemporaryPage.saved) {
182152
const pagePanel = state.topNavLists?.find((item) => item.id === PLUGIN_NAME.AppManage) || null
@@ -187,10 +157,6 @@ export default {
187157
}
188158
})
189159
190-
const openAIRobot = () => {
191-
robotComponent.value = components[PLUGIN_NAME.Robot]
192-
robotVisible.value = !robotVisible.value
193-
}
194160
const close = () => {
195161
state.prevIdex = -2
196162
useLayout().closePlugin(true)
@@ -205,17 +171,12 @@ export default {
205171
return {
206172
state,
207173
clickMenu,
208-
openAIRobot,
209174
pluginRef,
210-
robotVisible,
211-
robotComponent,
212175
close,
213176
fixPanel,
214177
pluginsState,
215178
components,
216179
iconComponents,
217-
completed,
218-
doCompleted,
219180
pluginState
220181
}
221182
}
@@ -329,28 +290,10 @@ export default {
329290
svg {
330291
font-size: 22px;
331292
}
332-
333-
.chatgpt-icon {
334-
width: 18px;
335-
height: 18px;
336-
}
337293
}
338294
}
339295
}
340296
341-
.robot-dialog {
342-
position: fixed;
343-
width: 700px;
344-
z-index: 5;
345-
right: 40px;
346-
bottom: 40px;
347-
background-color: var(--ti-lowcode-common-component-bg);
348-
box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.15);
349-
border: 1px solid #dbdbdb;
350-
padding: 10px 20px 30px;
351-
border-radius: 12px;
352-
}
353-
354297
:deep(.panel-svg) {
355298
font-size: 22px;
356299
}

packages/layout/src/Main.vue

Lines changed: 14 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<tiny-config-provider :design="designSmbConfig">
2+
<component :is="configProvider" :design="configProviderDesign">
33
<div id="tiny-engine">
44
<design-toolbars :toolbars="registry.toolbars"></design-toolbars>
55
<div class="tiny-engine-main">
@@ -22,44 +22,24 @@
2222
</div>
2323
</div>
2424
</div>
25-
</tiny-config-provider>
25+
</component>
2626
</template>
2727

2828
<script>
29-
import { reactive, watch, onUnmounted } from 'vue'
30-
import { ConfigProvider as TinyConfigProvider } from '@opentiny/vue'
31-
import designSmbConfig from '@opentiny/vue-design-smb'
32-
import {
33-
useResource,
34-
useLayout,
35-
useEditorInfo,
36-
useModal,
37-
useApp,
38-
useNotify,
39-
useCanvas
40-
} from '@opentiny/tiny-engine-meta-register'
29+
import { reactive } from 'vue'
30+
import { useLayout, getMergeRegistry } from '@opentiny/tiny-engine-meta-register'
4131
import AppManage from '@opentiny/tiny-engine-plugin-page'
42-
import { isVsCodeEnv } from '@opentiny/tiny-engine-common/js/environments'
4332
import DesignToolbars from './DesignToolbars.vue'
4433
import DesignPlugins from './DesignPlugins.vue'
4534
import DesignSettings from './DesignSettings.vue'
46-
import blockPlugin from '@opentiny/tiny-engine-plugin-block'
47-
import materials from '@opentiny/tiny-engine-plugin-materials'
48-
import { useBroadcastChannel } from '@vueuse/core'
49-
import { constants } from '@opentiny/tiny-engine-utils'
50-
51-
const { message } = useModal()
52-
const { requestInitBlocks } = blockPlugin.api
53-
const { fetchGroups } = materials.apis
54-
const { BROADCAST_CHANNEL } = constants
35+
import meta from '../meta'
5536
5637
export default {
5738
name: 'TinyLowCode',
5839
components: {
5940
DesignToolbars,
6041
DesignPlugins,
61-
DesignSettings,
62-
TinyConfigProvider
42+
DesignSettings
6343
},
6444
provide() {
6545
return {
@@ -72,31 +52,17 @@ export default {
7252
}
7353
},
7454
setup() {
55+
const layoutRegistry = getMergeRegistry(meta.type)
56+
const configProvider = layoutRegistry.options.configProvider
57+
const configProviderDesign = layoutRegistry.options.configProviderDesign
58+
7559
const state = reactive({
76-
globalClass: '',
77-
rightWidth: '',
78-
leftWidfth: '',
79-
preNode: AppManage,
80-
jsClose: null
60+
preNode: AppManage
8161
})
8262
8363
const { layoutState } = useLayout()
8464
const { plugins } = layoutState
8565
86-
// 此处接收画布内部的错误和警告提示
87-
const { data } = useBroadcastChannel({ name: BROADCAST_CHANNEL.Notify })
88-
89-
watch(data, (options) => useNotify(options))
90-
91-
watch(
92-
() => state.jsClose,
93-
() => {
94-
if (state.preNode) {
95-
plugins.render = state.preNode.id
96-
}
97-
}
98-
)
99-
10066
const toggleNav = ({ item, navLists }) => {
10167
if (navLists) state.preNode = navLists
10268
@@ -105,51 +71,13 @@ export default {
10571
plugins.render = plugins.render === item.id ? null : item.id
10672
}
10773
108-
useEditorInfo().getUserInfo()
109-
110-
watch(
111-
useCanvas().isCanvasApiReady,
112-
(ready) => {
113-
if (ready) {
114-
useResource().fetchResource()
115-
}
116-
},
117-
{
118-
immediate: true
119-
}
120-
)
121-
122-
const handlePopStateEvent = () => {
123-
useResource().handlePopStateEvent()
124-
}
125-
126-
window.addEventListener('popstate', handlePopStateEvent)
127-
128-
if (isVsCodeEnv) {
129-
const appId = useApp().appInfoState.selectedId
130-
fetchGroups(appId)
131-
.then((groups) => {
132-
const blocks = []
133-
groups.forEach((group) => {
134-
blocks.push(...group.blocks)
135-
})
136-
requestInitBlocks(blocks)
137-
})
138-
.catch((error) => {
139-
message({ message: error.message, status: 'error' })
140-
})
141-
}
142-
143-
onUnmounted(() => {
144-
window.removeEventListener('popstate', handlePopStateEvent)
145-
})
146-
14774
return {
75+
configProvider,
76+
configProviderDesign,
14877
state,
14978
plugins,
15079
toggleNav,
151-
layoutState,
152-
designSmbConfig
80+
layoutState
15381
}
15482
}
15583
}

0 commit comments

Comments
 (0)