@tarojs/with-weapp 3.8.0-canary.0 → 4.0.0-beta.0

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.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * 该模块仅存放 convert 转换时用到的工具函数或变量
3
+ */
4
+
5
+ export const cacheOptions = {
6
+ cacheOptions: {},
7
+ setOptionsToCache: function (options) {
8
+ if (Object.keys(options).length !== 0) {
9
+ this.cacheOptions = options
10
+ }
11
+ },
12
+ getOptionsFromCache: function () {
13
+ return this.cacheOptions
14
+ }
15
+ }
16
+
17
+ function toCamelCase (s) {
18
+ let camel = ''
19
+ let nextCap = false
20
+ for (let i = 0; i < s.length; i++) {
21
+ if (s[i] !== '-') {
22
+ camel += nextCap ? s[i].toUpperCase() : s[i]
23
+ nextCap = false
24
+ } else {
25
+ nextCap = true
26
+ }
27
+ }
28
+ return camel
29
+ }
30
+
31
+ export const convertToArray = function (value, fn) {
32
+ if (value instanceof Array) {
33
+ return value.map(fn)
34
+ } else if (typeof value === 'number') {
35
+ return Array.from({
36
+ length: value
37
+ }, (_, index) => index).map(fn)
38
+ } else if (typeof value === 'string') {
39
+ return Array.from(value).map(fn)
40
+ } else if (typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype) {
41
+ const result = Object.keys(value).map((item) => {
42
+ return fn(value[item], item)
43
+ })
44
+ return result
45
+ }
46
+ }
47
+
48
+ export const getTarget = (target, Taro) => {
49
+ if (!target) {
50
+ return { dataset: {} }
51
+ }
52
+ if (Taro.getEnv() === Taro.ENV_TYPE.MPHARMONY || Taro.getEnv() === Taro.ENV_TYPE.WEB) {
53
+ if (target.fullDataset) {
54
+ return { dataset: target.fullDataset }
55
+ }
56
+ const fullDataset = {}
57
+ // 获取元素的所有属性
58
+ const targetAttrKeys = Object.keys(target)
59
+ // 遍历所有属性
60
+ for (let i = 0; i < targetAttrKeys.length; i++) {
61
+ if (targetAttrKeys[i].startsWith('data-')) {
62
+ fullDataset[toCamelCase(targetAttrKeys[i].replace(/^data-/, '').toLowerCase())] = target[targetAttrKeys[i]]
63
+ }
64
+ }
65
+ target.fullDataset = fullDataset
66
+ return { dataset: fullDataset }
67
+ }
68
+ return target
69
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Func, getCurrentInstance } from '@tarojs/runtime'
2
- import { ComponentLifecycle, eventCenter, nextTick } from '@tarojs/taro'
2
+ import { ComponentLifecycle, createIntersectionObserver, createMediaQueryObserver, createSelectorQuery, eventCenter, nextTick } from '@tarojs/taro'
3
3
 
4
4
  import { clone } from './clone'
5
5
  import { diff } from './diff'
@@ -10,7 +10,8 @@ type Observer = (newProps, oldProps, changePath: string) => void
10
10
 
11
11
  interface ObserverProperties {
12
12
  name: string
13
- observer: string | Observer
13
+ observers: (string | Observer)[]
14
+ // observer: string | Observer
14
15
  }
15
16
 
16
17
  interface ComponentClass<P = Record<string, any>, S = Record<string, any>> extends ComponentLifecycle<P, S> {
@@ -39,14 +40,19 @@ function defineGetter (component, key: string, getter: string) {
39
40
  if (getter === 'props') {
40
41
  return component.props
41
42
  }
42
- return {
43
- ...component.state,
44
- ...component.props
45
- }
43
+ return component.state
44
+ // return {
45
+ // ...component.state,
46
+ // ...component.props
47
+ // }
46
48
  }
47
49
  })
48
50
  }
49
51
 
52
+ function propToState (newValue, _oldValue, key: string) {
53
+ this.state[key] = newValue
54
+ }
55
+
50
56
  function isFunction (o): o is Func {
51
57
  return typeof o === 'function'
52
58
  }
@@ -63,7 +69,8 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
63
69
  ['created', []],
64
70
  ['attached', []],
65
71
  ['ready', []],
66
- ['detached', []]
72
+ ['detached', []],
73
+ ['lifetimes', []]
67
74
  ])
