-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathVisibleElements.vue
More file actions
582 lines (541 loc) · 15.6 KB
/
VisibleElements.vue
File metadata and controls
582 lines (541 loc) · 15.6 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<!--
- SPDX-FileCopyrightText: 2024 LibreCode coop and LibreCode contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcDialog v-if="modal"
:name="t('libresign', 'Signature positions')"
size="normal"
@closing="closeModal">
<div v-if="filesStore.loading">
<NcLoadingIcon :size="64" :name="t('libresign', 'Loading …')" />
</div>
<div v-else class="sign-details">
<h2 class="modal_name">
<Chip :state="isDraft ? 'warning' : 'default'">
{{ statusLabel }}
</Chip>
<span class="name">{{ document.name }}</span>
</h2>
<p v-if="!signerSelected">
<NcNoteCard type="info"
:text="t('libresign', 'Select a signer to set their signature position')" />
</p>
<ul class="view-sign-detail__sidebar">
<li v-if="signerSelected"
:class="{ tip: signerSelected }">
{{ t('libresign', 'Click on the place you want to add.') }}
<NcButton variant="primary"
@click="stopAddSigner">
{{ t('libresign', 'Cancel') }}
</NcButton>
</li>
<Signer v-for="(signer, key) in document.signers"
:key="key"
:signer-index="key"
:class="{ disabled: signerSelected }"
event="libresign:visible-elements-select-signer">
<slot v-bind="{signer}" slot="actions" name="actions" />
</Signer>
</ul>
<NcButton v-if="canSave"
:variant="variantOfSaveButton"
:wide="true"
:class="{ disabled: signerSelected }"
@click="save()">
{{ t('libresign', 'Save') }}
</NcButton>
<NcButton v-if="canSign"
:variant="variantOfSignButton"
:wide="true"
@click="goToSign">
{{ t('libresign', 'Sign') }}
</NcButton>
</div>
<div class="image-page">
<PdfEditor v-if="!filesStore.loading && pdfFiles.length > 0"
ref="pdfEditor"
width="100%"
height="100%"
:files="pdfFiles"
:file-names="pdfFileNames"
@pdf-editor:end-init="updateSigners"
@pdf-editor:on-delete-signer="onDeleteSigner" />
</div>
</NcDialog>
</template>
<script>
import axios from '@nextcloud/axios'
import { getCapabilities } from '@nextcloud/capabilities'
import { showSuccess, showError } from '@nextcloud/dialogs'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import { generateOcsUrl, generateUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcDialog from '@nextcloud/vue/components/NcDialog'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
import Chip from '../Chip.vue'
import PdfEditor from '../PdfEditor/PdfEditor.vue'
import Signer from '../Signers/Signer.vue'
import { SIGN_STATUS } from '../../domains/sign/enum.js'
import { useFilesStore } from '../../store/files.js'
export default {
name: 'VisibleElements',
components: {
NcNoteCard,
NcDialog,
Signer,
Chip,
NcButton,
NcLoadingIcon,
PdfEditor,
},
setup() {
const filesStore = useFilesStore()
return { filesStore }
},
data() {
return {
canRequestSign: loadState('libresign', 'can_request_sign', false),
modal: false,
loading: false,
signerSelected: null,
width: getCapabilities().libresign.config['sign-elements']['full-signature-width'],
height: getCapabilities().libresign.config['sign-elements']['full-signature-height'],
filePagesMap: {},
elementsLoaded: false,
loadedPdfsCount: 0,
}
},
computed: {
variantOfSaveButton() {
if (this.canSave) {
return 'primary'
}
return 'secondary'
},
variantOfSignButton() {
if (this.canSave) {
return 'secondary'
}
return 'primary'
},
document() {
return this.filesStore.getFile()
},
pdfFiles() {
return (this.document.files || []).map(f => f.file)
},
pdfFileNames() {
return (this.document.files || []).map(f => `${f.name}.${f.metadata?.extension || 'pdf'}`)
},
documentNameWithExtension() {
const doc = this.document
if (!doc.metadata?.extension) {
return doc.name
}
return `${doc.name}.${doc.metadata.extension}`
},
canSign() {
if (this.status !== SIGN_STATUS.ABLE_TO_SIGN) {
return false
}
return (this.document?.settings?.signerFileUuid ?? '').length > 0
},
canSave() {
if (
[
SIGN_STATUS.DRAFT,
SIGN_STATUS.ABLE_TO_SIGN,
SIGN_STATUS.PARTIAL_SIGNED,
].includes(this.status)
) {
return true
}
return false
},
status() {
return Number(this.document?.status ?? -1)
},
statusLabel() {
return this.document.statusText
},
isDraft() {
return this.status === SIGN_STATUS.DRAFT
},
},
mounted() {
subscribe('libresign:show-visible-elements', this.showModal)
subscribe('libresign:visible-elements-select-signer', this.onSelectSigner)
},
beforeUnmount() {
unsubscribe('libresign:show-visible-elements', this.showModal)
unsubscribe('libresign:visible-elements-select-signer', this.onSelectSigner)
},
methods: {
getVuePdfEditor() {
return this.$refs.pdfEditor?.$refs?.vuePdfEditor
},
getCanvasList() {
const editor = this.getVuePdfEditor()
return editor?.$refs?.pdfBody?.querySelectorAll('canvas') || []
},
async showModal() {
if (!this.canRequestSign) {
return
}
if (getCapabilities()?.libresign?.config?.['sign-elements']?.['is-available'] === false) {
return
}
this.modal = true
this.filesStore.loading = true
if (this.document.nodeType === 'envelope' && this.document.files.length === 0) {
await this.fetchEnvelopeFiles()
} else if (this.document.nodeType !== 'envelope') {
if (!this.document.files || this.document.files.length === 0) {
const fileUrl = this.document.file || generateUrl('/apps/libresign/p/pdf/{uuid}', { uuid: this.document.uuid })
this.document.files = [{
id: this.document.id,
nodeId: this.document.nodeId,
uuid: this.document.uuid,
name: this.document.name,
file: fileUrl,
metadata: this.document.metadata,
signers: this.document.signers,
visibleElements: this.document.visibleElements || [],
}]
} else {
this.document.files = this.document.files.map(f => {
if (!f.file) {
const fileUrl = this.document.file || generateUrl('/apps/libresign/p/pdf/{uuid}', { uuid: f.uuid || this.document.uuid })
return { ...f, file: fileUrl }
}
return f
})
}
}
this.buildFilePagesMap()
this.filesStore.loading = false
},
async fetchEnvelopeFiles() {
const response = await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/list'), {
params: {
parentFileId: this.document.id,
force_fetch: true,
},
})
const childFiles = response?.data?.ocs?.data?.data || []
this.document.files = Array.isArray(childFiles) ? childFiles : []
},
buildFilePagesMap() {
this.filePagesMap = {}
const filesToProcess = this.document.files || []
if (!Array.isArray(filesToProcess)) {
return
}
let currentPage = 1
filesToProcess.forEach((file, index) => {
const metadata = file.metadata
const pageCount = metadata?.p || 0
for (let i = 0; i < pageCount; i++) {
this.filePagesMap[currentPage + i] = {
id: file.id,
fileIndex: index,
startPage: currentPage,
fileName: file.name,
}
}
currentPage += pageCount
})
},
closeModal() {
this.modal = false
this.filesStore.loading = false
this.elementsLoaded = false
this.loadedPdfsCount = 0
},
getPageHeightForFile(fileId, page) {
const filesToSearch = this.document.files || []
const fileInfo = filesToSearch.find(f => f.id === fileId)
const metadata = fileInfo?.metadata
return metadata?.d?.[page - 1]?.h
},
updateSigners(data) {
this.loadedPdfsCount++
const filesToProcess = this.document.files || []
const expectedPdfsCount = filesToProcess.length
if (this.elementsLoaded || this.loadedPdfsCount < expectedPdfsCount) {
return
}
let visibleElementsToAdd = []
filesToProcess.forEach((f, fileIndex) => {
const elements = Array.isArray(f.visibleElements) ? f.visibleElements : []
elements.forEach(element => {
visibleElementsToAdd.push({
...element,
documentIndex: fileIndex,
fileId: f.id,
})
})
})
visibleElementsToAdd.forEach(element => {
let envelopeSignerMatch = null
let childSigner = null
if (element.fileId) {
const fileInfo = filesToProcess.find(f => f.id === element.fileId)
if (fileInfo) {
childSigner = (fileInfo.signers || []).find(s => s.signRequestId === element.signRequestId)
}
}
if (childSigner) {
const childIdMethods = (childSigner.identifyMethods || []).map(m => `${m.method}:${m.value}`).sort().join('|')
envelopeSignerMatch = this.document.signers.find(s => {
const envIdMethods = (s.identifyMethods || []).map(m => `${m.method}:${m.value}`).sort().join('|')
return envIdMethods === childIdMethods
})
}
const baseSigner = envelopeSignerMatch || this.document.signers.find(s => s.signRequestId === element.signRequestId) || null
if (!baseSigner) {
return
}
const object = structuredClone(baseSigner)
const fileInfo = this.document.files.find(f => f.id === element.fileId)
if (fileInfo) {
const fileIndex = this.document.files.indexOf(fileInfo)
object.element = { ...element, documentIndex: fileIndex }
this.$refs.pdfEditor.addSigner(object)
return
}
object.element = element
this.$refs.pdfEditor.addSigner(object)
})
this.elementsLoaded = true
this.filesStore.loading = false
},
onSelectSigner(signer) {
if (!this.$refs.pdfEditor) {
return
}
this.signerSelected = signer
const canvasList = this.getCanvasList()
canvasList.forEach((canvas) => {
canvas.addEventListener('click', this.doSelectSigner)
})
},
doSelectSigner(event) {
const canvasList = this.getCanvasList()
const canvasIndex = Array.from(canvasList).indexOf(event.target)
const globalPageNumber = canvasIndex + 1 // 1-based
let documentIndex = 0
let pageInDocument = globalPageNumber
if (this.filePagesMap[globalPageNumber]) {
const pageInfo = this.filePagesMap[globalPageNumber]
documentIndex = pageInfo.fileIndex
pageInDocument = globalPageNumber - pageInfo.startPage + 1
}
this.addSignerToPosition(event, pageInDocument, documentIndex)
this.stopAddSigner()
},
addSignerToPosition(event, pageInDocument, documentIndex) {
const canvas = event.target
const rect = canvas.getBoundingClientRect()
const scale = this.$refs.pdfEditor.$refs.vuePdfEditor.scale || 1
let clickX = event.clientX - rect.left
let clickY = event.clientY - rect.top
if (Math.abs(rect.width - canvas.width) > 1) {
clickX = clickX * (canvas.width / rect.width)
clickY = clickY * (canvas.height / rect.height)
}
const normalizedX = clickX / scale
const normalizedY = clickY / scale
const targetFileId = this.document.files[documentIndex]?.id || this.document?.id
const pageHeight = this.getPageHeightForFile(targetFileId, pageInDocument)
if (!pageHeight) {
return
}
const left = normalizedX - this.width / 2
const top = normalizedY - this.height / 2
const llx = left
const ury = pageHeight - top
const coordinates = {
page: pageInDocument,
width: this.width,
height: this.height,
left,
top,
llx,
ury,
}
this.signerSelected.element = {
coordinates: coordinates,
}
this.signerSelected.element.documentIndex = documentIndex
this.$refs.pdfEditor.addSigner(this.signerSelected)
},
stopAddSigner() {
const canvasList = this.getCanvasList()
canvasList.forEach((canvas) => {
canvas.removeEventListener('click', this.doSelectSigner)
})
this.signerSelected = null
},
async onDeleteSigner(object) {
if (!object.signer.element.elementId) {
return
}
await axios.delete(generateOcsUrl('/apps/libresign/api/v1/file-element/{uuid}/{elementId}', {
uuid: this.document.uuid,
elementId: object.signer.element.elementId,
}))
},
async goToSign() {
const uuid = this.document.settings.signerFileUuid
if (await this.save()) {
const route = this.$router.resolve({ name: 'SignPDF', params: { uuid } })
window.location.href = route.href
}
},
async save() {
this.loading = true
const visibleElements = this.buildVisibleElements()
try {
const response = await this.filesStore.saveWithVisibleElements({ visibleElements })
showSuccess(t('libresign', response.message))
this.closeModal()
this.loading = false
return true
} catch (error) {
showError(error.response?.data?.ocs?.data?.message || t('libresign', 'An error occurred'))
this.loading = false
return false
}
},
buildVisibleElements() {
const visibleElements = []
const numDocuments = this.document.files.length
for (let docIndex = 0; docIndex < numDocuments; docIndex++) {
const objects = this.$refs.pdfEditor.$refs.vuePdfEditor.getAllObjects(docIndex)
objects.forEach(object => {
if (!object.signer) return
let globalPageNumber = object.pageNumber
for (const [page, info] of Object.entries(this.filePagesMap)) {
if (info.fileIndex === docIndex) {
globalPageNumber = info.startPage + object.pageNumber - 1
break
}
}
const pageInfo = this.filePagesMap[globalPageNumber]
const pageHeight = this.getPageHeightForFile(pageInfo.id, object.pageNumber)
if (!pageHeight) {
return
}
const left = Math.floor(object.normalizedCoordinates.llx)
const top = Math.floor(pageHeight - object.normalizedCoordinates.lly)
const width = Math.floor(object.normalizedCoordinates.width)
const height = Math.floor(object.normalizedCoordinates.height)
const coordinates = {
page: globalPageNumber,
width,
height,
left,
top,
}
const element = {
type: 'signature',
elementId: object.signer.element.elementId,
coordinates,
}
const targetFileId = pageInfo.id
element.fileId = targetFileId
element.coordinates.page = globalPageNumber - pageInfo.startPage + 1
const fileInfo = this.document.files.find(f => f.id === targetFileId)
if (!fileInfo || !Array.isArray(fileInfo.signers)) {
return
}
const envIdMethods = (object.signer.identifyMethods || []).map(m => `${m.method}:${m.value}`).sort().join('|')
const candidate = fileInfo.signers.find(s => {
const childIdMethods = (s.identifyMethods || []).map(m => `${m.method}:${m.value}`).sort().join('|')
return childIdMethods === envIdMethods
})
if (!candidate || !candidate.signRequestId) {
return
}
element.signRequestId = candidate.signRequestId
visibleElements.push(element)
})
}
return visibleElements
},
},
}
</script>
<style lang="scss" scoped>
.image-page {
::v-deep .py-12{
all: unset;
}
::v-deep .p-5 {
all: unset;
}
}
:deep(.dialog__name) {
display: none;
}
:deep(.modal-container__close) {
z-index: 10 !important;
}
.modal_name {
display: flex;
align-items: center;
.name {
flex: auto;
text-align: center;
font-size: 21px;
overflow-wrap: break-word;
}
}
.modal-container {
.notecard--info {
margin: unset;
}
&--sidebar {
width: 300px;
}
& {
overflow: auto;
}
.button-vue {
margin: 4px;
}
.sign-details {
padding: 0 8px 8px;
position: sticky;
top: 0;
z-index: 9;
background-color: var(--color-main-background);
&__sidebar {
li {
margin: 3px 3px 1em 3px;
}
}
.disabled {
pointer-events: none;
visibility: hidden;
}
.tip {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
margin-top: 1px;
margin-bottom: 1px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
}
}
</style>