PrimaryTools.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { TLSelectTool } from '@tldraw/core'
  2. import { useApp } from '@tldraw/react'
  3. import { observer } from 'mobx-react-lite'
  4. import * as React from 'react'
  5. import { Button } from '~components/Button'
  6. import { LogseqIcon, TablerIcon } from '~components/icons'
  7. interface ToolButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  8. id: string
  9. icon: string | React.ReactNode
  10. }
  11. const ToolButton = observer(({ id, icon, title, ...props }: ToolButtonProps) => {
  12. const app = useApp()
  13. const handleToolClick = React.useCallback(
  14. (e: React.MouseEvent<HTMLButtonElement>) => {
  15. const tool = e.currentTarget.dataset.tool
  16. if (tool) app.selectTool(tool)
  17. },
  18. [app]
  19. )
  20. // Tool must exist
  21. const Tool = app.Tools?.find(T => T.id === id) ?? TLSelectTool
  22. const shortcut = ((Tool as any)['shortcut'] as string[])?.[0]
  23. const titleWithShortcut = shortcut ? `${title} (${shortcut})` : title
  24. return (
  25. <Button
  26. {...props}
  27. title={titleWithShortcut}
  28. data-tool={id}
  29. data-selected={id === app.selectedTool.id}
  30. onClick={handleToolClick}
  31. >
  32. {typeof icon === 'string' ? <TablerIcon name={icon} /> : icon}
  33. </Button>
  34. )
  35. })
  36. export const PrimaryTools = observer(function PrimaryTools() {
  37. const app = useApp()
  38. return (
  39. <div className="tl-primary-tools">
  40. <div className="tl-tools-floating-panel" data-tool-locked={app.settings.isToolLocked}>
  41. <ToolButton title="Select" id="select" icon="select-cursor" />
  42. <ToolButton title="Draw" id="pencil" icon="ballpen" />
  43. <ToolButton title="Highlight" id="highlighter" icon="highlight" />
  44. <ToolButton title="Eraser" id="erase" icon="eraser" />
  45. <ToolButton title="Connector" id="line" icon="connector" />
  46. <ToolButton title="Text" id="text" icon="text" />
  47. <ToolButton title="Logseq Portal" id="logseq-portal" icon={<LogseqIcon />} />
  48. </div>
  49. </div>
  50. )
  51. })