persist code in hash
This commit is contained in:
parent
e286ab2830
commit
53e49952d8
1 changed files with 14 additions and 2 deletions
|
|
@ -9,12 +9,23 @@ import {
|
||||||
updateMiniLocations,
|
updateMiniLocations,
|
||||||
highlightMiniLocations,
|
highlightMiniLocations,
|
||||||
} from '@strudel/codemirror';
|
} from '@strudel/codemirror';
|
||||||
import { evalScope } from '@strudel/core';
|
import { evalScope, hash2code } from '@strudel/core';
|
||||||
import { Framer } from '@strudel/draw';
|
import { Framer } from '@strudel/draw';
|
||||||
import { persistentAtom } from '@nanostores/persistent';
|
import { persistentAtom } from '@nanostores/persistent';
|
||||||
import { DoughRepl } from './dough-repl.mjs';
|
import { DoughRepl } from './dough-repl.mjs';
|
||||||
|
|
||||||
const initialCode = '$: note("c a f e")';
|
let initialCode = '$: note("c a f e")';
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
try {
|
||||||
|
const codeParam = window.location.href.split('#')[1] || '';
|
||||||
|
if (codeParam) {
|
||||||
|
initialCode = hash2code(codeParam);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('could not init code from url');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const code = persistentAtom('vanilla-repl-code', initialCode, {
|
export const code = persistentAtom('vanilla-repl-code', initialCode, {
|
||||||
encode: JSON.stringify,
|
encode: JSON.stringify,
|
||||||
decode: JSON.parse,
|
decode: JSON.parse,
|
||||||
|
|
@ -64,6 +75,7 @@ export class DoughMirror {
|
||||||
this.flash();
|
this.flash();
|
||||||
await this.prebaked;
|
await this.prebaked;
|
||||||
const { miniLocations } = await this.repl.evaluate(this.code);
|
const { miniLocations } = await this.repl.evaluate(this.code);
|
||||||
|
window.location.hash = '#' + code2hash(this.code);
|
||||||
updateMiniLocations(this.editor, miniLocations);
|
updateMiniLocations(this.editor, miniLocations);
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue