From 6ec5574366e27382e577b4820f379695f747f76c Mon Sep 17 00:00:00 2001 From: Lucas Garron Date: Wed, 9 Oct 2024 12:05:46 -0700 Subject: [PATCH] [twizzle/edit] Add auto-detection to paste. --- src/sites/alpha.twizzle.net/edit/app.ts | 97 +++++++++++++++++---- src/sites/alpha.twizzle.net/edit/index.html | 10 ++- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/src/sites/alpha.twizzle.net/edit/app.ts b/src/sites/alpha.twizzle.net/edit/app.ts index c3f3ebec8..c5110870e 100644 --- a/src/sites/alpha.twizzle.net/edit/app.ts +++ b/src/sites/alpha.twizzle.net/edit/app.ts @@ -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( + ".auto-notation-change-back", + )!.hidden = true; + this.element.querySelector( + ".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( + ".auto-notation-change-back", + )!.hidden = false; + this.element.querySelector( + ".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 @@ -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 { @@ -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( ".auto-notation", )!.hidden = false; + this.element.querySelector( + ".auto-notation-change-back", + )!.hidden = false; + this.element.querySelector( + ".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 {} @@ -210,7 +270,6 @@ export class App { default: return; } - this.twistyPlayer.alg = algAppend(currentAlg, " Solution", solution); } async scramble(): Promise { diff --git a/src/sites/alpha.twizzle.net/edit/index.html b/src/sites/alpha.twizzle.net/edit/index.html index ac77192df..fb52eb3df 100644 --- a/src/sites/alpha.twizzle.net/edit/index.html +++ b/src/sites/alpha.twizzle.net/edit/index.html @@ -27,9 +27,13 @@