Selaa lähdekoodia

[feature] Added LineTool functionality (#343)

* Added LineTool functionality

* Fix Typo error

* Create LineIcon

* Update useKeyboardShortcuts.tsx

Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
main
Proful Sadangi 3 vuotta sitten
vanhempi
commit
dad6daf758
No account linked to committer's email address

+ 9
- 0
packages/tldraw/src/components/Primitives/icons/LineIcon.tsx Näytä tiedosto

@@ -0,0 +1,9 @@
1
+import * as React from 'react'
2
+
3
+export function LineIcon() {
4
+  return (
5
+    <svg width="15" height="15" viewBox="0 0 15 15" fill="black" xmlns="http://www.w3.org/2000/svg">
6
+      <path d="M3.64645 11.3536C3.45118 11.1583 3.45118 10.8417 3.64645 10.6465L11.1464 3.14645C11.3417 2.95118 11.6583 2.95118 11.8536 3.14645C12.0488 3.34171 12.0488 3.65829 11.8536 3.85355L4.35355 11.3536C4.15829 11.5488 3.84171 11.5488 3.64645 11.3536Z" />
7
+    </svg>
8
+  )
9
+}

+ 1
- 0
packages/tldraw/src/components/Primitives/icons/index.ts Näytä tiedosto

@@ -14,3 +14,4 @@ export * from './SizeLargeIcon'
14 14
 export * from './EraserIcon'
15 15
 export * from './MultiplayerIcon'
16 16
 export * from './DiscordIcon'
17
+export * from './LineIcon'

+ 3
- 3
packages/tldraw/src/components/ToolsPanel/PrimaryTools.tsx Näytä tiedosto

@@ -75,7 +75,7 @@ export const PrimaryTools = React.memo(function PrimaryTools(): JSX.Element {
75 75
       </ToolButtonWithTooltip>
76 76
       <ShapesMenu activeTool={activeTool} isToolLocked={isToolLocked} />
77 77
       <ToolButtonWithTooltip
78
-        kbd={'6'}
78
+        kbd={'7'}
79 79
         label={TDShapeType.Arrow}
80 80
         onClick={selectArrowTool}
81 81
         isLocked={isToolLocked}
@@ -84,7 +84,7 @@ export const PrimaryTools = React.memo(function PrimaryTools(): JSX.Element {
84 84
         <ArrowTopRightIcon />
85 85
       </ToolButtonWithTooltip>
86 86
       <ToolButtonWithTooltip
87
-        kbd={'7'}
87
+        kbd={'8'}
88 88
         label={TDShapeType.Text}
89 89
         onClick={selectTextTool}
90 90
         isLocked={isToolLocked}
@@ -93,7 +93,7 @@ export const PrimaryTools = React.memo(function PrimaryTools(): JSX.Element {
93 93
         <TextIcon />
94 94
       </ToolButtonWithTooltip>
95 95
       <ToolButtonWithTooltip
96
-        kbd={'8'}
96
+        kbd={'9'}
97 97
         label={TDShapeType.Sticky}
98 98
         onClick={selectStickyTool}
99 99
         isActive={activeTool === TDShapeType.Sticky}

+ 4
- 2
packages/tldraw/src/components/ToolsPanel/ShapesMenu.tsx Näytä tiedosto

@@ -6,17 +6,19 @@ import { TDShapeType, TDToolType } from '~types'
6 6
 import { useTldrawApp } from '~hooks'
7 7
 import { SquareIcon, CircleIcon } from '@radix-ui/react-icons'
8 8
 import { Tooltip } from '~components/Primitives/Tooltip'
9
+import { LineIcon } from '~components/Primitives/icons'
9 10
 
10 11
 interface ShapesMenuProps {
11 12
   activeTool: TDToolType
12 13
   isToolLocked: boolean
13 14
 }
14 15
 
15
-type ShapeShape = TDShapeType.Rectangle | TDShapeType.Ellipse
16
-const shapeShapes: ShapeShape[] = [TDShapeType.Rectangle, TDShapeType.Ellipse]
16
+type ShapeShape = TDShapeType.Rectangle | TDShapeType.Ellipse | TDShapeType.Line
17
+const shapeShapes: ShapeShape[] = [TDShapeType.Rectangle, TDShapeType.Ellipse, TDShapeType.Line]
17 18
 const shapeShapeIcons = {
18 19
   [TDShapeType.Rectangle]: <SquareIcon />,
19 20
   [TDShapeType.Ellipse]: <CircleIcon />,
21
+  [TDShapeType.Line]: <LineIcon />,
20 22
 }
21 23
 
22 24
 export const ShapesMenu = React.memo(function ShapesMenu({

+ 12
- 2
packages/tldraw/src/hooks/useKeyboardShortcuts.tsx Näytä tiedosto

@@ -66,7 +66,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
66 66
     'a,6',
67 67
     () => {
68 68
       if (!canHandleEvent()) return
69
-      app.selectTool(TDShapeType.Arrow)
69
+      app.selectTool(TDShapeType.Line)
70 70
     },
71 71
     undefined,
72 72
     [app]
@@ -76,7 +76,7 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
76 76
     't,7',
77 77
     () => {
78 78
       if (!canHandleEvent()) return
79
-      app.selectTool(TDShapeType.Text)
79
+      app.selectTool(TDShapeType.Arrow)
80 80
     },
81 81
     undefined,
82 82
     [app]
@@ -84,6 +84,16 @@ export function useKeyboardShortcuts(ref: React.RefObject<HTMLDivElement>) {
84 84
 
85 85
   useHotkeys(
86 86
     'n,8',
87
+    () => {
88
+      if (!canHandleEvent()) return
89
+      app.selectTool(TDShapeType.Text)
90
+    },
91
+    undefined,
92
+    [app]
93
+  )
94
+
95
+  useHotkeys(
96
+    'n,9',
87 97
     () => {
88 98
       if (!canHandleEvent()) return
89 99
       app.selectTool(TDShapeType.Sticky)

+ 2
- 0
packages/tldraw/src/state/TldrawApp.ts Näytä tiedosto

@@ -56,6 +56,7 @@ import { TextTool } from './tools/TextTool'
56 56
 import { DrawTool } from './tools/DrawTool'
57 57
 import { EllipseTool } from './tools/EllipseTool'
58 58
 import { RectangleTool } from './tools/RectangleTool'
59
+import { LineTool } from './tools/LineTool'
59 60
 import { ArrowTool } from './tools/ArrowTool'
60 61
 import { StickyTool } from './tools/StickyTool'
61 62
 
@@ -130,6 +131,7 @@ export class TldrawApp extends StateManager<TDSnapshot> {
130 131
     [TDShapeType.Draw]: new DrawTool(this),
131 132
     [TDShapeType.Ellipse]: new EllipseTool(this),
132 133
     [TDShapeType.Rectangle]: new RectangleTool(this),
134
+    [TDShapeType.Line]: new LineTool(this),
133 135
     [TDShapeType.Arrow]: new ArrowTool(this),
134 136
     [TDShapeType.Sticky]: new StickyTool(this),
135 137
   }

+ 9
- 0
packages/tldraw/src/state/tools/LineTool/LineTool.spec.ts Näytä tiedosto

@@ -0,0 +1,9 @@
1
+import { TldrawApp } from '~state'
2
+import { LineTool } from '.'
3
+
4
+describe('LineTool', () => {
5
+  it('creates tool', () => {
6
+    const app = new TldrawApp()
7
+    new LineTool(app)
8
+  })
9
+})

+ 36
- 0
packages/tldraw/src/state/tools/LineTool/LineTool.ts Näytä tiedosto

@@ -0,0 +1,36 @@
1
+import { Utils, TLPointerEventHandler } from '@tldraw/core'
2
+import { Arrow } from '~state/shapes'
3
+import { SessionType, TDShapeType } from '~types'
4
+import { BaseTool, Status } from '../BaseTool'
5
+
6
+export class LineTool extends BaseTool {
7
+  type = TDShapeType.Line as const
8
+
9
+  /* ----------------- Event Handlers ----------------- */
10
+
11
+  onPointerDown: TLPointerEventHandler = () => {
12
+    const {
13
+      currentPoint,
14
+      appState: { currentPageId, currentStyle },
15
+    } = this.app
16
+
17
+    const childIndex = this.getNextChildIndex()
18
+
19
+    const id = Utils.uniqueId()
20
+
21
+    const newShape = Arrow.create({
22
+      id,
23
+      parentId: currentPageId,
24
+      childIndex,
25
+      point: currentPoint,
26
+      decorations: undefined,
27
+      style: { ...currentStyle },
28
+    })
29
+
30
+    this.app.patchCreate([newShape])
31
+
32
+    this.app.startSession(SessionType.Arrow, newShape.id, 'end', true)
33
+
34
+    this.setStatus(Status.Creating)
35
+  }
36
+}

+ 1
- 0
packages/tldraw/src/state/tools/LineTool/index.ts Näytä tiedosto

@@ -0,0 +1 @@
1
+export * from './LineTool'

+ 3
- 0
packages/tldraw/src/state/tools/index.ts Näytä tiedosto

@@ -1,5 +1,6 @@
1 1
 import { TDShapeType, TDToolType } from '~types'
2 2
 import { ArrowTool } from './ArrowTool'
3
+import { LineTool } from './LineTool'
3 4
 import { DrawTool } from './DrawTool'
4 5
 import { EllipseTool } from './EllipseTool'
5 6
 import { RectangleTool } from './RectangleTool'
@@ -15,6 +16,7 @@ export interface ToolsMap {
15 16
   [TDShapeType.Draw]: typeof DrawTool
16 17
   [TDShapeType.Ellipse]: typeof EllipseTool
17 18
   [TDShapeType.Rectangle]: typeof RectangleTool
19
+  [TDShapeType.Line]: typeof LineTool
18 20
   [TDShapeType.Arrow]: typeof ArrowTool
19 21
   [TDShapeType.Sticky]: typeof StickyTool
20 22
 }
@@ -30,6 +32,7 @@ export const tools: { [K in TDToolType]: ToolsMap[K] } = {
30 32
   [TDShapeType.Draw]: DrawTool,
31 33
   [TDShapeType.Ellipse]: EllipseTool,
32 34
   [TDShapeType.Rectangle]: RectangleTool,
35
+  [TDShapeType.Line]: LineTool,
33 36
   [TDShapeType.Arrow]: ArrowTool,
34 37
   [TDShapeType.Sticky]: StickyTool,
35 38
 }

+ 2
- 0
packages/tldraw/src/types.ts Näytä tiedosto

@@ -205,6 +205,7 @@ export type TDToolType =
205 205
   | TDShapeType.Draw
206 206
   | TDShapeType.Ellipse
207 207
   | TDShapeType.Rectangle
208
+  | TDShapeType.Line
208 209
   | TDShapeType.Arrow
209 210
   | TDShapeType.Sticky
210 211
 
@@ -270,6 +271,7 @@ export enum TDShapeType {
270 271
   Rectangle = 'rectangle',
271 272
   Draw = 'draw',
272 273
   Arrow = 'arrow',
274
+  Line = 'line',
273 275
   Text = 'text',
274 276
   Group = 'group',
275 277
 }

Loading…
Peruuta
Tallenna