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 e28f13f94a9fea5c1e8aaa928f25b8d84f236499
parent 19fa47167484fba41a37b86668e58d9040ea349a
Author: Eric Butler <ebutler@striven.com>
Date:   Thu, 27 Feb 2025 09:13:19 -0500

update to image placeholder logic

Diffstat:
Msrc/striveneditor.js | 73++++++++++++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 44 insertions(+), 29 deletions(-)

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -92,6 +92,9 @@ export default class StrivenEditor { // Default Option Groups with SVG Data se.optionGroups = OPTIONGROUPS; + // Gets whether an image is being uploaded when pasting or inserting an image + se._isImageUploading = false; + // Initialize Options if (options) { // Set options property @@ -144,6 +147,7 @@ export default class StrivenEditor { se.initResponsive(); // Editor Reponsive Logic se.initOverflow(); // Overflow Content Logic se.overflow(); // Trigger overflow login on init + se.getIsImageUploading = () => se._isImageUploading; // DOM Access to the Editor Instance el.StrivenEditor = () => se; @@ -1445,7 +1449,6 @@ export default class StrivenEditor { // Resolve the promise when all the images have been processed Promise.all(processPromises).then(() => { resolve(); - }); }); }; @@ -1478,7 +1481,6 @@ export default class StrivenEditor { // Create an event handler for when the images are selected imageMenuUploadInput.onchange = (e) => { - se.body.focus(); se.setRange(); @@ -2141,7 +2143,7 @@ export default class StrivenEditor { if (selection) { return window.getSelection().getRangeAt(0); } - } catch (e) { } + } catch (e) {} } /** @@ -2628,7 +2630,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(se.range); } - } catch (e) { } + } catch (e) {} } /** @@ -3089,7 +3091,7 @@ export default class StrivenEditor { }; let formatedHtml = js_beautify.html(se.body.innerHTML, options); - + // Set the content of the editor to the formatted html se.body.textContent = formatedHtml; se.overflow(); @@ -3403,53 +3405,70 @@ export default class StrivenEditor { const se = this; return new Promise((resolveImageInsert) => { 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); + // 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>` + ); + // Replace the temporary image with the uploaded image + const tempImg = document.getElementById(randomString); + getImageDataURL(file) .then((imgDataUrl) => { getImageDimensions(imgDataUrl) - .then(({width, height}) => { - - const {width: newWidth, height: newHeight} = computeImageDimensions(width, height, bodyWidth); + .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 showUI = !(se.browser.isFirefox() || se.browser.isEdge()) if (se.options.imageUrl) { - // 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:${newWidth}px; height: ${newHeight}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>`); - //upload image if imageUrl is provided se.getImage(imgDataUrl) .then((imageUrl) => { - // Replace the temporary image with the uploaded image - const tempImg = document.getElementById(randomString); tempImg.outerHTML = `<img id="${randomString}" src="${imageUrl}" width="${newWidth}px" alt=""/>`; - + se._isImageUploading = false; + setTimeout(function () { + se.overflow(); + }, 100); }) .catch((err) => { - document.execCommand('insertHTML', showUI, imgTag); + tempImg.outerHTML = imgTag; + se._isImageUploading = false; }); } else { - document.execCommand('insertHTML', showUI, imgTag); + tempImg.outerHTML = imgTag; + se._isImageUploading = false; + setTimeout(function () { + se.overflow(); + }, 0); } - setTimeout(function () { - se.overflow(); - }, 0); resolveImageInsert(); }) .catch((err) => { console.error(err); + tempImg.remove(); + se._isImageUploading = false; }); }) .catch((err) => { console.error(err); + tempImg.remove(); + se._isImageUploading = false; }); + }).catch((err) => { + console.error(err); + tempImg.remove(); + se._isImageUploading = false; }); } @@ -3475,7 +3494,6 @@ export default class StrivenEditor { imageWrapper.style.width = `${imageRect.width}px`; imageWrapper.style.height = `${imageRect.height}px`; - image.parentElement.insertBefore(imageWrapper, image); imageWrapper.appendChild(image); @@ -3589,7 +3607,6 @@ export default class StrivenEditor { } startImageResize(e, resizeMarker, handlePosition, img) { - e.preventDefault && e.preventDefault(); let startX = e.clientX; @@ -3671,7 +3688,5 @@ export default class StrivenEditor { document.addEventListener('touchmove', mouseMoveHandler); document.addEventListener('mouseend', mouseUpHandler); - - } }