Browse Source

store state in hash fragment – closes #4

update-to-0.20.1
Rich-Harris 9 years ago
parent
commit
b8df8c73ff
  1. 2
      src/app/Input/index.html
  2. 40
      src/app/main.js
  3. 18
      src/app/utils/examplesMatch.js
  4. 2
      src/app/utils/path.js

2
src/app/Input/index.html

@ -45,6 +45,8 @@
oninit () { oninit () {
this.observe({ this.observe({
selectedExample: example => { selectedExample: example => {
if ( !example ) return;
const modules = Object.keys( example.modules ).map( key => { const modules = Object.keys( example.modules ).map( key => {
return { return {
name: `${key}.js`, name: `${key}.js`,

40
src/app/main.js

@ -3,14 +3,47 @@ import Output from './Output/index';
import Footer from './Footer'; import Footer from './Footer';
import examples from './examples'; import examples from './examples';
import { dirname, extname, resolve } from './utils/path'; import { dirname, extname, resolve } from './utils/path';
import examplesMatch from './utils/examplesMatch';
// recover state from hash fragment
const json = window.location.hash.slice( 1 );
let saved;
let selectedExample;
try {
saved = JSON.parse( json );
let example;
// does this match an existing example?
for ( let i = 0; i < examples.length; i += 1 ) {
example = examples[i];
if ( examplesMatch( example.modules, saved.modules ) ) {
selectedExample = example;
break;
}
}
} catch ( err ) {
// do nothing
}
const input = new Input({ const input = new Input({
el: '.input', el: '.input',
data: { examples } data: {
examples,
selectedExample
}
}); });
if ( saved ) input.set( 'modules', saved.modules );
const output = new Output({ const output = new Output({
el: '.output' el: '.output',
data: {
options: saved ? saved.options : {
format: 'amd',
moduleName: 'myBundle'
}
}
}); });
const footer = new Footer({ const footer = new Footer({
@ -27,6 +60,9 @@ function update () {
moduleById[ module.name ] = module; moduleById[ module.name ] = module;
}); });
// save state as hash fragment
window.location.hash = JSON.stringify({ options, modules });
/*global rollup */ /*global rollup */
rollup.rollup({ rollup.rollup({
entry: '@main', entry: '@main',

18
src/app/utils/examplesMatch.js

@ -0,0 +1,18 @@
export default function examplesMatch ( exampleModules, saved ) {
let i = saved.length;
if ( !i ) return false;
let name;
while ( i-- ) {
name = saved[i].name.replace( /\.js$/, '' );
const a = exampleModules[ name ];
const b = saved[i].code;
if ( !a ) return false;
if ( a.trim() !== b.trim() ) return false;
}
return true;
}

2
src/app/utils/path.js

@ -19,7 +19,7 @@ export function dirname ( path ) {
export function extname ( path ) { export function extname ( path ) {
const match = /\.[^\.]+$/.exec( path ); const match = /\.[^\.]+$/.exec( path );
if ( !match ) return ''; if ( !match ) return '';
return match[0] return match[0];
} }
export function relative ( from, to ) { export function relative ( from, to ) {

Loading…
Cancel
Save