Commit e3c51ee3 authored by rockyl's avatar rockyl

解决html文本的问题

parent d364a706
......@@ -561,18 +561,19 @@ export class TextField extends Sprite {
} else {
//textWidth取每行最大值,如果没设置过textWidth
let measureCache = {};
if (!textWidth) {
for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i];
if (!str) continue;
let lineWidth = 0;
for (let char of str) {
let charWidth = measureCache[char];
if (charWidth === undefined) {
charWidth = measureCache[char] = s._getMeasuredWidth(char);
}
lineWidth += charWidth;
const shouldMeasureTextWidth = !textWidth;
for (let i = 0, l = hardLines.length; i < l; i++) {
let str = hardLines[i];
if (!str) continue;
let lineWidth = 0;
for (let char of str) {
let charWidth = measureCache[char];
if (charWidth === undefined) {
charWidth = measureCache[char] = s._getMeasuredWidth(char);
}
lineWidth += charWidth;
}
if(shouldMeasureTextWidth){
textWidth = Math.max(lineWidth, textWidth);
}
}
......
......@@ -3,7 +3,7 @@
*/
import {TextField} from "../../../2d/text";
import {dataCenter} from "../data-center";
import {ESCAPE_REG_EXP} from "../../utils";
import {ESCAPE_REG_EXP, htmlToPureText} from "../../utils";
import {Event} from "../../../2d/events";
/**
......@@ -14,7 +14,6 @@ export class Label extends TextField {
private _escapes = [];
private _registeredEvents = [];
private _htmlText;
private _el;
/**
* 重载文本设置
......@@ -26,24 +25,28 @@ export class Label extends TextField {
let matchResult = value.match(ESCAPE_REG_EXP);
if (matchResult) {
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, this.onDataMutated, this);
this._registeredEvents.push(eventName);
}
this.onDataMutated();
this.dealEscape(value, matchResult, this.onTextMutated);
} else {
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() {
return this._htmlText;
}
......@@ -51,19 +54,17 @@ export class Label extends TextField {
/**
* 设置html文本
*/
set htmlText(v) {
if (this._htmlText != v) {
this._htmlText = v;
set htmlText(value) {
if (this._htmlText != value) {
this._htmlText = value;
let el = this._el;
if(!el){
el = this._el = document.createElement('div');
let matchResult = value.match(ESCAPE_REG_EXP);
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 {
*/
private unregisterEvents() {
for (let item of this._registeredEvents) {
dataCenter.removeEventListener(item, this.onDataMutated, this);
dataCenter.removeEventListener(item, this.onTextMutated, this);
}
this._registeredEvents.splice(0);
}
/**
* 当数据改变时
* 当TEXT改变时
* @param e
*/
private onDataMutated(e = null) {
private onTextMutated = (e = null) => {
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