@tarojs/plugin-platform-harmony-ets 4.0.0-beta.13 → 4.0.0-beta.131

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.
Files changed (146) hide show
  1. package/dist/apis/base/system.ts +60 -25
  2. package/dist/apis/base/weapp/life-cycle.ts +1 -1
  3. package/dist/apis/canvas/index.ts +10 -1
  4. package/dist/apis/device/clipboard.ts +23 -8
  5. package/dist/apis/framework/index.ts +1 -1
  6. package/dist/apis/index.ts +24 -16
  7. package/dist/apis/media/image/index.ts +169 -17
  8. package/dist/apis/network/downloadFile.ts +3 -1
  9. package/dist/apis/network/uploadFile.ts +3 -1
  10. package/dist/apis/route/index.ts +1 -2
  11. package/dist/apis/storage/index.ts +135 -131
  12. package/dist/apis/ui/animation/animation.ts +71 -29
  13. package/dist/apis/ui/background.ts +3 -2
  14. package/dist/apis/ui/interaction/index.ts +58 -59
  15. package/dist/apis/ui/navigation-bar/index.ts +2 -2
  16. package/dist/apis/ui/pull-down-refresh.ts +9 -3
  17. package/dist/apis/ui/scroll/index.ts +5 -5
  18. package/dist/apis/ui/tab-bar.ts +4 -5
  19. package/dist/apis/utils/handler.ts +2 -1
  20. package/dist/apis/utils/index.ts +20 -2
  21. package/dist/apis/utils/permissions.ts +6 -0
  22. package/dist/apis/wxml/IntersectionObserver.ts +18 -10
  23. package/dist/apis/wxml/selectorQuery.ts +26 -13
  24. package/dist/components-harmony-ets/button.ets +36 -9
  25. package/dist/components-harmony-ets/canvas.ets +51 -0
  26. package/dist/components-harmony-ets/checkbox.ets +69 -60
  27. package/dist/components-harmony-ets/form.ets +33 -15
  28. package/dist/components-harmony-ets/icon.ets +16 -8
  29. package/dist/components-harmony-ets/image.ets +22 -6
  30. package/dist/components-harmony-ets/index.ets +39 -0
  31. package/dist/components-harmony-ets/innerHtml.ets +6 -5
  32. package/dist/components-harmony-ets/input.ets +63 -37
  33. package/dist/components-harmony-ets/label.ets +43 -21
  34. package/dist/components-harmony-ets/listView.ets +32 -0
  35. package/dist/components-harmony-ets/movableArea.ets +110 -51
  36. package/dist/components-harmony-ets/movableView.ets +74 -47
  37. package/dist/components-harmony-ets/navigationBar.ets +65 -0
  38. package/dist/components-harmony-ets/pageMeta.ets +94 -0
  39. package/dist/components-harmony-ets/picker.ets +36 -33
  40. package/dist/components-harmony-ets/progress.ets +31 -24
  41. package/dist/components-harmony-ets/pseudo.ets +43 -35
  42. package/dist/components-harmony-ets/radio.ets +75 -66
  43. package/dist/components-harmony-ets/richText.ets +4 -25
  44. package/dist/components-harmony-ets/scrollList.ets +108 -0
  45. package/dist/components-harmony-ets/scrollView.ets +53 -44
  46. package/dist/components-harmony-ets/slider.ets +19 -15
  47. package/dist/components-harmony-ets/stickySection.ets +42 -0
  48. package/dist/components-harmony-ets/style.ets +274 -38
  49. package/dist/components-harmony-ets/swiper.ets +42 -5
  50. package/dist/components-harmony-ets/switch.ets +36 -32
  51. package/dist/components-harmony-ets/{index.ts → tag.ts} +6 -0
  52. package/dist/components-harmony-ets/text.ets +102 -46
  53. package/dist/components-harmony-ets/textArea.ets +49 -34
  54. package/dist/components-harmony-ets/utils/AttributeManager.ets +2 -2
  55. package/dist/components-harmony-ets/utils/DynamicCenter.ts +2 -2
  56. package/dist/components-harmony-ets/utils/flexManager.ets +43 -13
  57. package/dist/components-harmony-ets/utils/helper.ets +20 -8
  58. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +1 -2
  59. package/dist/components-harmony-ets/utils/htmlParser/index.ts +1 -1
  60. package/dist/components-harmony-ets/utils/index.ts +54 -50
  61. package/dist/components-harmony-ets/utils/styles.ets +165 -104
  62. package/dist/components-harmony-ets/video.ets +28 -20
  63. package/dist/components-harmony-ets/view.ets +44 -38
  64. package/dist/components-harmony-ets/webView.ets +6 -5
  65. package/dist/index.d.ts +152 -0
  66. package/dist/index.js +73 -44
  67. package/dist/index.js.map +1 -1
  68. package/dist/runtime-ets/bom/URL.ts +2 -0
  69. package/dist/runtime-ets/bom/document.ts +5 -4
  70. package/dist/runtime-ets/bom/getComputedStyle.ts +2 -3
  71. package/dist/runtime-ets/bom/history.ts +1 -0
  72. package/dist/runtime-ets/bom/location.ts +1 -0
  73. package/dist/runtime-ets/bom/navigator.ts +1 -21
  74. package/dist/runtime-ets/bom/raf.ts +1 -37
  75. package/dist/runtime-ets/bom/window.ts +9 -5
  76. package/dist/runtime-ets/constant.ts +17 -10
  77. package/dist/runtime-ets/current.ts +26 -2
  78. package/dist/runtime-ets/dom/bind.ts +28 -12
  79. package/dist/runtime-ets/dom/class-list.ts +2 -2
  80. package/dist/runtime-ets/dom/cssNesting.ts +419 -0
  81. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +28 -42
  82. package/dist/runtime-ets/dom/document.ts +9 -12
  83. package/dist/runtime-ets/dom/element/canvas.ts +137 -0
  84. package/dist/runtime-ets/dom/element/element.ts +355 -71
  85. package/dist/runtime-ets/dom/element/form.ts +26 -22
  86. package/dist/runtime-ets/dom/element/index.ts +22 -2
  87. package/dist/runtime-ets/dom/element/movableArea.ts +0 -1
  88. package/dist/runtime-ets/dom/element/movableView.ts +238 -2
  89. package/dist/runtime-ets/dom/element/normal.ts +27 -6
  90. package/dist/runtime-ets/dom/element/progress.ts +0 -2
  91. package/dist/runtime-ets/dom/element/text.ts +0 -8
  92. package/dist/runtime-ets/dom/element/video.ts +4 -4
  93. package/dist/runtime-ets/dom/element/webView.ts +4 -5
  94. package/dist/runtime-ets/dom/event-source.ts +1 -0
  95. package/dist/runtime-ets/dom/event.ts +3 -5
  96. package/dist/runtime-ets/dom/eventTarget.ts +3 -4
  97. package/dist/runtime-ets/dom/node.ts +53 -22
  98. package/dist/runtime-ets/dom/stylesheet/covertWeb2Hm.ts +401 -243
  99. package/dist/runtime-ets/dom/stylesheet/index.ts +27 -316
  100. package/dist/runtime-ets/dom/stylesheet/type.ts +52 -11
  101. package/dist/runtime-ets/dom/stylesheet/util.ts +33 -27
  102. package/dist/runtime-ets/emitter/emitter.ts +1 -0
  103. package/dist/runtime-ets/env.ts +1 -0
  104. package/dist/runtime-ets/index.ts +23 -7
  105. package/dist/runtime-ets/interface/event.ts +1 -1
  106. package/dist/runtime-ets/interface/index.ts +6 -0
  107. package/dist/runtime-ets/utils/index.ts +78 -19
  108. package/dist/runtime-ets/utils/info.ts +2 -2
  109. package/dist/runtime-ets/utils/router.ts +9 -0
  110. package/dist/runtime-framework/react/app.ts +10 -10
  111. package/dist/runtime-framework/react/hooks.ts +1 -2
  112. package/dist/runtime-framework/react/index.ts +0 -2
  113. package/dist/runtime-framework/react/native-page.ts +212 -80
  114. package/dist/runtime-framework/react/page.ts +3 -10
  115. package/dist/runtime-framework/react/utils/index.ts +3 -3
  116. package/dist/runtime-framework/solid/app.ts +29 -46
  117. package/dist/runtime-framework/solid/connect.ts +21 -3
  118. package/dist/runtime-framework/solid/hooks.ts +16 -11
  119. package/dist/runtime-framework/solid/index.ts +6 -2
  120. package/dist/runtime-framework/solid/page.ts +84 -31
  121. package/dist/runtime-framework/solid/reconciler/props.ts +73 -29
  122. package/dist/runtime-framework/solid/reconciler/render.ts +16 -6
  123. package/dist/runtime-framework/solid/reconciler/use.ts +0 -1
  124. package/dist/runtime-framework/solid/utils/index.ts +3 -5
  125. package/dist/runtime-utils.d.ts +826 -0
  126. package/dist/runtime-utils.js +575 -284
  127. package/dist/runtime-utils.js.map +1 -1
  128. package/dist/runtime.d.ts +1 -0
  129. package/dist/runtime.js +575 -284
  130. package/dist/runtime.js.map +1 -1
  131. package/index.js +3 -1
  132. package/package.json +14 -15
  133. package/static/media/cancel.svg +1 -1
  134. package/static/media/circle.svg +1 -1
  135. package/static/media/clear.svg +1 -1
  136. package/static/media/download.svg +1 -1
  137. package/static/media/info.svg +1 -1
  138. package/static/media/info_circle.svg +1 -1
  139. package/static/media/search.svg +1 -1
  140. package/static/media/success.svg +1 -1
  141. package/static/media/success_no_circle.svg +1 -1
  142. package/static/media/warn.svg +1 -1
  143. package/types/harmony.d.ts +5 -0
  144. package/types/index.d.ts +4 -0
  145. package/types/runtime.d.ts +8 -1
  146. /package/dist/runtime-framework/solid/{contant.ts → constant.ts} +0 -0