68
75
  const behaviorProperties = {}
69
76
  if (weappConf.behaviors?.length) {
@@ -123,20 +130,47 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
123
130
  }
124
131
 
125
132
  private initProps (props: any) {
133
+ const properties = {}
126
134
  for (const propKey in props) {
127
135
  if (props.hasOwnProperty(propKey)) {
128
136
  const propValue = props[propKey]
129
137
  // propValue 可能是 null, 构造函数, 对象
130
- if (propValue && !isFunction(propValue)) {
138
+ const observers = [propToState]
139
+ if (propValue === null || propValue === undefined){ // propValue为null、undefined情况
140
+ properties[propKey] = null
141
+ }
142
+ else if (isFunction(propValue)) { // propValue为Function,即Array、String、Boolean等情况时
143
+ if (propValue.name === 'Array') {
144
+ properties[propKey] = []
145
+ } else if (propValue.name === 'String'){
146
+ properties[propKey] = ''
147
+ } else if (propValue.name === 'Boolean'){
148
+ properties[propKey] = false
149
+ } else if (propValue.name === 'Number') {
150
+ properties[propKey] = 0
151
+ } else {
152
+ properties[propKey] = null
153
+ }
154
+ }
155
+ else if (typeof propValue === 'object') { // propValue为对象时
156
+ properties[propKey] = propValue.value
131
157
  if (propValue.observer) {
132
- this._observeProps.push({
133
- name: propKey,
134
- observer: propValue.observer
135
- })
158
+ observers.push(propValue.observer)
136
159
  }
137
160
  }
161
+ else {
162
+ properties[propKey] = null
163
+ }
164
+ this._observeProps.push({
165
+ name: propKey,
166
+ observers: observers
167
+ })
138
168
  }
139
169
  }
170
+ this.state = {
171
+ ...properties,
172
+ ...this.state
173
+ }
140
174
  }
141
175
 
142
176
  private init (options: WxOptions) {
@@ -285,6 +319,13 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
285
319
  }
286
320
  })
287
321
  break
322
+ case 'lifetimes':
323
+ list.forEach(lifetimesObject => {
324
+ for (const key in lifetimesObject) {
325
+ this.initLifeCycles(key, lifetimesObject[key])
326
+ }
327
+ })
328
+ break
288
329
  default:
289
330
  break
290
331
  }
@@ -367,19 +408,21 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
367
408
  }
368
409
 
369
410
  private triggerPropertiesObservers (prevProps, nextProps) {
370
- this._observeProps.forEach(({ name: key, observer }) => {
411
+ this._observeProps.forEach(({ name: key, observers }) => {
371
412
  const prop = prevProps?.[key]
372
413
  const nextProp = nextProps[key]
373
414
  // 小程序是深比较不同之后才 trigger observer
374
415
  if (!isEqual(prop, nextProp)) {
375
- if (typeof observer === 'string') {
376
- const ob = this[observer]
377
- if (isFunction(ob)) {
378
- ob.call(this, nextProp, prop, key)
416
+ observers.forEach((observer)=>{
417
+ if (typeof observer === 'string') {
418
+ const ob = this[observer]
419
+ if (isFunction(ob)) {
420
+ ob.call(this, nextProp, prop, key)
421
+ }
422
+ } else if (isFunction(observer)) {
423
+ observer.call(this, nextProp, prop, key)
379
424
  }
380
- } else if (isFunction(observer)) {
381
- observer.call(this, nextProp, prop, key)
382
- }
425
+ })
383
426
  }
384
427
  })
385
428
  }
@@ -563,6 +606,13 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
563
606
  const page = this.current.page
564
607
  if (page?.[method]) {
565
608
  return page[method](...args)
609
+ } else if (method === 'createSelectorQuery') {
610
+ return createSelectorQuery()
611
+ } else if (method === 'createIntersectionObserver') {
612
+ // @ts-ignore
613
+ return createIntersectionObserver(...args)
614
+ } else if (method === 'createMediaQueryObserver') {
615
+ return createMediaQueryObserver()
566
616
  } else {
567
617
  console.error(`page 下没有 ${method} 方法`)
568
618
  }
@@ -625,3 +675,5 @@ export default function withWeapp (weappConf: WxOptions, isApp = false) {
625
675
  return BaseComponent
626
676
  }
627
677
  }
678
+
679
+ export * from './convert-tools'