@tarojs/plugin-platform-harmony-ets 4.0.0-beta.40 → 4.0.0-beta.42

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.
@@ -28,11 +28,10 @@ import http from '@ohos.net.http';
28
28
  import webSocket from '@ohos.net.webSocket';
29
29
  import router from '@ohos.router';
30
30
  import bundleManager from '@ohos.bundle.bundleManager';
31
- import dataPreferences from '@ohos.data.preferences';
32
- import hilog from '@ohos.hilog';
31
+ import distributedKVStore from '@ohos.data.distributedKVStore';
33
32
  import matrix4 from '@ohos.matrix4';
34
33
  import prompt from '@ohos.prompt';
35
- import { pxTransformHelper } from '@tarojs/taro';
34
+ import { pxTransformHelper as pxTransformHelper$1 } from '@tarojs/taro';
36
35
 
37
36
  class MethodHandler {
38
37
  constructor({ name, success, fail, complete }) {
@@ -3234,28 +3233,48 @@ const createCacheManager = /* @__PURE__ */ temporarilyNotSupport('createCacheMan
3234
3233
  * https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-data-preferences-0000001427745052-V3
3235
3234
  */
3236
3235
  let context;
3237
- let preferences;
3236
+ let kvManager;
3237
+ let kvStore;
3238
3238
  Current.contextPromise.then((ctx) => {
3239
3239
  context = ctx;
3240
- return context;
3241
- });
3242
- function getPreferences() {
3240
+ const kvManagerConfig = {
3241
+ context: context,
3242
+ bundleName: 'com.example.taro'
3243
+ };
3243
3244
  try {
3244
- if (!preferences && context) {
3245
- const data = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
3246
- preferences = dataPreferences.getPreferencesSync(context, { name: `${data.appInfo.uid}Store` });
3247
- }
3245
+ // 创建KVManager实例
3246
+ kvManager = distributedKVStore.createKVManager(kvManagerConfig);
3247
+ // 继续创建获取数据库
3248
+ const options = {
3249
+ createIfMissing: true,
3250
+ encrypt: false,
3251
+ backup: false,
3252
+ autoSync: false,
3253
+ // kvStoreType不填时,默认创建多设备协同数据库
3254
+ kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
3255
+ // 多设备协同数据库:kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION,
3256
+ securityLevel: distributedKVStore.SecurityLevel.S1
3257
+ };
3258
+ const data = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
3259
+ kvManager.getKVStore(`${data.appInfo.uid}Store`, options, (err, store) => {
3260
+ if (err) {
3261
+ console.error(`Failed to get KVStore: Code:${err.code},message:${err.message}`);
3262
+ return;
3263
+ }
3264
+ kvStore = store;
3265
+ // 请确保获取到键值数据库实例后,再进行相关数据操作
3266
+ });
3248
3267
  }
3249
- catch (error) {
3250
- hilog.error(0x0000, 'TaroFailedTag', 'Failed to load the storage. Cause: %{public}s', error.code ? JSON.stringify(error) : error.message || error);
3268
+ catch (e) {
3269
+ console.error(`Failed to create KVManager. Code:${e.code},message:${e.message}`);
3251
3270
  }
3252
- return preferences;
3253
- }
3271
+ return context;
3272
+ });
3254
3273
  const storageSchema = {
3255
3274
  key: 'String'
3256
3275
  };
3257
3276
  function checkContextExist(api, isAsync = false) {
3258
- if (!context) {
3277
+ if (!context || !kvStore) {
3259
3278
  const message = `${api} 调用失败,Taro 不支持过早地调用 ${api},请确保页面已经渲染完成再调用此 API`;
3260
3279
  if (isAsync) {
3261
3280
  return {
@@ -3290,39 +3309,16 @@ function getStorage(options) {
3290
3309
  const res = { errMsg: error.message };
3291
3310
  return handle.fail(res, { resolve, reject });
3292
3311
  }
3293
- const preferences = getPreferences();
3294
- if (!preferences)
3295
- return handle.fail({}, { resolve, reject });
3296
- const data = preferences.getSync(key, null);
3297
- if (data) {
3298
- return handle.success({ data }, { resolve, reject });
3299
- }
3300
- else {
3301
- return handle.success({ errMsg: 'data not found' }, { resolve, reject });
3302
- }
3312
+ kvStore = kvStore;
3313
+ kvStore.get(key, (err, data) => {
3314
+ if (err) {
3315
+ handle.fail({ errMsg: `Failed to get data. Code:${err.code},message:${err.message}` }, { resolve, reject });
3316
+ return;
3317
+ }
3318
+ handle.success({ data }, { resolve, reject });
3319
+ });
3303
3320
  });
3304
3321
  }
3305
- function getStorageSync(key) {
3306
- const name = 'getStorageSync';
3307
- const { isExist, error } = checkContextExist(name, false);
3308
- if (!isExist) {
3309
- return error;
3310
- }
3311
- if (!key) {
3312
- throw new Error(`${name}:fail parameter error: parameter should be String`);
3313
- }
3314
- const preferences = getPreferences();
3315
- if (!preferences) {
3316
- throw new Error(`${name}:fail:preferences is null`);
3317
- }
3318
- const data = preferences.getSync(key, null);
3319
- if (data) {
3320
- return data;
3321
- }
3322
- else {
3323
- throw new Error('data not found');
3324
- }
3325
- }
3326
3322
  function setStorage(options) {
3327
3323
  const name = 'setStorage';
3328
3324
  const { isExist, error } = checkContextExist(name, true);
@@ -3339,30 +3335,16 @@ function setStorage(options) {
3339
3335
  const res = { errMsg: error.message };
3340
3336
  return handle.fail(res, { resolve, reject });
3341
3337
  }
3342
- const preferences = getPreferences();
3343
- if (!preferences)
3344
- return handle.fail({}, { resolve, reject });
3345
- preferences.putSync(key, data);
3346
- preferences.flush();
3347
- return handle.success({}, { resolve, reject });
3338
+ kvStore = kvStore;
3339
+ kvStore.put(key, data, (err) => {
3340
+ if (err) {
3341
+ handle.fail({ errMsg: `Failed to put data. Code:${err.code},message:${err.message}` }, { resolve, reject });
3342
+ return;
3343
+ }
3344
+ handle.success({}, { resolve, reject });
3345
+ });
3348
3346
  });
3349
3347
  }
3350
- function setStorageSync(key, data) {
3351
- const name = 'setStorageSync';
3352
- const { isExist, error } = checkContextExist(name, false);
3353
- if (!isExist) {
3354
- return error;
3355
- }
3356
- if (!key) {
3357
- throw new Error(`${name}:fail key error: key should be String`);
3358
- }
3359
- const preferences = getPreferences();
3360
- if (!preferences) {
3361
- throw new Error(`${name}:fail:preferences is null`);
3362
- }
3363
- preferences.putSync(key, data);
3364
- preferences.flush();
3365
- }
3366
3348
  function removeStorage(options) {
3367
3349
  const name = 'removeStorage';
3368
3350
  const { isExist, error } = checkContextExist(name, true);
@@ -3379,60 +3361,16 @@ function removeStorage(options) {
3379
3361
  const res = { errMsg: error.message };
3380
3362
  return handle.fail(res, { resolve, reject });
3381
3363
  }
3382
- const preferences = getPreferences();
3383
- if (!preferences)
3384
- return handle.fail({}, { resolve, reject });
3385
- preferences.deleteSync(key);
3386
- preferences.flush();
3387
- return handle.success({}, { resolve, reject });
3388
- });
3389
- }
3390
- function removeStorageSync(key) {
3391
- const name = 'removeStorageSync';
3392
- const { isExist, error } = checkContextExist(name, false);
3393
- if (!isExist) {
3394
- return error;
3395
- }
3396
- if (!key) {
3397
- throw new Error(`${name}:fail key error: key should be String`);
3398
- }
3399
- const preferences = getPreferences();
3400
- if (!preferences) {
3401
- throw new Error(`${name}:fail:preferences is null`);
3402
- }
3403
- preferences.deleteSync(key);
3404
- preferences.flush();
3405
- }
3406
- function clearStorage(options) {
3407
- const name = 'clearStorage';
3408
- const { isExist, error } = checkContextExist(name, true);
3409
- if (!isExist) {
3410
- return error;
3411
- }
3412
- const { success, fail, complete } = options || {};
3413
- const handle = new MethodHandler({ name, success, fail, complete });
3414
- return new Promise((resolve, reject) => {
3415
- const preferences = getPreferences();
3416
- if (!preferences)
3417
- return handle.fail({}, { resolve, reject });
3418
- preferences.clearSync();
3419
- preferences.flush();
3420
- return handle.success({}, { resolve, reject });
3364
+ kvStore = kvStore;
3365
+ kvStore.delete(key, (err) => {
3366
+ if (err) {
3367
+ handle.fail({ errMsg: `Failed to delete data. Code:${err.code},message:${err.message}` }, { resolve, reject });
3368
+ return;
3369
+ }
3370
+ handle.success({}, { resolve, reject });
3371
+ });
3421
3372
  });
3422
3373
  }
