Skip to content

Commit

Permalink
[twisty] Add the <twizzle-link> element.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarron committed Jan 22, 2022
1 parent c22b5f8 commit 201fcaa
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/cubing/twisty/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { TwistyPlayer } from "./views/TwistyPlayer";
export { TwistyAlgViewer } from "./views/TwistyAlgViewer";
export { TwistyAlgEditor } from "./views/TwistyAlgEditor/TwistyAlgEditor";
export { TwizzleLink } from "./views/twizzle/TwizzleLink";
export type { TwistyPlayerConfig } from "./views/TwistyPlayer";
export { experimentalForceNewRendererSharing } from "./views/3D/Twisty3DVantage";

Expand Down
28 changes: 28 additions & 0 deletions src/cubing/twisty/views/twizzle/TwizzleLink.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CSSSource } from "../ManagedCustomElement";

export const twizzleLinkCSS = new CSSSource(`
.wrapper {
background: rgb(255, 245, 235);
display: grid;
grid-template-columns: 1fr;
border: 1px solid rgba(0, 0, 0, 0.25);
}
.setup-alg, twisty-alg-viewer {
padding: 0.5em 1em;
}
.heading {
background: rgba(255, 230, 210, 1);
font-weight: bold;
padding: 0.25em 0.5em;
}
twisty-player {
width: 100%;
}
twisty-player + .heading {
padding-top: 0.5em;
}
`);
46 changes: 46 additions & 0 deletions src/cubing/twisty/views/twizzle/TwizzleLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TwistyPlayer } from "../..";
import { Alg } from "../../../alg";
import { ManagedCustomElement } from "../ManagedCustomElement";
import { customElementsShim } from "../node-custom-element-shims";
import { TwistyAlgViewer } from "../TwistyAlgViewer";
import { twizzleLinkCSS } from "./TwizzleLink.css";
import { getConfigFromURL } from "./url-params";

export class TwizzleLink extends ManagedCustomElement {
twistyPlayer: TwistyPlayer | null = null;
a: HTMLAnchorElement | null = null;
constructor() {
super({ mode: "open" });
}
connectedCallback() {
this.addCSS(twizzleLinkCSS);
this.a = this.querySelector("a");
if (this.a) {
const config = getConfigFromURL("", this.a.href);
this.twistyPlayer = this.addElement(new TwistyPlayer(config));

if (config.experimentalSetupAlg) {
this.addHeading("Setup");

const setupAlgDiv = this.addElement(document.createElement("div"));
setupAlgDiv.classList.add("setup-alg");
setupAlgDiv.textContent = new Alg(
config.experimentalSetupAlg,
).toString();
}
this.addHeading("Moves");
const twistyAlgViewer = this.addElement(
new TwistyAlgViewer({ twistyPlayer: this.twistyPlayer }),
);
twistyAlgViewer.part.add("twisty-alg-viewer");
}
}

addHeading(text: string): void {
const headingDiv = this.addElement(document.createElement("div"));
headingDiv.classList.add("heading");
headingDiv.textContent = text;
}
}

customElementsShim.define("twizzle-link", TwizzleLink);
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { EXPERIMENTAL_PROP_NO_VALUE } from "../../../cubing/twisty";
import { EXPERIMENTAL_PROP_NO_VALUE } from "../../../../cubing/twisty";
import type {
AlgProp,
AlgWithIssues,
} from "../../../cubing/twisty/model/props/puzzle/state/AlgProp";
import type { TwistyPlayerModel } from "../../../cubing/twisty/model/TwistyPlayerModel";
import type { TwistyPropSource } from "../../../cubing/twisty/model/props/TwistyProp";
} from "../../../../cubing/twisty/model/props/puzzle/state/AlgProp";
import type { TwistyPlayerModel } from "../../../../cubing/twisty/model/TwistyPlayerModel";
import type { TwistyPropSource } from "../../../../cubing/twisty/model/props/TwistyProp";
import {
TwistyPlayerAttribute,
twistyPlayerAttributeMap,
TwistyPlayerConfig,
} from "../../../cubing/twisty/views/TwistyPlayer";
} from "../../../../cubing/twisty/views/TwistyPlayer";

function updateURL(url: URL): void {
window.history.replaceState("", "", url.toString());
Expand Down Expand Up @@ -93,18 +93,23 @@ export class URLParamUpdater {
}
}

