From 29b65d6bac31bdf37aab8659f4ad22ff8f4ff215 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 21 Aug 2016 16:59:24 -0400 Subject: [PATCH] make examples more easily shareable --- src/app/main.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/app/main.js b/src/app/main.js index 432095f..e363f82 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -14,7 +14,20 @@ if ( supported ) { console.log( `running Rollup version %c${rollup.VERSION}`, 'font-weight: bold' ); // recover state from hash fragment - const json = decodeURIComponent( window.location.hash.slice( 1 ) ); + let json; + const match = /shareable=(.+)$/.exec( window.location.search ); + if ( match ) { + try { + json = decodeURIComponent( atob( match[1] ) ); + } catch ( err ) { + // noop + } + } + + if ( !match ) { + // legacy + json = decodeURIComponent( window.location.hash.slice( 1 ) ); + } let saved; let selectedExample; @@ -106,7 +119,7 @@ if ( supported ) { }); // save state as hash fragment - window.location.hash = encodeURIComponent( JSON.stringify({ options, modules }) ); + history.replaceState( {}, 'x', `/?shareable=${btoa( encodeURIComponent( JSON.stringify({ options, modules }) ) )}` ); }) .catch( error => { output.set( 'error', error );