Skip to content

Commit 9797ab7

Browse files
committed
fix: use vue-router friendly btoa/atob
1 parent 6640611 commit 9797ab7

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

common/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,23 @@ export function savePref (key, value) {
5656
localStorage.setItem(PREF_KEY, JSON.stringify(pref))
5757
}
5858

59+
function urlBtoA (binary) {
60+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_')
61+
}
62+
63+
function urlAtoB (base64) {
64+
return atob(base64.replace(/-/g, '+').replace(/_/g, '/'))
65+
}
66+
5967
export function utoa (data) {
6068
const buffer = strToU8(data)
6169
const zipped = zlibSync(buffer, { level: 9 })
6270
const binary = strFromU8(zipped, true)
63-
return btoa(binary)
71+
return urlBtoA(binary)
6472
}
6573

6674
export function atou (base64) {
67-
const binary = atob(base64)
75+
const binary = urlAtoB(base64)
6876

6977
// zlib header (x78), level 9 (xDA)
7078
if (binary.startsWith('\x78\xDA')) {

components/OneDemo.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
<slot name="desc"/>
3131
</section>
3232
<section class="actions">
33+
<veui-button
34+
v-tooltip="t('playInPlayground')"
35+
ui="icon"
36+
@click="play('VEUI')"
37+
>
38+
<veui-icon name="external-link"/>
39+
</veui-button>
3340
<veui-button
3441
v-tooltip="t('playInCodeSandbox')"
3542
ui="icon"
@@ -129,6 +136,7 @@ import OneEditLink from './OneEditLink'
129136
import OneRepl from './OneRepl'
130137
import OneFocus from './OneFocus'
131138
import 'veui-theme-dls-icons/copy'
139+
import 'veui-theme-dls-icons/external-link'
132140
133141
Vue.use(toast)
134142
@@ -194,11 +202,6 @@ export default {
194202
track('play', { vendor, path: this.path })
195203
},
196204
trackCopy (action) {
197-
if (this.copied) {
198-
return
199-
}
200-
201-
this.copied = true
202205
track('copy', { from: 'demo', path: this.path, action })
203206
},
204207
async copy () {

one/docs/play.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@ export default {
4141
methods: {
4242
deserialize () {
4343
try {
44-
const stringified = atou(location.hash.slice(1))
45-
const { code, theme } = stringified ? JSON.parse(stringified) : {}
44+
const hash = atou(location.hash.slice(1))
45+
const { code, theme } = hash ? JSON.parse(hash) : {}
4646
this.code = code || ''
4747
config.value.theme = theme || ''
4848
} catch (e) {
4949
console.error(e)
5050
}
5151
},
5252
serialize () {
53-
history.replaceState(
54-
{},
55-
'',
56-
`#${utoa(JSON.stringify({ code: this.code, theme: config.value.theme || undefined }))}`
53+
const hash = utoa(
54+
JSON.stringify({
55+
code: this.code,
56+
theme: config.value.theme || undefined
57+
})
5758
)
59+
history.replaceState({}, '', `#${hash}`)
5860
},
5961
handleCodeChange (code) {
6062
this.code = code

0 commit comments

Comments
 (0)