Add check for selected items visible and locked properties

This commit is contained in:
Mitchell McCaffrey 2021-07-22 15:21:36 +10:00
parent 7860871e7e
commit 543fe6f941
3 changed files with 35 additions and 0 deletions

View File

@ -151,6 +151,7 @@ function Map({
const { selectionTool, selectionMenu } = useMapSelection( const { selectionTool, selectionMenu } = useMapSelection(
map, map,
mapState,
onSelectionItemsChange, onSelectionItemsChange,
selectedToolId, selectedToolId,
settings.select settings.select

View File

@ -23,6 +23,7 @@ import { Note } from "../../types/Note";
import { getSelectionPoints } from "../../helpers/selection"; import { getSelectionPoints } from "../../helpers/selection";
import Vector2 from "../../helpers/Vector2"; import Vector2 from "../../helpers/Vector2";
import { useMapStage } from "../../contexts/MapStageContext"; import { useMapStage } from "../../contexts/MapStageContext";
import { MapState } from "../../types/MapState";
type SelectionMenuProps = { type SelectionMenuProps = {
isOpen: boolean; isOpen: boolean;
@ -30,6 +31,7 @@ type SelectionMenuProps = {
selection: Selection | null; selection: Selection | null;
onSelectionItemsChange: SelectionItemsChangeEventHandler; onSelectionItemsChange: SelectionItemsChangeEventHandler;
map: Map | null; map: Map | null;
mapState: MapState | null;
}; };
function SelectionMenu({ function SelectionMenu({
@ -38,6 +40,7 @@ function SelectionMenu({
selection, selection,
onSelectionItemsChange, onSelectionItemsChange,
map, map,
mapState,
}: SelectionMenuProps) { }: SelectionMenuProps) {
const userId = useUserId(); const userId = useUserId();
@ -101,6 +104,34 @@ function SelectionMenu({
setItemsLocked(!itemsLocked); setItemsLocked(!itemsLocked);
} }
useEffect(() => {
if (isOpen && selection && mapState) {
let allVisible = true;
let allLocked = true;
for (let item of selection.items) {
if (item.type === "token") {
const tokenState = mapState.tokens[item.id];
if (!tokenState.visible) {
allVisible = false;
}
if (!tokenState.locked) {
allLocked = false;
}
} else {
const note = mapState.notes[item.id];
if (!note.visible) {
allVisible = false;
}
if (!note.locked) {
allLocked = false;
}
}
}
setItemsVisible(allVisible);
setItemsLocked(allLocked);
}
}, [mapState, selection, isOpen]);
function handleModalContent(node: HTMLElement) { function handleModalContent(node: HTMLElement) {
if (node) { if (node) {
// Focus input // Focus input

View File

@ -3,11 +3,13 @@ import SelectionMenu from "../components/selection/SelectionMenu";
import SelectTool from "../components/tools/SelectTool"; import SelectTool from "../components/tools/SelectTool";
import { SelectionItemsChangeEventHandler } from "../types/Events"; import { SelectionItemsChangeEventHandler } from "../types/Events";
import { Map, MapToolId } from "../types/Map"; import { Map, MapToolId } from "../types/Map";
import { MapState } from "../types/MapState";
import { Selection } from "../types/Select"; import { Selection } from "../types/Select";
import { SelectToolSettings } from "../types/Select"; import { SelectToolSettings } from "../types/Select";
function useMapSelection( function useMapSelection(
map: Map | null, map: Map | null,
mapState: MapState | null,
onSelectionItemsChange: SelectionItemsChangeEventHandler, onSelectionItemsChange: SelectionItemsChangeEventHandler,
selectedToolId: MapToolId, selectedToolId: MapToolId,
settings: SelectToolSettings settings: SelectToolSettings
@ -38,6 +40,7 @@ function useMapSelection(
selection={selection} selection={selection}
onSelectionItemsChange={onSelectionItemsChange} onSelectionItemsChange={onSelectionItemsChange}
map={map} map={map}
mapState={mapState}
/> />
); );