Skip to content

Commit 103d05c

Browse files
committed
[twizzle/edit] Add auto-detection to paste.
1 parent b91e281 commit 103d05c

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

src/sites/alpha.twizzle.net/edit/app.ts

+77-18
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,57 @@ export class App {
118118
initialConfig.viewerLink = "none";
119119
this.twistyPlayer = new TwistyPlayer(initialConfig);
120120
this.puzzlePane.appendChild(this.twistyPlayer);
121-
const originalPuzzleID = initialConfig.puzzle ?? "3x3x3";
121+
122+
this.twistyPlayer.experimentalModel.puzzleSetupAlg.addFreshListener(() =>
123+
this.#onSetupOrAlgChange(),
124+
);
125+
this.twistyPlayer.experimentalModel.puzzleAlg.addFreshListener(() =>
126+
this.#onSetupOrAlgChange(),
127+
);
128+
this.element
129+
.querySelector(".auto-notation-undo")!
130+
.addEventListener("click", () => {
131+
this.controlPane.puzzleSelect.value = this.#autoNotationPuzzleOld!;
132+
this.controlPane.puzzleSelectChanged();
133+
this.element.querySelector<HTMLSpanElement>(
134+
".auto-notation-change-back",
135+
)!.hidden = true;
136+
this.element.querySelector<HTMLSpanElement>(
137+
".auto-notation-change-redo",
138+
)!.hidden = false;
139+
this.#autofixEnabled = false;
140+
});
141+
this.element
142+
.querySelector(".auto-notation-redo")!
143+
.addEventListener("click", () => {
144+
this.controlPane.puzzleSelect.value = this.#autoNotationPuzzleNew!;
145+
this.controlPane.puzzleSelectChanged();
146+
this.element.querySelector<HTMLSpanElement>(
147+
".auto-notation-change-back",
148+
)!.hidden = false;
149+
this.element.querySelector<HTMLSpanElement>(
150+
".auto-notation-change-redo",
151+
)!.hidden = true;
152+
this.#autofixEnabled = true;
153+
});
154+
}
155+
156+
#autoNotationPuzzleOld: PuzzleID | undefined;
157+
#autoNotationPuzzleNew: PuzzleID | undefined;
158+
#autofixEnabled: boolean = true;
159+
// TODO: factor this out into a class
160+
async #onSetupOrAlgChange() {
161+
console.log(this.#autofixEnabled);
162+
if (!this.#autofixEnabled) {
163+
return;
164+
}
122165
(async () => {
123-
const [puzzleAlgWithIssue, puzzleSetupAlgWithIssue] = await Promise.all([
124-
this.twistyPlayer.experimentalModel.puzzleAlg.get(),
125-
this.twistyPlayer.experimentalModel.puzzleSetupAlg.get(),
126-
]);
166+
const [originalPuzzleID, puzzleAlgWithIssue, puzzleSetupAlgWithIssue] =
167+
await Promise.all([
168+
this.twistyPlayer.experimentalModel.puzzleID.get(),
169+
this.twistyPlayer.experimentalModel.puzzleAlg.get(),
170+
this.twistyPlayer.experimentalModel.puzzleSetupAlg.get(),
171+
]);
127172
if (
128173
puzzleAlgWithIssue.issues.errors.length > 0 ||
129174
puzzleSetupAlgWithIssue.issues.errors.length > 0
@@ -132,7 +177,12 @@ export class App {
132177
this.twistyPlayer.experimentalModel.alg.get(),
133178
this.twistyPlayer.experimentalModel.setupAlg.get(),
134179
]);
135-
for (const puzzleID of ["square1", "clock", "megaminx"]) {
180+
for (const puzzleID of [
181+
"3x3x3",
182+
"square1",
183+
"clock",
184+
"megaminx",
185+
] satisfies PuzzleID[]) {
136186
const puzzleLoader = puzzles[puzzleID];
137187
const kpuzzle = await puzzleLoader.kpuzzle();
138188
try {
@@ -142,23 +192,32 @@ export class App {
142192
kpuzzle.defaultPattern().applyAlg(algWithIssue.alg) &&
143193
kpuzzle.defaultPattern().applyAlg(setupAlgWithIssue.alg) // TODO: This ignores e.g. bandaging
144194
) {
145-
this.element.querySelector(".auto-notation-puzzle")!.textContent =
146-
puzzleLoader.fullName;
195+
this.#autoNotationPuzzleOld = originalPuzzleID;
196+
this.#autoNotationPuzzleNew = puzzleID;
197+
198+
for (const elem of this.element.querySelectorAll(
199+
".auto-notation-puzzle-old",
200+
)) {
201+
elem.textContent = puzzles[originalPuzzleID].fullName;
202+
}
203+
for (const elem of this.element.querySelectorAll(
204+
".auto-notation-puzzle-new",
205+
)) {
206+
elem.textContent = puzzleLoader.fullName;
207+
}
208+
147209
this.element.querySelector<HTMLSpanElement>(
148210
".auto-notation",
149211
)!.hidden = false;
212+
this.element.querySelector<HTMLSpanElement>(
213+
".auto-notation-change-back",
214+
)!.hidden = false;
215+
this.element.querySelector<HTMLSpanElement>(
216+
".auto-notation-change-redo",
217+
)!.hidden = true;
218+
150219
this.controlPane.puzzleSelect.value = puzzleID;
151220
this.controlPane.puzzleSelectChanged();
152-
this.element
153-
.querySelector(".auto-notation-undo")!
154-
.addEventListener("click", () => {
155-
this.controlPane.puzzleSelect.value = originalPuzzleID;
156-
this.controlPane.puzzleSelectChanged();
157-
this.element.querySelector(
158-
".auto-notation-changed-back",
159-
)!.textContent =
160-
`This has been changed back to ${puzzles[originalPuzzleID].fullName}.`;
161-
});
162221
return;
163222
}
164223
} catch {}

src/sites/alpha.twizzle.net/edit/index.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
</section>
2828
<div class="alg-features">
2929
<p class="auto-notation" hidden>
30-
The puzzle has been automatically changed to <span class="auto-notation-puzzle"></span> based on the provided
31-
notation. (<span class="auto-notation-changed-back">Want to <a href="#" class="auto-notation-undo">change it
32-
back</a>?</span>)
30+
The puzzle has been automatically changed to <span class="auto-notation-puzzle-new"></span> based on the
31+
provided
32+
notation. (<span class="auto-notation-change-back">Want to <a href="#" class="auto-notation-undo">change it
33+
back to <span class="auto-notation-puzzle-old"></span></a>?</span><span
34+
class="auto-notation-change-redo">Auto-detection has been temporarily disabled. Want to <a href="#"
35+
class="auto-notation-redo">re-enable it and change the puzzle to
36+
to <span class="auto-notation-puzzle-new"></span></a> again?</span>)
3337
</p>
3438
</div>
3539
<expand-button for="setup" expanded="true" exclusive="false">

0 commit comments

Comments
 (0)