Tldraw/packages/tldraw/src/hooks/useKeyboardShortcuts.tsx

416 wiersze
5.6 KiB
TypeScript
Czysty Zwykły widok Historia

2021-08-10 16:12:55 +00:00
import * as React from 'react'
import { useHotkeys } from 'react-hotkeys-hook'
2021-08-13 09:28:09 +00:00
import { TLDrawShapeType } from '~types'
import { useTLDrawContext } from '~hooks'
export function useKeyboardShortcuts() {
const { tlstate } = useTLDrawContext()
2021-08-10 16:12:55 +00:00
React.useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
tlstate.onKeyDown(e.key)
2021-08-10 16:12:55 +00:00
}
const handleKeyUp = (e: KeyboardEvent) => {
tlstate.onKeyUp(e.key)
2021-08-10 16:12:55 +00:00
}
window.addEventListener('keydown', handleKeyDown)
window.addEventListener('keyup', handleKeyUp)
2021-08-10 16:12:55 +00:00
return () => {
window.removeEventListener('keydown', handleKeyDown)
window.removeEventListener('keyup', handleKeyUp)
}
}, [tlstate])
/* ---------------------- Tools --------------------- */
2021-09-08 10:16:10 +00:00
useHotkeys(
'v,1',
() => {
tlstate.selectTool('select')
},
undefined,
[tlstate]
)
useHotkeys(
'd,2',
() => {
tlstate.selectTool(TLDrawShapeType.Draw)
},
undefined,
[tlstate]
)
useHotkeys(
'r,3',
() => {
tlstate.selectTool(TLDrawShapeType.Rectangle)
},
undefined,
[tlstate]
)
useHotkeys(
'e,4',
() => {
tlstate.selectTool(TLDrawShapeType.Ellipse)
},
undefined,
[tlstate]
)
useHotkeys(
'a,5',
() => {
tlstate.selectTool(TLDrawShapeType.Arrow)
},
undefined,
[tlstate]
)
useHotkeys(
't,6',
() => {
tlstate.selectTool(TLDrawShapeType.Text)
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
/* ---------------------- Misc ---------------------- */
// Save
2021-09-08 10:16:10 +00:00
useHotkeys(
'ctrl+s,command+s',
() => {
tlstate.saveProject()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Undo Redo
2021-09-08 10:16:10 +00:00
useHotkeys(
'command+z,ctrl+z',
() => {
tlstate.undo()
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+shift-z,command+shift+z',
() => {
tlstate.redo()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
2021-08-15 14:35:23 +00:00
// Undo Redo
2021-09-08 10:16:10 +00:00
useHotkeys(
'command+u,ctrl+u',
() => {
tlstate.undoSelect()
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+shift-u,command+shift+u',
() => {
tlstate.redoSelect()
},
undefined,
[tlstate]
)
2021-08-15 14:35:23 +00:00
2021-08-10 16:12:55 +00:00
/* -------------------- Commands -------------------- */
// Camera
2021-09-08 10:16:10 +00:00
useHotkeys(
'ctrl+=,command+=',
(e) => {
tlstate.zoomIn()
e.preventDefault()
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+-,command+-',
(e) => {
tlstate.zoomOut()
e.preventDefault()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+1',
() => {
tlstate.zoomToFit()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+2',
() => {
tlstate.zoomToSelection()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+0',
() => {
tlstate.zoomToActual()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Duplicate
2021-09-08 10:16:10 +00:00
useHotkeys(
'ctrl+d,command+d',
(e) => {
tlstate.duplicate()
e.preventDefault()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Flip
2021-09-08 10:16:10 +00:00
useHotkeys(
'shift+h',
() => {
tlstate.flipHorizontal()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+v',
() => {
tlstate.flipVertical()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Cancel
2021-09-08 10:16:10 +00:00
useHotkeys(
'escape',
() => {
tlstate.cancel()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Delete
2021-09-08 10:16:10 +00:00
useHotkeys(
'backspace',
() => {
tlstate.delete()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Select All
2021-09-08 10:16:10 +00:00
useHotkeys(
'command+a,ctrl+a',
() => {
tlstate.selectAll()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Nudge
2021-09-08 10:16:10 +00:00
useHotkeys(
'up',
() => {
tlstate.nudge([0, -1], false)
},
undefined,
[tlstate]
)
useHotkeys(
'right',
() => {
tlstate.nudge([1, 0], false)
},
undefined,
[tlstate]
)
useHotkeys(
'down',
() => {
tlstate.nudge([0, 1], false)
},
undefined,
[tlstate]
)
useHotkeys(
'left',
() => {
tlstate.nudge([-1, 0], false)
},
undefined,
[tlstate]
)
useHotkeys(
'shift+up',
() => {
tlstate.nudge([0, -1], true)
},
undefined,
[tlstate]
)
useHotkeys(
'shift+right',
() => {
tlstate.nudge([1, 0], true)
},
undefined,
[tlstate]
)
useHotkeys(
'shift+down',
() => {
tlstate.nudge([0, 1], true)
},
undefined,
[tlstate]
)
useHotkeys(
'shift+left',
() => {
tlstate.nudge([-1, 0], true)
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Copy & Paste
2021-09-08 10:16:10 +00:00
useHotkeys(
'command+c,ctrl+c',
() => {
tlstate.copy()
},
undefined,
[tlstate]
)
useHotkeys(
'command+v,ctrl+v',
() => {
tlstate.paste()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Group & Ungroup
2021-09-08 10:16:10 +00:00
useHotkeys(
'command+g,ctrl+g',
(e) => {
tlstate.group()
e.preventDefault()
},
undefined,
[tlstate]
)
useHotkeys(
'command+shift+g,ctrl+shift+g',
(e) => {
tlstate.ungroup()
e.preventDefault()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Move
2021-09-08 10:16:10 +00:00
useHotkeys(
'[',
() => {
tlstate.moveBackward()
},
undefined,
[tlstate]
)
useHotkeys(
']',
() => {
tlstate.moveForward()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+[',
() => {
tlstate.moveToBack()
},
undefined,
[tlstate]
)
useHotkeys(
'shift+]',
() => {
tlstate.moveToFront()
},
undefined,
[tlstate]
)
useHotkeys(
'command+shift+backspace',
(e) => {
tlstate.resetDocument()
e.preventDefault()
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
}