PluginProbe
Code Snippets / 3.6.3
Code Snippets v3.6.3
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / js / utils / files.ts

files.ts in Code Snippets 3.6.3, at js/utils/files.ts

36 lines 1.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Snippet } from '../types/Snippet'
2 import { getSnippetType } from './snippets'
3
4 const SECOND_IN_MS = 1000
5 const TIMEOUT_SECONDS = 40
6
7 const MIME_INFO = <const> {
8 php: ['php', 'text/php'],
9 html: ['php', 'text/php'],
10 css: ['css', 'text/css'],
11 js: ['js', 'text/javascript'],
12 json: ['json', 'application/json']
13 }
14
15 export const downloadAsFile = (content: BlobPart, filename: string, type: string) => {
16 const link = document.createElement('a')
17 link.download = filename
18 link.href = URL.createObjectURL(new Blob([content], { type }))
19
20 setTimeout(() => URL.revokeObjectURL(link.href), TIMEOUT_SECONDS * SECOND_IN_MS)
21 setTimeout(() => link.click(), 0)
22 }
23
24 export const downloadSnippetExportFile = (
25 content: BlobPart,
26 { id, name, scope }: Snippet,
27 type?: keyof typeof MIME_INFO
28 ) => {
29 const [ext, mimeType] = MIME_INFO[type ?? getSnippetType(scope)]
30
31 const title = name.toLowerCase().replace(/[^\w-]+/g, '-') ?? `snippet-${id}`
32 const filename = `${title}.code-snippets.${ext}`
33
34 downloadAsFile(content, filename, mimeType)
35 }
36