Commit e3c51ee3 authored by rockyl's avatar rockyl

解决html文本的问题

parent d364a706
...@@ -561,18 +561,19 @@ export class TextField extends Sprite { ...@@ -561,18 +561,19 @@ export class TextField extends Sprite {
} else { } else {
//textWidth取每行最大值,如果没设置过textWidth //textWidth取每行最大值,如果没设置过textWidth
let measureCache = {}; let measureCache = {};
if (!textWidth) { const shouldMeasureTextWidth = !textWidth;
for (let i = 0, l = hardLines.length; i < l; i++) { for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i]; let str = hardLines[i];
if (!str) continue; if (!str) continue;
let lineWidth = 0; let lineWidth = 0;
for (let char of str) { for (let char of str) {
let charWidth = measureCache[char]; let charWidth = measureCache[char];
if (charWidth === undefined) { if (charWidth === undefined) {
charWidth = measureCache[char] = s._getMeasuredWidth(char); charWidth = measureCache[char] = s._getMeasuredWidth(char);
}
lineWidth += charWidth;
} }
lineWidth += charWidth;
}
if(shouldMeasureTextWidth){
textWidth = Math.max(lineWidth, textWidth); textWidth = Math.max(lineWidth, textWidth);
} }
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
*/ */
import {TextField} from "../../../2d/text"; import {TextField} from "../../../2d/text";
import {dataCenter} from "../data-center"; import {dataCenter} from "../data-center";
import {ESCAPE_REG_EXP} from "../../utils"; import {ESCAPE_REG_EXP, htmlToPureText} from "../../utils";
import {Event} from "../../../2d/events"; import {Event} from "../../../2d/events";
/** /**
...@@ -14,7 +14,6 @@ export class Label extends TextField { ...@@ -14,7 +14,6 @@ export class Label extends TextField {
private _escapes = []; private _escapes = [];
private _registeredEvents = []; private _registeredEvents = [];
private _htmlText; private _htmlText;
private _el;
/** /**
* 重载文本设置 * 重载文本设置
...@@ -26,24 +25,28 @@ export class Label extends TextField { ...@@ -26,24 +25,28 @@ export class Label extends TextField {
let matchResult = value.match(ESCAPE_REG_EXP); let matchResult = value.match(ESCAPE_REG_EXP);
if (matchResult) { if (matchResult) {
this.unregisterEvents(); this.dealEscape(value, matchResult, this.onTextMutated);
this._originText = value;
this._escapes.splice(0);
for (let item of matchResult) {
let escape = item.substr(2, item.length - 3);
let args = escape.split('|');
let eventName = args[0];
this._escapes.push(escape);
dataCenter.addEventListener(eventName, this.onDataMutated, this);
this._registeredEvents.push(eventName);
}
this.onDataMutated();
} else { } else {
super._setText(value); super._setText(value);
} }
} }
} }
private dealEscape(value, matchResult, onMutated) {
this.unregisterEvents();
this._originText = value;
this._escapes.splice(0);
for (let item of matchResult) {
let escape = item.substr(2, item.length - 3);
let args = escape.split('|');
let eventName = args[0];
this._escapes.push(escape);
dataCenter.addEventListener(eventName, onMutated, this);
this._registeredEvents.push(eventName);
}
onMutated();
}
get htmlText() { get htmlText() {
return this._htmlText; return this._htmlText;
} }
...@@ -51,19 +54,17 @@ export class Label extends TextField { ...@@ -51,19 +54,17 @@ export class Label extends TextField {
/** /**
* 设置html文本 * 设置html文本
*/ */
set htmlText(v) { set htmlText(value) {
if (this._htmlText != v) { if (this._htmlText != value) {
this._htmlText = v; this._htmlText = value;
let el = this._el; let matchResult = value.match(ESCAPE_REG_EXP);
if(!el){
el = this._el = document.createElement('div'); if (matchResult) {
this.dealEscape(value, matchResult, this.onHtmlMutated);
} else {
this.text = htmlToPureText(value);
} }
el.innerHTML = v;
document.body.append(el);
let pureText = el.innerText;
document.body.removeChild(el);
this.text = pureText;
} }
} }
...@@ -72,16 +73,25 @@ export class Label extends TextField { ...@@ -72,16 +73,25 @@ export class Label extends TextField {
*/ */
private unregisterEvents() { private unregisterEvents() {
for (let item of this._registeredEvents) { for (let item of this._registeredEvents) {
dataCenter.removeEventListener(item, this.onDataMutated, this); dataCenter.removeEventListener(item, this.onTextMutated, this);
} }
this._registeredEvents.splice(0); this._registeredEvents.splice(0);
} }
/** /**
* 当数据改变时 * 当TEXT改变时
* @param e * @param e
*/ */
private onDataMutated(e = null) { private onTextMutated = (e = null) => {
super._setText(dataCenter.formatString(this._originText, this._escapes)); super._setText(dataCenter.formatString(this._originText, this._escapes));
} }
/**
* 当HTML改变时
* @param e
*/
private onHtmlMutated = (e = null) => {
let html = dataCenter.formatString(this._originText, this._escapes);
this.text = htmlToPureText(html);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment