forked from mystiq/hydrogen-web
Avoid intermediate objects while parsing TextTiles' bodies.
This commit is contained in:
parent
9304ca330e
commit
c261b9fb23
2 changed files with 20 additions and 12 deletions
|
@ -28,22 +28,27 @@ export class BaseTextTile extends BaseMessageTile {
|
|||
return "message";
|
||||
}
|
||||
|
||||
_parseBody(body) {
|
||||
_parseBody(body, _) {
|
||||
return stringAsBody(body.string);
|
||||
}
|
||||
|
||||
_getBodyFormat() {
|
||||
return "plain";
|
||||
}
|
||||
|
||||
get body() {
|
||||
const body = this._getBody();
|
||||
const format = this._getBodyFormat();
|
||||
// body.string is a string, so we can check for difference by just
|
||||
// doing an equality check
|
||||
// Even if the body hasn't changed, but the format has, we need
|
||||
// to re-fill our cache.
|
||||
if (!this._messageBody || this._messageBody.sourceString !== body.string || this._messageFormat !== body.format) {
|
||||
if (!this._messageBody || this._messageBody.sourceString !== body || this._messageFormat !== format) {
|
||||
// body with markup is an array of parts,
|
||||
// so we should not recreate it for the same body string,
|
||||
// or else the equality check in the binding will always fail.
|
||||
// So cache it here.
|
||||
this._messageBody = this._parseBody(body);
|
||||
this._messageBody = this._parseBody(body, format);
|
||||
this._messageFormat = body.format;
|
||||
}
|
||||
return this._messageBody;
|
||||
|
|
|
@ -19,7 +19,7 @@ import {parsePlainBody} from "../MessageBody.js";
|
|||
import {parseHTMLBody} from "../deserialize.js";
|
||||
|
||||
export class TextTile extends BaseTextTile {
|
||||
_getContentString(key, format, fallback = null) {
|
||||
_getContentString(key, fallback = null) {
|
||||
const content = this._getContent();
|
||||
let val = content?.[key] || fallback;
|
||||
if (!val && val !== "") { // empty string is falsy, but OK here.
|
||||
|
@ -28,27 +28,30 @@ export class TextTile extends BaseTextTile {
|
|||
if (content.msgtype === "m.emote") {
|
||||
val = `* ${this.displayName} ${body}`;
|
||||
}
|
||||
return { string: val, format };
|
||||
return val;
|
||||
}
|
||||
|
||||
_getPlainBody() {
|
||||
return this._getContentString("body", "plain", "");
|
||||
return this._getContentString("body", "");
|
||||
}
|
||||
|
||||
_getFormattedBody() {
|
||||
return this._getContentString("formatted_body", "html");
|
||||
return this._getContentString("formatted_body");
|
||||
}
|
||||
|
||||
_getBody() {
|
||||
return this._getFormattedBody() || this._getPlainBody();
|
||||
}
|
||||
|
||||
_parseBody(body) {
|
||||
const string = body.string;
|
||||
if (body.format === "html") {
|
||||
return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, string);
|
||||
_getBodyFormat() {
|
||||
return this._getContent()?.["formatted_body"] ? "html" : "plain"
|
||||
}
|
||||
|
||||
_parseBody(body, format) {
|
||||
if (format === "html") {
|
||||
return parseHTMLBody({ mediaRepository: this._mediaRepository, platform: this.platform }, body);
|
||||
} else {
|
||||
return parsePlainBody(string);
|
||||
return parsePlainBody(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue