-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (49 loc) · 1.99 KB
/
index.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { gzipSync, gunzipSync, strToU8, strFromU8 } from "fflate"
import { base64ToBytes, bytesToBase64 } from "./bytesToBase64"
export function getCompressedBase64SnippetString(text: string) {
// Compress the input string
const compressedData = gzipSync(strToU8(text.trim()), {
mtime: 1739063707691, // Date.now() when i wrote this line, ensures consistent output
})
// Convert to base64
const base64Data = bytesToBase64(compressedData)
return base64Data
}
export function getBase64PoundSnippetString(text: string) {
const base64Data = getCompressedBase64SnippetString(text.trim())
return `#data:application/gzip;base64,${base64Data}`
}
export function getUncompressedSnippetString(
base64CompressedSnippetString: string,
) {
const base64Data = base64CompressedSnippetString.split(",").pop()
const compressedData = base64ToBytes(base64Data!)
const uncompressedData = gunzipSync(compressedData)
return strFromU8(uncompressedData)
}
export function createSvgUrl(
tscircuitCode: string,
svgType: "pcb" | "schematic" | "3d",
) {
const base64Data = getCompressedBase64SnippetString(tscircuitCode)
return `https://svg.tscircuit.com/?svg_type=${svgType}&code=${encodeURIComponent(base64Data)}`
}
export function createBrowserPreviewUrl(
tscircuitCode: string,
view?: "pcb" | "schematic" | "3d",
) {
const base64Data = getCompressedBase64SnippetString(tscircuitCode)
return `https://browser-preview.tscircuit.com/?view=${view}&code=${encodeURIComponent(base64Data)}`
}
export function createPngUrl(
tscircuitCode: string,
view: "pcb" | "schematic" | "3d",
) {
const base64Data = getCompressedBase64SnippetString(tscircuitCode)
return `https://png.tscircuit.com/?view=${view}&code=${encodeURIComponent(base64Data)}`
}
export function createSnippetUrl(text: string, snippet_type?: string): string {
// Construct the URL
const typeParam = snippet_type ? `&snippet_type=${snippet_type}` : ""
return `https://tscircuit.com/editor?${typeParam}${getBase64PoundSnippetString(text)}`
}