Current Path : /storage/v11800/shoptimizer-latest/public_html/wp-content/plugins/code-snippets/js/utils/ |
Linux v11800 5.3.0-1023-aws #25~18.04.1-Ubuntu SMP Fri Jun 5 15:19:18 UTC 2020 aarch64
|
Current File : /storage/v11800/shoptimizer-latest/public_html/wp-content/plugins/code-snippets/js/utils/files.ts |
import { Snippet } from '../types/Snippet'
import { getSnippetType } from './snippets'
const SECOND_IN_MS = 1000
const TIMEOUT_SECONDS = 40
const MIME_INFO = <const> {
php: ['php', 'text/php'],
html: ['php', 'text/php'],
css: ['css', 'text/css'],
js: ['js', 'text/javascript'],
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: BlobPart,
{ id, name, scope }: Snippet,
type?: keyof typeof MIME_INFO
) => {
const [ext, mimeType] = MIME_INFO[type ?? getSnippetType(scope)]
const title = name.toLowerCase().replace(/[^\w-]+/g, '-') ?? `snippet-${id}`
const filename = `${title}.code-snippets.${ext}`
downloadAsFile(content, filename, mimeType)
}