@tarojs/plugin-platform-harmony-ets 4.0.0-beta.112 → 4.0.0-beta.114

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.
@@ -12,10 +12,11 @@
12
12
  import fs from '@ohos.file.fs'
13
13
  import picker from '@ohos.file.picker'
14
14
  import image from '@ohos.multimedia.image'
15
+ import { Current } from '@tarojs/runtime'
15
16
  import { isNull } from '@tarojs/shared'
16
17
 
17
- import { callAsyncFail, callAsyncSuccess, requestPermissions, temporarilyNotSupport, validateParams } from '../../utils'
18
- import { READ_IMAGEVIDEO_PERMISSIONS } from '../../utils/permissions'
18
+ import { getSystemInfoSync } from '../../base'
19
+ import { callAsyncFail, callAsyncSuccess, temporarilyNotSupport, validateParams } from '../../utils'
19
20
 
20
21
  import type Taro from '@tarojs/taro/types'
21
22
 
@@ -30,7 +31,7 @@ interface IChooseImageData {
30
31
  tempFiles?: {
31
32
  path: string
32
33
  size: number
33
- }
34
+ }[]
34
35
  }
35
36
 
36
37
  const getImageInfoSchema = {
@@ -78,12 +79,18 @@ class CompressedImageInfo {
78
79
  }
79
80
 
80
81
  async function saveImage(compressedImageData, compressedImageUri) {
81
- // 定义要保存的压缩图片uri。afterCompressiona.jpeg表示压缩后的图片。
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
+
82
89
  try {
83
- const res = fs.accessSync(compressedImageUri)
90
+ const res = fs.accessSync(filePath)
84
91
  if (res) {
85
92
  // 如果图片afterCompressiona.jpeg已存在,则删除
86
- fs.unlinkSync(compressedImageUri)
93
+ fs.unlinkSync(filePath)
87
94
  }
88
95
  } catch (err) {
89
96
  console.error(`[Taro] saveImage Error: AccessSync failed with error message: ${err.message}, error code: ${err.code}`)
@@ -91,13 +98,13 @@ async function saveImage(compressedImageData, compressedImageUri) {
91
98
 
92
99
  // 知识点:保存图片。获取最终图片压缩数据compressedImageData,保存图片。
93
100
  // 压缩图片数据写入文件
94
- const file = fs.openSync(compressedImageUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
101
+ const file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
95
102
  fs.writeSync(file.fd, compressedImageData)
96
103
  fs.closeSync(file)
97
104
 
98
105
  // 获取压缩图片信息
99
106
  const compressedImageInfo = new CompressedImageInfo()
100
- compressedImageInfo.imageUri = compressedImageUri
107
+ compressedImageInfo.imageUri = filePath
101
108
  compressedImageInfo.imageByteLength = compressedImageData.byteLength
102
109
 
103
110
  return compressedImageInfo
@@ -105,83 +112,143 @@ async function saveImage(compressedImageData, compressedImageUri) {
105
112
 
106
113
  export const compressImage: typeof Taro.compressImage = function (options) {
107
114
  return new Promise((resolve, reject) => {
108
- requestPermissions([READ_IMAGEVIDEO_PERMISSIONS]).then(() => {
109
- try {
110
- validateParams('compressImage', options, compressImageSchema)
111
- } catch (error) {
112
- const res = { errMsg: error.message }
113
- return callAsyncFail(reject, res, options)
114
- }
115
- const { src, quality = 80 } = options
116
- const srcAfterCompress = src.split('.').join('_after_compress.')
117
- const file = fs.openSync(src, fs.OpenMode.READ_ONLY)
118
-
119
- // const stat = fs.statSync(file.fd)
120
- // console.log('[Taro] 压缩前图片的大小为:', stat.size)
121
-
122
- const source = image.createImageSource(file.fd)
123
- if (isNull(source)) {
124
- const createImageSourceError = { errMsg: 'compressImage fail: createImageSource has failed.' }
125
- callAsyncFail(reject, createImageSourceError, options)
126
- return
127
- }
115
+ try {
116
+ validateParams('compressImage', options, compressImageSchema)
117
+ } catch (error) {
118
+ const res = { errMsg: error.message }
119
+ return callAsyncFail(reject, res, options)
120
+ }
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)
128
124
 
129
- const packer = image.createImagePacker(file.fd)
130
- if (isNull(packer)) {
131
- const createImagePackerError = { errMsg: 'compressImage fail: createImagePacker has failed.' }
132
- callAsyncFail(reject, createImagePackerError, options)
133
- }
125
+ // const stat = fs.statSync(file.fd)
126
+ // console.log('[Taro] 压缩前图片的大小为:', stat.size)
134
127
 
135
- const packingOptionsOHOS: IPackingOptionOHOS = {
136
- // TODO:需要获取文件名后缀
137
- format: 'image/jpeg',
138
- quality: quality
139
- }
140
- packer.packing(source, packingOptionsOHOS).then((value) => {
141
- saveImage(value, srcAfterCompress).then(result => {
142
- callAsyncSuccess(resolve, { tempFilePath: result.imageUri }, options)
143
- })
144
- }).catch((error) => {
145
- callAsyncFail(reject, error, options)
146
- })
128
+ const source = image.createImageSource(file.fd)
129
+ if (isNull(source)) {
130
+ const createImageSourceError = { errMsg: 'compressImage fail: createImageSource has failed.' }
131
+ callAsyncFail(reject, createImageSourceError, options)
132
+ return
133
+ }
147
134
 
148
- fs.closeSync(file)
149
- }, (error: string) => {
150
- const res = { errMsg: error }
151
- return callAsyncFail(reject, res, 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)
147
+ }
148
+
149
+ const decodingOptions = {
150
+ editable: true,
151
+ desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
152
+ desiredSize: { width: wantWidth, height: wantHeight }
153
+ }
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
+ }
152
183
  })
153
184
  })
154
185
  }
155
186
 
156
187
  export const chooseImage: typeof Taro.chooseImage = function (options) {
157
188
  return new Promise((resolve, reject) => {
158
- requestPermissions([READ_IMAGEVIDEO_PERMISSIONS]).then(() => {
159
- try {
160
- validateParams('chooseImage', options, chooseImageSchema)
161
- } catch (error) {
162
- const res = { errMsg: error.message }
163
- return callAsyncFail(reject, res, options)
164
- }
189
+ try {
190
+ validateParams('chooseImage', options, chooseImageSchema)
191
+ } catch (error) {
192
+ const res = { errMsg: error.message }
193
+ return callAsyncFail(reject, res, options)
194
+ }
165
195
 
166
- const { count = 9 } = options
167
- const photoViewPicker = new picker.PhotoViewPicker()
168
- let sizeType = options.sizeType
196
+ const { count = 9 } = options
197
+ const photoViewPicker = new picker.PhotoViewPicker()
198
+ let sizeType = options.sizeType
169
199
 
170
- if (!sizeType || !sizeType.length) {
171
- sizeType = ['compressed', 'original']
172
- }
200
+ if (!sizeType || !sizeType.length) {
201
+ sizeType = ['compressed', 'original']
202
+ }
203
+
204
+ photoSelectOptions.maxSelectNumber = count // 选择媒体文件的最大数目
205
+ photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE // 过滤选择媒体文件类型为IMAGE
206
+
207
+ photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
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
173
217
 
174
- photoSelectOptions.maxSelectNumber = count // 选择媒体文件的最大数目
175
- photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE // 过滤选择媒体文件类型为IMAGE
218
+ fs.closeSync(file)
219
+ tempFilePaths.push(uri)
176
220
 
177
- photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
178
- const result: IChooseImageData = {}
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
+ })
179
244
 
180
- if (sizeType.includes('original')) {
181
- const tempFiles = photoSelectResult.photoUris.map(uri => {
245
+ Promise.all(actions).then(tempFilePaths => {
246
+ const tempFiles = tempFilePaths.map(uri => {
182
247
  const file = fs.openSync(uri, fs.OpenMode.READ_ONLY)
183
248
  const stat = fs.statSync(file.fd)
184
- const size = stat.size
249
+ const size: number = stat.size
250
+
251
+ fs.closeSync(file)
185
252
 
186
253
  return {
187
254
  size,
@@ -189,37 +256,17 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
189
256
  }
190
257
  })
191
258
 
259
+ result.tempFilePaths = tempFilePaths
192
260
  result.tempFiles = tempFiles
193
- }
194
-
195
- if (sizeType.includes('compressed')) {
196
- const actions: Promise<string>[] = photoSelectResult.photoUris.map(uri => {
197
- return new Promise<string>(resolve => {
198
- compressImage({
199
- src: uri,
200
- success: (compressResult) => {
201
- resolve(compressResult.tempFilePath)
202
- }
203
- })
204
- })
205
- })
206
261
 
207
- Promise.all(actions).then(tempFilePaths => {
208
- result.tempFilePaths = tempFilePaths
209
- callAsyncSuccess(resolve, result, options)
210
- }).catch(error => {
211
- const res = { errMsg: error }
212
- return callAsyncFail(reject, res, options)
213
- })
214
- } else {
215
262
  callAsyncSuccess(resolve, result, options)
216
- }
217
- }).catch((error) => {
218
- callAsyncFail(reject, error, options)
219
- })
220
- }, (error: string) => {
221
- const res = { errMsg: error }
222
- return callAsyncFail(reject, res, options)
263
+ }).catch(error => {
264
+ const res = { errMsg: error }
265
+ return callAsyncFail(reject, res, options)
266
+ })
267
+ }
268
+ }).catch((error) => {
269
+ callAsyncFail(reject, error, options)
223
270
  })
224
271
  })
225
272
  }
