Switch tiles to using enums and checking format.

This commit is contained in:
Danila Fedorin 2021-07-16 13:36:26 -07:00
parent 1e2945ca54
commit 9e1f57a2b1
3 changed files with 23 additions and 15 deletions

View file

@ -16,12 +16,15 @@ limitations under the License.
import {BaseMessageTile} from "./BaseMessageTile.js";
import {stringAsBody} from "../MessageBody.js";
import {createEnum} from "../../../../../utils/enum.js";
export const TextTileFormat = createEnum("Plain", "Html");
export class BaseTextTile extends BaseMessageTile {
constructor(options) {
super(options);
this._messageBody = null;
this._messageFormat = null
this._format = null
}
get shape() {
@ -33,7 +36,7 @@ export class BaseTextTile extends BaseMessageTile {
}
_getBodyFormat() {
return "plain";
return TextTileFormat.Plain;
}
get body() {
@ -43,13 +46,13 @@ export class BaseTextTile extends BaseMessageTile {
// 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 || this._messageFormat !== format) {
if (!this._messageBody || this._messageBody.sourceString !== body || this._format !== 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, format);
this._messageFormat = body.format;
this._format = body.format;
}
return this._messageBody;
}

View file

@ -42,6 +42,6 @@ export class EncryptedEventTile extends BaseTextTile {
} else {
string = decryptionError?.message || this.i18n`Could not decrypt message because of unknown reason.`;
}
return { string, format: "plain" };
return string;
}
}

View file

@ -14,17 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {BaseTextTile} from "./BaseTextTile.js";
import {BaseTextTile, TextTileFormat} from "./BaseTextTile.js";
import {parsePlainBody} from "../MessageBody.js";
import {parseHTMLBody} from "../deserialize.js";
export class TextTile extends BaseTextTile {
_getContentString(key, fallback = null) {
_getContentString(key) {
const content = this._getContent();
let val = content?.[key] || fallback;
if (!val && val !== "") { // empty string is falsy, but OK here.
return null;
}
let val = content?.[key] || "";
if (content.msgtype === "m.emote") {
val = `* ${this.displayName} ${val}`;
}
@ -32,7 +29,7 @@ export class TextTile extends BaseTextTile {
}
_getPlainBody() {
return this._getContentString("body", "");
return this._getContentString("body");
}
_getFormattedBody() {
@ -40,15 +37,23 @@ export class TextTile extends BaseTextTile {
}
_getBody() {
return this._getFormattedBody() || this._getPlainBody();
if (this._getBodyFormat() == TextTileFormat.Html) {
return this._getFormattedBody();
} else {
return this._getPlainBody();
}
}
_getBodyFormat() {
return this._getContent()?.["formatted_body"] ? "html" : "plain"
if (this._getContent()?.["format"] === "org.matrix.custom.html") {
return TextTileFormat.Html;
} else {
return TextTileFormat.Plain;
}
}
_parseBody(body, format) {
if (format === "html") {
if (format === TextTileFormat.Html) {
return parseHTMLBody(this.platform, this._mediaRepository, body);
} else {
return parsePlainBody(body);