From 55f2d79d077a83eeecb0bc34ab16cc222d5df57d Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Mon, 10 May 2021 21:37:27 +0530 Subject: [PATCH] Move newline handling to MessageBodyBuilder - Also add a insert method to reflect new design. Signed-off-by: RMidhunSuresh --- .../room/timeline/MessageBodyBuilder.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/domain/session/room/timeline/MessageBodyBuilder.js b/src/domain/session/room/timeline/MessageBodyBuilder.js index 2ed8ed47..3b297cbd 100644 --- a/src/domain/session/room/timeline/MessageBodyBuilder.js +++ b/src/domain/session/room/timeline/MessageBodyBuilder.js @@ -1,9 +1,28 @@ +import { linkify } from "./linkify.js"; + export class MessageBodyBuilder { constructor(message = []) { this._root = message; } + fromText(text) { + const components = text.split("\n"); + components.slice(0, -1).forEach(t => { + linkify(t, this.insert.bind(this)); + }); + const [last] = components.slice(-1); + linkify(last, this.insert.bind(this)); + } + + insert(text, isLink) { + if (!text.length) return; + if (isLink) + this.insertLink(text, text); + else + this.insertText(text); + } + insertText(text) { if (text.length) this._root.push({ type: "text", text: text });