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 0ddaf0e536ce047fc2ae7d67e8701170755f4b06
parent 247a77b57c2af95c261973f00446163d1c1ff593
Author: sanjogs <sanjogsharma@gmail.com>
Date:   Wed, 26 Feb 2025 07:11:23 -0500

added dimensions to image wrapper and added touch support for image resize

Diffstat:
Mpackage-lock.json | 4++--
Msrc/striveneditor.js | 62++++++++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 44 insertions(+), 22 deletions(-)

diff --git a/package-lock.json b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@striven-erp/striven-editor", - "version": "3.18.1", + "version": "3.19.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@striven-erp/striven-editor", - "version": "3.18.1", + "version": "3.19.2", "license": "MIT", "dependencies": { "@simonwep/pickr": "^1.9.1", diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -1430,7 +1430,7 @@ export default class StrivenEditor { const processFiles = function (files) { return new Promise((resolve) => { const processPromises = []; - + for (const file of files) { // Check if the file is an image if (!file.type.includes('image')) { @@ -1504,19 +1504,19 @@ export default class StrivenEditor { // Hook up dropzone events imageMenuUploadDropZone.ondragover = (e) => { e.preventDefault(); - + imageMenuUploadDropZone.classList.add('dropzone-hot'); }; imageMenuUploadDropZone.ondragleave = (e) => { e.preventDefault(); - + imageMenuUploadDropZone.classList.remove('dropzone-hot'); }; imageMenuUploadDropZone.ondrop = (e) => { e.preventDefault(); - + imageMenuUploadDropZone.classList.remove('dropzone-hot'); // For each file, get the data url and insert the image @@ -2141,7 +2141,7 @@ export default class StrivenEditor { if (selection) { return window.getSelection().getRangeAt(0); } - } catch (e) {} + } catch (e) { } } /** @@ -2628,7 +2628,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(se.range); } - } catch (e) {} + } catch (e) { } } /** @@ -3412,21 +3412,18 @@ export default class StrivenEditor { // Create the image tag to insert const imgTag = `<img src="${imgDataUrl}" width="${width}px" alt="" data-data-url="true"/>`; + const showUI = !(se.browser.isFirefox() || se.browser.isEdge()) if (se.options.imageUrl) { //upload image if imageUrl is provided se.getImage(imgDataUrl) .then((imageUrl) => { - document.execCommand('insertHTML', true, `<img src="${imageUrl}" width="${width}px" alt=""/>`); + document.execCommand('insertHTML', showUI, `<img src="${imageUrl}" width="${width}px" alt=""/>`); }) .catch((err) => { - document.execCommand('insertHTML', true, imgTag); + document.execCommand('insertHTML', showUI, imgTag); }); } else { - if (se.browser.isFirefox() || se.browser.isEdge()) { - document.execCommand('insertHTML', false, imgTag); - } else { - document.execCommand('insertHTML', true, imgTag); - } + document.execCommand('insertHTML', showUI, imgTag); } setTimeout(function () { se.overflow(); @@ -3456,19 +3453,23 @@ export default class StrivenEditor { if (!image.nextElementSibling || image.nextElementSibling.className !== 'se-image-options') { let resizeHandles = []; let imageWrapper = image.parentElement; + let imageRect = image.getBoundingClientRect(); + if (!imageWrapper.classList.contains('se-image-wrapper')) { //wrapper not present, create one imageWrapper = document.createElement('div'); imageWrapper.classList.add('se-image-wrapper'); imageWrapper.setAttribute('contenteditable', false); + imageWrapper.style.width = `${imageRect.width}px`; + imageWrapper.style.height = `${imageRect.height}px`; + + image.parentElement.insertBefore(imageWrapper, image); imageWrapper.appendChild(image); //Add a div that shows resize marker const resizeMarker = document.createElement('div'); resizeMarker.classList.add('resize-marker'); - resizeMarker.style.width = image.clientWidth + 'px'; - resizeMarker.style.height = image.clientHeight + 'px'; imageWrapper.appendChild(resizeMarker); // Add resize handles (8 in total) @@ -3492,6 +3493,11 @@ export default class StrivenEditor { e.preventDefault(); se.startImageResize(e, resizeMarker, pos, image); }); + + handle.addEventListener('touchstart', function (e) { + e.preventDefault(); + se.startImageResize(e.touches[0], resizeMarker, pos, image); + }); }); } @@ -3571,7 +3577,8 @@ export default class StrivenEditor { } startImageResize(e, resizeMarker, handlePosition, img) { - e.preventDefault(); + + e.preventDefault && e.preventDefault(); let startX = e.clientX; let startY = e.clientY; @@ -3581,6 +3588,8 @@ export default class StrivenEditor { let startTop = img.offsetTop; function mouseMoveHandler(e) { + if (e.touches) e = e.touches[0]; + let deltaX = e.clientX - startX; let deltaY = e.clientY - startY; @@ -3625,10 +3634,18 @@ export default class StrivenEditor { function mouseUpHandler() { //set properties of the image to the resized marker - img.width = parseFloat(resizeMarker.style.width); - img.height = parseFloat(resizeMarker.style.height); - img.style.left = resizeMarker.style.left; - img.style.top = resizeMarker.style.top; + let resizedRect = resizeMarker.getBoundingClientRect(); + + img.width = resizedRect.width; + img.height = resizedRect.height; + img.style.left = resizedRect.x; + img.style.top = resizedRect.y; + //reset the wrapper dimension if it's present + let imageWrapper = img.parentElement; + if (imageWrapper.classList.contains('se-image-wrapper')) { + imageWrapper.style.width = `${img.width}px`; + imageWrapper.style.height = `${img.height}px`; + } //restore the marker back to the original position resizeMarker.style.left = 0; resizeMarker.style.top = 0; @@ -3639,5 +3656,10 @@ export default class StrivenEditor { document.addEventListener('mousemove', mouseMoveHandler); document.addEventListener('mouseup', mouseUpHandler); + + document.addEventListener('touchmove', mouseMoveHandler); + document.addEventListener('mouseend', mouseUpHandler); + + } }