diff --git a/app/web/src/components/EditorMenu/AllShortcutsModal.tsx b/app/web/src/components/EditorMenu/AllShortcutsModal.tsx new file mode 100644 index 0000000..c37d3f1 --- /dev/null +++ b/app/web/src/components/EditorMenu/AllShortcutsModal.tsx @@ -0,0 +1,42 @@ +import { useEffect, useState } from 'react' +import { useHotkeys } from 'react-hotkeys-hook'; +import { makeStyles } from '@material-ui/core/styles' +import Dialog from '@material-ui/core/Dialog' +import { editorMenuConfig } from './menuConfig'; + +const SHORTCUT = 'ctrl+/, command+/' + +const useStyles = makeStyles({ + root: { + transform: `translate3d(0,0,50px)`, + }, +}) + +const AllShortcutsModal = () => { + const classes = useStyles() + const [open, setOpen] = useState(false) + useHotkeys(SHORTCUT, () => setOpen(!open), [open]) + + return ( + setOpen(false)} className={classes.root}> +
+

All Shortcuts

+ { editorMenuConfig.map(menu => menu.items.length ? ( +
+

{ menu.label }

+ { menu.items.map(item => ( +
+

{ item.label }

+ { item.shortcutLabel } +
+ ))} +
+ ) : <>)} +
+
+ ) + } + + + export default AllShortcutsModal diff --git a/app/web/src/components/EditorMenu/EditorMenu.tsx b/app/web/src/components/EditorMenu/EditorMenu.tsx index eefc57d..ac3e9d2 100644 --- a/app/web/src/components/EditorMenu/EditorMenu.tsx +++ b/app/web/src/components/EditorMenu/EditorMenu.tsx @@ -7,15 +7,12 @@ import { makeStlDownloadHandler, PullTitleFromFirstLine } from './helpers' import { useSaveCode } from 'src/components/IdeWrapper/useSaveCode' import CadPackage from 'src/components/CadPackage/CadPackage' import { EditorMenuConfig, EditorMenuItemConfig, editorMenuConfig } from './menuConfig' +import AllShortcutsModal from './AllShortcutsModal' const EditorMenu = () => { const handleRender = useRender() const saveCode = useSaveCode() const { state, thunkDispatch } = useIdeContext() - const onRender = () => { - handleRender() - saveCode({ code: state.code }) - } const handleStlDownload = makeStlDownloadHandler({ type: state.objectData?.type, ideType: state.ideType, @@ -30,7 +27,7 @@ const EditorMenu = () => { useHotkeys(shortcut, callback), [state]) ) - return ( + return (<>
@@ -38,8 +35,10 @@ const EditorMenu = () => {
{ editorMenuConfig.map(menu => ( - - { menu.items.map(itemConfig => ) } + + { menu.items.map(itemConfig => ( + ) + ) } )) }
@@ -53,69 +52,13 @@ const EditorMenu = () => {
- ) + + ) } export default EditorMenu -function FileDropdown({ handleRender, handleStlDownload }) { - return ( - - - {({ active }) => ( - - )} - - - {({ active }) => ( - - )} - - - ) -} - -function ViewDropdown({ handleLayoutReset }) { - return ( - - - {({ active }) => ( - - )} - - - ) -} - -function DropdownItem(config) { +function DropdownItem({ config }) { return ( {({ active }) => ( @@ -143,7 +86,7 @@ function Dropdown({ return (
- {label} + {label} { children && {children} diff --git a/app/web/src/components/EditorMenu/menuConfig.tsx b/app/web/src/components/EditorMenu/menuConfig.tsx index 296d1f2..433b1d5 100644 --- a/app/web/src/components/EditorMenu/menuConfig.tsx +++ b/app/web/src/components/EditorMenu/menuConfig.tsx @@ -1,47 +1,75 @@ import React from 'react' +import { useIdeContext } from 'src/helpers/hooks/useIdeContext' +import { useRender } from 'src/components/IdeWrapper/useRender' +import { makeStlDownloadHandler, PullTitleFromFirstLine } from './helpers' +import { useSaveCode } from 'src/components/IdeWrapper/useSaveCode' import Svg from 'src/components/Svg/Svg' -export const fileMenuConfig = [ - { - label: 'Cook Donut', - shortcut: 'ctrl+d, command+d', - shortcutLabel: <> D, - callback: () => alert('Donut is cooked!'), - } -] +const fileMenuConfig = { + name: 'file', + label: 'File', + disabled: false, + items: [ + { + label: 'Save & Render', + shortcut: 'ctrl+s, command+s', + shortcutLabel: <> S, + // This is my main issue. How do I pass in a callback that relies on the hooks and state within the component? + // Put another way, how do I make the state and hooks used within a component configurable from outside the component itself? + callback: (e, /* { options } */) => { + // These do not work + // const handleRender = useRender() + // const saveCode = useSaveCode() + // handleRender() + // saveCode({ code: state.code }) + e.preventDefault() + alert('Saving & Rendering!') + }, + }, + { + label: 'Download STL', + shortcut: 'ctrl+shift+d, command+shift+d', + shortcutLabel: <> Shift D, + callback: () => alert('Downloading STL!'), + }, + { + label: 'Cook Donut', + shortcut: 'ctrl+d, command+d', + shortcutLabel: <> D, + callback: () => alert('Donut is cooked!'), + } + ] +} -export const viewMenuConfig = [ - { - label: 'Reset layout', - shortcut: 'ctrl+alt+r, command+alt+r', - shortcutLabel: <> Alt R, - callback: () => alert('Resetting the layout!'), - } -] +const editMenuConfig = { + name: 'edit', + label: 'Edit', + disabled: true, + items: [], +} + +const viewMenuConfig = { + name: 'view', + label: 'View', + disabled: false, + items: [ + { + label: 'Reset layout', + shortcut: 'ctrl+alt+r, command+alt+r', + shortcutLabel: <> Alt R, + callback: () => alert('Resetting the layout!'), + }, + ], +} export const editorMenuConfig = [ - { - name: 'file', - label: 'File', - disabled: false, - items: fileMenuConfig, - }, - { - name: 'edit', - label: 'Edit', - disabled: true, - items: [], - }, - { - name: 'view', - label: 'View', - disabled: false, - items: viewMenuConfig, - }, - + fileMenuConfig, + editMenuConfig, + viewMenuConfig, ] +// TODO: set up these types properly. The callback is especially giving me trouble. export interface EditorMenuItemConfig { label: string, shortcut: string, @@ -57,12 +85,5 @@ export interface EditorMenuConfig { } function CmdOrCtrl() { - return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform) ? ( - - ) : ( - 'Ctrl' - ) + return { /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform) ? '⌘' : 'Ctrl' } } \ No newline at end of file