Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yangkaifeng
uni-app
提交
7f45e824
U
uni-app
项目概览
yangkaifeng
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7f45e824
编写于
11月 01, 2022
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(h5): darkmode
上级
90f5c6ba
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
181 addition
and
16 deletion
+181
-16
packages/shims-node.d.ts
packages/shims-node.d.ts
+1
-0
packages/uni-cli-shared/src/env/define.ts
packages/uni-cli-shared/src/env/define.ts
+5
-1
packages/uni-cli-shared/src/json/manifest.ts
packages/uni-cli-shared/src/json/manifest.ts
+8
-0
packages/uni-cli-shared/src/json/pages.ts
packages/uni-cli-shared/src/json/pages.ts
+8
-5
packages/uni-cli-shared/src/postcss/plugins/uniapp.ts
packages/uni-cli-shared/src/postcss/plugins/uniapp.ts
+43
-1
packages/uni-h5-vite/lib/api.json
packages/uni-h5-vite/lib/api.json
+1
-0
packages/uni-h5/src/framework/components/layout/tabBar.tsx
packages/uni-h5/src/framework/components/layout/tabBar.tsx
+21
-2
packages/uni-h5/src/framework/components/page/pageHead.tsx
packages/uni-h5/src/framework/components/page/pageHead.tsx
+14
-5
packages/uni-h5/src/framework/setup/index.ts
packages/uni-h5/src/framework/setup/index.ts
+17
-0
packages/uni-h5/src/helpers/theme.ts
packages/uni-h5/src/helpers/theme.ts
+27
-0
packages/uni-h5/src/service/api/base/getBrowserInfo.ts
packages/uni-h5/src/service/api/base/getBrowserInfo.ts
+13
-1
packages/uni-h5/src/service/api/device/getSystemInfoSync.ts
packages/uni-h5/src/service/api/device/getSystemInfoSync.ts
+1
-1
packages/uni-h5/src/service/api/device/theme.ts
packages/uni-h5/src/service/api/device/theme.ts
+11
-0
packages/uni-h5/src/service/api/index.ts
packages/uni-h5/src/service/api/index.ts
+1
-0
packages/uni-h5/style/framework/base.css
packages/uni-h5/style/framework/base.css
+6
-0
packages/vite-plugin-uni/src/configResolved/env.ts
packages/vite-plugin-uni/src/configResolved/env.ts
+4
-0
未找到文件。
packages/shims-node.d.ts
浏览文件 @
7f45e824
...
...
@@ -35,5 +35,6 @@ declare namespace NodeJS {
UNI_NVUE_APP_STYLES
:
string
UNI_APP_CHANGED_FILES
:
string
UNI_APP_CHANGED_PAGES
:
string
VUE_APP_DARK_MODE
:
'
true
'
|
'
false
'
}
}
packages/uni-cli-shared/src/env/define.ts
浏览文件 @
7f45e824
import
{
runByHBuilderX
}
from
'
../hbx/env
'
import
{
parseManifestJsonOnce
}
from
'
../json
'
import
{
parseManifestJsonOnce
,
getPlatformManifestJsonOnce
}
from
'
../json
'
export
function
initDefine
(
stringifyBoolean
:
boolean
=
false
)
{
const
manifestJson
=
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
)
const
platformManifestJson
=
getPlatformManifestJsonOnce
()
const
isRunByHBuilderX
=
runByHBuilderX
()
const
isDebug
=
!!
manifestJson
.
debug
...
...
@@ -49,6 +50,9 @@ export function initDefine(stringifyBoolean: boolean = false) {
'
process.env.VUE_APP_PLATFORM
'
:
JSON
.
stringify
(
process
.
env
.
UNI_PLATFORM
||
''
),
'
process.env.VUE_APP_DARK_MODE
'
:
JSON
.
stringify
(
platformManifestJson
.
darkmode
||
false
),
}
}
...
...
packages/uni-cli-shared/src/json/manifest.ts
浏览文件 @
7f45e824
...
...
@@ -124,3 +124,11 @@ export function isEnableTreeShaking(manifestJson: Record<string, any>) {
export
function
getDevServerOptions
(
manifestJson
:
Record
<
string
,
any
>
)
{
return
extend
({},
manifestJson
.
h5
?.
devServer
)
}
export
function
getPlatformManifestJsonOnce
()
{
const
platform
=
process
.
env
.
UNI_PLATFORM
===
'
app-plus
'
?
'
app
'
:
process
.
env
.
UNI_PLATFORM
return
!
process
.
env
.
UNI_INPUT_DIR
?
{}
:
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
)[
platform
]
||
{}
}
packages/uni-cli-shared/src/json/pages.ts
浏览文件 @
7f45e824
...
...
@@ -13,7 +13,7 @@ import { isVueSfcFile } from '../vue/utils'
import
{
parseVueRequest
}
from
'
../vite
'
import
{
EXTNAME_VUE_RE
,
TEXT_STYLE
}
from
'
../constants
'
import
{
initTheme
}
from
'
./theme
'
import
{
parse
ManifestJsonOnce
}
from
'
./manifest
'
import
{
getPlatform
ManifestJsonOnce
}
from
'
./manifest
'
const
pagesCacheSet
:
Set
<
string
>
=
new
Set
()
...
...
@@ -131,9 +131,12 @@ export function normalizePagesJson(
pagesCacheSet
.
clear
()
pagesJson
.
pages
.
forEach
((
page
)
=>
pagesCacheSet
.
add
(
page
.
path
))
return
process
.
env
.
UNI_PLATFORM
===
'
app
'
||
!
process
.
env
.
UNI_INPUT_DIR
?
pagesJson
:
initTheme
(
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
),
pagesJson
)
const
manifestJsonPlatform
=
getPlatformManifestJsonOnce
()
if
(
!
manifestJsonPlatform
.
darkmode
)
{
pagesJson
=
initTheme
(
manifestJsonPlatform
,
pagesJson
)
}
return
pagesJson
}
export
function
validatePages
(
pagesJson
:
Record
<
string
,
any
>
,
jsonStr
:
string
)
{
...
...
@@ -342,7 +345,7 @@ function normalizeNavigationBar(
}
const
parsedNavigationBar
=
initTheme
(
parseManifestJsonOnce
(
process
.
env
.
UNI_INPUT_DIR
),
getPlatformManifestJsonOnce
(
),
navigationBar
)
...
...
packages/uni-cli-shared/src/postcss/plugins/uniapp.ts
浏览文件 @
7f45e824
import
{
extend
}
from
'
@vue/shared
'
import
type
{
Rule
,
Declaration
,
Plugin
}
from
'
postcss
'
import
type
{
Rule
,
Declaration
,
Plugin
,
Root
}
from
'
postcss
'
import
postcss
from
'
postcss
'
import
selectorParser
from
'
postcss-selector-parser
'
import
{
createRpx2Unit
,
defaultRpx2Unit
,
isBuiltInComponent
,
COMPONENT_SELECTOR_PREFIX
,
normalizeStyles
,
}
from
'
@dcloudio/uni-shared
'
import
{
parsePagesJsonOnce
,
normalizeThemeConfigOnce
,
getPlatformManifestJsonOnce
,
}
from
'
../../json
'
export
interface
UniAppCssProcessorOptions
{
unit
?:
string
// 目标单位,默认rem
unitRatio
?:
number
// 单位转换比例,默认10/320
...
...
@@ -83,6 +91,37 @@ function walkDecls(rpx2unit: ReturnType<typeof createRpx2Unit>) {
}
}
function
darkmodeAtRule
(
root
:
Root
,
platform
:
UniApp
.
PLATFORM
)
{
const
pageJson
=
parsePagesJsonOnce
(
process
.
env
.
UNI_PLATFORM
,
platform
)
const
filePath
=
root
.
source
?.
input
.
file
||
''
if
(
process
.
env
.
VUE_APP_DARK_MODE
===
'
true
'
&&
filePath
.
indexOf
(
'
App.vue
'
)
!==
-
1
)
{
const
pageBGC
=
(
pageJson
.
globalStyle
||
{}).
backgroundColor
||
''
if
(
pageBGC
.
indexOf
(
'
@
'
)
===
0
)
{
;[
'
dark
'
,
'
light
'
].
forEach
((
theme
)
=>
{
const
{
backgroundColor
}
=
normalizeStyles
(
{
backgroundColor
:
pageBGC
},
normalizeThemeConfigOnce
(
getPlatformManifestJsonOnce
()),
theme
as
UniApp
.
ThemeMode
)
if
(
backgroundColor
!==
'
undefined
'
)
{
const
mediaRoot
=
postcss
.
parse
(
`
@media (prefers-color-scheme:
${
theme
}
) {
body,
uni-page-body {
background-color:
${
backgroundColor
}
;
}
}
`
)
root
.
nodes
=
[...
mediaRoot
.
nodes
,
...
root
.
nodes
]
}
})
}
}
}
const
baiduTags
:
Record
<
string
,
string
>
=
{
navigator
:
'
nav
'
,
}
...
...
@@ -132,6 +171,9 @@ const uniapp = (opts?: UniAppCssProcessorOptions) => {
OnceExit
(
root
)
{
root
.
walkDecls
(
walkDecls
(
rpx2unit
))
const
rewriteTag
=
transforms
[
platform
]
if
([
'
h5
'
,
'
app
'
].
includes
(
platform
))
{
darkmodeAtRule
(
root
,
platform
)
}
if
(
rewriteTag
)
{
root
.
walkRules
(
walkRules
({
...
...
packages/uni-h5-vite/lib/api.json
浏览文件 @
7f45e824
...
...
@@ -107,6 +107,7 @@
"onSocketMessage"
,
"onSocketOpen"
,
"onTabBarMidButtonTap"
,
"onThemeChange"
,
"onUnhandledRejection"
,
"onUserCaptureScreen"
,
"onWindowResize"
,
...
...
packages/uni-h5/src/framework/components/layout/tabBar.tsx
浏览文件 @
7f45e824
import
{
watch
,
watchEffect
,
computed
,
ref
,
Ref
,
onMounted
}
from
'
vue
'
import
{
watch
,
watchEffect
,
computed
,
ref
,
Ref
,
onMounted
,
reactive
,
}
from
'
vue
'
import
{
RouteLocationNormalizedLoaded
,
useRoute
}
from
'
vue-router
'
import
{
invokeHook
,
updatePageCssVar
}
from
'
@dcloudio/uni-core
'
import
{
...
...
@@ -12,6 +20,7 @@ import { useTabBar } from '../../setup/state'
import
{
cssBackdropFilter
}
from
'
../../../service/api/base/canIUse
'
import
{
loadFontFace
}
from
'
../../../service/api/ui/loadFontFace
'
import
{
normalizeWindowBottom
}
from
'
../../../helpers/cssVar
'
import
{
parseTheme
,
onThemeChange
}
from
'
../../../helpers/theme
'
const
UNI_TABBAR_ICON_FONT
=
'
UniTabbarIconFont
'
...
...
@@ -19,12 +28,22 @@ export default /*#__PURE__*/ defineSystemComponent({
name
:
'
TabBar
'
,
setup
()
{
const
visibleList
=
ref
<
UniApp
.
TabBarItemOptions
[]
>
([])
const
tabBar
=
useTabBar
()
!
const
_tabBar
=
useTabBar
()
!
const
tabBar
=
reactive
(
parseTheme
(
_tabBar
))
useVisibleList
(
tabBar
,
visibleList
)
useTabBarCssVar
(
tabBar
)
const
onSwitchTab
=
useSwitchTab
(
useRoute
(),
tabBar
,
visibleList
)
const
{
style
,
borderStyle
,
placeholderStyle
}
=
useTabBarStyle
(
tabBar
)
onThemeChange
(()
=>
{
const
tabBarStyle
=
parseTheme
(
_tabBar
)
tabBar
.
backgroundColor
=
tabBarStyle
.
backgroundColor
tabBar
.
borderStyle
=
tabBarStyle
.
borderStyle
tabBar
.
color
=
tabBarStyle
.
color
tabBar
.
selectedColor
=
tabBarStyle
.
selectedColor
tabBar
.
blurEffect
=
tabBarStyle
.
blurEffect
})
onMounted
(()
=>
{
if
(
tabBar
.
iconfontSrc
)
{
loadFontFace
({
...
...
packages/uni-h5/src/framework/components/page/pageHead.tsx
浏览文件 @
7f45e824
import
{
computed
,
onBeforeMount
,
ref
}
from
'
vue
'
import
{
computed
,
onBeforeMount
,
ref
,
reactive
}
from
'
vue
'
import
{
extend
,
isArray
}
from
'
@vue/shared
'
import
{
defineSystemComponent
,
Input
}
from
'
@dcloudio/uni-components
'
import
{
getRealPath
}
from
'
@dcloudio/uni-platform
'
...
...
@@ -22,6 +22,7 @@ import {
usePageHeadTransparent
,
usePageHeadTransparentBackgroundColor
,
}
from
'
./transparent
'
import
{
parseTheme
,
onThemeChange
}
from
'
../../../helpers/theme
'
const
ICON_PATHS
=
{
none
:
''
,
...
...
@@ -43,10 +44,16 @@ export default /*#__PURE__*/ defineSystemComponent({
setup
()
{
const
headRef
=
ref
(
null
)
const
pageMeta
=
usePageMeta
()
const
navigationBar
=
pageMeta
.
navigationBar
const
navigationBar
=
reactive
(
parseTheme
(
pageMeta
.
navigationBar
))
// UniServiceJSBridge.emit('onNavigationBarChange', navigationBar.titleText)
const
{
clazz
,
style
}
=
usePageHead
(
navigationBar
)
onThemeChange
(()
=>
{
const
_navigationBar
=
parseTheme
(
pageMeta
.
navigationBar
)
navigationBar
.
backgroundColor
=
_navigationBar
.
backgroundColor
navigationBar
.
titleColor
=
_navigationBar
.
titleColor
})
const
buttons
=
(
__UNI_FEATURE_NAVIGATIONBAR_BUTTONS__
&&
usePageHeadButtons
(
pageMeta
))
as
PageHeadButtons
...
...
@@ -61,7 +68,7 @@ export default /*#__PURE__*/ defineSystemComponent({
return
()
=>
{
// 单页面无需back按钮
const
backButtonTsx
=
__UNI_FEATURE_PAGES__
?
createBackButtonTsx
(
pageMeta
)
?
createBackButtonTsx
(
navigationBar
,
pageMeta
.
isQuit
)
:
null
const
leftButtonsTsx
=
__UNI_FEATURE_NAVIGATIONBAR_BUTTONS__
?
createButtonsTsx
(
buttons
.
left
)
...
...
@@ -95,8 +102,10 @@ export default /*#__PURE__*/ defineSystemComponent({
},
})
function
createBackButtonTsx
(
pageMeta
:
UniApp
.
PageRouteMeta
)
{
const
{
navigationBar
,
isQuit
}
=
pageMeta
function
createBackButtonTsx
(
navigationBar
:
UniApp
.
PageNavigationBar
,
isQuit
?:
Boolean
)
{
if
(
!
isQuit
)
{
return
(
<
div
class
=
"uni-page-head-btn"
onClick
=
{
onPageHeadBackButton
}
>
...
...
packages/uni-h5/src/framework/setup/index.ts
浏览文件 @
7f45e824
...
...
@@ -21,6 +21,7 @@ import {
ON_RESIZE
,
ON_WEB_INVOKE_APP_SERVICE
,
WEB_INVOKE_APPSERVICE
,
ON_THEME_CHANGE
,
}
from
'
@dcloudio/uni-shared
'
import
{
injectAppHooks
}
from
'
@dcloudio/uni-api
'
import
{
subscribeViewMethod
,
unsubscribeViewMethod
}
from
'
@dcloudio/uni-core
'
...
...
@@ -181,6 +182,7 @@ export function setupApp(comp: any) {
)
window
.
addEventListener
(
'
message
'
,
onMessage
)
document
.
addEventListener
(
'
visibilitychange
'
,
onVisibilityChange
)
onThemeChange
()
})
return
route
.
query
},
...
...
@@ -233,3 +235,18 @@ function onVisibilityChange() {
emit
(
ON_APP_ENTER_BACKGROUND
)
}
}
function
onThemeChange
()
{
let
mediaQueryList
:
MediaQueryList
|
null
=
null
try
{
mediaQueryList
=
window
.
matchMedia
(
'
(prefers-color-scheme: dark)
'
)
}
catch
(
error
)
{}
if
(
mediaQueryList
)
{
mediaQueryList
.
addEventListener
(
'
change
'
,
(
e
)
=>
{
UniServiceJSBridge
.
emit
(
ON_THEME_CHANGE
,
{
theme
:
e
.
matches
?
'
dark
'
:
'
light
'
,
})
})
}
}
packages/uni-h5/src/helpers/theme.ts
0 → 100644
浏览文件 @
7f45e824
import
{
getTheme
}
from
'
../service/api/base/getBrowserInfo
'
import
{
normalizeStyles
,
ON_THEME_CHANGE
}
from
'
@dcloudio/uni-shared
'
export
function
onThemeChange
(
callback
:
(
res
:
{
theme
:
UniApp
.
ThemeMode
})
=>
void
)
{
if
(
__uniConfig
.
darkmode
)
{
UniServiceJSBridge
.
on
(
ON_THEME_CHANGE
,
callback
as
UniApp
.
CallbackFunction
)
}
}
export
function
offThemeChange
(
callback
:
UniApp
.
CallbackFunction
)
{
UniServiceJSBridge
.
off
(
ON_THEME_CHANGE
,
callback
)
}
export
function
parseTheme
<
T
extends
Object
>
(
pageStyle
:
T
):
T
{
let
parsedStyle
=
{}
as
T
if
(
__uniConfig
.
darkmode
)
{
parsedStyle
=
normalizeStyles
(
pageStyle
,
__uniConfig
.
themeConfig
,
getTheme
()
)
}
return
__uniConfig
.
darkmode
?
parsedStyle
:
pageStyle
}
packages/uni-h5/src/service/api/base/getBrowserInfo.ts
浏览文件 @
7f45e824
...
...
@@ -34,6 +34,18 @@ function IEVersion() {
}
}
export
function
getTheme
()
{
try
{
return
(
window
.
matchMedia
(
'
(prefers-color-scheme: light)
'
).
matches
?
'
light
'
:
'
dark
'
)
as
UniApp
.
ThemeMode
}
catch
(
error
)
{
return
'
light
'
}
}
export
function
getBrowserInfo
()
{
let
osname
let
osversion
=
'
0
'
...
...
@@ -205,6 +217,6 @@ export function getBrowserInfo() {
ua
,
osname
,
osversion
,
theme
:
undefined
,
theme
:
getTheme
()
,
}
}
packages/uni-h5/src/service/api/device/getSystemInfoSync.ts
浏览文件 @
7f45e824
...
...
@@ -122,7 +122,7 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
delete
(
systemInfo
as
any
).
screenTop
delete
(
systemInfo
as
any
).
enableDebug
delete
(
systemInfo
as
any
).
theme
if
(
!
__uniConfig
.
darkmode
)
delete
(
systemInfo
as
any
).
theme
return
sortObject
(
systemInfo
)
}
...
...
packages/uni-h5/src/service/api/device/theme.ts
0 → 100644
浏览文件 @
7f45e824
import
{
ON_THEME_CHANGE
}
from
'
@dcloudio/uni-shared
'
import
{
defineOnApi
}
from
'
@dcloudio/uni-api
'
export
const
onThemeChange
=
defineOnApi
<
typeof
uni
.
onThemeChange
>
(
ON_THEME_CHANGE
,
()
=>
{
UniServiceJSBridge
.
on
(
ON_THEME_CHANGE
,
(
res
)
=>
{
UniServiceJSBridge
.
invokeOnCallback
(
ON_THEME_CHANGE
,
res
)
})
}
)
packages/uni-h5/src/service/api/index.ts
浏览文件 @
7f45e824
...
...
@@ -22,6 +22,7 @@ export * from './device/compass'
export
*
from
'
./device/vibrate
'
export
*
from
'
./device/clipboard
'
export
*
from
'
./device/getWindowInfo
'
export
*
from
'
./device/theme
'
export
*
from
'
./storage/storage
'
...
...
packages/uni-h5/style/framework/base.css
浏览文件 @
7f45e824
...
...
@@ -11,6 +11,12 @@ body {
height
:
100%
;
}
@media
(
prefers-color-scheme
:
dark
)
{
body
{
color-scheme
:
dark
;
}
}
body
{
overflow-x
:
hidden
;
font-size
:
16px
;
...
...
packages/vite-plugin-uni/src/configResolved/env.ts
浏览文件 @
7f45e824
...
...
@@ -2,6 +2,8 @@ import fs from 'fs'
import
path
from
'
path
'
import
{
ResolvedConfig
}
from
'
vite
'
import
{
getPlatformManifestJsonOnce
}
from
'
@dcloudio/uni-cli-shared
'
export
function
initEnv
(
config
:
ResolvedConfig
)
{
if
(
!
process
.
env
.
UNI_PLATFORM
)
{
process
.
env
.
UNI_PLATFORM
=
'
h5
'
...
...
@@ -15,6 +17,8 @@ export function initEnv(config: ResolvedConfig) {
if
(
!
process
.
env
.
UNI_OUTPUT_DIR
)
{
process
.
env
.
UNI_OUTPUT_DIR
=
path
.
resolve
(
config
.
root
,
config
.
build
.
outDir
)
}
process
.
env
.
VUE_APP_DARK_MODE
=
getPlatformManifestJsonOnce
().
darkmode
||
false
process
.
env
.
BROWSERSLIST_CONFIG
=
[
path
.
resolve
(
process
.
env
.
UNI_INPUT_DIR
,
'
.browserslistrc
'
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录