1- import { encode , decode } from 'js-base64'
1+ import { decode } from 'js-base64'
22import { $ , $$ } from './utils/dom.js'
33import { createEditor } from './editor.js'
44import debounce from './utils/debounce.js'
@@ -11,6 +11,8 @@ import setTheme from './theme.js'
1111import setLanguage from './language.js'
1212import { configurePrettierHotkeys } from './monaco-prettier/configurePrettier'
1313import { getHistoryState , subscribeHistory , setHistory } from './history.js'
14+ import { getEncodedString } from './utils/url.js'
15+ import { setUrlSync , handleUrlSyncOnType } from './url-sync.js'
1416
1517import './aside.js'
1618import './skypack.js'
@@ -70,15 +72,11 @@ subscribe(state => {
7072 setGridLayout ( state . layout )
7173 setTheme ( state . theme )
7274 setLanguage ( state . language )
75+ setUrlSync ( state . urlSync , EDITORS )
7376} )
7477
7578const MS_UPDATE_DEBOUNCED_TIME = 200
76- const MS_UPDATE_HASH_DEBOUNCED_TIME = 1000
7779const debouncedUpdate = debounce ( update , MS_UPDATE_DEBOUNCED_TIME )
78- const debouncedUpdateHash = debounce (
79- updateHashedCode ,
80- MS_UPDATE_HASH_DEBOUNCED_TIME
81- )
8280
8381const { html : htmlEditor , css : cssEditor , javascript : jsEditor } = EDITORS
8482
@@ -116,8 +114,9 @@ function update ({ notReload } = {}) {
116114
117115 Preview . updatePreview ( values )
118116
117+ const { maxExecutionTime, urlSync } = getState ( )
118+
119119 if ( ! notReload ) {
120- const { maxExecutionTime } = getState ( )
121120 runJs ( values . js , parseInt ( maxExecutionTime ) )
122121 . then ( ( ) => {
123122 iframe . setAttribute ( 'src' , Preview . getPreviewUrl ( ) )
@@ -129,10 +128,17 @@ function update ({ notReload } = {}) {
129128
130129 updateCss ( )
131130
132- debouncedUpdateHash ( values )
133131 if ( saveLocalstorage ) {
134132 updateHistory ( values )
135133 }
134+
135+ if ( urlSync ) {
136+ handleUrlSyncOnType ( values )
137+ }
138+
139+ // fixes url bugs when using history
140+ setUrlSync ( urlSync , EDITORS )
141+
136142 updateButtonAvailabilityIfContent ( values )
137143}
138144
@@ -144,14 +150,9 @@ function updateCss () {
144150 }
145151}
146152
147- function updateHashedCode ( { html, css, js } ) {
148- const hashedCode = `${ encode ( html ) } |${ encode ( css ) } |${ encode ( js ) } `
149- window . history . replaceState ( null , null , `/${ hashedCode } ` )
150- }
151-
152153function updateHistory ( { html, css, js } ) {
153154 const { history } = getHistoryState ( )
154- const hashedCode = ` ${ encode ( html ) } | ${ encode ( css ) } | ${ encode ( js ) } `
155+ const hashedCode = getEncodedString ( { html , css, js } )
155156 const isEmpty = ! html . replace ( / \n / g, '' ) . trim ( ) && ! css . replace ( / \n / g, '' ) . trim ( ) && ! js . replace ( / \n / g, '' ) . trim ( )
156157
157158 if ( isEmpty && ! history . current ) {
0 commit comments