refactor: Extract zoom drag handlers to functions in zoom._hs
- Move inline drag handling (~35 lines) to external functions (~4 lines) - Add isZoomDragTarget(el), startZoomDrag(), moveZoomDrag(), endZoomDrag() - Note: 'target' is reserved in hyperscript, use 'el' for parameters - Drag state stored on element (_isDragging, _initialX, _initialY) Zoom control HTML now has clean, minimal hyperscript handlers.
This commit is contained in:
@@ -139,3 +139,64 @@ def toggleZoomControl()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ==============================================================================
|
||||
-- ZOOM CONTROL DRAG HANDLERS
|
||||
-- ==============================================================================
|
||||
-- Check if drag target is valid (not a button or input)
|
||||
def isZoomDragTarget(el)
|
||||
if el.tagName is 'INPUT'
|
||||
return false
|
||||
end
|
||||
if el.tagName is 'BUTTON'
|
||||
return false
|
||||
end
|
||||
if el.matches('.zoom-slider, .zoom-close-btn, .zoom-reset-btn, .zoom-value')
|
||||
return false
|
||||
end
|
||||
if el.closest('.zoom-close-btn')
|
||||
return false
|
||||
end
|
||||
if el.closest('.zoom-reset-btn')
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Start dragging zoom control
|
||||
def startZoomDrag(control, clientX, clientY)
|
||||
set control._isDragging to true
|
||||
set control's *transition to 'none'
|
||||
set rect to control.getBoundingClientRect()
|
||||
set control._initialX to clientX - rect.left
|
||||
set control._initialY to clientY - rect.top
|
||||
end
|
||||
|
||||
-- Handle drag move
|
||||
def moveZoomDrag(control, clientX, clientY)
|
||||
if control._isDragging is not true
|
||||
return false
|
||||
end
|
||||
set currentX to clientX - control._initialX
|
||||
set currentY to clientY - control._initialY
|
||||
set maxX to window.innerWidth - control.offsetWidth
|
||||
set maxY to window.innerHeight - control.offsetHeight
|
||||
set currentX to Math.max(0, Math.min(currentX, maxX))
|
||||
set currentY to Math.max(0, Math.min(currentY, maxY))
|
||||
set control's *left to `${currentX}px`
|
||||
set control's *bottom to `${window.innerHeight - currentY - control.offsetHeight}px`
|
||||
set control's *transform to 'none'
|
||||
return true
|
||||
end
|
||||
|
||||
-- End drag and save position
|
||||
def endZoomDrag(control)
|
||||
if control._isDragging is not true
|
||||
return false
|
||||
end
|
||||
set control._isDragging to false
|
||||
set control's *transition to 'all 0.3s ease'
|
||||
set position to { bottom: control.style.bottom, left: control.style.left }
|
||||
set localStorage['cv-zoom-position'] to JSON.stringify(position)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -2,39 +2,9 @@
|
||||
<!-- Zoom Control (Fixed Bottom Center, Draggable) - Hyperscript Enhanced -->
|
||||
<div id="zoom-control" class="zoom-control no-print zoom-hidden" role="group" aria-label="{{if eq .Lang "es"}}Control de zoom{{else}}Zoom control{{end}}"
|
||||
_="on load call initZoomControl(me)
|
||||
|
||||
on mousedown(clientX, clientY)
|
||||
set target to event.target
|
||||
set tag to target.tagName
|
||||
if tag is 'INPUT' or tag is 'BUTTON' then exit end
|
||||
if target.classList.contains('zoom-slider') or target.classList.contains('zoom-close-btn') or target.classList.contains('zoom-reset-btn') or target.classList.contains('zoom-value') then exit end
|
||||
if target.closest('.zoom-close-btn') or target.closest('.zoom-reset-btn') then exit end
|
||||
set :isDragging to true
|
||||
set my *transition to 'none'
|
||||
set rect to my getBoundingClientRect()
|
||||
set :initialX to clientX - rect.left
|
||||
set :initialY to clientY - rect.top
|
||||
halt the event
|
||||
|
||||
on mousemove(clientX, clientY) from document
|
||||
if :isDragging is not true then exit end
|
||||
halt the event
|
||||
set currentX to clientX - :initialX
|
||||
set currentY to clientY - :initialY
|
||||
set maxX to window.innerWidth - my offsetWidth
|
||||
set maxY to window.innerHeight - my offsetHeight
|
||||
set currentX to Math.max(0, Math.min(currentX, maxX))
|
||||
set currentY to Math.max(0, Math.min(currentY, maxY))
|
||||
set my *left to `${currentX}px`
|
||||
set my *bottom to `${window.innerHeight - currentY - my offsetHeight}px`
|
||||
set my *transform to 'none'
|
||||
|
||||
on mouseup from document
|
||||
if :isDragging is not true then exit end
|
||||
set :isDragging to false
|
||||
set my *transition to 'all 0.3s ease'
|
||||
set position to { bottom: my *bottom, left: my *left }
|
||||
set localStorage['cv-zoom-position'] to JSON.stringify(position)">
|
||||
on mousedown(clientX, clientY) if isZoomDragTarget(event.target) call startZoomDrag(me, clientX, clientY) then halt the event end
|
||||
on mousemove(clientX, clientY) from document if moveZoomDrag(me, clientX, clientY) halt the event end
|
||||
on mouseup from document call endZoomDrag(me)">
|
||||
|
||||
<button
|
||||
id="zoom-close"
|
||||
|
||||
Reference in New Issue
Block a user