Skip to content

Commit

Permalink
fix: remove non-breaking space from output
Browse files Browse the repository at this point in the history
This commit will be amended later in the future, as there's still a flaw in paragraph spacing that affects text behavior, e.g., breaking each letter instead of each word.

*Edited: i just did amend it
  • Loading branch information
rezzvy committed Feb 22, 2025
1 parent 1be9fd7 commit bb48377
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/handler/handler_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export default class {
this.view.disable(false, "#modal-edit-save");

let content = this.targetContainer.innerHTML.trim();
content = content.replace(/\ /g, " ").replace(/\u2011/g, "-");

this.view.html(this.editorContainer.firstElementChild, content);
}

Expand All @@ -60,13 +58,29 @@ export default class {
const editorContent = this.editorContainer.firstElementChild;

this.view.els("p", editorContent).forEach((paragraph) => {
const spacingElement = this.view.el("br", paragraph);
const children = paragraph.children;

if (!children) this.view.remove(paragraph);

if (!paragraph.querySelector("br")) {
const text = paragraph.innerHTML;
paragraph.innerHTML = text.replace(/\s+/g, " ");
}

for (const el of children) {
if (el.tagName === "BR") {
this.view.dataset(el, "spacing", "%SPCITM%");
}

if (spacingElement) this.view.dataset(spacingElement, "spacing", "%SPCITM%");
if (!paragraph.innerHTML.trim()) this.view.remove(paragraph);
if (el.textContent === " ") {
el.outerHTML = " ";
}

const spacing = paragraph.innerHTML.replace(/(?<=^|>)[^<>]+(?=<|$)/g, (text) => text.replace(/ /g, "\u00A0").replace(/-/g, "\u2011"));
this.view.html(paragraph, spacing);
if (el.textContent.startsWith(" ")) {
el.innerHTML = el.innerHTML.trim();
el.outerHTML = " " + el.outerHTML;
}
}
});

this.view.html(this.targetContainer, editorContent.innerHTML);
Expand Down
1 change: 1 addition & 0 deletions src/mvc/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export default class Model {
.replace(/^\s+/gm, "") // Removes leading whitespace from lines
.replace(/^[\s]*\r?\n/gm, "") // Removes empty lines
.replace(/%SPCITM%\s*/g, "\n") // Replaces placeholder with newline
.replace(/\u00A0/g, " ")
.trim();
}

Expand Down

0 comments on commit bb48377

Please sign in to comment.