From b8df8c73ff33a53d4bd179c3f497821fb7d9cf6b Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 11 Oct 2015 10:31:21 -0400 Subject: [PATCH] =?UTF-8?q?store=20state=20in=20hash=20fragment=20?= =?UTF-8?q?=E2=80=93=20closes=20#4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/Input/index.html | 2 ++ src/app/main.js | 40 ++++++++++++++++++++++++++++++++-- src/app/utils/examplesMatch.js | 18 +++++++++++++++ src/app/utils/path.js | 2 +- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/app/utils/examplesMatch.js diff --git a/src/app/Input/index.html b/src/app/Input/index.html index 9ddb5ec..866c477 100644 --- a/src/app/Input/index.html +++ b/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`, diff --git a/src/app/main.js b/src/app/main.js index 1892e5a..6e6fc38 100644 --- a/src/app/main.js +++ b/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', diff --git a/src/app/utils/examplesMatch.js b/src/app/utils/examplesMatch.js new file mode 100644 index 0000000..fb13a9b --- /dev/null +++ b/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; +} diff --git a/src/app/utils/path.js b/src/app/utils/path.js index 6023d78..b670b69 100644 --- a/src/app/utils/path.js +++ b/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 ) {