-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathgetInitialCode.ts
More file actions
31 lines (27 loc) · 1.05 KB
/
Copy pathgetInitialCode.ts
File metadata and controls
31 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import lzstring from "./vendor/lzstring.min"
/**
* Grabs the sourcecode for an example from the query hash or local storage
* @param fallback if nothing is found return this
* @param location DI'd copy of document.location
*/
export const getInitialCode = (fallback: string, location: Location) => {
// Old school support
if (location.hash.startsWith("#src")) {
const code = location.hash.replace("#src=", "").trim()
return decodeURIComponent(code)
}
// New school support
if (location.hash.startsWith("#code")) {
const code = location.hash.replace("#code/", "").trim()
let userCode = lzstring.decompressFromEncodedURIComponent(code)
// Fallback incase there is an extra level of decoding:
// https://gitter.im/Microsoft/TypeScript?at=5dc478ab9c39821509ff189a
if (!userCode) userCode = lzstring.decompressFromEncodedURIComponent(decodeURIComponent(code))
return userCode
}
// Local copy fallback
if (localStorage.getItem("sandbox-history")) {
return localStorage.getItem("sandbox-history")!
}
return fallback
}