Skip to content

Commit

Permalink
fixed: match-media can`t work in HX3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Fasttian authored and zhetengbiji committed Dec 23, 2020
1 parent e44961b commit b413924
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/core/service/api/ui/create-media-query-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import createCallbacks from 'uni-helpers/callbacks'
import {
getCurrentPageVm
} from '../../platform'

import {
checkInWindows
} from 'uni-helpers/windows'
const createMediaQueryObserverCallbacks = createCallbacks('requestMediaQueryObserver')

class ServiceMediaQueryObserver {
Expand All @@ -24,13 +26,13 @@ class ServiceMediaQueryObserver {
reqId: this.reqId,
component: this.component,
options: this.options
})
}, checkInWindows(this.component) ? this.component : this.pageId)
}

disconnect () {
UniServiceJSBridge.publishHandler('destroyMediaQueryObserver', {
reqId: this.reqId
})
}, checkInWindows(this.component) ? this.component : this.pageId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export function requestMediaQueryObserver ({
reqId,
options
}, pageId) {
let pageVm
if (pageId._isVue) {
pageVm = pageId
} else {
const pages = getCurrentPages() // 跨平台时,View 层也应该实现该方法,举例 App 上,View 层的 getCurrentPages 返回长度为1的当前页面数组
const page = pages.find(page => page.$page.id === pageId)
if (!page) {
throw new Error(`Not Found:Page[${pageId}]`)
}
pageVm = page.$vm
}

// 创建一个媒体查询对象
const mediaQueryObserver = mediaQueryObservers[reqId] = window.matchMedia(handleMediaQueryStr(options))

Expand All @@ -42,7 +54,7 @@ export function requestMediaQueryObserver ({
UniViewJSBridge.publishHandler('onRequestMediaQueryObserver', {
reqId,
res: e.matches
})
}, pageVm.$page.id)
}

listener(mediaQueryObserver) // 监听前执行一次媒体查询
Expand Down

0 comments on commit b413924

Please sign in to comment.