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 b5b2250d6e124f936a9ebcc2a0ba176cf15aae27
parent 3b65b4270369bf7c644456dbabc9280780f11c08
Author: Eric Butler <ebutler@striven.com>
Date:   Sun, 23 Mar 2025 16:59:41 -0400

Fixed inserting images when there is no selection in body. Resolves issue #167

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 @@ -3401,6 +3401,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 @@ -3422,6 +3463,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) => {