@@ -1 +1,6 @@
1
1
  export const READ_IMAGEVIDEO_PERMISSIONS = 'ohos.permission.READ_IMAGEVIDEO'
2
+ export const READ_MEDIA_PERMISSIONS = 'ohos.permission.READ_MEDIA'
3
+ export const WRITE_MEDIA_PERMISSIONS = 'ohos.permission.WRITE_MEDIA'
4
+ export const MEDIA_LOCATION_PERMISSIONS = 'ohos.permission.MEDIA_LOCATION'
5
+
6
+ export const IMAGE_PERMISSION = [READ_IMAGEVIDEO_PERMISSIONS, READ_MEDIA_PERMISSIONS, WRITE_MEDIA_PERMISSIONS, MEDIA_LOCATION_PERMISSIONS]
@@ -231,14 +231,18 @@ export class TaroMovableViewElement extends TaroElement<MovableViewProps & { ani
231
231
  public callTouchEventFnFromGesture(eventName: string, gestureEvent: GestureEvent) {
232
232
  const touchFns = (this?.__listeners?.[eventName] || []) as Function[]
233
233
  touchFns.forEach(fn => {
234
- fn({
235
- _hmEvent: gestureEvent,
236
- target: this,
237
- changedTouches: gestureEvent.fingerList.map(finger => ({
238
- clientX: finger.globalX,
239
- clientY: finger.globalY
240
- }))
241
- })
234
+ try {
235
+ fn({
236
+ _hmEvent: gestureEvent,
237
+ target: this,
238
+ changedTouches: gestureEvent.fingerList.map(finger => ({
239
+ clientX: finger.globalX,
240
+ clientY: finger.globalY
241
+ }))
242
+ })
243
+ } catch (error) {
244
+ console.error(error)
245
+ }
242
246
  })
243
247
  }
244
248
  }
@@ -38,10 +38,10 @@ export interface TaroStyleType {
38
38
 
39
39
  // position
40
40
  position?: 'relative' | 'absolute' | 'fixed'
41
- top?: Length
42
- left?: Length
43
- bottom?: Length
44
- right?: Length
41
+ top?: Dimension
42
+ left?: Dimension
43
+ bottom?: Dimension
44
+ right?: Dimension
45
45
 
46
46
  // flex
47
47
  flexBasis?: number | string