Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat router page embedded view toggle #1010

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions designer-demo/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Breadcrumb,
Fullscreen,
Lang,
ViewSetting,
Logo,
Lock,
Media,
Expand Down Expand Up @@ -70,7 +71,8 @@ export default {
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang']
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
]
}
}
Expand All @@ -96,7 +98,8 @@ export default {
GenerateCode,
Save,
Fullscreen,
Lang
Lang,
ViewSetting
],
plugins: [Materials, Tree, Page, Block, Datasource, Bridge, I18n, Script, State, Schema, Help, Robot],
dsls: [{ id: 'engine.dsls.dslvue' }],
Expand Down
2 changes: 2 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@opentiny/tiny-engine-toolbar-breadcrumb": ["packages/toolbars/breadcrumb/index"],
"@opentiny/tiny-engine-toolbar-fullscreen": ["packages/toolbars/fullscreen/index"],
"@opentiny/tiny-engine-toolbar-lang": ["packages/toolbars/lang/index"],
"@opentiny/tiny-engine-toolbar-view-setting": ["packages/toolbars/view-setting/index"],
"@opentiny/tiny-engine-toolbar-layout": ["packages/toolbars/layout/index"],
"@opentiny/tiny-engine-toolbar-lock": ["packages/toolbars/lock/index"],
"@opentiny/tiny-engine-toolbar-logo": ["packages/toolbars/logo/index"],
Expand Down Expand Up @@ -51,6 +52,7 @@
"@opentiny/tiny-engine-toolbar-breadcrumb/*": ["packages/toolbars/breadcrumb/*"],
"@opentiny/tiny-engine-toolbar-fullscreen/*": ["packages/toolbars/fullscreen/*"],
"@opentiny/tiny-engine-toolbar-lang/*": ["packages/toolbars/lang/*"],
"@opentiny/tiny-engine-toolbar-view-setting/*": ["packages/toolbars/view-setting/*"],
"@opentiny/tiny-engine-toolbar-layout/*": ["packages/toolbars/layout/*"],
"@opentiny/tiny-engine-toolbar-lock/*": ["packages/toolbars/lock/*"],
"@opentiny/tiny-engine-toolbar-logo/*": ["packages/toolbars/logo/*"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const getDevAlias = (useSourceAlias) => {
'@opentiny/tiny-engine-toolbar-breadcrumb': path.resolve(basePath, 'packages/toolbars/breadcrumb/index.js'),
'@opentiny/tiny-engine-toolbar-fullscreen': path.resolve(basePath, 'packages/toolbars/fullscreen/index.js'),
'@opentiny/tiny-engine-toolbar-lang': path.resolve(basePath, 'packages/toolbars/lang/index.js'),
'@opentiny/tiny-engine-toolbar-view-setting': path.resolve(basePath, 'packages/toolbars/view-setting/index.js'),
'@opentiny/tiny-engine-toolbar-layout': path.resolve(basePath, 'packages/toolbars/layout/index.js'),
'@opentiny/tiny-engine-toolbar-lock': path.resolve(basePath, 'packages/toolbars/lock/index.js'),
'@opentiny/tiny-engine-toolbar-logo': path.resolve(basePath, 'packages/toolbars/logo/index.js'),
Expand Down
14 changes: 13 additions & 1 deletion packages/canvas/DesignCanvas/src/DesignCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
:canvas-src-doc="canvasSrcDoc"
@remove="removeNode"
@selected="nodeSelected"
></component>
>
</component>
</template>
<template #footer>
<component :is="CanvasBreadcrumb" :data="footData"></component>
Expand Down Expand Up @@ -209,6 +210,17 @@ export default {
}
})()

// TODO: 待挪到 getBaseInfo
const postUrlChanged = () => {
usePage().postLocationHistoryChanged(Object.fromEntries(new URLSearchParams(window.location.search)))
}
onMounted(() => {
window.addEventListener('popstate', postUrlChanged)
})
onUnmounted(() => {
window.removeEventListener('popstate', postUrlChanged)
})

