Fix email editor when plaintext setting enabled
This commit is contained in:
+16
-9
@@ -1,5 +1,6 @@
|
||||
import { _get, _post, toggleLoader } from "../modules/common.js";
|
||||
import { Marked } from "@ts-stack/markdown";
|
||||
import { stripMarkdown } from "../modules/stripmd.js";
|
||||
|
||||
interface settingsBoolEvent extends Event {
|
||||
detail: boolean;
|
||||
@@ -672,17 +673,12 @@ class ombiDefaults {
|
||||
}
|
||||
}
|
||||
|
||||
interface Email {
|
||||
subject: string;
|
||||
html: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
interface templateEmail {
|
||||
content: string;
|
||||
variables: string[];
|
||||
values: { [key: string]: string };
|
||||
html: string;
|
||||
plaintext: string;
|
||||
}
|
||||
|
||||
interface emailListEl {
|
||||
@@ -700,6 +696,7 @@ class EmailEditor {
|
||||
private _variables = document.getElementById("editor-variables") as HTMLDivElement;
|
||||
private _textArea = document.getElementById("textarea-editor") as HTMLTextAreaElement;
|
||||
private _preview = document.getElementById("editor-preview") as HTMLDivElement;
|
||||
private _previewContent: HTMLElement;
|
||||
// private _timeout: number;
|
||||
// private _finishInterval = 200;
|
||||
|
||||
@@ -734,7 +731,12 @@ class EmailEditor {
|
||||
}
|
||||
this._templ = req.response as templateEmail;
|
||||
this._textArea.value = this._templ.content;
|
||||
this._preview.innerHTML = this._templ.html;
|
||||
if (this._templ.html == "") {
|
||||
this._preview.innerHTML = `<pre id="preview-content" class="monospace"></pre>`;
|
||||
} else {
|
||||
this._preview.innerHTML = this._templ.html;
|
||||
}
|
||||
this._previewContent = document.getElementById("preview-content");
|
||||
this.loadPreview();
|
||||
this._content = this._templ.content;
|
||||
const colors = ["info", "urge", "positive", "neutral"];
|
||||
@@ -764,8 +766,13 @@ class EmailEditor {
|
||||
if (value === undefined) { value = variable; }
|
||||
content = content.replace(new RegExp(variable, "g"), value);
|
||||
}
|
||||
content = Marked.parse(content)
|
||||
document.getElementById("preview-content").innerHTML = content;
|
||||
if (this._templ.html == "") {
|
||||
content = stripMarkdown(content);
|
||||
this._previewContent.textContent = content;
|
||||
} else {
|
||||
content = Marked.parse(content);
|
||||
this._previewContent.innerHTML = content;
|
||||
}
|
||||
// _post("/config/emails/" + this._currentID + "/test", { "content": this._textArea.value }, (req: XMLHttpRequest) => {
|
||||
// if (req.readyState == 4) {
|
||||
// if (req.status != 200) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
const removeMd = require("remove-markdown");
|
||||
|
||||
export function stripMarkdown(md: string): string {
|
||||
let foundOpenSquare = false;
|
||||
let openSquare = -1;
|
||||
let openBracket = -1;
|
||||
let closeBracket = -1;
|
||||
let openSquares: number[] = [];
|
||||
let closeBrackets: number[] = [];
|
||||
let links: string[] = [];
|
||||
let foundOpen = false;
|
||||
for (let i = 0; i < md.length; i++) {
|
||||
const c = md.charAt(i);
|
||||
if (!foundOpenSquare && !foundOpen && c != '[' && c != ']') {
|
||||
continue;
|
||||
}
|
||||
if (c == '[' && md.charAt(i-1) != '!') {
|
||||
foundOpenSquare = true;
|
||||
openSquare = i;
|
||||
} else if (c == ']') {
|
||||
if (md.charAt(i+1) == '(') {
|
||||
foundOpenSquare = false;
|
||||
foundOpen = true;
|
||||
openBracket = i + 1;
|
||||
continue;
|
||||
}
|
||||
} else if (c == ')') {
|
||||
closeBracket = i;
|
||||
openSquares.push(openSquare);
|
||||
closeBrackets.push(closeBracket);
|
||||
links.push(md.slice(openBracket+1, closeBracket))
|
||||
openBracket = -1;
|
||||
closeBracket = -1;
|
||||
openSquare = -1;
|
||||
foundOpenSquare = false;
|
||||
foundOpen = false;
|
||||
}
|
||||
}
|
||||
let fullLinks: string[] = new Array(openSquares.length);
|
||||
for (let i = 0; i < openSquares.length; i++) {
|
||||
if (openSquares[i] != -1 && closeBrackets[i] != -1) {
|
||||
fullLinks[i] = md.slice(openSquares[i], closeBrackets[i]+1)
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < openSquares.length; i++) {
|
||||
md = md.replace(fullLinks[i], links[i]);
|
||||
}
|
||||
return removeMd(md);
|
||||
}
|
||||
Reference in New Issue
Block a user