Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/sfc-playground/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { reactive, watchEffect } from 'vue'
import { compileFile, MAIN_FILE } from './sfcCompiler'
import { utoa, atou } from './utils'

const welcomeCode = `
<template>
Expand Down Expand Up @@ -38,7 +39,7 @@ let files: Store['files'] = {}

const savedFiles = location.hash.slice(1)
if (savedFiles) {
const saved = JSON.parse(atob(savedFiles))
const saved = JSON.parse(atou(savedFiles))
for (const filename in saved) {
files[filename] = new File(filename, saved[filename])
}
Expand Down Expand Up @@ -70,7 +71,7 @@ for (const file in store.files) {
}

watchEffect(() => {
history.replaceState({}, '', '#' + btoa(JSON.stringify(exportFiles())))
history.replaceState({}, '', '#' + utoa(JSON.stringify(exportFiles())))
})

export function exportFiles() {
Expand Down
10 changes: 10 additions & 0 deletions packages/sfc-playground/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export function debounce(fn: Function, n = 100) {
}, n)
}
}

// prefer old unicode hacks for backward compatibility
// https://base64.guru/developers/javascript/examples/unicode-strings
export function utoa(data: string): string {
return btoa(unescape(encodeURIComponent(data)))
}

export function atou(base64: string): string {
return decodeURIComponent(escape(atob(base64)))
}