Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
84887cae
U
uni-app
项目概览
DCloud
/
uni-app
6 个月 前同步成功
通知
750
Star
38709
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
8
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
8
Issue
8
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
84887cae
编写于
7月 02, 2019
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update: uni.createIntersectionObserver 接口改为在 view 层处理
上级
edc7b710
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
109 addition
and
48 deletion
+109
-48
src/core/service/api/create-intersection-observer.js
src/core/service/api/create-intersection-observer.js
+24
-45
src/core/service/bridge/subscribe.js
src/core/service/bridge/subscribe.js
+2
-1
src/core/view/bridge/subscribe/index.js
src/core/view/bridge/subscribe/index.js
+7
-2
src/core/view/bridge/subscribe/request-component-observer.js
src/core/view/bridge/subscribe/request-component-observer.js
+76
-0
未找到文件。
src/core/service/api/create-intersection-observer.js
浏览文件 @
84887cae
import
Vue
from
'
vue
'
import
'
intersection-observer
'
import
createCallbacks
from
'
uni-helpers/callbacks
'
const
createIntersectionObserverCallbacks
=
createCallbacks
(
'
requestComponentObserver
'
)
const
defaultOptions
=
{
thresholds
:
[
0
],
...
...
@@ -8,60 +10,38 @@ const defaultOptions = {
}
class
MPIntersectionObserver
{
_intersectionObserver
_el
_options
_root
=
null
_rootMargin
=
'
0
'
constructor
(
context
,
options
)
{
this
.
_el
=
context
.
$el
this
.
_options
=
Object
.
assign
({},
defaultOptions
,
options
)
constructor
(
pageId
,
options
)
{
this
.
pageId
=
pageId
this
.
options
=
Object
.
assign
({},
defaultOptions
,
options
)
}
_makeRootMargin
(
margins
=
{})
{
this
.
_
rootMargin
=
[
'
top
'
,
'
right
'
,
'
bottom
'
,
'
left
'
].
map
(
name
=>
`
${
Number
(
margins
[
name
])
||
0
}
px`
).
join
(
'
'
)
this
.
options
.
rootMargin
=
[
'
top
'
,
'
right
'
,
'
bottom
'
,
'
left
'
].
map
(
name
=>
`
${
Number
(
margins
[
name
])
||
0
}
px`
).
join
(
'
'
)
}
relativeTo
(
selector
,
margins
)
{
this
.
_root
=
this
.
_el
.
querySelector
(
selector
)
this
.
options
.
relativeToSelector
=
selector
this
.
_makeRootMargin
(
margins
)
}
relativeToViewport
(
margins
)
{
this
.
_root
=
null
this
.
options
.
relativeToSelector
=
null
this
.
_makeRootMargin
(
margins
)
}
observe
(
selector
,
callback
)
{
if
(
typeof
callback
!==
'
function
'
)
{
return
}
let
options
=
{
root
:
this
.
_root
,
rootMargin
:
this
.
_rootMargin
,
threshold
:
this
.
_options
.
thresholds
}
let
intersectionObserver
=
this
.
_intersectionObserver
=
new
IntersectionObserver
((
entries
,
observer
)
=>
{
entries
.
forEach
(
entrie
=>
{
callback
({
intersectionRatio
:
entrie
.
intersectionRatio
,
intersectionRect
:
entrie
.
intersectionRect
,
boundingClientRect
:
entrie
.
boundingClientRect
,
relativeRect
:
entrie
.
rootBounds
,
time
:
entrie
.
time
,
dataset
:
entrie
.
target
.
dataset
,
id
:
entrie
.
target
.
id
})
})
},
options
)
if
(
this
.
_options
.
observeAll
)
{
intersectionObserver
.
USE_MUTATION_OBSERVER
=
true
Array
.
prototype
.
map
.
call
(
this
.
_el
.
querySelectorAll
(
selector
),
el
=>
{
intersectionObserver
.
observe
(
el
)
})
}
else
{
intersectionObserver
.
USE_MUTATION_OBSERVER
=
false
intersectionObserver
.
observe
(
this
.
_el
.
querySelector
(
selector
))
}
this
.
options
.
selector
=
selector
this
.
reqId
=
createIntersectionObserverCallbacks
.
push
(
callback
)
UniServiceJSBridge
.
publishHandler
(
'
requestComponentObserver
'
,
{
reqId
:
this
.
reqId
,
options
:
this
.
options
},
this
.
pageId
)
}
disconnect
()
{
this
.
_intersectionObserver
&&
this
.
_intersectionObserver
.
disconnect
()
UniServiceJSBridge
.
publishHandler
(
'
destroyComponentObserver
'
,
{
reqId
:
this
.
reqId
},
this
.
pageId
)
}
}
...
...
@@ -71,12 +51,11 @@ export function createIntersectionObserver (context, options) {
context
=
null
}
if
(
context
)
{
return
new
MPIntersectionObserver
(
context
,
options
)
return
new
MPIntersectionObserver
(
context
.
$page
.
id
,
options
)
}
const
pages
=
getCurrentPages
()
if
(
pages
.
length
)
{
context
=
pages
[
pages
.
length
-
1
]
return
new
MPIntersectionObserver
(
context
,
options
)
const
app
=
getApp
()
if
(
app
.
$route
&&
app
.
$route
.
params
.
__id__
)
{
return
new
MPIntersectionObserver
(
app
.
$route
.
params
.
__id__
,
options
)
}
else
{
UniServiceJSBridge
.
emit
(
'
onError
'
,
'
createIntersectionObserver:fail
'
)
}
...
...
src/core/service/bridge/subscribe.js
浏览文件 @
84887cae
...
...
@@ -39,6 +39,7 @@ function onRequestComponentObserver ({
if
(
callback
)
{
if
(
reqEnd
)
{
requestComponentObserverCallbacks
.
pop
(
reqId
)
return
}
callback
(
res
)
}
...
...
@@ -51,4 +52,4 @@ export default function initSubscribe (subscribe) {
subscribe
(
'
onRequestComponentInfo
'
,
onRequestComponentInfo
)
subscribe
(
'
onRequestComponentObserver
'
,
onRequestComponentObserver
)
}
}
src/core/view/bridge/subscribe/index.js
浏览文件 @
84887cae
...
...
@@ -17,6 +17,8 @@ import {
import
requestComponentInfo
from
'
./request-component-info
'
import
{
requestComponentObserver
,
destroyComponentObserver
}
from
'
./request-component-observer
'
const
passiveOptions
=
supportsPassive
?
{
passive
:
false
}
:
false
...
...
@@ -40,6 +42,9 @@ export default function initSubscribe (subscribe) {
subscribe
(
'
pageScrollTo
'
,
pageScrollTo
)
subscribe
(
'
requestComponentObserver
'
,
requestComponentObserver
)
subscribe
(
'
destroyComponentObserver
'
,
destroyComponentObserver
)
if
(
__PLATFORM__
===
'
h5
'
)
{
let
scrollListener
=
false
let
disableScrollListener
=
false
...
...
@@ -48,7 +53,7 @@ export default function initSubscribe (subscribe) {
updateCssVar
(
vm
)
})
subscribe
(
'
onPageShow
'
,
vm
=>
{
subscribe
(
'
onPageShow
'
,
vm
=>
{
const
pageVm
=
vm
.
$parent
.
$parent
if
(
vm
.
_isMounted
)
{
// 非首次 show 才 update(首次 show 的时候在 onPageLoad 中触发了)
...
...
@@ -87,4 +92,4 @@ export default function initSubscribe (subscribe) {
}
})
}
}
}
src/core/view/bridge/subscribe/request-component-observer.js
0 → 100644
浏览文件 @
84887cae
import
'
intersection-observer
'
import
{
normalizeDataset
}
from
'
uni-helpers
'
function
getRect
(
rect
)
{
return
{
bottom
:
rect
.
bottom
,
height
:
rect
.
height
,
left
:
rect
.
left
,
right
:
rect
.
right
,
top
:
rect
.
top
,
width
:
rect
.
width
}
}
const
intersectionObservers
=
{}
export
function
requestComponentObserver
({
reqId
,
options
},
pageId
)
{
const
pages
=
getCurrentPages
()
const
pageVm
=
pages
.
find
(
page
=>
page
.
$page
.
id
===
pageId
)
if
(
!
pageVm
)
{
throw
new
Error
(
`Not Found:Page[
${
pageId
}
]`
)
}
const
$el
=
pageVm
.
$el
const
root
=
options
.
relativeToSelector
?
$el
.
querySelector
(
options
.
relativeToSelector
)
:
null
let
intersectionObserver
=
intersectionObservers
[
reqId
]
=
new
IntersectionObserver
((
entries
,
observer
)
=>
{
entries
.
forEach
(
entrie
=>
{
UniViewJSBridge
.
publishHandler
(
'
onRequestComponentObserver
'
,
{
reqId
,
res
:
{
intersectionRatio
:
entrie
.
intersectionRatio
,
intersectionRect
:
getRect
(
entrie
.
intersectionRect
),
boundingClientRect
:
getRect
(
entrie
.
boundingClientRect
),
relativeRect
:
getRect
(
entrie
.
rootBounds
),
time
:
Date
.
now
(),
dataset
:
normalizeDataset
(
entrie
.
target
.
dataset
||
{}),
id
:
entrie
.
target
.
id
}
},
pageVm
.
$page
.
id
)
})
},
{
root
,
rootMargin
:
options
.
rootMargin
,
threshold
:
options
.
thresholds
})
if
(
options
.
observeAll
)
{
intersectionObserver
.
USE_MUTATION_OBSERVER
=
true
Array
.
prototype
.
map
.
call
(
$el
.
querySelectorAll
(
options
.
selector
),
el
=>
{
intersectionObserver
.
observe
(
el
)
})
}
else
{
intersectionObserver
.
USE_MUTATION_OBSERVER
=
false
intersectionObserver
.
observe
(
$el
.
querySelector
(
options
.
selector
))
}
}
export
function
destroyComponentObserver
({
reqId
})
{
const
intersectionObserver
=
intersectionObservers
[
reqId
]
if
(
intersectionObserver
)
{
intersectionObserver
.
disconnect
()
UniViewJSBridge
.
publishHandler
(
'
onRequestComponentObserver
'
,
{
reqId
,
reqEnd
:
true
})
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录