striven-editor

Maintained and contributed to an open source WYSIWYG editor as an internship project that made it to production and was adopted by the company I was working at.
git clone https://unitehenry.com/git/striven-editor.git
Log | Files | Refs | README | LICENSE

commit f56ce3a25737a447f688a81e9b5e7757f664c0df
parent 1dcbbec41d73b46e8d7fef50079b442487f73d8c
Author: ebutler <e.c.butler@outlook.com>
Date:   Tue, 28 Sep 2021 17:14:00 -0400

added package for html formatter and formatted text when switching to html mode

Diffstat:
Mpackage-lock.json | 7++++++-
Mpackage.json | 1+
Msrc/striveneditor.js | 19++++++++++++++++++-
3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/package-lock.json b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@striven-erp/striven-editor", - "version": "3.9.9", + "version": "3.9.10", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5833,6 +5833,11 @@ "parse-passwd": "^1.0.0" } }, + "html-formatter": { + "version": "0.1.9", + "resolved": "https://registry.npmjs.org/html-formatter/-/html-formatter-0.1.9.tgz", + "integrity": "sha1-WOiFlxLz1NnnCUAnUXxMRvTDNrg=" + }, "htmlparser2": { "version": "3.10.1", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz", diff --git a/package.json b/package.json @@ -52,6 +52,7 @@ "webpack-cli": "^3.3.9" }, "dependencies": { + "html-formatter": "^0.1.9", "js-beautify": "^1.11.0", "linkifyjs": "^2.1.9" } diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -31,6 +31,9 @@ import linkify from 'linkifyjs/element'; import './classic.min.css'; import Pickr from './pickr.min.js'; +// Formatting +import htmlFormatter from "html-formatter"; + /* Represents an instance of the Striven Editor */ export default class StrivenEditor { /** @@ -3011,6 +3014,7 @@ export default class StrivenEditor { switch (command) { case 'html': + const saveoption = document.createElement('div'); saveoption.classList.add('se-toolbar-option'); saveoption.setAttribute('title', 'Design'); @@ -3021,6 +3025,8 @@ export default class StrivenEditor { saveoption.onclick = () => { saveoption.remove(); se.body.style.fontFamily = null; + // Remove white-space: pre to prevent text wrapping weirdly + se.body.style.whiteSpace = null; se.toolbar.classList.remove('se-html'); se.setContent(se.body.textContent); @@ -3029,7 +3035,18 @@ export default class StrivenEditor { se.toolbar.prepend(saveoption); se.body.style.fontFamily = 'Courier'; - se.body.textContent = se.body.innerHTML; + + // Add this so that the textContent will wrap at line breaks (not br tags) + se.body.style.whiteSpace = "pre"; + + // Format the HTML so that it looks somewhat pretty + let formatedHtml = htmlFormatter.render(se.body.innerHTML, ); + // This library uses /t for indenting. Replace with spaces to make it less + // drastic. + formatedHtml = formatedHtml.replaceAll("\t", " "); + // Set the content of the editor to the formatted html + se.body.textContent = formatedHtml; + break; case 'fullscreen': const opt = se.toolbar.querySelector('#toolbar-fullscreen');