Add some prototype rendering implementations.
This commit is contained in:
parent
cc506756a1
commit
db202b23ae
1 changed files with 14 additions and 3 deletions
|
@ -36,18 +36,29 @@ export class TextMessageView extends BaseMessageView {
|
||||||
* Map from part to function that outputs DOM for the part
|
* Map from part to function that outputs DOM for the part
|
||||||
*/
|
*/
|
||||||
const formatFunction = {
|
const formatFunction = {
|
||||||
|
header: headerBlock => tag["h" + Math.min(6,headerBlock.level)]({}, renderParts(headerBlock.inlines)),
|
||||||
|
codeblock: codeBlock => tag.pre({}, tag.code({}, text(codeBlock.text))),
|
||||||
|
emph: emphPart => tag.em({}, [renderPart(emphPart.wraps)]),
|
||||||
|
code: codePart => tag.code({}, text(codePart.text)),
|
||||||
text: textPart => text(textPart.text),
|
text: textPart => text(textPart.text),
|
||||||
link: linkPart => tag.a({ href: linkPart.url, target: "_blank", rel: "noopener" }, [linkPart.text]),
|
link: linkPart => tag.a({ href: linkPart.url, target: "_blank", rel: "noopener" }, [linkPart.text]),
|
||||||
newline: () => tag.br()
|
newline: () => tag.br()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function renderPart(part) {
|
||||||
|
const f = formatFunction[part.type];
|
||||||
|
return f(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderParts(parts) {
|
||||||
|
return Array.from(parts, renderPart);
|
||||||
|
}
|
||||||
|
|
||||||
class BodyView extends StaticView {
|
class BodyView extends StaticView {
|
||||||
render(t, messageBody) {
|
render(t, messageBody) {
|
||||||
const container = t.span();
|
const container = t.span();
|
||||||
for (const part of messageBody.parts) {
|
for (const part of messageBody.parts) {
|
||||||
const f = formatFunction[part.type];
|
container.appendChild(renderPart(part));
|
||||||
const element = f(part);
|
|
||||||
container.appendChild(element);
|
|
||||||
}
|
}
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue