diff --git a/.eslintrc.json b/.eslintrc.json index 893b410..7fc714a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -50,7 +50,8 @@ "error", "always-multiline" ], - "no-unused-vars": [ + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": [ "warn", { "varsIgnorePattern": "^_", diff --git a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx new file mode 100644 index 0000000..5c72800 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx @@ -0,0 +1,65 @@ +import { useEffect, useMemo, useState } from "react"; +import { animated, useSpringRef, useTransition } from "@react-spring/web"; + +import { findPrefixCount } from "../../../../utils/strings"; +import { SlotText, SlotWindow } from "./styles"; + +type SlotSelectorProps = { + items: Array, + onChange: (value: string) => void, + removePrefix?: boolean, + formatter?: (value: string) => string, + initialIndex?: number, +}; +export function SlotSelector({ + items, + onChange, + removePrefix = false, + formatter = (x) => x, + initialIndex = 0, +}: SlotSelectorProps) { + const [index, setIndex] = useState(initialIndex); + const [prefixMask, setPrefixMask] = useState(0); + const [formattedItems, setFormattedItems] = useState>([]); + + // useMemo makes these functions only run once when mounting. Not ever re-render. + useMemo(() => { + const tempFormattedItems = items.map(item => formatter(item)); + setFormattedItems(tempFormattedItems); + setPrefixMask(findPrefixCount(tempFormattedItems)); + }, [items]); + + const onClick = () => { + const nextIndex = (index + 1) % items.length; + setIndex(nextIndex); + onChange(items[nextIndex]); + }; + + const getItem = (i: number) => + formattedItems.length && + removePrefix && + formattedItems[0].length !== prefixMask ? + formattedItems[i].slice(prefixMask) : + formattedItems[i]; + + const transRef = useSpringRef(); + const transitions = useTransition(index, { + ref: transRef, + keys: null, + from: { opacity: 0, transform: "translate3d(0, 100%, 0" }, + enter: { opacity: 1, transform: "translate3d(0, 0%, 0" }, + leave: { opacity: 0, transform: "translate3d(0, -100%, 0" }, + }); + + useEffect(() => { + transRef.start(); + }, [index]); + + return ( + + {transitions((style, i) => ( + {getItem(i)} + ))} + + ); +} diff --git a/src/components/Common/Inputs/SlotSelector/index.ts b/src/components/Common/Inputs/SlotSelector/index.ts new file mode 100644 index 0000000..31ed247 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/index.ts @@ -0,0 +1 @@ +export * from "./SlotSelector"; diff --git a/src/components/Common/Inputs/SlotSelector/styles.ts b/src/components/Common/Inputs/SlotSelector/styles.ts new file mode 100644 index 0000000..8af82f4 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/styles.ts @@ -0,0 +1,27 @@ +import styled from "styled-components"; + +export const SlotWindow = styled.div` + position: relative; + height: 1.7em; + width: auto; + + border-radius: 0.6em; + background-color: white; + box-shadow: inset 0 0 50px -50px ${p => p.theme.colors.darkGrey}; + border: 1px solid ${p => p.theme.colors.darkGrey}; + + cursor: pointer; + + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +`; + +export const SlotText = styled.span` + position: absolute; + display: block; + margin-left: .4em; + font-family: ${p => p.theme.fonts.f1Regular}; + color: ${p => p.theme.colors.darkGrey}; + user-select: none; +`; diff --git a/src/components/Common/Inputs/Toggle/Toggle.tsx b/src/components/Common/Inputs/Toggle/Toggle.tsx index 43b7876..3d496ba 100644 --- a/src/components/Common/Inputs/Toggle/Toggle.tsx +++ b/src/components/Common/Inputs/Toggle/Toggle.tsx @@ -12,7 +12,7 @@ import { export type ToggleProps = { label: string, toggled: boolean, - onToggle: (value: boolean) => void, // eslint-disable-line no-unused-vars + onToggle: (value: boolean) => void, showIndicators?: boolean, }; /** diff --git a/src/components/Common/MenuItem.styled.ts b/src/components/Common/MenuItem.styled.ts index eb73e53..cc7bad7 100644 --- a/src/components/Common/MenuItem.styled.ts +++ b/src/components/Common/MenuItem.styled.ts @@ -6,7 +6,7 @@ const hoverRules = css` color: white; `; -export type MenuItemProps = { +type MenuItemProps = { isHovered?: boolean, isActive?: boolean, }; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index 72df498..d4babc1 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -1,6 +1,7 @@ -import { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; +import { Inline } from "@bedrock-layout/inline"; import { PadBox } from "@bedrock-layout/padbox"; +import { Stack } from "@bedrock-layout/stack"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { @@ -10,31 +11,18 @@ import { } from "../../../features/overlayTool/overlayToolSlice"; import { Toggle } from "../../Common/Inputs/Toggle"; +import { MockupBlock } from "../../Common/MockupBlock.styled"; +import { SlotSelector } from "../../Common/Inputs/SlotSelector"; export function OverlayControl() { const dispatch = useDispatch(); const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector); - - - - /* For prototyping use only */ - const [idNum, setIdNum] = useState(0); - - const nextIdNum = () => { - const nextIdNum = (idNum + 1) % overlayIds.length; - setIdNum(nextIdNum); - dispatch(setCurrentOverlayId(overlayIds[nextIdNum])); - }; - /* For prototyping use only */ - - - const onToggle = (value: boolean) => { if (currentOverlayItem == null) { dispatch(initNewOverlayItem({ - overlayId: overlayIds[idNum], + overlayId: overlayIds[0], initialPosition: { x: 0, y: 0 }, setVisible: true, })); @@ -44,22 +32,27 @@ export function OverlayControl() { }; return ( - - - - {/* For prototyping use only */} -
- - Id #{idNum}: "{overlayIds[idNum]}" - - {/* For prototyping use only */} - -
+ + + + + dispatch(setCurrentOverlayId(value))} + removePrefix + formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")} + /> + + + + + Opacity Slider + + + ); } diff --git a/src/components/Workspaces/Workspace/Workspace.tsx b/src/components/Workspaces/Workspace/Workspace.tsx index 7a79ae0..2fac558 100644 --- a/src/components/Workspaces/Workspace/Workspace.tsx +++ b/src/components/Workspaces/Workspace/Workspace.tsx @@ -5,7 +5,7 @@ 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 { updateWorkspace } from "../../../features/workspace/workspaceSlice"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { newWorkspace } from "../../../features/overlayTool/overlayToolSlice"; @@ -14,31 +14,33 @@ import { WorkspaceControls } from "./WorkspaceControls"; import { MockupBlock } from "../../Common/MockupBlock.styled"; import { OverlayDisplay } from "../../OverlayTool"; -import { workspaceObject } from "../workspaces"; +import { workspaces } from "../workspaces"; export const Workspace = () => { const dispatch = useDispatch(); const { workspaceId } = useParams(); - const { animatedBG } = useSelector(workspaceSelector); + const { animatedBG, workspaceProperties } = useSelector(workspaceSelector); const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector); useEffect(() => { if (workspaceId == null) return; // Update workspace store for ID. - dispatch(setWorkspaceId(workspaceId)); + dispatch(updateWorkspace({ + workspaceId, + workspaceProperties: workspaces[workspaceId], + })); // Switching Workspace means we need to init a newWorkspace for Overlay tool. if (overlayWorkspaceId !== workspaceId) { dispatch(newWorkspace({ - overlayIds: workspaceObject[workspaceId].overlayIds, + overlayIds: workspaces[workspaceId].overlayIds, workspaceId: workspaceId, })); } }, [workspaceId]); if (workspaceId == null) return null; - const workspace = workspaceObject[workspaceId]; return ( { maxWidth="80%" centerChildren > -

{workspaceId ? workspace.name : "Unknown"}

+

{workspaceId ? workspaceProperties.name : "Unknown"}

{ width="100px" >Preview Item diff --git a/src/components/Workspaces/Workspace/WorkspaceControls.tsx b/src/components/Workspaces/Workspace/WorkspaceControls.tsx index c0b53f3..70069a9 100644 --- a/src/components/Workspaces/Workspace/WorkspaceControls.tsx +++ b/src/components/Workspaces/Workspace/WorkspaceControls.tsx @@ -1,4 +1,5 @@ import { Grid } from "@bedrock-layout/grid"; +import { PadBox } from "@bedrock-layout/padbox"; import { useDispatch, useSelector } from "react-redux"; import { workspaceSelector } from "../../../features/workspace/workspaceSelector"; @@ -12,14 +13,18 @@ export const WorkspaceControls = () => { const { animatedBG } = useSelector(workspaceSelector); return( - - { - dispatch(setAnimatedBG(value)); - }} /> - + + + { + dispatch(setAnimatedBG(value)); + }} /> + + + + ); }; diff --git a/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx b/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx index bc5c27d..8f3e63c 100644 --- a/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx +++ b/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx @@ -5,13 +5,13 @@ import { WorkspaceListBox } from "./styles"; import { Stack } from "@bedrock-layout/stack"; import { MenuItem } from "../../Common/MenuItem.styled"; -import { workspaceObject } from "../workspaces"; +import { workspaces } from "../workspaces"; type WorkspaceListProps = { headerOffset?: number, }; export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => { - const workspaceList = _.keys(workspaceObject); + const workspaceList = _.keys(workspaces); return ( @@ -19,7 +19,7 @@ export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => { ))} diff --git a/src/components/Workspaces/workspaces.ts b/src/components/Workspaces/workspaces.ts index db93ab1..d4c8fde 100644 --- a/src/components/Workspaces/workspaces.ts +++ b/src/components/Workspaces/workspaces.ts @@ -1,13 +1,9 @@ -import { Dimensions, OverlayIds } from "../../types/state"; +import { WorkspaceProperties } from "../../types/state"; -export type WorkspaceObject = { - [id: string]: { - name: string, - overlayIds: OverlayIds, - previewWindowDimensions: Dimensions, - }, +type WorkspaceObject = { + [id: string]: WorkspaceProperties, }; -export const workspaceObject: WorkspaceObject = { +export const workspaces: WorkspaceObject = { chyronSimple: { name: "Chyron Simple", overlayIds: [ diff --git a/src/features/overlayTool/overlayToolSlice.ts b/src/features/overlayTool/overlayToolSlice.ts index 966dafc..e66140d 100644 --- a/src/features/overlayTool/overlayToolSlice.ts +++ b/src/features/overlayTool/overlayToolSlice.ts @@ -65,6 +65,10 @@ export const overlayToolSlice = createSlice({ state.currentWorkspaceId = action.payload.workspaceId; state.overlayIds = action.payload.overlayIds; }, + setOpacity: (state, action: PayloadAction) => { + if (state.currentOverlayItem == null) return; + state.currentOverlayItem.opacity = action.payload; + }, }, }); @@ -74,5 +78,6 @@ export const { setCurrentOverlayId, setPosition, newWorkspace, + setOpacity, } = overlayToolSlice.actions; export default overlayToolSlice.reducer; diff --git a/src/features/workspace/workspaceSlice.ts b/src/features/workspace/workspaceSlice.ts index 4abc2e0..9913f80 100644 --- a/src/features/workspace/workspaceSlice.ts +++ b/src/features/workspace/workspaceSlice.ts @@ -1,9 +1,19 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; -import { RootState, WorkspaceId } from "../../types/state"; +import { RootState, WorkspaceId, WorkspaceProperties } from "../../types/state"; const initialState: RootState["workspace"] = { workspaceId: "", animatedBG: true, + workspaceProperties: { + name: "undefined", + overlayIds: [], + previewWindowDimensions: { width: 0, height: 0 }, + }, +}; + +export type UpdateWorkspaceAction = { + workspaceId: WorkspaceId, + workspaceProperties: WorkspaceProperties, }; export const workspaceSlice = createSlice({ @@ -16,8 +26,16 @@ export const workspaceSlice = createSlice({ setAnimatedBG: (state, action: PayloadAction) => { state.animatedBG = action.payload; }, + updateWorkspace: (state, action: PayloadAction) => { + state.workspaceId = action.payload.workspaceId; + state.workspaceProperties = action.payload.workspaceProperties; + }, }, }); -export const { setWorkspaceId, setAnimatedBG } = workspaceSlice.actions; +export const { + setWorkspaceId, + setAnimatedBG, + updateWorkspace, +} = workspaceSlice.actions; export default workspaceSlice.reducer; diff --git a/src/types/state.d.ts b/src/types/state.d.ts index 0d7d47f..de6fb3f 100644 --- a/src/types/state.d.ts +++ b/src/types/state.d.ts @@ -16,9 +16,15 @@ export type Dimensions = { width: number, height: number }; export type Workspace = { workspaceId: WorkspaceId, animatedBG: boolean, + workspaceProperties: WorkspaceProperties, }; export type WorkspaceId = string; +export type WorkspaceProperties = { + name: string, + overlayIds: OverlayIds, + previewWindowDimensions: Dimensions, +}; // Overlay Tool export type OverlayTool = { diff --git a/src/utils/strings.spec.ts b/src/utils/strings.spec.ts new file mode 100644 index 0000000..d7c4935 --- /dev/null +++ b/src/utils/strings.spec.ts @@ -0,0 +1,53 @@ +import { findPrefixCount } from "./strings"; + +describe("strings", () => { + describe("findPrefixCount", () => { + describe("finds matches", () => { + it("gets correct count all the same", () => { + expect(findPrefixCount([ + "hello", + "hello", + "hello", + "hello", + ], true)).toBe(5); + }); + it("gets correct count for hello prefix", () => { + expect(findPrefixCount([ + "hello world", + "hello mother", + "hello father", + "hello!", + ], true)).toBe(5); + }); + it("gets zero when nothing matches", () => { + expect(findPrefixCount([ + "cat", + "dog", + "rat", + ], true)).toBe(0); + }); + it("returns 0 when only given a single string", () => { + expect(findPrefixCount([ + "by myself", + ], true)).toBe(0); + }); + it("returns 0 when given no strings", () => { + expect(findPrefixCount([], true)).toBe(0); + }); + it("is case sensitive", () => { + expect(findPrefixCount([ + "Hello", + "HELLO", + "Hello", + ], true)).toBe(1); + }); + it("is correctly case-insensitive", () => { + expect(findPrefixCount([ + "Hello", + "HELLO", + "Hello", + ], false)).toBe(5); + }); + }); + }); +}); diff --git a/src/utils/strings.ts b/src/utils/strings.ts new file mode 100644 index 0000000..19652ba --- /dev/null +++ b/src/utils/strings.ts @@ -0,0 +1,25 @@ +/** + * Gives you the number of characters that prefix all the given strings. + * I use this to remove redundant repeated characters shared between file names. + * Basically, this is a method to do the " you find in a list. + * @param strings + * @returns number of characters that all strings share from the start + */ +export function findPrefixCount(strings: Array, caseSensitive = true): number { + if (strings.length <= 1) { + return 0; // Cannot find anything. So, most neutral return is 0. + } + + let maskLength = strings[0].length; + for (let x = 1; x < strings.length; ++x) { + for (let c = 0; c < maskLength; c++) { + const left = caseSensitive ? strings[0][c] : strings[0][c].toLowerCase(); + const right = caseSensitive ? strings[x][c] : strings[x][c].toLowerCase(); + if (left !== right) { + maskLength = c; + break; + } + } + } + return maskLength; +}