import { getSnippetType } from './snippets/snippets'
import type { SnippetsExport } from '../types/schema/SnippetsExport'
import type { Snippet } from '../types/Snippet'
const SECOND_IN_MS = 1000
const TIMEOUT_SECONDS = 40
const JSON_INDENT_SPACES = 2
const MIME_INFO = <const> {
php: ['php', 'text/php'],
html: ['php', 'text/php'],
css: ['css', 'text/css'],
js: ['js', 'text/javascript'],
cond: ['json', 'application/json'],
json: ['json', 'application/json']
}
export const downloadAsFile = (content: BlobPart, filename: string, type: string) => {
const link = document.createElement('a')
link.download = filename
link.href = URL.createObjectURL(new Blob([content], { type }))
setTimeout(() => URL.revokeObjectURL(link.href), TIMEOUT_SECONDS * SECOND_IN_MS)
setTimeout(() => link.click(), 0)
}
export const downloadSnippetExportFile = (
content: SnippetsExport | string,
{ id, name, scope }: Snippet,
type?: keyof typeof MIME_INFO
) => {
const sanitizedName = name.toLowerCase().replace(/[^\w-]+/g, '-').trim()
const title = '' === sanitizedName ? `snippet-${id}` : sanitizedName
if ('string' === typeof content) {
const [ext, mimeType] = MIME_INFO[type ?? getSnippetType({ scope })]
const filename = `${title}.code-snippets.${ext}`
downloadAsFile(content, filename, mimeType)
} else {
const filename = `${title}.code-snippets.json`
downloadAsFile(JSON.stringify(content, undefined, JSON_INDENT_SPACES), filename, 'application/json')
}
}
| nama | waktu | ukuran | owner | perm | aksi |
|---|---|---|---|---|---|
| .. | |||||
| snippets/ | 2026-07-30 21:37 | 6.43 KB | 76129f60960:kunden | dxwrx-r--r | |
| Linter.ts | 2026-07-30 21:37 | 4.44 KB | 76129f60960:kunden | -xw-x----- | |
| errors.ts | 2026-07-30 21:37 | 80 B | 76129f60960:kunden | -xw-x----- | |
| files.ts | 2026-07-30 21:37 | 1.44 KB | 76129f60960:kunden | -xw-x----- | |
| hooks.ts | 2026-07-30 21:37 | 498 B | 76129f60960:kunden | -xw-x----- | |
| restAPI.ts | 2026-07-30 21:37 | 473 B | 76129f60960:kunden | -xw-x----- | |
| screen.ts | 2026-07-30 21:37 | 249 B | 76129f60960:kunden | -xw-x----- | |
| text.ts | 2026-07-30 21:37 | 787 B | 76129f60960:kunden | -xw-x----- | |