@mpxjs/api-proxy 2.9.0 → 2.9.6

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.9.0",
3
+ "version": "2.9.6",
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": "91ac258dbf1d0a324a74cac606e2cc10ef557823"
42
+ "gitHead": "9034343edacc95da66b6e8ddb5c2f64a4be97231"
43
43
  }
@@ -26,13 +26,46 @@ function isTabBarPage (url, router) {
26
26
  function createDom (tag, attrs = {}, children = []) {
27
27
  const dom = document.createElement(tag)
28
28
  Object.keys(attrs).forEach(k => dom.setAttribute(k, attrs[k]))
29
- children.length && children.forEach(child => dom.appendChild(child))
29
+ children.length && children.forEach(child => dom.appendChild(typeof child === 'string' ? document.createTextNode(child) : child))
30
30
  return dom
31
31
  }
32
32
 
33
+ // 在H5中,直接绑定 click 可能出现延时问题,很多点击可以关闭的组件被唤出之后,有概率立马触发点击事件,导致组件被关闭。
34
+ // 使用该方法通过 touchstart 和 touchend 模拟 click 事件,解决延时问题。
35
+ function bindTap (dom, handler) {
36
+ let startTime = 0; let x = 0; let y = 0
37
+ const touchStart = (e) => {
38
+ startTime = Date.now()
39
+ x = e.touches[0].pageX
40
+ y = e.touches[0].pageY
41
+ }
42
+ const touchEnd = (e) => {
43
+ if (Date.now() - startTime < 300 && Math.abs(e.changedTouches[0].pageX - x) < 10 && Math.abs(e.changedTouches[0].pageY - y) < 10) {
44
+ handler(e)
45
+ }
46
+ }
47
+ dom.addEventListener('touchstart', touchStart)
48
+ dom.addEventListener('touchend', touchEnd)
49
+ return () => {
50
+ dom.removeEventListener('touchstart', touchStart)
51
+ dom.removeEventListener('touchend', touchEnd)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 获取弹窗应当挂载的根节点
57
+ * @returns dom
58
+ */
59
+ function getRootElement () {
60
+ const page = getCurrentPages().slice(-1)[0]?.$el
61
+ return page || document.body
62
+ }
63
+
33
64
  export {
34
65
  webHandleSuccess,
35
66
  webHandleFail,
36
67
  createDom,
68
+ bindTap,
69
+ getRootElement,
37
70
  isTabBarPage
38
71
  }
@@ -9,12 +9,14 @@
9
9
  z-index 1000
10
10
  .__mpx_preview_tip__
11
11
  position absolute
12
- top 0
12
+ top 7px
13
13
  left 50%
14
14
  transform translateX(-50%)
15
- color white
16
- font-size 48px
17
- letter-spacing 0.3em
15
+ color #fff
16
+ font-size 18px
17
+ letter-spacing .3em
18
+ text-align center
19
+ width 100%
18
20
  .__mpx_preview_images__
19
21
  display flex
20
22
  width 100%
@@ -1,4 +1,4 @@
1
- import { ToPromise, webHandleSuccess, webHandleFail } from '../../../common/js'
1
+ import { ToPromise, webHandleSuccess, webHandleFail, createDom, bindTap, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/ActionSheet.styl'
3
3
 
4
4
  export default class ActionSheet extends ToPromise {
@@ -12,37 +12,20 @@ export default class ActionSheet extends ToPromise {
12
12
  complete: null
13
13
  }
14
14
  this.hideTimer = null
15
-
16
- const actionSheet = document.createElement('div')
17
- actionSheet.setAttribute('class', '__mpx_actionsheet__')
18
-
19
- const mask = document.createElement('div')
20
- mask.setAttribute('class', '__mpx_mask__')
21
-
22
- const box = document.createElement('div')
23
- box.setAttribute('class', '__mpx_actionsheet_box__')
24
-
25
- const list = document.createElement('div')
26
- list.setAttribute('class', '__mpx_actionsheet_list__')
27
-
28
- const cancelBtn = document.createElement('div')
29
- cancelBtn.setAttribute('class', '__mpx_actionsheet_cancel__')
30
- cancelBtn.textContent = '取消'
31
-
32
- box.appendChild(list)
33
- box.appendChild(cancelBtn)
34
- actionSheet.appendChild(mask)
35
- actionSheet.appendChild(box)
36
- document.body.appendChild(actionSheet)
37
-
38
- this.actionSheet = actionSheet
39
- this.mask = mask
40
- this.box = box
41
- this.list = list
42
- this.cancelBtn = cancelBtn
15
+ // 临时绑定事件的解绑方法数组,用于在 hide 时解绑
16
+ this.tempListeners = []
17
+
18
+ this.actionSheet = createDom('div', { class: '__mpx_actionsheet__' }, [
19
+ this.mask = createDom('div', { class: '__mpx_mask__' }),
20
+ this.box = createDom('div', { class: '__mpx_actionsheet_box__' }, [
21
+ this.list = createDom('div', { class: '__mpx_actionsheet_list__' }),
22
+ this.cancelBtn = createDom('div', { class: '__mpx_actionsheet_cancel__' }, ['取消'])
23
+ ])
24
+ ])
43
25
  }
44
26
 
45
27
  show (options) {
28
+ getRootElement().appendChild(this.actionSheet) // show 则挂载
46
29
  if (this.hideTimer) {
47
30
  clearTimeout(this.hideTimer)
48
31
  this.hideTimer = null
@@ -50,14 +33,12 @@ export default class ActionSheet extends ToPromise {
50
33
 
51
34
  const opts = Object.assign({}, this.defaultOpts, options)
52
35
 
53
- const list = document.createElement('div')
54
- list.setAttribute('class', '__mpx_actionsheet_list__')
36
+ const list = createDom('div', { class: '__mpx_actionsheet_list__' })
55
37
 
38
+ // todo 使用事件代理
56
39
  opts.itemList.forEach((item, index) => {
57
- const sheet = document.createElement('div')
58
- sheet.setAttribute('class', '__mpx_actionsheet_sheet__')
59
- sheet.textContent = item
60
- sheet.onclick = () => {
40
+ const sheet = createDom('div', { class: '__mpx_actionsheet_sheet__' }, [item])
41
+ this.tempListeners.push(bindTap(sheet, () => {
61
42
  this.hide()
62
43
  const res = {
63
44
  errMsg: 'showActionSheet:ok',
@@ -65,7 +46,7 @@ export default class ActionSheet extends ToPromise {
65
46
  }
66
47
  webHandleSuccess(res, opts.success, opts.complete)
67
48
  this.toPromiseResolve(res)
68
- }
49
+ }))
69
50
  list.appendChild(sheet)
70
51
  })
71
52
 
@@ -73,15 +54,18 @@ export default class ActionSheet extends ToPromise {
73
54
  this.list = list
74
55
  this.list.style.color = opts.itemColor
75
56
 
76
- this.cancelBtn.onclick = () => {
57
+ this.tempListeners.push(bindTap(this.cancelBtn, () => {
77
58
  this.hide()
78
59
  const err = { errMsg: 'showActionSheet:fail cancel' }
79
60
  webHandleFail(err, opts.fail, opts.complete)
80
61
  !opts.fail && this.toPromiseReject(err)
81
- }
82
-
83
- this.box.classList.add('show')
62
+ }))
63
+ // make transition next frame
84
64
  this.actionSheet.classList.add('show')
65
+ // 如果使用 requestAnimationFrame,第一次展示不会有动画效果,原因待确认,这里先使用 setTimeout
66
+ setTimeout(() => {
67
+ this.box.classList.add('show')
68
+ }, 17)
85
69
 
86
70
  return this.toPromiseInitPromise()
87
71
  }
@@ -91,10 +75,12 @@ export default class ActionSheet extends ToPromise {
91
75
  clearTimeout(this.hideTimer)
92
76
  this.hideTimer = null
93
77
  }
94
-
78
+ this.tempListeners.forEach(unbind => unbind())
79
+ this.tempListeners = []
80
+ this.box.classList.remove('show')
95
81
  this.hideTimer = setTimeout(() => {
96
- this.box.classList.remove('show')
97
82
  this.actionSheet.classList.remove('show')
98
- }, 0)
83
+ this.actionSheet.remove() // hide 则卸载
84
+ }, 300) // animation duration is 300ms
99
85
  }
100
86
  }
@@ -1,4 +1,4 @@
1
- import { ToPromise, webHandleSuccess } from '../../../common/js'
1
+ import { ToPromise, createDom, getRootElement, webHandleSuccess } from '../../../common/js'
2
2
  import '../../../common/stylus/Modal.styl'
3
3
  // import { forEach } from '@didi/mpx-fetch/src/util'
4
4
  // 汉字为两个字符,字母/数字为一个字符
@@ -28,52 +28,24 @@ export default class Modal extends ToPromise {
28
28
  fail: (...args) => {},
29
29
  complete: (...args) => {}
30
30
  }
31
- this.hideTimer = null
32
-
33
- const modal = document.createElement('div')
34
- modal.setAttribute('class', '__mpx_modal__')
35
-
36
- const mask = document.createElement('div')
37
- mask.setAttribute('class', '__mpx_mask__')
38
-
39
- const box = document.createElement('div')
40
- box.setAttribute('class', '__mpx_modal_box__')
41
-
42
- const title = document.createElement('div')
43
- title.setAttribute('class', '__mpx_modal_title__')
44
31
 
45
- const content = document.createElement('div')
46
- content.setAttribute('class', '__mpx_modal_content__')
47
-
48
- const btns = document.createElement('div')
49
- btns.setAttribute('class', '__mpx_modal_btns__')
50
-
51
- const cancelBtn = document.createElement('div')
52
- cancelBtn.setAttribute('class', '__mpx_modal_cancel__')
53
-
54
- const confirmBtn = document.createElement('div')
55
- confirmBtn.setAttribute('class', '__mpx_modal_confirm__')
56
-
57
- btns.appendChild(cancelBtn)
58
- btns.appendChild(confirmBtn)
59
- box.appendChild(title)
60
- box.appendChild(content)
61
- box.appendChild(btns)
62
- modal.appendChild(mask)
63
- modal.appendChild(box)
64
- document.body.appendChild(modal)
32
+ this.hideTimer = null
65
33
 
66
- this.modal = modal
67
- this.mask = mask
68
- this.box = box
69
- this.title = title
70
- this.content = content
71
- this.btns = btns
72
- this.cancelBtn = cancelBtn
73
- this.confirmBtn = confirmBtn
34
+ this.modal = createDom('div', { class: '__mpx_modal__' }, [
35
+ this.mask = createDom('div', { class: '__mpx_mask__' }),
36
+ this.box = createDom('div', { class: '__mpx_modal_box__' }, [
37
+ this.title = createDom('div', { class: '__mpx_modal_title__' }),
38
+ this.content = createDom('div', { class: '__mpx_modal_content__' }),
39
+ this.btns = createDom('div', { class: '__mpx_modal_btns__' }, [
40
+ this.cancelBtn = createDom('div', { class: '__mpx_modal_cancel__' }),
41
+ this.confirmBtn = createDom('div', { class: '__mpx_modal_confirm__' })
42
+ ])
43
+ ])
44
+ ])
74
45
  }
75
46
 
76
47
  show (options = {}) {
48
+ getRootElement().appendChild(this.modal)
77
49
  if (options.confirmText && _getLength(options.confirmText) > 8) {
78
50
  // eslint-disable-next-line
79
51
  return Promise.reject({errMsg: 'showModal:fail confirmText length should not larger than 4 Chinese characters'})
@@ -136,6 +108,7 @@ export default class Modal extends ToPromise {
136
108
 
137
109
  this.hideTimer = setTimeout(() => {
138
110
  this.modal.classList.remove('show')
111
+ this.modal.remove()
139
112
  }, 0)
140
113
  }
141
114
  }
@@ -1,4 +1,4 @@
1
- import { webHandleSuccess, webHandleFail, createDom, warn } from '../../../common/js'
1
+ import { webHandleSuccess, webHandleFail, createDom, warn, bindTap, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/Preview.styl'
3
3
  /**
4
4
  * Preview class for displaying images in a slideshow format.
@@ -12,7 +12,6 @@ export default class Preview {
12
12
  this.preview = createDom('div', { class: '__mpx_preview__' }, [
13
13
  this.textTip = createDom('div', { class: '__mpx_preview_tip__' })
14
14
  ])
15
- document.body.appendChild(this.preview)
16
15
  this.initEvent()
17
16
  }
18
17
 
@@ -41,9 +40,11 @@ export default class Preview {
41
40
  this.updateTextTip()
42
41
  })
43
42
  // click to close
44
- this.preview.addEventListener('click', () => {
43
+ bindTap(this.preview, () => {
44
+ this.currentIndex = 0
45
45
  this.preview.style.display = 'none'
46
46
  this.preview.querySelector('.__mpx_preview_images__').remove()
47
+ this.preview.remove()
47
48
  })
48
49
  }
49
50
 
@@ -57,6 +58,7 @@ export default class Preview {
57
58
  Object.keys(options).forEach(key => !supported.includes(key) && warn(`previewImage: 暂不支持选项 ${key} !`))
58
59
  const { urls, success, fail, complete } = options
59
60
  try {
61
+ getRootElement().appendChild(this.preview)
60
62
  this.preview.style.display = 'block'
61
63
  // create images with urls
62
64
  // append to preview
@@ -1,4 +1,4 @@
1
- import { webHandleSuccess, createDom } from '../../../common/js'
1
+ import { webHandleSuccess, createDom, getRootElement } from '../../../common/js'
2
2
  import '../../../common/stylus/Toast.styl'
3
3
  import '../../../common/stylus/Loading.styl'
4
4
 
@@ -30,11 +30,10 @@ export default class Toast {
30
30
  this.loading = createDom('div', { class: '__mpx_loading_wrapper__' }, Array.from({ length: 12 }, (_, i) => {
31
31
  return createDom('div', { class: `line${i + 1}` })
32
32
  }))
33
-
34
- document.body.appendChild(this.toast)
35
33
  }
36
34
 
37
35
  show (options, type) {
36
+ getRootElement().appendChild(this.toast) // show 则挂载
38
37
  if (this.hideTimer) {
39
38
  clearTimeout(this.hideTimer)
40
39
  this.hideTimer = null
@@ -92,7 +91,10 @@ export default class Toast {
92
91
  this.hideTimer = null
93
92
  }
94
93
 
95
- this.hideTimer = setTimeout(() => { this.toast.classList.remove('show') }, duration)
94
+ this.hideTimer = setTimeout(() => {
95
+ this.toast.classList.remove('show')
96
+ this.toast.remove() // hide 则卸载
97
+ }, duration)
96
98
  return Promise.resolve({ errMsg })
97
99
  }
98
100
  }