From dad6daf7587bcd7df65046c82061ee5b37b2869e Mon Sep 17 00:00:00 2001 From: Proful Sadangi Date: Mon, 22 Nov 2021 23:28:56 +1100 Subject: [PATCH] [feature] Added LineTool functionality (#343) * Added LineTool functionality * Fix Typo error * Create LineIcon * Update useKeyboardShortcuts.tsx Co-authored-by: Steve Ruiz --- .../components/Primitives/icons/LineIcon.tsx | 9 +++++ .../src/components/Primitives/icons/index.ts | 1 + .../components/ToolsPanel/PrimaryTools.tsx | 6 ++-- .../src/components/ToolsPanel/ShapesMenu.tsx | 6 ++-- .../tldraw/src/hooks/useKeyboardShortcuts.tsx | 14 ++++++-- packages/tldraw/src/state/TldrawApp.ts | 2 ++ .../src/state/tools/LineTool/LineTool.spec.ts | 9 +++++ .../src/state/tools/LineTool/LineTool.ts | 36 +++++++++++++++++++ .../tldraw/src/state/tools/LineTool/index.ts | 1 + packages/tldraw/src/state/tools/index.ts | 3 ++ packages/tldraw/src/types.ts | 2 ++ 11 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 packages/tldraw/src/components/Primitives/icons/LineIcon.tsx create mode 100644 packages/tldraw/src/state/tools/LineTool/LineTool.spec.ts create mode 100644 packages/tldraw/src/state/tools/LineTool/LineTool.ts create mode 100644 packages/tldraw/src/state/tools/LineTool/index.ts diff --git a/packages/tldraw/src/components/Primitives/icons/LineIcon.tsx b/packages/tldraw/src/components/Primitives/icons/LineIcon.tsx new file mode 100644 index 000000000..2ab367dea --- /dev/null +++ b/packages/tldraw/src/components/Primitives/icons/LineIcon.tsx @@ -0,0 +1,9 @@ +import * as React from 'react' + +export function LineIcon() { + return ( + + + + ) +} diff --git a/packages/tldraw/src/components/Primitives/icons/index.ts b/packages/tldraw/src/components/Primitives/icons/index.ts index 03a08533a..95521c38a 100644 --- a/packages/tldraw/src/components/Primitives/icons/index.ts +++ b/packages/tldraw/src/components/Primitives/icons/index.ts @@ -14,3 +14,4 @@ export * from './SizeLargeIcon' export * from './EraserIcon' export * from './MultiplayerIcon' export * from './DiscordIcon' +export * from './LineIcon' diff --git a/packages/tldraw/src/components/ToolsPanel/PrimaryTools.tsx b/packages/tldraw/src/components/ToolsPanel/PrimaryTools.tsx index 58ce890ac..e8510fa1c 100644 --- a/packages/tldraw/src/components/ToolsPanel/PrimaryTools.tsx +++ b/packages/tldraw/src/components/ToolsPanel/PrimaryTools.tsx @@ -75,7 +75,7 @@ export const PrimaryTools = React.memo(function PrimaryTools(): JSX.Element { , [TDShapeType.Ellipse]: , + [TDShapeType.Line]: , } export const ShapesMenu = React.memo(function ShapesMenu({ diff --git a/packages/tldraw/src/hooks/useKeyboardShortcuts.tsx b/packages/tldraw/src/hooks/useKeyboardShortcuts.tsx index f50e6850e..c66bf5b3b 100644 --- a/packages/tldraw/src/hooks/useKeyboardShortcuts.tsx +++ b/packages/tldraw/src/hooks/useKeyboardShortcuts.tsx @@ -66,7 +66,7 @@ export function useKeyboardShortcuts(ref: React.RefObject) { 'a,6', () => { if (!canHandleEvent()) return - app.selectTool(TDShapeType.Arrow) + app.selectTool(TDShapeType.Line) }, undefined, [app] @@ -76,7 +76,7 @@ export function useKeyboardShortcuts(ref: React.RefObject) { 't,7', () => { if (!canHandleEvent()) return - app.selectTool(TDShapeType.Text) + app.selectTool(TDShapeType.Arrow) }, undefined, [app] @@ -84,6 +84,16 @@ export function useKeyboardShortcuts(ref: React.RefObject) { useHotkeys( 'n,8', + () => { + if (!canHandleEvent()) return + app.selectTool(TDShapeType.Text) + }, + undefined, + [app] + ) + + useHotkeys( + 'n,9', () => { if (!canHandleEvent()) return app.selectTool(TDShapeType.Sticky) diff --git a/packages/tldraw/src/state/TldrawApp.ts b/packages/tldraw/src/state/TldrawApp.ts index 9b53be208..b8fda364d 100644 --- a/packages/tldraw/src/state/TldrawApp.ts +++ b/packages/tldraw/src/state/TldrawApp.ts @@ -56,6 +56,7 @@ import { TextTool } from './tools/TextTool' import { DrawTool } from './tools/DrawTool' import { EllipseTool } from './tools/EllipseTool' import { RectangleTool } from './tools/RectangleTool' +import { LineTool } from './tools/LineTool' import { ArrowTool } from './tools/ArrowTool' import { StickyTool } from './tools/StickyTool' @@ -130,6 +131,7 @@ export class TldrawApp extends StateManager { [TDShapeType.Draw]: new DrawTool(this), [TDShapeType.Ellipse]: new EllipseTool(this), [TDShapeType.Rectangle]: new RectangleTool(this), + [TDShapeType.Line]: new LineTool(this), [TDShapeType.Arrow]: new ArrowTool(this), [TDShapeType.Sticky]: new StickyTool(this), } diff --git a/packages/tldraw/src/state/tools/LineTool/LineTool.spec.ts b/packages/tldraw/src/state/tools/LineTool/LineTool.spec.ts new file mode 100644 index 000000000..7a1d76b08 --- /dev/null +++ b/packages/tldraw/src/state/tools/LineTool/LineTool.spec.ts @@ -0,0 +1,9 @@ +import { TldrawApp } from '~state' +import { LineTool } from '.' + +describe('LineTool', () => { + it('creates tool', () => { + const app = new TldrawApp() + new LineTool(app) + }) +}) diff --git a/packages/tldraw/src/state/tools/LineTool/LineTool.ts b/packages/tldraw/src/state/tools/LineTool/LineTool.ts new file mode 100644 index 000000000..d74b60f56 --- /dev/null +++ b/packages/tldraw/src/state/tools/LineTool/LineTool.ts @@ -0,0 +1,36 @@ +import { Utils, TLPointerEventHandler } from '@tldraw/core' +import { Arrow } from '~state/shapes' +import { SessionType, TDShapeType } from '~types' +import { BaseTool, Status } from '../BaseTool' + +export class LineTool extends BaseTool { + type = TDShapeType.Line as const + + /* ----------------- Event Handlers ----------------- */ + + onPointerDown: TLPointerEventHandler = () => { + const { + currentPoint, + appState: { currentPageId, currentStyle }, + } = this.app + + const childIndex = this.getNextChildIndex() + + const id = Utils.uniqueId() + + const newShape = Arrow.create({ + id, + parentId: currentPageId, + childIndex, + point: currentPoint, + decorations: undefined, + style: { ...currentStyle }, + }) + + this.app.patchCreate([newShape]) + + this.app.startSession(SessionType.Arrow, newShape.id, 'end', true) + + this.setStatus(Status.Creating) + } +} diff --git a/packages/tldraw/src/state/tools/LineTool/index.ts b/packages/tldraw/src/state/tools/LineTool/index.ts new file mode 100644 index 000000000..a59ca3d7a --- /dev/null +++ b/packages/tldraw/src/state/tools/LineTool/index.ts @@ -0,0 +1 @@ +export * from './LineTool' diff --git a/packages/tldraw/src/state/tools/index.ts b/packages/tldraw/src/state/tools/index.ts index 955e07721..5c4f13bba 100644 --- a/packages/tldraw/src/state/tools/index.ts +++ b/packages/tldraw/src/state/tools/index.ts @@ -1,5 +1,6 @@ import { TDShapeType, TDToolType } from '~types' import { ArrowTool } from './ArrowTool' +import { LineTool } from './LineTool' import { DrawTool } from './DrawTool' import { EllipseTool } from './EllipseTool' import { RectangleTool } from './RectangleTool' @@ -15,6 +16,7 @@ export interface ToolsMap { [TDShapeType.Draw]: typeof DrawTool [TDShapeType.Ellipse]: typeof EllipseTool [TDShapeType.Rectangle]: typeof RectangleTool + [TDShapeType.Line]: typeof LineTool [TDShapeType.Arrow]: typeof ArrowTool [TDShapeType.Sticky]: typeof StickyTool } @@ -30,6 +32,7 @@ export const tools: { [K in TDToolType]: ToolsMap[K] } = { [TDShapeType.Draw]: DrawTool, [TDShapeType.Ellipse]: EllipseTool, [TDShapeType.Rectangle]: RectangleTool, + [TDShapeType.Line]: LineTool, [TDShapeType.Arrow]: ArrowTool, [TDShapeType.Sticky]: StickyTool, } diff --git a/packages/tldraw/src/types.ts b/packages/tldraw/src/types.ts index a45e38d06..bdefafbd7 100644 --- a/packages/tldraw/src/types.ts +++ b/packages/tldraw/src/types.ts @@ -205,6 +205,7 @@ export type TDToolType = | TDShapeType.Draw | TDShapeType.Ellipse | TDShapeType.Rectangle + | TDShapeType.Line | TDShapeType.Arrow | TDShapeType.Sticky @@ -270,6 +271,7 @@ export enum TDShapeType { Rectangle = 'rectangle', Draw = 'draw', Arrow = 'arrow', + Line = 'line', Text = 'text', Group = 'group', }