From 4673342682bdf5172fa3eb2a9a4466c475cd2507 Mon Sep 17 00:00:00 2001 From: Simon Richard Date: Thu, 2 Jul 2020 21:56:45 -0400 Subject: [PATCH] #653 Generate just an OBF file when exporting a single board --- .../Settings/Export/Export.container.js | 8 ++-- .../Settings/Export/Export.helpers.js | 43 +++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/components/Settings/Export/Export.container.js b/src/components/Settings/Export/Export.container.js index 6358865e4..d10dfb5f4 100644 --- a/src/components/Settings/Export/Export.container.js +++ b/src/components/Settings/Export/Export.container.js @@ -33,7 +33,9 @@ export class ExportContainer extends PureComponent { } const { boards, intl, activeBoardId, showNotification } = this.props; - if (type !== 'pdf' && !singleBoard) { + if (type === 'openboard' && singleBoard) { + await EXPORT_HELPERS.openboardExportAdapter(singleBoard, intl); + } else if (type !== 'pdf' && !singleBoard) { await EXPORT_HELPERS[exportConfig.callback](boards, intl); } else { if (singleBoard) { @@ -49,10 +51,6 @@ export class ExportContainer extends PureComponent { doneCallback(); }; - openboardExportAdapter(boards) { - return boards; - } - render() { const { boards, intl, history } = this.props; diff --git a/src/components/Settings/Export/Export.helpers.js b/src/components/Settings/Export/Export.helpers.js index 9c907f46c..8768cfe41 100644 --- a/src/components/Settings/Export/Export.helpers.js +++ b/src/components/Settings/Export/Export.helpers.js @@ -25,6 +25,7 @@ import { writeCvaFile } from '../../../cordova-util'; import { getStore } from '../../../store'; +import * as _ from 'lodash'; pdfMake.vfs = pdfFonts.pdfMake.vfs; @@ -97,7 +98,7 @@ function getBase64Image(base64Str = '') { * an OBZ archive. */ // TODO: Embed sounds as well. -async function boardToOBF(boardsMap, board = {}, intl, embed = false) { +async function boardToOBF(boardsMap, board = {}, intl, { embed = false }) { if (!board.tiles || board.tiles.length < 1) { return { obf: null, images: null }; } @@ -434,7 +435,41 @@ const getDisplaySettings = () => { return displaySettings; }; -export async function openboardExportAdapter(boards = [], intl) { +/** + * Export one or several boards in the Open Board Format. If we specifically + * want to export a single board, we generate a single OBF file, otherwise + * we generate an OBZ archive. + * + * @param boardOrBoards A board, or an array of boards. + * @param intl + * @returns {Promise} Nothing. + */ +export async function openboardExportAdapter(boardOrBoards, intl) { + return _.isArray(boardOrBoards) + ? openboardExportManyAdapter(boardOrBoards, intl) + : openboardExportOneAdapter(boardOrBoards, intl); +} + +export async function openboardExportOneAdapter(board, intl) { + const { obf } = await boardToOBF({ [board.id]: board }, board, intl, { + embed: true + }); + const content = new Blob([JSON.stringify(obf, null, 2)], { + type: 'application/json' + }); + + if (content) { + const prefix = moment().format('YYYY-MM-DD_HH-mm-ss-') + board.name + ' '; + if (isCordova()) { + requestCvaWritePermissions(); + writeCvaFile('Download/' + prefix + 'board.obf', content); + } else { + saveAs(content, prefix + 'board.obf'); + } + } +} + +export async function openboardExportManyAdapter(boards = [], intl) { const boardsLength = boards.length; const boardsForManifest = {}; const imagesMap = {}; @@ -448,7 +483,9 @@ export async function openboardExportAdapter(boards = [], intl) { for (let i = 0; i < boardsLength; i++) { const board = boards[i]; const boardMapFilename = `boards/${board.id}.obf`; - const { obf, images } = await boardToOBF(boardsMap, board, intl); + const { obf, images } = await boardToOBF(boardsMap, board, intl, { + embed: false + }); if (!obf) { continue;