|
|
@@ -1,10 +1,16 @@
|
|
|
+import { TLSelectTool } from '@tldraw/core'
|
|
|
import { useApp } from '@tldraw/react'
|
|
|
import { observer } from 'mobx-react-lite'
|
|
|
import * as React from 'react'
|
|
|
import { Button } from '~components/Button'
|
|
|
import { LogseqIcon, TablerIcon } from '~components/icons'
|
|
|
|
|
|
-export const PrimaryTools = observer(function PrimaryTools() {
|
|
|
+interface ToolButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
|
+ id: string
|
|
|
+ icon: string | React.ReactNode
|
|
|
+}
|
|
|
+
|
|
|
+const ToolButton = observer(({ id, icon, title, ...props }: ToolButtonProps) => {
|
|
|
const app = useApp()
|
|
|
|
|
|
const handleToolClick = React.useCallback(
|
|
|
@@ -15,79 +21,38 @@ export const PrimaryTools = observer(function PrimaryTools() {
|
|
|
[app]
|
|
|
)
|
|
|
|
|
|
- const handleToolDoubleClick = React.useCallback(
|
|
|
- (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
- const tool = e.currentTarget.dataset.tool
|
|
|
- if (tool) app.selectTool(tool)
|
|
|
- app.settings.update({ isToolLocked: true })
|
|
|
- },
|
|
|
- [app]
|
|
|
+ // Tool must exist
|
|
|
+ const Tool = app.Tools?.find(T => T.id === id) ?? TLSelectTool
|
|
|
+
|
|
|
+ const shortcut = ((Tool as any)['shortcut'] as string[])?.[0]
|
|
|
+
|
|
|
+ const titleWithShortcut = shortcut ? `${title} (${shortcut})` : title
|
|
|
+ return (
|
|
|
+ <Button
|
|
|
+ {...props}
|
|
|
+ title={titleWithShortcut}
|
|
|
+ data-tool={id}
|
|
|
+ data-selected={id === app.selectedTool.id}
|
|
|
+ onClick={handleToolClick}
|
|
|
+ >
|
|
|
+ {typeof icon === 'string' ? <TablerIcon name={icon} /> : icon}
|
|
|
+ </Button>
|
|
|
)
|
|
|
+})
|
|
|
|
|
|
- const selectedToolId = app.selectedTool.id
|
|
|
+export const PrimaryTools = observer(function PrimaryTools() {
|
|
|
+ const app = useApp()
|
|
|
|
|
|
return (
|
|
|
<div className="tl-primary-tools">
|
|
|
<div className="tl-tools-floating-panel" data-tool-locked={app.settings.isToolLocked}>
|
|
|
- <Button
|
|
|
- title="Select tool"
|
|
|
- data-tool="select"
|
|
|
- data-selected={selectedToolId === 'select'}
|
|
|
- onClick={handleToolClick}
|
|
|
- >
|
|
|
- <TablerIcon name="select-cursor" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Draw tool"
|
|
|
- data-tool="pencil"
|
|
|
- data-selected={selectedToolId === 'pencil'}
|
|
|
- onClick={handleToolClick}
|
|
|
- >
|
|
|
- <TablerIcon name="ballpen" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Highlight tool"
|
|
|
- data-tool="highlighter"
|
|
|
- data-selected={selectedToolId === 'highlighter'}
|
|
|
- onClick={handleToolClick}
|
|
|
- >
|
|
|
- <TablerIcon name="highlight" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Eraser tool"
|
|
|
- data-tool="erase"
|
|
|
- data-selected={selectedToolId === 'erase'}
|
|
|
- onClick={handleToolClick}
|
|
|
- >
|
|
|
- <TablerIcon name="eraser" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Line tool"
|
|
|
- data-tool="line"
|
|
|
- data-selected={selectedToolId === 'line'}
|
|
|
- onClick={handleToolClick}
|
|
|
- onDoubleClick={handleToolDoubleClick}
|
|
|
- >
|
|
|
- <TablerIcon name="connector" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Text tool"
|
|
|
- data-tool="text"
|
|
|
- data-selected={selectedToolId === 'text'}
|
|
|
- onClick={handleToolClick}
|
|
|
- onDoubleClick={handleToolDoubleClick}
|
|
|
- >
|
|
|
- <TablerIcon name="text" />
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- title="Logseq Portal tool"
|
|
|
- data-tool="logseq-portal"
|
|
|
- data-selected={selectedToolId === 'logseq-portal'}
|
|
|
- onClick={handleToolClick}
|
|
|
- onDoubleClick={handleToolDoubleClick}
|
|
|
- >
|
|
|
- <LogseqIcon />
|
|
|
- </Button>
|
|
|
+ <ToolButton title="Select" id="select" icon="select-cursor" />
|
|
|
+ <ToolButton title="Draw" id="pencil" icon="ballpen" />
|
|
|
+ <ToolButton title="Highlight" id="highlighter" icon="highlight" />
|
|
|
+ <ToolButton title="Eraser" id="erase" icon="eraser" />
|
|
|
+ <ToolButton title="Connector" id="line" icon="connector" />
|
|
|
+ <ToolButton title="Text" id="text" icon="text" />
|
|
|
+ <ToolButton title="Logseq Portal" id="logseq-portal" icon={<LogseqIcon />} />
|
|
|
</div>
|
|
|
</div>
|
|
|
)
|