loading works again
This commit is contained in:
@@ -21,43 +21,41 @@ export class App {
|
||||
|
||||
// This function will be called in the ShowEnvelope.razor page
|
||||
// and will trigger loading of the Editor Interface
|
||||
public static async loadPDFFromUrl(container: string, envelopeKey: string) {
|
||||
public static async init(container: string, envelopeKey: string) {
|
||||
|
||||
// Initialize classes
|
||||
console.debug("Initializing classes..")
|
||||
App.UI = new UI();
|
||||
App.Network = new Network();
|
||||
App.Annotation = new Annotation();
|
||||
|
||||
console.debug("Loading PSPDFKit..");
|
||||
// Load the envelope from the database
|
||||
console.debug("Loading envelope from database..")
|
||||
const envelopeObject: EnvelopeResponse = await App.Network.getEnvelope(envelopeKey);
|
||||
|
||||
console.debug(envelopeObject)
|
||||
|
||||
const envelopeObject: EnvelopeResponse = await App.Network.loadData(envelopeKey);
|
||||
App.envelopeKey = envelopeKey;
|
||||
App.currentDocument = envelopeObject.envelope.documents[0];
|
||||
|
||||
// Load the document from the filestore
|
||||
console.debug("Loading document from filestore")
|
||||
let arrayBuffer
|
||||
try {
|
||||
arrayBuffer = await App.Network.loadDocument(envelopeKey, App.currentDocument.id);
|
||||
arrayBuffer = await App.Network.getDocument(envelopeKey, App.currentDocument.id);
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
// Load PSPDFKit
|
||||
console.debug("Loading PSPDFKit..")
|
||||
App.Instance = await App.UI.loadPSPDFKit(arrayBuffer, container)
|
||||
App.UI.configurePSPDFKit(this.Instance, App.handleClick)
|
||||
|
||||
console.debug(envelopeObject.envelope);
|
||||
console.debug("PSPDFKit configured!");
|
||||
|
||||
const annotations: any[] = [];
|
||||
|
||||
App.currentDocument.elements.forEach(function (element: Element) {
|
||||
console.log("Creating annotation for element", element.id)
|
||||
|
||||
const [annotation, formField] = App.Annotation.createAnnotationFromElement(element)
|
||||
annotations.push(annotation);
|
||||
annotations.push(formField);
|
||||
})
|
||||
App.UI.configurePSPDFKit(this.Instance, App.handleClick)
|
||||
|
||||
// Load annotations into PSPDFKit
|
||||
console.debug("Loading annotations..")
|
||||
const annotations = App.Annotation.createAnnotations(App.currentDocument)
|
||||
const createdAnnotations = await App.Instance.create(annotations)
|
||||
|
||||
console.debug(createdAnnotations)
|
||||
}
|
||||
|
||||
public static async handleClick(eventType: string) {
|
||||
@@ -121,7 +119,33 @@ export class App {
|
||||
}
|
||||
|
||||
class Annotation {
|
||||
public createAnnotationFromElement(element: Element): [annotation: WidgetAnnotation, formField: SignatureFormFieldType] {
|
||||
public createAnnotations(document: Document): AnnotationsUnion[] {
|
||||
const annotations: any[] = [];
|
||||
|
||||
document.elements.forEach((element: Element) => {
|
||||
console.log("Creating annotation for element", element.id)
|
||||
|
||||
const [annotation, formField] = this.createAnnotationFromElement(element)
|
||||
annotations.push(annotation);
|
||||
annotations.push(formField);
|
||||
})
|
||||
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public async deleteAnnotations(instance: Instance): Promise<any> {
|
||||
let pageAnnotations = (
|
||||
await Promise.all(Array.from({ length: instance.totalPageCount }).map((_, pageIndex) =>
|
||||
instance.getAnnotations(pageIndex)
|
||||
))
|
||||
).flatMap((annotations) =>
|
||||
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
||||
).filter((annotation) => !!annotation.isSignature);
|
||||
//deleting all Annotations
|
||||
return await instance.delete(pageAnnotations);
|
||||
}
|
||||
|
||||
private createAnnotationFromElement(element: Element): [annotation: WidgetAnnotation, formField: SignatureFormFieldType] {
|
||||
const id = PSPDFKit.generateInstantId()
|
||||
const width = this.inchToPoint(element.width)
|
||||
const height = this.inchToPoint(element.height)
|
||||
@@ -140,18 +164,6 @@ class Annotation {
|
||||
return [annotation, formField]
|
||||
}
|
||||
|
||||
public async deleteAnnotations(instance: Instance): Promise<any> {
|
||||
let pageAnnotations = (
|
||||
await Promise.all(Array.from({ length: instance.totalPageCount }).map((_, pageIndex) =>
|
||||
instance.getAnnotations(pageIndex)
|
||||
))
|
||||
).flatMap((annotations) =>
|
||||
annotations.reduce((acc, annotation) => acc.concat(annotation), [])
|
||||
).filter((annotation) => !!annotation.isSignature);
|
||||
//deleting all Annotations
|
||||
return await instance.delete(pageAnnotations);
|
||||
}
|
||||
|
||||
private createSignatureAnnotation(id: string, width: number, height: number, top: number, left: number, pageIndex: number): WidgetAnnotation {
|
||||
const annotation = new PSPDFKit.Annotations.WidgetAnnotation({
|
||||
id: id,
|
||||
@@ -169,20 +181,25 @@ class Annotation {
|
||||
}
|
||||
|
||||
class Network {
|
||||
// Makes a call to the supplied url and fetches the binary response as an array buffer
|
||||
public loadDocument(envelopeKey: string, documentId: number): Promise<ArrayBuffer> {
|
||||
return fetch(`/api/file/${envelopeKey}?id=${documentId}`, { credentials: "include" })
|
||||
.then(res => res.arrayBuffer());
|
||||
}
|
||||
|
||||
// Makes a call to the supplied url and fetches the json response as an object
|
||||
public loadData(envelopeKey): Promise<any> {
|
||||
public getEnvelope(envelopeKey: string): Promise<any> {
|
||||
return fetch(`/api/envelope/${envelopeKey}`, { credentials: "include" })
|
||||
.then(res => res.json());
|
||||
}
|
||||
|
||||
public getDocument(envelopeKey: string, documentId: number): Promise<ArrayBuffer> {
|
||||
return fetch(`/api/document/${envelopeKey}?index=${documentId}`, { credentials: "include" })
|
||||
.then(res => res.arrayBuffer());
|
||||
}
|
||||
|
||||
public postDocument(envelopeKey: string, documentId: number, buffer: ArrayBuffer): Promise<any> {
|
||||
return fetch(`/api/upload/${envelopeKey}/${documentId}`, { credentials: "include", method: "POST", body: buffer })
|
||||
return fetch(`/api/document/${envelopeKey}/${documentId}`, { credentials: "include", method: "POST", body: buffer })
|
||||
.then(res => res.json());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public postEnvelope(envelopeKey: string, documentId: number, buffer: ArrayBuffer): Promise<any> {
|
||||
return fetch(`/api/envelope/${envelopeKey}/${documentId}`, { credentials: "include", method: "POST", body: buffer })
|
||||
.then(res => res.json());
|
||||
}
|
||||
}
|
||||
@@ -236,6 +253,8 @@ class UI {
|
||||
|
||||
const toolbarItems = this.getToolbarItems(instance, handler)
|
||||
instance.setToolbarItems(toolbarItems)
|
||||
|
||||
console.debug("PSPDFKit configured!");
|
||||
}
|
||||
|
||||
public getToolbarItems(instance: Instance, handler: any): ToolbarItem[] {
|
||||
|
||||
Reference in New Issue
Block a user