-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathopenInLibreSignAction.js
More file actions
114 lines (97 loc) · 3.08 KB
/
openInLibreSignAction.js
File metadata and controls
114 lines (97 loc) · 3.08 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
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { registerFileAction, FileAction } from '@nextcloud/files'
import { getCapabilities } from '@nextcloud/capabilities'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import { showError } from '@nextcloud/dialogs'
import { spawnDialog } from '@nextcloud/vue/functions/dialog'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
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
*/
function promptEnvelopeName() {
return new Promise((resolve) => {
const propsData = {
title: t('libresign', 'Envelope name'),
label: t('libresign', 'Enter a name for the envelope'),
placeholder: t('libresign', 'Envelope name'),
}
spawnDialog(
{
...EditNameDialog,
mounted() {
EditNameDialog.mounted?.call(this)
this.$on('close', (value) => {
resolve(value)
})
},
},
propsData,
)
})
}
export const action = new FileAction({
id: 'open-in-libresign',
displayName: () => t('libresign', 'Open in LibreSign'),
iconSvgInline: () => SvgIcon,
enabled(nodes) {
return loadState('libresign', 'certificate_ok')
&& nodes.length > 0 && nodes
.map(node => node.mime)
.every(mime => mime === 'application/pdf')
},
async exec(node) {
try {
await window.OCA.Files.Sidebar.open(node.path)
OCA.Files.Sidebar.setActiveTab('libresign')
return null
} catch (error) {
logger.error('Error while opening sidebar', { error })
return false
}
},
/**
* Multiple files: create envelope (if > 1) or delegate to exec (if = 1)
*/
async execBatch({ nodes }) {
if (nodes.length === 1) {
await this.exec({ nodes })
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}`
return axios.post(generateOcsUrl('/apps/libresign/api/v1/file'), {
files: nodes.map(node => ({ fileId: node.fileid })),
name: envelopeName,
settings: {
path: envelopePath,
},
}).then((response) => {
const envelopeData = response.data?.ocs?.data
window.OCA.Libresign.pendingEnvelope = envelopeData
window.OCA.Files.Sidebar.close()
window.OCA.Files.Sidebar.setActiveTab('libresign')
const firstNode = nodes[0]
window.OCA.Files.Sidebar.open(firstNode.path)
return new Array(nodes.length).fill(null)
}).catch((error) => {
console.error('[LibreSign] API error:', error)
showError(error.response?.data?.ocs?.data?.message)
return new Array(nodes.length).fill(null)
})
},
order: -1000,
})
registerFileAction(action)