Browse Source

Merge pull request #18 from rollup/hash-fragment

store state in hash fragment – closes #4
update-to-0.20.1
Rich Harris 9 years ago
parent
commit
25558b8d23
  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 () {
this.observe({
selectedExample: example => {
if ( !example ) return;
const modules = Object.keys( example.modules ).map( key => {
return {
name: `${key}.js`,

40
src/app/main.js

@ -3,14 +3,47 @@ import Output from './Output/index';
import Footer from './Footer';
import examples from './examples';
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({
el: '.input',
data: { examples }
data: {
examples,
selectedExample
}
});
if ( saved ) input.set( 'modules', saved.modules );
const output = new Output({
el: '.output'
el: '.output',
data: {
options: saved ? saved.options : {
format: 'amd',
moduleName: 'myBundle'
}
}
});
const footer = new Footer({
@ -27,6 +60,9 @@ function update () {
moduleById[ module.name ] = module;
});
// save state as hash fragment
window.location.hash = JSON.stringify({ options, modules });
/*global rollup */
rollup.rollup({
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 ) {
const match = /\.[^\.]+$/.exec( path );
if ( !match ) return '';
return match[0]
return match[0];
}
export function relative ( from, to ) {

Loading…
Cancel
Save