vOn.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.STRINGIFY_JSON = exports.ATTR_DATASET_EVENT_OPTS = exports.addEventOpts = exports.createCustomEventExpr = exports.createTransformOn = exports.defaultMatch = void 0;
  4. const uni_shared_1 = require("@dcloudio/uni-shared");
  5. const compiler_core_1 = require("@vue/compiler-core");
  6. const utils_1 = require("../utils");
  7. function defaultMatch(name, node, context) {
  8. return isCustomEvent(name) && (0, utils_1.isUserComponent)(node, context);
  9. }
  10. exports.defaultMatch = defaultMatch;
  11. /**
  12. * 百度、快手小程序的自定义组件,不支持动态事件绑定,故转换为静态事件 + dataset
  13. * @param baseTransformOn
  14. * @returns
  15. */
  16. function createTransformOn(baseTransformOn, { match } = {
  17. match: defaultMatch,
  18. }) {
  19. return (dir, node, context, augmentor) => {
  20. const res = baseTransformOn(dir, node, context, augmentor);
  21. const { name, arg, exp } = dir;
  22. if (name !== 'on' || !arg || !exp || !(0, compiler_core_1.isStaticExp)(arg)) {
  23. return res;
  24. }
  25. if (!match(arg.content, node, context)) {
  26. return res;
  27. }
  28. const value = res.props[0].value;
  29. res.props[0].value = createCustomEventExpr();
  30. addEventOpts(node.tagType === 1 /* ElementTypes.COMPONENT */
  31. ? (0, uni_shared_1.customizeEvent)(arg.content)
  32. : arg.content, value, node, context);
  33. return res;
  34. };
  35. }
  36. exports.createTransformOn = createTransformOn;
  37. function createCustomEventExpr() {
  38. return (0, compiler_core_1.createSimpleExpression)('__e', true);
  39. }
  40. exports.createCustomEventExpr = createCustomEventExpr;
  41. function addEventOpts(event, value, node, context) {
  42. const attrName = node.tagType === 1 /* ElementTypes.COMPONENT */
  43. ? ATTR_DATA_EVENT_OPTS
  44. : exports.ATTR_DATASET_EVENT_OPTS;
  45. const opts = (0, compiler_core_1.findProp)(node, attrName, true);
  46. if (!opts) {
  47. node.props.push(createDataEventOptsProp(attrName, event, value, context));
  48. }
  49. else {
  50. const children = opts.exp.children;
  51. children.splice(children.length - 2, 0, createDataEventOptsProperty(event, value));
  52. }
  53. }
  54. exports.addEventOpts = addEventOpts;
  55. const ATTR_DATA_EVENT_OPTS = 'eO';
  56. exports.ATTR_DATASET_EVENT_OPTS = 'data-e-o';
  57. function createDataEventOptsProperty(event, exp) {
  58. return (0, compiler_core_1.createCompoundExpression)([`'${event}'`, ': ', exp, ',']);
  59. }
  60. exports.STRINGIFY_JSON = Symbol(`stringifyJson`);
  61. function createDataEventOptsProp(name, event, exp, context) {
  62. const children = [];
  63. const stringify = name === ATTR_DATA_EVENT_OPTS;
  64. if (stringify) {
  65. children.push(context.helperString(exports.STRINGIFY_JSON) + '(');
  66. }
  67. children.push('{', createDataEventOptsProperty(event, exp), '}');
  68. if (stringify) {
  69. children.push(')');
  70. }
  71. return {
  72. type: 7 /* NodeTypes.DIRECTIVE */,
  73. name: 'bind',
  74. loc: compiler_core_1.locStub,
  75. modifiers: [],
  76. arg: (0, compiler_core_1.createSimpleExpression)(name, true),
  77. exp: (0, compiler_core_1.createCompoundExpression)(children),
  78. };
  79. }
  80. const builtInEvents = [
  81. '__l',
  82. 'tap',
  83. 'longtap',
  84. 'longpress',
  85. 'touchstart',
  86. 'touchmove',
  87. 'touchcancel',
  88. 'touchend',
  89. 'touchforcechange',
  90. 'transitionend',
  91. 'animationstart',
  92. 'animationiteration',
  93. 'animationend',
  94. ];
  95. function isCustomEvent(name) {
  96. return !builtInEvents.includes(name);
  97. }