Update token state and note events to be more similar

This commit is contained in:
Mitchell McCaffrey 2021-07-20 20:17:41 +10:00
parent fe8e4387c6
commit 24dddad66f
10 changed files with 76 additions and 41 deletions

View File

@ -81,11 +81,12 @@ function Note({
const noteGroup = event.target; const noteGroup = event.target;
if (userId) { if (userId) {
onNoteChange?.({ onNoteChange?.({
...note, [note.id]: {
x: noteGroup.x() / mapWidth, x: noteGroup.x() / mapWidth,
y: noteGroup.y() / mapHeight, y: noteGroup.y() / mapHeight,
lastModifiedBy: userId, lastModifiedBy: userId,
lastModified: Date.now(), lastModified: Date.now(),
},
}); });
} }
onNoteDragEnd?.(event, note.id); onNoteDragEnd?.(event, note.id);

View File

@ -36,6 +36,7 @@ import {
NoteChangeEventHandler, NoteChangeEventHandler,
NoteRemoveEventHander, NoteRemoveEventHander,
TokenStateChangeEventHandler, TokenStateChangeEventHandler,
NoteCreateEventHander,
} from "../../types/Events"; } from "../../types/Events";
import useMapTokens from "../../hooks/useMapTokens"; import useMapTokens from "../../hooks/useMapTokens";
@ -55,6 +56,7 @@ type MapProps = {
onFogDraw: (action: Action<FogState>) => void; onFogDraw: (action: Action<FogState>) => void;
onFogDrawUndo: () => void; onFogDrawUndo: () => void;
onFogDrawRedo: () => void; onFogDrawRedo: () => void;
onMapNoteCreate: NoteCreateEventHander;
onMapNoteChange: NoteChangeEventHandler; onMapNoteChange: NoteChangeEventHandler;
onMapNoteRemove: NoteRemoveEventHander; onMapNoteRemove: NoteRemoveEventHander;
allowMapDrawing: boolean; allowMapDrawing: boolean;
@ -79,6 +81,7 @@ function Map({
onFogDraw, onFogDraw,
onFogDrawUndo, onFogDrawUndo,
onFogDrawRedo, onFogDrawRedo,
onMapNoteCreate,
onMapNoteChange, onMapNoteChange,
onMapNoteRemove, onMapNoteRemove,
allowMapDrawing, allowMapDrawing,
@ -206,6 +209,7 @@ function Map({
const { notes, noteMenu, noteDragOverlay } = useMapNotes( const { notes, noteMenu, noteDragOverlay } = useMapNotes(
map, map,
mapState, mapState,
onMapNoteCreate,
onMapNoteChange, onMapNoteChange,
onMapNoteRemove, onMapNoteRemove,
selectedToolId, selectedToolId,

View File

@ -1,9 +1,10 @@
import Konva from "konva"; import Konva from "konva";
import { NoteRemoveEventHander } from "../../types/Events";
import DragOverlay from "../map/DragOverlay"; import DragOverlay from "../map/DragOverlay";
type NoteDragOverlayProps = { type NoteDragOverlayProps = {
onNoteRemove: (noteId: string) => void; onNoteRemove: NoteRemoveEventHander;
noteId: string; noteId: string;
noteGroup: Konva.Node; noteGroup: Konva.Node;
dragging: boolean; dragging: boolean;
@ -16,7 +17,7 @@ function NoteDragOverlay({
dragging, dragging,
}: NoteDragOverlayProps) { }: NoteDragOverlayProps) {
function handleNoteRemove() { function handleNoteRemove() {
onNoteRemove(noteId); onNoteRemove([noteId]);
} }
return ( return (

View File

@ -73,31 +73,31 @@ function NoteMenu({
function handleTextChange(event: React.ChangeEvent<HTMLTextAreaElement>) { function handleTextChange(event: React.ChangeEvent<HTMLTextAreaElement>) {
const text = event.target.value.substring(0, 1024); const text = event.target.value.substring(0, 1024);
note && onNoteChange({ ...note, text: text }); note && onNoteChange({ [note.id]: { text: text } });
} }
function handleColorChange(color: Color) { function handleColorChange(color: Color) {
if (!note) { if (!note) {
return; return;
} }
onNoteChange({ ...note, color: color }); onNoteChange({ [note.id]: { color: color } });
} }
function handleSizeChange(event: React.ChangeEvent<HTMLInputElement>) { function handleSizeChange(event: React.ChangeEvent<HTMLInputElement>) {
const newSize = parseFloat(event.target.value); const newSize = parseFloat(event.target.value);
note && onNoteChange({ ...note, size: newSize }); note && onNoteChange({ [note.id]: { size: newSize } });
} }
function handleVisibleChange() { function handleVisibleChange() {
note && onNoteChange({ ...note, visible: !note.visible }); note && onNoteChange({ [note.id]: { visible: !note.visible } });
} }
function handleLockChange() { function handleLockChange() {
note && onNoteChange({ ...note, locked: !note.locked }); note && onNoteChange({ [note.id]: { locked: !note.locked } });
} }
function handleModeChange() { function handleModeChange() {
note && onNoteChange({ ...note, textOnly: !note.textOnly }); note && onNoteChange({ [note.id]: { textOnly: !note.textOnly } });
} }
function handleModalContent(node: HTMLElement) { function handleModalContent(node: HTMLElement) {

View File

@ -18,7 +18,7 @@ function TokenDragOverlay({
dragging, dragging,
}: TokenDragOverlayProps) { }: TokenDragOverlayProps) {
function handleTokenRemove() { function handleTokenRemove() {
onTokenStateRemove(tokenState); onTokenStateRemove([tokenState.id]);
} }
return ( return (

View File

@ -17,7 +17,7 @@ import Note from "../konva/Note";
import { Map } from "../../types/Map"; import { Map } from "../../types/Map";
import { Note as NoteType } from "../../types/Note"; import { Note as NoteType } from "../../types/Note";
import { import {
NoteAddEventHander, NoteCreateEventHander,
NoteChangeEventHandler, NoteChangeEventHandler,
NoteDragEventHandler, NoteDragEventHandler,
NoteMenuOpenEventHandler, NoteMenuOpenEventHandler,
@ -28,7 +28,7 @@ const defaultNoteSize = 2;
type MapNoteProps = { type MapNoteProps = {
map: Map | null; map: Map | null;
active: boolean; active: boolean;
onNoteAdd: NoteAddEventHander; onNoteCreate: NoteCreateEventHander;
onNoteChange: NoteChangeEventHandler; onNoteChange: NoteChangeEventHandler;
notes: NoteType[]; notes: NoteType[];
onNoteMenuOpen: NoteMenuOpenEventHandler; onNoteMenuOpen: NoteMenuOpenEventHandler;
@ -41,7 +41,7 @@ type MapNoteProps = {
function NoteTool({ function NoteTool({
map, map,
active, active,
onNoteAdd, onNoteCreate,
onNoteChange, onNoteChange,
notes, notes,
onNoteMenuOpen, onNoteMenuOpen,
@ -127,7 +127,7 @@ function NoteTool({
function handleBrushUp() { function handleBrushUp() {
if (noteData && creatingNoteRef.current) { if (noteData && creatingNoteRef.current) {
onNoteAdd(noteData); onNoteCreate([noteData]);
onNoteMenuOpen(noteData.id, creatingNoteRef.current); onNoteMenuOpen(noteData.id, creatingNoteRef.current);
} }
setNoteData(null); setNoteData(null);

View File

@ -4,7 +4,11 @@ import { useState } from "react";
import NoteDragOverlay from "../components/note/NoteDragOverlay"; import NoteDragOverlay from "../components/note/NoteDragOverlay";
import NoteMenu from "../components/note/NoteMenu"; import NoteMenu from "../components/note/NoteMenu";
import NoteTool from "../components/tools/NoteTool"; import NoteTool from "../components/tools/NoteTool";
import { NoteChangeEventHandler, NoteRemoveEventHander } from "../types/Events"; import {
NoteChangeEventHandler,
NoteCreateEventHander,
NoteRemoveEventHander,
} from "../types/Events";
import { Map, MapToolId } from "../types/Map"; import { Map, MapToolId } from "../types/Map";
import { MapState } from "../types/MapState"; import { MapState } from "../types/MapState";
import { Note, NoteDraggingOptions, NoteMenuOptions } from "../types/Note"; import { Note, NoteDraggingOptions, NoteMenuOptions } from "../types/Note";
@ -12,6 +16,7 @@ import { Note, NoteDraggingOptions, NoteMenuOptions } from "../types/Note";
function useMapNotes( function useMapNotes(
map: Map | null, map: Map | null,
mapState: MapState | null, mapState: MapState | null,
onNoteCreate: NoteCreateEventHander,
onNoteChange: NoteChangeEventHandler, onNoteChange: NoteChangeEventHandler,
onNoteRemove: NoteRemoveEventHander, onNoteRemove: NoteRemoveEventHander,
selectedToolId: MapToolId, selectedToolId: MapToolId,
@ -38,8 +43,8 @@ function useMapNotes(
setNoteDraggingOptions({ ...noteDraggingOptions, dragging: false }); setNoteDraggingOptions({ ...noteDraggingOptions, dragging: false });
} }
function handleNoteRemove(noteId: string) { function handleNoteRemove(noteIds: string[]) {
onNoteRemove(noteId); onNoteRemove(noteIds);
setNoteDraggingOptions(undefined); setNoteDraggingOptions(undefined);
} }
@ -47,7 +52,7 @@ function useMapNotes(
<NoteTool <NoteTool
map={map} map={map}
active={selectedToolId === "note"} active={selectedToolId === "note"}
onNoteAdd={onNoteChange} onNoteCreate={onNoteCreate}
onNoteChange={onNoteChange} onNoteChange={onNoteChange}
notes={ notes={
mapState mapState

View File

@ -56,8 +56,8 @@ function useMapTokens(
}); });
} }
function handleTokenStateRemove(tokenState: TokenState) { function handleTokenStateRemove(tokenStateIds: string[]) {
onTokenStateRemove(tokenState); onTokenStateRemove(tokenStateIds);
setTokenDraggingOptions(undefined); setTokenDraggingOptions(undefined);
} }

View File

@ -356,29 +356,48 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
} }
}, [currentMap]); }, [currentMap]);
function handleNoteChange(note: Note) { function handleNoteCreate(notes: Note[]) {
setCurrentMapState((prevMapState) => { setCurrentMapState((prevMapState) => {
if (!prevMapState) { if (!prevMapState) {
return prevMapState; return prevMapState;
} }
let newNotes = { ...prevMapState.notes };
for (let note of notes) {
newNotes[note.id] = note;
}
return { return {
...prevMapState, ...prevMapState,
notes: { notes: newNotes,
...prevMapState.notes,
[note.id]: note,
},
}; };
}); });
} }
function handleNoteRemove(noteId: string) { function handleNoteChange(changes: Record<string, Partial<Note>>) {
setCurrentMapState((prevMapState) => {
if (!prevMapState) {
return prevMapState;
}
let notes = { ...prevMapState.notes };
for (let id in changes) {
if (id in notes) {
notes[id] = { ...notes[id], ...changes[id] } as Note;
}
}
return {
...prevMapState,
notes,
};
});
}
function handleNoteRemove(noteIds: string[]) {
setCurrentMapState((prevMapState) => { setCurrentMapState((prevMapState) => {
if (!prevMapState) { if (!prevMapState) {
return prevMapState; return prevMapState;
} }
return { return {
...prevMapState, ...prevMapState,
notes: omit(prevMapState.notes, [noteId]), notes: omit(prevMapState.notes, noteIds),
}; };
}); });
} }
@ -415,7 +434,7 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
} }
function handleMapTokenStateChange( function handleMapTokenStateChange(
change: Record<string, Partial<TokenState>> changes: Record<string, Partial<TokenState>>
) { ) {
if (!currentMapState) { if (!currentMapState) {
return; return;
@ -425,9 +444,9 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
return prevMapState; return prevMapState;
} }
let tokens = { ...prevMapState.tokens }; let tokens = { ...prevMapState.tokens };
for (let id in change) { for (let id in changes) {
if (id in tokens) { if (id in tokens) {
tokens[id] = { ...tokens[id], ...change[id] } as TokenState; tokens[id] = { ...tokens[id], ...changes[id] } as TokenState;
} }
} }
@ -438,13 +457,15 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
}); });
} }
function handleMapTokenStateRemove(tokenState: TokenState) { function handleMapTokenStateRemove(tokenStateIds: string[]) {
setCurrentMapState((prevMapState) => { setCurrentMapState((prevMapState) => {
if (!prevMapState) { if (!prevMapState) {
return prevMapState; return prevMapState;
} }
const { [tokenState.id]: old, ...rest } = prevMapState.tokens; return {
return { ...prevMapState, tokens: rest }; ...prevMapState,
tokens: omit(prevMapState.tokens, tokenStateIds),
};
}); });
} }
@ -552,6 +573,7 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
onFogDraw={handleFogDraw} onFogDraw={handleFogDraw}
onFogDrawUndo={handleFogDrawUndo} onFogDrawUndo={handleFogDrawUndo}
onFogDrawRedo={handleFogDrawRedo} onFogDrawRedo={handleFogDrawRedo}
onMapNoteCreate={handleNoteCreate}
onMapNoteChange={handleNoteChange} onMapNoteChange={handleNoteChange}
onMapNoteRemove={handleNoteRemove} onMapNoteRemove={handleNoteRemove}
allowMapDrawing={!!canEditMapDrawing} allowMapDrawing={!!canEditMapDrawing}

View File

@ -22,7 +22,7 @@ export type DiceSelectEventHandler = (dice: DefaultDice) => void;
export type RequestCloseEventHandler = () => void; export type RequestCloseEventHandler = () => void;
export type TokensStateCreateHandler = (states: TokenState[]) => void; export type TokensStateCreateHandler = (states: TokenState[]) => void;
export type TokenStateRemoveHandler = (state: TokenState) => void; export type TokenStateRemoveHandler = (tokenStateIds: string[]) => void;
export type TokenStateChangeEventHandler = ( export type TokenStateChangeEventHandler = (
changes: Record<string, Partial<TokenState>> changes: Record<string, Partial<TokenState>>
) => void; ) => void;
@ -36,9 +36,11 @@ export type TokenDragEventHandler = (
tokenStateId: string tokenStateId: string
) => void; ) => void;
export type NoteAddEventHander = (note: Note) => void; export type NoteCreateEventHander = (notes: Note[]) => void;
export type NoteRemoveEventHander = (noteId: string) => void; export type NoteRemoveEventHander = (noteIds: string[]) => void;
export type NoteChangeEventHandler = (note: Note) => void; export type NoteChangeEventHandler = (
changes: Record<string, Partial<Note>>
) => void;
export type NoteMenuOpenEventHandler = ( export type NoteMenuOpenEventHandler = (
noteId: string, noteId: string,
noteNode: Konva.Node noteNode: Konva.Node