Skip to content

Commit

Permalink
switch back to brush tool when selecting colors
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenist committed Dec 23, 2023
1 parent 29172b1 commit 052484d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
13 changes: 7 additions & 6 deletions apps/web/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./style.css"

import { WebDrawingEngine, EventType, ToolNames } from "@libs/drawing-engine"
import { WebDrawingEngine, EventType, ToolNames, ToolName } from "@libs/drawing-engine"
import { ColorPicker } from "@libs/color-picker"
import { Color } from "@libs/shared"

Expand Down Expand Up @@ -90,22 +90,22 @@ function main() {
e.returnValue = ""
})
}
function makeToolbar<T extends string>(
function makeToolbar(
root: HTMLElement,
options: {
state: { hasDrawn: boolean }
initialColor?: Color
initialOpacity?: number
initialTool?: T
initialTool?: ToolName
getLineWeight: () => number | null

tools: ReadonlyArray<{ label: string; value: T }>
tools: ReadonlyArray<{ label: string; value: ToolName }>

onClear: () => void
onSetOpacity: (opacity: number) => void
onSetLineWeight: (weight: number) => void
onSetColor: (color: Color) => void
onSetTool: (tool: T) => void
onSetTool: (tool: ToolName) => void
onExport: (name: string) => void
onLoadImage: (image: HTMLImageElement) => void

Expand Down Expand Up @@ -137,6 +137,7 @@ function makeToolbar<T extends string>(
const recentColors = recentColorTray({
onColorSelect(color) {
picker.setColor(color)
options.onSetTool(ToolNames.brush)
},
})

Expand Down Expand Up @@ -182,7 +183,7 @@ function makeToolbar<T extends string>(

let setWeightRef: { setValue?: (weight: number | string) => void } = {}
toolSelect.addEventListener("change", () => {
options.onSetTool(toolSelect.value as T)
options.onSetTool(toolSelect.value as ToolName)
const weight = options.getLineWeight()
if (weight) {
setWeightRef.setValue?.(weight)
Expand Down
4 changes: 4 additions & 0 deletions libs/drawing-engine/src/engine/DrawingEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export class DrawingEngine {
this.callListeners(EventType.changeTool, { tool })
}

public getState(): Readonly<DrawingEngineState> {
return this.state
}

public usePrevTool() {
this.setTool(this.state.prevTool)
}
Expand Down
6 changes: 5 additions & 1 deletion libs/drawing-engine/src/tools/EyeDropperTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export class EyeDropperTool {
}

onCancel() {
this.engine.usePrevTool()
if (this.engine.getState().prevTool === ToolNames.eraser) {
this.engine.setTool(ToolNames.brush)
} else {
this.engine.usePrevTool()
}
this.engine.callListeners(EventType.previewColor, { color: null })
}

Expand Down

0 comments on commit 052484d

Please sign in to comment.