Skip to content

Commit

Permalink
[twizzle/edit] Add auto-detection to paste.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Oct 9, 2024
1 parent b91e281 commit 83e8855
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
96 changes: 78 additions & 18 deletions src/sites/alpha.twizzle.net/edit/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,58 @@ export class App {
initialConfig.viewerLink = "none";
this.twistyPlayer = new TwistyPlayer(initialConfig);
this.puzzlePane.appendChild(this.twistyPlayer);
const originalPuzzleID = initialConfig.puzzle ?? "3x3x3";

this.twistyPlayer.experimentalModel.puzzleSetupAlg.addFreshListener(() =>
this.#onSetupOrAlgChange(),
);
this.twistyPlayer.experimentalModel.puzzleAlg.addFreshListener(() =>
this.#onSetupOrAlgChange(),
);
this.element
.querySelector(".auto-notation-undo")!
.addEventListener("click", () => {
this.controlPane.puzzleSelect.value = this.#autoNotationPuzzleOld!;
this.controlPane.puzzleSelectChanged();
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-back",
)!.hidden = true;
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-redo",
)!.hidden = false;
this.#autofixEnabled = false;
});
this.element
.querySelector(".auto-notation-redo")!
.addEventListener("click", () => {
this.controlPane.puzzleSelect.value = this.#autoNotationPuzzleNew!;
this.controlPane.puzzleSelectChanged();
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-back",
)!.hidden = false;
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-redo",
)!.hidden = true;
this.#autofixEnabled = true;
});
}

#autoNotationPuzzleOld: PuzzleID | undefined;
#autoNotationPuzzleNew: PuzzleID | undefined;
#autofixEnabled: boolean = true;
// TODO: factor this out into a class
// TODO: cancel this behaviour if the user has already undone once during this page load?
async #onSetupOrAlgChange() {
console.log(this.#autofixEnabled);
if (!this.#autofixEnabled) {
return;
}
(async () => {
const [puzzleAlgWithIssue, puzzleSetupAlgWithIssue] = await Promise.all([
this.twistyPlayer.experimentalModel.puzzleAlg.get(),
this.twistyPlayer.experimentalModel.puzzleSetupAlg.get(),
]);
const [originalPuzzleID, puzzleAlgWithIssue, puzzleSetupAlgWithIssue] =
await Promise.all([
this.twistyPlayer.experimentalModel.puzzleID.get(),
this.twistyPlayer.experimentalModel.puzzleAlg.get(),
this.twistyPlayer.experimentalModel.puzzleSetupAlg.get(),
]);
if (
puzzleAlgWithIssue.issues.errors.length > 0 ||
puzzleSetupAlgWithIssue.issues.errors.length > 0
Expand All @@ -132,7 +178,12 @@ export class App {
this.twistyPlayer.experimentalModel.alg.get(),
this.twistyPlayer.experimentalModel.setupAlg.get(),
]);
for (const puzzleID of ["square1", "clock", "megaminx"]) {
for (const puzzleID of [
"3x3x3",
"square1",
"clock",
"megaminx",
] satisfies PuzzleID[]) {
const puzzleLoader = puzzles[puzzleID];
const kpuzzle = await puzzleLoader.kpuzzle();
try {
Expand All @@ -142,23 +193,32 @@ export class App {
kpuzzle.defaultPattern().applyAlg(algWithIssue.alg) &&
kpuzzle.defaultPattern().applyAlg(setupAlgWithIssue.alg) // TODO: This ignores e.g. bandaging
) {
this.element.querySelector(".auto-notation-puzzle")!.textContent =
puzzleLoader.fullName;
this.#autoNotationPuzzleOld = originalPuzzleID;
this.#autoNotationPuzzleNew = puzzleID;

for (const elem of this.element.querySelectorAll(
".auto-notation-puzzle-old",
)) {
elem.textContent = puzzles[originalPuzzleID].fullName;
}
for (const elem of this.element.querySelectorAll(
".auto-notation-puzzle-new",
)) {
elem.textContent = puzzleLoader.fullName;
}

this.element.querySelector<HTMLSpanElement>(
".auto-notation",
)!.hidden = false;
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-back",
)!.hidden = false;
this.element.querySelector<HTMLSpanElement>(
".auto-notation-change-redo",
)!.hidden = true;

this.controlPane.puzzleSelect.value = puzzleID;
this.controlPane.puzzleSelectChanged();
this.element
.querySelector(".auto-notation-undo")!
.addEventListener("click", () => {
this.controlPane.puzzleSelect.value = originalPuzzleID;
this.controlPane.puzzleSelectChanged();
this.element.querySelector(
".auto-notation-changed-back",
)!.textContent =
`This has been changed back to ${puzzles[originalPuzzleID].fullName}.`;
});
return;
}
} catch {}
Expand Down
10 changes: 7 additions & 3 deletions src/sites/alpha.twizzle.net/edit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
</section>
<div class="alg-features">
<p class="auto-notation" hidden>
The puzzle has been automatically changed to <span class="auto-notation-puzzle"></span> based on the provided
notation. (<span class="auto-notation-changed-back">Want to <a href="#" class="auto-notation-undo">change it
back</a>?</span>)
The puzzle has been automatically changed to <span class="auto-notation-puzzle-new"></span> based on the
provided
notation. (<span class="auto-notation-change-back">Want to <a href="#" class="auto-notation-undo">change it
back to <span class="auto-notation-puzzle-old"></span></a>?</span><span
class="auto-notation-change-redo">Auto-detection has been temporarily disabled. Want to <a href="#"
class="auto-notation-redo">re-enable it and change the puzzle to
to <span class="auto-notation-puzzle-new"></span></a> again?</span>)
</p>
</div>
<expand-button for="setup" expanded="true" exclusive="false">
Expand Down

0 comments on commit 83e8855

Please sign in to comment.