focalboard/webapp/src/archiver.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-21 03:54:39 +02:00
import {BoardTree} from './viewModel/boardTree'
2020-10-20 21:52:56 +02:00
import mutator from './mutator'
2020-10-21 03:58:48 +02:00
import {IBlock} from './blocks/block'
2020-10-20 21:52:56 +02:00
import {Utils} from './utils'
2020-10-08 18:21:27 +02:00
interface Archive {
2020-10-20 21:50:53 +02:00
version: number
date: number
2020-10-20 23:32:39 +02:00
blocks: readonly IBlock[]
2020-10-08 18:21:27 +02:00
}
class Archiver {
2020-10-20 22:36:54 +02:00
static async exportBoardTree(boardTree: BoardTree): Promise<void> {
2020-10-20 21:50:53 +02:00
const blocks = boardTree.allBlocks
const archive: Archive = {
version: 1,
date: Date.now(),
blocks,
}
this.exportArchive(archive)
}
2020-10-20 22:36:54 +02:00
static async exportFullArchive(): Promise<void> {
2020-10-20 21:50:53 +02:00
const blocks = await mutator.exportFullArchive()
const archive: Archive = {
version: 1,
date: Date.now(),
blocks,
}
this.exportArchive(archive)
}
2020-10-20 22:36:54 +02:00
private static exportArchive(archive: Archive): void {
2020-10-20 21:50:53 +02:00
const content = JSON.stringify(archive)
const date = new Date()
const filename = `archive-${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.octo`
const link = document.createElement('a')
2020-10-20 21:52:56 +02:00
link.style.display = 'none'
2020-10-20 21:50:53 +02:00
// const file = new Blob([content], { type: "text/json" })
// link.href = URL.createObjectURL(file)
link.href = 'data:text/json,' + encodeURIComponent(content)
link.download = filename
document.body.appendChild(link) // FireFox support
link.click()
// TODO: Remove or reuse link
}
static importFullArchive(onComplete?: () => void): void {
const input = document.createElement('input')
2020-10-20 21:52:56 +02:00
input.type = 'file'
input.accept = '.octo'
2020-10-20 21:50:53 +02:00
input.onchange = async () => {
const file = input.files[0]
const contents = await (new Response(file)).text()
Utils.log(`Import ${contents.length} bytes.`)
const archive: Archive = JSON.parse(contents)
const {blocks} = archive
const date = new Date(archive.date)
Utils.log(`Import archive, version: ${archive.version}, date/time: ${date.toLocaleString()}, ${blocks.length} block(s).`)
// Basic error checking
const filteredBlocks = blocks.filter((o) => {
if (!o.id) {
return false
}
return true
2020-10-20 21:52:56 +02:00
})
2020-10-20 21:50:53 +02:00
Utils.log(`Import ${filteredBlocks.length} filtered blocks.`)
await mutator.importFullArchive(filteredBlocks)
Utils.log('Import completed')
onComplete?.()
2020-10-20 21:52:56 +02:00
}
2020-10-20 21:50:53 +02:00
2020-10-20 21:52:56 +02:00
input.style.display = 'none'
2020-10-20 21:50:53 +02:00
document.body.appendChild(input)
input.click()
// TODO: Remove or reuse input
}
2020-10-08 18:21:27 +02:00
}
2020-10-20 21:50:53 +02:00
export {Archiver}