forked from mystiq/hydrogen-web
Switch tiles to using enums and checking format.
This commit is contained in:
parent
1e2945ca54
commit
9e1f57a2b1
3 changed files with 23 additions and 15 deletions
|
@ -16,12 +16,15 @@ limitations under the License.
|
||||||
|
|
||||||
import {BaseMessageTile} from "./BaseMessageTile.js";
|
import {BaseMessageTile} from "./BaseMessageTile.js";
|
||||||
import {stringAsBody} from "../MessageBody.js";
|
import {stringAsBody} from "../MessageBody.js";
|
||||||
|
import {createEnum} from "../../../../../utils/enum.js";
|
||||||
|
|
||||||
|
export const TextTileFormat = createEnum("Plain", "Html");
|
||||||
|
|
||||||
export class BaseTextTile extends BaseMessageTile {
|
export class BaseTextTile extends BaseMessageTile {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super(options);
|
super(options);
|
||||||
this._messageBody = null;
|
this._messageBody = null;
|
||||||
this._messageFormat = null
|
this._format = null
|
||||||
}
|
}
|
||||||
|
|
||||||
get shape() {
|
get shape() {
|
||||||
|
@ -33,7 +36,7 @@ export class BaseTextTile extends BaseMessageTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
_getBodyFormat() {
|
_getBodyFormat() {
|
||||||
return "plain";
|
return TextTileFormat.Plain;
|
||||||
}
|
}
|
||||||
|
|
||||||
get body() {
|
get body() {
|
||||||
|
@ -43,13 +46,13 @@ export class BaseTextTile extends BaseMessageTile {
|
||||||
// doing an equality check
|
// doing an equality check
|
||||||
// Even if the body hasn't changed, but the format has, we need
|
// Even if the body hasn't changed, but the format has, we need
|
||||||
// to re-fill our cache.
|
// 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,
|
// body with markup is an array of parts,
|
||||||
// so we should not recreate it for the same body string,
|
// so we should not recreate it for the same body string,
|
||||||
// or else the equality check in the binding will always fail.
|
// or else the equality check in the binding will always fail.
|
||||||
// So cache it here.
|
// So cache it here.
|
||||||
this._messageBody = this._parseBody(body, format);
|
this._messageBody = this._parseBody(body, format);
|
||||||
this._messageFormat = body.format;
|
this._format = body.format;
|
||||||
}
|
}
|
||||||
return this._messageBody;
|
return this._messageBody;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,6 @@ export class EncryptedEventTile extends BaseTextTile {
|
||||||
} else {
|
} else {
|
||||||
string = decryptionError?.message || this.i18n`Could not decrypt message because of unknown reason.`;
|
string = decryptionError?.message || this.i18n`Could not decrypt message because of unknown reason.`;
|
||||||
}
|
}
|
||||||
return { string, format: "plain" };
|
return string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,17 +14,14 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {BaseTextTile} from "./BaseTextTile.js";
|
import {BaseTextTile, TextTileFormat} from "./BaseTextTile.js";
|
||||||
import {parsePlainBody} from "../MessageBody.js";
|
import {parsePlainBody} from "../MessageBody.js";
|
||||||
import {parseHTMLBody} from "../deserialize.js";
|
import {parseHTMLBody} from "../deserialize.js";
|
||||||
|
|
||||||
export class TextTile extends BaseTextTile {
|
export class TextTile extends BaseTextTile {
|
||||||
_getContentString(key, fallback = null) {
|
_getContentString(key) {
|
||||||
const content = this._getContent();
|
const content = this._getContent();
|
||||||
let val = content?.[key] || fallback;
|
let val = content?.[key] || "";
|
||||||
if (!val && val !== "") { // empty string is falsy, but OK here.
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (content.msgtype === "m.emote") {
|
if (content.msgtype === "m.emote") {
|
||||||
val = `* ${this.displayName} ${val}`;
|
val = `* ${this.displayName} ${val}`;
|
||||||
}
|
}
|
||||||
|
@ -32,7 +29,7 @@ export class TextTile extends BaseTextTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
_getPlainBody() {
|
_getPlainBody() {
|
||||||
return this._getContentString("body", "");
|
return this._getContentString("body");
|
||||||
}
|
}
|
||||||
|
|
||||||
_getFormattedBody() {
|
_getFormattedBody() {
|
||||||
|
@ -40,15 +37,23 @@ export class TextTile extends BaseTextTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
_getBody() {
|
_getBody() {
|
||||||
return this._getFormattedBody() || this._getPlainBody();
|
if (this._getBodyFormat() == TextTileFormat.Html) {
|
||||||
|
return this._getFormattedBody();
|
||||||
|
} else {
|
||||||
|
return this._getPlainBody();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_getBodyFormat() {
|
_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) {
|
_parseBody(body, format) {
|
||||||
if (format === "html") {
|
if (format === TextTileFormat.Html) {
|
||||||
return parseHTMLBody(this.platform, this._mediaRepository, body);
|
return parseHTMLBody(this.platform, this._mediaRepository, body);
|
||||||
} else {
|
} else {
|
||||||
return parsePlainBody(body);
|
return parsePlainBody(body);
|
||||||
|
|
Loading…
Reference in a new issue