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 060723c2f265a3ca87403c7caf60f8e5076e71e7
parent c3328c9c10fda745ac0d9c2d3b2f8896cff30f65
Author: sanjogs <sanjogsharma@gmail.com>
Date:   Fri, 28 Feb 2025 04:27:42 -0500

Merge branch 'dev'

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

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -2156,7 +2156,7 @@ export default class StrivenEditor { if (selection) { return window.getSelection().getRangeAt(0); } - } catch (e) {} + } catch (e) { } } /** @@ -2653,7 +2653,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(se.range); } - } catch (e) {} + } catch (e) { } } /** @@ -3445,7 +3445,7 @@ export default class StrivenEditor { ); const tempImg = document.getElementById(randomString); - if (!tempImg){ + if (!tempImg) { console.error("Failed to insert image into the DOM. Make sure the editor body is focused and try again."); return; } @@ -3650,50 +3650,54 @@ export default class StrivenEditor { let startHeight = img.offsetHeight; let startLeft = img.offsetLeft; let startTop = img.offsetTop; + let aspectRatio = startWidth / startHeight; // Maintain original aspect ratio function mouseMoveHandler(e) { if (e.touches) e = e.touches[0]; let deltaX = e.clientX - startX; let deltaY = e.clientY - startY; + let newWidth, newHeight; //update position and dimension of resize marker to show the resize effect switch (handlePosition) { case 'top-left': - resizeMarker.style.width = startWidth - deltaX + 'px'; - resizeMarker.style.height = startHeight - deltaY + 'px'; - resizeMarker.style.left = startLeft + deltaX + 'px'; - resizeMarker.style.top = startTop + deltaY + 'px'; - break; case 'top-right': - resizeMarker.style.width = startWidth + deltaX + 'px'; - resizeMarker.style.height = startHeight - deltaY + 'px'; - resizeMarker.style.top = startTop + deltaY + 'px'; - break; case 'bottom-left': - resizeMarker.style.width = startWidth - deltaX + 'px'; - resizeMarker.style.height = startHeight + deltaY + 'px'; - resizeMarker.style.left = startLeft + deltaX + 'px'; - break; case 'bottom-right': - resizeMarker.style.width = startWidth + deltaX + 'px'; - resizeMarker.style.height = startHeight + deltaY + 'px'; + if (Math.abs(deltaX) > Math.abs(deltaY)) { + newWidth = startWidth + (handlePosition.includes('left') ? -deltaX : deltaX); + newHeight = newWidth / aspectRatio; + } else { + newHeight = startHeight + (handlePosition.includes('top') ? -deltaY : deltaY); + newWidth = newHeight * aspectRatio; + } break; case 'top-middle': - resizeMarker.style.height = startHeight - deltaY + 'px'; - resizeMarker.style.top = startTop + deltaY + 'px'; + newHeight = startHeight - deltaY; break; case 'bottom-middle': - resizeMarker.style.height = startHeight + deltaY + 'px'; + newHeight = startHeight + deltaY; break; case 'left-middle': - resizeMarker.style.width = startWidth - deltaX + 'px'; - resizeMarker.style.left = startLeft + deltaX + 'px'; + newWidth = startWidth - deltaX; break; case 'right-middle': - resizeMarker.style.width = startWidth + deltaX + 'px'; + newWidth = startWidth + deltaX; break; + default: + newWidth = startWidth; + newHeight = startHeight; + } + + if (handlePosition.includes('top')) { + resizeMarker.style.top = startTop + deltaY + 'px'; } + if (handlePosition.includes('left')) { + resizeMarker.style.left = startLeft + deltaX + 'px'; + } + resizeMarker.style.width = newWidth + 'px'; + resizeMarker.style.height = newHeight + 'px'; } function mouseUpHandler() { @@ -3702,14 +3706,13 @@ export default class StrivenEditor { 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;