Skip to content

Commit

Permalink
feat: add button to convert to pdf
Browse files Browse the repository at this point in the history
Integrates backend endpoint to convert docx, odt to pdf
  • Loading branch information
Yelinz committed Feb 22, 2024
1 parent 4dad0e1 commit ac5d3a4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ module.exports = function () {
};
```

### Others

- `enablePDFConversion`: Set to `true` to enable docx/odt to pdf conversion. Make sure the backend is enabled aswell.
- `namespace`: Set to API namespace
- `zipDownloadHost`: Set if the ZIP download is different
- `zipDownloadNamespace`: Set if the ZIP download is namespaced

## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.
Expand Down
7 changes: 5 additions & 2 deletions addon/components/document-view.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="uk-flex uk-flex-middle uk-padding-small">
<DocumentUploadButton
@category={{@filters.category}}
@afterUpload={{this.afterUpload}}
@afterUpload={{this.refreshDocumentList}}
data-test-upload
/>

Expand Down Expand Up @@ -86,7 +86,10 @@
{{/if}}
</div>
</div>
<DocumentsSidePanel @selectedDocuments={{this.documents.selectedDocuments}} />
<DocumentsSidePanel
@selectedDocuments={{this.documents.selectedDocuments}}
@refreshDocumentList={{this.refreshDocumentList}}
/>
</div>

<div
Expand Down
3 changes: 2 additions & 1 deletion addon/components/document-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class DocumentViewComponent extends Component {
});
}

// should this be extracted to a service or somewhere for easier state management through child components?
fetchedDocuments = trackedTask(this, this.fetchDocuments, () => [
this.sort,
this.sortDirection,
Expand Down Expand Up @@ -210,7 +211,7 @@ export default class DocumentViewComponent extends Component {
}

@action
afterUpload() {
refreshDocumentList() {
this.uploadedDocuments++;
}

Expand Down
5 changes: 4 additions & 1 deletion addon/components/documents-side-panel.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
<div class="uk-overflow-auto">
<div class="uk-padding-small">
{{#if (eq @selectedDocuments.length 1)}}
<SingleDocumentDetails @document={{object-at 0 @selectedDocuments}} />
<SingleDocumentDetails
@document={{object-at 0 @selectedDocuments}}
@refreshDocumentList={{@refreshDocumentList}}
/>
{{else}}
<MultiDocumentDetails
@selectedDocuments={{@selectedDocuments}}
Expand Down
15 changes: 14 additions & 1 deletion addon/components/single-document-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,20 @@
</ul>
</div>

<div class="uk-grid-small uk-child-width-1-2" uk-grid>
<div class="uk-grid uk-grid-small {{if this.displayConvertButton 'uk-child-width-1-3' 'uk-child-width-1-2'}}" uk-grid>
{{#if this.displayConvertButton}}
<div>
<UkButton
@size="small"
class="uk-width-1"
@onClick={{perform this.convertDocument}}
data-test-convert-button
>
{{t "alexandria.document-details.convert"}}
</UkButton>
</div>
{{/if}}

<div uk-form-custom>
<input
data-test-replace
Expand Down
33 changes: 31 additions & 2 deletions addon/components/single-document-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { ErrorHandler } from "ember-alexandria/helpers/error-handler";
// TODO: This should be refactored and the SingleDocumentDetailsComponent should NOT
// be inheriting from DocumentCard
export default class SingleDocumentDetailsComponent extends DocumentCard {
@service router;
@service("alexandria-documents") documents;
@service("alexandria-tags") tags;
@service("alexandria-config") config;
@service("alexandria-side-panel") sidePanel;
@service router;
@service intl;
@service store;
@service fetch;

@tracked editTitle = false;
@tracked editDescription = false;
Expand All @@ -30,6 +32,16 @@ export default class SingleDocumentDetailsComponent extends DocumentCard {
return formats[language] ?? defaultFormat;
}

get displayConvertButton() {
return (
this.config.enablePDFConversion &&
[
"application/vnd.oasis.opendocument.text",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
].includes(this.args.document.latestFile.value?.mimeType)
);
}

@action updateDocumentTitle({ target: { value: title } }) {
this.validTitle = Boolean(title);
this.args.document.title = title;
Expand Down Expand Up @@ -86,4 +98,21 @@ export default class SingleDocumentDetailsComponent extends DocumentCard {
);
}
}

@dropTask
*convertDocument(event) {
event?.preventDefault();
try {
const modelName = "document";
const adapter = this.store.adapterFor(modelName);
const url = adapter.buildURL(modelName, this.args.document.id);
yield this.fetch.fetch(`${url}/convert`);

this.args.refreshDocumentList();

this.notification.success(this.intl.t("alexandria.success.covert-pdf"));
} catch (error) {
new ErrorHandler(this, error).notify("alexandria.errors.convert-pdf");
}
}
}
3 changes: 2 additions & 1 deletion addon/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export default class FileModel extends Model {
@attr name;
@attr downloadUrl;
@attr metainfo;
@attr content;
@attr content; // needed for upload
@attr mimeType;
@attr checksum;

@attr createdAt;
Expand Down
6 changes: 6 additions & 0 deletions addon/services/alexandria-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import Service from "@ember/service";
import { tracked } from "@glimmer/tracking";

export default class AlexandriaConfigService extends Service {
namespace = undefined;
zipDownloadHost = undefined;
zipDownloadNamespace = undefined;

enablePDFConversion = false;

markIcons = {};

@tracked alexandriaQueryParams = {};
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/components/single-document-details-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Service from "@ember/service";
import { render, click, fillIn, waitFor } from "@ember/test-helpers";
import { tracked } from "@glimmer/tracking";
import AlexandriaConfigService from "dummy/services/alexandria-config";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
Expand Down Expand Up @@ -146,4 +147,32 @@ module("Integration | Component | single-document-details", function (hooks) {

assert.dom("[data-test-date]").hasText("10/31/2023");
});

test("it renders conversion", async function (assert) {
const mockConfigService = class ConfigService extends AlexandriaConfigService {
enablePDFConversion = true;
};
this.owner.register("service:alexandria-config", mockConfigService);
this.selectedDocument = {
latestFile: { value: { mimeType: "application/pdf" } },
};

await render(
hbs`<SingleDocumentDetails @document={{this.selectedDocument}} />`,
);

assert.dom("[data-test-convert-button]").doesNotExist();

this.selectedDocument = {
latestFile: {
value: { mimeType: "application/vnd.oasis.opendocument.text" },
},
};

await render(
hbs`<SingleDocumentDetails @document={{this.selectedDocument}} />`,
);

assert.dom("[data-test-convert-button]").exists();
});
});
3 changes: 3 additions & 0 deletions translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alexandria:
update: "Your changes chould'nt be saved. Please try again."
no-permission: "You don't have permission to perform this action."
move-document: "While moving {count, plural, one {the document} other {# documents}}, an error occured. Please try again."
covert-pdf: "While converting, an error occured. Please try again."

success:
delete-document: "Document deleted successfully"
Expand All @@ -30,6 +31,7 @@ alexandria:
} uploaded successfully.
update: "Changes saved."
move-document: "{count, plural, one {Document} other {# documents}} moved successfully"
covert-pdf: "Document converted successfully."

category-nav:
all-files: "All documents"
Expand Down Expand Up @@ -70,6 +72,7 @@ alexandria:
created-by-user: "Created by user"
created-by-group: "Created by group"
replace: "Replace"
convert: "To PDF"
tags:
title: "Tags"
add: "Add"
Expand Down

0 comments on commit ac5d3a4

Please sign in to comment.