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

499 wiersze
7.8 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 { useFileSystemHandlers, useTLDrawContext } from '~hooks'
export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
const { tlstate } = useTLDrawContext()
2021-08-10 16:12:55 +00:00
const canHandleEvent = React.useCallback(() => {
const elm = ref.current
return elm && (document.activeElement === elm || elm.contains(document.activeElement))
}, [ref])
2021-08-10 16:12:55 +00:00
/* ---------------------- Tools --------------------- */
2021-09-08 10:16:10 +00:00
useHotkeys(
'v,1',
() => {
if (canHandleEvent()) tlstate.selectTool('select')
2021-09-08 10:16:10 +00:00
},
[tlstate, ref.current]
2021-09-08 10:16:10 +00:00
)
useHotkeys(
'd,2',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Draw)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'r,3',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Rectangle)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'e,4',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Ellipse)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'a,5',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Arrow)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
't,6',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Text)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
useHotkeys(
'n,7',
() => {
if (canHandleEvent()) tlstate.selectTool(TLDrawShapeType.Sticky)
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
/* ---------------------- Misc ---------------------- */
// Dark Mode
useHotkeys(
'ctrl+shift+d,command+shift+d',
(e) => {
if (canHandleEvent()) {
tlstate.toggleDarkMode()
e.preventDefault()
}
},
undefined,
[tlstate]
)
// Focus Mode
useHotkeys(
'ctrl+.,command+.',
() => {
if (canHandleEvent()) tlstate.toggleFocusMode()
},
undefined,
[tlstate]
)
// File System
const { onNewProject, onOpenProject, onSaveProject, onSaveProjectAs } = useFileSystemHandlers()
useHotkeys(
'ctrl+n,command+n',
(e) => {
if (canHandleEvent()) {
onNewProject(e)
}
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+s,command+s',
(e) => {
if (canHandleEvent()) {
onSaveProject(e)
}
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
2021-09-08 10:16:10 +00:00
useHotkeys(
'ctrl+shift+s,command+shift+s',
(e) => {
if (canHandleEvent()) {
onSaveProjectAs(e)
}
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+o,command+o',
(e) => {
if (canHandleEvent()) {
onOpenProject(e)
}
2021-09-08 10:16:10 +00:00
},
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',
() => {
if (canHandleEvent()) {
if (tlstate.session) {
tlstate.cancelSession()
} else {
tlstate.undo()
}
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+shift-z,command+shift+z',
() => {
if (canHandleEvent()) {
if (tlstate.session) {
tlstate.cancelSession()
} else {
tlstate.redo()
}
}
2021-09-08 10:16:10 +00:00
},
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',
() => {
if (canHandleEvent()) tlstate.undoSelect()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+shift-u,command+shift+u',
() => {
if (canHandleEvent()) tlstate.redoSelect()
2021-09-08 10:16:10 +00:00
},
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) => {
if (canHandleEvent()) {
tlstate.zoomIn()
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'ctrl+-,command+-',
(e) => {
if (canHandleEvent()) {
tlstate.zoomOut()
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+1',
() => {
if (canHandleEvent()) tlstate.zoomToFit()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+2',
() => {
if (canHandleEvent()) tlstate.zoomToSelection()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+0',
() => {
if (canHandleEvent()) tlstate.zoomToActual()
2021-09-08 10:16:10 +00:00
},
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) => {
if (canHandleEvent()) {
tlstate.duplicate()
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Flip
2021-09-08 10:16:10 +00:00
useHotkeys(
'shift+h',
() => {
if (canHandleEvent()) tlstate.flipHorizontal()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+v',
() => {
if (canHandleEvent()) tlstate.flipVertical()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Cancel
2021-09-08 10:16:10 +00:00
useHotkeys(
'escape',
() => {
if (canHandleEvent()) {
tlstate.cancel()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Delete
2021-09-08 10:16:10 +00:00
useHotkeys(
'backspace',
() => {
if (canHandleEvent()) tlstate.delete()
2021-09-08 10:16:10 +00:00
},
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',
() => {
if (canHandleEvent()) tlstate.selectAll()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Nudge
2021-09-08 10:16:10 +00:00
useHotkeys(
'up',
() => {
if (canHandleEvent()) tlstate.nudge([0, -1], false)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'right',
() => {
if (canHandleEvent()) tlstate.nudge([1, 0], false)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'down',
() => {
if (canHandleEvent()) tlstate.nudge([0, 1], false)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'left',
() => {
if (canHandleEvent()) tlstate.nudge([-1, 0], false)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+up',
() => {
if (canHandleEvent()) tlstate.nudge([0, -1], true)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+right',
() => {
if (canHandleEvent()) tlstate.nudge([1, 0], true)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+down',
() => {
if (canHandleEvent()) tlstate.nudge([0, 1], true)
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+left',
() => {
if (canHandleEvent()) tlstate.nudge([-1, 0], true)
2021-09-08 10:16:10 +00:00
},
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',
() => {
if (canHandleEvent()) tlstate.copy()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'command+v,ctrl+v',
() => {
if (canHandleEvent()) tlstate.paste()
2021-09-08 10:16:10 +00:00
},
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) => {
if (canHandleEvent()) {
tlstate.group()
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'command+shift+g,ctrl+shift+g',
(e) => {
if (canHandleEvent()) {
tlstate.ungroup()
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
// Move
2021-09-08 10:16:10 +00:00
useHotkeys(
'[',
() => {
if (canHandleEvent()) tlstate.moveBackward()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
']',
() => {
if (canHandleEvent()) tlstate.moveForward()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+[',
() => {
if (canHandleEvent()) tlstate.moveToBack()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'shift+]',
() => {
if (canHandleEvent()) tlstate.moveToFront()
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
useHotkeys(
'command+shift+backspace',
(e) => {
if (canHandleEvent()) {
if (process.env.NODE_ENV === 'development') {
tlstate.resetDocument()
}
e.preventDefault()
}
2021-09-08 10:16:10 +00:00
},
undefined,
[tlstate]
)
2021-08-10 16:12:55 +00:00
}