use space to toggle pan mode

This commit is contained in:
Sanster 2022-02-06 19:52:45 +08:00
parent e68f409003
commit 028a63ea34

View File

@ -71,6 +71,7 @@ export default function Editor(props: EditorProps) {
const [historyLineCount, setHistoryLineCount] = useState<number[]>([]) const [historyLineCount, setHistoryLineCount] = useState<number[]>([])
const [{ x, y }, setCoords] = useState({ x: -1, y: -1 }) const [{ x, y }, setCoords] = useState({ x: -1, y: -1 })
const [showBrush, setShowBrush] = useState(false) const [showBrush, setShowBrush] = useState(false)
const [isPanning, setIsPanning] = useState<boolean>(false)
const [showOriginal, setShowOriginal] = useState(false) const [showOriginal, setShowOriginal] = useState(false)
const [isInpaintingLoading, setIsInpaintingLoading] = useState(false) const [isInpaintingLoading, setIsInpaintingLoading] = useState(false)
const [showSeparator, setShowSeparator] = useState(false) const [showSeparator, setShowSeparator] = useState(false)
@ -280,6 +281,9 @@ export default function Editor(props: EditorProps) {
} }
const onMouseDrag = (ev: SyntheticEvent) => { const onMouseDrag = (ev: SyntheticEvent) => {
if (isPanning) {
return
}
if (!isDraging) { if (!isDraging) {
return return
} }
@ -290,6 +294,9 @@ export default function Editor(props: EditorProps) {
} }
const onPointerUp = () => { const onPointerUp = () => {
if (isPanning) {
return
}
if (!original.src) { if (!original.src) {
return return
} }
@ -319,6 +326,9 @@ export default function Editor(props: EditorProps) {
} }
const onMouseDown = (ev: SyntheticEvent) => { const onMouseDown = (ev: SyntheticEvent) => {
if (isPanning) {
return
}
if (!original.src) { if (!original.src) {
return return
} }
@ -409,11 +419,35 @@ export default function Editor(props: EditorProps) {
} }
const toggleShowBrush = (newState: boolean) => { const toggleShowBrush = (newState: boolean) => {
if (newState !== showBrush) { if (newState !== showBrush && !isPanning) {
setShowBrush(newState) setShowBrush(newState)
} }
} }
const getCursor = useCallback(() => {
if (isPanning) {
return 'grab'
}
if (showBrush) {
return 'none'
}
return undefined
}, [showBrush, isPanning])
// Toggle clean/zoom tool on spacebar.
useKey(
' ',
ev => {
ev?.preventDefault()
setShowBrush(!showBrush)
setIsPanning(!isPanning)
},
{
event: 'keydown',
},
[isPanning, showBrush]
)
if (!original || !scale || !minScale) { if (!original || !scale || !minScale) {
return <></> return <></>
} }
@ -435,7 +469,7 @@ export default function Editor(props: EditorProps) {
viewportRef.current = r viewportRef.current = r
} }
}} }}
panning={{ disabled: true, velocityDisabled: true }} panning={{ disabled: !isPanning, velocityDisabled: true }}
wheel={{ step: 0.05 }} wheel={{ step: 0.05 }}
centerZoomedOut centerZoomedOut
alignmentAnimation={{ disabled: true }} alignmentAnimation={{ disabled: true }}
@ -462,7 +496,7 @@ export default function Editor(props: EditorProps) {
<> <>
<canvas <canvas
className="rounded-sm" className="rounded-sm"
style={showBrush ? { cursor: 'none' } : {}} style={{ cursor: getCursor() }}
onContextMenu={e => { onContextMenu={e => {
e.preventDefault() e.preventDefault()
}} }}
@ -514,7 +548,7 @@ export default function Editor(props: EditorProps) {
</TransformComponent> </TransformComponent>
</TransformWrapper> </TransformWrapper>
{showBrush && !isInpaintingLoading && ( {showBrush && !isInpaintingLoading && !isPanning && (
<div <div
className="hidden sm:block absolute rounded-full border border-primary bg-primary bg-opacity-80 pointer-events-none" className="hidden sm:block absolute rounded-full border border-primary bg-primary bg-opacity-80 pointer-events-none"
style={{ style={{