From e76f28294bda15eb98f3975f81a78e408b77e7f2 Mon Sep 17 00:00:00 2001 From: benweet Date: Tue, 21 Aug 2018 10:41:47 +0100 Subject: [PATCH] Fixed pdf and pandoc export --- src/components/modals/PandocExportModal.vue | 63 +++++++++++---------- src/components/modals/PdfExportModal.vue | 50 ++++++++-------- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/components/modals/PandocExportModal.vue b/src/components/modals/PandocExportModal.vue index 8cac3cec..089f1e90 100644 --- a/src/components/modals/PandocExportModal.vue +++ b/src/components/modals/PandocExportModal.vue @@ -43,37 +43,40 @@ export default modalTemplate({ const currentFile = this.$store.getters['file/current']; const currentContent = this.$store.getters['content/current']; const { selectedFormat } = this; - const [sponsorToken, token] = await this.$store.dispatch('queue/enqueue', () => Promise.all([ - Promise.resolve().then(() => { - const tokenToRefresh = this.$store.getters['workspace/sponsorToken']; - return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh); - }), - sponsorSvc.getToken(), - ])); - try { - const { body } = await networkSvc.request({ - method: 'POST', - url: 'pandocExport', - params: { - token, - idToken: sponsorToken && sponsorToken.idToken, - format: selectedFormat, - options: JSON.stringify(this.$store.getters['data/computedSettings'].pandoc), - metadata: JSON.stringify(currentContent.properties), - }, - body: JSON.stringify(editorSvc.getPandocAst()), - blob: true, - timeout: 60000, - }); - FileSaver.saveAs(body, `${currentFile.name}.${selectedFormat}`); - } catch (err) { - if (err.status === 401) { - this.$store.dispatch('modal/open', 'sponsorOnly'); - } else { - console.error(err); // eslint-disable-line no-console - this.$store.dispatch('notification/error', err); + this.$store.dispatch('queue/enqueue', async () => { + const [sponsorToken, token] = await Promise.all([ + Promise.resolve().then(() => { + const tokenToRefresh = this.$store.getters['workspace/sponsorToken']; + return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh); + }), + sponsorSvc.getToken(), + ]); + + try { + const { body } = await networkSvc.request({ + method: 'POST', + url: 'pandocExport', + params: { + token, + idToken: sponsorToken && sponsorToken.idToken, + format: selectedFormat, + options: JSON.stringify(this.$store.getters['data/computedSettings'].pandoc), + metadata: JSON.stringify(currentContent.properties), + }, + body: JSON.stringify(editorSvc.getPandocAst()), + blob: true, + timeout: 60000, + }); + FileSaver.saveAs(body, `${currentFile.name}.${selectedFormat}`); + } catch (err) { + if (err.status === 401) { + this.$store.dispatch('modal/open', 'sponsorOnly'); + } else { + console.error(err); // eslint-disable-line no-console + this.$store.dispatch('notification/error', err); + } } - } + }); }, }, }); diff --git a/src/components/modals/PdfExportModal.vue b/src/components/modals/PdfExportModal.vue index abc59518..1e11d139 100644 --- a/src/components/modals/PdfExportModal.vue +++ b/src/components/modals/PdfExportModal.vue @@ -36,8 +36,8 @@ export default modalTemplate({ async resolve() { this.config.resolve(); const currentFile = this.$store.getters['file/current']; - const [sponsorToken, token, html] = await this.$store - .dispatch('queue/enqueue', () => Promise.all([ + this.$store.dispatch('queue/enqueue', async () => { + const [sponsorToken, token, html] = await Promise.all([ Promise.resolve().then(() => { const tokenToRefresh = this.$store.getters['workspace/sponsorToken']; return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh); @@ -48,29 +48,31 @@ export default modalTemplate({ this.allTemplatesById[this.selectedTemplate], true, ), - ])); - try { - const { body } = await networkSvc.request({ - method: 'POST', - url: 'pdfExport', - params: { - token, - idToken: sponsorToken && sponsorToken.idToken, - options: JSON.stringify(this.$store.getters['data/computedSettings'].wkhtmltopdf), - }, - body: html, - blob: true, - timeout: 60000, - }); - FileSaver.saveAs(body, `${currentFile.name}.pdf`); - } catch (err) { - if (err.status === 401) { - this.$store.dispatch('modal/open', 'sponsorOnly'); - } else { - console.error(err); // eslint-disable-line no-console - this.$store.dispatch('notification/error', err); + ]); + + try { + const { body } = await networkSvc.request({ + method: 'POST', + url: 'pdfExport', + params: { + token, + idToken: sponsorToken && sponsorToken.idToken, + options: JSON.stringify(this.$store.getters['data/computedSettings'].wkhtmltopdf), + }, + body: html, + blob: true, + timeout: 60000, + }); + FileSaver.saveAs(body, `${currentFile.name}.pdf`); + } catch (err) { + if (err.status === 401) { + this.$store.dispatch('modal/open', 'sponsorOnly'); + } else { + console.error(err); // eslint-disable-line no-console + this.$store.dispatch('notification/error', err); + } } - } + }); }, }, });