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 a9793d0e0dfdd433bf6dac4f41e94be5d43910ac
parent 11e47f0f71a2d8b1b9a2db07bdf182587a3ee7a2
Author: Eric Butler <ebutler@striven.com>
Date:   Fri, 28 Feb 2025 17:35:50 -0500

Merge branch 'dev'

Diffstat:
Mpackage.json | 2+-
Msrc/striveneditor.css | 10++++++++++
Msrc/striveneditor.js | 28+++++++++++++++-------------
3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/package.json b/package.json @@ -1,7 +1,7 @@ { "name": "@striven-erp/striven-editor", "private": false, - "version": "3.19.7", + "version": "3.19.8", "description": "The editor used for the Striven ERP system.", "main": "./dist", "scripts": { diff --git a/src/striveneditor.css b/src/striveneditor.css @@ -197,6 +197,16 @@ fill: #777 !important; } +.striven-editor .se-image-uploading { + width: 100%; height: 100%; background-color: #eee;display: flex; align-items: center; justify-content: center; +} + +.striven-editor .se-image-uploading::after { + content: 'Uploading...'; + font-size: 16px; + color: #777; +} + /** Toolbar **/ .striven-editor .se-toolbar { diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -2155,7 +2155,7 @@ export default class StrivenEditor { if (selection) { return window.getSelection().getRangeAt(0); } - } catch (e) { } + } catch (e) {} } /** @@ -2652,7 +2652,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(se.range); } - } catch (e) { } + } catch (e) {} } /** @@ -3425,8 +3425,7 @@ export default class StrivenEditor { */ insertImage(file) { const se = this; - se.body.focus(); - se.setRange(); + return new Promise((resolveImageInsert) => { const bodyWidth = se.body.offsetWidth; @@ -3434,18 +3433,18 @@ export default class StrivenEditor { se._isImageUploading = true; // Generate a random string to use as the image name const randomString = 'image-' + Math.random().toString(36); + // Insert image with a temporary src document.execCommand( 'insertHTML', showUI, - `<div id="${randomString}" style="width:${se.body.width}px; height: ${se.body.height}px; background-color: #eee;display: flex; align-items: center; justify-content: center;" > - <svg width="25%" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>cloud-upload-outline</title><path d="M6.5 20Q4.22 20 2.61 18.43 1 16.85 1 14.58 1 12.63 2.17 11.1 3.35 9.57 5.25 9.15 5.88 6.85 7.75 5.43 9.63 4 12 4 14.93 4 16.96 6.04 19 8.07 19 11 20.73 11.2 21.86 12.5 23 13.78 23 15.5 23 17.38 21.69 18.69 20.38 20 18.5 20H13Q12.18 20 11.59 19.41 11 18.83 11 18V12.85L9.4 14.4L8 13L12 9L16 13L14.6 14.4L13 12.85V18H18.5Q19.55 18 20.27 17.27 21 16.55 21 15.5 21 14.45 20.27 13.73 19.55 13 18.5 13H17V11Q17 8.93 15.54 7.46 14.08 6 12 6 9.93 6 8.46 7.46 7 8.93 7 11H6.5Q5.05 11 4.03 12.03 3 13.05 3 14.5 3 15.95 4.03 17 5.05 18 6.5 18H9V20M12 13Z" /></svg> - </div>` + `<div id="${randomString}" class="se-image-uploading" ></div>` ); const tempImg = document.getElementById(randomString); + if (!tempImg) { - console.error("Failed to insert image into the DOM. Make sure the editor body is focused and try again."); + console.error('Failed to insert image into the DOM. Make sure the editor body is focused and try again.'); return; } @@ -3453,28 +3452,31 @@ export default class StrivenEditor { .then((imgDataUrl) => { getImageDimensions(imgDataUrl) .then(({ width, height }) => { - const { width: newWidth, height: newHeight } = computeImageDimensions(width, height, bodyWidth); - - // Create the image tag to insert - const imgTag = `<img src="${imgDataUrl}" width="${newWidth}px" alt="" data-data-url="true"/>`; + const { width: newWidth } = computeImageDimensions(width, height, bodyWidth); if (se.options.imageUrl) { //upload image if imageUrl is provided se.getImage(imgDataUrl) .then((imageUrl) => { // Replace the temporary image with the uploaded image - tempImg.outerHTML = `<img id="${randomString}" src="${imageUrl}" width="${newWidth}px" alt=""/>`; + tempImg.outerHTML = `<img src="${imageUrl}" width="${newWidth}px" alt=""/>`; + se._isImageUploading = false; + setTimeout(function () { se.overflow(); }, 1000); }) .catch((err) => { + // 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 tempImg.outerHTML = imgTag; se._isImageUploading = false; }); } else { + // 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 tempImg.outerHTML = imgTag; se._isImageUploading = false;