commit 12e35a16205ad3c108b3b5320aa59a05230cfaf6
parent be3f58f065272875672ec23f63aa21cf724e0141
Author: Eric Butler <ebutler@striven.com>
Date: Fri, 26 Sep 2025 16:40:17 -0400
possible fix for issue #169 - all text on page up to the editor is selected on focus when editor is empty
Diffstat:
2 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/package-lock.json b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@striven-erp/striven-editor",
- "version": "3.19.4",
+ "version": "3.19.14",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@striven-erp/striven-editor",
- "version": "3.19.4",
+ "version": "3.19.14",
"license": "MIT",
"dependencies": {
"@simonwep/pickr": "^1.9.1",
diff --git a/src/striveneditor.js b/src/striveneditor.js
@@ -1067,17 +1067,8 @@ export default class StrivenEditor {
se.editor.classList.add('se-focus');
if (se.body.textContent.trim() === '') {
- const r = se.getRange();
+ se.placeCaretAtEnd(se.body);
- if (r) {
- const selNode = document.createTextNode('');
- se.body.append(selNode);
-
- setTimeout(() => {
- se.getRange().selectNode(selNode);
- selNode.remove();
- }, 0);
- }
}
if (se.scrollPosition && !se.browser.isEdge()) {
@@ -2130,6 +2121,27 @@ export default class StrivenEditor {
}
/**
+ * Places the caret at the end of the specified element. See https://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser
+ * @param {HTMLElement} el Element to place the caret at the end of
+ */
+ placeCaretAtEnd(el) {
+ if (typeof window.getSelection != "undefined"
+ && typeof document.createRange != "undefined") {
+ var range = document.createRange();
+ range.selectNodeContents(el);
+ range.collapse(false);
+ var sel = window.getSelection();
+ sel.removeAllRanges();
+ sel.addRange(range);
+ } else if (typeof document.body.createTextRange != "undefined") {
+ var textRange = document.body.createTextRange();
+ textRange.moveToElementText(el);
+ textRange.collapse(false);
+ textRange.select();
+ }
+ }
+
+ /**
* Get the range of the window
* @returns {Range} Range of the window
*/
@@ -2137,11 +2149,11 @@ export default class StrivenEditor {
try {
const selection = window.getSelection();
if (selection) {
- return window.getSelection().getRangeAt(0);
+ return selection.getRangeAt(0);
}
} catch (e) {}
}
-
+
/**
* Gets the meta data from the metaUrl option
* @param {String} url Url to fetch meta data from
@@ -3465,8 +3477,6 @@ export default class StrivenEditor {
*/
insertImages(files) {
const se = this;
-
- console.log("Inserting images");
se.ensureSelectionInBody(se.body);
const currentRange = se.getRange();