3423
- function clearStorageSync() {
3424
- const name = 'clearStorageSync';
3425
- const { isExist, error } = checkContextExist(name, false);
3426
- if (!isExist) {
3427
- return error;
3428
- }
3429
- const preferences = getPreferences();
3430
- if (!preferences) {
3431
- throw new Error(`${name}:fail:preferences is null`);
3432
- }
3433
- preferences.clearSync();
3434
- preferences.flush();
3435
- }
3436
3374
  const getStorageInfoSync = temporarilyNotSupport('getStorageInfoSync');
3437
3375
  const getStorageInfo = temporarilyNotSupport('getStorageInfo');
3438
3376
  const createBufferURL = /* @__PURE__ */ temporarilyNotSupport('createBufferURL');
@@ -3441,6 +3379,11 @@ const batchSetStorageSync = /* @__PURE__ */ temporarilyNotSupport('batchSetStora
3441
3379
  const batchSetStorage = /* @__PURE__ */ temporarilyNotSupport('batchSetStorage');
3442
3380
  const batchGetStorageSync = /* @__PURE__ */ temporarilyNotSupport('batchGetStorageSync');
3443
3381
  const batchGetStorage = /* @__PURE__ */ temporarilyNotSupport('batchGetStorage');
3382
+ const clearStorage = temporarilyNotSupport('removeStorageSync');
3383
+ const getStorageSync = temporarilyNotSupport('getStorageSync', 'getStorage');
3384
+ const setStorageSync = temporarilyNotSupport('setStorageSync', 'setStorage');
3385
+ const clearStorageSync = temporarilyNotSupport('clearStorageSync', 'clearStorage');
3386
+ const removeStorageSync = temporarilyNotSupport('removeStorageSync', 'removeStorage');
3444
3387
 
3445
3388
  class Animation {
3446
3389
  constructor({ duration = 400, delay = 0, timingFunction = 'linear', transformOrigin = '50% 50% 0', unit = 'px' } = {}) {
@@ -3880,7 +3823,7 @@ const pageScrollTo = (options) => {
3880
3823
  scroller = getPageScrollerOrNode(scroller, page);
3881
3824
  const { yOffset } = scroller.currentOffset();
3882
3825
  if (areaInfo) {
3883
- scrollValue = areaInfo.globalPosition.y + yOffset + pxTransformHelper(offsetTop, 'px', true);
3826
+ scrollValue = areaInfo.globalPosition.y + yOffset + pxTransformHelper$1(offsetTop, 'px', true);
3884
3827
  }
3885
3828
  }
3886
3829
  const { xOffset } = scroller.currentOffset();
@@ -4880,7 +4823,49 @@ function initPxTransform({ designWidth = defaultDesignWidth, deviceRatio = defau
4880
4823
  }
4881
4824
  }
4882
4825
  const display = _display.getDefaultDisplaySync();
4883
- display.width;
4826
+ let displayWidth = display.width;
4827
+ let ratioCache = false;
4828
+ let designWidthFunc;
4829
+ let designWidth = defaultDesignWidth;
4830
+ let deviceRatio = defaultDesignRatio;
4831
+ function getRatio(value) {
4832
+ var _a;
4833
+ // Note: 提前调用 display 可能无法获取正确值
4834
+ if (ratioCache === false || displayWidth !== display.width) {
4835
+ const config = ((_a = Current.taro) === null || _a === void 0 ? void 0 : _a.config) || {};
4836
+ if (!isFunction(designWidthFunc)) {
4837
+ designWidthFunc = isFunction(config.designWidth)
4838
+ ? config.designWidth
4839
+ : () => config.designWidth;
4840
+ designWidth = designWidthFunc(value) || defaultDesignWidth;
4841
+ deviceRatio = config.deviceRatio || defaultDesignRatio;
4842
+ if (!(designWidth in deviceRatio)) {
4843
+ throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
4844
+ }
4845
+ }
4846
+ displayWidth = display.width;
4847
+ ratioCache = Math.min(display.width, display.height) / designWidth;
4848
+ }
4849
+ return ratioCache;
4850
+ }
4851
+ // Note: 设置为 style 单位时会自动完成设计稿转换,设计开发者调用 API 时也许抹平差异,例如 pageScrollTo[option.offsetTop]
4852
+ function pxTransformHelper(size, unit, isNumber = false) {
4853
+ var _a;
4854
+ const config = ((_a = Current.taro) === null || _a === void 0 ? void 0 : _a.config) || {};
4855
+ const targetUnit = unit || config.targetUnit || defaultTargetUnit;
4856
+ if (targetUnit === 'PX') {
4857
+ return px2vp(size * display.scaledDensity) + 'vp';
4858
+ }
4859
+ const ratio = getRatio(size);
4860
+ let val = size * ratio;
4861
+ switch (targetUnit) {
4862
+ case 'vp':
4863
+ // Note: 在应用创建前调用无效
4864
+ val = px2vp(val);
4865
+ break;
4866
+ }
4867
+ return isNumber ? val : val + targetUnit;
4868
+ }
4884
4869
  function pxTransform(size) {
4885
4870
  var _a;
4886
4871
  const config = ((_a = Current.taro) === null || _a === void 0 ? void 0 : _a.config) || {};
@@ -4888,8 +4873,7 @@ function pxTransform(size) {
4888
4873
  const val = size;
4889
4874
  switch (targetUnit) {
4890
4875
  case 'vp':
4891
- // return pxTransformHelper(size, 'px')
4892
- return `${size}lpx`;
4876
+ return pxTransformHelper(size, 'px');
4893
4877
  // NOTE: 鸿蒙环境下 style 会自动完成设计稿转换,无需在方法内二次调整
4894
4878
  }
4895
4879
  return val + targetUnit;