@galacean/effects-plugin-rich-text 2.3.0-alpha.1 → 2.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/alipay.js CHANGED
@@ -1,596 +1,2 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var EFFECTS = require('@galacean/effects/alipay');
6
-
7
- function _interopNamespace(e) {
8
- if (e && e.__esModule) return e;
9
- var n = Object.create(null);
10
- if (e) {
11
- Object.keys(e).forEach(function (k) {
12
- if (k !== 'default') {
13
- var d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(n, k, d.get ? d : {
15
- enumerable: true,
16
- get: function () { return e[k]; }
17
- });
18
- }
19
- });
20
- }
21
- n["default"] = e;
22
- return Object.freeze(n);
23
- }
24
-
25
- var EFFECTS__namespace = /*#__PURE__*/_interopNamespace(EFFECTS);
26
-
27
- function _set_prototype_of(o, p) {
28
- _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
29
- o.__proto__ = p;
30
- return o;
31
- };
32
- return _set_prototype_of(o, p);
33
- }
34
-
35
- function _inherits(subClass, superClass) {
36
- if (typeof superClass !== "function" && superClass !== null) {
37
- throw new TypeError("Super expression must either be null or a function");
38
- }
39
- subClass.prototype = Object.create(superClass && superClass.prototype, {
40
- constructor: {
41
- value: subClass,
42
- writable: true,
43
- configurable: true
44
- }
45
- });
46
- if (superClass) _set_prototype_of(subClass, superClass);
47
- }
48
-
49
- var RichTextLoader = /*#__PURE__*/ function(AbstractPlugin) {
50
- _inherits(RichTextLoader, AbstractPlugin);
51
- function RichTextLoader() {
52
- var _this;
53
- _this = AbstractPlugin.apply(this, arguments) || this;
54
- _this.name = "rich-text";
55
- return _this;
56
- }
57
- return RichTextLoader;
58
- }(EFFECTS.AbstractPlugin);
59
-
60
- function _array_like_to_array(arr, len) {
61
- if (len == null || len > arr.length) len = arr.length;
62
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
63
- return arr2;
64
- }
65
-
66
- function _unsupported_iterable_to_array(o, minLen) {
67
- if (!o) return;
68
- if (typeof o === "string") return _array_like_to_array(o, minLen);
69
- var n = Object.prototype.toString.call(o).slice(8, -1);
70
- if (n === "Object" && o.constructor) n = o.constructor.name;
71
- if (n === "Map" || n === "Set") return Array.from(n);
72
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
73
- }
74
-
75
- function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
76
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
77
- if (it) return (it = it.call(o)).next.bind(it);
78
- // Fallback for engines without symbol support
79
- if (Array.isArray(o) || (it = _unsupported_iterable_to_array(o)) || allowArrayLike && o && typeof o.length === "number") {
80
- if (it) o = it;
81
- var i = 0;
82
- return function() {
83
- if (i >= o.length) return {
84
- done: true
85
- };
86
- return {
87
- done: false,
88
- value: o[i++]
89
- };
90
- };
91
- }
92
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
93
- }
94
-
95
- var TokenType;
96
- (function(TokenType) {
97
- TokenType["ContextStart"] = "ContextStart";
98
- TokenType["Text"] = "Text";
99
- TokenType["ContextEnd"] = "ContextEnd";
100
- })(TokenType || (TokenType = {}));
101
- var contextStartRegexp = /^<([a-z]+)(=([^>]+))?>$/;
102
- var contextEndRegexp = /^<\/([a-z]+)>$/;
103
- var rules = [
104
- [
105
- "ContextStart",
106
- /^<[a-z]+(=[^>]+)?>/
107
- ],
108
- [
109
- "Text",
110
- /^[^</>=]+/
111
- ],
112
- [
113
- "ContextEnd",
114
- /^<\/[a-z]+>/
115
- ]
116
- ];
117
- var lexer = function(input, lexed, cursor) {
118
- if (lexed === void 0) lexed = [];
119
- if (cursor === void 0) cursor = 0;
120
- if (!input) {
121
- return lexed;
122
- }
123
- for(var _iterator = _create_for_of_iterator_helper_loose(rules), _step; !(_step = _iterator()).done;){
124
- var _step_value = _step.value, tokenType = _step_value[0], regex = _step_value[1];
125
- var _regex_exec;
126
- var _ref = (_regex_exec = regex.exec(input)) != null ? _regex_exec : [], tokenMatch = _ref[0];
127
- if (tokenMatch) {
128
- var len = tokenMatch.length;
129
- return lexer(input.slice(len), lexed.concat({
130
- tokenType: tokenType,
131
- value: tokenMatch
132
- }), cursor + len);
133
- }
134
- }
135
- throw new Error('Unexpected token: "' + input[0] + '" at position ' + cursor + ' while reading "' + input + '"');
136
- };
137
- var richTextParser = function(input) {
138
- var Text = function Text() {
139
- var maybeText = peek();
140
- if ((maybeText == null ? void 0 : maybeText.tokenType) === "Text") {
141
- shift();
142
- return maybeText.value;
143
- }
144
- return undefined;
145
- };
146
- var ContextStart = function ContextStart() {
147
- var maybeContextStart = peek();
148
- if ((maybeContextStart == null ? void 0 : maybeContextStart.tokenType) === "ContextStart") {
149
- shift();
150
- var matches = maybeContextStart.value.match(contextStartRegexp);
151
- if (matches) {
152
- var attributeName = matches[1];
153
- var _matches_;
154
- var attributeParam = (_matches_ = matches[3]) != null ? _matches_ : "";
155
- return {
156
- attributeName: attributeName,
157
- attributeParam: attributeParam
158
- };
159
- }
160
- throw new Error("Expected a start tag marker at position " + cursor);
161
- }
162
- return {};
163
- };
164
- var ContextEnd = function ContextEnd() {
165
- var maybeContextEnd = peek();
166
- if ((maybeContextEnd == null ? void 0 : maybeContextEnd.tokenType) === "ContextEnd") {
167
- shift();
168
- var matches = maybeContextEnd.value.match(contextEndRegexp);
169
- if (matches) {
170
- var attributeName = matches[1];
171
- return {
172
- attributeName: attributeName
173
- };
174
- }
175
- throw new Error("Expect an end tag marker at position " + cursor);
176
- }
177
- return {};
178
- };
179
- var lexed = lexer(input);
180
- var cursor = 0;
181
- var shift = function() {
182
- var shifted = lexed.shift();
183
- var _shifted_value_length;
184
- cursor += (_shifted_value_length = shifted == null ? void 0 : shifted.value.length) != null ? _shifted_value_length : 0;
185
- return shifted;
186
- };
187
- var peek = function() {
188
- return lexed[0];
189
- };
190
- var ast = [];
191
- function Grammar(attributes, expectedEndAttributeName) {
192
- if (attributes === void 0) attributes = [];
193
- if (expectedEndAttributeName === void 0) expectedEndAttributeName = "";
194
- var parsing = true;
195
- while(parsing){
196
- var maybeText = Text();
197
- if (maybeText) {
198
- ast.push({
199
- attributes: attributes,
200
- text: maybeText
201
- });
202
- continue;
203
- }
204
- var _ContextStart = ContextStart(), attributeName = _ContextStart.attributeName, attributeParam = _ContextStart.attributeParam;
205
- if (attributeName) {
206
- Grammar(attributes.concat({
207
- attributeName: attributeName,
208
- attributeParam: attributeParam
209
- }), attributeName);
210
- continue;
211
- }
212
- if (expectedEndAttributeName) {
213
- var _ContextEnd = ContextEnd(), endAttributeName = _ContextEnd.attributeName;
214
- if (!endAttributeName) {
215
- throw new Error('Expect an end tag marker "' + expectedEndAttributeName + '" at position ' + cursor + " but found no tag!");
216
- }
217
- if (endAttributeName !== expectedEndAttributeName) {
218
- throw new Error('Expect an end tag marker "' + expectedEndAttributeName + '" at position ' + cursor + ' but found tag "' + endAttributeName + '"');
219
- }
220
- return;
221
- }
222
- break;
223
- }
224
- }
225
- Grammar();
226
- return ast;
227
- };
228
- function generateProgram(textHandler) {
229
- return function(richText) {
230
- var ast = richTextParser(richText);
231
- for(var _iterator = _create_for_of_iterator_helper_loose(ast), _step; !(_step = _iterator()).done;){
232
- var node = _step.value;
233
- var text = node.text;
234
- var context = node.attributes.reduce(function(ctx, param) {
235
- var attributeName = param.attributeName, attributeParam = param.attributeParam;
236
- if (attributeName) {
237
- ctx[attributeName] = attributeParam;
238
- }
239
- return ctx;
240
- }, {});
241
- textHandler(text, context);
242
- }
243
- };
244
- }
245
- function isRichText(text) {
246
- var lexed = lexer(text);
247
- var contextTokens = lexed.filter(function(param) {
248
- var tokenType = param.tokenType;
249
- return tokenType === "ContextStart" || tokenType === "ContextEnd";
250
- });
251
- var contextStartTokens = contextTokens.filter(function(param) {
252
- var tokenType = param.tokenType;
253
- return tokenType === "ContextStart";
254
- });
255
- var contextEndTokens = contextTokens.filter(function(param) {
256
- var tokenType = param.tokenType;
257
- return tokenType === "ContextEnd";
258
- });
259
- if (contextStartTokens.length !== contextEndTokens.length || !contextStartTokens.length) {
260
- return false;
261
- }
262
- var tokensOfAttribute = contextTokens.map(function(param) {
263
- var tokenType = param.tokenType, value = param.value;
264
- return {
265
- tokenType: tokenType,
266
- value: tokenType === "ContextStart" ? value.match(contextStartRegexp)[1] : value.match(contextEndRegexp)[1]
267
- };
268
- });
269
- function checkPaired(param, startContextAttributes) {
270
- var token = param[0], restToken = param.slice(1);
271
- if (startContextAttributes === void 0) startContextAttributes = [];
272
- if (!token) {
273
- return startContextAttributes.length === 0;
274
- }
275
- if (token.tokenType === "ContextStart") {
276
- return checkPaired(restToken, startContextAttributes.concat(token.value));
277
- } else if (token.tokenType === "ContextEnd") {
278
- var attributeName = startContextAttributes[startContextAttributes.length - 1];
279
- if (attributeName !== token.value) {
280
- return false;
281
- }
282
- return checkPaired(restToken, startContextAttributes.slice(0, -1));
283
- }
284
- throw new Error("Unexpected token: " + token.tokenType);
285
- }
286
- return checkPaired(tokensOfAttribute);
287
- }
288
-
289
- function __decorate(decorators, target, key, desc) {
290
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
291
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
292
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
293
- return c > 3 && r && Object.defineProperty(target, key, r), r;
294
- }
295
- typeof SuppressedError === "function" ? SuppressedError : function _SuppressedError(error, suppressed, message) {
296
- var e = new Error(message);
297
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
298
- };
299
-
300
- /**
301
- * 将颜色名称转换为 RGBA
302
- * @param colorName - 颜色名称
303
- * @returns RGBA 颜色字符串
304
- */ function colorNameToRGBA(colorName) {
305
- if (typeof colorName !== "string" || !colorName) {
306
- throw new Error("Invalid color name provided");
307
- }
308
- if (typeof document === "undefined") {
309
- throw new Error("This method requires a browser environment");
310
- }
311
- var canvas = document.createElement("canvas");
312
- var context = canvas.getContext("2d");
313
- if (context) {
314
- try {
315
- context.fillStyle = colorName;
316
- var result = context.fillStyle;
317
- return result;
318
- } finally{
319
- // Clean up DOM element
320
- canvas.remove();
321
- }
322
- }
323
- throw new Error("Failed to get 2D context for color conversion!");
324
- }
325
- /**
326
- * 将 16 进制颜色转换为 RGBA
327
- * @param hex - 16 进制颜色
328
- * @param alpha - 透明度
329
- * @returns - RGBA 颜色
330
- */ function hexToRGBA(hex, alpha) {
331
- if (alpha === void 0) alpha = 1;
332
- hex = hex.replace(/^#/, "");
333
- if (hex.length === 3 || hex.length === 4) {
334
- hex = hex.split("").map(function(char) {
335
- return char + char;
336
- }).join("");
337
- }
338
- // Handle alpha channel in hex
339
- if (hex.length === 8) {
340
- var a = parseInt(hex.slice(6, 8), 16) / 255;
341
- hex = hex.slice(0, 6);
342
- alpha = a;
343
- }
344
- var bigint = parseInt(hex, 16);
345
- var r = bigint >> 16 & 255;
346
- var g = bigint >> 8 & 255;
347
- var b = bigint & 255;
348
- return [
349
- r,
350
- g,
351
- b,
352
- alpha
353
- ];
354
- }
355
- /**
356
- * 将颜色字符串转换为 RGBA
357
- * @param color - 颜色字符串
358
- * @param alpha - 透明度
359
- * @returns - RGBA 颜色
360
- */ function toRGBA(color, alpha) {
361
- if (alpha === void 0) alpha = 1;
362
- if (typeof color !== "string" || !color) {
363
- throw new Error("Invalid color string");
364
- }
365
- if (color.startsWith("#")) {
366
- return hexToRGBA(color, alpha);
367
- } else {
368
- return hexToRGBA(colorNameToRGBA(color));
369
- }
370
- }
371
-
372
- var seed = 0;
373
- exports.RichTextComponent = /*#__PURE__*/ function(TextComponent) {
374
- _inherits(RichTextComponent, TextComponent);
375
- function RichTextComponent(engine) {
376
- var _this;
377
- _this = TextComponent.call(this, engine) || this;
378
- _this.processedTextOptions = [];
379
- _this.singleLineHeight = 1.571;
380
- _this.size = null;
381
- /**
382
- * 获取第一次渲染的 size
383
- */ _this.initialized = false;
384
- /**
385
- * canvas 大小
386
- */ _this.canvasSize = null;
387
- _this.name = "MRichText" + seed++;
388
- return _this;
389
- }
390
- var _proto = RichTextComponent.prototype;
391
- _proto.generateTextProgram = function generateTextProgram(text) {
392
- var _this = this;
393
- this.processedTextOptions = [];
394
- var program = generateProgram(function(text, context) {
395
- // 如果富文本仅包含换行符,则在每个换行符后添加一个空格
396
- if (/^\n+$/.test(text)) {
397
- text = text.replace(/\n/g, "\n ");
398
- }
399
- var textArr = text.split("\n");
400
- textArr.forEach(function(text, index) {
401
- var options = {
402
- text: text,
403
- fontSize: _this.textStyle.fontSize,
404
- isNewLine: false
405
- };
406
- if (index > 0) {
407
- options.isNewLine = true;
408
- }
409
- if ("b" in context) {
410
- options.fontWeight = EFFECTS.spec.TextWeight.bold;
411
- }
412
- if ("i" in context) {
413
- options.fontStyle = EFFECTS.spec.FontStyle.italic;
414
- }
415
- if ("size" in context && context.size) {
416
- options.fontSize = parseInt(context.size, 10);
417
- }
418
- if ("color" in context && context.color) {
419
- options.fontColor = toRGBA(context.color);
420
- }
421
- _this.processedTextOptions.push(options);
422
- });
423
- });
424
- program(text);
425
- };
426
- _proto.updateTexture = function updateTexture(flipY) {
427
- var _this = this;
428
- if (flipY === void 0) flipY = true;
429
- if (!this.isDirty || !this.context || !this.canvas) {
430
- return;
431
- }
432
- this.generateTextProgram(this.text);
433
- var width = 0, height = 0;
434
- var _this1 = this, textLayout = _this1.textLayout, textStyle = _this1.textStyle;
435
- var overflow = textLayout.overflow;
436
- var context = this.context;
437
- context.save();
438
- var charsInfo = [];
439
- var fontHeight = textStyle.fontSize * this.textStyle.fontScale;
440
- var charInfo = {
441
- richOptions: [],
442
- offsetX: [],
443
- width: 0,
444
- lineHeight: fontHeight * this.singleLineHeight,
445
- offsetY: fontHeight * (this.singleLineHeight - 1) / 2
446
- };
447
- this.processedTextOptions.forEach(function(options) {
448
- var text = options.text, isNewLine = options.isNewLine, fontSize = options.fontSize;
449
- if (isNewLine) {
450
- charsInfo.push(charInfo);
451
- width = Math.max(width, charInfo.width);
452
- charInfo = {
453
- richOptions: [],
454
- offsetX: [],
455
- width: 0,
456
- lineHeight: fontHeight * _this.singleLineHeight,
457
- offsetY: fontHeight * (_this.singleLineHeight - 1) / 2
458
- };
459
- height += charInfo.lineHeight;
460
- }
461
- //恢复默认设置
462
- context.font = (options.fontWeight || textStyle.textWeight) + " 10px " + (options.fontFamily || textStyle.fontFamily);
463
- var textWidth = context.measureText(text).width;
464
- var textHeight = fontSize * _this.singleLineHeight * _this.textStyle.fontScale;
465
- if (textHeight > charInfo.lineHeight) {
466
- height += textHeight - charInfo.lineHeight;
467
- charInfo.lineHeight = textHeight;
468
- charInfo.offsetY = fontSize * _this.textStyle.fontScale * (_this.singleLineHeight - 1) / 2;
469
- }
470
- charInfo.offsetX.push(charInfo.width);
471
- charInfo.width += textWidth * fontSize * _this.SCALE_FACTOR * _this.textStyle.fontScale;
472
- charInfo.richOptions.push(options);
473
- });
474
- charsInfo.push(charInfo);
475
- width = Math.max(width, charInfo.width);
476
- height += charInfo.lineHeight;
477
- if (width === 0 || height === 0) {
478
- this.isDirty = false;
479
- return;
480
- }
481
- if (this.size === undefined || this.size === null) {
482
- this.size = this.item.transform.size.clone();
483
- }
484
- var _this_size = this.size, _this_size_x = _this_size.x, x = _this_size_x === void 0 ? 1 : _this_size_x, _this_size_y = _this_size.y, y = _this_size_y === void 0 ? 1 : _this_size_y;
485
- if (!this.initialized) {
486
- this.canvasSize = new EFFECTS.math.Vector2(width, height);
487
- this.item.transform.size.set(x * width * this.SCALE_FACTOR * this.SCALE_FACTOR, y * height * this.SCALE_FACTOR * this.SCALE_FACTOR);
488
- this.size = this.item.transform.size.clone();
489
- this.initialized = true;
490
- }
491
- EFFECTS.assertExist(this.canvasSize);
492
- var _this_canvasSize = this.canvasSize, canvasWidth = _this_canvasSize.x, canvasHeight = _this_canvasSize.y;
493
- this.textLayout.width = canvasWidth / textStyle.fontScale;
494
- this.textLayout.height = canvasHeight / textStyle.fontScale;
495
- this.canvas.width = canvasWidth;
496
- this.canvas.height = canvasHeight;
497
- context.clearRect(0, 0, canvasWidth, canvasHeight);
498
- // fix bug 1/255
499
- context.fillStyle = "rgba(255, 255, 255, " + this.ALPHA_FIX_VALUE + ")";
500
- if (!flipY) {
501
- context.translate(0, canvasHeight);
502
- context.scale(1, -1);
503
- }
504
- if (charsInfo.length === 0) {
505
- return;
506
- }
507
- var charsLineHeight = textLayout.getOffsetY(textStyle, charsInfo.length, fontHeight * this.singleLineHeight, textStyle.fontSize);
508
- charsInfo.forEach(function(charInfo, index) {
509
- var richOptions = charInfo.richOptions, offsetX = charInfo.offsetX, width = charInfo.width;
510
- var charWidth = width;
511
- var offset = offsetX;
512
- if (overflow === EFFECTS.spec.TextOverflow.display) {
513
- if (width > canvasWidth) {
514
- var scale = canvasWidth / width;
515
- charWidth *= scale;
516
- offset = offsetX.map(function(x) {
517
- return x * scale;
518
- });
519
- }
520
- }
521
- var x = _this.textLayout.getOffsetX(textStyle, charWidth);
522
- if (index > 0) {
523
- charsLineHeight += charInfo.lineHeight - charInfo.offsetY;
524
- }
525
- richOptions.forEach(function(options, index) {
526
- var fontScale = textStyle.fontScale, textColor = textStyle.textColor, textFamily = textStyle.fontFamily, textWeight = textStyle.textWeight, richStyle = textStyle.fontStyle;
527
- var text = options.text, fontSize = options.fontSize, _options_fontColor = options.fontColor, fontColor = _options_fontColor === void 0 ? textColor : _options_fontColor, _options_fontFamily = options.fontFamily, fontFamily = _options_fontFamily === void 0 ? textFamily : _options_fontFamily, _options_fontWeight = options.fontWeight, fontWeight = _options_fontWeight === void 0 ? textWeight : _options_fontWeight, _options_fontStyle = options.fontStyle, fontStyle = _options_fontStyle === void 0 ? richStyle : _options_fontStyle;
528
- var textSize = fontSize;
529
- if (overflow === EFFECTS.spec.TextOverflow.display) {
530
- if (width > canvasWidth) {
531
- textSize /= width / canvasWidth;
532
- }
533
- }
534
- context.font = fontStyle + " " + fontWeight + " " + textSize * fontScale + "px " + fontFamily;
535
- context.fillStyle = "rgba(" + fontColor[0] + ", " + fontColor[1] + ", " + fontColor[2] + ", " + fontColor[3] + ")";
536
- context.fillText(text, offset[index] + x, charsLineHeight);
537
- });
538
- });
539
- //与 toDataURL() 两种方式都需要像素读取操作
540
- var imageData = context.getImageData(0, 0, this.canvas.width, this.canvas.height);
541
- var texture = EFFECTS.Texture.createWithData(this.engine, {
542
- data: new Uint8Array(imageData.data),
543
- width: imageData.width,
544
- height: imageData.height
545
- }, {
546
- flipY: flipY,
547
- magFilter: EFFECTS.glContext.LINEAR,
548
- minFilter: EFFECTS.glContext.LINEAR,
549
- wrapS: EFFECTS.glContext.CLAMP_TO_EDGE,
550
- wrapT: EFFECTS.glContext.CLAMP_TO_EDGE
551
- });
552
- this.renderer.texture = texture;
553
- this.material.setTexture("_MainTex", texture);
554
- this.isDirty = false;
555
- context.restore();
556
- };
557
- /**
558
- * 设置文本溢出模式
559
- *
560
- * - clip: 当文本内容超出边界框时,多余的会被截断。
561
- * - display: 该模式下会显示所有文本,会自动调整文本字号以保证显示完整。
562
- * > 当存在多行时,部分行内文本可能存在文本字号变小的情况,其他行为正常情况
563
- *
564
- * @param overflow - 文本溢出模式
565
- */ _proto.setOverflow = function setOverflow(overflow) {
566
- this.textLayout.overflow = overflow;
567
- this.isDirty = true;
568
- };
569
- _proto.updateWithOptions = function updateWithOptions(options) {
570
- this.textStyle = new EFFECTS.TextStyle(options);
571
- this.textLayout = new EFFECTS.TextLayout(options);
572
- this.textLayout.textBaseline = options.textBaseline || EFFECTS.spec.TextBaseline.middle;
573
- this.text = options.text ? options.text.toString() : " ";
574
- };
575
- return RichTextComponent;
576
- }(EFFECTS.TextComponent);
577
- exports.RichTextComponent = __decorate([
578
- EFFECTS.effectsClass(EFFECTS.spec.DataType.RichTextComponent)
579
- ], exports.RichTextComponent);
580
-
581
- /**
582
- * 插件版本号
583
- */ var version = "2.3.0-alpha.1";
584
- EFFECTS.registerPlugin("rich-text", RichTextLoader, EFFECTS.VFXItem, true);
585
- EFFECTS.logger.info("Plugin rich text version: " + version + ".");
586
- if (version !== EFFECTS__namespace.version) {
587
- console.error("注意:请统一 RichText 插件与 Player 版本,不统一的版本混用会有不可预知的后果!", "\nAttention: Please ensure the RichText plugin is synchronized with the Player version. Mixing and matching incompatible versions may result in unpredictable consequences!");
588
- }
589
-
590
- exports.RichTextLoader = RichTextLoader;
591
- exports.generateProgram = generateProgram;
592
- exports.isRichText = isRichText;
593
- exports.lexer = lexer;
594
- exports.richTextParser = richTextParser;
595
- exports.version = version;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@galacean/effects/alipay");function e(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var n=e(t);function r(t,e){return r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},r(t,e)}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&r(t,e)}var o,a=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).name="rich-text",e}return i(e,t),e}(t.AbstractPlugin);function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function l(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return s(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(n):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}!function(t){t.ContextStart="ContextStart",t.Text="Text",t.ContextEnd="ContextEnd"}(o||(o={}));var h=/^<([a-z]+)(=([^>]+))?>$/,c=/^<\/([a-z]+)>$/,f=[["ContextStart",/^<[a-z]+(=[^>]+)?>/],["Text",/^[^</>=]+/],["ContextEnd",/^<\/[a-z]+>/]],u=function(t,e,n){if(void 0===e&&(e=[]),void 0===n&&(n=0),!t)return e;for(var r,i=l(f);!(r=i()).done;){var o,a=r.value,s=a[0],h=(null!=(o=a[1].exec(t))?o:[])[0];if(h){var c=h.length;return u(t.slice(c),e.concat({tokenType:s,value:h}),n+c)}}throw new Error('Unexpected token: "'+t[0]+'" at position '+n+' while reading "'+t+'"')},p=function(t){var e=function(){var t=s();if("Text"===(null==t?void 0:t.tokenType))return a(),t.value},n=function(){var t=s();if("ContextStart"===(null==t?void 0:t.tokenType)){a();var e,n=t.value.match(h);if(n)return{attributeName:n[1],attributeParam:null!=(e=n[3])?e:""};throw new Error("Expected a start tag marker at position "+o)}return{}},r=function(){var t=s();if("ContextEnd"===(null==t?void 0:t.tokenType)){a();var e=t.value.match(c);if(e)return{attributeName:e[1]};throw new Error("Expect an end tag marker at position "+o)}return{}},i=u(t),o=0,a=function(){var t,e=i.shift();return o+=null!=(t=null==e?void 0:e.value.length)?t:0,e},s=function(){return i[0]},l=[];return function t(i,a){for(void 0===i&&(i=[]),void 0===a&&(a="");;){var s=e();if(s)l.push({attributes:i,text:s});else{var h=n(),c=h.attributeName,f=h.attributeParam;if(!c){if(a){var u=r().attributeName;if(!u)throw new Error('Expect an end tag marker "'+a+'" at position '+o+" but found no tag!");if(u!==a)throw new Error('Expect an end tag marker "'+a+'" at position '+o+' but found tag "'+u+'"');return}break}t(i.concat({attributeName:c,attributeParam:f}),c)}}}(),l};function v(t){return function(e){for(var n,r=l(p(e));!(n=r()).done;){var i=n.value,o=i.text,a=i.attributes.reduce((function(t,e){var n=e.attributeName,r=e.attributeParam;return n&&(t[n]=r),t}),{});t(o,a)}}}function d(t,e){if(void 0===e&&(e=1),3!==(t=t.replace(/^#/,"")).length&&4!==t.length||(t=t.split("").map((function(t){return t+t})).join("")),8===t.length){var n=parseInt(t.slice(6,8),16)/255;t=t.slice(0,6),e=n}var r=parseInt(t,16);return[r>>16&255,r>>8&255,255&r,e]}function x(t,e){if(void 0===e&&(e=1),"string"!=typeof t||!t)throw new Error("Invalid color string");return t.startsWith("#")?d(t,e):d(function(t){if("string"!=typeof t||!t)throw new Error("Invalid color name provided");if("undefined"==typeof document)throw new Error("This method requires a browser environment");var e=document.createElement("canvas"),n=e.getContext("2d");if(n)try{return n.fillStyle=t,n.fillStyle}finally{e.remove()}throw new Error("Failed to get 2D context for color conversion!")}(t))}"function"==typeof SuppressedError&&SuppressedError;var g=0;exports.RichTextComponent=function(e){function n(t){var n;return(n=e.call(this,t)||this).processedTextOptions=[],n.singleLineHeight=1.571,n.size=null,n.initialized=!1,n.canvasSize=null,n.name="MRichText"+g++,n}i(n,e);var r=n.prototype;return r.generateTextProgram=function(e){var n=this;this.processedTextOptions=[];var r=v((function(e,r){/^\n+$/.test(e)&&(e=e.replace(/\n/g,"\n ")),e.split("\n").forEach((function(e,i){var o={text:e,fontSize:n.textStyle.fontSize,isNewLine:!1};i>0&&(o.isNewLine=!0),"b"in r&&(o.fontWeight=t.spec.TextWeight.bold),"i"in r&&(o.fontStyle=t.spec.FontStyle.italic),"size"in r&&r.size&&(o.fontSize=parseInt(r.size,10)),"color"in r&&r.color&&(o.fontColor=x(r.color)),n.processedTextOptions.push(o)}))}));r(e)},r.updateTexture=function(e){var n=this;if(void 0===e&&(e=!0),this.isDirty&&this.context&&this.canvas){this.generateTextProgram(this.text);var r=0,i=0,o=this.textLayout,a=this.textStyle,s=o.overflow,l=o.letterSpace,h=void 0===l?0:l,c=this.context;c.save();var f=[],u=a.fontSize*this.textStyle.fontScale,p={richOptions:[],offsetX:[],width:0,lineHeight:u*this.singleLineHeight,offsetY:u*(this.singleLineHeight-1)/2};if(this.processedTextOptions.forEach((function(t){var e=t.text,o=t.isNewLine,s=t.fontSize;o&&(f.push(p),r=Math.max(r,p.width),p={richOptions:[],offsetX:[],width:0,lineHeight:u*n.singleLineHeight,offsetY:u*(n.singleLineHeight-1)/2},i+=p.lineHeight),c.font=(t.fontWeight||a.textWeight)+" 10px "+(t.fontFamily||a.fontFamily);var l=c.measureText(e).width,v=s*n.singleLineHeight*n.textStyle.fontScale;v>p.lineHeight&&(i+=v-p.lineHeight,p.lineHeight=v,p.offsetY=s*n.textStyle.fontScale*(n.singleLineHeight-1)/2),p.offsetX.push(p.width),p.width+=(l<=0?0:l)*s*n.SCALE_FACTOR*n.textStyle.fontScale+e.length*h,p.richOptions.push(t)})),f.push(p),r=Math.max(r,p.width),i+=p.lineHeight,0!==r&&0!==i){void 0!==this.size&&null!==this.size||(this.size=this.item.transform.size.clone());var v=this.size,d=v.x,x=void 0===d?1:d,g=v.y,y=void 0===g?1:g;if(!this.initialized){this.canvasSize=this.canvasSize?this.canvasSize:new t.math.Vector2(r,i);var m=this.canvasSize,w=m.x,S=m.y;this.item.transform.size.set(x*w*this.SCALE_FACTOR*this.SCALE_FACTOR,y*S*this.SCALE_FACTOR*this.SCALE_FACTOR),this.size=this.item.transform.size.clone(),this.initialized=!0}t.assertExist(this.canvasSize);var T=this.canvasSize,b=T.x,E=T.y;if(this.textLayout.width=b/a.fontScale,this.textLayout.height=E/a.fontScale,this.canvas.width=b,this.canvas.height=E,c.clearRect(0,0,b,E),c.fillStyle="rgba(255, 255, 255, "+this.ALPHA_FIX_VALUE+")",e||(c.translate(0,E),c.scale(1,-1)),0!==f.length){var C=o.getOffsetY(a,f.length,u*this.singleLineHeight,a.fontSize);f.forEach((function(e,r){var i=e.richOptions,o=e.offsetX,l=e.width,f=l,u=o;if(s===t.spec.TextOverflow.display&&l>b){var p=b/l;f*=p,u=o.map((function(t){return t*p}))}var v=n.textLayout.getOffsetX(a,f);r>0&&(C+=e.lineHeight-e.offsetY),i.forEach((function(e,n){var r=a.fontScale,i=a.textColor,o=a.fontFamily,f=a.textWeight,p=a.fontStyle,d=e.text,x=e.fontSize,g=e.fontColor,y=void 0===g?i:g,m=e.fontFamily,w=void 0===m?o:m,S=e.fontWeight,T=void 0===S?f:S,E=e.fontStyle,O=void 0===E?p:E,z=x;s===t.spec.TextOverflow.display&&l>b&&(z/=l/b),c.font=O+" "+T+" "+z*r+"px "+w,c.fillStyle="rgba("+y[0]+", "+y[1]+", "+y[2]+", "+y[3]+")";var L=u[n]+v;if(" "===d)c.fillText(d,u[n]+v,C);else for(var A=0;A<d.length;A++){var P=d[A],k=c.measureText(P).width;c.fillText(P,L,C),L+=k+(A===d.length-1||" "===d?0:h*z/x)}}))}));var O=c.getImageData(0,0,this.canvas.width,this.canvas.height),z=t.Texture.createWithData(this.engine,{data:new Uint8Array(O.data),width:O.width,height:O.height},{flipY:e,magFilter:t.glContext.LINEAR,minFilter:t.glContext.LINEAR,wrapS:t.glContext.CLAMP_TO_EDGE,wrapT:t.glContext.CLAMP_TO_EDGE});this.renderer.texture=z,this.material.setTexture("_MainTex",z),this.isDirty=!1,c.restore()}}else this.isDirty=!1}},r.setShadowOffsetY=function(t){throw new Error("Method not implemented.")},r.setShadowBlur=function(t){throw new Error("Method not implemented.")},r.setShadowOffsetX=function(t){throw new Error("Method not implemented.")},r.setShadowColor=function(t){throw new Error("Method not implemented.")},r.setOutlineWidth=function(t){throw new Error("Method not implemented.")},r.setAutoWidth=function(t){throw new Error("Method not implemented.")},r.updateWithOptions=function(e){this.textStyle=new t.TextStyle(e),this.textLayout=new t.TextLayout(e),this.textLayout.textBaseline=e.textBaseline||t.spec.TextBaseline.middle,this.text=e.text?e.text.toString():" "},r.renderText=function(e){var n=e.size;n&&(this.canvasSize=new t.math.Vector2(n[0],n[1])),this.updateTexture()},n}(t.TextComponent),exports.RichTextComponent=function(t,e,n,r){var i,o=arguments.length,a=o<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,n,r);else for(var s=t.length-1;s>=0;s--)(i=t[s])&&(a=(o<3?i(a):o>3?i(e,n,a):i(e,n))||a);return o>3&&a&&Object.defineProperty(e,n,a),a}([t.effectsClass(t.spec.DataType.RichTextComponent)],exports.RichTextComponent);var y="2.3.0-beta.0";t.registerPlugin("rich-text",a,t.VFXItem,!0),t.logger.info("Plugin rich text version: "+y+"."),y!==n.version&&console.error("注意:请统一 RichText 插件与 Player 版本,不统一的版本混用会有不可预知的后果!","\nAttention: Please ensure the RichText plugin is synchronized with the Player version. Mixing and matching incompatible versions may result in unpredictable consequences!"),exports.RichTextLoader=a,exports.generateProgram=v,exports.isRichText=function(t){var e=u(t).filter((function(t){var e=t.tokenType;return"ContextStart"===e||"ContextEnd"===e})),n=e.filter((function(t){return"ContextStart"===t.tokenType})),r=e.filter((function(t){return"ContextEnd"===t.tokenType}));return!(n.length!==r.length||!n.length)&&function t(e,n){var r=e[0],i=e.slice(1);if(void 0===n&&(n=[]),!r)return 0===n.length;if("ContextStart"===r.tokenType)return t(i,n.concat(r.value));if("ContextEnd"===r.tokenType)return n[n.length-1]===r.value&&t(i,n.slice(0,-1));throw new Error("Unexpected token: "+r.tokenType)}(e.map((function(t){var e=t.tokenType,n=t.value;return{tokenType:e,value:"ContextStart"===e?n.match(h)[1]:n.match(c)[1]}})))},exports.lexer=u,exports.richTextParser=p,exports.version=y;
596
2
  //# sourceMappingURL=alipay.js.map