Transform new message format into DOM elements

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-05-08 19:22:49 +05:30
parent 2f883d049c
commit bbeb0a5764

View file

@ -21,7 +21,7 @@ import {renderMessage} from "./common.js";
export class TextMessageView extends TemplateView { export class TextMessageView extends TemplateView {
render(t, vm) { render(t, vm) {
const bodyView = t.mapView(vm => vm.text, text => new BodyView(text)); const bodyView = t.mapView(vm => vm.messageFormat, messageFormat => new BodyView(messageFormat));
return renderMessage(t, vm, return renderMessage(t, vm,
[t.p([bodyView, t.time({className: {hidden: !vm.date}}, vm.date + " " + vm.time)])] [t.p([bodyView, t.time({className: {hidden: !vm.date}}, vm.date + " " + vm.time)])]
); );
@ -29,20 +29,22 @@ export class TextMessageView extends TemplateView {
} }
class BodyView extends StaticView { class BodyView extends StaticView {
get _formatFunction() {
return {
text: (param) => text(param.obj.text),
link: (param) => param.t.a({ href: param.obj.url, target: "_blank", rel: "noopener" }, [text(param.obj.text)]),
newline: (param) => param.t.br()
};
}
render(t, value) { render(t, value) {
const lines = (value || "").split("\n"); const children = [];
if (lines.length === 1) { for (const m of value) {
return text(lines[0]); const f = this._formatFunction[m.type];
const element = f({ obj: m, t: t });
children.push(element);
} }
const elements = []; return t.span(children);
for (const line of lines) {
if (elements.length) {
elements.push(t.br());
}
if (line.length) {
elements.push(t.span(line));
}
}
return t.span(elements);
} }
} }