Skip to content

Commit

Permalink
feat(runtime-web): 支持动态改变页面配置
Browse files Browse the repository at this point in the history
  • Loading branch information
hwaphon committed Dec 20, 2023
1 parent 2c863ed commit e0d8e54
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-web/src/components/src/private/page-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
converterForPx,
defineElement,
getCurrentPageParams,
getPageConfig,
shouldEnableFor
} from '../utils'
import boolConverter from '../utils/bool-converter'
Expand Down Expand Up @@ -113,6 +114,22 @@ export default class PageHost extends LitElement implements IPageHost {
private styleElement: HTMLStyleElement
setConfig(config: IPageConfig) {
this.config = { ...this.config, ...config }
// 业务在页面打开时有覆盖页面配置的场景
const updatePageConfig = get(window.$MOR_APP_CONFIG, 'updatePageConfig')
if (updatePageConfig) {
const { path: pagePath, options: pageOptions } = getCurrentPageParams([
'path',
'options'
])
const resultConfig = getPageConfig(
updatePageConfig,
pagePath,
pageOptions
)

if (typeof resultConfig === 'object')
this.config = Object.assign(this.config, resultConfig)
}
// 强制渲染
this.requestUpdate()
// NOTE: 页面背景色
Expand Down
19 changes: 19 additions & 0 deletions packages/runtime-web/src/components/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ export function shouldEnableFor(
}
}

export function getPageConfig(
config:
| Record<string, any>
| ((
pagePath: string,
pageOptions: Record<string, any>
) => Record<string, any>),
pagePath: string,
pageOptions: Record<string, any>
) {
if (!pagePath) return
if (typeof config === 'object') return config

if (typeof config === 'function') {
const result = config(pagePath, pageOptions)
if (typeof result === 'object') return result
}
}

export const getCurrentPageParams = (keys = []): Record<string, any> => {
try {
const pageStack = getCurrentPages() || []
Expand Down

0 comments on commit e0d8e54

Please sign in to comment.