diff --git a/domain/section/pdfjs/pdfjs.go b/domain/section/pdfjs/pdfjs.go index a52ecda4..960ec718 100644 --- a/domain/section/pdfjs/pdfjs.go +++ b/domain/section/pdfjs/pdfjs.go @@ -46,10 +46,10 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R // Render just sends back HMTL as-is. func (*Provider) Render(ctx *provider.Context, config, data string) string { - return data + return config } // Refresh just sends back data as-is. func (*Provider) Refresh(ctx *provider.Context, config, data string) string { - return data + return config } diff --git a/gui/app/components/section/base-editor-inline.js b/gui/app/components/section/base-editor-inline.js index e88ebb3a..31bde62f 100644 --- a/gui/app/components/section/base-editor-inline.js +++ b/gui/app/components/section/base-editor-inline.js @@ -12,20 +12,14 @@ import $ from 'jquery'; import { empty } from '@ember/object/computed'; import { computed } from '@ember/object'; -import { inject as service } from '@ember/service'; import Modals from '../../mixins/modal'; import Notifier from '../../mixins/notifier'; import Component from '@ember/component'; export default Component.extend(Modals, Notifier, { - appMeta: service(), - session: service(), - documentSvc: service('document'), busy: false, mousetrap: null, showLinkModal: false, - files: null, - downloadQuery: '', hasNameError: empty('page.title'), hasDescError: empty('page.excerpt'), pageId: computed('page', function () { diff --git a/gui/app/components/section/pdf/type-editor.js b/gui/app/components/section/pdf/type-editor.js index 7c229d35..317da9c0 100644 --- a/gui/app/components/section/pdf/type-editor.js +++ b/gui/app/components/section/pdf/type-editor.js @@ -18,22 +18,59 @@ export default Component.extend({ editorId: computed('page', function () { let page = this.get('page'); return `pdf-editor-${page.id}`; - }), + }), + pdfOption: null, + pdfName: '', init() { this._super(...arguments); - this.set('pageBody', this.get('meta.rawBody')); + this.pdfOption = {}; }, - didInsertElement() { + didReceiveAttrs() { + let pdfOption = {}; + + try { + pdfOption = JSON.parse(this.get('meta.config')); + } catch (e) {} // eslint-disable-line no-empty + + if (_.isEmpty(pdfOption)) { + pdfOption = { + height: 600, + sidebar: 'none', // none, bookmarks, thumbs + startPage: 1, + fileId: '' + }; + } + + this.set('pdfOption', pdfOption); + this.setPDF(); + }, + + didUpdateAttrs() { this._super(...arguments); + this.setPDF(); }, - willDestroyElement() { - this._super(...arguments); + setPDF() { + let files = this.get('attachments'); + if (!_.isArray(files)) return; + + for (let i=0; i < files.length; i++) { + if (_.endsWith(files[i].get('extension'), 'pdf') && + files[i].get('pageId') === this.get('page.id')) { + this.set('pdfName', files[i].get('filename')); + this.set('pdfOption.fileId', files[i].get('id')); + break; + } + } }, actions: { + onSetSidebar(e) { + this.set('pdfOption.sidebar', e); + }, + isDirty() { return this.get('isDirty'); }, @@ -44,11 +81,14 @@ export default Component.extend({ }, onAction(title) { + let config = this.get('pdfOption'); let page = this.get('page'); let meta = this.get('meta'); - meta.set('rawBody', ''); + page.set('title', title); - page.set('body', meta.get('rawBody')); + page.set('body', JSON.stringify(config)); + meta.set('config', JSON.stringify(config)); + meta.set('rawBody', JSON.stringify(config)); let cb = this.get('onAction'); cb(page, meta); diff --git a/gui/app/components/section/pdf/type-renderer.js b/gui/app/components/section/pdf/type-renderer.js index 058138db..27efee07 100644 --- a/gui/app/components/section/pdf/type-renderer.js +++ b/gui/app/components/section/pdf/type-renderer.js @@ -9,9 +9,14 @@ // // https://documize.com +import { inject as service } from '@ember/service'; import Component from '@ember/component'; export default Component.extend({ + appMeta: service(), + session: service(), + + // PDF URL is calculated pdfUrl: '', // https://github.com/mozilla/pdf.js/wiki/Viewer-options @@ -26,10 +31,35 @@ export default Component.extend({ return; } - let page = this.get('page'); - let rawBody = page.get('body'); + let pdfOption = {}; - this.set('pdfUrl', encodeURIComponent('https://demo.test:5001/api/public/attachment/4Tec34w8/bhird7crtr314et90n7g?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiJkZW1vIiwiZXhwIjoxNTg2MzQ1ODA2LCJpc3MiOiJEb2N1bWl6ZSIsIm9yZyI6IjRUZWMzNHc4Iiwic3ViIjoid2ViYXBwIiwidXNlciI6ImlKZGY2cVVXIn0.YPrf_xlNJZVK1Ikt3S0HJagIqqnVjxwepUVQ44VYXR4')); + try { + pdfOption = JSON.parse(this.get('page.body')); + } catch (e) {} // eslint-disable-line no-empty + + if (_.isEmpty(pdfOption)) { + pdfOption = { + height: 600, + sidebar: 'none', // none, bookmarks, thumbs + startPage: 1, + }; + } + + this.set('pdfOption', pdfOption); + + let endpoint = this.get('appMeta.endpoint'); + let orgId = this.get('appMeta.orgId'); + let fileId = this.get('pdfOption.fileId'); + + // For authenticated users we send server auth token. + let qry = ''; + if (this.get('session.hasSecureToken')) { + qry = '?secure=' + this.get('session.secureToken'); + } else if (this.get('session.authenticated')) { + qry = '?token=' + this.get('session.authToken'); + } + + this.set('pdfUrl', encodeURIComponent(`${endpoint}/public/attachment/${orgId}/${fileId}${qry}`)); }, didInsertElement() { diff --git a/gui/app/templates/components/section/pdf/type-editor.hbs b/gui/app/templates/components/section/pdf/type-editor.hbs index 4367f2ad..af0b0826 100644 --- a/gui/app/templates/components/section/pdf/type-editor.hbs +++ b/gui/app/templates/components/section/pdf/type-editor.hbs @@ -1,3 +1,37 @@ {{#section/base-editor-inline document=document folder=folder page=page tip="Select PDF to render" isDirty=(action "isDirty") onCancel=(action "onCancel") onAction=(action "onAction")}} +