const paramMapping: Record<string, TwistyPlayerAttribute> = {
"alg": "alg",
"setup-alg": "experimental-setup-alg",
"setup-anchor": "experimental-setup-anchor",
"puzzle": "puzzle",
"stickering": "experimental-stickering",
"puzzle-description": "experimental-puzzle-description",
};
export function getConfigFromURL(
prefix = "",
url: string = location.href,
): TwistyPlayerConfig {
// TODO: Why does `Object.entries(paramMapping)` crash if this is defined elswhere?
const paramMapping: Record<string, TwistyPlayerAttribute> = {
"alg": "alg",
"setup-alg": "experimental-setup-alg",
"setup-anchor": "experimental-setup-anchor",
"puzzle": "puzzle",
"stickering": "experimental-stickering",
"puzzle-description": "experimental-puzzle-description",
};

export function getConfigFromURL(prefix = ""): TwistyPlayerConfig {
const params = new URL(location.href).searchParams;
const params = new URL(url).searchParams;
const config: TwistyPlayerConfig = {};
console.log(paramMapping);
for (const [ourParam, twistyPlayerParam] of Object.entries(paramMapping)) {
const paramValue = params.get(prefix + ourParam);
if (paramValue !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/sites/alpha.twizzle.net/edit/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import { customElementsShim } from "../../../cubing/twisty/views/node-custom-ele
import "../../../cubing/twisty/views/stream/TwistyStreamSource";
import type { TwistyStreamSource } from "../../../cubing/twisty/views/stream/TwistyStreamSource";
import type { TwistyAlgEditor } from "../../../cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor";
import { URLParamUpdater } from "../core/url-params";
import { findOrCreateChild, findOrCreateChildWithClass } from "./dom";
import { examples } from "./examples";
import { APP_TITLE } from "./strings";
import { puzzleGroups, supportedPuzzles } from "./supported-puzzles";
import type { SetupToLocation } from "../../../cubing/twisty/model/props/puzzle/state/SetupAnchorProp";
import { URLParamUpdater } from "../../../cubing/twisty/views/twizzle/url-params";
// import { setURLParams } from "./url-params";

function algAppend(oldAlg: Alg, comment: string, newAlg: Alg): Alg {
Expand Down
5 changes: 4 additions & 1 deletion src/sites/alpha.twizzle.net/edit/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import "../../../cubing/twisty";
import { experimentalDebugShowRenderStats } from "../../../cubing/twisty";
import { getConfigFromURL, remapLegacyURLParams } from "../core/url-params";
import {
getConfigFromURL,
remapLegacyURLParams,
} from "../../../cubing/twisty/views/twizzle/url-params";
import { App } from "./app";
import { getURLParam } from "./url-params";

Expand Down
2 changes: 1 addition & 1 deletion src/sites/alpha.twizzle.net/explore/twisty-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../../../cubing/twisty";
import type { OrbitCoordinates } from "../../../cubing/twisty/model/props/viewer/OrbitCoordinatesRequestProp";
import { positionToOrbitCoordinates } from "../../../cubing/twisty/views/3D/TwistyOrbitControls";
import { getConfigFromURL } from "../core/url-params";
import { getConfigFromURL } from "../../../cubing/twisty/views/twizzle/url-params";
import { setupPropInputs } from "./prop-inputs";
import { getURLParam, setAlgParam } from "./url-params";

Expand Down
1 change: 1 addition & 0 deletions src/sites/experiments.cubing.net/cubing.js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ <h2><code>cubing.js</code><br />Dev Apps</h2>
🔄
<a href="./twisty/twisty-alg-editor/">twisty/twisty-alg-editor</a>
</div>
<div>🔗 <a href="./twisty/twizzle-link.html">twisty/twizzle-link</a></div>
<div>
🚨
<a href="./twisty/error-puzzle.html">twisty/error-puzzle</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "../../../../cubing/twisty";
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>Twizzle Link | twisty.js</title>
<link href="./inline.css" rel="stylesheet" type="text/css" />
<script src="./twisty.js" href="./twisty.ts" type="module"></script>
<style>
twizzle-link {
margin: 1em;
font-family: Ubuntu;
}
</style>
</head>

<body>
<div class="content">
<twizzle-link>
<a
href="https://alpha.twizzle.net/edit/?alg=R+U+R%27+U%27+R%27+F+R2+U%27+R%27+U%27+R+U+R%27+F%27&stickering=PLL"
>https://alpha.twizzle.net/edit/?alg=R+U+R%27+U%27+R%27+F+R2+U%27+R%27+U%27+R+U+R%27+F%27&stickering=PLL</a
>
</twizzle-link>
<twizzle-link>
<a
href="https://alpha.twizzle.net/edit/?alg=y+x%27+%2F%2F+inspection%0AU+R2+U%27+F%27+L+F%27+U%27+L%27+%2F%2F+XX-Cross+%2B+EO%0AU%27+R+U+R%27+%2F%2F+3rd+slot%0AR%27+U+R+U2%27+R%27+U+R+%2F%2F+4th+slot%0AU+R%27+U%27+R+U%27+R%27+U2+R+%2F%2F+OLL+%2F+ZBLL%0AU+%2F%2F+AUF%0A%0A%2F%2F+from+http%3A%2F%2Fcubesolv.es%2Fsolve%2F5757&setup-alg=F+U2+L2+B2+F%27+U+L2+U+R2+D2+L%27+B+L2+B%27+R2+U2"
></a>
</twizzle-link>
</div>
</body>
</html>

0 comments on commit 201fcaa

Please sign in to comment.