mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
Move newline handling to MessageBodyBuilder
- Also add a insert method to reflect new design. Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
parent
787d438a74
commit
55f2d79d07
@ -1,9 +1,28 @@
|
|||||||
|
import { linkify } from "./linkify.js";
|
||||||
|
|
||||||
export class MessageBodyBuilder {
|
export class MessageBodyBuilder {
|
||||||
|
|
||||||
constructor(message = []) {
|
constructor(message = []) {
|
||||||
this._root = 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) {
|
insertText(text) {
|
||||||
if (text.length)
|
if (text.length)
|
||||||
this._root.push({ type: "text", text: text });
|
this._root.push({ type: "text", text: text });
|
||||||
|
Loading…
Reference in New Issue
Block a user