Fix/undo on menu open (#436)

* fix for undo button on menu open

* Change toggle to set, add option for keyboard events

* Update .gitignore

* Update .gitignore

* Remove isStyleOpen

* Remove isStyleOpen

Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
This commit is contained in:
megnakokkalera 2021-12-06 13:23:53 -05:00 committed by GitHub
parent 0db19e3043
commit c67c0871ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 878 additions and 826 deletions

2
.gitignore vendored
View file

@ -15,4 +15,4 @@ apps/www/public/workbox-*
apps/www/public/worker-* apps/www/public/worker-*
apps/www/public/sw.js apps/www/public/sw.js
apps/www/public/sw.js.map apps/www/public/sw.js.map
.env .env

View file

@ -65,11 +65,11 @@
"lerna": "^3.22.1", "lerna": "^3.22.1",
"lint-staged": "^11.2.6", "lint-staged": "^11.2.6",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"pretty-quick": "^3.1.2",
"resize-observer-polyfill": "^1.5.1", "resize-observer-polyfill": "^1.5.1",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"typedoc": "^0.22.3", "typedoc": "^0.22.3",
"typescript": "^4.5.2", "typescript": "^4.5.2"
"pretty-quick": "^3.1.2"
}, },
"husky": { "husky": {
"hooks": { "hooks": {
@ -77,4 +77,4 @@
"pre-push": "fix:style && eslint && test" "pre-push": "fix:style && eslint && test"
} }
} }
} }

View file

