-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathpageManipulationFunctions.js
227 lines (200 loc) · 7.2 KB
/
pageManipulationFunctions.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import extractPagesWithAnnotations from 'helpers/extractPagesWithAnnotations';
import core from 'core';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import actions from 'actions';
import i18next from 'i18next';
import { workerTypes } from 'constants/types';
import { redactionTypeMap } from 'constants/redactionTypes';
const getNewRotation = (curr, counterClockwise = false) => {
const { E_0, E_90, E_180, E_270 } = window.Core.PageRotation;
switch (curr) {
case E_270:
return counterClockwise ? E_180 : E_0;
case E_180:
return counterClockwise ? E_90 : E_270;
case E_90:
return counterClockwise ? E_0 : E_180;
default:
return counterClockwise ? E_270 : E_90;
}
};
const canRotateLoadedDocument = () => {
const doc = core.getDocument();
const docType = doc?.type;
return (
workerTypes.PDF === docType ||
workerTypes.IMAGE === docType ||
(docType === workerTypes.WEBVIEWER_SERVER && !doc.isWebViewerServerDocument())
);
};
const rotatePages = (pageNumbers, counterClockwise) => {
if (canRotateLoadedDocument()) {
const rotation = counterClockwise ? window.Core.PageRotation.E_270 : window.Core.PageRotation.E_90;
pageNumbers.forEach((index) => {
core.rotatePages([index], rotation);
});
} else {
const docViewer = core.getDocumentViewer();
const currentRotations = docViewer.getPageRotations();
for (const page of pageNumbers) {
docViewer.setRotation(getNewRotation(currentRotations[page], counterClockwise), page);
}
}
};
const rotateClockwise = (pageNumbers) => {
rotatePages(pageNumbers, false);
};
const rotateCounterClockwise = (pageNumbers) => {
rotatePages(pageNumbers, true);
};
const insertAbove = (pageNumbers) => {
core.insertBlankPages(pageNumbers, core.getPageWidth(pageNumbers[0]), core.getPageHeight(pageNumbers[0]));
};
const insertBelow = (pageNumbers) => {
core.insertBlankPages(pageNumbers.map((i) => i + 1), core.getPageWidth(pageNumbers[0]), core.getPageHeight(pageNumbers[0]));
};
const replace = (dispatch) => {
dispatch(actions.closeElement('pageManipulationOverlay'));
dispatch(actions.openElement('pageReplacementModal'));
};
const extractPages = (pageNumbers, dispatch) => {
const message = i18next.t('warning.extractPage.message');
const title = i18next.t('warning.extractPage.title');
const confirmBtnText = i18next.t('warning.extractPage.confirmBtn');
const secondaryBtnText = i18next.t('warning.extractPage.secondaryBtn');
const warning = {
message,
title,
confirmBtnText,
onConfirm: () => extractPagesWithAnnotations(pageNumbers).then((file) => {
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file, 'extractedDocument.pdf');
} else {
saveAs(file, 'extractedDocument.pdf');
}
}),
secondaryBtnText,
onSecondary: () => {
extractPagesWithAnnotations(pageNumbers).then((file) => {
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file, 'extractedDocument.pdf');
} else {
saveAs(file, 'extractedDocument.pdf');
}
core.removePages(pageNumbers).then(() => {
dispatch(actions.setSelectedPageThumbnails([]));
});
});
},
};
dispatch(actions.showWarningMessage(warning));
};
const deletePages = (pageNumbers, dispatch, isModalEnabled = true) => {
if (isModalEnabled) {
let message = i18next.t('warning.deletePage.deleteMessage');
const title = i18next.t('warning.deletePage.deleteTitle');
const confirmBtnText = i18next.t('action.ok');
let warning = {
message,
title,
confirmBtnText,
onConfirm: () => core.removePages(pageNumbers).then(() => {
dispatch(actions.setSelectedPageThumbnails([]));
}),
};
if (core.getDocumentViewer().getPageCount() === pageNumbers.length) {
message = i18next.t('warning.deletePage.deleteLastPageMessage');
warning = {
message,
title,
confirmBtnText,
onConfirm: () => Promise.resolve(),
};
}
dispatch(actions.showWarningMessage(warning));
} else {
core.removePages(pageNumbers).then(() => {
dispatch(actions.setSelectedPageThumbnails([]));
});
}
};
const movePagesToBottom = (pageNumbers) => {
core.movePages(pageNumbers, core.getTotalPages() + 1);
};
const movePagesToTop = (pageNumbers) => {
core.movePages(pageNumbers, 0);
};
const noPagesSelectedWarning = (pageNumbers, dispatch) => {
if (pageNumbers.length === 0) {
const title = i18next.t('warning.selectPage.selectTitle');
const message = i18next.t('warning.selectPage.selectMessage');
const confirmBtnText = i18next.t('action.ok');
const warning = {
message,
title,
confirmBtnText,
onConfirm: () => Promise.resolve(),
keepOpen: ['leftPanel'],
};
dispatch(actions.showWarningMessage(warning));
return true;
}
return false;
};
const redactPages = (pageNumbers, redactionStyles) => {
core.applyRedactions(createPageRedactions(pageNumbers, redactionStyles));
};
const createPageRedactions = (pageNumbers, redactionStyles) => {
const annots = [];
for (const page of pageNumbers) {
const pageInfo = core.getPageInfo(page);
if (pageInfo) {
const redaction = new Annotations.RedactionAnnotation({
PageNumber: page,
Rect: new Annotations.Rect(0, 0, pageInfo.width, pageInfo.height),
...redactionStyles
});
redaction.type = redactionTypeMap['FULL_PAGE'];
redaction.setCustomData('trn-redaction-type', redactionTypeMap['FULL_PAGE']);
redaction.Author = core.getCurrentUser();
annots.push(redaction);
}
}
core.getAnnotationManager().addAnnotations(annots);
core.getAnnotationManager().drawAnnotationsFromList(annots);
return annots;
};
const replacePages = async (sourceDoc, pagesToRemove, pagesToReplaceIntoDocument) => {
const documentLoadedInViewer = core.getDocument();
const pageCountOfLoadedDocument = documentLoadedInViewer.getPageCount();
const pagesToRemoveFromOriginal = pagesToRemove.sort((a, b) => a - b);
// If document to replace into has only one page, or we are replacing all pages
// then we can insert pages at the end, and then remove the pages to avoid an error of removing all pages
if (pageCountOfLoadedDocument === 1 || pagesToRemoveFromOriginal.length === pageCountOfLoadedDocument) {
await documentLoadedInViewer.insertPages(sourceDoc, pagesToReplaceIntoDocument);
await documentLoadedInViewer.removePages(pagesToRemoveFromOriginal);
} else {
// If document to replace into has > 1 page we need insert the new pages at the spot of the first removed page
// pagesToRemoveFromOriginal is sorted in ascending order. Interleaving pages would be complex.
await documentLoadedInViewer.removePages(pagesToRemoveFromOriginal);
await documentLoadedInViewer.insertPages(sourceDoc, pagesToReplaceIntoDocument, pagesToRemoveFromOriginal[0]);
}
};
export {
rotateClockwise,
rotateCounterClockwise,
insertAbove,
insertBelow,
replace,
extractPages,
deletePages,
movePagesToBottom,
movePagesToTop,
noPagesSelectedWarning,
redactPages,
createPageRedactions,
replacePages,
};