diff --git a/package.json b/package.json index c2ecee1..60d4e41 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@testing-library/user-event": "^13.2.1", "@types/d3": "^7.1.0", "@types/jest": "^27.0.1", + "@types/lodash": "^4.14.178", "@types/node": "^16.7.13", "@types/react": "^17.0.20", "@types/react-dom": "^17.0.9", @@ -30,6 +31,7 @@ "@use-gesture/react": "^10.2.4", "d3": "^7.2.1", "history": "5", + "lodash": "^4.17.21", "react": "^17.0.2", "react-dom": "^17.0.2", "react-redux": "^7.2.6", diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index b6c76f1..6cc4af5 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -15,13 +15,13 @@ import { Toggle } from "../../Common/Inputs/Toggle"; export function OverlayControl() { const dispatch = useDispatch(); - const { visible, currentOverlayItem } = useSelector(overlayToolSelector); + const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector); /* For prototyping use only */ - const [overlayId, setOverlayId] = useState("foo_1.00000"); + const [idNum, setIdNum] = useState(0); const getRandomArbitrary = (min: number, max: number) => Math.random() * (max - min) + min; @@ -32,10 +32,10 @@ export function OverlayControl() { y: Math.floor(getRandomArbitrary(-50, 650)), })); }; - const idRandomizer = () => { - const newId = `id_${getRandomArbitrary(1, 10)}`.slice(0, 10); - setOverlayId(newId); - dispatch(setCurrentOverlayId(newId)); + const nextIdNum = () => { + const nextIdNum = (idNum + 1) % overlayIds.length; + setIdNum(nextIdNum); + dispatch(setCurrentOverlayId(overlayIds[nextIdNum])); }; /* For prototyping use only */ @@ -44,8 +44,8 @@ export function OverlayControl() { const onToggle = (value: boolean) => { if (currentOverlayItem == null) { dispatch(initNewOverlayItem({ - overlayId, - initialPosition: { x: 400, y: 300 }, + overlayId: overlayIds[idNum], + initialPosition: { x: 0, y: 300 }, setVisible: true, })); } else { @@ -70,9 +70,9 @@ export function OverlayControl() {
- Id: {overlayId} + Id #{idNum}: "{overlayIds[idNum]}" {/* For prototyping use only */} diff --git a/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx b/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx index cf1ccfb..16392a1 100644 --- a/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx +++ b/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx @@ -5,15 +5,21 @@ import { useDrag } from "@use-gesture/react"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { setPosition } from "../../../features/overlayTool/overlayToolSlice"; -import { AnimatedOverlayItemContainer } from "./styles"; +import { OverlayItemContainer } from "./styles"; export function OverlayDisplay() { - const { currentOverlayItem, visible } = useSelector(overlayToolSelector); + const { + currentOverlayId, + currentOverlayItem, + visible, + } = useSelector(overlayToolSelector); + const dispatch = useDispatch(); - const bindOverlayItem = useDrag((state) => { + + const bindOverlayItem = useDrag(({ args, delta }) => { dispatch(setPosition({ - x: state.args[0].xPos + state.delta[0], - y: state.args[0].yPos + state.delta[1], + x: args[0].xPos + delta[0], + y: args[0].yPos + delta[1], })); }); @@ -28,31 +34,33 @@ export function OverlayDisplay() { leave: { top: 50, opacity: 0 }, }); - if (currentOverlayItem == null) { - return null; // Exit - There is no overlay item yet! + // Cannot make an earlier return because all hooks must run or React gets mad. + if (currentOverlayId == null || currentOverlayItem == null) { + return null; } - const { id, position } = currentOverlayItem; + const { position } = currentOverlayItem; - return ( - transition((style, visible) => - visible ? ( + return transition((style, visible) => + visible ? ( + - - {id} + + {currentOverlayId}
- (x:{position.x}px, y:{position.y}px) -
+ (x:{Math.floor(position.x)}px, y:{Math.floor(position.y)}px) +
- ) : null, - ) +
+ ) : null, ); } diff --git a/src/components/OverlayTool/OverlayDisplay/styles.ts b/src/components/OverlayTool/OverlayDisplay/styles.ts index 50e9c68..66deb2d 100644 --- a/src/components/OverlayTool/OverlayDisplay/styles.ts +++ b/src/components/OverlayTool/OverlayDisplay/styles.ts @@ -1,47 +1,21 @@ -import { animated } from "@react-spring/web"; import styled from "styled-components"; -import { OverlayPosition } from "../../../types/state"; - export type OverlayItemContainerProps = { - position: OverlayPosition, visible: boolean, + tempColor: string, }; export const OverlayItemContainer = styled.div.attrs(({ - position: { x, y }, visible, }) => ({ style: { - top: y, - left: x, display: visible ? undefined : "none", }, }))` /* Temporary. Overlay will be transparent and based on an image. */ width: 150px; height: 150px; - background-color: white; + background-color: ${({ tempColor }) => tempColor}; box-shadow: 0px 7px 11px 1px black; `; OverlayItemContainer.displayName = "OverlayItemContainer"; -type AnimatedOverlayItemContainerProps = { - visible: boolean, -} -export const AnimatedOverlayItemContainer = styled(animated.div).attrs(({ - visible, -}) => ({ - style: { - display: visible ? undefined : "none", - }, -}))` - position: absolute; - z-index: 1; - - /* Temporary. Overlay will be transparent and based on an image. */ - width: 150px; - height: 150px; - background-color: white; - box-shadow: 0px 7px 11px 1px black; -`; - diff --git a/src/components/Workspaces/Workspace/Workspace.tsx b/src/components/Workspaces/Workspace/Workspace.tsx index 2f8a764..be39521 100644 --- a/src/components/Workspaces/Workspace/Workspace.tsx +++ b/src/components/Workspaces/Workspace/Workspace.tsx @@ -1,9 +1,13 @@ +import { useEffect } from "react"; import { useParams } from "react-router-dom"; -import { useSelector } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { Center } from "@bedrock-layout/center"; import { Stack } from "@bedrock-layout/stack"; import { workspaceSelector } from "../../../features/workspace/workspaceSelector"; +import { setWorkspaceId } from "../../../features/workspace/workspaceSlice"; +import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; +import { newWorkspace } from "../../../features/overlayTool/overlayToolSlice"; import { PreviewWindow, H2, ControlsContainer } from "./styles"; import { WorkspaceControls } from "./WorkspaceControls"; @@ -15,6 +19,24 @@ import { workspaceObject } from "../workspaces"; export const Workspace = () => { const { workspaceId } = useParams(); const { animatedBG } = useSelector(workspaceSelector); + const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector); + const dispatch = useDispatch(); + + useEffect(() => { + if (workspaceId == null) return; + + // Update workspace store for ID. + dispatch(setWorkspaceId(workspaceId)); + + // Switching Workspace means we need to init a newWorkspace for Overlay tool. + if (overlayWorkspaceId !== workspaceId) { + dispatch(newWorkspace({ + overlayIds: workspaceObject[workspaceId].overlayIds, + workspaceId: workspaceId, + })); + } + }, [workspaceId]); + return ( { maxWidth="80%" centerChildren > -

{workspaceId ? workspaceObject[workspaceId] : "Unknown"}

+

{workspaceId ? workspaceObject[workspaceId].name : "Unknown"}

( - - - {workspaceList.map((workspace, i) => ( - - ))} - - -); +export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => { + const workspaceList = _.keys(workspaceObject); + return ( + + + {workspaceList.map((id, i) => ( + + ))} + + + ); +}; type WorkspaceListNavLinkProps = { to: string, diff --git a/src/components/Workspaces/WorkspacesRoute.tsx b/src/components/Workspaces/WorkspacesRoute.tsx index a563750..c1da8ee 100644 --- a/src/components/Workspaces/WorkspacesRoute.tsx +++ b/src/components/Workspaces/WorkspacesRoute.tsx @@ -13,7 +13,7 @@ import { LayoutGrid, LayoutGridItem } from "../Common/LayoutGrid.styled"; const WORKSPACE_LIST_WIDTH = 180; export default function Workspaces() { - const { height } = useSelector(mainMenuDimensionsSelector); + const { height: mainMenuHeight } = useSelector(mainMenuDimensionsSelector); const dispatch = useDispatch(); const [workspaceRef, { height: workspaceHeight, @@ -31,14 +31,14 @@ export default function Workspaces() { return ( - + diff --git a/src/components/Workspaces/workspaces.ts b/src/components/Workspaces/workspaces.ts index d10fb05..fa2c6b6 100644 --- a/src/components/Workspaces/workspaces.ts +++ b/src/components/Workspaces/workspaces.ts @@ -1,56 +1,57 @@ -export const workspaceList = [ - { +import { OverlayIds } from "../../types/state"; + +export type WorkspaceObject = { + [id: string]: { + name: string, + overlayIds: OverlayIds, + }, +}; +export const workspaceObject: WorkspaceObject = { + workspace1: { name: "Workspace 1", - id: "workspace1", + overlayIds: ["red", "blue", "white"], }, - { + workspace2: { name: "Workspace 2", - id: "workspace2", + overlayIds: ["green"], }, - { + workspace3: { name: "Workspace 3", - id: "workspace3", + overlayIds: ["green"], }, - { + workspace4: { name: "Workspace 4 has a longer name than the rest", - id: "workspace4", + overlayIds: ["green"], }, - { name: "blah", id: "blah0" }, - { name: "blah", id: "blah1" }, - { name: "blah", id: "blah2" }, - { name: "blah", id: "blah3" }, - { name: "blah", id: "blah4" }, - { name: "blah", id: "blah5" }, - { name: "blah", id: "blah6" }, - { name: "blah", id: "blah7" }, - { name: "blah", id: "blah8" }, - { name: "blah", id: "blah9" }, - { name: "blah", id: "blah10" }, - { name: "blah", id: "blah11" }, - { name: "blah", id: "blah12" }, - { name: "blah", id: "blah13" }, - { name: "blah", id: "blah14" }, - { name: "blah", id: "blah15" }, - { name: "blah", id: "blah16" }, - { name: "blah", id: "blah17" }, - { name: "blah", id: "blah18" }, - { name: "blah", id: "blah19" }, - { name: "blah", id: "blah20" }, - { name: "blah", id: "blah21" }, - { name: "blah", id: "blah22" }, - { name: "blah", id: "blah23" }, - { name: "blah", id: "blah24" }, - { name: "blah", id: "blah25" }, - { name: "blah", id: "blah26" }, - { name: "blah", id: "blah27" }, - { name: "blah", id: "blah28" }, - { name: "blah", id: "blah29" }, -]; + blah0: { name: "blah", overlayIds: ["green"] }, + blah1: { name: "blah", overlayIds: ["green"] }, + blah2: { name: "blah", overlayIds: ["green"] }, + blah3: { name: "blah", overlayIds: ["green"] }, + blah4: { name: "blah", overlayIds: ["green"] }, + blah5: { name: "blah", overlayIds: ["green"] }, + blah6: { name: "blah", overlayIds: ["green"] }, + blah7: { name: "blah", overlayIds: ["green"] }, + blah8: { name: "blah", overlayIds: ["green"] }, + blah9: { name: "blah", overlayIds: ["green"] }, + blah10: { name: "blah", overlayIds: ["green"] }, + blah11: { name: "blah", overlayIds: ["green"] }, + blah12: { name: "blah", overlayIds: ["green"] }, + blah13: { name: "blah", overlayIds: ["green"] }, + blah14: { name: "blah", overlayIds: ["green"] }, + blah15: { name: "blah", overlayIds: ["green"] }, + blah16: { name: "blah", overlayIds: ["green"] }, + blah17: { name: "blah", overlayIds: ["green"] }, + blah18: { name: "blah", overlayIds: ["green"] }, + blah19: { name: "blah", overlayIds: ["green"] }, + blah20: { name: "blah", overlayIds: ["green"] }, + blah21: { name: "blah", overlayIds: ["green"] }, + blah22: { name: "blah", overlayIds: ["green"] }, + blah23: { name: "blah", overlayIds: ["green"] }, + blah24: { name: "blah", overlayIds: ["green"] }, + blah25: { name: "blah", overlayIds: ["green"] }, + blah26: { name: "blah", overlayIds: ["green"] }, + blah27: { name: "blah", overlayIds: ["green"] }, + blah28: { name: "blah", overlayIds: ["green"] }, + blah29: { name: "blah", overlayIds: ["green"] }, +}; -export const workspaceObject = ((list: typeof workspaceList) => { - const obj: {[key: string]: string} = {}; - list.forEach(item => { - obj[item.id] = item.name; - }); - return obj; -})(workspaceList); diff --git a/src/features/overlayTool/overlayToolSlice.ts b/src/features/overlayTool/overlayToolSlice.ts index 36bb4e6..966dafc 100644 --- a/src/features/overlayTool/overlayToolSlice.ts +++ b/src/features/overlayTool/overlayToolSlice.ts @@ -2,27 +2,20 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { RootState, + WorkspaceId, OverlayPosition, OverlayId, + OverlayIds, } from "../../types/state"; const initialState: RootState["overlayTool"] = { + currentWorkspaceId: "", overlayIds: [], currentOverlayId: null, currentOverlayItem: null, visible: false, }; -const makeNewOverlayItem = ( - overlayId: OverlayId, - initialPosition: OverlayPosition, -) => ({ - id: overlayId, - position: initialPosition, - opacity: 1, - scale: 1, -}); - type InitNewOverlayItemAction = { overlayId: OverlayId, @@ -30,16 +23,21 @@ type InitNewOverlayItemAction = { setVisible: boolean, }; +type NewWorkspaceAction = { + overlayIds: OverlayIds, + workspaceId: WorkspaceId, +} + export const overlayToolSlice = createSlice({ name: "overlayTool", initialState, reducers: { initNewOverlayItem: (state, action: PayloadAction) => { if (state.currentOverlayItem == null) { - state.currentOverlayItem = makeNewOverlayItem( - action.payload.overlayId, - action.payload.initialPosition, - ); + state.currentOverlayItem = { + position: action.payload.initialPosition, + opacity: 1, + }; state.currentOverlayId = action.payload.overlayId; state.visible = action.payload.setVisible; } @@ -47,16 +45,12 @@ export const overlayToolSlice = createSlice({ setVisibility: (state, action: PayloadAction) => { state.visible = action.payload; }, - setOverlayIds: (state, action: PayloadAction>) => { - state.overlayIds = action.payload; - }, setCurrentOverlayId: (state, action: PayloadAction) => { if ( action.payload !== state.currentOverlayId && state.currentOverlayItem != null ) { state.currentOverlayId = action.payload; - state.currentOverlayItem.id = action.payload; } }, setPosition: (state, action: PayloadAction) => { @@ -64,14 +58,21 @@ export const overlayToolSlice = createSlice({ state.currentOverlayItem.position = action.payload; } }, + newWorkspace: (state, action: PayloadAction) => { + state.currentOverlayId = null; + state.currentOverlayItem = null; + state.visible = false; + state.currentWorkspaceId = action.payload.workspaceId; + state.overlayIds = action.payload.overlayIds; + }, }, }); export const { initNewOverlayItem, setVisibility, - setOverlayIds, setCurrentOverlayId, setPosition, + newWorkspace, } = overlayToolSlice.actions; export default overlayToolSlice.reducer; diff --git a/src/types/state.d.ts b/src/types/state.d.ts index f0bffdb..0d7d47f 100644 --- a/src/types/state.d.ts +++ b/src/types/state.d.ts @@ -22,17 +22,16 @@ export type WorkspaceId = string; // Overlay Tool export type OverlayTool = { - // workspaceId: WorkspaceId, - overlayIds: Array, + currentWorkspaceId: WorkspaceId, + overlayIds: OverlayIds, currentOverlayId: OverlayId | null, currentOverlayItem: OverlayItem | null, visible: boolean, }; export type OverlayId = string; +export type OverlayIds = Array; export type OverlayPosition = { x: number, y: number }; export type OverlayItem = { - id: OverlayId, position: OverlayPosition, opacity: number, - scale: number, }; diff --git a/yarn.lock b/yarn.lock index 515fd36..c36c32a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2103,6 +2103,11 @@ resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= +"@types/lodash@^4.14.178": + version "4.14.178" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8" + integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw== + "@types/node@*": version "17.0.0" resolved "https://registry.npmjs.org/@types/node/-/node-17.0.0.tgz" @@ -6334,11 +6339,16 @@ lodash.uniq@^4.5.0: resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz" integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M= -lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.7.0: +lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.7.0: version "4.17.21" resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== +lodash@^4.17.21: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" + integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== + loose-envify@^1.1.0, loose-envify@^1.4.0: version "1.4.0" resolved "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz"