-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEditor.vue
More file actions
274 lines (258 loc) · 6.67 KB
/
Editor.vue
File metadata and controls
274 lines (258 loc) · 6.67 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
<!--
- SPDX-FileCopyrightText: 2024 LibreCode coop and LibreCode contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="container-draw">
<div class="actions">
<div class="color-selector">
<label class="color-label" @click="$refs.colorPicker.$el.querySelector('button').click()">
{{ t('libresign', 'Color') }}
</label>
<NcColorPicker ref="colorPicker"
v-model="color"
:palette="customPalette"
@submit="updateColor">
<button class="color-preview"
:style="{ backgroundColor: color }"
:aria-label="t('libresign', 'Choose color')" />
</NcColorPicker>
</div>
<!-- TRANSLATORS Accessible label for the button that clears the current drawing from the canvas. Does not delete any saved file. -->
<NcButton :aria-label="t('libresign', 'Delete')"
@click="clear">
<template #icon>
<NcIconSvgWrapper :path="mdiDelete" :size="20" />
</template>
</NcButton>
</div>
<div ref="canvasWrapper" class="canvas-wrapper">
<p class="sr-only">
<!-- TRANSLATORS Screen-reader-only instruction for the signature drawing canvas. "Text" and "Upload" must match the translated labels of the other two tabs in this dialog. -->
{{ t('libresign', 'Drawing area. Use a mouse or touch screen to draw your signature. If you cannot draw, use the Text or Upload tabs instead.') }}
</p>
<canvas ref="canvas"
class="canvas"
:aria-label="t('libresign', 'Draw your signature here')"
role="img"
_width="10px"
_height="10px" />
</div>
<div class="action-buttons">
<NcButton variant="primary"
:disabled="!canSave"
@click="confirmationDraw">
{{ t('libresign', 'Save') }}
</NcButton>
<NcButton @click="close">
{{ t('libresign', 'Cancel') }}
</NcButton>
</div>
<NcDialog v-if="modal"
:name="t('libresign', 'Confirm your signature')"
@closing="handleModal(false)">
<PreviewSignature :src="imageData" />
<template #actions>
<NcButton variant="primary" @click="saveSignature">
{{ t('libresign', 'Save') }}
</NcButton>
<NcButton @click="handleModal(false)">
{{ t('libresign', 'Cancel') }}
</NcButton>
</template>
</NcDialog>
</div>
</template>
<script>
import { t } from '@nextcloud/l10n'
import { mdiDelete } from '@mdi/js'
import SignaturePad from 'signature_pad'
import { getCapabilities } from '@nextcloud/capabilities'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcColorPicker from '@nextcloud/vue/components/NcColorPicker'
import NcDialog from '@nextcloud/vue/components/NcDialog'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import PreviewSignature from '../PreviewSignature/PreviewSignature.vue'
export default {
name: 'Editor',
components: {
NcDialog,
NcColorPicker,
NcButton,
NcIconSvgWrapper,
PreviewSignature,
},
setup() {
return {
mdiDelete,
}
},
data: () => ({
canvasWidth: getCapabilities().libresign.config['sign-elements']['signature-width'],
canvasHeight: getCapabilities().libresign.config['sign-elements']['signature-height'],
color: '#000000',
customPalette: [
'#000000',
'#ff0000',
'#0000ff',
'#008000',
],
imageData: null,
modal: false,
mounted: false,
canSave: false,
scale: 1,
}),
mounted() {
this.mounted = true
this.$nextTick(() => {
this.applyCanvasSize()
this.$refs.canvas.signaturePad = new SignaturePad(this.$refs.canvas)
this.$refs.canvas.signaturePad.addEventListener('endStroke', () => {
this.canSave = !this.$refs.canvas.signaturePad.isEmpty()
})
})
},
beforeUnmount() {
this.mounted = false
if (this.$refs.canvas?.signaturePad) {
this.$refs.canvas.signaturePad.clear()
}
this.imageData = null
},
methods: {
t,
applyCanvasSize() {
if (!this.$refs.canvasWrapper || !this.$refs.canvas) {
return
}
const padding = 12
const wrapperWidth = this.$refs.canvasWrapper.offsetWidth || 0
const maxScaleWidth = wrapperWidth ? (wrapperWidth - padding) / this.canvasWidth : 1
const maxScale = maxScaleWidth
const minDisplayWidth = 420
const minDisplayHeight = 220
const minScaleWidth = minDisplayWidth / this.canvasWidth
const minScaleHeight = minDisplayHeight / this.canvasHeight
const minScale = Math.max(1, minScaleWidth, minScaleHeight)
this.scale = Math.min(maxScale || 1, minScale)
const finalWidth = Math.round(this.canvasWidth * this.scale)
const finalHeight = Math.round(this.canvasHeight * this.scale)
this.$refs.canvas.width = finalWidth
this.$refs.canvas.height = finalHeight
this.$refs.canvas.style.width = `${finalWidth}px`
this.$refs.canvas.style.height = `${finalHeight}px`
},
updateColor() {
this.$refs.canvas.signaturePad.penColor = this.color
},
clear() {
this.$refs.canvas.signaturePad.clear()
this.canSave = false
},
createDataImage() {
this.imageData = this.$refs.canvas.signaturePad.toDataURL('image/png')
},
confirmationDraw() {
this.createDataImage()
this.handleModal(true)
},
handleModal(status) {
this.modal = status
},
close() {
this.$emit('close')
},
saveSignature() {
this.handleModal(false)
this.$emit('save', this.imageData)
},
},
}
</script>
<style lang="scss" scoped>
.container-draw{
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100%;
min-height: 0;
.actions{
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
.color-selector {
display: flex;
align-items: center;
gap: 8px;
.color-label {
font-weight: 500;
cursor: pointer;
user-select: none;
}
.color-preview {
width: 36px;
height: 36px;
border-radius: var(--border-radius-large);
border: 2px solid var(--color-border);
cursor: pointer;
transition: border-color 0.2s;
&:hover {
border-color: var(--color-primary-element);
}
}
}
.action-delete{
cursor: pointer;
margin-right: 20px;
}
}
.canvas-wrapper{
display: flex;
position: relative;
overflow: auto;
width: 100%;
height: 100%;
min-height: 0;
flex: 1 1 auto;
justify-content: center;
align-items: center;
.canvas{
max-width: none;
max-height: none;
position: block;
background-color: #cecece;
border-radius: 10px;
}
}
.action-buttons{
justify-content: end;
display: flex;
box-sizing: border-box;
grid-gap: 10px;
position: sticky;
bottom: 0;
background: var(--color-main-background);
padding: 8px 0;
z-index: 1;
}
}
img{
padding: 20px;
@media screen and (max-width: 650px){
width: 100%;
}
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
</style>