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 21830b995432dd4749716cc8bbfc38709b625cbd
parent a9d61d9e1677a1e4bab5b7e586ffaf7227a92f7c
Author: Eric Butler <ebutler@striven.com>
Date:   Sun, 23 Mar 2025 17:00:20 -0400

Merge branch 'dev'

Diffstat:
Mpackage.json | 2+-
Msrc/striveneditor.js | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "3.19.12", + "version": "3.19.13", "description": "The editor used for the Striven ERP system.", "main": "./dist", "scripts": { diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -3400,6 +3400,47 @@ export default class StrivenEditor { } /** + * Ensures that the selection is within the body of the editor + * @param {*} contentEditableElement + */ + ensureSelectionInBody(contentEditableElement) { + if (contentEditableElement) { + const selection = window.getSelection(); + + // Check if the contentEditableElement has focus and the selection is within it + if (document.activeElement !== contentEditableElement || !contentEditableElement.contains(selection.anchorNode)) { + contentEditableElement.focus(); // Ensure the element has focus + + const range = document.createRange(); + + if (contentEditableElement.childNodes.length > 0) { + const lastChild = contentEditableElement.childNodes[contentEditableElement.childNodes.length - 1]; + + if (lastChild.nodeType === Node.TEXT_NODE) { + range.setStart(lastChild, lastChild.length); + range.collapse(true); + selection.removeAllRanges(); + selection.addRange(range); + } else { + // If the last child is an element node, set the cursor after it. + range.selectNodeContents(contentEditableElement); + range.collapse(false); // Collapse to the end + selection.removeAllRanges(); + selection.addRange(range); + } + + } else { + // If contenteditable is empty. + range.selectNodeContents(contentEditableElement); + range.collapse(false); + selection.removeAllRanges(); + selection.addRange(range); + } + } + } + } + + /** * Inserts a node into the editor * @param {*} node * @param {*} range @@ -3421,6 +3462,9 @@ export default class StrivenEditor { */ insertImages(files) { const se = this; + + console.log("Inserting images"); + se.ensureSelectionInBody(se.body); const currentRange = se.getRange(); return new Promise((resolve) => {