Skip to content

Commit

Permalink
#653 Generate just an OBF file when exporting a single board
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvansson committed Jul 3, 2020
1 parent 06c0075 commit 4673342
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/components/Settings/Export/Export.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -49,10 +51,6 @@ export class ExportContainer extends PureComponent {
doneCallback();
};

openboardExportAdapter(boards) {
return boards;
}

render() {
const { boards, intl, history } = this.props;

Expand Down
43 changes: 40 additions & 3 deletions src/components/Settings/Export/Export.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
writeCvaFile
} from '../../../cordova-util';
import { getStore } from '../../../store';
import * as _ from 'lodash';

pdfMake.vfs = pdfFonts.pdfMake.vfs;

Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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<void>} 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 = {};
Expand All @@ -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;
Expand Down

0 comments on commit 4673342

Please sign in to comment.