UNPKG

@areslabs/alita-core

Version:

alita-core

120 lines (93 loc) 5.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = addEventHandler; var _ErrorLogTraverse = _interopRequireDefault(require("../util/ErrorLogTraverse")); var t = _interopRequireWildcard(require("@babel/types")); var _constants = require("../constants"); var _uast = require("../util/uast"); var _configure = _interopRequireDefault(require("../configure")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Copyright (c) Areslabs. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ function addEventHandler(ast) { (0, _ErrorLogTraverse.default)(ast, { exit: path => { if (path.type === 'JSXOpeningElement' && (0, _uast.getOriginal)(path)) { const original = (0, _uast.getOriginal)(path); const diuu = getDiuu(path.node.attributes); let hasEvent = false; path.node.attributes.forEach(attri => { if (attri.type === 'JSXAttribute' && attri.name.name === 'onPress') { attri.name.name = "catchtap"; attri.value = t.stringLiteral('eventHandler'); hasEvent = true; } if (attri.type === 'JSXAttribute' && attri.name.name === 'onLongPress') { attri.name.name = "catchlongpress"; attri.value = t.stringLiteral('eventHandler'); hasEvent = true; } // image的事件 if (attri.type === 'JSXAttribute' && attri.name.name === 'onLoad') { attri.name.name = "bindload"; attri.value = t.stringLiteral('eventHandler'); hasEvent = true; } if (attri.type === 'JSXAttribute' && attri.name.name === 'onError') { attri.name.name = "binderror"; attri.value = t.stringLiteral('eventHandler'); hasEvent = true; } }); if (!hasEvent) { // 没有事件绑定 return; } path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-diuu'), t.stringLiteral(`{{${diuu}}}`)), t.jsxAttribute(t.jsxIdentifier('hover-stop-propagation'))); if (original === _constants.touchableOpacityOrigin) { path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('hover-class'), t.stringLiteral(`{{${diuu}hoverClass}}`)), t.jsxAttribute(t.jsxIdentifier('hover-start-time'), t.stringLiteral("0")), t.jsxAttribute(t.jsxIdentifier('hover-stay-time'), t.stringLiteral("100"))); } if (original === _constants.touchableHighlightOrigin) { const jsxElement = path.parentPath.node; for (let i = 0; i < jsxElement.children.length; i++) { const child = jsxElement.children[i]; if (child.type === 'JSXElement') { child.openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('thFirst')), t.jsxAttribute(t.jsxIdentifier('hover-class'), t.stringLiteral(`{{${diuu}hoverClass}}`))); break; } } jsxElement.children.push(t.jsxText(`<view class="thFabricate" style="background-color: {{${diuu}underlayColor}}"/>`)); path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('hover-class'), t.stringLiteral(`thHover`)), t.jsxAttribute(t.jsxIdentifier('hover-start-time'), t.stringLiteral("0")), t.jsxAttribute(t.jsxIdentifier('hover-stay-time'), t.stringLiteral("100"))); } return; } /** * 当直接使用小程序组件 view/text/image等 */ if (path.type === 'JSXAttribute' && (path.node.name.name.startsWith('bind') || path.node.name.name.startsWith('catch')) && (_constants.wxBaseComp.has(path.parentPath.node.name.name) || _configure.default.configObj.miniprogramComponents[path.parentPath.node.name.name])) { path.node.value = t.stringLiteral('eventHandler'); if (!path.parentPath.node.attributes.some(attr => attr.type === 'JSXAttribute' && attr.name.name === 'data-diuu')) { const diuu = getDiuu(path.parentPath.node.attributes); path.parentPath.node.attributes.push(t.jsxAttribute(t.jsxIdentifier('data-diuu'), t.stringLiteral(`{{${diuu}}}`))); } } } }); return ast; } function getDiuu(attris) { for (let i = 0; i < attris.length; i++) { const item = attris[i]; if (item.type === 'JSXAttribute' && item.name.name === 'diuu') { return item.value.value; } } }