forked from LibreSign/libresign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenInLibreSignAction.js
More file actions
117 lines (96 loc) · 3.04 KB
/
openInLibreSignAction.js
File metadata and controls
117 lines (96 loc) · 3.04 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
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { registerFileAction } from '@nextcloud/files'
import { getCapabilities } from '@nextcloud/capabilities'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'
import { spawnDialog } from '@nextcloud/vue/functions/dialog'
import EditNameDialog from '../components/Common/EditNameDialog.vue'
// eslint-disable-next-line import/no-unresolved
import SvgIcon from '../../img/app-dark.svg?raw'
/**
* Prompts user for envelope name via dialog
*/
async function promptEnvelopeName() {
const envelopeName = await spawnDialog(
EditNameDialog,
{
title: t('libresign', 'Envelope name'),
label: t('libresign', 'Enter a name for the envelope'),
placeholder: t('libresign', 'Envelope name'),
},
)
return envelopeName
}
export const action = {
id: 'open-in-libresign',
displayName: () => t('libresign', 'Open in LibreSign'),
iconSvgInline: () => SvgIcon,
enabled(nodes) {
const getNodeMime = (node) => node?.mime || node?.mimetype || ''
if (!loadState('libresign', 'certificate_ok', false)) {
return false
}
if (!nodes?.length) {
return false
}
if (nodes.length === 1 && nodes[0].type === 'folder') {
return nodes[0].attributes?.['libresign-signature-status'] !== undefined
}
const allPdf = nodes.every(node => getNodeMime(node) === 'application/pdf')
if (!allPdf) {
return false
}
if (nodes.length > 1) {
return getCapabilities()?.libresign?.config?.envelope?.['is-available'] === true
}
return true
},
/**
* Single file or folder: open in sidebar
*/
async exec(node) {
await window.OCA.Files.Sidebar.open(node.path)
window.OCA.Files.Sidebar.setActiveTab('libresign')
return null
},
/**
* Multiple files: prepare envelope data and delegate to sidebar
* Similar to exec, but passes multiple files to the sidebar for processing
*/
async execBatch(nodes) {
const getNodeFileId = (node) => node?.fileid ?? node?.id
if (nodes.length === 1) {
await this.exec(nodes[0])
return [null]
}
const envelopeName = await promptEnvelopeName()
if (!envelopeName) {
return new Array(nodes.length).fill(null)
}
const rawDir = nodes[0].dirname ?? nodes[0].path.substring(0, nodes[0].path.lastIndexOf('/'))
const normalizedDir = (rawDir && rawDir !== '/') ? rawDir.replace(/\/+$/, '') : ''
const envelopePath = normalizedDir ? `${normalizedDir}/${envelopeName}` : `/${envelopeName}`
const temporaryEnvelopeId = -Date.now()
window.OCA.Libresign.pendingEnvelope = {
id: temporaryEnvelopeId,
nodeType: 'envelope',
name: envelopeName,
settings: {
path: envelopePath,
},
files: nodes.map(node => ({ fileId: getNodeFileId(node) })),
filesCount: nodes.length,
signers: [],
uuid: null,
}
const firstNode = nodes[0]
await window.OCA.Files.Sidebar.open(firstNode.path)
window.OCA.Files.Sidebar.setActiveTab('libresign')
return new Array(nodes.length).fill(null)
},
order: -1000,
}
registerFileAction(action)