Fixed bug with corrupted map state on token deletion

This commit is contained in:
Mitchell McCaffrey 2021-04-16 13:36:14 +10:00
parent 3577938501
commit 1d5cf77aea
7 changed files with 36 additions and 24 deletions

View File

@ -209,7 +209,6 @@ function Map({
tokenGroup={tokenDraggingOptions && tokenDraggingOptions.tokenGroup} tokenGroup={tokenDraggingOptions && tokenDraggingOptions.tokenGroup}
dragging={!!(tokenDraggingOptions && tokenDraggingOptions.dragging)} dragging={!!(tokenDraggingOptions && tokenDraggingOptions.dragging)}
token={tokensById[tokenDraggingOptions.tokenState.tokenId]} token={tokensById[tokenDraggingOptions.tokenState.tokenId]}
mapState={mapState}
/> />
); );

View File

@ -31,7 +31,6 @@ function MapToken({
onTokenDragStart, onTokenDragStart,
onTokenDragEnd, onTokenDragEnd,
draggable, draggable,
mapState,
fadeOnHover, fadeOnHover,
map, map,
}) { }) {
@ -111,7 +110,6 @@ function MapToken({
mountedToken.moveTo(parent); mountedToken.moveTo(parent);
mountedToken.absolutePosition(position); mountedToken.absolutePosition(position);
mountChanges[mountedToken.id()] = { mountChanges[mountedToken.id()] = {
...mapState.tokens[mountedToken.id()],
x: mountedToken.x() / mapWidth, x: mountedToken.x() / mapWidth,
y: mountedToken.y() / mapHeight, y: mountedToken.y() / mapHeight,
lastModifiedBy: userId, lastModifiedBy: userId,
@ -124,7 +122,6 @@ function MapToken({
onTokenStateChange({ onTokenStateChange({
...mountChanges, ...mountChanges,
[tokenState.id]: { [tokenState.id]: {
...tokenState,
x: tokenGroup.x() / mapWidth, x: tokenGroup.x() / mapWidth,
y: tokenGroup.y() / mapHeight, y: tokenGroup.y() / mapHeight,
lastModifiedBy: userId, lastModifiedBy: userId,

View File

@ -119,7 +119,6 @@ function MapTokens({
!(tokenState.id in disabledTokens) && !(tokenState.id in disabledTokens) &&
!tokenState.locked !tokenState.locked
} }
mapState={mapState}
fadeOnHover={selectedToolId === "drawing"} fadeOnHover={selectedToolId === "drawing"}
map={map} map={map}
/> />

View File

@ -15,7 +15,6 @@ function TokenDragOverlay({
tokenState, tokenState,
tokenGroup, tokenGroup,
dragging, dragging,
mapState,
}) { }) {
const { userId } = useAuth(); const { userId } = useAuth();
@ -34,7 +33,6 @@ function TokenDragOverlay({
mountedToken.absolutePosition(position); mountedToken.absolutePosition(position);
onTokenStateChange({ onTokenStateChange({
[mountedToken.id()]: { [mountedToken.id()]: {
...mapState.tokens[mountedToken.id()],
x: mountedToken.x() / mapWidth, x: mountedToken.x() / mapWidth,
y: mountedToken.y() / mapHeight, y: mountedToken.y() / mapHeight,
lastModifiedBy: userId, lastModifiedBy: userId,

View File

@ -51,8 +51,7 @@ function TokenMenu({
function handleLabelChange(event) { function handleLabelChange(event) {
const label = event.target.value.substring(0, 144); const label = event.target.value.substring(0, 144);
tokenState && tokenState && onTokenStateChange({ [tokenState.id]: { label: label } });
onTokenStateChange({ [tokenState.id]: { ...tokenState, label: label } });
} }
function handleStatusChange(status) { function handleStatusChange(status) {
@ -66,35 +65,34 @@ function TokenMenu({
statuses.add(status); statuses.add(status);
} }
onTokenStateChange({ onTokenStateChange({
[tokenState.id]: { ...tokenState, statuses: [...statuses] }, [tokenState.id]: { statuses: [...statuses] },
}); });
} }
function handleSizeChange(event) { function handleSizeChange(event) {
const newSize = parseFloat(event.target.value); const newSize = parseFloat(event.target.value);
tokenState && tokenState && onTokenStateChange({ [tokenState.id]: { size: newSize } });
onTokenStateChange({ [tokenState.id]: { ...tokenState, size: newSize } });
} }
function handleRotationChange(event) { function handleRotationChange(event) {
const newRotation = parseInt(event.target.value); const newRotation = parseInt(event.target.value);
tokenState && tokenState &&
onTokenStateChange({ onTokenStateChange({
[tokenState.id]: { ...tokenState, rotation: newRotation }, [tokenState.id]: { rotation: newRotation },
}); });
} }
function handleVisibleChange() { function handleVisibleChange() {
tokenState && tokenState &&
onTokenStateChange({ onTokenStateChange({
[tokenState.id]: { ...tokenState, visible: !tokenState.visible }, [tokenState.id]: { visible: !tokenState.visible },
}); });
} }
function handleLockChange() { function handleLockChange() {
tokenState && tokenState &&
onTokenStateChange({ onTokenStateChange({
[tokenState.id]: { ...tokenState, locked: !tokenState.locked }, [tokenState.id]: { locked: !tokenState.locked },
}); });
} }

View File

@ -1,9 +1,18 @@
import { applyChange, revertChange, diff as deepDiff } from "deep-diff"; import { applyChange, revertChange, diff as deepDiff } from "deep-diff";
import get from "lodash.get";
export function applyChanges(target, changes) { export function applyChanges(target, changes) {
for (let change of changes) { for (let change of changes) {
if (change.path && (change.kind === "E" || change.kind === "A")) {
// If editing an object or array ensure that the value exists
const valid = get(target, change.path) !== undefined;
if (valid) {
applyChange(target, true, change); applyChange(target, true, change);
} }
} else {
applyChange(target, true, change);
}
}
} }
export function revertChanges(target, changes) { export function revertChanges(target, changes) {

View File

@ -377,20 +377,32 @@ function NetworkedMapAndTokens({ session }) {
const { id, lastModified, owner } = token; const { id, lastModified, owner } = token;
addAssetIfNeeded({ type: "token", id, lastModified, owner }); addAssetIfNeeded({ type: "token", id, lastModified, owner });
} }
handleMapTokenStateChange({ [tokenState.id]: tokenState }); setCurrentMapState((prevMapState) => ({
...prevMapState,
tokens: {
...prevMapState.tokens,
[tokenState.id]: tokenState,
},
}));
} }
function handleMapTokenStateChange(change) { function handleMapTokenStateChange(change) {
if (!currentMapState) { if (!currentMapState) {
return; return;
} }
setCurrentMapState((prevMapState) => ({ setCurrentMapState((prevMapState) => {
let tokens = { ...prevMapState.tokens };
for (let id in change) {
if (id in tokens) {
tokens[id] = { ...tokens[id], ...change[id] };
}
}
return {
...prevMapState, ...prevMapState,
tokens: { tokens,
...prevMapState.tokens, };
...change, });
},
}));
} }
function handleMapTokenStateRemove(tokenState) { function handleMapTokenStateRemove(tokenState) {