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 92034d5f19ae91cc95fddea7a3dc0972536be49b
parent d37a3281ba7ad26bdb549d23d9578b94fc282b84
Author: Eric Butler <ebutler@striven.com>
Date:   Mon,  3 Mar 2025 15:48:19 -0500

fixes for image upload placeholder

Diffstat:
Mpackage.json | 2+-
Msrc/striveneditor.css | 10+++++++---
Msrc/striveneditor.js | 77++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
3 files changed, 54 insertions(+), 35 deletions(-)

diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "3.19.9", + "version": "3.19.10", "description": "The editor used for the Striven ERP system.", "main": "./dist", "scripts": { diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -202,9 +202,13 @@ } .striven-editor .se-image-uploading::after { - content: 'Uploading...'; - font-size: 16px; - color: #777; + content: ""; + display: block; + width: 100px; /* Adjust as needed */ + height: 100px; /* Adjust as needed */ + background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ctitle%3Ecloud-arrow-up-outline%3C/title%3E%3Cg %3E%3Cpath fill='%23666' d='M4.03 12.03C3.34 12.71 3 13.53 3 14.5S3.34 16.29 4.03 17C4.71 17.66 5.53 18 6.5 18H13.09C13.04 18.33 13 18.66 13 19C13 19.34 13.04 19.67 13.09 20H6.5C5 20 3.69 19.5 2.61 18.43C1.54 17.38 1 16.09 1 14.58C1 13.28 1.39 12.12 2.17 11.1S4 9.43 5.25 9.15C5.67 7.62 6.5 6.38 7.75 5.43S10.42 4 12 4C13.95 4 15.6 4.68 16.96 6.04C18.32 7.4 19 9.05 19 11C20.15 11.13 21.1 11.63 21.86 12.5C22.37 13.07 22.7 13.71 22.86 14.42C21.82 13.54 20.5 13 19 13C18.89 13 18.79 13 18.68 13C18.62 13 18.56 13 18.5 13H17V11C17 9.62 16.5 8.44 15.54 7.46C14.56 6.5 13.38 6 12 6S9.44 6.5 8.46 7.46C7.5 8.44 7 9.62 7 11H6.5C5.53 11 4.71 11.34 4.03 12.03M16 18H18V22H20V18H22L19 15L16 18Z' /%3E%3Canimate attributeName='opacity' values='0.2; 1; 0.2' dur='1.5s' repeatCount='indefinite' /%3E%3C/g%3E%3C/svg%3E"); + background-size: cover; /* Adjust as needed */ + background-repeat: no-repeat; /* Adjust as needed */ } /** Toolbar **/ diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -1437,23 +1437,33 @@ export default class StrivenEditor { // Process files const processFiles = function (files) { return new Promise((resolve) => { - const processPromises = []; + // Create a promise chain to insert each image in the files array only after the previous one is completed. + // p = Promise.resolve() is used to start the chain with a resolved promise. + let p = Promise.resolve(); + for (let i = 0; i < files.length; i++) { + p = p.then(() => se.insertImage(files[i])); + } - for (const file of files) { - // Check if the file is an image - if (!file.type.includes('image')) { - continue; - } + // Add the resolve to the end of the promise chain + p.then(() => resolve()); - const processPromise = se.insertImage(file); + //const processPromises = []; - processPromises.push(processPromise); - } + // for (const file of files) { + // // Check if the file is an image + // if (!file.type.includes('image')) { + // continue; + // } - // Resolve the promise when all the images have been processed - Promise.all(processPromises).then(() => { - resolve(); - }); + // const processPromise = se.insertImage(file); + + // processPromises.push(processPromise); + // } + + // // Resolve the promise when all the images have been processed + // Promise.all(processPromises).then(() => { + // resolve(); + // }); }); }; @@ -1489,9 +1499,6 @@ export default class StrivenEditor { // Create an event handler for when the images are selected imageMenuUploadInput.onchange = (e) => { - se.body.focus(); - se.setRange(); - // Get the files that were selected const files = e.target.files; @@ -2140,7 +2147,7 @@ export default class StrivenEditor { [...body.querySelectorAll('.se-image-wrapper')].forEach((iwrap) => { //find image inside the wrapper and replace the wrapper with image const img = iwrap.querySelector('img'); - iwrap.parentNode.replaceChild(img,iwrap); + iwrap.parentNode.replaceChild(img, iwrap); }); if (body.textContent || se.body.getElementsByTagName('img').length || se.body.getElementsByTagName('iframe').length) { @@ -3430,26 +3437,34 @@ export default class StrivenEditor { */ insertImage(file) { const se = this; - - return new Promise((resolveImageInsert) => { + se.body.focus(); + se.setRange(); + return new Promise((resolveImageInsert, reject) => { const bodyWidth = se.body.offsetWidth; const showUI = !(se.browser.isFirefox() || se.browser.isEdge()); se._isImageUploading = true; // Generate a random string to use as the image name - const randomString = 'image-' + Math.random().toString(36); - + //const randomString = 'image-' + Math.random().toString(36); + let currentRange = se.getRange(); // Insert image with a temporary src - document.execCommand( - 'insertHTML', - showUI, - `<div id="${randomString}" class="se-image-uploading" ></div>` - ); - - const tempImg = document.getElementById(randomString); + //document.execCommand('insertHTML', showUI, `<div id="${randomString}" class="se-image-uploading" ></div>`); + //const tempImg = document.getElementById(randomString); + const tempImg = document.createElement('div'); + //tempImg.id = randomString; + tempImg.classList.add('se-image-uploading'); + // Ensre that any selected contents is deleted before inserting + currentRange.deleteContents(); + currentRange.insertNode(tempImg); + currentRange.setStartAfter(tempImg); + this.range.collapse(true); + + se.setRange(currentRange); if (!tempImg) { - console.error('Failed to insert image into the DOM. Make sure the editor body is focused and try again.'); + const err = 'Failed to insert image into the DOM. Make sure the editor body is focused and try again.'; + + reject(err); return; } @@ -3472,7 +3487,7 @@ export default class StrivenEditor { se.overflow(); }, 100); }) - .catch((err) => { + .catch(() => { // Create the image tag to insert const imgTag = `<img src="${imgDataUrl}" width="${newWidth}px" alt="" data-data-url="true"/>`; // Replace the temporary image with the uploaded image @@ -3648,7 +3663,7 @@ export default class StrivenEditor { } startImageResize(e, resizeMarker, handlePosition, img) { - const se=this; + const se = this; e.preventDefault && e.preventDefault(); let startX = e.clientX;