@tarojs/shared
Advanced tools
Comparing version 3.1.0-beta.5 to 3.1.0-beta.6
@@ -6,1 +6,2 @@ export * from './is'; | ||
export * from './template'; | ||
export * from './native-apis'; |
@@ -569,2 +569,22 @@ 'use strict'; | ||
} | ||
function unsupport(api) { | ||
return function () { | ||
console.warn(("小程序暂不支持 " + api)); | ||
}; | ||
} | ||
function setUniqueKeyToRoute(key, obj) { | ||
var routerParamsPrivateKey = '__key_'; | ||
var useDataCacheApis = [ | ||
'navigateTo', | ||
'redirectTo', | ||
'reLaunch', | ||
'switchTab' | ||
]; | ||
if (useDataCacheApis.indexOf(key) > -1) { | ||
var url = obj.url = obj.url || ''; | ||
var hasMark = url.indexOf('?') > -1; | ||
var cacheKey = getUniqueKey(); | ||
obj.url += (hasMark ? '&' : '?') + routerParamsPrivateKey + "=" + cacheKey; | ||
} | ||
} | ||
@@ -768,3 +788,3 @@ /** | ||
var nextLevel = isSupportRecursive ? 0 : level + 1; | ||
var data = !this.isSupportRecursive | ||
var data = !this.isSupportRecursive && this.supportXS | ||
? ("" + (this.dataKeymap('i:item,l:l'))) | ||
@@ -774,3 +794,3 @@ : this.dataKeymap('i:item'); | ||
? ("<template is=\"{{xs.e(" + (isSupportRecursive ? 0 : 'cid+1') + ")}}\" data=\"{{" + data + "}}\" />") | ||
: ("<template is=\"tmpl_" + nextLevel + "_" + ("container") + "\" data=\"{{" + (this.dataKeymap('i:item')) + "}}\" />"); | ||
: ("<template is=\"tmpl_" + nextLevel + "_" + ("container") + "\" data=\"{{" + data + "}}\" />"); | ||
if (isFunction(this.modifyLoopBody)) { | ||
@@ -819,5 +839,7 @@ child = this.modifyLoopBody(child, comp.nodeName); | ||
var isSupportRecursive = ref.isSupportRecursive; | ||
var supportXS = ref.supportXS; | ||
var nestElements = ref.nestElements; | ||
var nextLevel = isSupportRecursive ? 0 : level + 1; | ||
var template = ''; | ||
var data = !this.isSupportRecursive && this.supportXS | ||
var data = !isSupportRecursive && supportXS | ||
? ("" + (this.dataKeymap('i:item,l:l'))) | ||
@@ -830,3 +852,8 @@ : this.dataKeymap('i:item'); | ||
else { | ||
template += "\n<template name=\"tmpl_" + level + "_" + compName + "\">\n <" + compName + " " + (this$1.buildThirdPartyAttr(attrs)) + " id=\"{{i.uid}}\">\n <block " + (Adapter.for) + "=\"{{i." + ("cn") + "}}\" " + (Adapter.key) + "=\"uid\">\n <template is=\"tmpl_" + nextLevel + "_" + ("container") + "\" data=\"{{" + data + "}}\" />\n </block>\n </" + compName + ">\n</template>\n "; | ||
if (!isSupportRecursive && supportXS && nestElements.has(compName) && level + 1 > nestElements.get(compName)) | ||
{ return; } | ||
var child = supportXS | ||
? ("<template is=\"{{xs.e(" + (isSupportRecursive ? 0 : 'cid+1') + ")}}\" data=\"{{" + data + "}}\" />") | ||
: ("<template is=\"tmpl_" + nextLevel + "_" + ("container") + "\" data=\"{{" + data + "}}\" />"); | ||
template += "\n<template name=\"tmpl_" + level + "_" + compName + "\">\n <" + compName + " " + (this$1.buildThirdPartyAttr(attrs)) + " id=\"{{i.uid}}\">\n <block " + (Adapter.for) + "=\"{{i." + ("cn") + "}}\" " + (Adapter.key) + "=\"uid\">\n " + child + "\n </block>\n </" + compName + ">\n</template>\n "; | ||
} | ||
@@ -988,10 +1015,15 @@ }); | ||
UnRecursiveTemplate.prototype.buildXSTmplName = function buildXSTmplName () { | ||
var comps = Array.from(this.nestElements.keys()).concat( Array.from(this.componentConfig.thirdPartyComponents.keys()) | ||
var isLoopComps = Array.from(this.nestElements.keys()).concat( Array.from(this.componentConfig.thirdPartyComponents.keys()) | ||
); | ||
var isLoopCompsSet = new Set(isLoopComps); | ||
var hasMaxComps = []; | ||
this.nestElements.forEach(function (max, comp) { | ||
if (max > -1) | ||
{ hasMaxComps.push(comp); } | ||
if (max > 1) { | ||
hasMaxComps.push(comp); | ||
} | ||
else if (max === 1 && isLoopCompsSet.has(comp)) { | ||
isLoopCompsSet.delete(comp); | ||
} | ||
}); | ||
return ("function (l, n, s) {\n var a = " + (JSON.stringify(comps)) + "\n var b = " + (JSON.stringify(hasMaxComps)) + "\n if (a.indexOf(n) === -1) {\n l = 0\n }\n if (b.indexOf(n) > -1) {\n var u = s.split(',')\n var depth = 0\n for (var i = 0; i < u.length; i++) {\n if (u[i] === n) depth++\n }\n l = depth\n }\n return 'tmpl_' + l + '_' + n\n }"); | ||
return ("function (l, n, s) {\n var a = " + (JSON.stringify(Array.from(isLoopCompsSet))) + "\n var b = " + (JSON.stringify(hasMaxComps)) + "\n if (a.indexOf(n) === -1) {\n l = 0\n }\n if (b.indexOf(n) > -1) {\n var u = s.split(',')\n var depth = 0\n for (var i = 0; i < u.length; i++) {\n if (u[i] === n) depth++\n }\n l = depth\n }\n return 'tmpl_' + l + '_' + n\n }"); | ||
}; | ||
@@ -1001,3 +1033,3 @@ UnRecursiveTemplate.prototype.buildXSTmpExtra = function buildXSTmpExtra () { | ||
this.nestElements.forEach(function (max, comp) { | ||
if (max > -1) | ||
if (max > 1) | ||
{ hasMaxComps.push(comp); } | ||
@@ -1013,2 +1045,379 @@ }); | ||
var noPromiseApis = new Set([ | ||
'clearStorageSync', | ||
'getBatteryInfoSync', | ||
'getExtConfigSync', | ||
'getFileSystemManager', | ||
'getLaunchOptionsSync', | ||
'getStorageInfoSync', | ||
'getStorageSync', | ||
'getSystemInfoSync', | ||
'offAccelerometerChange', | ||
'offAppHide', | ||
'offAppShow', | ||
'offAudioInterruptionBegin', | ||
'offAudioInterruptionEnd', | ||
'offBLECharacteristicValueChange', | ||
'offBLEConnectionStateChange', | ||
'offBluetoothAdapterStateChange', | ||
'offBluetoothDeviceFound', | ||
'offCompassChange', | ||
'offError', | ||
'offGetWifiList', | ||
'offGyroscopeChange', | ||
'offMemoryWarning', | ||
'offNetworkStatusChange', | ||
'offPageNotFound', | ||
'offUnhandledRejection', | ||
'offUserCaptureScreen', | ||
'onAccelerometerChange', | ||
'onAppHide', | ||
'onAppShow', | ||
'onAudioInterruptionBegin', | ||
'onAudioInterruptionEnd', | ||
'onBLECharacteristicValueChange', | ||
'onBLEConnectionStateChange', | ||
'onBeaconServiceChange', | ||
'onBeaconUpdate', | ||
'onBluetoothAdapterStateChange', | ||
'onBluetoothDeviceFound', | ||
'onCompassChange', | ||
'onDeviceMotionChange', | ||
'onError', | ||
'onGetWifiList', | ||
'onGyroscopeChange', | ||
'onMemoryWarning', | ||
'onNetworkStatusChange', | ||
'onPageNotFound', | ||
'onSocketClose', | ||
'onSocketError', | ||
'onSocketMessage', | ||
'onSocketOpen', | ||
'onUnhandledRejection', | ||
'onUserCaptureScreen', | ||
'removeStorageSync', | ||
'reportAnalytics', | ||
'setStorageSync', | ||
'arrayBufferToBase64', | ||
'base64ToArrayBuffer', | ||
'canIUse', | ||
'createAnimation', | ||
'createCameraContext', | ||
'createCanvasContext', | ||
'createInnerAudioContext', | ||
'createIntersectionObserver', | ||
'createInterstitialAd', | ||
'createLivePlayerContext', | ||
'createMapContext', | ||
'createSelectorQuery', | ||
'createVideoContext', | ||
'getBackgroundAudioManager', | ||
'getMenuButtonBoundingClientRect', | ||
'getRecorderManager', | ||
'getUpdateManager' | ||
]); | ||
var needPromiseApis = new Set([ | ||
'addPhoneContact', | ||
'authorize', | ||
'canvasGetImageData', | ||
'canvasPutImageData', | ||
'canvasToTempFilePath', | ||
'checkSession', | ||
'chooseAddress', | ||
'chooseImage', | ||
'chooseInvoiceTitle', | ||
'chooseLocation', | ||
'chooseVideo', | ||
'clearStorage', | ||
'closeBLEConnection', | ||
'closeBluetoothAdapter', | ||
'closeSocket', | ||
'compressImage', | ||
'connectSocket', | ||
'createBLEConnection', | ||
'downloadFile', | ||
'getAvailableAudioSources', | ||
'getBLEDeviceCharacteristics', | ||
'getBLEDeviceServices', | ||
'getBatteryInfo', | ||
'getBeacons', | ||
'getBluetoothAdapterState', | ||
'getBluetoothDevices', | ||
'getClipboardData', | ||
'getConnectedBluetoothDevices', | ||
'getConnectedWifi', | ||
'getExtConfig', | ||
'getFileInfo', | ||
'getImageInfo', | ||
'getLocation', | ||
'getNetworkType', | ||
'getSavedFileInfo', | ||
'getSavedFileList', | ||
'getScreenBrightness', | ||
'getSetting', | ||
'getStorage', | ||
'getStorageInfo', | ||
'getSystemInfo', | ||
'getUserInfo', | ||
'getWifiList', | ||
'hideHomeButton', | ||
'hideShareMenu', | ||
'hideTabBar', | ||
'hideTabBarRedDot', | ||
'loadFontFace', | ||
'login', | ||
'makePhoneCall', | ||
'navigateBack', | ||
'navigateBackMiniProgram', | ||
'navigateTo', | ||
'navigateToBookshelf', | ||
'navigateToMiniProgram', | ||
'notifyBLECharacteristicValueChange', | ||
'hideKeyboard', | ||
'hideLoading', | ||
'hideNavigationBarLoading', | ||
'hideToast', | ||
'openBluetoothAdapter', | ||
'openDocument', | ||
'openLocation', | ||
'openSetting', | ||
'pageScrollTo', | ||
'previewImage', | ||
'queryBookshelf', | ||
'reLaunch', | ||
'readBLECharacteristicValue', | ||
'redirectTo', | ||
'removeSavedFile', | ||
'removeStorage', | ||
'removeTabBarBadge', | ||
'requestSubscribeMessage', | ||
'saveFile', | ||
'saveImageToPhotosAlbum', | ||
'saveVideoToPhotosAlbum', | ||
'scanCode', | ||
'sendSocketMessage', | ||
'setBackgroundColor', | ||
'setBackgroundTextStyle', | ||
'setClipboardData', | ||
'setEnableDebug', | ||
'setInnerAudioOption', | ||
'setKeepScreenOn', | ||
'setNavigationBarColor', | ||
'setNavigationBarTitle', | ||
'setScreenBrightness', | ||
'setStorage', | ||
'setTabBarBadge', | ||
'setTabBarItem', | ||
'setTabBarStyle', | ||
'showActionSheet', | ||
'showFavoriteGuide', | ||
'showLoading', | ||
'showModal', | ||
'showShareMenu', | ||
'showTabBar', | ||
'showTabBarRedDot', | ||
'showToast', | ||
'startBeaconDiscovery', | ||
'startBluetoothDevicesDiscovery', | ||
'startDeviceMotionListening', | ||
'startPullDownRefresh', | ||
'stopBeaconDiscovery', | ||
'stopBluetoothDevicesDiscovery', | ||
'stopCompass', | ||
'startCompass', | ||
'startAccelerometer', | ||
'stopAccelerometer', | ||
'showNavigationBarLoading', | ||
'stopDeviceMotionListening', | ||
'stopPullDownRefresh', | ||
'switchTab', | ||
'uploadFile', | ||
'vibrateLong', | ||
'vibrateShort', | ||
'writeBLECharacteristicValue' | ||
]); | ||
function getCanIUseWebp(taro) { | ||
return function () { | ||
if (typeof taro.getSystemInfoSync !== 'function') { | ||
console.error('不支持 API canIUseWebp'); | ||
return false; | ||
} | ||
var ref = taro.getSystemInfoSync(); | ||
var platform = ref.platform; | ||
var platformLower = platform.toLowerCase(); | ||
if (platformLower === 'android' || platformLower === 'devtools') { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
} | ||
function getNormalRequest(global) { | ||
return function request(options) { | ||
options = options || {}; | ||
if (typeof options === 'string') { | ||
options = { | ||
url: options | ||
}; | ||
} | ||
var originSuccess = options.success; | ||
var originFail = options.fail; | ||
var originComplete = options.complete; | ||
var requestTask; | ||
var p = new Promise(function (resolve, reject) { | ||
options.success = function (res) { | ||
originSuccess && originSuccess(res); | ||
resolve(res); | ||
}; | ||
options.fail = function (res) { | ||
originFail && originFail(res); | ||
reject(res); | ||
}; | ||
options.complete = function (res) { | ||
originComplete && originComplete(res); | ||
}; | ||
requestTask = global.request(options); | ||
}); | ||
p.abort = function (cb) { | ||
cb && cb(); | ||
if (requestTask) { | ||
requestTask.abort(); | ||
} | ||
return p; | ||
}; | ||
return p; | ||
}; | ||
} | ||
function processApis(taro, global, config) { | ||
if ( config === void 0 ) config = {}; | ||
var patchNoPromiseApis = config.noPromiseApis || []; | ||
var patchNeedPromiseApis = config.needPromiseApis || []; | ||
var _noPromiseApis = new Set(patchNoPromiseApis.concat( noPromiseApis)); | ||
var _needPromiseApis = new Set(patchNeedPromiseApis.concat( needPromiseApis)); | ||
var apis = _noPromiseApis.concat( _needPromiseApis); | ||
apis.forEach(function (key) { | ||
if (_needPromiseApis.has(key)) { | ||
var originKey = key; | ||
taro[originKey] = function (options) { | ||
if ( options === void 0 ) options = {}; | ||
var args = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 1 ]; | ||
var key = originKey; | ||
// 第一个参数 options 为字符串,单独处理 | ||
if (typeof options === 'string') { | ||
if (args.length) { | ||
return global[key].apply(global, [ options ].concat( args )); | ||
} | ||
return global[key](options); | ||
} | ||
// 改变 key 或 option 字段,如需要把支付宝标准的字段对齐微信标准的字段 | ||
if (config.transformMeta) { | ||
var transformResult = config.transformMeta(key, options); | ||
key = transformResult.key; | ||
options = transformResult.options; | ||
// 新 key 可能不存在 | ||
if (!global.hasOwnProperty(key)) { | ||
return unsupport(key)(); | ||
} | ||
} | ||
var task = null; | ||
var obj = Object.assign({}, options); | ||
// 为页面跳转相关的 API 设置一个随机数作为路由参数。为了给 runtime 区分页面。 | ||
setUniqueKeyToRoute(key, options); | ||
// Promise 化 | ||
var p = new Promise(function (resolve, reject) { | ||
obj.success = function (res) { | ||
var _a, _b; | ||
(_a = config.modifyAsyncResult) === null || _a === void 0 ? void 0 : _a.call(config, key, res); | ||
(_b = options.success) === null || _b === void 0 ? void 0 : _b.call(options, res); | ||
if (key === 'connectSocket') { | ||
resolve(Promise.resolve().then(function () { return Object.assign(task, res); })); | ||
} | ||
else { | ||
resolve(res); | ||
} | ||
}; | ||
obj.fail = function (res) { | ||
var _a; | ||
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, res); | ||
reject(res); | ||
}; | ||
obj.complete = function (res) { | ||
var _a; | ||
(_a = options.complete) === null || _a === void 0 ? void 0 : _a.call(options, res); | ||
}; | ||
if (args.length) { | ||
task = global[key].apply(global, [ obj ].concat( args )); | ||
} | ||
else { | ||
task = global[key](obj); | ||
} | ||
}); | ||
// 给 promise 对象挂载属性 | ||
if (key === 'uploadFile' || key === 'downloadFile') { | ||
p.progress = function (cb) { | ||
task === null || task === void 0 ? void 0 : task.onProgressUpdate(cb); | ||
return p; | ||
}; | ||
p.abort = function (cb) { | ||
cb === null || cb === void 0 ? void 0 : cb(); | ||
task === null || task === void 0 ? void 0 : task.abort(); | ||
return p; | ||
}; | ||
} | ||
return p; | ||
}; | ||
} | ||
else { | ||
// API 不存在 | ||
if (!global.hasOwnProperty(key)) { | ||
taro[key] = unsupport(key); | ||
return; | ||
} | ||
taro[key] = function () { | ||
var args = [], len = arguments.length; | ||
while ( len-- ) args[ len ] = arguments[ len ]; | ||
if (config.handleSyncApis) { | ||
return config.handleSyncApis(key, global, args); | ||
} | ||
else { | ||
return global[key].apply(global, args); | ||
} | ||
}; | ||
} | ||
}); | ||
equipCommonApis(taro, global, config); | ||
} | ||
/** | ||
* 挂载常用 API | ||
* @param taro Taro 对象 | ||
* @param global 小程序全局对象,如微信的 wx,支付宝的 my | ||
*/ | ||
function equipCommonApis(taro, global, apis) { | ||
if ( apis === void 0 ) apis = {}; | ||
taro.canIUseWebp = getCanIUseWebp(taro); | ||
taro.getCurrentPages = getCurrentPages || unsupport('getCurrentPages'); | ||
taro.getApp = getApp || unsupport('getApp'); | ||
taro.env = global.env || {}; | ||
try { | ||
taro.requirePlugin = requirePlugin || unsupport('requirePlugin'); | ||
} | ||
catch (error) { | ||
taro.requirePlugin = unsupport('requirePlugin'); | ||
} | ||
// request & interceptors | ||
var request = apis.request ? apis.request : getNormalRequest(global); | ||
function taroInterceptor(chain) { | ||
return request(chain.requestParams); | ||
} | ||
var link = new taro.Link(taroInterceptor); | ||
taro.request = link.request.bind(link); | ||
taro.addInterceptor = link.addInterceptor.bind(link); | ||
taro.cleanInterceptors = link.cleanInterceptors.bind(link); | ||
taro.miniGlobal = global; | ||
} | ||
exports.BaseTemplate = BaseTemplate; | ||
@@ -1046,3 +1455,5 @@ exports.EMPTY_ARR = EMPTY_ARR; | ||
exports.noop = noop; | ||
exports.processApis = processApis; | ||
exports.queryToJson = queryToJson; | ||
exports.setUniqueKeyToRoute = setUniqueKeyToRoute; | ||
exports.singleQuote = singleQuote; | ||
@@ -1056,4 +1467,5 @@ exports.specialEvents = specialEvents; | ||
exports.unbox = unbox; | ||
exports.unsupport = unsupport; | ||
exports.voidElements = voidElements; | ||
exports.warn = warn; | ||
//# sourceMappingURL=index.js.map |
@@ -549,2 +549,22 @@ function isString(o) { | ||
} | ||
function unsupport(api) { | ||
return function () { | ||
console.warn(`小程序暂不支持 ${api}`); | ||
}; | ||
} | ||
function setUniqueKeyToRoute(key, obj) { | ||
const routerParamsPrivateKey = '__key_'; | ||
const useDataCacheApis = [ | ||
'navigateTo', | ||
'redirectTo', | ||
'reLaunch', | ||
'switchTab' | ||
]; | ||
if (useDataCacheApis.indexOf(key) > -1) { | ||
const url = obj.url = obj.url || ''; | ||
const hasMark = url.indexOf('?') > -1; | ||
const cacheKey = getUniqueKey(); | ||
obj.url += (hasMark ? '&' : '?') + `${routerParamsPrivateKey}=${cacheKey}`; | ||
} | ||
} | ||
@@ -782,3 +802,3 @@ /** | ||
const nextLevel = isSupportRecursive ? 0 : level + 1; | ||
const data = !this.isSupportRecursive | ||
const data = !this.isSupportRecursive && this.supportXS | ||
? `${this.dataKeymap('i:item,l:l')}` | ||
@@ -788,3 +808,3 @@ : this.dataKeymap('i:item'); | ||
? `<template is="{{xs.e(${isSupportRecursive ? 0 : 'cid+1'})}}" data="{{${data}}}" />` | ||
: `<template is="tmpl_${nextLevel}_${"container" /* Container */}" data="{{${this.dataKeymap('i:item')}}}" />`; | ||
: `<template is="tmpl_${nextLevel}_${"container" /* Container */}" data="{{${data}}}" />`; | ||
if (isFunction(this.modifyLoopBody)) { | ||
@@ -840,6 +860,6 @@ child = this.modifyLoopBody(child, comp.nodeName); | ||
buildThirdPartyTemplate(level, componentConfig) { | ||
const { Adapter, isSupportRecursive } = this; | ||
const { Adapter, isSupportRecursive, supportXS, nestElements } = this; | ||
const nextLevel = isSupportRecursive ? 0 : level + 1; | ||
let template = ''; | ||
const data = !this.isSupportRecursive && this.supportXS | ||
const data = !isSupportRecursive && supportXS | ||
? `${this.dataKeymap('i:item,l:l')}` | ||
@@ -857,2 +877,7 @@ : this.dataKeymap('i:item'); | ||
else { | ||
if (!isSupportRecursive && supportXS && nestElements.has(compName) && level + 1 > nestElements.get(compName)) | ||
return; | ||
const child = supportXS | ||
? `<template is="{{xs.e(${isSupportRecursive ? 0 : 'cid+1'})}}" data="{{${data}}}" />` | ||
: `<template is="tmpl_${nextLevel}_${"container" /* Container */}" data="{{${data}}}" />`; | ||
template += ` | ||
@@ -862,3 +887,3 @@ <template name="tmpl_${level}_${compName}"> | ||
<block ${Adapter.for}="{{i.${"cn" /* Childnodes */}}}" ${Adapter.key}="uid"> | ||
<template is="tmpl_${nextLevel}_${"container" /* Container */}" data="{{${data}}}" /> | ||
${child} | ||
</block> | ||
@@ -1012,13 +1037,18 @@ </${compName}> | ||
buildXSTmplName() { | ||
const comps = [ | ||
const isLoopComps = [ | ||
...Array.from(this.nestElements.keys()), | ||
...Array.from(this.componentConfig.thirdPartyComponents.keys()) | ||
]; | ||
const isLoopCompsSet = new Set(isLoopComps); | ||
const hasMaxComps = []; | ||
this.nestElements.forEach((max, comp) => { | ||
if (max > -1) | ||
if (max > 1) { | ||
hasMaxComps.push(comp); | ||
} | ||
else if (max === 1 && isLoopCompsSet.has(comp)) { | ||
isLoopCompsSet.delete(comp); | ||
} | ||
}); | ||
return `function (l, n, s) { | ||
var a = ${JSON.stringify(comps)} | ||
var a = ${JSON.stringify(Array.from(isLoopCompsSet))} | ||
var b = ${JSON.stringify(hasMaxComps)} | ||
@@ -1042,3 +1072,3 @@ if (a.indexOf(n) === -1) { | ||
this.nestElements.forEach((max, comp) => { | ||
if (max > -1) | ||
if (max > 1) | ||
hasMaxComps.push(comp); | ||
@@ -1057,3 +1087,368 @@ }); | ||
export { BaseTemplate, EMPTY_ARR, EMPTY_OBJ, RecursiveTemplate, UnRecursiveTemplate, animationEvents, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, defaultReconciler, ensure, events, focusComponents, getUniqueKey, hasOwn, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, noop, queryToJson, singleQuote, specialEvents, styles, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, voidElements, warn }; | ||
const noPromiseApis = new Set([ | ||
'clearStorageSync', | ||
'getBatteryInfoSync', | ||
'getExtConfigSync', | ||
'getFileSystemManager', | ||
'getLaunchOptionsSync', | ||
'getStorageInfoSync', | ||
'getStorageSync', | ||
'getSystemInfoSync', | ||
'offAccelerometerChange', | ||
'offAppHide', | ||
'offAppShow', | ||
'offAudioInterruptionBegin', | ||
'offAudioInterruptionEnd', | ||
'offBLECharacteristicValueChange', | ||
'offBLEConnectionStateChange', | ||
'offBluetoothAdapterStateChange', | ||
'offBluetoothDeviceFound', | ||
'offCompassChange', | ||
'offError', | ||
'offGetWifiList', | ||
'offGyroscopeChange', | ||
'offMemoryWarning', | ||
'offNetworkStatusChange', | ||
'offPageNotFound', | ||
'offUnhandledRejection', | ||
'offUserCaptureScreen', | ||
'onAccelerometerChange', | ||
'onAppHide', | ||
'onAppShow', | ||
'onAudioInterruptionBegin', | ||
'onAudioInterruptionEnd', | ||
'onBLECharacteristicValueChange', | ||
'onBLEConnectionStateChange', | ||
'onBeaconServiceChange', | ||
'onBeaconUpdate', | ||
'onBluetoothAdapterStateChange', | ||
'onBluetoothDeviceFound', | ||
'onCompassChange', | ||
'onDeviceMotionChange', | ||
'onError', | ||
'onGetWifiList', | ||
'onGyroscopeChange', | ||
'onMemoryWarning', | ||
'onNetworkStatusChange', | ||
'onPageNotFound', | ||
'onSocketClose', | ||
'onSocketError', | ||
'onSocketMessage', | ||
'onSocketOpen', | ||
'onUnhandledRejection', | ||
'onUserCaptureScreen', | ||
'removeStorageSync', | ||
'reportAnalytics', | ||
'setStorageSync', | ||
'arrayBufferToBase64', | ||
'base64ToArrayBuffer', | ||
'canIUse', | ||
'createAnimation', | ||
'createCameraContext', | ||
'createCanvasContext', | ||
'createInnerAudioContext', | ||
'createIntersectionObserver', | ||
'createInterstitialAd', | ||
'createLivePlayerContext', | ||
'createMapContext', | ||
'createSelectorQuery', | ||
'createVideoContext', | ||
'getBackgroundAudioManager', | ||
'getMenuButtonBoundingClientRect', | ||
'getRecorderManager', | ||
'getUpdateManager' | ||
]); | ||
const needPromiseApis = new Set([ | ||
'addPhoneContact', | ||
'authorize', | ||
'canvasGetImageData', | ||
'canvasPutImageData', | ||
'canvasToTempFilePath', | ||
'checkSession', | ||
'chooseAddress', | ||
'chooseImage', | ||
'chooseInvoiceTitle', | ||
'chooseLocation', | ||
'chooseVideo', | ||
'clearStorage', | ||
'closeBLEConnection', | ||
'closeBluetoothAdapter', | ||
'closeSocket', | ||
'compressImage', | ||
'connectSocket', | ||
'createBLEConnection', | ||
'downloadFile', | ||
'getAvailableAudioSources', | ||
'getBLEDeviceCharacteristics', | ||
'getBLEDeviceServices', | ||
'getBatteryInfo', | ||
'getBeacons', | ||
'getBluetoothAdapterState', | ||
'getBluetoothDevices', | ||
'getClipboardData', | ||
'getConnectedBluetoothDevices', | ||
'getConnectedWifi', | ||
'getExtConfig', | ||
'getFileInfo', | ||
'getImageInfo', | ||
'getLocation', | ||
'getNetworkType', | ||
'getSavedFileInfo', | ||
'getSavedFileList', | ||
'getScreenBrightness', | ||
'getSetting', | ||
'getStorage', | ||
'getStorageInfo', | ||
'getSystemInfo', | ||
'getUserInfo', | ||
'getWifiList', | ||
'hideHomeButton', | ||
'hideShareMenu', | ||
'hideTabBar', | ||
'hideTabBarRedDot', | ||
'loadFontFace', | ||
'login', | ||
'makePhoneCall', | ||
'navigateBack', | ||
'navigateBackMiniProgram', | ||
'navigateTo', | ||
'navigateToBookshelf', | ||
'navigateToMiniProgram', | ||
'notifyBLECharacteristicValueChange', | ||
'hideKeyboard', | ||
'hideLoading', | ||
'hideNavigationBarLoading', | ||
'hideToast', | ||
'openBluetoothAdapter', | ||
'openDocument', | ||
'openLocation', | ||
'openSetting', | ||
'pageScrollTo', | ||
'previewImage', | ||
'queryBookshelf', | ||
'reLaunch', | ||
'readBLECharacteristicValue', | ||
'redirectTo', | ||
'removeSavedFile', | ||
'removeStorage', | ||
'removeTabBarBadge', | ||
'requestSubscribeMessage', | ||
'saveFile', | ||
'saveImageToPhotosAlbum', | ||
'saveVideoToPhotosAlbum', | ||
'scanCode', | ||
'sendSocketMessage', | ||
'setBackgroundColor', | ||
'setBackgroundTextStyle', | ||
'setClipboardData', | ||
'setEnableDebug', | ||
'setInnerAudioOption', | ||
'setKeepScreenOn', | ||
'setNavigationBarColor', | ||
'setNavigationBarTitle', | ||
'setScreenBrightness', | ||
'setStorage', | ||
'setTabBarBadge', | ||
'setTabBarItem', | ||
'setTabBarStyle', | ||
'showActionSheet', | ||
'showFavoriteGuide', | ||
'showLoading', | ||
'showModal', | ||
'showShareMenu', | ||
'showTabBar', | ||
'showTabBarRedDot', | ||
'showToast', | ||
'startBeaconDiscovery', | ||
'startBluetoothDevicesDiscovery', | ||
'startDeviceMotionListening', | ||
'startPullDownRefresh', | ||
'stopBeaconDiscovery', | ||
'stopBluetoothDevicesDiscovery', | ||
'stopCompass', | ||
'startCompass', | ||
'startAccelerometer', | ||
'stopAccelerometer', | ||
'showNavigationBarLoading', | ||
'stopDeviceMotionListening', | ||
'stopPullDownRefresh', | ||
'switchTab', | ||
'uploadFile', | ||
'vibrateLong', | ||
'vibrateShort', | ||
'writeBLECharacteristicValue' | ||
]); | ||
function getCanIUseWebp(taro) { | ||
return function () { | ||
if (typeof taro.getSystemInfoSync !== 'function') { | ||
console.error('不支持 API canIUseWebp'); | ||
return false; | ||
} | ||
const { platform } = taro.getSystemInfoSync(); | ||
const platformLower = platform.toLowerCase(); | ||
if (platformLower === 'android' || platformLower === 'devtools') { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
} | ||
function getNormalRequest(global) { | ||
return function request(options) { | ||
options = options || {}; | ||
if (typeof options === 'string') { | ||
options = { | ||
url: options | ||
}; | ||
} | ||
const originSuccess = options.success; | ||
const originFail = options.fail; | ||
const originComplete = options.complete; | ||
let requestTask; | ||
const p = new Promise((resolve, reject) => { | ||
options.success = res => { | ||
originSuccess && originSuccess(res); | ||
resolve(res); | ||
}; | ||
options.fail = res => { | ||
originFail && originFail(res); | ||
reject(res); | ||
}; | ||
options.complete = res => { | ||
originComplete && originComplete(res); | ||
}; | ||
requestTask = global.request(options); | ||
}); | ||
p.abort = (cb) => { | ||
cb && cb(); | ||
if (requestTask) { | ||
requestTask.abort(); | ||
} | ||
return p; | ||
}; | ||
return p; | ||
}; | ||
} | ||
function processApis(taro, global, config = {}) { | ||
const patchNoPromiseApis = config.noPromiseApis || []; | ||
const patchNeedPromiseApis = config.needPromiseApis || []; | ||
const _noPromiseApis = new Set([...patchNoPromiseApis, ...noPromiseApis]); | ||
const _needPromiseApis = new Set([...patchNeedPromiseApis, ...needPromiseApis]); | ||
const apis = [..._noPromiseApis, ..._needPromiseApis]; | ||
apis.forEach(key => { | ||
if (_needPromiseApis.has(key)) { | ||
const originKey = key; | ||
taro[originKey] = (options = {}, ...args) => { | ||
let key = originKey; | ||
// 第一个参数 options 为字符串,单独处理 | ||
if (typeof options === 'string') { | ||
if (args.length) { | ||
return global[key](options, ...args); | ||
} | ||
return global[key](options); | ||
} | ||
// 改变 key 或 option 字段,如需要把支付宝标准的字段对齐微信标准的字段 | ||
if (config.transformMeta) { | ||
const transformResult = config.transformMeta(key, options); | ||
key = transformResult.key; | ||
options = transformResult.options; | ||
// 新 key 可能不存在 | ||
if (!global.hasOwnProperty(key)) { | ||
return unsupport(key)(); | ||
} | ||
} | ||
let task = null; | ||
const obj = Object.assign({}, options); | ||
// 为页面跳转相关的 API 设置一个随机数作为路由参数。为了给 runtime 区分页面。 | ||
setUniqueKeyToRoute(key, options); | ||
// Promise 化 | ||
const p = new Promise((resolve, reject) => { | ||
obj.success = res => { | ||
var _a, _b; | ||
(_a = config.modifyAsyncResult) === null || _a === void 0 ? void 0 : _a.call(config, key, res); | ||
(_b = options.success) === null || _b === void 0 ? void 0 : _b.call(options, res); | ||
if (key === 'connectSocket') { | ||
resolve(Promise.resolve().then(() => Object.assign(task, res))); | ||
} | ||
else { | ||
resolve(res); | ||
} | ||
}; | ||
obj.fail = res => { | ||
var _a; | ||
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, res); | ||
reject(res); | ||
}; | ||
obj.complete = res => { | ||
var _a; | ||
(_a = options.complete) === null || _a === void 0 ? void 0 : _a.call(options, res); | ||
}; | ||
if (args.length) { | ||
task = global[key](obj, ...args); | ||
} | ||
else { | ||
task = global[key](obj); | ||
} | ||
}); | ||
// 给 promise 对象挂载属性 | ||
if (key === 'uploadFile' || key === 'downloadFile') { | ||
p.progress = cb => { | ||
task === null || task === void 0 ? void 0 : task.onProgressUpdate(cb); | ||
return p; | ||
}; | ||
p.abort = cb => { | ||
cb === null || cb === void 0 ? void 0 : cb(); | ||
task === null || task === void 0 ? void 0 : task.abort(); | ||
return p; | ||
}; | ||
} | ||
return p; | ||
}; | ||
} | ||
else { | ||
// API 不存在 | ||
if (!global.hasOwnProperty(key)) { | ||
taro[key] = unsupport(key); | ||
return; | ||
} | ||
taro[key] = (...args) => { | ||
if (config.handleSyncApis) { | ||
return config.handleSyncApis(key, global, args); | ||
} | ||
else { | ||
return global[key].apply(global, args); | ||
} | ||
}; | ||
} | ||
}); | ||
equipCommonApis(taro, global, config); | ||
} | ||
/** | ||
* 挂载常用 API | ||
* @param taro Taro 对象 | ||
* @param global 小程序全局对象,如微信的 wx,支付宝的 my | ||
*/ | ||
function equipCommonApis(taro, global, apis = {}) { | ||
taro.canIUseWebp = getCanIUseWebp(taro); | ||
taro.getCurrentPages = getCurrentPages || unsupport('getCurrentPages'); | ||
taro.getApp = getApp || unsupport('getApp'); | ||
taro.env = global.env || {}; | ||
try { | ||
taro.requirePlugin = requirePlugin || unsupport('requirePlugin'); | ||
} | ||
catch (error) { | ||
taro.requirePlugin = unsupport('requirePlugin'); | ||
} | ||
// request & interceptors | ||
const request = apis.request ? apis.request : getNormalRequest(global); | ||
function taroInterceptor(chain) { | ||
return request(chain.requestParams); | ||
} | ||
const link = new taro.Link(taroInterceptor); | ||
taro.request = link.request.bind(link); | ||
taro.addInterceptor = link.addInterceptor.bind(link); | ||
taro.cleanInterceptors = link.cleanInterceptors.bind(link); | ||
taro.miniGlobal = global; | ||
} | ||
export { BaseTemplate, EMPTY_ARR, EMPTY_OBJ, RecursiveTemplate, UnRecursiveTemplate, animationEvents, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, defaultReconciler, ensure, events, focusComponents, getUniqueKey, hasOwn, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, noop, processApis, queryToJson, setUniqueKeyToRoute, singleQuote, specialEvents, styles, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, unsupport, voidElements, warn }; | ||
//# sourceMappingURL=shared.esm.js.map |
@@ -14,3 +14,2 @@ /** | ||
*/ | ||
import { internalComponents } from './components'; | ||
interface Component { | ||
@@ -78,3 +77,3 @@ nodeName: string; | ||
buildXScript: () => string; | ||
mergeComponents(ctx: any, patch: typeof internalComponents): void; | ||
mergeComponents(ctx: any, patch: Record<string, Record<string, string>>): void; | ||
protected buildXSTmplName(): string; | ||
@@ -81,0 +80,0 @@ protected buildXSTmpExtra(): string; |
@@ -51,1 +51,3 @@ export declare const EMPTY_OBJ: any; | ||
export declare function mergeReconciler(hostConfig: any): void; | ||
export declare function unsupport(api: any): () => void; | ||
export declare function setUniqueKeyToRoute(key: string, obj: any): void; |
{ | ||
"name": "@tarojs/shared", | ||
"version": "3.1.0-beta.5", | ||
"version": "3.1.0-beta.6", | ||
"description": "> TODO: description", | ||
@@ -28,3 +28,3 @@ "author": "yuche <i@yuche.me>", | ||
}, | ||
"gitHead": "571ff27906e15fe6af38ef35016c47310fe768a3" | ||
"gitHead": "3918cd94d45eda6179afd0aa83a5b008edd749e3" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
273998
14
3101