2020-11-04 15:03:34 +11:00
|
|
|
import React, { useContext, useState, useEffect, useRef } from "react";
|
2020-11-03 17:15:39 +11:00
|
|
|
import shortid from "shortid";
|
|
|
|
|
import { Group } from "react-konva";
|
2020-11-03 16:21:39 +11:00
|
|
|
|
|
|
|
|
import MapInteractionContext from "../../contexts/MapInteractionContext";
|
|
|
|
|
import MapStageContext from "../../contexts/MapStageContext";
|
2020-11-03 17:15:39 +11:00
|
|
|
import AuthContext from "../../contexts/AuthContext";
|
2020-11-03 16:21:39 +11:00
|
|
|
|
|
|
|
|
import { getBrushPositionForTool } from "../../helpers/drawing";
|
|
|
|
|
import { getRelativePointerPositionNormalized } from "../../helpers/konva";
|
|
|
|
|
|
2020-11-03 17:15:39 +11:00
|
|
|
import MapNote from "./MapNote";
|
|
|
|
|
|
2020-11-03 16:21:39 +11:00
|
|
|
const defaultNoteSize = 2;
|
|
|
|
|
|
2020-11-03 17:15:39 +11:00
|
|
|
function MapNotes({
|
|
|
|
|
map,
|
|
|
|
|
selectedToolSettings,
|
|
|
|
|
active,
|
|
|
|
|
gridSize,
|
|
|
|
|
onNoteAdd,
|
2020-11-04 15:03:34 +11:00
|
|
|
onNoteChange,
|
2020-11-03 17:15:39 +11:00
|
|
|
notes,
|
2020-11-04 15:03:34 +11:00
|
|
|
onNoteMenuOpen,
|
2020-11-03 17:15:39 +11:00
|
|
|
}) {
|
|
|
|
|
const { interactionEmitter } = useContext(MapInteractionContext);
|
|
|
|
|
const { userId } = useContext(AuthContext);
|
2020-11-03 16:21:39 +11:00
|
|
|
const mapStageRef = useContext(MapStageContext);
|
|
|
|
|
const [isBrushDown, setIsBrushDown] = useState(false);
|
2020-11-03 17:15:39 +11:00
|
|
|
const [noteData, setNoteData] = useState(null);
|
2020-11-03 16:21:39 +11:00
|
|
|
|
2020-11-04 15:03:34 +11:00
|
|
|
const creatingNoteRef = useRef();
|
|
|
|
|
|
2020-11-03 16:21:39 +11:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!active) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const mapStage = mapStageRef.current;
|
|
|
|
|
|
|
|
|
|
function getBrushPosition() {
|
|
|
|
|
const mapImage = mapStage.findOne("#mapImage");
|
|
|
|
|
return getBrushPositionForTool(
|
|
|
|
|
map,
|
|
|
|
|
getRelativePointerPositionNormalized(mapImage),
|
|
|
|
|
map.snapToGrid,
|
|
|
|
|
false,
|
|
|
|
|
gridSize,
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleBrushDown() {
|
2020-11-04 15:03:34 +11:00
|
|
|
if (selectedToolSettings.type === "add") {
|
|
|
|
|
const brushPosition = getBrushPosition();
|
|
|
|
|
setNoteData({
|
|
|
|
|
x: brushPosition.x,
|
|
|
|
|
y: brushPosition.y,
|
|
|
|
|
size: defaultNoteSize,
|
|
|
|
|
text: "",
|
|
|
|
|
id: shortid.generate(),
|
|
|
|
|
lastModified: Date.now(),
|
|
|
|
|
lastModifiedBy: userId,
|
|
|
|
|
visible: true,
|
|
|
|
|
locked: false,
|
|
|
|
|
color: "yellow",
|
|
|
|
|
});
|
|
|
|
|
setIsBrushDown(true);
|
|
|
|
|
}
|
2020-11-03 16:21:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleBrushMove() {
|
2020-11-04 15:03:34 +11:00
|
|
|
if (selectedToolSettings.type === "add") {
|
|
|
|
|
const brushPosition = getBrushPosition();
|
|
|
|
|
setNoteData((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
x: brushPosition.x,
|
|
|
|
|
y: brushPosition.y,
|
|
|
|
|
}));
|
|
|
|
|
setIsBrushDown(true);
|
|
|
|
|
}
|
2020-11-03 16:21:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleBrushUp() {
|
2020-11-04 15:03:34 +11:00
|
|
|
if (selectedToolSettings.type === "add") {
|
|
|
|
|
onNoteAdd(noteData);
|
|
|
|
|
onNoteMenuOpen(noteData.id, creatingNoteRef.current);
|
|
|
|
|
}
|
2020-11-03 17:15:39 +11:00
|
|
|
setNoteData(null);
|
2020-11-03 16:21:39 +11:00
|
|
|
setIsBrushDown(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interactionEmitter.on("dragStart", handleBrushDown);
|
|
|
|
|
interactionEmitter.on("drag", handleBrushMove);
|
|
|
|
|
interactionEmitter.on("dragEnd", handleBrushUp);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
interactionEmitter.off("dragStart", handleBrushDown);
|
|
|
|
|
interactionEmitter.off("drag", handleBrushMove);
|
|
|
|
|
interactionEmitter.off("dragEnd", handleBrushUp);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Group>
|
2020-11-03 17:15:39 +11:00
|
|
|
{notes.map((note) => (
|
2020-11-04 15:03:34 +11:00
|
|
|
<MapNote
|
|
|
|
|
note={note}
|
|
|
|
|
map={map}
|
|
|
|
|
key={note.id}
|
|
|
|
|
onNoteMenuOpen={onNoteMenuOpen}
|
|
|
|
|
draggable={active && selectedToolSettings.type === "move"}
|
|
|
|
|
onNoteChange={onNoteChange}
|
|
|
|
|
/>
|
2020-11-03 17:15:39 +11:00
|
|
|
))}
|
2020-11-04 15:03:34 +11:00
|
|
|
<Group ref={creatingNoteRef}>
|
|
|
|
|
{isBrushDown && noteData && (
|
|
|
|
|
<MapNote note={noteData} map={map} draggable={false} />
|
|
|
|
|
)}
|
|
|
|
|
</Group>
|
2020-11-03 16:21:39 +11:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapNotes;
|