-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpreview.js
More file actions
94 lines (83 loc) · 1.83 KB
/
Copy pathpreview.js
File metadata and controls
94 lines (83 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { api } from './api';
/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* A cache of template schema data, grouped by form
* @type {Object}
*/
const templateSchema = {};
/**
* Get template schema data
*
* @param {number} formId
* @param {string} template
* @return {Object} Template schema.
*
* @since 7.0
*/
export async function getTemplateSchema(formId, template) {
// add formId key to cache
if (!templateSchema[formId]) {
templateSchema[formId] = {};
}
// return cached schema
if (templateSchema[formId][template]) {
return templateSchema[formId][template];
}
const url =
GFPDF.restUrl +
'form/' +
encodeURIComponent(formId) +
'/schema/?template=' +
encodeURIComponent(template);
const response = await api(url, {
method: 'GET',
headers: {
'X-WP-Nonce': GFPDF.restNonce,
},
});
try {
if (!response.ok) {
throw new Error(await response.json());
}
templateSchema[formId][template] = await response.json();
return templateSchema[formId][template];
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}
/**
* Generate a PDF Preview using the defined PDF settings
*
* @param {FormData} formData
* @return {Blob|null} The rendered PDF, or null on failure.
*
* @since 7.0
*/
export async function getPdfPreview(formData) {
const url =
GFPDF.restUrl +
'form/' +
encodeURIComponent(formData.get('form')) +
'/preview';
const response = await api(url, {
method: 'POST',
headers: {
'X-WP-Nonce': GFPDF.restNonce,
},
body: formData,
});
try {
if (!response.ok) {
throw new Error(await response.json());
}
return await response.blob();
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}