PluginProbe
Code Snippets / 3.5.0
Code Snippets v3.5.0
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 / general.ts

general.ts in Code Snippets 3.5.0, at js/utils/general.ts

40 lines 1.1 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
8 const MIME_INFO = <const> {
9 php: ['php', 'text/php'],
10 html: ['php', 'text/php'],
11 css: ['css', 'text/css'],
12 js: ['js', 'text/javascript'],
13 json: ['json', 'application/json']
14 }
15
16 export const isNetworkAdmin = () =>
17 window.pagenow.endsWith('-network')
18
19 export const downloadAsFile = (content: BlobPart, filename: string, type: string) => {
20 const link = document.createElement('a')
21 link.download = filename
22 link.href = URL.createObjectURL(new Blob([content], { type }))
23
24 setTimeout(() => URL.revokeObjectURL(link.href), TIMEOUT_SECONDS * SECOND_IN_MS)
25 setTimeout(() => link.click(), 0)
26 }
27
28 export const downloadSnippetExportFile = (
29 content: BlobPart,
30 { id, name, scope }: Snippet,
31 type?: keyof typeof MIME_INFO
32 ) => {
33 const [ext, mimeType] = MIME_INFO[type ?? getSnippetType(scope)]
34
35 const title = name.toLowerCase().replace(/[^\w-]+/g, '-') ?? `snippet-${id}`
36 const filename = `${title}.code-snippets.${ext}`
37
38 downloadAsFile(content, filename, mimeType)
39 }
40