diff --git a/src/dbp-official-signature-pdf-upload.js b/src/dbp-official-signature-pdf-upload.js index 4e3e3ff2958a7199a0d9d28bb8b572024050e783..90d7bb35bc6f911b699bc7a2550ac8ac3c9877e5 100644 --- a/src/dbp-official-signature-pdf-upload.js +++ b/src/dbp-official-signature-pdf-upload.js @@ -168,7 +168,8 @@ class OfficialSignaturePdfUpload extends ScopedElementsMixin(DBPSignatureLitElem fileSize: humanFileSize(file.size, false), }); - await this.uploadFile(file, params); + const annotations = this.takeAnnotationsFromQueue(key); + await this.uploadFile(file, params, annotations, i18n); this.uploadInProgress = false; } diff --git a/src/dbp-signature-lit-element.js b/src/dbp-signature-lit-element.js index f7822b2d87e664f9344bd5806e292eec36ac4e30..fce8150780010c67d69e1f9e5865d09e523166e0 100644 --- a/src/dbp-signature-lit-element.js +++ b/src/dbp-signature-lit-element.js @@ -1,6 +1,7 @@ import * as utils from "./utils"; import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element"; import JSONLD from "@dbp-toolkit/common/jsonld"; +import * as commonUtils from "@dbp-toolkit/common/utils"; export class DBPSignatureBaseLitElement extends AdapterLitElement { constructor() { @@ -90,9 +91,9 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { /** * @param file - * @returns {Promise<string>} key of the queued item + * @returns key of the queued item */ - async queueFile(file) { + queueFile(file) { this._queueKey++; const key = this._queueKey; this.queuedFiles[key] = file; @@ -118,25 +119,49 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { * * @param key */ - async addAnnotation(key) { + addAnnotation(key) { if (!this.queuedFilesAnnotations[key]) { this.queuedFilesAnnotations[key] = []; this.queuedFilesAnnotationsCount = 0; } - this.queuedFilesAnnotations[key].push({'key1': 'my key 1', 'key2': 'my key 2', 'value': 'my value'}); + // TODO: remove key/value presets + const number = Math.floor((Math.random() * 1000) + 1); + this.queuedFilesAnnotations[key].push({'key1': 'geschaeftszahl', 'key2': 'F' + number, 'value': 'my value ' + number}); // we just need this so the UI will update this.queuedFilesAnnotationsCount++; } + async addAnnotationsToFile(file, annotations, i18n) { + await commonUtils.asyncObjectForEach(annotations, async (annotation) => { + console.log("annotation", annotation); + console.log("file before", file); + const key1 = annotation.key1.trim(); + const key2 = annotation.key2.trim(); + const value = annotation.value.trim(); + + if (key1 === '' || key2 === '' || value === '') { + return; + } + + const annotationKey = key1 + '-' + key2; + file = await utils.addKeyValuePdfAnnotation(file, i18n, 'AppName', this.auth['user-full-name'], annotationKey, value); + console.log("file after", file); + }); + + console.log("addAnnotationsToFile file", file); + + return file; + } + /** * Remove an annotation of a file on the queue * * @param key * @param id */ - async removeAnnotation(key, id) { + removeAnnotation(key, id) { if (this.queuedFilesAnnotations[key] && this.queuedFilesAnnotations[key][id]) { delete this.queuedFilesAnnotations[key][id]; // we just need this so the UI will update @@ -144,6 +169,18 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { } } + /** + * Takes the annotations of a file off of the queue + * + * @param key + */ + takeAnnotationsFromQueue(key) { + const annotations = this.queuedFilesAnnotations[key]; + delete this.queuedFilesAnnotations[key]; + + return annotations; + } + /** * Update an annotation of a file on the queue * @@ -152,7 +189,7 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { * @param annotationKey * @param value */ - async updateAnnotation(key, id, annotationKey, value) { + updateAnnotation(key, id, annotationKey, value) { if (this.queuedFilesAnnotations[key] && this.queuedFilesAnnotations[key][id]) { this.queuedFilesAnnotations[key][id][annotationKey] = value; } @@ -222,11 +259,20 @@ export default class DBPSignatureLitElement extends DBPSignatureBaseLitElement { /** * @param file * @param params + * @param annotations + * @param i18n * @returns {Promise<void>} */ - async uploadFile(file, params = {}) { + async uploadFile(file, params = {}, annotations = [], i18n = {}) { this.uploadInProgress = true; this.uploadStatusFileName = file.name; + + // add annotations + if (annotations.length > 0) { + file = await this.addAnnotationsToFile(file, annotations, i18n) + console.log("uploadFile file", file); + } + let url = new URL(this.fileSourceUrl); let formData = new FormData(); formData.append('file', file); diff --git a/src/utils.js b/src/utils.js index 5c5ad9946feae7982c4c6e51ba3ccbc05ba24626..63326b4feeabf26a3f1ef5c45c0ff0057a23a914 100644 --- a/src/utils.js +++ b/src/utils.js @@ -143,6 +143,14 @@ export const getPDFSignatureCount = async (file) => { }; export const addKeyValuePdfAnnotation = async (file, i18n, appName, personName, key, value) => { + key = key.trim(); + value = value.trim(); + + // don't annotate if key or value are empty + if (key === '' || value === '') { + return file; + } + const data = await readArrayBufferFileContent(file); let pdfFactory = new AnnotationFactory(data); @@ -154,7 +162,7 @@ export const addKeyValuePdfAnnotation = async (file, i18n, appName, personName, appName: appName, personName: personName }); - const contents = 'DBP-SIGNATURE-' + key + ': ' + value; + const contents = 'dbp-annotation-' + key + '=' + value; // pdfFactory.checkRect(4, rect);