bccaed1a10ad029e51735ac956443512bae004ac.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, CCString, error, StringFormatFunction, VMBase, VMEnv, _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _crd, ccclass, property, menu, executeInEditMode, help, LABEL_TYPE, VMLabel;
  4. function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
  5. function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
  6. function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
  7. function _reportPossibleCrUseOfStringFormatFunction(extras) {
  8. _reporterNs.report("StringFormatFunction", "./StringFormat", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfVMBase(extras) {
  11. _reporterNs.report("VMBase", "./VMBase", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfVMEnv(extras) {
  14. _reporterNs.report("VMEnv", "./VMEnv", _context.meta, extras);
  15. }
  16. return {
  17. setters: [function (_unresolved_) {
  18. _reporterNs = _unresolved_;
  19. }, function (_cc) {
  20. _cclegacy = _cc.cclegacy;
  21. __checkObsolete__ = _cc.__checkObsolete__;
  22. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  23. _decorator = _cc._decorator;
  24. CCString = _cc.CCString;
  25. error = _cc.error;
  26. }, function (_unresolved_2) {
  27. StringFormatFunction = _unresolved_2.StringFormatFunction;
  28. }, function (_unresolved_3) {
  29. VMBase = _unresolved_3.VMBase;
  30. }, function (_unresolved_4) {
  31. VMEnv = _unresolved_4.VMEnv;
  32. }],
  33. execute: function () {
  34. _crd = true;
  35. _cclegacy._RF.push({}, "545c05XsG9GDJispEGWKvYv", "VMLabel", undefined);
  36. __checkObsolete__(['_decorator', 'CCString', 'error']);
  37. ({
  38. ccclass,
  39. property,
  40. menu,
  41. executeInEditMode,
  42. help
  43. } = _decorator);
  44. LABEL_TYPE = {
  45. CC_LABEL: 'cc.Label',
  46. CC_RICH_TEXT: 'cc.RichText',
  47. CC_EDIT_BOX: 'cc.EditBox'
  48. };
  49. /**
  50. * [VM-Label]
  51. * 专门处理 Label 相关 的组件,如 ccLabel,ccRichText,ccEditBox
  52. * 可以使用模板化的方式将数据写入,可以处理字符串格式等
  53. * todo 加入stringFormat 可以解析转换常见的字符串格式
  54. */
  55. _export("default", VMLabel = (_dec = menu('OopsFramework/Mvvm/VM-Label (标签)'), _dec2 = help('https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037641&doc_id=2873565'), _dec3 = property({
  56. tooltip: '是否启用模板代码,只能在运行时之前设置,\n将会动态解析模板语法 {{0}},并且自动设置监听的路径'
  57. }), _dec4 = property({
  58. visible() {
  59. // @ts-ignore
  60. return this.templateMode === false;
  61. }
  62. }), _dec5 = property({
  63. readonly: true
  64. }), _dec6 = property({
  65. type: [CCString],
  66. visible() {
  67. // @ts-ignore
  68. return this.templateMode === true;
  69. }
  70. }), ccclass(_class = executeInEditMode(_class = _dec(_class = _dec2(_class = (_class2 = class VMLabel extends (_crd && VMBase === void 0 ? (_reportPossibleCrUseOfVMBase({
  71. error: Error()
  72. }), VMBase) : VMBase) {
  73. constructor() {
  74. super(...arguments);
  75. _initializerDefineProperty(this, "templateMode", _descriptor, this);
  76. _initializerDefineProperty(this, "watchPath", _descriptor2, this);
  77. _initializerDefineProperty(this, "labelType", _descriptor3, this);
  78. _initializerDefineProperty(this, "watchPathArr", _descriptor4, this);
  79. /** 按照路径参数顺序保存的 值的数组(固定)*/
  80. this.templateValueArr = [];
  81. /** 保存着字符模板格式的数组 (只会影响显示参数) */
  82. this.templateFormatArr = [];
  83. /** 源字符串 */
  84. this.originText = null;
  85. }
  86. onRestore() {
  87. this.checkLabel();
  88. }
  89. onLoad() {
  90. super.onLoad();
  91. this.checkLabel();
  92. if ((_crd && VMEnv === void 0 ? (_reportPossibleCrUseOfVMEnv({
  93. error: Error()
  94. }), VMEnv) : VMEnv).editor) return;
  95. if (this.templateMode) {
  96. this.originText = this.getLabelValue();
  97. this.parseTemplate();
  98. }
  99. }
  100. start() {
  101. if ((_crd && VMEnv === void 0 ? (_reportPossibleCrUseOfVMEnv({
  102. error: Error()
  103. }), VMEnv) : VMEnv).editor) return;
  104. this.onValueInit();
  105. }
  106. /** 解析模板 获取初始格式化字符串格式的信息 */
  107. parseTemplate() {
  108. var regexAll = /\{\{(.+?)\}\}/g; // 匹配: 所有的{{value}}
  109. var regex = /\{\{(.+?)\}\}/; // 匹配: {{value}} 中的 value
  110. var res = this.originText.match(regexAll); // 匹配结果数组
  111. if (res == null) return;
  112. for (var i = 0; i < res.length; i++) {
  113. var e = res[i];
  114. var arr = e.match(regex);
  115. var matchName = arr[1]; // let paramIndex = parseInt(matchName) || 0;
  116. var matchInfo = matchName.split(':')[1] || '';
  117. this.templateFormatArr[i] = matchInfo;
  118. }
  119. }
  120. /** 获取解析字符串模板后得到的值 */
  121. getReplaceText() {
  122. if (!this.originText) return "";
  123. var regexAll = /\{\{(.+?)\}\}/g; // 匹配: 所有的{{value}}
  124. var regex = /\{\{(.+?)\}\}/; // 匹配: {{value}} 中的 value
  125. var res = this.originText.match(regexAll); // 匹配结果数组 [{{value}},{{value}},{{value}}]
  126. if (res == null) return ''; // 未匹配到文本
  127. var str = this.originText; // 原始字符串模板 "name:{{0}} 或 name:{{0:fix2}}"
  128. for (var i = 0; i < res.length; i++) {
  129. var e = res[i];
  130. var getValue = void 0;
  131. var arr = e.match(regex); // 匹配到的数组 [{{value}}, value]
  132. var indexNum = parseInt(arr[1] || '0') || 0; // 取出数组的 value 元素 转换成整数
  133. var format = this.templateFormatArr[i]; // 格式化字符 的 配置参数
  134. getValue = this.templateValueArr[indexNum];
  135. str = str.replace(e, this.getValueFromFormat(getValue, format)); //从路径缓存值获取数据
  136. }
  137. return str;
  138. }
  139. /** 格式化字符串 */
  140. getValueFromFormat(value, format) {
  141. return (_crd && StringFormatFunction === void 0 ? (_reportPossibleCrUseOfStringFormatFunction({
  142. error: Error()
  143. }), StringFormatFunction) : StringFormatFunction).deal(value, format);
  144. }
  145. /** 初始化获取数据 */
  146. onValueInit() {
  147. //更新信息
  148. if (this.templateMode === false) {
  149. this.setLabelValue(this.VM.getValue(this.watchPath)); //
  150. } else {
  151. var max = this.watchPathArr.length;
  152. for (var i = 0; i < max; i++) {
  153. this.templateValueArr[i] = this.VM.getValue(this.watchPathArr[i], '?');
  154. }
  155. this.setLabelValue(this.getReplaceText()); // 重新解析
  156. }
  157. }
  158. /** 监听数据发生了变动的情况 */
  159. onValueChanged(n, o, pathArr) {
  160. if (this.templateMode === false) {
  161. this.setLabelValue(n);
  162. } else {
  163. var path = pathArr.join('.'); // 寻找缓存位置
  164. var index = this.watchPathArr.findIndex(v => v === path);
  165. if (index >= 0) {
  166. //如果是所属的路径,就可以替换文本了
  167. this.templateValueArr[index] = n; // 缓存值
  168. this.setLabelValue(this.getReplaceText()); // 重新解析文本
  169. }
  170. }
  171. }
  172. setLabelValue(value) {
  173. var component = this.getComponent(this.labelType);
  174. component.string = value + '';
  175. }
  176. getLabelValue() {
  177. var component = this.getComponent(this.labelType);
  178. return component.string;
  179. }
  180. checkLabel() {
  181. var checkArray = ['cc.Label', 'cc.RichText', 'cc.EditBox'];
  182. for (var i = 0; i < checkArray.length; i++) {
  183. var e = checkArray[i];
  184. var comp = this.node.getComponent(e);
  185. if (comp) {
  186. this.labelType = e;
  187. return true;
  188. }
  189. }
  190. error('没有挂载任何label组件');
  191. return false;
  192. }
  193. }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "templateMode", [_dec3], {
  194. configurable: true,
  195. enumerable: true,
  196. writable: true,
  197. initializer: function initializer() {
  198. return false;
  199. }
  200. }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "watchPath", [_dec4], {
  201. configurable: true,
  202. enumerable: true,
  203. writable: true,
  204. initializer: function initializer() {
  205. return '';
  206. }
  207. }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "labelType", [_dec5], {
  208. configurable: true,
  209. enumerable: true,
  210. writable: true,
  211. initializer: function initializer() {
  212. return LABEL_TYPE.CC_LABEL;
  213. }
  214. }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "watchPathArr", [_dec6], {
  215. configurable: true,
  216. enumerable: true,
  217. writable: true,
  218. initializer: function initializer() {
  219. return [];
  220. }
  221. })), _class2)) || _class) || _class) || _class) || _class));
  222. _cclegacy._RF.pop();
  223. _crd = false;
  224. }
  225. };
  226. });
  227. //# sourceMappingURL=bccaed1a10ad029e51735ac956443512bae004ac.js.map