Fixed pdf and pandoc export

This commit is contained in:
benweet 2018-08-21 10:41:47 +01:00
parent 87f281c700
commit e76f28294b
2 changed files with 59 additions and 54 deletions

View File

@ -43,37 +43,40 @@ export default modalTemplate({
const currentFile = this.$store.getters['file/current']; const currentFile = this.$store.getters['file/current'];
const currentContent = this.$store.getters['content/current']; const currentContent = this.$store.getters['content/current'];
const { selectedFormat } = this; const { selectedFormat } = this;
const [sponsorToken, token] = await this.$store.dispatch('queue/enqueue', () => Promise.all([ this.$store.dispatch('queue/enqueue', async () => {
Promise.resolve().then(() => { const [sponsorToken, token] = await Promise.all([
const tokenToRefresh = this.$store.getters['workspace/sponsorToken']; Promise.resolve().then(() => {
return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh); const tokenToRefresh = this.$store.getters['workspace/sponsorToken'];
}), return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh);
sponsorSvc.getToken(), }),
])); sponsorSvc.getToken(),
try { ]);
const { body } = await networkSvc.request({
method: 'POST', try {
url: 'pandocExport', const { body } = await networkSvc.request({
params: { method: 'POST',
token, url: 'pandocExport',
idToken: sponsorToken && sponsorToken.idToken, params: {
format: selectedFormat, token,
options: JSON.stringify(this.$store.getters['data/computedSettings'].pandoc), idToken: sponsorToken && sponsorToken.idToken,
metadata: JSON.stringify(currentContent.properties), format: selectedFormat,
}, options: JSON.stringify(this.$store.getters['data/computedSettings'].pandoc),
body: JSON.stringify(editorSvc.getPandocAst()), metadata: JSON.stringify(currentContent.properties),
blob: true, },
timeout: 60000, body: JSON.stringify(editorSvc.getPandocAst()),
}); blob: true,
FileSaver.saveAs(body, `${currentFile.name}.${selectedFormat}`); timeout: 60000,
} catch (err) { });
if (err.status === 401) { FileSaver.saveAs(body, `${currentFile.name}.${selectedFormat}`);
this.$store.dispatch('modal/open', 'sponsorOnly'); } catch (err) {
} else { if (err.status === 401) {
console.error(err); // eslint-disable-line no-console this.$store.dispatch('modal/open', 'sponsorOnly');
this.$store.dispatch('notification/error', err); } else {
console.error(err); // eslint-disable-line no-console
this.$store.dispatch('notification/error', err);
}
} }
} });
}, },
}, },
}); });

View File

@ -36,8 +36,8 @@ export default modalTemplate({
async resolve() { async resolve() {
this.config.resolve(); this.config.resolve();
const currentFile = this.$store.getters['file/current']; const currentFile = this.$store.getters['file/current'];
const [sponsorToken, token, html] = await this.$store this.$store.dispatch('queue/enqueue', async () => {
.dispatch('queue/enqueue', () => Promise.all([ const [sponsorToken, token, html] = await Promise.all([
Promise.resolve().then(() => { Promise.resolve().then(() => {
const tokenToRefresh = this.$store.getters['workspace/sponsorToken']; const tokenToRefresh = this.$store.getters['workspace/sponsorToken'];
return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh); return tokenToRefresh && googleHelper.refreshToken(tokenToRefresh);
@ -48,29 +48,31 @@ export default modalTemplate({
this.allTemplatesById[this.selectedTemplate], this.allTemplatesById[this.selectedTemplate],
true, true,
), ),
])); ]);
try {
const { body } = await networkSvc.request({ try {
method: 'POST', const { body } = await networkSvc.request({
url: 'pdfExport', method: 'POST',
params: { url: 'pdfExport',
token, params: {
idToken: sponsorToken && sponsorToken.idToken, token,
options: JSON.stringify(this.$store.getters['data/computedSettings'].wkhtmltopdf), idToken: sponsorToken && sponsorToken.idToken,
}, options: JSON.stringify(this.$store.getters['data/computedSettings'].wkhtmltopdf),
body: html, },
blob: true, body: html,
timeout: 60000, blob: true,
}); timeout: 60000,
FileSaver.saveAs(body, `${currentFile.name}.pdf`); });
} catch (err) { FileSaver.saveAs(body, `${currentFile.name}.pdf`);
if (err.status === 401) { } catch (err) {
this.$store.dispatch('modal/open', 'sponsorOnly'); if (err.status === 401) {
} else { this.$store.dispatch('modal/open', 'sponsorOnly');
console.error(err); // eslint-disable-line no-console } else {
this.$store.dispatch('notification/error', err); console.error(err); // eslint-disable-line no-console
this.$store.dispatch('notification/error', err);
}
} }
} });
}, },
}, },
}); });