tools.umd.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Tools = {}));
  5. })(this, (function (exports) { 'use strict';
  6. !!(process.env.NODE_ENV !== "production") ? Object.freeze({}) : {};
  7. !!(process.env.NODE_ENV !== "production") ? Object.freeze([]) : [];
  8. const hasOwnProperty = Object.prototype.hasOwnProperty;
  9. const hasOwn = (val, key) => hasOwnProperty.call(val, key);
  10. const ACTION_TYPE_PAGE_CREATE = 1;
  11. const ACTION_TYPE_PAGE_CREATED = 2;
  12. const ACTION_TYPE_CREATE = 3;
  13. const ACTION_TYPE_INSERT = 4;
  14. const ACTION_TYPE_REMOVE = 5;
  15. const ACTION_TYPE_SET_ATTRIBUTE = 6;
  16. const ACTION_TYPE_REMOVE_ATTRIBUTE = 7;
  17. const ACTION_TYPE_ADD_EVENT = 8;
  18. const ACTION_TYPE_REMOVE_EVENT = 9;
  19. const ACTION_TYPE_SET_TEXT = 10;
  20. const ACTION_TYPE_ADD_WXS_EVENT = 12;
  21. const ACTION_TYPE_DICT = 0;
  22. function createGetDict(dict) {
  23. if (!dict.length) {
  24. return (v) => v;
  25. }
  26. const getDict = (value, includeValue = true) => {
  27. if (typeof value === 'number') {
  28. return dict[value];
  29. }
  30. const res = {};
  31. value.forEach(([n, v]) => {
  32. if (includeValue) {
  33. res[getDict(n)] = getDict(v);
  34. }
  35. else {
  36. res[getDict(n)] = v;
  37. }
  38. });
  39. return res;
  40. };
  41. return getDict;
  42. }
  43. function decodeActions(actions) {
  44. const [type, dict] = actions[0];
  45. if (type !== ACTION_TYPE_DICT) {
  46. return actions;
  47. }
  48. const getDict = createGetDict(dict);
  49. return actions.map((action) => {
  50. switch (action[0]) {
  51. case ACTION_TYPE_DICT:
  52. return action;
  53. case ACTION_TYPE_PAGE_CREATE:
  54. return decodePageCreateAction(action);
  55. case ACTION_TYPE_PAGE_CREATED:
  56. return decodePageCreatedAction(action);
  57. case ACTION_TYPE_CREATE:
  58. return decodeCreateAction(action, getDict);
  59. case ACTION_TYPE_INSERT:
  60. return decodeInsertAction(action, getDict);
  61. case ACTION_TYPE_REMOVE:
  62. return decodeRemoveAction(action);
  63. case ACTION_TYPE_SET_ATTRIBUTE:
  64. return decodeSetAttributeAction(action, getDict);
  65. case ACTION_TYPE_REMOVE_ATTRIBUTE:
  66. return decodeRemoveAttributeAction(action, getDict);
  67. case ACTION_TYPE_ADD_EVENT:
  68. return decodeAddEventAction(action, getDict);
  69. case ACTION_TYPE_ADD_WXS_EVENT:
  70. return decodeAddWxsEventAction(action, getDict);
  71. case ACTION_TYPE_REMOVE_EVENT:
  72. return decodeRemoveEventAction(action, getDict);
  73. case ACTION_TYPE_SET_TEXT:
  74. return decodeSetTextAction(action, getDict);
  75. }
  76. });
  77. }
  78. function decodePageCreateAction([, pageCreateData]) {
  79. return ['pageCreate', pageCreateData];
  80. }
  81. function decodePageCreatedAction([]) {
  82. return ['pageCreated'];
  83. }
  84. function decodeNodeJson(getDict, nodeJson) {
  85. if (!nodeJson) {
  86. return;
  87. }
  88. if (hasOwn(nodeJson, 'a')) {
  89. nodeJson.a = getDict(nodeJson.a);
  90. }
  91. if (hasOwn(nodeJson, 'e')) {
  92. nodeJson.e = getDict(nodeJson.e, false);
  93. }
  94. if (hasOwn(nodeJson, 'w')) {
  95. nodeJson.w = getWxsEventDict(nodeJson.w, getDict);
  96. }
  97. if (hasOwn(nodeJson, 's')) {
  98. nodeJson.s = getDict(nodeJson.s);
  99. }
  100. if (hasOwn(nodeJson, 't')) {
  101. nodeJson.t = getDict(nodeJson.t);
  102. }
  103. return nodeJson;
  104. }
  105. function getWxsEventDict(w, getDict) {
  106. const res = {};
  107. w.forEach(([name, [wxsEvent, flag]]) => {
  108. res[getDict(name)] = [getDict(wxsEvent), flag];
  109. });
  110. return res;
  111. }
  112. function decodeCreateAction([, nodeId, nodeName, parentNodeId, refNodeId, nodeJson], getDict) {
  113. return [
  114. 'create',
  115. nodeId,
  116. getDict(nodeName),
  117. parentNodeId,
  118. refNodeId,
  119. decodeNodeJson(getDict, nodeJson),
  120. ];
  121. }
  122. function decodeInsertAction([, ...action], getDict) {
  123. return [
  124. 'insert',
  125. action[0],
  126. action[1],
  127. action[2],
  128. action[3] ? decodeNodeJson(getDict, action[3]) : {},
  129. ];
  130. }
  131. function decodeRemoveAction([, ...action]) {
  132. return ['remove', ...action];
  133. }
  134. function decodeAddEventAction([, ...action], getDict) {
  135. return ['addEvent', action[0], getDict(action[1]), action[2]];
  136. }
  137. function decodeAddWxsEventAction([, ...action], getDict) {
  138. return [
  139. 'addWxsEvent',
  140. action[0],
  141. getDict(action[1]),
  142. getDict(action[2]),
  143. action[3],
  144. ];
  145. }
  146. function decodeRemoveEventAction([, ...action], getDict) {
  147. return ['removeEvent', action[0], getDict(action[1])];
  148. }
  149. function decodeSetAttributeAction([, ...action], getDict) {
  150. return [
  151. 'setAttr',
  152. action[0],
  153. getDict(action[1]),
  154. getDict(action[2]),
  155. ];
  156. }
  157. function decodeRemoveAttributeAction([, ...action], getDict) {
  158. return ['removeAttr', action[0], getDict(action[1])];
  159. }
  160. function decodeSetTextAction([, ...action], getDict) {
  161. return ['setText', action[0], getDict(action[1])];
  162. }
  163. exports.createGetDict = createGetDict;
  164. exports.decodeActions = decodeActions;
  165. exports.decodeNodeJson = decodeNodeJson;
  166. }));