diff --git a/src/components/token/SelectTokensButton.js b/src/components/token/SelectTokensButton.js index 503abd4..398aef7 100644 --- a/src/components/token/SelectTokensButton.js +++ b/src/components/token/SelectTokensButton.js @@ -5,7 +5,7 @@ import SelectTokensIcon from "../../icons/SelectTokensIcon"; import SelectTokensModal from "../../modals/SelectTokensModal"; -function SelectTokensButton({ onMapTokenStateCreate }) { +function SelectTokensButton({ onMapTokensStateCreate }) { const [isModalOpen, setIsModalOpen] = useState(false); function openModal() { setIsModalOpen(true); @@ -30,7 +30,7 @@ function SelectTokensButton({ onMapTokenStateCreate }) { isOpen={isModalOpen} onRequestClose={closeModal} onDone={handleDone} - onMapTokenStateCreate={onMapTokenStateCreate} + onMapTokensStateCreate={onMapTokensStateCreate} /> ); diff --git a/src/components/token/TokenBar.js b/src/components/token/TokenBar.js index f00fea8..ec7539a 100644 --- a/src/components/token/TokenBar.js +++ b/src/components/token/TokenBar.js @@ -21,7 +21,7 @@ import { clientPositionToMapPosition, } from "../../helpers/token"; -function TokenBar({ onMapTokenStateCreate }) { +function TokenBar({ onMapTokensStateCreate }) { const { userId } = useAuth(); const { tokensById, tokenGroups } = useTokenData(); const [fullScreen] = useSetting("map.fullScreen"); @@ -53,7 +53,7 @@ function TokenBar({ onMapTokenStateCreate }) { const token = tokensById[active.id]; if (token && mapPosition) { const tokenState = createTokenState(token, mapPosition, userId); - onMapTokenStateCreate(tokenState); + onMapTokensStateCreate([tokenState]); } } } @@ -123,7 +123,7 @@ function TokenBar({ onMapTokenStateCreate }) { alignItems: "center", }} > - + {createPortal( 0) { + onMapTokensStateCreate(newTokenStates); + } } /** diff --git a/src/network/NetworkedMapAndTokens.js b/src/network/NetworkedMapAndTokens.js index 6517d25..1e209b9 100644 --- a/src/network/NetworkedMapAndTokens.js +++ b/src/network/NetworkedMapAndTokens.js @@ -88,20 +88,18 @@ function NetworkedMapAndTokens({ session }) { setAssetManifest({ mapId: map.id, assets }, true, true); } - function addAssetIfNeeded(asset) { + function addAssetsIfNeeded(assets) { setAssetManifest((prevManifest) => { if (prevManifest?.assets) { - const id = asset.id; - const exists = id in prevManifest.assets; - if (!exists) { - return { - ...prevManifest, - assets: { - ...prevManifest.assets, - [id]: asset, - }, - }; + let newManifset = { ...prevManifest }; + for (let asset of assets) { + const id = asset.id; + const exists = id in prevManifest.assets; + if (!exists) { + newManifset[id] = asset; + } } + return newManifset; } return prevManifest; }); @@ -337,20 +335,28 @@ function NetworkedMapAndTokens({ session }) { * Token state */ - async function handleMapTokenStateCreate(tokenState) { + async function handleMapTokensStateCreate(tokenStates) { if (!currentMap || !currentMapState) { return; } - if (tokenState.file) { - addAssetIfNeeded({ id: tokenState.file, owner: tokenState.owner }); + + let assets = []; + for (let tokenState of tokenStates) { + if (tokenState.type === "file") { + assets.push({ id: tokenState.file, owner: tokenState.owner }); + } } - setCurrentMapState((prevMapState) => ({ - ...prevMapState, - tokens: { - ...prevMapState.tokens, - [tokenState.id]: tokenState, - }, - })); + if (assets.length > 0) { + addAssetsIfNeeded(assets); + } + + setCurrentMapState((prevMapState) => { + let newMapTokens = { ...prevMapState.tokens }; + for (let tokenState of tokenStates) { + newMapTokens[tokenState.id] = tokenState; + } + return { ...prevMapState, tokens: newMapTokens }; + }); } function handleMapTokenStateChange(change) { @@ -475,7 +481,7 @@ function NetworkedMapAndTokens({ session }) { disabledTokens={disabledMapTokens} session={session} /> - + ); }