Add more efficient method of adding multiple tokens to the map at once

This commit is contained in:
Mitchell McCaffrey 2021-06-03 12:42:19 +10:00
parent 0a03387bf4
commit 1ae9ce06cb
4 changed files with 42 additions and 32 deletions

View File

@ -5,7 +5,7 @@ import SelectTokensIcon from "../../icons/SelectTokensIcon";
import SelectTokensModal from "../../modals/SelectTokensModal"; import SelectTokensModal from "../../modals/SelectTokensModal";
function SelectTokensButton({ onMapTokenStateCreate }) { function SelectTokensButton({ onMapTokensStateCreate }) {
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
function openModal() { function openModal() {
setIsModalOpen(true); setIsModalOpen(true);
@ -30,7 +30,7 @@ function SelectTokensButton({ onMapTokenStateCreate }) {
isOpen={isModalOpen} isOpen={isModalOpen}
onRequestClose={closeModal} onRequestClose={closeModal}
onDone={handleDone} onDone={handleDone}
onMapTokenStateCreate={onMapTokenStateCreate} onMapTokensStateCreate={onMapTokensStateCreate}
/> />
</> </>
); );

View File

@ -21,7 +21,7 @@ import {
clientPositionToMapPosition, clientPositionToMapPosition,
} from "../../helpers/token"; } from "../../helpers/token";
function TokenBar({ onMapTokenStateCreate }) { function TokenBar({ onMapTokensStateCreate }) {
const { userId } = useAuth(); const { userId } = useAuth();
const { tokensById, tokenGroups } = useTokenData(); const { tokensById, tokenGroups } = useTokenData();
const [fullScreen] = useSetting("map.fullScreen"); const [fullScreen] = useSetting("map.fullScreen");
@ -53,7 +53,7 @@ function TokenBar({ onMapTokenStateCreate }) {
const token = tokensById[active.id]; const token = tokensById[active.id];
if (token && mapPosition) { if (token && mapPosition) {
const tokenState = createTokenState(token, mapPosition, userId); const tokenState = createTokenState(token, mapPosition, userId);
onMapTokenStateCreate(tokenState); onMapTokensStateCreate([tokenState]);
} }
} }
} }
@ -123,7 +123,7 @@ function TokenBar({ onMapTokenStateCreate }) {
alignItems: "center", alignItems: "center",
}} }}
> >
<SelectTokensButton onMapTokenStateCreate={onMapTokenStateCreate} /> <SelectTokensButton onMapTokensStateCreate={onMapTokensStateCreate} />
</Flex> </Flex>
{createPortal( {createPortal(
<DragOverlay <DragOverlay

View File

@ -40,7 +40,7 @@ import { useMapStage } from "../contexts/MapStageContext";
import shortcuts from "../shortcuts"; import shortcuts from "../shortcuts";
function SelectTokensModal({ isOpen, onRequestClose, onMapTokenStateCreate }) { function SelectTokensModal({ isOpen, onRequestClose, onMapTokensStateCreate }) {
const { addToast } = useToasts(); const { addToast } = useToasts();
const { userId } = useAuth(); const { userId } = useAuth();
@ -175,11 +175,11 @@ function SelectTokensModal({ isOpen, onRequestClose, onMapTokenStateCreate }) {
return; return;
} }
let newTokenStates = [];
for (let id of groupIds) { for (let id of groupIds) {
if (id in tokensById) { if (id in tokensById) {
onMapTokenStateCreate( newTokenStates.push(createTokenState(tokensById[id], position, userId));
createTokenState(tokensById[id], position, userId)
);
position = Vector2.add(position, 0.01); position = Vector2.add(position, 0.01);
} else { } else {
// Check if a group is selected // Check if a group is selected
@ -191,7 +191,7 @@ function SelectTokensModal({ isOpen, onRequestClose, onMapTokenStateCreate }) {
const items = getGroupItems(group); const items = getGroupItems(group);
for (let item of items) { for (let item of items) {
if (item.id in tokensById) { if (item.id in tokensById) {
onMapTokenStateCreate( newTokenStates.push(
createTokenState(tokensById[item.id], position, userId) createTokenState(tokensById[item.id], position, userId)
); );
position = Vector2.add(position, 0.01); position = Vector2.add(position, 0.01);
@ -200,6 +200,10 @@ function SelectTokensModal({ isOpen, onRequestClose, onMapTokenStateCreate }) {
} }
} }
} }
if (newTokenStates.length > 0) {
onMapTokensStateCreate(newTokenStates);
}
} }
/** /**

View File

@ -88,21 +88,19 @@ function NetworkedMapAndTokens({ session }) {
setAssetManifest({ mapId: map.id, assets }, true, true); setAssetManifest({ mapId: map.id, assets }, true, true);
} }
function addAssetIfNeeded(asset) { function addAssetsIfNeeded(assets) {
setAssetManifest((prevManifest) => { setAssetManifest((prevManifest) => {
if (prevManifest?.assets) { if (prevManifest?.assets) {
let newManifset = { ...prevManifest };
for (let asset of assets) {
const id = asset.id; const id = asset.id;
const exists = id in prevManifest.assets; const exists = id in prevManifest.assets;
if (!exists) { if (!exists) {
return { newManifset[id] = asset;
...prevManifest,
assets: {
...prevManifest.assets,
[id]: asset,
},
};
} }
} }
return newManifset;
}
return prevManifest; return prevManifest;
}); });
} }
@ -337,20 +335,28 @@ function NetworkedMapAndTokens({ session }) {
* Token state * Token state
*/ */
async function handleMapTokenStateCreate(tokenState) { async function handleMapTokensStateCreate(tokenStates) {
if (!currentMap || !currentMapState) { if (!currentMap || !currentMapState) {
return; 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, if (assets.length > 0) {
tokens: { addAssetsIfNeeded(assets);
...prevMapState.tokens, }
[tokenState.id]: tokenState,
}, setCurrentMapState((prevMapState) => {
})); let newMapTokens = { ...prevMapState.tokens };
for (let tokenState of tokenStates) {
newMapTokens[tokenState.id] = tokenState;
}
return { ...prevMapState, tokens: newMapTokens };
});
} }
function handleMapTokenStateChange(change) { function handleMapTokenStateChange(change) {
@ -475,7 +481,7 @@ function NetworkedMapAndTokens({ session }) {
disabledTokens={disabledMapTokens} disabledTokens={disabledMapTokens}
session={session} session={session}
/> />
<TokenBar onMapTokenStateCreate={handleMapTokenStateCreate} /> <TokenBar onMapTokensStateCreate={handleMapTokensStateCreate} />
</> </>
); );
} }