@mpxjs/api-proxy 2.7.41 → 2.7.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/api-proxy",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.44",
|
|
4
4
|
"description": "convert miniprogram API at each end",
|
|
5
5
|
"module": "src/index.js",
|
|
6
6
|
"types": "@types/index.d.ts",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"axios": "^0.21.1"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "306aee23bc55c7f1af58b8de0c6e6394370c88d4"
|
|
43
43
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { nextTick } from '../next-tick'
|
|
2
|
+
|
|
3
|
+
let isInit = true
|
|
4
|
+
|
|
5
|
+
class WebIntersectionObserver {
|
|
6
|
+
constructor (_component, options) {
|
|
7
|
+
this._component = _component
|
|
8
|
+
this._options = options || {}
|
|
9
|
+
this._relativeInfo = []
|
|
10
|
+
this._callback = null
|
|
11
|
+
this._observer = null
|
|
12
|
+
this._root = null
|
|
13
|
+
this._rootMargin = ''
|
|
14
|
+
this._disconnected = false
|
|
15
|
+
this._minThreshold = this.getMinThreshold()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
initObserver () {
|
|
19
|
+
if (this._observer) {
|
|
20
|
+
this._observer = null
|
|
21
|
+
}
|
|
22
|
+
this._disconnected = false
|
|
23
|
+
// eslint-disable-next-line no-undef
|
|
24
|
+
return new IntersectionObserver((entries, observer) => {
|
|
25
|
+
const initialRatio = this._options.initialRatio || 0
|
|
26
|
+
entries.forEach(entry => {
|
|
27
|
+
if (!isInit || (isInit && (entry.intersectionRatio !== initialRatio && (this._minThreshold <= entry.intersectionRatio)))) {
|
|
28
|
+
Object.defineProperties(entry, {
|
|
29
|
+
id: {
|
|
30
|
+
value: entry.target.getAttribute('id') || '',
|
|
31
|
+
writable: false,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true
|
|
34
|
+
},
|
|
35
|
+
dataset: {
|
|
36
|
+
value: entry.target.dataset || {},
|
|
37
|
+
writable: false,
|
|
38
|
+
enumerable: true,
|
|
39
|
+
configurable: true
|
|
40
|
+
},
|
|
41
|
+
relativeRect: {
|
|
42
|
+
value: entry.rootBounds || {},
|
|
43
|
+
writable: false,
|
|
44
|
+
enumerable: true,
|
|
45
|
+
configurable: true
|
|
46
|
+
},
|
|
47
|
+
time: {
|
|
48
|
+
value: new Date().valueOf(),
|
|
49
|
+
writable: false,
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
this._callback && this._callback(entry)
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
isInit = false
|
|
58
|
+
}, {
|
|
59
|
+
root: this._root || null,
|
|
60
|
+
rootMargin: this._rootMargin,
|
|
61
|
+
threshold: this._options.thresholds || [0]
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
observe (targetSelector, callback) {
|
|
66
|
+
nextTick(async () => {
|
|
67
|
+
if (!targetSelector) {
|
|
68
|
+
const res = { errMsg: 'observe:targetSelector can not be empty' }
|
|
69
|
+
return Promise.reject(res)
|
|
70
|
+
}
|
|
71
|
+
this._observer = await this.initObserver()
|
|
72
|
+
this._callback = callback
|
|
73
|
+
let targetElement = []
|
|
74
|
+
if (this._options.observeAll) {
|
|
75
|
+
targetElement = [...document.querySelectorAll(targetSelector)]
|
|
76
|
+
} else {
|
|
77
|
+
targetElement = [document.querySelector(targetSelector)]
|
|
78
|
+
}
|
|
79
|
+
targetElement.forEach((element) => {
|
|
80
|
+
this._observer && this._observer.observe(element)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
relativeTo (selector, margins) {
|
|
86
|
+
nextTick(() => {
|
|
87
|
+
const marginsTemp = margins || {}
|
|
88
|
+
const { left = 0, right = 0, top = 0, bottom = 0 } = marginsTemp
|
|
89
|
+
this._root = document.querySelector(selector)
|
|
90
|
+
this._rootMargin = `${top}px ${right}px ${bottom}px ${left}px`
|
|
91
|
+
this._relativeInfo.push({ selector, margins })
|
|
92
|
+
})
|
|
93
|
+
return this
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
relativeToViewport (margins) {
|
|
97
|
+
nextTick(() => {
|
|
98
|
+
const marginsTemp = margins || {}
|
|
99
|
+
const { left = 0, right = 0, top = 0, bottom = 0 } = marginsTemp
|
|
100
|
+
this._root = document.querySelector('html')
|
|
101
|
+
const viewportWidth = window.innerWidth || document.documentElement.clientWidth
|
|
102
|
+
const viewportHeight = window.innerHeight || document.documentElement.clientHeight
|
|
103
|
+
const rootWidth = this._root.offsetWidth || 0
|
|
104
|
+
const rootHeight = this._root.offsetHeight || 0
|
|
105
|
+
if (rootHeight >= viewportHeight) {
|
|
106
|
+
this._rootMargin = `${top}px ${viewportWidth - rootWidth + right}px ${viewportHeight - rootHeight + bottom}px ${left}px`
|
|
107
|
+
} else {
|
|
108
|
+
this._rootMargin = `${top}px ${right}px ${bottom}px ${left}px`
|
|
109
|
+
}
|
|
110
|
+
this._relativeInfo.push({ selector: null, margins })
|
|
111
|
+
})
|
|
112
|
+
return this
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
disconnect () {
|
|
116
|
+
this._disconnected = true
|
|
117
|
+
this._observer.disconnect()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getMinThreshold () {
|
|
121
|
+
const thresholds = this._options.thresholds || [0]
|
|
122
|
+
const thresholdsSortArr = thresholds.sort((a, b) => a - b)
|
|
123
|
+
return thresholdsSortArr[0] || 0
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export default WebIntersectionObserver
|