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(plugin-compiler-web & runtime-web): 转 web 时兼容 tabbar 的微信使用场景 #191

Merged
merged 2 commits into from
Apr 26, 2024
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
58 changes: 43 additions & 15 deletions packages/plugin-compiler-web/src/plugins/generateJSXEntryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
WebCompilerUserConfig,
WEB_RUNTIMES
} from '../constants'
import { getValueByOrder } from '../utils'

// Web 的运行时配置,主要用于指定路由和 tabBar 等
interface WebRuntimeConfig {
Expand Down Expand Up @@ -290,26 +291,53 @@ export class GenerateJSXEntryPlugin implements Plugin {

// 设置底部导航栏
if (appJson.tabBar) {
// 修复 items 中的路径问题
if (appJson.tabBar.items) {
;(appJson.tabBar as WebRuntimeConfig['tabBar']).items.map((item) => {
if (item.activeIcon || item.selectedIconPath) {
item.activeIcon = addLeadingSlash(
item.activeIcon || item.selectedIconPath
)
}
if (item.icon || item.iconPath) {
item.icon = addLeadingSlash(item.icon || item.iconPath)
const initializeTabItem = (list) => {
return list.map((item) => {
const resItem = {}

const withLeadingSlashItem = {
activeIcon: getValueByOrder(item, [
'activeIcon',
'selectedIconPath'
]),
icon: getValueByOrder(item, ['icon', 'iconPath']),
pagePath: getValueByOrder(item, ['pagePath'])
}
if (item.pagePath) {
item.pagePath = addLeadingSlash(item.pagePath)
}
if (item.name || item.text) {
item.name = item.name || item.text
const commonItem = {
name: getValueByOrder(item, ['name', 'text'])
}

Object.keys(withLeadingSlashItem).map((key) => {
const value = withLeadingSlashItem[key]
if (value) resItem[key] = addLeadingSlash(value)
})

Object.keys(commonItem).map((key) => {
const value = commonItem[key]
if (value) resItem[key] = value
})

return resItem
})
}

// 默认文字颜色
const textColor = getValueByOrder(appJson.tabBar, [
'textColor',
'color'
])
if (textColor) appJson.tabBar.textColor = textColor

// 修复 items 中的路径问题
if (appJson.tabBar.items) {
appJson.tabBar.items = initializeTabItem(appJson.tabBar.items)
} else if (appJson.tabBar.list) {
// 兼容微信转 web 使用场景
const items = appJson.tabBar.list
delete appJson.tabBar.list
appJson.tabBar.items = initializeTabItem(items)
}

webRuntimeConfig.tabBar = appJson.tabBar
}

Expand Down
16 changes: 16 additions & 0 deletions packages/plugin-compiler-web/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ export function getRelativePath(path) {
export const isObject = (param) => {
return Object.prototype.toString.call(param) === '[object Object]'
}

export const getValueByOrder = (obj, props) => {
if (!isObject(obj)) return
const { length } = props
let result

for (let i = 0; i < length; i++) {
const prop = props[i]
if (obj[prop]) {
result = obj[prop]
break
}
}

return result
}
4 changes: 3 additions & 1 deletion packages/runtime-web/src/components/src/base/tabbar-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ export default class TabbarItem extends BaseElement {
<div
style=${styleMap({ display: 'inline-block', position: 'relative' })}
>
<img src="${this.icon}" alt="" class="tiga-tabbar__icon" />
${this.icon
? html`<img src="${this.icon}" alt="" class="tiga-tabbar__icon" />`
: ''}
${this.badgeText
? html` <span class="tiga-badge" style=${styleMap(this.badgeStyle)}>
${this.badgeText}
Expand Down