also serialize to text and method to tell us if we need html for model

pull/21833/head
Bruno Windels 2019-05-14 10:37:16 +01:00
parent 2fbe73e658
commit 3abdf6b100
2 changed files with 43 additions and 14 deletions

View File

@ -1,14 +0,0 @@
export function htmlSerialize(model) {
return model.parts.reduce((html, part) => {
switch (part.type) {
case "newline":
return html + "<br />";
case "plain":
case "pill-candidate":
return html + part.text;
case "room-pill":
case "user-pill":
return html + `<a href="https://matrix.to/#/${part.resourceId}">${part.text}</a>`;
}
}, "");
}

43
src/editor/serialize.js Normal file
View File

@ -0,0 +1,43 @@
export function htmlSerialize(model) {
return model.parts.reduce((html, part) => {
switch (part.type) {
case "newline":
return html + "<br />";
case "plain":
case "pill-candidate":
return html + part.text;
case "room-pill":
case "user-pill":
return html + `<a href="https://matrix.to/#/${part.resourceId}">${part.text}</a>`;
}
}, "");
}
export function textSerialize(model) {
return model.parts.reduce((text, part) => {
switch (part.type) {
case "newline":
return text + "\n";
case "plain":
case "pill-candidate":
return text + part.text;
case "room-pill":
case "user-pill":
return text + `${part.resourceId}`;
}
}, "");
}
export function requiresHtml(model) {
return model.parts.some(part => {
switch (part.type) {
case "newline":
case "plain":
case "pill-candidate":
return false;
case "room-pill":
case "user-pill":
return true;
}
});
}