@ -31,7 +31,6 @@ import {
} from '@radix-ui/react-icons' } from '@radix-ui/react-icons'
import { DMContent } from '~components/Primitives/DropdownMenu' import { DMContent } from '~components/Primitives/DropdownMenu'
import { Divider } from '~components/Primitives/Divider' import { Divider } from '~components/Primitives/Divider'
import { TrashIcon } from '~components/Primitives/icons'
import { ToolButton } from '~components/Primitives/ToolButton' import { ToolButton } from '~components/Primitives/ToolButton'
const selectedShapesCountSelector = (s: TDSnapshot) => const selectedShapesCountSelector = (s: TDSnapshot) =>
@ -172,8 +171,15 @@ export function ActionButton(): JSX.Element {
app.distribute(DistributeType.Horizontal) app.distribute(DistributeType.Horizontal)
}, [app]) }, [app])
const handleMenuOpenChange = React.useCallback(
(open: boolean) => {
app.setMenuOpen(open)
},
[app]
)
return ( return (
<DropdownMenu.Root dir="ltr"> <DropdownMenu.Root dir="ltr" onOpenChange={handleMenuOpenChange}>
<DropdownMenu.Trigger dir="ltr" asChild> <DropdownMenu.Trigger dir="ltr" asChild>
<ToolButton variant="circle"> <ToolButton variant="circle">
<DotsHorizontalIcon /> <DotsHorizontalIcon />

View file

@ -147,8 +147,15 @@ export const StyleMenu = React.memo(function ColorMenu(): JSX.Element {
app.style({ textAlign: value as AlignStyle }) app.style({ textAlign: value as AlignStyle })
}, []) }, [])
const handleMenuOpenChange = React.useCallback(
(open: boolean) => {
app.setMenuOpen(open)
},
[app]
)
return ( return (
<DropdownMenu.Root dir="ltr"> <DropdownMenu.Root dir="ltr" onOpenChange={handleMenuOpenChange}>
<DropdownMenu.Trigger asChild> <DropdownMenu.Trigger asChild>
<ToolButton variant="text"> <ToolButton variant="text">
Styles Styles

View file

@ -6,17 +6,21 @@ import { useFileSystemHandlers, useTldrawApp } from '~hooks'
export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) { export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
const app = useTldrawApp() const app = useTldrawApp()
const canHandleEvent = React.useCallback(() => { const canHandleEvent = React.useCallback(
const elm = ref.current (ignoreMenus = false) => {
return elm && (document.activeElement === elm || elm.contains(document.activeElement)) const elm = ref.current
}, [ref]) if (ignoreMenus && app.isMenuOpen()) return true
return elm && (document.activeElement === elm || elm.contains(document.activeElement))
},
[ref]
)
/* ---------------------- Tools --------------------- */ /* ---------------------- Tools --------------------- */
useHotkeys( useHotkeys(
'v,1', 'v,1',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool('select') app.selectTool('select')
}, },
[app, ref.current] [app, ref.current]
@ -25,7 +29,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'd,p,2', 'd,p,2',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Draw) app.selectTool(TDShapeType.Draw)
}, },
undefined, undefined,
@ -35,7 +39,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'e,3', 'e,3',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool('erase') app.selectTool('erase')
}, },
undefined, undefined,
@ -45,7 +49,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'r,4', 'r,4',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Rectangle) app.selectTool(TDShapeType.Rectangle)
}, },
undefined, undefined,
@ -55,7 +59,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'o,5', 'o,5',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Ellipse) app.selectTool(TDShapeType.Ellipse)
}, },
undefined, undefined,
@ -65,7 +69,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'l,6', 'l,6',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Line) app.selectTool(TDShapeType.Line)
}, },
undefined, undefined,
@ -75,7 +79,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'a,7', 'a,7',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Arrow) app.selectTool(TDShapeType.Arrow)
}, },
undefined, undefined,
@ -85,7 +89,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
't,8', 't,8',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Text) app.selectTool(TDShapeType.Text)
}, },
undefined, undefined,
@ -95,7 +99,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
's,9', 's,9',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectTool(TDShapeType.Sticky) app.selectTool(TDShapeType.Sticky)
}, },
undefined, undefined,
@ -109,7 +113,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+shift+d,⌘+shift+d', 'ctrl+shift+d,⌘+shift+d',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.toggleDarkMode() app.toggleDarkMode()
e.preventDefault() e.preventDefault()
}, },
@ -122,7 +126,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+.,⌘+.', 'ctrl+.,⌘+.',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.toggleFocusMode() app.toggleFocusMode()
}, },
undefined, undefined,
@ -132,7 +136,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+shift+g,⌘+shift+g', 'ctrl+shift+g,⌘+shift+g',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.toggleGrid() app.toggleGrid()
}, },
undefined, undefined,
@ -190,7 +194,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'⌘+z,ctrl+z', '⌘+z,ctrl+z',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
if (app.session) { if (app.session) {
app.cancelSession() app.cancelSession()
@ -205,7 +209,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+shift-z,⌘+shift+z', 'ctrl+shift-z,⌘+shift+z',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
if (app.session) { if (app.session) {
app.cancelSession() app.cancelSession()
@ -246,7 +250,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+=,⌘+=,ctrl+num_subtract,⌘+num_subtract', 'ctrl+=,⌘+=,ctrl+num_subtract,⌘+num_subtract',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.zoomIn() app.zoomIn()
e.preventDefault() e.preventDefault()
}, },
@ -257,7 +261,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'ctrl+-,⌘+-,ctrl+num_add,⌘+num_add', 'ctrl+-,⌘+-,ctrl+num_add,⌘+num_add',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.zoomOut() app.zoomOut()
e.preventDefault() e.preventDefault()
@ -269,7 +273,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+0,ctrl+numpad_0,⌘+numpad_0', 'shift+0,ctrl+numpad_0,⌘+numpad_0',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.resetZoom() app.resetZoom()
}, },
undefined, undefined,
@ -279,7 +283,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+1', 'shift+1',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.zoomToFit() app.zoomToFit()
}, },
undefined, undefined,
@ -289,7 +293,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+2', 'shift+2',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.zoomToSelection() app.zoomToSelection()
}, },
undefined, undefined,
@ -315,7 +319,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+h', 'shift+h',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.flipHorizontal() app.flipHorizontal()
}, },
undefined, undefined,
@ -325,7 +329,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+v', 'shift+v',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.flipVertical() app.flipVertical()
}, },
undefined, undefined,
@ -337,7 +341,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'escape', 'escape',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.cancel() app.cancel()
}, },
@ -362,7 +366,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'⌘+a,ctrl+a', '⌘+a,ctrl+a',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.selectAll() app.selectAll()
}, },
undefined, undefined,
@ -524,7 +528,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'[', '[',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.moveBackward() app.moveBackward()
}, },
undefined, undefined,
@ -534,7 +538,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
']', ']',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.moveForward() app.moveForward()
}, },
undefined, undefined,
@ -544,7 +548,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+[', 'shift+[',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.moveToBack() app.moveToBack()
}, },
undefined, undefined,
@ -554,7 +558,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'shift+]', 'shift+]',
() => { () => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.moveToFront() app.moveToFront()
}, },
undefined, undefined,
@ -579,7 +583,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'alt+command+l,alt+ctrl+l', 'alt+command+l,alt+ctrl+l',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.style({ textAlign: AlignStyle.Start }) app.style({ textAlign: AlignStyle.Start })
e.preventDefault() e.preventDefault()
}, },
@ -590,7 +594,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'alt+command+t,alt+ctrl+t', 'alt+command+t,alt+ctrl+t',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.style({ textAlign: AlignStyle.Middle }) app.style({ textAlign: AlignStyle.Middle })
e.preventDefault() e.preventDefault()
}, },
@ -601,7 +605,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
useHotkeys( useHotkeys(
'alt+command+r,alt+ctrl+r', 'alt+command+r,alt+ctrl+r',
(e) => { (e) => {
if (!canHandleEvent()) return if (!canHandleEvent(true)) return
app.style({ textAlign: AlignStyle.End }) app.style({ textAlign: AlignStyle.End })
e.preventDefault() e.preventDefault()
}, },

View file

@ -962,18 +962,16 @@ export class TldrawApp extends StateManager<TDSnapshot> {
} }
/** /**
* Toggle the style panel. * Toggles the state if menu is opened
*/ */
toggleStylePanel = (): this => { setMenuOpen = (isOpen: boolean): this => {
if (this.session) return this this.patchState({ appState: { isMenuOpen: isOpen } }, 'ui:toggled_menu_opened')
this.patchState(
{ appState: { isStyleOpen: !this.appState.isStyleOpen } },
'ui:toggled_style_panel'
)
this.persist() this.persist()
return this return this
} }
isMenuOpen = (): boolean => this.appState.isMenuOpen
/** /**
* Toggle grids. * Toggle grids.
*/ */
@ -3224,7 +3222,7 @@ export class TldrawApp extends StateManager<TDSnapshot> {
currentPageId: 'page', currentPageId: 'page',
currentStyle: defaultStyle, currentStyle: defaultStyle,
isToolLocked: false, isToolLocked: false,
isStyleOpen: false, isMenuOpen: false,
isEmptyCanvas: false, isEmptyCanvas: false,
snapLines: [], snapLines: [],
}, },

View file

@ -341,9 +341,6 @@ export class SelectTool extends BaseTool<Status> {
} }
onPointerDown: TLPointerEventHandler = (info, e) => { onPointerDown: TLPointerEventHandler = (info, e) => {
if (this.app.appState.isStyleOpen) {
this.app.toggleStylePanel()
}
if (e.buttons === 4) { if (e.buttons === 4) {
this.setStatus(Status.MiddleWheelPanning) this.setStatus(Status.MiddleWheelPanning)
} }

View file

@ -98,8 +98,8 @@ export interface TDSnapshot {
hoveredId?: string hoveredId?: string
activeTool: TDToolType activeTool: TDToolType
isToolLocked: boolean isToolLocked: boolean
isStyleOpen: boolean
isEmptyCanvas: boolean isEmptyCanvas: boolean
isMenuOpen: boolean
status: string status: string
snapLines: TLSnapLine[] snapLines: TLSnapLine[]
} }

1586
yarn.lock

File diff suppressed because it is too large Load diff