return {
removeNode,
canvasSrc,
Expand Down
50 changes: 41 additions & 9 deletions packages/canvas/render/src/RenderMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
*/

import { provide, watch, defineComponent, PropType, ref, inject, onUnmounted, h, Ref } from 'vue'
import { getDesignMode, setDesignMode, setController, useCustomRenderer, getController } from './canvas-function'
import {
getDesignMode,
setDesignMode,
setController,
useCustomRenderer,
getController,
useRouterViewSetting,
useLocale
} from './canvas-function'
import { removeBlockCompsCache, setConfigure } from './material-function'
import { useUtils, useBridge, useDataSourceMap, useGlobalState } from './application-function'
import { IPageSchema, useContext, usePageContext, useSchema } from './page-block-function'
Expand Down Expand Up @@ -79,6 +87,9 @@ const throttleUpdateSchema = useThrottleFn(
true
)

const pageRenderer = getRenderer()
const { routerViewSetting } = useRouterViewSetting()

export default defineComponent({
props: {
entry: {
Expand Down Expand Up @@ -120,23 +131,39 @@ export default defineComponent({
pageContext.pageId = props.pageId || pageIdFromPath
pageContext.active = props.active || !pageIdFromPath
pageContext.setCssScopeId(props.cssScopeId || (props.entry ? null : `data-te-page-${pageContext.pageId}`))

if (props.entry) {
provide('page-ancestors', pageAncestors)
getPageAncestors(pageContext.pageId).then((value) => {
pageAncestors.value = value
})
const updatePageAncestor = () => {
if (routerViewSetting.viewMode === 'standalone') {
pageAncestors.value = []
return
}
getPageAncestors(pageContext.pageId).then((value) => {
pageAncestors.value = value
})
}
updatePageAncestor()

const cancel = getController().addHistoryDataChangedCallback(() => {
const pageIdFromPath = getController().getBaseInfo().pageId
pageContext.pageId = props.pageId || pageIdFromPath
pageContext.active = props.active || !pageIdFromPath
getPageAncestors(pageContext.pageId).then((value) => {
pageAncestors.value = value
})
updatePageAncestor()
})
onUnmounted(() => {
cancel()
})

watch(
() => routerViewSetting.viewMode,
() => {
updatePageAncestor()
}
)

useLocale()

window.host.subscribe({
topic: 'schemaChange',
subscriber: 'canvasRenderer',
Expand Down Expand Up @@ -245,11 +272,16 @@ export default defineComponent({
)
}

const renderer = getRenderer()
return () =>
pageAncestors.value === null
? h(CanvasEmpty, { placeholderText: '页面分析加载中' })
: renderer(schema, refreshKey, props.entry, pageContext.active, !!pageContext.pageId)
: pageRenderer(
schema,
refreshKey,
props.entry,
pageContext.active,
!!pageContext.pageId && pageAncestors.value.length
)
}
})

Expand Down
1 change: 1 addition & 0 deletions packages/canvas/render/src/canvas-function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './design-mode'
export * from './global-notify'
export * from './custom-renderer'
export * from './locale'
export * from './router-view-setting'
39 changes: 39 additions & 0 deletions packages/canvas/render/src/canvas-function/router-view-setting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { reactive, watch } from 'vue'
import { useBroadcastChannel } from '@vueuse/core'
import { constants } from '@opentiny/tiny-engine-utils'

const { BROADCAST_CHANNEL, CANVAS_ROUTER_VIEW_SETTING_VIEW_MODE_KEY } = constants

export enum ViewMode {
EMBEDDED = 'embedded',
STANDALONE = 'standalone'
}
export interface IRouterViewSetting {
viewMode: ViewMode
}

function getCacheValue() {
const value = localStorage.getItem(CANVAS_ROUTER_VIEW_SETTING_VIEW_MODE_KEY)
if (!(Object.values(ViewMode) as string[]).includes(value)) {
return ViewMode.EMBEDDED
}
return value as ViewMode
}

export function useRouterViewSetting() {
const routerViewSetting = reactive<IRouterViewSetting>({
viewMode: getCacheValue()
})

const { data } = useBroadcastChannel<IRouterViewSetting, IRouterViewSetting>({
name: BROADCAST_CHANNEL.CanvasRouterViewSetting
})

watch(data, () => {
routerViewSetting.viewMode = data.value.viewMode
})

return {
routerViewSetting
}
}
Comment on lines +23 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding cleanup for broadcast channel subscription.

The implementation looks good, but the broadcast channel subscription should be cleaned up when the component is unmounted.

 export function useRouterViewSetting() {
   const routerViewSetting = reactive<IRouterViewSetting>({
     viewMode: getCacheValue()
   })

   const { data, cleanup } = useBroadcastChannel<IRouterViewSetting, IRouterViewSetting>({
     name: BROADCAST_CHANNEL.CanvasRouterViewSetting
   })

   watch(data, () => {
     routerViewSetting.viewMode = data.value.viewMode
   })

   return {
     routerViewSetting,
+    cleanup
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function useRouterViewSetting() {
const routerViewSetting = reactive<IRouterViewSetting>({
viewMode: getCacheValue()
})
const { data } = useBroadcastChannel<IRouterViewSetting, IRouterViewSetting>({
name: BROADCAST_CHANNEL.CanvasRouterViewSetting
})
watch(data, () => {
routerViewSetting.viewMode = data.value.viewMode
})
return {
routerViewSetting
}
}
export function useRouterViewSetting() {
const routerViewSetting = reactive<IRouterViewSetting>({
viewMode: getCacheValue()
})
const { data, cleanup } = useBroadcastChannel<IRouterViewSetting, IRouterViewSetting>({
name: BROADCAST_CHANNEL.CanvasRouterViewSetting
})
watch(data, () => {
routerViewSetting.viewMode = data.value.viewMode
})
return {
routerViewSetting,
cleanup
}
}

1 change: 1 addition & 0 deletions packages/design-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { init } from './src/init'
export { default as Breadcrumb, BreadcrumbService } from '@opentiny/tiny-engine-toolbar-breadcrumb'
export { default as Fullscreen } from '@opentiny/tiny-engine-toolbar-fullscreen'
export { default as Lang } from '@opentiny/tiny-engine-toolbar-lang'
export { default as ViewSetting } from '@opentiny/tiny-engine-toolbar-view-setting'
export { default as Logo } from '@opentiny/tiny-engine-toolbar-logo'
export { default as Lock } from '@opentiny/tiny-engine-toolbar-lock'
export { default as Media } from '@opentiny/tiny-engine-toolbar-media'
Expand Down
1 change: 1 addition & 0 deletions packages/design-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@opentiny/tiny-engine-toolbar-fullscreen": "workspace:*",
"@opentiny/tiny-engine-toolbar-generate-code": "workspace:*",
"@opentiny/tiny-engine-toolbar-lang": "workspace:*",
"@opentiny/tiny-engine-toolbar-view-setting": "workspace:*",
"@opentiny/tiny-engine-toolbar-layout": "workspace:*",
"@opentiny/tiny-engine-toolbar-lock": "workspace:*",
"@opentiny/tiny-engine-toolbar-logo": "workspace:*",
Expand Down
7 changes: 5 additions & 2 deletions packages/engine-cli/template/designer/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Breadcrumb,
Fullscreen,
Lang,
ViewSetting,
Logo,
Lock,
Media,
Expand Down Expand Up @@ -70,7 +71,8 @@ export default {
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang']
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
]
}
}
Expand All @@ -96,7 +98,8 @@ export default {
GenerateCode,
Save,
Fullscreen,
Lang
Lang,
ViewSetting
],
plugins: [Materials, Tree, Page, Block, Datasource, Bridge, I18n, Script, State, Schema, Help, Robot],
dsls: [{ id: 'engine.dsls.dslvue' }],
Expand Down
3 changes: 2 additions & 1 deletion packages/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export default {
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang']
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
]
}
},
Expand Down
19 changes: 19 additions & 0 deletions packages/toolbars/view-setting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/

import entry from './src/Main.vue'
import metaData from './meta'

export default {
...metaData,
entry
}
12 changes: 12 additions & 0 deletions packages/toolbars/view-setting/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
id: 'engine.toolbars.viewSetting',
type: 'toolbars',
title: 'viewSetting',
options: {
icon: {
default: 'routerview'
},
renderType: 'icon',
collapsed: true
}
}
42 changes: 42 additions & 0 deletions packages/toolbars/view-setting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@opentiny/tiny-engine-toolbar-view-setting",
"version": "2.1.0",
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "vite build"
},
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/opentiny/tiny-engine",
"directory": "packages/toolbars/view-setting"
},
"bugs": {
"url": "https://github.com/opentiny/tiny-engine/issues"
},
"author": "OpenTiny Team",
"license": "MIT",
"homepage": "https://opentiny.design/tiny-engine",
"dependencies": {
"@opentiny/tiny-engine-common": "workspace:*",
"@opentiny/tiny-engine-utils": "workspace:*",
"@vueuse/core": "^9.6.0"
},
"devDependencies": {
"@opentiny/tiny-engine-vite-plugin-meta-comments": "workspace:*",
"@vitejs/plugin-vue": "^5.1.2",
"@vitejs/plugin-vue-jsx": "^4.0.1",
"vite": "^5.4.2"
},
"peerDependencies": {
"@opentiny/vue": "^3.14.0",
"vue": "^3.4.15"
}
}
Loading