-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from rocicorp/resize2
Add resize.
- Loading branch information
Showing
8 changed files
with
232 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useLayoutEffect, useState } from "react"; | ||
import { touchToMouse } from "./events"; | ||
|
||
export type PageCoords = { | ||
pageX: number; | ||
pageY: number; | ||
}; | ||
|
||
export type DragEvent = PageCoords & { | ||
movementX: number; | ||
movementY: number; | ||
}; | ||
|
||
export function useDrag({ | ||
onDragStart, | ||
onDrag, | ||
onDragEnd, | ||
}: { | ||
onDragStart?: () => void; | ||
onDrag?: (e: DragEvent) => void; | ||
onDragEnd?: () => void; | ||
}) { | ||
const [lastDrag, setLastDrag] = useState<{ | ||
pageX: number; | ||
pageY: number; | ||
} | null>(null); | ||
|
||
const onTouchStart = (e: any) => touchToMouse(e, onMouseDown); | ||
const onMouseDown = (e: PageCoords) => { | ||
setLastDrag(e); | ||
onDragStart && onDragStart(); | ||
}; | ||
|
||
const onTouchMove = (e: any) => touchToMouse(e, onMouseMove); | ||
const onMouseMove = (e: PageCoords) => { | ||
if (lastDrag) { | ||
const { pageX, pageY } = e; | ||
onDrag && | ||
onDrag({ | ||
pageX, | ||
pageY, | ||
movementX: pageX - lastDrag.pageX, | ||
movementY: pageY - lastDrag.pageY, | ||
}); | ||
setLastDrag(e); | ||
} | ||
}; | ||
|
||
const onMouseUp = () => { | ||
setLastDrag(null); | ||
onDragEnd && onDragEnd(); | ||
}; | ||
|
||
const dragListeners = { | ||
mousemove: (e: any) => onMouseMove(e), | ||
touchmove: (e: any) => onTouchMove(e), | ||
mouseup: onMouseUp, | ||
touchend: onMouseUp, | ||
}; | ||
|
||
useLayoutEffect(() => { | ||
if (!lastDrag) { | ||
return; | ||
} | ||
Object.entries(dragListeners).forEach(([key, val]) => | ||
window.addEventListener(key, val) | ||
); | ||
return () => { | ||
Object.entries(dragListeners).forEach(([key, val]) => | ||
window.removeEventListener(key, val) | ||
); | ||
}; | ||
}); | ||
|
||
return { | ||
onMouseDown, | ||
onTouchStart, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Data } from "./data"; | ||
import { DragEvent, useDrag } from "./drag"; | ||
import { Rect } from "./rect"; | ||
|
||
export function Selection({ data, id }: { data: Data; id: string }) { | ||
const shape = data.useShapeByID(id); | ||
const gripSize = 19; | ||
|
||
const onDrag = (e: DragEvent) => { | ||
if (!shape) { | ||
return; | ||
} | ||
|
||
const shapeCenter = { | ||
x: shape.x + shape.width / 2, | ||
y: shape.y + shape.height / 2, | ||
}; | ||
|
||
const size = (x1: number, x2: number, y1: number, y2: number) => { | ||
const distanceSqFromCenterToCursor = | ||
Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2); | ||
return Math.sqrt(distanceSqFromCenterToCursor / 2) * 2; | ||
}; | ||
|
||
const s0 = size( | ||
shapeCenter.x, | ||
e.pageX - e.movementX, | ||
shapeCenter.y, | ||
e.pageY - e.movementY | ||
); | ||
const s1 = size(shapeCenter.x, e.pageX, shapeCenter.y, e.pageY); | ||
|
||
data.resizeShape({ id, ds: s1 - s0 }); | ||
}; | ||
|
||
const drag = useDrag({ onDrag }); | ||
|
||
if (!shape) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<Rect | ||
{...{ | ||
data, | ||
id, | ||
highlight: true, | ||
}} | ||
/> | ||
<div | ||
style={{ | ||
position: "absolute", | ||
transform: `translate3d(${shape.x}px, ${shape.y}px, 0) rotate(${shape.rotate}deg)`, | ||
width: shape.width, | ||
height: shape.height, | ||
pointerEvents: "none", | ||
}} | ||
{...drag} | ||
> | ||
<svg | ||
width={gripSize} | ||
height={gripSize} | ||
style={{ | ||
position: "absolute", | ||
transform: `translate3d(${shape.width - gripSize / 2 - 2}px, ${ | ||
shape.height - gripSize / 2 - 2 | ||
}px, 0)`, | ||
cursor: "grab", | ||
pointerEvents: "all", | ||
...drag, | ||
}} | ||
> | ||
<rect | ||
strokeWidth={2} | ||
stroke="rgb(74,158,255)" | ||
width={gripSize} | ||
height={gripSize} | ||
fill="white" | ||
/> | ||
</svg> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.