parent
1d70aef400
commit
2d8ff4baf7
@ -1,77 +1,77 @@ |
|||||||
import { useEffect, useState } from 'react' |
import { useEffect, useState } from 'react' |
||||||
|
|
||||||
export const useDragTerminal = (minHeight: number, defaultPosition: number) => { |
export const useDragTerminal = (minHeight: number, defaultPosition: number) => { |
||||||
const [isOpen, setIsOpen] = useState(defaultPosition > minHeight) |
const [isOpen, setIsOpen] = useState(defaultPosition > minHeight) |
||||||
const [lastYPosition, setLastYPosition] = useState(0) |
const [lastYPosition, setLastYPosition] = useState(0) |
||||||
const [terminalPosition, setTerminalPosition] = useState(defaultPosition) |
const [terminalPosition, setTerminalPosition] = useState(defaultPosition) |
||||||
// Used to save position of the terminal when it is closed
|
// Used to save position of the terminal when it is closed
|
||||||
const [lastTerminalPosition, setLastTerminalPosition] = useState(defaultPosition) |
const [lastTerminalPosition, setLastTerminalPosition] = useState(defaultPosition) |
||||||
const [isDragging, setIsDragging] = useState(false) |
const [isDragging, setIsDragging] = useState(false) |
||||||
|
|
||||||
const handleDraggingStart = (event: React.MouseEvent) => { |
const handleDraggingStart = (event: React.MouseEvent) => { |
||||||
setLastYPosition(event.clientY) |
setLastYPosition(event.clientY) |
||||||
setIsDragging(true) |
setIsDragging(true) |
||||||
} |
} |
||||||
|
|
||||||
const handleDragging = (event: MouseEvent) => { |
const handleDragging = (event: MouseEvent) => { |
||||||
event.preventDefault() |
event.preventDefault() |
||||||
|
|
||||||
if (isDragging) { |
if (isDragging) { |
||||||
const mouseYPosition = event.clientY |
const mouseYPosition = event.clientY |
||||||
const difference = lastYPosition - mouseYPosition |
const difference = lastYPosition - mouseYPosition |
||||||
const newTerminalPosition = terminalPosition + difference |
const newTerminalPosition = terminalPosition + difference |
||||||
setTerminalPosition(newTerminalPosition) |
setTerminalPosition(newTerminalPosition) |
||||||
setLastYPosition(mouseYPosition) |
setLastYPosition(mouseYPosition) |
||||||
} |
|
||||||
} |
} |
||||||
|
} |
||||||
|
|
||||||
const handleDraggingEnd = () => { |
const handleDraggingEnd = () => { |
||||||
if(!isDragging) return |
if (!isDragging) return |
||||||
|
|
||||||
setIsDragging(false) |
|
||||||
|
|
||||||
// Check terminal position to determine if it should be open or closed
|
setIsDragging(false) |
||||||
setIsOpen(terminalPosition > minHeight) |
|
||||||
} |
|
||||||
|
|
||||||
const handleToggleTerminal = (event: React.MouseEvent<HTMLElement>) => { |
// Check terminal position to determine if it should be open or closed
|
||||||
event.preventDefault() |
setIsOpen(terminalPosition > minHeight) |
||||||
event.stopPropagation() |
} |
||||||
|
|
||||||
if(isOpen) { |
const handleToggleTerminal = (event: React.MouseEvent<HTMLElement>) => { |
||||||
setLastTerminalPosition(terminalPosition) |
event.preventDefault() |
||||||
setLastYPosition(0) |
event.stopPropagation() |
||||||
setTerminalPosition(minHeight) |
|
||||||
} else { |
|
||||||
setTerminalPosition(lastTerminalPosition <= minHeight ? 323 : lastTerminalPosition) |
|
||||||
} |
|
||||||
|
|
||||||
setIsOpen(!isOpen) |
if (isOpen) { |
||||||
|
setLastTerminalPosition(terminalPosition) |
||||||
|
setLastYPosition(0) |
||||||
|
setTerminalPosition(minHeight) |
||||||
|
} else { |
||||||
|
setTerminalPosition(lastTerminalPosition <= minHeight ? 323 : lastTerminalPosition) |
||||||
} |
} |
||||||
|
|
||||||
// Add event listeners for dragging
|
setIsOpen(!isOpen) |
||||||
useEffect(() => { |
} |
||||||
document.addEventListener('mousemove', handleDragging) |
|
||||||
document.addEventListener('mouseup', handleDraggingEnd) |
|
||||||
|
|
||||||
return () => { |
// Add event listeners for dragging
|
||||||
document.removeEventListener('mousemove', handleDragging) |
useEffect(() => { |
||||||
document.removeEventListener('mouseup', handleDraggingEnd) |
document.addEventListener('mousemove', handleDragging) |
||||||
} |
document.addEventListener('mouseup', handleDraggingEnd) |
||||||
}, [handleDragging, handleDraggingEnd]) |
|
||||||
|
|
||||||
// Reset terminal position
|
return () => { |
||||||
useEffect(() => { |
document.removeEventListener('mousemove', handleDragging) |
||||||
if(!terminalPosition){ |
document.removeEventListener('mouseup', handleDraggingEnd) |
||||||
setTerminalPosition(defaultPosition) |
} |
||||||
} |
}, [handleDragging, handleDraggingEnd]) |
||||||
}, [terminalPosition, setTerminalPosition]) |
|
||||||
|
|
||||||
return { |
// Reset terminal position
|
||||||
isOpen, |
useEffect(() => { |
||||||
terminalPosition, |
if (!terminalPosition) { |
||||||
isDragging, |
setTerminalPosition(defaultPosition) |
||||||
handleDraggingStart, |
|
||||||
handleToggleTerminal, |
|
||||||
} |
} |
||||||
|
}, [terminalPosition, setTerminalPosition]) |
||||||
|
|
||||||
|
return { |
||||||
|
isOpen, |
||||||
|
terminalPosition, |
||||||
|
isDragging, |
||||||
|
handleDraggingStart, |
||||||
|
handleToggleTerminal, |
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue