Skip to content

Commit 9a5bdb1

Browse files
chore(playground): support unicode in sfc playground (vuejs#3662)
atob/btoa only supports ASCII string which makes playground fails to save unicode source. This patch add unicode support by combining escape/encodeURIComponent. `escape` is chosen for backward compatibility.
1 parent de67c88 commit 9a5bdb1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/sfc-playground/src/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { reactive, watchEffect } from 'vue'
22
import { compileFile, MAIN_FILE } from './sfcCompiler'
3+
import { utoa, atou } from './utils'
34

45
const welcomeCode = `
56
<template>
@@ -38,7 +39,7 @@ let files: Store['files'] = {}
3839

3940
const savedFiles = location.hash.slice(1)
4041
if (savedFiles) {
41-
const saved = JSON.parse(atob(savedFiles))
42+
const saved = JSON.parse(atou(savedFiles))
4243
for (const filename in saved) {
4344
files[filename] = new File(filename, saved[filename])
4445
}
@@ -70,7 +71,7 @@ for (const file in store.files) {
7071
}
7172

7273
watchEffect(() => {
73-
history.replaceState({}, '', '#' + btoa(JSON.stringify(exportFiles())))
74+
history.replaceState({}, '', '#' + utoa(JSON.stringify(exportFiles())))
7475
})
7576

7677
export function exportFiles() {

packages/sfc-playground/src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ export function debounce(fn: Function, n = 100) {
77
}, n)
88
}
99
}
10+
11+
// prefer old unicode hacks for backward compatibility
12+
// https://base64.guru/developers/javascript/examples/unicode-strings
13+
export function utoa(data: string): string {
14+
return btoa(unescape(encodeURIComponent(data)))
15+
}
16+
17+
export function atou(base64: string): string {
18+
return decodeURIComponent(escape(atob(base64)))
19+
}

0 commit comments

Comments
 (0)