added resetting image dimensions to initial size when pressing ESC

This commit is contained in:
bdbch 2025-05-01 18:55:07 +02:00
parent 4083d5194b
commit 94cba69c40

View File

@ -23,6 +23,11 @@ export function setupResizeHandlers(
let isResizing = false let isResizing = false
let currentPosition: { x: number; y: number } | null = null let currentPosition: { x: number; y: number } | null = null
let initialDimensions = {
width: dom.clientWidth,
height: dom.clientHeight,
}
// Determine which dimensions to update based on direction // Determine which dimensions to update based on direction
const updateWidth = const updateWidth =
direction === 'left' || direction === 'left' ||
@ -44,6 +49,23 @@ export function setupResizeHandlers(
const affectsLeft = direction === 'left' || direction === 'top-left' || direction === 'bottom-left' const affectsLeft = direction === 'left' || direction === 'top-left' || direction === 'bottom-left'
const affectsTop = direction === 'top' || direction === 'top-left' || direction === 'top-right' const affectsTop = direction === 'top' || direction === 'top-left' || direction === 'top-right'
function updateNodeSize() {
const pos = getPos()
if (pos === undefined) {
return
}
editor.commands.command(({ tr }) => {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
width: updateWidth ? dom.clientWidth : node.attrs.width,
height: updateHeight ? dom.clientHeight : node.attrs.height,
})
return true
})
}
const handleMove = (e: MouseEvent | TouchEvent): void => { const handleMove = (e: MouseEvent | TouchEvent): void => {
if (!isResizing || !currentPosition) { if (!isResizing || !currentPosition) {
return return
@ -98,26 +120,48 @@ export function setupResizeHandlers(
document.removeEventListener('mouseup', handleEnd) document.removeEventListener('mouseup', handleEnd)
document.removeEventListener('touchend', handleEnd) document.removeEventListener('touchend', handleEnd)
const pos = getPos() updateNodeSize()
if (pos === undefined) {
return
} }
editor.commands.command(({ tr }) => { const handleKeydown = (e: KeyboardEvent): void => {
tr.setNodeMarkup(pos, undefined, { // if we press escape, we stop resizing and stop all event handlers
...node.attrs, // and also reset the initial dimensions
width: updateWidth ? dom.clientWidth : node.attrs.width, if (e.key === 'Escape') {
height: updateHeight ? dom.clientHeight : node.attrs.height, e.preventDefault()
}) e.stopPropagation()
return true isResizing = false
}) currentPosition = null
document.removeEventListener('mousemove', handleMove)
document.removeEventListener('touchmove', handleMove)
document.removeEventListener('mouseup', handleEnd)
document.removeEventListener('touchend', handleEnd)
document.removeEventListener('keydown', handleKeydown)
dom.style.width = `${initialDimensions.width}px`
dom.style.height = `${initialDimensions.height}px`
updateNodeSize()
initialDimensions = {
width: dom.clientWidth,
height: dom.clientHeight,
}
document.removeEventListener('keydown', handleKeydown)
}
} }
const handleStart = (e: MouseEvent | TouchEvent): void => { const handleStart = (e: MouseEvent | TouchEvent): void => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
initialDimensions = {
width: dom.clientWidth,
height: dom.clientHeight,
}
isResizing = true isResizing = true
currentPosition = { currentPosition = {
x: e instanceof MouseEvent ? e.clientX : e.touches[0].clientX, x: e instanceof MouseEvent ? e.clientX : e.touches[0].clientX,
@ -128,6 +172,7 @@ export function setupResizeHandlers(
document.addEventListener('touchmove', handleMove) document.addEventListener('touchmove', handleMove)
document.addEventListener('mouseup', handleEnd) document.addEventListener('mouseup', handleEnd)
document.addEventListener('touchend', handleEnd) document.addEventListener('touchend', handleEnd)
document.addEventListener('keydown', handleKeydown)
} }
element.addEventListener('mousedown', handleStart) element.addEventListener('mousedown', handleStart)