Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playground: add an option for URL hash persistence #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/bc-datalog-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
export class BCDatalogPlayground extends LitElement {
@property() code = "";
@property() showBlocks = false;
@property() fromHash = false;
@state() blocks: Array<{ code: string; externalKey: string | null }> = [];
@state() private started = false;

Expand All @@ -40,6 +41,26 @@ export class BCDatalogPlayground extends LitElement {
return { code, externalKey };
})
.filter(({ code }, i) => i === 0 || code !== "");
setTimeout(() => this.updateFromHash(), 0);
}

updateFromHash() {
if (!this.fromHash) return;
let hashContents;
try {
hashContents = JSON.parse(atob(window.location.hash.substr(1)));
} catch (e) {
console.error({ e });
}
const [code, blocks] = hashContents;
this.code = code;
this.blocks = blocks;
}

updateHash() {
if (!this.fromHash) return;
const ser = [this.code, this.blocks];
window.location.hash = btoa(JSON.stringify(ser));
}

firstUpdated() {
Expand All @@ -50,6 +71,7 @@ export class BCDatalogPlayground extends LitElement {
const newBlocks = [...this.blocks];
newBlocks.push({ code: "", externalKey: null });
this.blocks = newBlocks;
this.updateHash();
}

onUpdatedBlock(blockId: number, e: { detail: { code: string } }) {
Expand All @@ -59,6 +81,7 @@ export class BCDatalogPlayground extends LitElement {
externalKey: newBlocks[blockId].externalKey,
};
this.blocks = newBlocks;
this.updateHash();
}

onUpdatedExternalKey(blockId: number, e: InputEvent) {
Expand All @@ -69,10 +92,12 @@ export class BCDatalogPlayground extends LitElement {
externalKey: newValue !== "" ? newValue : null,
};
this.blocks = newBlocks;
this.updateHash();
}

onUpdatedCode(e: { detail: { code: string } }) {
this.code = e.detail.code;
this.updateHash();
}

render() {
Expand Down