Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit 9b6fca2

Browse files
Add common download helper (#628)
* Refactor download functions into a common helper function * Removed unused import
1 parent c0d0590 commit 9b6fca2

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const download = (data: BlobPart, fileName: string, mimeType: string): void => {
2+
const file = new Blob([data], { type: mimeType })
3+
const helperElement = document.createElement('a')
4+
helperElement.href = URL.createObjectURL(file)
5+
helperElement.download = fileName
6+
document.body.appendChild(helperElement)
7+
helperElement.click()
8+
helperElement.remove()
9+
}

src/components/editor/document-bar/revisions/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { Revision } from '../../../../api/revisions/types'
22
import { getUserById } from '../../../../api/users'
33
import { UserResponse } from '../../../../api/users/types'
4+
import { download } from '../../../common/download/download'
45

56
export const downloadRevision = (noteId: string, revision: Revision | null): void => {
67
if (!revision) {
78
return
89
}
9-
const encoded = Buffer.from(revision.content).toString('base64')
10-
const wrapper = document.createElement('a')
11-
wrapper.download = `${noteId}-${revision.timestamp}.md`
12-
wrapper.href = `data:text/markdown;charset=utf-8;base64,${encoded}`
13-
document.body.appendChild(wrapper)
14-
wrapper.click()
15-
document.body.removeChild(wrapper)
10+
download(revision.content, `${noteId}-${revision.timestamp}.md`, 'text/markdown')
1611
}
1712

1813
export const getUserDataForRevision = (authors: string[]): UserResponse[] => {

src/components/history-page/history-page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { useSelector } from 'react-redux'
55
import { deleteHistory, deleteHistoryEntry, getHistory, setHistory, updateHistoryEntry } from '../../api/history'
66
import { deleteNote } from '../../api/notes'
77
import { ApplicationState } from '../../redux'
8+
import { download } from '../common/download/download'
89

910
import {
1011
collectEntries,
11-
downloadHistory,
1212
loadHistoryFromLocalStore,
1313
mergeEntryArrays,
1414
setHistoryToLocalStore,
@@ -90,7 +90,7 @@ export const HistoryPage: React.FC = () => {
9090
version: 2,
9191
entries: mergeEntryArrays(localHistoryEntries, remoteHistoryEntries)
9292
}
93-
downloadHistory(dataObject)
93+
download(JSON.stringify(dataObject), `history_${(new Date()).getTime()}.json`, 'application/json')
9494
}, [localHistoryEntries, remoteHistoryEntries])
9595

9696
const clearHistory = useCallback(() => {

src/components/history-page/utils.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { SortModeEnum } from './sort-button/sort-button'
33
import {
44
HistoryEntry,
55
HistoryEntryOrigin,
6-
HistoryJson,
76
LocatedHistoryEntry
87
} from './history-page'
98
import { HistoryToolbarState } from './history-toolbar/history-toolbar'
@@ -126,13 +125,3 @@ export function loadHistoryFromLocalStore (): HistoryEntry[] {
126125
export function setHistoryToLocalStore (entries: HistoryEntry[]): void {
127126
window.localStorage.setItem('history', JSON.stringify(entries))
128127
}
129-
130-
export function downloadHistory (dataObject: HistoryJson): void {
131-
const data = 'data:text/json;charset=utf-8;base64,' + Buffer.from(JSON.stringify(dataObject)).toString('base64')
132-
const downloadLink = document.createElement('a')
133-
downloadLink.setAttribute('href', data)
134-
downloadLink.setAttribute('download', `history_${(new Date()).getTime()}.json`)
135-
document.body.appendChild(downloadLink)
136-
downloadLink.click()
137-
downloadLink.remove()
138-
}

0 commit comments

Comments
 (0)