Remove need to pass node into drag overlay

This commit is contained in:
Mitchell McCaffrey 2021-07-22 16:40:33 +10:00
parent 93ce4bab0e
commit 19139dec82
6 changed files with 14 additions and 34 deletions

View File

@ -1,23 +1,25 @@
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { Box, IconButton } from "theme-ui"; import { Box, IconButton } from "theme-ui";
import Konva from "konva";
import RemoveTokenIcon from "../../icons/RemoveTokenIcon"; import RemoveTokenIcon from "../../icons/RemoveTokenIcon";
import { useMapStage } from "../../contexts/MapStageContext";
type DragOverlayProps = { type DragOverlayProps = {
dragging: boolean; dragging: boolean;
node: Konva.Node;
onRemove: () => void; onRemove: () => void;
}; };
function DragOverlay({ dragging, node, onRemove }: DragOverlayProps) { function DragOverlay({ dragging, onRemove }: DragOverlayProps) {
const [isRemoveHovered, setIsRemoveHovered] = useState(false); const [isRemoveHovered, setIsRemoveHovered] = useState(false);
const removeTokenRef = useRef<HTMLDivElement>(null); const removeTokenRef = useRef<HTMLDivElement>(null);
const mapStageRef = useMapStage();
// Detect token hover on remove icon manually to support touch devices // Detect token hover on remove icon manually to support touch devices
useEffect(() => { useEffect(() => {
function detectRemoveHover() { function detectRemoveHover() {
if (!node || !dragging || !removeTokenRef.current) { const mapStage = mapStageRef.current;
if (!mapStage || !dragging || !removeTokenRef.current) {
return; return;
} }
const map = document.querySelector(".map"); const map = document.querySelector(".map");
@ -25,11 +27,7 @@ function DragOverlay({ dragging, node, onRemove }: DragOverlayProps) {
return; return;
} }
const mapRect = map.getBoundingClientRect(); const mapRect = map.getBoundingClientRect();
const stage = node.getStage(); const pointerPosition = mapStage.getPointerPosition();
if (!stage) {
return;
}
const pointerPosition = stage.getPointerPosition();
if (!pointerPosition) { if (!pointerPosition) {
return; return;
} }
@ -54,7 +52,7 @@ function DragOverlay({ dragging, node, onRemove }: DragOverlayProps) {
} }
let handler: NodeJS.Timeout; let handler: NodeJS.Timeout;
if (node && dragging) { if (dragging) {
handler = setInterval(detectRemoveHover, 100); handler = setInterval(detectRemoveHover, 100);
} }
@ -63,11 +61,11 @@ function DragOverlay({ dragging, node, onRemove }: DragOverlayProps) {
clearInterval(handler); clearInterval(handler);
} }
}; };
}, [isRemoveHovered, dragging, node]); }, [isRemoveHovered, dragging, mapStageRef]);
// Detect drag end of token image and remove it if it is over the remove icon // Detect drag end of token image and remove it if it is over the remove icon
useEffect(() => { useEffect(() => {
if (!dragging && node && isRemoveHovered) { if (!dragging && isRemoveHovered) {
onRemove(); onRemove();
} }
}); });

View File

@ -1,5 +1,3 @@
import Konva from "konva";
import DragOverlay from "../map/DragOverlay"; import DragOverlay from "../map/DragOverlay";
import { TokenStateRemoveHandler } from "../../types/Events"; import { TokenStateRemoveHandler } from "../../types/Events";
import { TokenState } from "../../types/TokenState"; import { TokenState } from "../../types/TokenState";
@ -7,27 +5,19 @@ import { TokenState } from "../../types/TokenState";
type TokenDragOverlayProps = { type TokenDragOverlayProps = {
onTokenStateRemove: TokenStateRemoveHandler; onTokenStateRemove: TokenStateRemoveHandler;
tokenState: TokenState; tokenState: TokenState;
tokenNode: Konva.Node;
dragging: boolean; dragging: boolean;
}; };
function TokenDragOverlay({ function TokenDragOverlay({
onTokenStateRemove, onTokenStateRemove,
tokenState, tokenState,
tokenNode,
dragging, dragging,
}: TokenDragOverlayProps) { }: TokenDragOverlayProps) {
function handleTokenRemove() { function handleTokenRemove() {
onTokenStateRemove([tokenState.id]); onTokenStateRemove([tokenState.id]);
} }
return ( return <DragOverlay dragging={dragging} onRemove={handleTokenRemove} />;
<DragOverlay
dragging={dragging}
onRemove={handleTokenRemove}
node={tokenNode}
/>
);
} }
export default TokenDragOverlay; export default TokenDragOverlay;

View File

@ -31,11 +31,8 @@ function useMapNotes(
setIsNoteMenuOpen(true); setIsNoteMenuOpen(true);
} }
function handleNoteDragStart( function handleNoteDragStart(_: KonvaEventObject<DragEvent>, noteId: string) {
event: KonvaEventObject<DragEvent>, setNoteDraggingOptions({ dragging: true, noteId });
noteId: string
) {
setNoteDraggingOptions({ dragging: true, noteId, noteGroup: event.target });
} }
function handleNoteDragEnd() { function handleNoteDragEnd() {
@ -86,7 +83,6 @@ function useMapNotes(
const noteDragOverlay = noteDraggingOptions ? ( const noteDragOverlay = noteDraggingOptions ? (
<NoteDragOverlay <NoteDragOverlay
dragging={noteDraggingOptions.dragging} dragging={noteDraggingOptions.dragging}
noteGroup={noteDraggingOptions.noteGroup}
noteId={noteDraggingOptions.noteId} noteId={noteDraggingOptions.noteId}
onNoteRemove={handleNoteRemove} onNoteRemove={handleNoteRemove}
/> />

View File

@ -55,13 +55,12 @@ function useMapTokens(
} }
function handleTokenDragStart( function handleTokenDragStart(
event: KonvaEventObject<DragEvent>, _: KonvaEventObject<DragEvent>,
tokenStateId: string tokenStateId: string
) { ) {
setTokenDraggingOptions({ setTokenDraggingOptions({
dragging: true, dragging: true,
tokenStateId, tokenStateId,
tokenNode: event.target,
}); });
} }
@ -122,7 +121,6 @@ function useMapTokens(
<TokenDragOverlay <TokenDragOverlay
onTokenStateRemove={handleTokenStateRemove} onTokenStateRemove={handleTokenStateRemove}
tokenState={tokenDraggingState} tokenState={tokenDraggingState}
tokenNode={tokenDraggingOptions.tokenNode}
dragging={!!(tokenDraggingOptions && tokenDraggingOptions.dragging)} dragging={!!(tokenDraggingOptions && tokenDraggingOptions.dragging)}
/> />
); );

View File

@ -23,7 +23,6 @@ export type NoteMenuOptions = {
export type NoteDraggingOptions = { export type NoteDraggingOptions = {
dragging: boolean; dragging: boolean;
noteId: string; noteId: string;
noteGroup: Konva.Node;
}; };
export type Notes = Record<string, Note>; export type Notes = Record<string, Note>;

View File

@ -39,5 +39,4 @@ export type TokenMenuOptions = {
export type TokenDraggingOptions = { export type TokenDraggingOptions = {
dragging: boolean; dragging: boolean;
tokenStateId: string; tokenStateId: string;
tokenNode: Konva.Node;
}; };