Skip to content

Commit

Permalink
fix tag handling from/to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Apr 7, 2024
1 parent 3421dae commit 3ece869
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions app/packs/src/decidim/cfj/editor/extensions/html_edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export default Extension.create({
cancelText: "キャンセル",
};

const noNewlines = (src) => {
const replaced = src
.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, "</$1>") // 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<li>/g, "</li><li>") // remove spaces between list items, they tend to break quill
.replace(/\s<\//g, "</") // remove spaces before the end of tags
.replace(/(<[^\/<>]+>)\s(<[^\/<>]+>)/g, "$1$2") // remove space between multiple starting tags
.trim();
return replaced;
};

const createEditorContainer = (options) => {
const overlayContainer = document.createElement("div");
overlayContainer.setAttribute("class", "html-edit-overlay");
Expand Down Expand Up @@ -45,7 +58,7 @@ export default Extension.create({
document.body.appendChild(overlayContainer);

buttonOk.addEventListener('click', () => {
const updatedHtml = htmlEditTextarea.value;
const updatedHtml = noNewlines(htmlEditTextarea.value);
this.editor.commands.setContent(updatedHtml, { html: true });
overlayContainer.style.display = 'none';
});
Expand Down Expand Up @@ -124,19 +137,6 @@ export default Extension.create({
return result;
}

const noNewlines = (src) => {
const replaced = src
.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, "</$1>") // 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<li>/g, "</li><li>") // remove spaces between list items, they tend to break quill
.replace(/\s<\//g, "</") // remove spaces before the end of tags
.replace(/(<[^\/<>]+>)\s(<[^\/<>]+>)/g, "$1$2") // remove space between multiple starting tags
.trim();
return replaced;
};

const html = editor.getHTML();
document.querySelector('.html-edit-textarea').value = formatHtml(html);
document.querySelector('.html-edit-overlay').style.display = 'flex';
Expand Down

0 comments on commit 3ece869

Please sign in to comment.