Skip to content

Commit 4918015

Browse files
authored
Merge pull request #11 from comatory/comatory/refactor-use-wc
refactoring
2 parents 38c9088 + 67af62f commit 4918015

25 files changed

+691
-496
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
rules: {
2323
quotes: ["error", "double"],
2424
"no-shadow": ["error", { hoist: "functions", allow: [] }],
25+
"no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
2526
},
2627
};

css/panel.css

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,25 @@
3737
justify-content: space-between;
3838
}
3939

40-
.panel-row button {
40+
.panel-row ui-button,
41+
.panel-row color-button,
42+
.panel-row tool-button,
43+
.panel-row variant-button,
44+
.panel-row variant-stamp-button {
4145
margin-right: var(--button-space);
42-
width: var(--button-size);
43-
height: var(--button-size);
44-
border: 2px solid black;
45-
border-radius: 6px;
4646
}
4747

48-
.panel-row button:last-child {
48+
.panel-row ui-button:last-child,
49+
.panel-row color-button:last-child,
50+
.panel-row tool-button:last-child,
51+
.panel-row variant-button:last-child,
52+
.panel-row variant-stamp-button:last-child {
4953
margin-right: 0px;
5054
}
5155

52-
.panel-row button.active {
53-
border: 2px solid white;
54-
box-shadow: 0px 0px 0px 4px orange;
56+
#tools {
57+
display: flex;
58+
flex: 1;
5559
}
5660

5761
#actions {

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ <h3>New version available</h3>
7676
</div>
7777
<div id="right-panel-column">
7878
<section id="global-actions" class="panel-row">
79-
<button id="clear"><img src="img/buttons/clear.svg" /></button>
80-
<button id="save"><img src="img/buttons/save.svg" /></button>
81-
<button id="info"><img src="img/buttons/info.svg" /></button>
79+
<ui-button id="clear" icon-url="img/buttons/clear.svg"></ui-button>
80+
<ui-button id="save" icon-url="img/buttons/save.svg"></ui-button>
81+
<ui-button id="info" icon-url="img/buttons/info.svg"></ui-button>
8282
</section>
8383
</div>
8484
</section>

js/boot.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import { attachKeyboardListeners } from "./controls/keyboard.mjs";
2222
import { attachGamepadBlockListeners } from "./controls/general.mjs";
2323
import { attachGamepadListeners } from "./controls/gamepad.mjs";
2424
import { initializeCursor } from "./cursor.mjs";
25+
import { ColorButton } from "./ui/color.mjs";
26+
import { ToolButton } from "./ui/tool.mjs";
27+
import { VariantButton } from "./ui/variant.mjs";
28+
import { VariantStampButton } from "./ui/variant-stamp.mjs";
29+
import { UiButton } from "./ui/button.mjs";
30+
31+
function registerComponents() {
32+
customElements.define("ui-button", UiButton);
33+
customElements.define("color-button", ColorButton);
34+
customElements.define("tool-button", ToolButton);
35+
customElements.define("variant-button", VariantButton);
36+
customElements.define("variant-stamp-button", VariantStampButton);
37+
}
2538

2639
function attachResizeListeners() {
2740
const canvas = getCanvas();
@@ -59,6 +72,7 @@ export function boot() {
5972
const state = createState();
6073
const canvas = getCanvas();
6174

75+
registerComponents();
6276
restorePreviousCanvas(canvas);
6377
attachCanvasSaveListener(canvas);
6478

js/controls/keyboard.mjs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ export function attachKeyboardListeners(state) {
3939
let accelerationTimer = null;
4040
let keysPressed = NO_KEYS_PRESSED;
4141

42-
function shouldBlockInteractions() {
43-
return state.get((prevState) => prevState.blockedInteractions);
44-
}
45-
4642
window.addEventListener("keyup", (event) => {
47-
if (shouldBlockInteractions()) {
48-
return;
49-
}
50-
5143
switch (event.key) {
5244
case "ArrowUp":
5345
keysPressed = produceMovementKeysPressed(keysPressed, "up", false);
@@ -67,10 +59,6 @@ export function attachKeyboardListeners(state) {
6759
});
6860

6961
window.addEventListener("keydown", (event) => {
70-
if (shouldBlockInteractions()) {
71-
return;
72-
}
73-
7462
if (event.key === "p") {
7563
setTool(TOOLS.PEN, { state });
7664
} else if (event.key === "f") {

js/cursor.mjs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@ export function initializeCursor({ state }) {
2323
const y = cursor.y;
2424
let setCursorTimer = null;
2525

26-
function shouldBlockInteractions() {
27-
return state.get((prevState) => prevState.blockedInteractions);
28-
}
29-
3026
function drawCursorOnMouseMove(event) {
3127
if (setCursorTimer) {
3228
window.clearTimeout(setCursorTimer);
3329
}
3430

35-
if (shouldBlockInteractions()) {
36-
return;
37-
}
38-
3931
const rect = canvas.getBoundingClientRect();
4032
const { clientX, clientY } = event;
4133

js/dom.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,28 @@ export function getPanelTools() {
1212
return document.getElementById("tools");
1313
}
1414

15+
export function getToolButtons() {
16+
return getPanelTools().querySelectorAll("tool-button");
17+
}
18+
19+
export function getToolButtonById(id) {
20+
return Array.from(getToolButtons()).find((button) => button.id === id);
21+
}
22+
1523
export function getPanelToolVariants() {
1624
return document.getElementById("variants");
1725
}
1826

27+
export function getVariantButtons() {
28+
return getPanelToolVariants().querySelectorAll(
29+
"variant-button,variant-stamp-button",
30+
);
31+
}
32+
33+
export function getVariantButtonById(id) {
34+
return Array.from(getVariantButtons()).find((button) => button.id === id);
35+
}
36+
1937
export function getPanelToolActions() {
2038
return document.getElementById("actions");
2139
}
@@ -24,6 +42,14 @@ export function getPanelColors() {
2442
return document.getElementById("colors");
2543
}
2644

45+
export function getColorButtons() {
46+
return getPanelColors().querySelectorAll("color-button");
47+
}
48+
49+
export function getColorButtonByColor(color) {
50+
return Array.from(getColorButtons()).find((button) => button.color === color);
51+
}
52+
2753
export function getPanel() {
2854
return document.getElementById("panel");
2955
}

js/state/actions/cam.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getCountdownAnimationLengthInSeconds,
77
getFlashAnimationLengthInSeconds,
88
} from "../../dom.mjs";
9-
import { blockInteractions, unblockInteractions } from "../actions/ui.mjs";
109

1110
function memorizePhoto({ state }) {
1211
state.set(() => ({
@@ -29,7 +28,6 @@ export function takePhoto({ state }) {
2928
const flashAnimationLength = getFlashAnimationLengthInSeconds() * 1000;
3029

3130
insertCountdown();
32-
blockInteractions({ state });
3331

3432
const videoSettings = cam.srcObject.getVideoTracks()[0]?.getSettings();
3533
const height = canvas.height;
@@ -39,7 +37,6 @@ export function takePhoto({ state }) {
3937
ctx.drawImage(cam, canvas.width / 2 - width / 2, 0, width, height);
4038
memorizePhoto({ state });
4139
removeCountdown();
42-
unblockInteractions({ state });
4340
}, countdownAnimationLength + flashAnimationLength);
4441
}
4542

js/state/actions/ui.mjs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import { getInfoDialog } from "../../dom.mjs";
22

3-
export function blockInteractions({ state }) {
4-
state.set(() => ({
5-
blockedInteractions: true,
6-
}));
7-
}
8-
9-
export function unblockInteractions({ state }) {
10-
state.set(() => ({
11-
blockedInteractions: false,
12-
}));
13-
}
14-
153
export function showInfo() {
164
const dialog = getInfoDialog();
175
const closeButton = dialog.querySelector("#close-info");

js/state/state.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export function createState() {
1717
color: loadColor(),
1818
gamepad: null,
1919
photoMemorized: false,
20-
blockedInteractions: false,
2120
gamepadBlocked: false,
2221
};
2322

0 commit comments

Comments
 (0)