@@ -5,39 +5,74 @@ import deviceInfo from '@ohos.deviceInfo'
5
5
  import _display from '@ohos.display'
6
6
  // 从 API Version 7 开始支持
7
7
  import i18n from '@ohos.i18n'
8
+ import { Current, window } from '@tarojs/runtime'
8
9
 
9
10
  import { callAsyncFail, callAsyncSuccess } from '../utils'
10
11
 
11
12
  import type Taro from '@tarojs/taro/types'
12
13
 
13
14
  let display
15
+ let navigationIndicatorRect
14
16
  let safeArea: TaroGeneral.SafeAreaResult | null = null
15
- try {
16
- display = _display.getDefaultDisplaySync()
17
- // @ts-ignore
18
- display.getCutoutInfo((err, { boundingRects = [], waterfallDisplayAreaRects = {} }: _display.CutoutInfo = {}) => {
19
- if (err?.code) {
20
- console.error('Failed to get cutout info', JSON.stringify(err))
21
- return
22
- }
17
+ let statusBarHeight
18
+ let windowRect
19
+
20
+ (Current as any).contextPromise.then((context) => {
21
+ const win = window.__ohos.getLastWindow(context)
22
+ win.then(mainWindow => {
23
+ const topRect = mainWindow.getWindowAvoidArea(window.__ohos.AvoidAreaType.TYPE_SYSTEM).topRect
24
+ navigationIndicatorRect = mainWindow.getWindowAvoidArea(window.__ohos.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect
25
+ statusBarHeight = topRect.top + topRect.height
26
+ windowRect = mainWindow.getWindowProperties().windowRect
27
+
28
+ try {
29
+ display = _display.getDefaultDisplaySync()
30
+
31
+ setSafeArea({
32
+ top: statusBarHeight,
33
+ left: 0,
34
+ right: display.width,
35
+ bottom: navigationIndicatorRect?.top
36
+ })
23
37
 
24
- const top = Math.max(...boundingRects.map(rect => rect.top * 2 + rect.height), waterfallDisplayAreaRects.top?.top + waterfallDisplayAreaRects.top?.height)
25
- const bottom = (waterfallDisplayAreaRects.bottom?.top + waterfallDisplayAreaRects.bottom?.height) || display.height
26
- const left = waterfallDisplayAreaRects.left?.left + waterfallDisplayAreaRects.left?.width
27
- const right = (waterfallDisplayAreaRects.right?.left + waterfallDisplayAreaRects.right?.width) || display.width
28
- safeArea = {
29
- top,
30
- bottom,
31
- left,
32
- right,
33
- height: bottom - top,
34
- width: right - left,
38
+ // @ts-ignore
39
+ display.getCutoutInfo((err, { boundingRects = [], waterfallDisplayAreaRects = {} }: _display.CutoutInfo = {}) => {
40
+ if (err?.code) {
41
+ console.error('Failed to get cutout info', JSON.stringify(err))
42
+ return
43
+ }
44
+
45
+ const top = Math.max(...boundingRects.map(rect => rect.top + rect.height), waterfallDisplayAreaRects.top?.top + waterfallDisplayAreaRects.top?.height, statusBarHeight)
46
+ const bottom = Math.min(display.height - waterfallDisplayAreaRects.bottom?.top, navigationIndicatorRect?.top)
47
+ const left = waterfallDisplayAreaRects.left?.left + waterfallDisplayAreaRects.left?.width
48
+ const right = display.width - waterfallDisplayAreaRects.right?.left
49
+
50
+ setSafeArea({
51
+ top,
52
+ left,
53
+ right,
54
+ bottom
55
+ })
56
+ })
57
+ } catch (e) {
58
+ console.error('Failed to get display', e)
35
59
  }
36
60
  })
37
- } catch (e) {
38
- console.error('Failed to get display', e)
61
+ })
62
+
63
+
64
+ function setSafeArea({ top, left, right, bottom }) {
65
+ safeArea = {
66
+ top,
67
+ bottom,
68
+ left,
69
+ right,
70
+ height: bottom - top,
71
+ width: right - left,
72
+ }
39
73
  }
40
74
 
75
+
41
76
  /* 同步版本 */
42
77
  export const getSystemInfoSync: typeof Taro.getSystemInfoSync = function () {
43
78
  const res: any = {}
@@ -60,17 +95,17 @@ export const getSystemInfoSync: typeof Taro.getSystemInfoSync = function () {
60
95
  res.notificationSoundAuthorized = false // 通知带有声音的开关(仅 iOS 有效)boolean
61
96
  res.phoneCalendarAuthorized = null // 使用日历的开关 boolean
62
97
  res.wifiEnabled = false // Wi-Fi 的系统开关 boolean
63
- res.pixelRatio = display && (Math.round(display.xDPI / display.width * 100) / 100) // 设备像素比,number
98
+ res.pixelRatio = display && display.densityPixels // 设备像素比,number
64
99
  res.platform = 'harmony' // 客户端平台 string
65
100
  res.safeArea = safeArea // 在竖屏正方向下的安全区域 General.SafeAreaResult
66
101
  res.screenHeight = display?.height // 屏幕高度,单位px number
67
102
  res.screenWidth = display?.width // 屏幕宽度,单位px number
68
- res.statusBarHeight = safeArea?.top // 状态栏的高度,单位px number
103
+ res.statusBarHeight = statusBarHeight // 状态栏的高度,单位px number
69
104
  res.system = deviceInfo?.osFullName // 操作系统及版本 string
70
105
  // Note: 更新配置时才能记录
71
106
  res.theme = AppStorage.get('__TARO_APP_CONFIG')?.colorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK ? 'dark' : 'light' // 系统当前主题,取值为light或dark 'light' | 'dark'
72
- res.windowHeight = display?.height // 可使用窗口高度,单位px number
73
- res.windowWidth = display?.width // 可使用窗口宽度,单位px number
107
+ res.windowHeight = windowRect?.height // 可使用窗口高度,单位px number
108
+ res.windowWidth = windowRect?.width // 可使用窗口宽度,单位px number
74
109
  res.version = deviceInfo?.displayVersion // 版本号 string
75
110
 
76
111
  return res
@@ -1,4 +1,4 @@
1
- import { eventCenter } from '@tarojs/runtime/dist/runtime.esm'
1
+ import { eventCenter } from '@tarojs/runtime'
2
2
 
3
3
  import type Taro from '@tarojs/taro/types'
4
4
 
@@ -1,12 +1,21 @@
1
+ import { eventSource } from '@tarojs/runtime'
2
+
1
3
  import { temporarilyNotSupport } from '../utils'
2
4
 
5
+ // @ts-ignore
6
+ import type { TaroCanvasElement } from '@tarojs/runtime'
3
7
  // 画布
4
8
 
5
9
  /** 创建离屏 canvas 实例 */
6
10
  export const createOffscreenCanvas = /* @__PURE__ */ temporarilyNotSupport('createOffscreenCanvas')
7
11
 
8
12
  /** 创建 canvas 的绘图上下文 CanvasContext 对象 */
9
- export const createCanvasContext = /* @__PURE__ */ temporarilyNotSupport('createOffscreenCanvas')
13
+ // export const createCanvasContext = /* @__PURE__ */ temporarilyNotSupport('createOffscreenCanvas')
14
+ export const createCanvasContext = (canvasId: string) => {
15
+ const dom = eventSource.get(`canvasId-${canvasId}`)
16
+ // return dom as TaroCanvasElement
17
+ if (dom) return (dom as unknown as TaroCanvasElement).context
18
+ }
10
19
 
11
20
  /** 把当前画布指定区域的内容导出生成指定大小的图片 */
12
21
  export const canvasToTempFilePath = /* @__PURE__ */ temporarilyNotSupport('createOffscreenCanvas')
@@ -1,7 +1,10 @@
1
1
  // 从 API Version 6 开始支持
2
2
  import pasteboard from '@ohos.pasteboard'
3
+ // @ts-ignore
4
+ import { Current } from '@tarojs/runtime'
3
5
  import { isString } from '@tarojs/shared'
4
6
 
7
+ import {} from '../'
5
8
  import { callAsyncFail, getParameterError, object2String } from '../utils'
6
9
  import { MethodHandler } from '../utils/handler'
7
10
 
@@ -27,9 +30,25 @@ export const setClipboardData: typeof Taro.setClipboardData = function (options)
27
30
 
28
31
  return new Promise((resolve, reject) => {
29
32
  const systemPasteboard = pasteboard.getSystemPasteboard()
30
- const pasteData = pasteboard.createPlainTextData(data)
33
+ const pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, data)
31
34
 
32
- systemPasteboard.setPasteData(pasteData, (error, data) => { // callback形式调用异步接口
35
+ try {
36
+ systemPasteboard.setDataSync(pasteData)
37
+ // @ts-ignore
38
+ const uiContext = Current?.page?.getUIContext?.()
39
+
40
+ if (!uiContext) return
41
+
42
+ uiContext.getPromptAction().showToast({
43
+ message: '内容已复制',
44
+ duration: 1500,
45
+ bottom: '50%',
46
+ showMode: 1 // 设置弹窗显示模式,显示在应用之上。
47
+ })
48
+ return handle.success({
49
+ data,
50
+ }, { resolve, reject })
51
+ } catch (error) {
33
52
  if (error) {
34
53
  console.error('Failed to set PasteData. Cause: ' + JSON.stringify(error))
35
54
  res = {
@@ -37,12 +56,8 @@ export const setClipboardData: typeof Taro.setClipboardData = function (options)
37
56
  error: error
38
57
  }
39
58
  callAsyncFail(reject, res, options)
40
- } else {
41
- return handle.success({
42
- data,
43
- }, { resolve, reject })
44
59
  }
45
- })
60
+ }
46
61
  })
47
62
  }
48
63
 
@@ -54,7 +69,7 @@ export const getClipboardData: typeof Taro.getClipboardData = function (options)
54
69
  const handle = new MethodHandler({ name: 'getClipboardData', success, fail, complete })
55
70
  return new Promise((resolve, reject) => {
56
71
  const systemPasteboard = pasteboard.getSystemPasteboard()
57
- systemPasteboard.getPasteData((error, pasteData) => { // callback 形式调用异步接口
72
+ systemPasteboard.getData((error, pasteData) => { // callback 形式调用异步接口
58
73
  if (error) {
59
74
  console.error('Failed to obtain PasteData. Cause: ' + JSON.stringify(error))
60
75
  return handle.fail({
@@ -24,4 +24,4 @@ export { Current, getCurrentInstance } from '@tarojs/runtime'
24
24
 
25
25
  export const requirePlugin = temporarilyNotSupport('requirePlugin')
26
26
 
27
- export { eventCenter, Events, History } from '@tarojs/runtime/dist/runtime.esm'
27
+ export { eventCenter, Events, History } from '@tarojs/runtime'
@@ -58,20 +58,25 @@ export function initPxTransform ({
58
58
 
59
59
  const display = _display.getDefaultDisplaySync()
60
60
 
61
+ let displayWidth = display.width
62
+ let ratioCache: number | false = false
61
63
  let designWidthFunc: (input: number) => number
64
+ let designWidth = defaultDesignWidth
62
65
  function getRatio (value: number) {
63
- const config = (Current as any).taro?.config || {}
64
- if (!isFunction(designWidthFunc)) {
65
- designWidthFunc = isFunction(config.designWidth)
66
- ? config.designWidth
67
- : () => config.designWidth
68
- }
69
- const designWidth = designWidthFunc(value) || defaultDesignWidth
70
- const deviceRatio = config.deviceRatio || defaultDesignRatio
71
- if (!(designWidth in deviceRatio)) {
72
- throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`)
66
+ // Note: 提前调用 display 可能无法获取正确值
67
+ if (ratioCache === false || displayWidth !== display.width) {
68
+ const config = (Current as any).taro?.config || {}
69
+ if (!isFunction(designWidthFunc)) {
70
+ designWidthFunc = isFunction(config.designWidth)
71
+ ? config.designWidth
72
+ : () => config.designWidth
73
+ designWidth = designWidthFunc(value) || defaultDesignWidth
74
+ }
75
+ displayWidth = display.width
76
+ ratioCache = Math.min(display.width, display.height) / designWidth
73
77
  }
74
- return Math.min(display.width, display.height) / designWidth / deviceRatio[designWidth]
78
+
79
+ return ratioCache
75
80
  }
76
81
 
77
82
  // Note: 设置为 style 单位时会自动完成设计稿转换,设计开发者调用 API 时也许抹平差异,例如 pageScrollTo[option.offsetTop]
@@ -79,6 +84,9 @@ export function pxTransformHelper (size: number, unit?: string, isNumber = false
79
84
  const config = (Current as any).taro?.config || {}
80
85
  const targetUnit = unit || config.targetUnit || defaultTargetUnit
81
86
 
87
+ if (targetUnit === 'PX') {
88
+ return px2vp(size * display.scaledDensity) + 'vp'
89
+ }
82
90
  const ratio = getRatio(size)
83
91
  let val = size * ratio
84
92
 
@@ -92,19 +100,19 @@ export function pxTransformHelper (size: number, unit?: string, isNumber = false
92
100
  return isNumber ? val : val + targetUnit
93
101
  }
94
102
 
95
- export function pxTransform (size: number): number {
103
+ export function pxTransform (size: number): number | string {
96
104
  const config = (Current as any).taro?.config || {}
97
105
  const targetUnit = config.targetUnit || defaultTargetUnit
98
106
 
99
- let val = size
107
+ const val = size
100
108
  switch (targetUnit) {
101
109
  case 'vp':
102
- val = px2vp(size)
103
- break
110
+ return pxTransformHelper(size, 'px')
111
+ // return `${size}lpx`
104
112
  default:
105
113
  // NOTE: 鸿蒙环境下 style 会自动完成设计稿转换,无需在方法内二次调整
106
114
  }
107
- return val + config.targetUnit
115
+ return val + targetUnit
108
116
  }
109
117
 
110
118
  export function canIUseWebp () {
@@ -9,10 +9,13 @@
9
9
  // ❌ wx.saveImageToPhotosAlbum(Object object) api 9+ HarmonyOS不支持
10
10
  // ❌ wx.previewImage(Object object) api 9+ HarmonyOS不支持
11
11
 
12
+ import fs from '@ohos.file.fs'
12
13
  import picker from '@ohos.file.picker'
13
14
  import image from '@ohos.multimedia.image'
15
+ import { Current } from '@tarojs/runtime'
14
16
  import { isNull } from '@tarojs/shared'
15
17
 
18
+ import { getSystemInfoSync } from '../../base'
16
19
  import { callAsyncFail, callAsyncSuccess, temporarilyNotSupport, validateParams } from '../../utils'
17
20
 
18
21
  import type Taro from '@tarojs/taro/types'
@@ -22,12 +25,21 @@ interface IPackingOptionOHOS {
22
25
  quality: number
23
26
  }
24
27
 
28
+ interface IChooseImageData {
29
+ tempFilePaths?: string[]
30
+
31
+ tempFiles?: {
32
+ path: string
33
+ size: number
34
+ }[]
35
+ }
36
+
25
37
  const getImageInfoSchema = {
26
- url: 'String'
38
+ src: 'String'
27
39
  }
28
40
 
29
41
  const compressImageSchema = {
30
- url: 'String'
42
+ src: 'String'
31
43
  }
32
44
 
33
45
  const chooseImageSchema = {
@@ -60,6 +72,44 @@ export const getImageInfo: typeof Taro.getImageInfo = function (options) {
60
72
  })
61
73
  }
62
74
 
75
+
76
+ class CompressedImageInfo {
77
+ imageUri = '' // 压缩后图片保存位置的uri
78
+ imageByteLength = 0 // 压缩后图片字节长度
79
+ }
80
+
81
+ async function saveImage(compressedImageData, compressedImageUri) {
82
+ const tempArr = compressedImageUri.split('/')
83
+ const name = tempArr[tempArr.length - 1]
84
+ const context = getContext(Current?.page)
85
+ const applicationContext = context.getApplicationContext()
86
+ const tempDir = applicationContext.tempDir
87
+ const filePath = `${tempDir}/${name}`
88
+
89
+ try {
90
+ const res = fs.accessSync(filePath)
91
+ if (res) {
92
+ // 如果图片afterCompressiona.jpeg已存在,则删除
93
+ fs.unlinkSync(filePath)
94
+ }
95
+ } catch (err) {
96
+ console.error(`[Taro] saveImage Error: AccessSync failed with error message: ${err.message}, error code: ${err.code}`)
97
+ }
98
+
99
+ // 知识点:保存图片。获取最终图片压缩数据compressedImageData,保存图片。
100
+ // 压缩图片数据写入文件
101
+ const file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
102
+ fs.writeSync(file.fd, compressedImageData)
103
+ fs.closeSync(file)
104
+
105
+ // 获取压缩图片信息
106
+ const compressedImageInfo = new CompressedImageInfo()
107
+ compressedImageInfo.imageUri = filePath
108
+ compressedImageInfo.imageByteLength = compressedImageData.byteLength
109
+
110
+ return compressedImageInfo
111
+ }
112
+
63
113
  export const compressImage: typeof Taro.compressImage = function (options) {
64
114
  return new Promise((resolve, reject) => {
65
115
  try {
@@ -68,30 +118,68 @@ export const compressImage: typeof Taro.compressImage = function (options) {
68
118
  const res = { errMsg: error.message }
69
119
  return callAsyncFail(reject, res, options)
70
120
  }
71
- const { src, quality = 80 } = options
121
+ const { src, quality = 80, compressedWidth, compressedHeight } = options
122
+ const srcAfterCompress = src.includes('_after_compress') ? src : src.split('.').join('_after_compress.')
123
+ const file = fs.openSync(src, fs.OpenMode.READ_ONLY)
72
124
 
73
- const source = image.createImageSource(src)
125
+ // const stat = fs.statSync(file.fd)
126
+ // console.log('[Taro] 压缩前图片的大小为:', stat.size)
127
+
128
+ const source = image.createImageSource(file.fd)
74
129
  if (isNull(source)) {
75
130
  const createImageSourceError = { errMsg: 'compressImage fail: createImageSource has failed.' }
76
131
  callAsyncFail(reject, createImageSourceError, options)
77
132
  return
78
133
  }
79
134
 
80
- const packer = image.createImagePacker(src)
81
- if (isNull(packer)) {
82
- const createImagePackerError = { errMsg: 'compressImage fail: createImagePacker has failed.' }
83
- callAsyncFail(reject, createImagePackerError, options)
135
+ const width = source.getImageInfoSync().size.width
136
+ const height = source.getImageInfoSync().size.height
137
+ let wantWidth = compressedWidth || compressedHeight || 0
138
+ let wantHeight = compressedHeight || compressedWidth || 0
139
+
140
+ if (width > wantWidth || height > wantHeight) {
141
+ const heightRatio = height / wantHeight
142
+ const widthRatio = width / wantWidth
143
+ const finalRatio = heightRatio < widthRatio ? heightRatio : widthRatio
144
+
145
+ wantWidth = Math.round(width / finalRatio)
146
+ wantHeight = Math.round(height / finalRatio)
84
147
  }
85
148
 
86
- const packingOptionsOHOS: IPackingOptionOHOS = {
87
- // TODO:需要获取文件名后缀
88
- format: 'image/jpeg',
89
- quality: quality
149
+ const decodingOptions = {
150
+ editable: true,
151
+ desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
152
+ desiredSize: { width: wantWidth, height: wantHeight }
90
153
  }
91
- packer.packing(source, packingOptionsOHOS).then((value) => {
92
- callAsyncSuccess(resolve, value, options)
93
- }).catch((error) => {
94
- callAsyncFail(reject, error, options)
154
+ source.createPixelMap(decodingOptions, (error, pixelMap) => {
155
+ if (error !== undefined) {
156
+ fs.closeSync(file)
157
+ const res = { errMsg: error }
158
+ callAsyncFail(reject, res, options)
159
+ } else {
160
+ const packer = image.createImagePacker(file.fd)
161
+ if (isNull(packer)) {
162
+ fs.closeSync(file)
163
+ const createImagePackerError = { errMsg: 'compressImage fail: createImagePacker has failed.' }
164
+ callAsyncFail(reject, createImagePackerError, options)
165
+ return
166
+ }
167
+
168
+ const isPNG = src.endsWith('.png')
169
+ const packingOptionsOHOS: IPackingOptionOHOS = {
170
+ format: isPNG ? 'image/png' : 'image/jpeg',
171
+ quality: quality
172
+ }
173
+ packer.packing(pixelMap, packingOptionsOHOS).then((value) => {
174
+ fs.closeSync(file)
175
+ saveImage(value, srcAfterCompress).then(result => {
176
+ callAsyncSuccess(resolve, { tempFilePath: result.imageUri }, options)
177
+ })
178
+ }).catch((error) => {
179
+ fs.closeSync(file)
180
+ callAsyncFail(reject, error, options)
181
+ })
182
+ }
95
183
  })
96
184
  })
97
185
  }
@@ -107,12 +195,76 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
107
195
 
108
196
  const { count = 9 } = options
109
197
  const photoViewPicker = new picker.PhotoViewPicker()
198
+ let sizeType = options.sizeType
199
+
200
+ if (!sizeType || !sizeType.length) {
201
+ sizeType = ['compressed', 'original']
202
+ }
110
203
 
111
204
  photoSelectOptions.maxSelectNumber = count // 选择媒体文件的最大数目
112
205
  photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE // 过滤选择媒体文件类型为IMAGE
113
206
 
114
207
  photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
115
- callAsyncSuccess(resolve, { tempFilePaths: photoSelectResult.photoUris })
208
+ const result: IChooseImageData = {}
209
+ const isOrigin = photoSelectResult.isOriginalPhoto
210
+
211
+ if (isOrigin) {
212
+ const tempFilePaths: string[] = []
213
+ const tempFiles = photoSelectResult.photoUris.map(uri => {
214
+ const file = fs.openSync(uri, fs.OpenMode.READ_ONLY)
215
+ const stat = fs.statSync(file.fd)
216
+ const size = stat.size
217
+
218
+ fs.closeSync(file)
219
+ tempFilePaths.push(uri)
220
+
221
+ return {
222
+ size,
223
+ path: uri,
224
+ }
225
+ })
226
+
227
+ result.tempFiles = tempFiles
228
+ result.tempFilePaths = tempFilePaths
229
+
230
+ callAsyncSuccess(resolve, result, options)
231
+ } else {
232
+ const actions: Promise<string>[] = photoSelectResult.photoUris.map(uri => {
233
+ return new Promise<string>(resolve => {
234
+ compressImage({
235
+ src: uri,
236
+ compressedWidth: getSystemInfoSync().screenWidth / 2,
237
+ compressedHeight: getSystemInfoSync().screenHeight / 2,
238
+ success: (compressResult) => {
239
+ resolve(compressResult.tempFilePath)
240
+ }
241
+ })
242
+ })
243
+ })
244
+
245
+ Promise.all(actions).then(tempFilePaths => {
246
+ const tempFiles = tempFilePaths.map(uri => {
247
+ const file = fs.openSync(uri, fs.OpenMode.READ_ONLY)
248
+ const stat = fs.statSync(file.fd)
249
+ const size: number = stat.size
250
+
251
+ fs.closeSync(file)
252
+
253
+ return {
254
+ size,
255
+ path: uri,
256
+ }
257
+ })
258
+
259
+ result.tempFilePaths = tempFilePaths
260
+ result.tempFiles = tempFiles
261
+
262
+ callAsyncSuccess(resolve, result, options)
263
+ }).catch(error => {
264
+ const res = { errMsg: error }
265
+ return callAsyncFail(reject, res, options)
266
+ })
267
+ }
116
268
  }).catch((error) => {
117
269
  callAsyncFail(reject, error, options)
118
270
  })
@@ -8,10 +8,12 @@
8
8
  // ❌ DownloadTask.offHeadersReceived 此接口 ohos 不支持
9
9
 
10
10
  import request from '@ohos.request'
11
- import Taro, { DownloadTask } from '@tarojs/taro'
12
11
 
13
12
  import { callAsyncFail, callAsyncSuccess, validateParams } from '../utils'
14
13
 
14
+ import type Taro from '@tarojs/taro/types'
15
+ import type { DownloadTask } from '@tarojs/taro/types'
16
+
15
17
  interface IDownloadConfigOHOS {
16
18
  url: string
17
19
  header?: any
@@ -9,10 +9,12 @@
9
9
 
10
10
  import request from '@ohos.request'
11
11
  import { isUndefined } from '@tarojs/shared'
12
- import Taro, { UploadTask } from '@tarojs/taro'
13
12
 
14
13
  import { callAsyncFail, callAsyncSuccess, validateParams } from '../utils'
15
14
 
15
+ import type Taro from '@tarojs/taro/types'
16
+ import type { UploadTask } from '@tarojs/taro/types'
17
+
16
18
  interface IOHOSFileType {
17
19
  filename?: string
18
20
  name?: string
@@ -1,6 +1,5 @@
1
1
  import router from '@ohos.router'
2
- import { window } from '@tarojs/runtime' // eslint-disable-line import/no-duplicates
3
- import { eventCenter } from '@tarojs/runtime/dist/runtime.esm' // eslint-disable-line import/no-duplicates
2
+ import { eventCenter, window } from '@tarojs/runtime'
4
3
  import { queryToJson } from '@tarojs/shared'
5
4
 
6
5
  import { callAsyncFail, callAsyncSuccess } from '../utils'