Skip to content

Commit

Permalink
removes eyedropper from history state
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenist committed Dec 23, 2023
1 parent 2d7bb96 commit 766dfbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
33 changes: 18 additions & 15 deletions libs/drawing-engine/src/engine/CanvasHistory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { LineDrawInfo } from "../tools/LineTool"
import { EyeDropperInfo } from "../tools/EyeDropperTool"
import { DrawingEngine } from "./DrawingEngine"

type ToolInfo = LineDrawInfo | EyeDropperInfo
type ToolInfo = LineDrawInfo

export interface HistoryState {
toolInfo: ToolInfo
Expand Down Expand Up @@ -64,29 +63,34 @@ export class CanvasHistory {
imageData: tool.updatesImageData ? canvas.toDataURL() : null,
})
}
public async undo() {
if (!this.canUndo()) {
return
}
const undoneState = this.history.pop()
if (!undoneState) {
return
}
const currentState = this.history[this.history.length - 1]
if (currentState) this.engine.setTool(currentState.toolInfo.tool)
this.redoHistory.push(undoneState)
return await this.drawState(currentState)
}

public async redo() {
if (!this.canRedo()) {
return
}
const state = this.redoHistory.pop()
if (!state) {
return
}
this.history.push(state)
this.engine.setTool(state.toolInfo.tool)
return await this.drawState(state)
}

public async undo() {
const state = this.history.pop()
console.log("Undoing", state)
if (!state) {
return
}
const currentState = this.history[this.history.length - 1]
this.redoHistory.push(state)
return await this.drawState(currentState)
}

protected addHistory(state: HistoryState) {
console.log("Adding history", state)
if (this.history.length >= this.options.maxHistory) {
this.hasTruncated = true
this.history.shift()
Expand All @@ -101,7 +105,6 @@ export class CanvasHistory {
return Promise.resolve(null)
}
const { toolInfo, imageData } = state
console.log("Drawing state", state)
if (!imageData) {
return Promise.resolve(toolInfo)
}
Expand Down
14 changes: 2 additions & 12 deletions libs/drawing-engine/src/engine/DrawingEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SourceImage } from "../utils/image/SourceImage"
import { ToolName, ToolNames } from "../exports"
import { LineDrawInfo, LineTool } from "../tools/LineTool"
import { InputPoint } from "../tools/InputPoint"
import { EyeDropperInfo, EyeDropperTool } from "../tools/EyeDropperTool"
import { EyeDropperTool } from "../tools/EyeDropperTool"
import { CanvasHistory, HistoryState } from "./CanvasHistory"

interface DrawingEngineState {
Expand All @@ -27,7 +27,7 @@ export interface DrawingEngineOptions {
pixelDensity?: number
}

type ToolInfo = LineDrawInfo | EyeDropperInfo
type ToolInfo = LineDrawInfo

export interface DrawingEngineEventMap {
draw: ToolInfo
Expand Down Expand Up @@ -287,11 +287,6 @@ export class DrawingEngine {
if (!toolInfo) {
return
}
if (toolInfo.tool === "eyedropper" && "previousColor" in toolInfo) {
this.setColor(toolInfo.previousColor.copy())
} else {
this.setTool(toolInfo.tool)
}
const undosLeft = this.history.getHistory().undo.length
this.callListeners("undo", { toolInfo, undosLeft })
}
Expand All @@ -301,11 +296,6 @@ export class DrawingEngine {
if (!toolInfo) {
return
}
if (toolInfo.tool === "eyedropper" && "color" in toolInfo) {
this.setColor(toolInfo.color.copy())
} else {
this.setTool(toolInfo.tool)
}
const redosLeft = this.history.getHistory().redo.length
this.callListeners("redo", { toolInfo, redosLeft })
}
Expand Down
6 changes: 0 additions & 6 deletions libs/drawing-engine/src/tools/EyeDropperTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,8 @@ export class EyeDropperTool {
if (!color) {
return false
}
const previousColor = this.engine.getCurrentColor()
this.engine.setColor(color)
this.engine.callListeners("pickColor", { color })
this.engine.addHistory({
tool: this.toolName,
color,
previousColor,
})
return true
}
}

0 comments on commit 766dfbf

Please sign in to comment.