@tarojs/plugin-platform-harmony-ets 3.7.0-alpha.27
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/LICENSE +160 -0
- package/dist/apis/apis.ts +15 -0
- package/dist/apis/base/index.ts +5 -0
- package/dist/apis/base/system.ts +77 -0
- package/dist/apis/device/accelerometer.ts +76 -0
- package/dist/apis/device/battery.ts +24 -0
- package/dist/apis/device/clipboard.ts +70 -0
- package/dist/apis/device/index.ts +9 -0
- package/dist/apis/device/keyboard.ts +62 -0
- package/dist/apis/device/memory.ts +4 -0
- package/dist/apis/device/network.ts +72 -0
- package/dist/apis/device/phone.ts +43 -0
- package/dist/apis/device/screen.ts +79 -0
- package/dist/apis/device/vibrate.ts +24 -0
- package/dist/apis/files/index.ts +135 -0
- package/dist/apis/files/manager.ts +941 -0
- package/dist/apis/framework/index.ts +32 -0
- package/dist/apis/index.ts +124 -0
- package/dist/apis/location/index.ts +133 -0
- package/dist/apis/media/common.ts +74 -0
- package/dist/apis/media/image.ts +122 -0
- package/dist/apis/media/index.ts +3 -0
- package/dist/apis/media/video.ts +86 -0
- package/dist/apis/navigate/index.ts +8 -0
- package/dist/apis/network/downloadFile.ts +113 -0
- package/dist/apis/network/index.ts +4 -0
- package/dist/apis/network/request.ts +113 -0
- package/dist/apis/network/uploadFile.ts +158 -0
- package/dist/apis/network/webSocket.ts +130 -0
- package/dist/apis/open-api/index.ts +7 -0
- package/dist/apis/open-api/user-info/index.ts +85 -0
- package/dist/apis/page/index.ts +92 -0
- package/dist/apis/route/index.ts +138 -0
- package/dist/apis/storage/index.ts +133 -0
- package/dist/apis/tabbar/index.ts +147 -0
- package/dist/apis/ui/animation.ts +222 -0
- package/dist/apis/ui/background.ts +35 -0
- package/dist/apis/ui/index.ts +14 -0
- package/dist/apis/ui/interaction/index.ts +195 -0
- package/dist/apis/ui/navigation-bar/index.ts +33 -0
- package/dist/apis/ui/pull-down-refresh.ts +41 -0
- package/dist/apis/utils/handler.ts +116 -0
- package/dist/apis/utils/index.ts +85 -0
- package/dist/apis/utils/types.ts +12 -0
- package/dist/apis/utils/validate.ts +87 -0
- package/dist/apis/wxml/IntersectionObserver.ts +119 -0
- package/dist/apis/wxml/index.ts +12 -0
- package/dist/apis/wxml/nodesRef.ts +56 -0
- package/dist/apis/wxml/selectorQuery.ts +287 -0
- package/dist/components/components-react.js +51 -0
- package/dist/components/components-react.js.map +1 -0
- package/dist/components-harmony/button/index.css +109 -0
- package/dist/components-harmony/button/index.hml +31 -0
- package/dist/components-harmony/button/index.js +105 -0
- package/dist/components-harmony/camera/index.hml +6 -0
- package/dist/components-harmony/camera/index.js +12 -0
- package/dist/components-harmony/checkbox/index.hml +11 -0
- package/dist/components-harmony/checkbox/index.js +60 -0
- package/dist/components-harmony/checkbox-group/index.hml +3 -0
- package/dist/components-harmony/checkbox-group/index.js +22 -0
- package/dist/components-harmony/cover-image/index.css +4 -0
- package/dist/components-harmony/cover-image/index.hml +9 -0
- package/dist/components-harmony/cover-image/index.js +15 -0
- package/dist/components-harmony/form/index.hml +10 -0
- package/dist/components-harmony/form/index.js +11 -0
- package/dist/components-harmony/icon/index.css +27 -0
- package/dist/components-harmony/icon/index.hml +54 -0
- package/dist/components-harmony/icon/index.js +17 -0
- package/dist/components-harmony/image/index.css +5 -0
- package/dist/components-harmony/image/index.hml +11 -0
- package/dist/components-harmony/image/index.js +28 -0
- package/dist/components-harmony/input/index.hml +21 -0
- package/dist/components-harmony/input/index.js +98 -0
- package/dist/components-harmony/label/index.hml +3 -0
- package/dist/components-harmony/label/index.js +6 -0
- package/dist/components-harmony/navbar/index.css +56 -0
- package/dist/components-harmony/navbar/index.hml +9 -0
- package/dist/components-harmony/navbar/index.js +30 -0
- package/dist/components-harmony/navigator/index.css +8 -0
- package/dist/components-harmony/navigator/index.hml +9 -0
- package/dist/components-harmony/navigator/index.js +104 -0
- package/dist/components-harmony/picker/index.hml +25 -0
- package/dist/components-harmony/picker/index.js +132 -0
- package/dist/components-harmony/picker-view/index.hml +23 -0
- package/dist/components-harmony/picker-view/index.js +161 -0
- package/dist/components-harmony/picker-view-column/index.hml +3 -0
- package/dist/components-harmony/picker-view-column/index.js +3 -0
- package/dist/components-harmony/progress/index.css +4 -0
- package/dist/components-harmony/progress/index.hml +28 -0
- package/dist/components-harmony/progress/index.js +23 -0
- package/dist/components-harmony/radio/index.hml +11 -0
- package/dist/components-harmony/radio/index.js +41 -0
- package/dist/components-harmony/radio-group/index.hml +3 -0
- package/dist/components-harmony/radio-group/index.js +11 -0
- package/dist/components-harmony/rich-text/index.hml +5 -0
- package/dist/components-harmony/rich-text/index.js +16 -0
- package/dist/components-harmony/scroll-view/index.css +5 -0
- package/dist/components-harmony/scroll-view/index.hml +12 -0
- package/dist/components-harmony/scroll-view/index.js +65 -0
- package/dist/components-harmony/slider/index.hml +16 -0
- package/dist/components-harmony/slider/index.js +53 -0
- package/dist/components-harmony/swiper/index.css +3 -0
- package/dist/components-harmony/swiper/index.hml +14 -0
- package/dist/components-harmony/swiper/index.js +19 -0
- package/dist/components-harmony/switch/index.hml +7 -0
- package/dist/components-harmony/switch/index.js +16 -0
- package/dist/components-harmony/tabbar/index.css +7 -0
- package/dist/components-harmony/tabbar/index.hml +26 -0
- package/dist/components-harmony/tabbar/index.js +42 -0
- package/dist/components-harmony/textarea/index.hml +24 -0
- package/dist/components-harmony/textarea/index.js +92 -0
- package/dist/components-harmony/utils/index.js +41 -0
- package/dist/components-harmony/video/index.hml +7 -0
- package/dist/components-harmony/video/index.js +73 -0
- package/dist/components-harmony/web-view/index.hml +8 -0
- package/dist/components-harmony/web-view/index.js +22 -0
- package/dist/components-harmony-ets/base.ets +63 -0
- package/dist/components-harmony-ets/button.ets +119 -0
- package/dist/components-harmony-ets/checkbox.ets +219 -0
- package/dist/components-harmony-ets/element.ets +223 -0
- package/dist/components-harmony-ets/form.ets +149 -0
- package/dist/components-harmony-ets/icon.ets +84 -0
- package/dist/components-harmony-ets/image.ets +85 -0
- package/dist/components-harmony-ets/index.ts +49 -0
- package/dist/components-harmony-ets/innerHtml.ets +79 -0
- package/dist/components-harmony-ets/input.ets +195 -0
- package/dist/components-harmony-ets/label.ets +111 -0
- package/dist/components-harmony-ets/picker.ets +370 -0
- package/dist/components-harmony-ets/radio.ets +225 -0
- package/dist/components-harmony-ets/richText.ets +97 -0
- package/dist/components-harmony-ets/scrollView.ets +199 -0
- package/dist/components-harmony-ets/slider.ets +159 -0
- package/dist/components-harmony-ets/swiper.ets +204 -0
- package/dist/components-harmony-ets/switch.ets +113 -0
- package/dist/components-harmony-ets/text.ets +97 -0
- package/dist/components-harmony-ets/textArea.ets +169 -0
- package/dist/components-harmony-ets/utils/AttributeManager.ets +250 -0
- package/dist/components-harmony-ets/utils/DynamicCenter.ts +48 -0
- package/dist/components-harmony-ets/utils/constant/event.ets +25 -0
- package/dist/components-harmony-ets/utils/constant/icon.ts +19 -0
- package/dist/components-harmony-ets/utils/constant/style.ets +88 -0
- package/dist/components-harmony-ets/utils/events.ts +24 -0
- package/dist/components-harmony-ets/utils/flexManager.ets +107 -0
- package/dist/components-harmony-ets/utils/helper.ets +99 -0
- package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +103 -0
- package/dist/components-harmony-ets/utils/htmlParser/index.ts +56 -0
- package/dist/components-harmony-ets/utils/index.ts +83 -0
- package/dist/components-harmony-ets/utils/styles.ets +91 -0
- package/dist/components-harmony-ets/video.ets +161 -0
- package/dist/components-harmony-ets/view.ets +79 -0
- package/dist/index.js +1183 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime-ets/bom/document.ts +38 -0
- package/dist/runtime-ets/bom/getComputedStyle.ts +5 -0
- package/dist/runtime-ets/bom/navigator.ts +21 -0
- package/dist/runtime-ets/bom/raf.ts +37 -0
- package/dist/runtime-ets/bom/window.ts +44 -0
- package/dist/runtime-ets/constant.ts +29 -0
- package/dist/runtime-ets/current.ts +16 -0
- package/dist/runtime-ets/dom/class-list.ts +117 -0
- package/dist/runtime-ets/dom/comment.ts +10 -0
- package/dist/runtime-ets/dom/cssStyleDeclaration.ts +105 -0
- package/dist/runtime-ets/dom/document.ts +108 -0
- package/dist/runtime-ets/dom/element.ts +457 -0
- package/dist/runtime-ets/dom/event.ts +164 -0
- package/dist/runtime-ets/dom/eventTarget.ts +98 -0
- package/dist/runtime-ets/dom/node.ts +215 -0
- package/dist/runtime-ets/dom/stylesheet/index.ts +687 -0
- package/dist/runtime-ets/dom/stylesheet/type.ts +136 -0
- package/dist/runtime-ets/dom/stylesheet/util.ts +134 -0
- package/dist/runtime-ets/dom/text.ts +19 -0
- package/dist/runtime-ets/index.ts +19 -0
- package/dist/runtime-ets/interface/event.ts +9 -0
- package/dist/runtime-ets/interface/index.ts +1 -0
- package/dist/runtime-ets/utils/bind.ts +35 -0
- package/dist/runtime-ets/utils/index.ts +135 -0
- package/dist/runtime-ets/utils/info.ts +128 -0
- package/dist/runtime-framework/react/app.ts +256 -0
- package/dist/runtime-framework/react/connect.ts +24 -0
- package/dist/runtime-framework/react/constant.ts +5 -0
- package/dist/runtime-framework/react/hooks.ts +101 -0
- package/dist/runtime-framework/react/index.ts +16 -0
- package/dist/runtime-framework/react/page.ts +263 -0
- package/dist/runtime-framework/react/utils/index.ts +25 -0
- package/dist/runtime-framework/react/utils/is.ts +46 -0
- package/dist/runtime-framework/solid/README.md +99 -0
- package/dist/runtime-framework/solid/app.ts +200 -0
- package/dist/runtime-framework/solid/connect.ts +11 -0
- package/dist/runtime-framework/solid/contant.ts +5 -0
- package/dist/runtime-framework/solid/hooks.ts +88 -0
- package/dist/runtime-framework/solid/index.ts +10 -0
- package/dist/runtime-framework/solid/page.ts +202 -0
- package/dist/runtime-framework/solid/reconciler/h.ts +132 -0
- package/dist/runtime-framework/solid/reconciler/index.ts +3 -0
- package/dist/runtime-framework/solid/reconciler/props.ts +151 -0
- package/dist/runtime-framework/solid/reconciler/render.ts +61 -0
- package/dist/runtime-framework/solid/reconciler/use.ts +8 -0
- package/dist/runtime-framework/solid/utils/index.ts +49 -0
- package/dist/runtime-framework/solid/utils/is.ts +46 -0
- package/dist/runtime-utils.js +4149 -0
- package/dist/runtime-utils.js.map +1 -0
- package/dist/runtime.js +4150 -0
- package/dist/runtime.js.map +1 -0
- package/dist/template/container.js +7 -0
- package/index.js +4 -0
- package/package.json +54 -0
- package/types/api.d.ts +4 -0
- package/types/component.d.ts +4 -0
- package/types/harmony.d.ts +11 -0
- package/types/runtime.d.ts +15 -0
- package/types/taro.d.ts +7 -0
- package/types/webpack-sources.d.ts +108 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { eventCenter } from '@tarojs/runtime/dist/runtime.esm'
|
|
2
|
+
|
|
3
|
+
import { ICallbackResult, MethodHandler } from './handler'
|
|
4
|
+
|
|
5
|
+
import type { FunctionType, IAsyncParams } from './types'
|
|
6
|
+
|
|
7
|
+
export * from './validate'
|
|
8
|
+
export { noop } from '@tarojs/shared'
|
|
9
|
+
|
|
10
|
+
export function object2String (obj) {
|
|
11
|
+
let str = ''
|
|
12
|
+
for (const item in obj) {
|
|
13
|
+
str = str + item + ':' + obj[item] + ' \n'
|
|
14
|
+
}
|
|
15
|
+
return str
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function temporarilyNotSupport (name: string, recommended?: string) {
|
|
19
|
+
return (option = {}, ...args) => {
|
|
20
|
+
const { success, fail, complete } = option as any
|
|
21
|
+
const handle = new MethodHandler({ name, success, fail, complete })
|
|
22
|
+
let errMsg = '暂时不支持 API'
|
|
23
|
+
if (recommended) {
|
|
24
|
+
errMsg += `, 请使用 ${recommended}`
|
|
25
|
+
}
|
|
26
|
+
eventCenter.trigger('__taroNotSupport', {
|
|
27
|
+
name,
|
|
28
|
+
args: [option, ...args],
|
|
29
|
+
type: 'method',
|
|
30
|
+
category: 'temporarily',
|
|
31
|
+
})
|
|
32
|
+
if (process.env.NODE_ENV === 'production') {
|
|
33
|
+
console.warn(errMsg)
|
|
34
|
+
return handle.success({ errMsg })
|
|
35
|
+
} else {
|
|
36
|
+
return handle.fail({ errMsg })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function permanentlyNotSupport (name = '') {
|
|
42
|
+
return (option = {}, ...args: any[]) => {
|
|
43
|
+
const { success, fail, complete } = option as any
|
|
44
|
+
const handle = new MethodHandler<ICallbackResult>({ name, success, fail, complete })
|
|
45
|
+
const errMsg = '不支持 API'
|
|
46
|
+
eventCenter.trigger('__taroNotSupport', {
|
|
47
|
+
name,
|
|
48
|
+
args: [option, ...args],
|
|
49
|
+
type: 'method',
|
|
50
|
+
category: 'permanently',
|
|
51
|
+
})
|
|
52
|
+
if (process.env.NODE_ENV === 'production') {
|
|
53
|
+
console.warn(errMsg)
|
|
54
|
+
return handle.success({ errMsg })
|
|
55
|
+
} else {
|
|
56
|
+
return handle.fail({ errMsg })
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @deprecated */
|
|
62
|
+
export function callCallbackSuccess<T extends FunctionType> (res, options?: IAsyncParams<T>) {
|
|
63
|
+
options?.success?.(res)
|
|
64
|
+
options?.complete?.(res)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @deprecated */
|
|
68
|
+
export function callCallbackFail<T extends FunctionType> (res, options?: IAsyncParams<T>) {
|
|
69
|
+
options?.fail?.(res)
|
|
70
|
+
options?.complete?.(res)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** @deprecated */
|
|
74
|
+
export function callAsyncSuccess<T extends FunctionType> (resolve, res, options?: IAsyncParams<T>) {
|
|
75
|
+
options?.success?.(res)
|
|
76
|
+
options?.complete?.(res)
|
|
77
|
+
resolve(res)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @deprecated */
|
|
81
|
+
export function callAsyncFail<T extends FunctionType> (reject, res, options?: IAsyncParams<T>) {
|
|
82
|
+
options?.fail?.(res)
|
|
83
|
+
options?.complete?.(res)
|
|
84
|
+
reject(res)
|
|
85
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type GeneralCallback = (res: any) => void
|
|
2
|
+
|
|
3
|
+
export type FunctionType = (...args: any) => any
|
|
4
|
+
|
|
5
|
+
export interface IAsyncParams<T extends FunctionType = GeneralCallback> {
|
|
6
|
+
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
|
|
7
|
+
complete?: GeneralCallback
|
|
8
|
+
/** 接口调用失败的回调函数 */
|
|
9
|
+
fail?: GeneralCallback
|
|
10
|
+
/** 接口调用成功的回调函数 */
|
|
11
|
+
success?: T
|
|
12
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { isArray, isNull, isObject, isString } from '@tarojs/shared'
|
|
2
|
+
|
|
3
|
+
type JSTypes = 'string' | 'number' | 'boolean' | 'undefined' | 'null' | 'bigint' | 'symbol' | 'array' | 'object' | 'function'
|
|
4
|
+
|
|
5
|
+
type NormalParamSchema = string[]
|
|
6
|
+
type ObjectParamSchema = Record<string, string>
|
|
7
|
+
|
|
8
|
+
type Schema = NormalParamSchema | ObjectParamSchema
|
|
9
|
+
|
|
10
|
+
interface ValidateParams {
|
|
11
|
+
<T>(name: string, params: any, schema: Schema): asserts params is T
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function shouldBeObject (target: unknown) {
|
|
15
|
+
if (target && typeof target === 'object') return { flag: true }
|
|
16
|
+
return {
|
|
17
|
+
flag: false,
|
|
18
|
+
msg: getParameterError({
|
|
19
|
+
correct: 'Object',
|
|
20
|
+
wrong: target
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface IParameterErrorParam {
|
|
26
|
+
name?: string
|
|
27
|
+
para?: string
|
|
28
|
+
correct?: string
|
|
29
|
+
wrong?: unknown
|
|
30
|
+
level?: 'warn' | 'error' | 'log' | 'info' | 'debug'
|
|
31
|
+
}
|
|
32
|
+
export function getParameterError ({ name = '', para, correct, wrong, level = 'error' }: IParameterErrorParam) {
|
|
33
|
+
const parameter = para ? `parameter.${para}` : 'parameter'
|
|
34
|
+
const errorType = upperCaseFirstLetter(wrong === null ? 'Null' : typeof wrong)
|
|
35
|
+
return `${name ? `${name}:fail ` : ''}parameter ${level}: ${parameter} should be ${correct} instead of ${errorType}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function upperCaseFirstLetter (string: string): string {
|
|
39
|
+
if (!isString(string)) return string
|
|
40
|
+
string = string.replace(/^./, match => match.toUpperCase())
|
|
41
|
+
return string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getType (param: string): JSTypes {
|
|
45
|
+
if (isNull(param)) {
|
|
46
|
+
return 'null'
|
|
47
|
+
} else if (isArray(param)) {
|
|
48
|
+
return 'array'
|
|
49
|
+
} else {
|
|
50
|
+
return typeof param
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function assertType (target: any, type: string, methodName: string, paramName?: string) {
|
|
55
|
+
const correct = upperCaseFirstLetter(type)
|
|
56
|
+
if (correct !== upperCaseFirstLetter(getType(target))) {
|
|
57
|
+
const err = getParameterError({ name: methodName, para: paramName, correct, wrong: target })
|
|
58
|
+
throw new Error(err)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 用于校验方法的参数类型
|
|
64
|
+
* @param name 方法名称
|
|
65
|
+
* @param params [array|object] 当前参数
|
|
66
|
+
* @param schema [array|object] 参数类型规范
|
|
67
|
+
* @example
|
|
68
|
+
* ```js
|
|
69
|
+
* // 例子一:
|
|
70
|
+
* validateParams(methodA, [key], [string])
|
|
71
|
+
* // 例子二:
|
|
72
|
+
* validateParams(methodB, { key: 123 }, { key: number })
|
|
73
|
+
* ```
|
|
74
|
+
* @todo 校验可选参数
|
|
75
|
+
*/
|
|
76
|
+
export const validateParams: ValidateParams = function (name, params, schema) {
|
|
77
|
+
if (isArray(schema) && isString(schema[0])) {
|
|
78
|
+
schema.forEach((correctType, index) => assertType(params[index], correctType, name, `[${index}]`))
|
|
79
|
+
} else if (isObject(schema)) {
|
|
80
|
+
const optionsSchema = schema
|
|
81
|
+
|
|
82
|
+
assertType(params, 'object', name)
|
|
83
|
+
for (const key in optionsSchema) {
|
|
84
|
+
assertType(params[key], optionsSchema[key], name, key)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import Taro from '@tarojs/api'
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import { Current, disconnectEvent, findChildNodeWithDFS, getPageScrollerOrNode, setNodeEventCallbackAndTriggerComponentUpdate, TaroElement, VISIBLE_CHANGE_EVENT_NAME } from '@tarojs/runtime'
|
|
4
|
+
|
|
5
|
+
import { temporarilyNotSupport } from '../utils'
|
|
6
|
+
|
|
7
|
+
export class IntersectionObserver implements Taro.IntersectionObserver {
|
|
8
|
+
// 自定义组件实例
|
|
9
|
+
private _component: any
|
|
10
|
+
// 监听的nodes
|
|
11
|
+
private _observerNodes: TaroElement[] | TaroElement | null
|
|
12
|
+
// 选项
|
|
13
|
+
private _options = {
|
|
14
|
+
thresholds: [0],
|
|
15
|
+
initialRatio: 0,
|
|
16
|
+
observeAll: false
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
constructor(component: any, options: Taro.createIntersectionObserver.Option = {}) {
|
|
20
|
+
const taro = (Current as any).taro
|
|
21
|
+
const page = taro.getCurrentInstance().page
|
|
22
|
+
const currentPage = getPageScrollerOrNode(page.node, page)
|
|
23
|
+
this._component = component || currentPage
|
|
24
|
+
Object.assign(this._options, options)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public disconnect (): void {
|
|
28
|
+
if (this._observerNodes) {
|
|
29
|
+
if (this._observerNodes instanceof Array) {
|
|
30
|
+
this._observerNodes.forEach((n: TaroElement & any) => {
|
|
31
|
+
disconnectEvent(n, VISIBLE_CHANGE_EVENT_NAME)
|
|
32
|
+
if (n._instance) {
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
n._instance?.nodeInfoMap?.[n._nid]?.thresholds = null
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
} else {
|
|
38
|
+
disconnectEvent(this._observerNodes, VISIBLE_CHANGE_EVENT_NAME)
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
if (this._observerNodes._instance) {
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
this._observerNodes._instance?.nodeInfoMap?.[this._observerNodes._nid]?.thresholds = null
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public observe (targetSelector: string, callback: Taro.IntersectionObserver.ObserveCallback): void {
|
|
49
|
+
const { observeAll, thresholds } = this._options
|
|
50
|
+
const node = findChildNodeWithDFS(this._component, targetSelector, observeAll)
|
|
51
|
+
this._observerNodes = node
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if (node) {
|
|
55
|
+
if (node instanceof Array) {
|
|
56
|
+
node.forEach(n => {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
n.awaitAppear.then(() => {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
n._instance?.nodeInfoMap?.[n._nid]?.thresholds = thresholds
|
|
61
|
+
|
|
62
|
+
setNodeEventCallbackAndTriggerComponentUpdate(n, VISIBLE_CHANGE_EVENT_NAME, (isVisible: boolean, currentRatio: number) => {
|
|
63
|
+
callback(this.handleResult(isVisible, currentRatio))
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
} else {
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
node.awaitAppear.then(() => {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
node._instance?.nodeInfoMap?.[node._nid]?.thresholds = thresholds
|
|
72
|
+
|
|
73
|
+
setNodeEventCallbackAndTriggerComponentUpdate(node, VISIBLE_CHANGE_EVENT_NAME, (isVisible: boolean, currentRatio: number) => {
|
|
74
|
+
callback(this.handleResult(isVisible, currentRatio))
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public relativeTo (): Taro.IntersectionObserver {
|
|
82
|
+
temporarilyNotSupport('relativeTo')()
|
|
83
|
+
return this
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public relativeToViewport (): Taro.IntersectionObserver {
|
|
87
|
+
temporarilyNotSupport('relativeToViewport')()
|
|
88
|
+
return this
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
private handleResult (isVisible: boolean, currentRatio: number): Taro.IntersectionObserver.ObserveCallbackResult {
|
|
93
|
+
const result = {
|
|
94
|
+
intersectionRatio: currentRatio,
|
|
95
|
+
// TODO 未做,等待能拿到element的info信息
|
|
96
|
+
boundingClientRect: {
|
|
97
|
+
left: 0,
|
|
98
|
+
right: 0,
|
|
99
|
+
top: 0,
|
|
100
|
+
bottom: 0,
|
|
101
|
+
height: 0,
|
|
102
|
+
width: 0
|
|
103
|
+
},
|
|
104
|
+
// TODO 未做,等待能拿到element和监听_component的info信息做运算
|
|
105
|
+
intersectionRect: {
|
|
106
|
+
left: 0,
|
|
107
|
+
right: 0,
|
|
108
|
+
top: 0,
|
|
109
|
+
bottom: 0,
|
|
110
|
+
height: 0,
|
|
111
|
+
width: 0
|
|
112
|
+
},
|
|
113
|
+
// TODO 未做,等待能拿到element和监听_component的info信息做运算
|
|
114
|
+
relativeRect: { left: 0, right: 0, top: 0, bottom: 0 },
|
|
115
|
+
time: new Date().getTime()
|
|
116
|
+
}
|
|
117
|
+
return result
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Taro from '@tarojs/taro'
|
|
2
|
+
|
|
3
|
+
import { IntersectionObserver } from './IntersectionObserver'
|
|
4
|
+
import { SelectorQuery } from './selectorQuery'
|
|
5
|
+
|
|
6
|
+
export const createSelectorQuery = () => {
|
|
7
|
+
return new SelectorQuery()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const createIntersectionObserver: typeof Taro.createIntersectionObserver = (component, options) => {
|
|
11
|
+
return new IntersectionObserver(component, options)
|
|
12
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import Taro from '@tarojs/api'
|
|
2
|
+
|
|
3
|
+
import { SelectorQuery } from './selectorQuery'
|
|
4
|
+
|
|
5
|
+
export class NodesRef implements Taro.NodesRef {
|
|
6
|
+
_component?: any
|
|
7
|
+
_selector: string
|
|
8
|
+
_selectorQuery: SelectorQuery
|
|
9
|
+
_single: boolean
|
|
10
|
+
|
|
11
|
+
constructor (selector: string, querySelectorQuery: SelectorQuery, single: boolean) {
|
|
12
|
+
this._component = querySelectorQuery._component
|
|
13
|
+
this._selector = selector
|
|
14
|
+
this._selectorQuery = querySelectorQuery
|
|
15
|
+
this._single = single
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
context (cb) {
|
|
19
|
+
const { _selector, _component, _single, _selectorQuery } = this
|
|
20
|
+
_selectorQuery._push(_selector, _component, _single, { context: !0 }, cb)
|
|
21
|
+
return _selectorQuery
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
node (cb?: Taro.NodesRef.NodeCallback) {
|
|
25
|
+
const { _selector, _component, _single, _selectorQuery } = this
|
|
26
|
+
_selectorQuery._push(_selector, _component, _single, { nodeCanvasType: !0, node: !0 }, cb)
|
|
27
|
+
return _selectorQuery
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
boundingClientRect (cb?: Taro.NodesRef.BoundingClientRectCallback) {
|
|
31
|
+
const { _selector, _component, _single, _selectorQuery } = this
|
|
32
|
+
_selectorQuery._push(_selector, _component, _single, { id: !0, dataset: !0, rect: !0, size: !0 }, cb)
|
|
33
|
+
return _selectorQuery
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
scrollOffset (cb) {
|
|
37
|
+
const { _selector, _component, _single, _selectorQuery } = this
|
|
38
|
+
_selectorQuery._push(_selector, _component, _single, { id: !0, dataset: !0, scrollOffset: !0 }, cb)
|
|
39
|
+
return _selectorQuery
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fields (fields: Taro.NodesRef.Fields, cb?: Taro.NodesRef.FieldsCallback) {
|
|
43
|
+
const { _selector, _component, _single, _selectorQuery } = this
|
|
44
|
+
const { id, dataset, rect, size, scrollOffset, properties = [], computedStyle = [] } = fields
|
|
45
|
+
_selectorQuery._push(_selector, _component, _single, {
|
|
46
|
+
id,
|
|
47
|
+
dataset,
|
|
48
|
+
rect,
|
|
49
|
+
size,
|
|
50
|
+
scrollOffset,
|
|
51
|
+
properties,
|
|
52
|
+
computedStyle
|
|
53
|
+
}, cb)
|
|
54
|
+
return _selectorQuery
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { AREA_CHANGE_EVENT_NAME, Current, getPageScrollerOrNode, setNodeEventCallbackAndTriggerComponentUpdate } from '@tarojs/runtime'
|
|
2
|
+
|
|
3
|
+
import { NodesRef } from './nodesRef'
|
|
4
|
+
|
|
5
|
+
import type Taro from '@tarojs/api'
|
|
6
|
+
|
|
7
|
+
interface ISelectorQueryQueue {
|
|
8
|
+
component: any
|
|
9
|
+
selector: string
|
|
10
|
+
single: boolean
|
|
11
|
+
fields
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type TSelectorQueryQueueCallback = (res: ISelectorQueryQueue) => void
|
|
15
|
+
|
|
16
|
+
let arr: any = []
|
|
17
|
+
|
|
18
|
+
// 深度搜索 rootDom 下的所有节点,存放在 arr 中
|
|
19
|
+
function traversalDFSDom (rootDom) {
|
|
20
|
+
if (!rootDom) return
|
|
21
|
+
arr.push(rootDom)
|
|
22
|
+
|
|
23
|
+
if (rootDom.childNodes.length === 0) return
|
|
24
|
+
for (let i = 0; i < rootDom.childNodes.length; i++) {
|
|
25
|
+
traversalDFSDom(rootDom.childNodes[i])
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 从 arr 中寻找所有符合 selector 的节点
|
|
30
|
+
function parseHandler (selector, selectAll) {
|
|
31
|
+
const domList: any = []
|
|
32
|
+
if (arr.length === 0) return null
|
|
33
|
+
|
|
34
|
+
let selectedId, clsList
|
|
35
|
+
switch (selector.charAt(0)) {
|
|
36
|
+
case '#': // id selector
|
|
37
|
+
selectedId = selector.substring(1)
|
|
38
|
+
for (let i = 0; i < arr.length; i++) {
|
|
39
|
+
if (arr[i].id === selectedId) {
|
|
40
|
+
domList.push(arr[i])
|
|
41
|
+
if (!selectAll) {
|
|
42
|
+
break
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
break
|
|
47
|
+
case '.':
|
|
48
|
+
clsList = selector.split('.').filter((item) => item !== '')
|
|
49
|
+
if (clsList.length === 0) break
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < arr.length; i++) {
|
|
52
|
+
for (let j = 0; j < clsList.length; j++) {
|
|
53
|
+
if (arr[i].className?.includes(clsList[j])) {
|
|
54
|
+
domList.push(arr[i])
|
|
55
|
+
if (!selectAll) {
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
default:
|
|
63
|
+
console.warn('unSupport selector')
|
|
64
|
+
break
|
|
65
|
+
}
|
|
66
|
+
if (selectAll) {
|
|
67
|
+
return domList
|
|
68
|
+
} else if (domList.length > 0) {
|
|
69
|
+
return [domList[0]]
|
|
70
|
+
}
|
|
71
|
+
return []
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 从 TaroNode 里找到对应的 fields 内容
|
|
75
|
+
function filter (fields, dom) {
|
|
76
|
+
if (!dom) return null
|
|
77
|
+
const {
|
|
78
|
+
id,
|
|
79
|
+
// dataset,
|
|
80
|
+
rect,
|
|
81
|
+
size,
|
|
82
|
+
scrollOffset,
|
|
83
|
+
properties = [],
|
|
84
|
+
computedStyle = [],
|
|
85
|
+
nodeCanvasType,
|
|
86
|
+
node,
|
|
87
|
+
context
|
|
88
|
+
} = fields
|
|
89
|
+
const res: any = {}
|
|
90
|
+
|
|
91
|
+
if (nodeCanvasType && node) { // Node节点获取处理
|
|
92
|
+
const typeName = dom.nodeName
|
|
93
|
+
res.node = {
|
|
94
|
+
id: dom.id,
|
|
95
|
+
$taroElement: dom
|
|
96
|
+
}
|
|
97
|
+
if (/^canvas/i.test(typeName)) {
|
|
98
|
+
// harmony todo canvas attr type
|
|
99
|
+
const canvasType = dom._attrs.type || ''
|
|
100
|
+
res.nodeCanvasType = canvasType
|
|
101
|
+
if (/^(2d|webgl)/i.test(canvasType) && dom) {
|
|
102
|
+
res.node = dom
|
|
103
|
+
} else {
|
|
104
|
+
res.node = null
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
// TODO https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html
|
|
108
|
+
// if (/^taro-scroll-view-core/i.test(tagName))
|
|
109
|
+
res.nodeCanvasType = ''
|
|
110
|
+
res.node = dom
|
|
111
|
+
}
|
|
112
|
+
return res
|
|
113
|
+
}
|
|
114
|
+
if (context) {
|
|
115
|
+
// TODO: 暂未实现获取 context
|
|
116
|
+
// const typeName = dom.type
|
|
117
|
+
// if (/^video/i.test(typeName)) {
|
|
118
|
+
// return { context: dom }
|
|
119
|
+
// } else if (/^canvas/i.test(typeName)) {
|
|
120
|
+
// const type = dom.type || '2d'
|
|
121
|
+
// // harmony todo canvas context
|
|
122
|
+
// const ctx = dom?.getContext(type)
|
|
123
|
+
// return { context: ctx }
|
|
124
|
+
// } else if (/^taro-live-player-core/i.test(typeName)) {
|
|
125
|
+
// console.error('暂时不支持通过 NodesRef.context 获取 LivePlayerContext')
|
|
126
|
+
// } else if (/^taro-editor-core/i.test(typeName)) {
|
|
127
|
+
// console.error('暂时不支持通过 NodesRef.context 获取 EditorContext')
|
|
128
|
+
// } else if (/^taro-map-core/i.test(typeName)) {
|
|
129
|
+
// console.error('暂时不支持通过 NodesRef.context 获取 MapContext')
|
|
130
|
+
// }
|
|
131
|
+
}
|
|
132
|
+
if (id) res.id = dom.id
|
|
133
|
+
|
|
134
|
+
// TODO harmony dataset
|
|
135
|
+
// if (dataset) res.dataset = Object.assign({}, dom.dataset)
|
|
136
|
+
|
|
137
|
+
if (rect || size) {
|
|
138
|
+
const id = dom?._nid
|
|
139
|
+
const { areaInfo } = dom?._instance?.nodeInfoMap?.[id] || {}
|
|
140
|
+
|
|
141
|
+
if (areaInfo) {
|
|
142
|
+
if (rect) {
|
|
143
|
+
res.top = vp2px(areaInfo.globalPosition.y)
|
|
144
|
+
res.left = vp2px(areaInfo.globalPosition.x)
|
|
145
|
+
res.right = vp2px(areaInfo.globalPosition.x + areaInfo.width)
|
|
146
|
+
res.bottom = vp2px(areaInfo.globalPosition.y + areaInfo.height)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (size) {
|
|
150
|
+
res.width = vp2px(areaInfo.width)
|
|
151
|
+
res.height = vp2px(areaInfo.height)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (scrollOffset) {
|
|
156
|
+
const scroller = dom._instance.scroller
|
|
157
|
+
|
|
158
|
+
if (scroller) {
|
|
159
|
+
const { xOffset, yOffset } = scroller.currentOffset()
|
|
160
|
+
|
|
161
|
+
res.scrollLeft = vp2px(xOffset)
|
|
162
|
+
res.scrollTop = vp2px(yOffset)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (properties.length) {
|
|
166
|
+
properties.forEach(prop => {
|
|
167
|
+
const attrs = dom._attrs
|
|
168
|
+
if (attrs[prop]) res[prop] = attrs[prop]
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
if (computedStyle.length) {
|
|
172
|
+
const styles = dom._st
|
|
173
|
+
computedStyle.forEach(key => {
|
|
174
|
+
const value = styles[key]
|
|
175
|
+
if (value) res[key] = value
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
return res
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function querySelector (selector, selectAll) {
|
|
182
|
+
if (typeof selector === 'string') {
|
|
183
|
+
return parseHandler(selector, selectAll)
|
|
184
|
+
}
|
|
185
|
+
return null
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function queryBat (queue, cb) {
|
|
189
|
+
const result: any = []
|
|
190
|
+
const taro = (Current as any).taro
|
|
191
|
+
const page = taro.getCurrentInstance().page
|
|
192
|
+
const element = getPageScrollerOrNode(page.node, page)
|
|
193
|
+
|
|
194
|
+
if (element == null) return null
|
|
195
|
+
|
|
196
|
+
arr = []
|
|
197
|
+
traversalDFSDom(element)
|
|
198
|
+
queue.forEach(item => {
|
|
199
|
+
const { selector, single, fields } = item
|
|
200
|
+
const nodeList = querySelector(selector, !single)
|
|
201
|
+
|
|
202
|
+
result.push(nodeList.map(dom => {
|
|
203
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
204
|
+
return new Promise(async resolve => {
|
|
205
|
+
await setNodeEventCallbackAndTriggerComponentUpdate(dom, AREA_CHANGE_EVENT_NAME, null, true)
|
|
206
|
+
|
|
207
|
+
resolve(filter(fields, dom))
|
|
208
|
+
})
|
|
209
|
+
}))
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
Promise.all(result.map(item => {
|
|
213
|
+
return Promise.all(item)
|
|
214
|
+
})).then(data => {
|
|
215
|
+
cb(data)
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export class SelectorQuery implements Taro.SelectorQuery {
|
|
220
|
+
_queue: ISelectorQueryQueue[]
|
|
221
|
+
_queueCb: (TSelectorQueryQueueCallback | null)[]
|
|
222
|
+
_component?: any
|
|
223
|
+
|
|
224
|
+
constructor () {
|
|
225
|
+
this._queue = []
|
|
226
|
+
this._queueCb = []
|
|
227
|
+
// this._component
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 设置选择器的选取范围
|
|
232
|
+
* @param component 指定组件
|
|
233
|
+
* @return selectQuery 返回查询对象
|
|
234
|
+
*/
|
|
235
|
+
in = (_: any) => {
|
|
236
|
+
this._component = null
|
|
237
|
+
console.warn('暂不支持 in 操作')
|
|
238
|
+
// this._component = component
|
|
239
|
+
return this
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 在当前页面下选择第一个匹配选择器selector的节点
|
|
244
|
+
* @param selector
|
|
245
|
+
* @return nodesRef 返回一个NodesRef 对象实例,可以用于获取节点信息
|
|
246
|
+
*/
|
|
247
|
+
select (selector: string) {
|
|
248
|
+
return new NodesRef(selector, this, true)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 在当前页面下选择匹配选择器selector的所有节点
|
|
253
|
+
* @param selector
|
|
254
|
+
*/
|
|
255
|
+
selectAll (selector: string) {
|
|
256
|
+
return new NodesRef(selector, this, false)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 选择显示区域。可用于获取显示区域的尺寸、滚动位置等信息
|
|
261
|
+
*/
|
|
262
|
+
selectViewport () {
|
|
263
|
+
return new NodesRef('.taro_page', this, true)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
exec (cb?: (...args: any[]) => any) {
|
|
267
|
+
queryBat(this._queue, res => {
|
|
268
|
+
const _queueCb = this._queueCb
|
|
269
|
+
res.forEach((item, index) => {
|
|
270
|
+
const cb = _queueCb[index]
|
|
271
|
+
typeof cb === 'function' && cb.call(this, item)
|
|
272
|
+
})
|
|
273
|
+
typeof cb === 'function' && cb.call(this, res)
|
|
274
|
+
})
|
|
275
|
+
return this as any
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
_push (selector, component, single, fields, callback: Taro.NodesRef.FieldsCallback | null = null) {
|
|
279
|
+
this._queue.push({
|
|
280
|
+
component,
|
|
281
|
+
selector,
|
|
282
|
+
single,
|
|
283
|
+
fields
|
|
284
|
+
})
|
|
285
|
+
this._queueCb.push(callback)
|
|
286
|
+
}
|
|
287
|
+
}
|