finished history toolbar buttons (#117)

* added history toolbar functionality
* export now adds a version number
* renamed OldHistoryEntry to V0HistoryEntry

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
Co-authored-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
Philip Molares 2020-06-04 22:41:44 +02:00 committed by GitHub
parent d7fdb73814
commit 0d8ca681f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 197 additions and 33 deletions

View file

@ -1,5 +1,5 @@
import { HistoryEntry } from '../components/landing/pages/history/history'
import moment from 'moment'
import { HistoryEntry, HistoryJson } from '../components/landing/pages/history/history'
import { HistoryToolbarState } from '../components/landing/pages/history/history-toolbar/history-toolbar'
import { SortModeEnum } from '../components/sort-button/sort-button'
@ -58,30 +58,35 @@ export function formatHistoryDate (date: Date): string {
return moment(date).format('llll')
}
export interface OldHistoryEntry {
id: string;
text: string;
time: number;
tags: string[];
pinned: boolean;
export interface V1HistoryEntry {
id: string;
text: string;
time: number;
tags: string[];
pinned: boolean;
}
export function convertV1History (oldHistory: V1HistoryEntry[]): HistoryEntry[] {
return oldHistory.map((entry: V1HistoryEntry) => {
return {
id: entry.id,
title: entry.text,
lastVisited: moment(entry.time).toDate(),
tags: entry.tags,
pinned: entry.pinned
}
})
}
export function loadHistoryFromLocalStore (): HistoryEntry[] {
const historyJsonString = window.localStorage.getItem('history')
if (!historyJsonString) {
// if localStorage["history"] is empty we check the old localStorage["notehistory"]
// and convert it to the new format
const oldHistoryJsonString = window.localStorage.getItem('notehistory')
const oldHistory = oldHistoryJsonString ? JSON.parse(JSON.parse(oldHistoryJsonString)) as OldHistoryEntry[] : []
return oldHistory.map((entry: OldHistoryEntry) => {
return {
id: entry.id,
title: entry.text,
lastVisited: moment(entry.time).toDate(),
tags: entry.tags,
pinned: entry.pinned
}
})
const oldHistory = oldHistoryJsonString ? JSON.parse(JSON.parse(oldHistoryJsonString)) as V1HistoryEntry[] : []
return convertV1History(oldHistory)
} else {
return JSON.parse(historyJsonString) as HistoryEntry[]
}
@ -90,3 +95,13 @@ export function loadHistoryFromLocalStore (): HistoryEntry[] {
export function setHistoryToLocalStore (entries: HistoryEntry[]): void {
window.localStorage.setItem('history', JSON.stringify(entries))
}
export function downloadHistory (dataObject: HistoryJson): void {
const data = 'data:text/json;charset=utf-8;base64,' + Buffer.from(JSON.stringify(dataObject)).toString('base64')
const downloadLink = document.createElement('a')
downloadLink.setAttribute('href', data)
downloadLink.setAttribute('download', `history_${(new Date()).getTime()}.json`)
document.body.appendChild(downloadLink)
downloadLink.click()
downloadLink.remove()
}