Skip to content

Commit 2cb1122

Browse files
committed
Disable brush and eraser when canvas is hidden
Resolves #42.
1 parent 3f2d6fe commit 2cb1122

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

index.css

+7-2
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ body {
234234
user-select: none;
235235
}
236236

237-
#toolbox>div:hover {
237+
#toolbox>div:not([data-disabled]):hover {
238238
background-color: var(--widget-color-bg-hover);
239239
color: var(--text-hover);
240240
}
241241

242-
#toolbox>div:active {
242+
#toolbox>div:not([data-disabled]):active {
243243
opacity: var(--widget-active-opacity);
244244
}
245245

@@ -249,6 +249,11 @@ body {
249249
left: 8px;
250250
}
251251

252+
#toolbox>div[data-disabled] {
253+
opacity: 0.6;
254+
cursor: not-allowed;
255+
}
256+
252257
.settings-form {
253258
display: grid;
254259
grid-template-columns: 4fr 1fr;

index.js

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ function setUpHeroMap(element: HTMLDivElement): HeroMap {
5454
else
5555
toggle.removeAttribute("data-active");
5656
}
57+
if (layer.id == "canvas") {
58+
if (isVisible)
59+
StateManager.dispatch(State.toolbox.canvasEnabled, {} as never);
60+
else
61+
StateManager.dispatch(State.toolbox.canvasDisabled, {} as never);
62+
}
5763
});
5864

5965
const layerNameFromId = (id: string) => {

src/state/toolbox.ts

+14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
namespace State {
44

55
export namespace toolbox {
6+
export const canvasDisabled = "toolbox/canvas/disabled";
7+
export const canvasEnabled = "toolbox/canvas/enabled";
68
export const setup = "toolbox/setup";
79
export const setTool = "toolbox/setTool";
810
}
911

1012
export interface ToolBoxState {
1113
current: string | null;
14+
canvasEnabled: boolean;
1215
map: HeroMap | null;
1316
}
1417

1518
export const defaultToolboxState: ToolBoxState = {
1619
current: null,
20+
canvasEnabled: true,
1721
map: null,
1822
};
1923

@@ -24,6 +28,16 @@ namespace State {
2428
data: never,
2529
): ToolBoxState {
2630
switch (action) {
31+
case toolbox.canvasDisabled:
32+
return {
33+
...state,
34+
canvasEnabled: false,
35+
};
36+
case toolbox.canvasEnabled:
37+
return {
38+
...state,
39+
canvasEnabled: true,
40+
};
2741
case toolbox.setup:
2842
return {
2943
...state,

src/toolbox/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ document.addEventListener("DOMContentLoaded", () => {
1818
btn.innerText = tool.displayName;
1919
btn.setAttribute("data-tool-id", tool.id);
2020
btn.addEventListener("click", () => {
21+
if (btn.hasAttribute("data-disabled"))
22+
return;
2123
StateManager.dispatch(
2224
State.toolbox.setTool, { id: tool.id } as never);
2325
});
@@ -78,4 +80,23 @@ document.addEventListener("DOMContentLoaded", () => {
7880
document.getElementById("tool-help")!.innerText = help;
7981
}
8082
});
83+
84+
// Toggle canvas
85+
const onCanvasToggle = (state: State.AppState) => {
86+
const isEnabled = state.toolbox.canvasEnabled;
87+
["brush", "eraser"].forEach(id => {
88+
const tool = toolInstances.get(id);
89+
if (tool && state.toolbox.current === id && !isEnabled)
90+
StateManager.dispatch(State.toolbox.setTool, { id: Tool.id } as never);
91+
92+
const btn = document.querySelector<HTMLDivElement>(
93+
`[data-tool-id="${id}"]`);
94+
if (isEnabled)
95+
btn?.removeAttribute("data-disabled");
96+
else
97+
btn?.setAttribute("data-disabled", "");
98+
});
99+
};
100+
StateManager.subscribe(State.toolbox.canvasEnabled, onCanvasToggle);
101+
StateManager.subscribe(State.toolbox.canvasDisabled, onCanvasToggle);
81102
});

0 commit comments

Comments
 (0)