From fb82cb6c8717e496bcf1894ec355840e6596966d Mon Sep 17 00:00:00 2001 From: Evan Sharp Date: Tue, 15 Sep 2020 22:09:19 -0400 Subject: [PATCH] fix: spacing on save previously spaces between tags would be removed causing words to be put together with this change spacing is preserved where it can be, but still removed from places spaces cannot be (such as lists) fixes #18 --- src/quill.htmlEditButton.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/quill.htmlEditButton.js b/src/quill.htmlEditButton.js index 10686d01..a3483253 100644 --- a/src/quill.htmlEditButton.js +++ b/src/quill.htmlEditButton.js @@ -110,8 +110,16 @@ function launchPopupEditor(quill, options) { e.stopPropagation(); }; buttonOk.onclick = function() { - const output = editor.container.querySelector(".ql-editor").innerText.split(/\r?\n/g).map(el => el.trim()); - const noNewlines = output.join(""); + const output = editor.container.querySelector(".ql-editor").innerText; + const noNewlines = output + .replace(/\s+/g, " ") // convert multiple spaces to a single space. This is how HTML treats them + .replace(/(<[^\/<>]+>)\s+/g, "$1") // remove spaces after the start of a new tag + .replace(/<\/(p|ol|ul)>\s/g, "") // remove spaces after the end of lists and paragraphs, they tend to break quill + .replace(/\s<(p|ol|ul)>/g, "<$1>") // remove spaces before the start of lists and paragraphs, they tend to break quill + .replace(/<\/li>\s
  • /g, "
  • ") // remove spaces between list items, they tend to break quill + .replace(/\s<\//g, "]+>)\s(<[^\/<>]+>)/g, "$1$2") // remove space between multiple starting tags + .trim(); quill.container.querySelector(".ql-editor").innerHTML = noNewlines; document.body.removeChild(overlayContainer); }; @@ -138,7 +146,8 @@ function formatHTML(code) { const isBrTag = code.substr(pos, 4) === "
    "; const isOpeningTag = char === "<" && nextChar !== "/" && !isBrTag; const isClosingTag = char === "<" && nextChar === "/" && !isBrTag; - const isTagEnd = prevChar === ">" && char !== "<" && char !== " " && currentIndent > 0; + const isTagEnd = prevChar === ">" && char !== "<" && currentIndent > 0; + const isTagNext = !isBrTag && !isOpeningTag && !isClosingTag && isTagEnd && code.substr(pos, code.substr(pos).indexOf("<")).trim() === ""; if (isBrTag) { // If opening tag, add newline character and indention result += newlineChar; @@ -156,10 +165,6 @@ function formatHTML(code) { if (--currentIndent < 0) currentIndent = 0; result += newlineChar + whitespace.repeat(currentIndent); } - if(isTagEnd) { - result += newlineChar + whitespace.repeat(currentIndent); - } - // remove multiple whitespaces else if (stripWhiteSpaces === true && char === " " && nextChar === " ") char = ""; @@ -169,6 +174,9 @@ function formatHTML(code) { if (code.substr(pos, code.substr(pos).indexOf("<")).trim() === "") char = ""; } + if(isTagEnd && !isTagNext) { + result += newlineChar + whitespace.repeat(currentIndent); + } result += char; }