refactor(app): simplify App initialization and rename PSPDFKit instance

- Initialize `currentDocument` and `currentReceiver` directly in constructor
- Replace `Instance` with `pdfKit` for clearer naming
- Remove redundant envelope/document loading in `init`
- Update all method references to use `this.pdfKit`
This commit is contained in:
2025-09-19 15:16:59 +02:00
parent 3b9b930b82
commit 7d4106d0a5
2 changed files with 18 additions and 37 deletions

View File

@@ -2,9 +2,9 @@ class App {
constructor(envelopeKey, envelopeReceiver, documentBytes, licenseKey, locale, container) {
this.container = container ?? `#${this.constructor.name.toLowerCase()}`;
this.envelopeKey = envelopeKey
this.Instance = null
this.currentDocument = null
this.currentReceiver = null
this.pdfKit = null
this.currentDocument = envelopeReceiver.envelope.documents[0]
this.currentReceiver = envelopeReceiver.receiver
this.signatureCount = 0;
this.envelopeReceiver = envelopeReceiver;
this.documentBytes = documentBytes;
@@ -12,51 +12,32 @@ class App {
this.locale = locale;
}
// This function will be called from the ShowEnvelope.razor page
// and will trigger loading of the Editor Interface
async init() {
// Load the envelope from the database
this.currentDocument = this.envelopeReceiver.envelope.documents[0]
this.currentReceiver = this.envelopeReceiver.receiver
// Load the document from the filestore
const documentResponse = this.documentBytes
if (documentResponse.fatal || documentResponse.error) {
return Swal.fire({
title: 'Fehler',
text: 'Dokument konnte nicht geladen werden!',
icon: 'error',
})
}
const arrayBuffer = this.documentBytes
// Load PSPDFKit
this.Instance = await UI.loadPSPDFKit(arrayBuffer, this.container, this.licenseKey, this.locale)
UI.addToolbarItems(this.Instance, this.handleClick.bind(this))
this.pdfKit = await UI.loadPSPDFKit(this.documentBytes, this.container, this.licenseKey, this.locale)
UI.addToolbarItems(this.pdfKit, this.handleClick.bind(this))
this.Instance.addEventListener(
this.pdfKit.addEventListener(
'annotations.load',
this.handleAnnotationsLoad.bind(this)
)
this.Instance.addEventListener(
this.pdfKit.addEventListener(
'annotations.change',
this.handleAnnotationsChange.bind(this)
)
this.Instance.addEventListener(
this.pdfKit.addEventListener(
'annotations.create',
this.handleAnnotationsCreate.bind(this)
)
this.Instance.addEventListener("annotations.willChange", _ => {
this.pdfKit.addEventListener("annotations.willChange", _ => {
Comp.ActPanel.Toggle();
});
// Load annotations into PSPDFKit
try {
this.signatureCount = this.currentDocument.elements.length
await createAnnotations(this.currentDocument, this.Instance)
await createAnnotations(this.currentDocument, this.pdfKit)
} catch (e) {
console.error("Error loading annotations:", e);
}
@@ -96,7 +77,7 @@ class App {
const request = await fetch(imageUrl)
const blob = await request.blob()
const imageAttachmentId = await this.Instance.createAttachment(blob)
const imageAttachmentId = await this.pdfKit.createAttachment(blob)
const frameAnnotation = createImageAnnotation(
new PSPDFKit.Geometry.Rect({
@@ -109,7 +90,7 @@ class App {
imageAttachmentId
)
this.Instance.create(frameAnnotation)
this.pdfKit.create(frameAnnotation)
}
}
@@ -195,7 +176,7 @@ class App {
}
async handleFinish(event) {
const iJSON = await this.Instance.exportInstantJSON()
const iJSON = await this.pdfKit.exportInstantJSON()
const iFormFieldValues = await iJSON.formFieldValues;
//check required
@@ -249,7 +230,7 @@ class App {
//---
// Save changes before doing anything
try {
await this.Instance.save()
await this.pdfKit.save()
} catch (e) {
Swal.fire({
title: 'Fehler',
@@ -293,7 +274,7 @@ class App {
async validateAnnotations(totalSignatures) {
const annotations = await getAnnotations(this.Instance)
const annotations = await getAnnotations(this.pdfKit)
const filtered = annotations
.map(a => a.toJS())
.filter(a => a.isSignature)
@@ -314,7 +295,7 @@ class App {
})
if (result.isConfirmed) {
const result = await deleteAnnotations(this.Instance)
const result = await deleteAnnotations(this.pdfKit)
}
return result