/
githubmirror
/
obsidian-clipper
Обзор
Документация
Войти
/
githubmirror
/
obsidian-clipper
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/utils/file-utils.ts
88 строк
2 KB
Steph Ango
Save to .md
05 дек 2024, 06:22
05 дек 2024, 06:22
c9ba937
Код
Авторство
О чём код?
import browser from './browser-polyfill'; import { detectBrowser } from './browser-detection'; export interface SaveFileOptions { content: string; fileName: string; mimeType?: string; tabId?: number; onError?: (error: Error) => void; } export function base64EncodeUnicode(str: string): string { const utf8Bytes = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(parseInt(p1, 16)) ); return btoa(utf8Bytes); } export async function saveFile({ content, fileName, mimeType = 'text/markdown', tabId, onError }: SaveFileOptions): Promise<void> { try { if (mimeType === 'text/markdown' && !fileName.toLowerCase().endsWith('.md')) { fileName = `${fileName}.md`; } const browserType = await detectBrowser(); const isSafari = ['ios', 'mobile-ios', 'ipad-os', 'safari', 'mobile-safari'].includes(browserType); if (isSafari) { const blob = new Blob([content], { type: 'application/json' }); const file = new File([blob], fileName, { type: 'application/json' }); const dataUri = `data:${mimeType};charset=utf-8,${encodeURIComponent(content)}`; // Use share API if there is no tab ID, e.g. in settings pages if (!tabId) { if (navigator.share) { try { await navigator.share({ files: [file], text: fileName }); } catch (error) { console.error('Error sharing:', error); // Fallback to opening in a new tab if sharing fails window.open(dataUri); } } else { // Fallback for older iOS versions window.open(dataUri); } throw new Error('Tab ID is required for saving files in Safari'); } await browser.scripting.executeScript({ target: { tabId }, func: (fileName: string, dataUri: string) => { const a = document.createElement('a'); a.href = dataUri; a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); }, args: [fileName, dataUri] }); } else { const blob = new Blob([content], { type: `${mimeType};charset=utf-8` }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } } catch (error) { console.error('Failed to save file:', error); if (onError) { onError(error as Error); } } }