-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathImageEditor.vue
575 lines (507 loc) · 12.4 KB
/
ImageEditor.vue
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
<template>
<div ref="editor" class="viewer__image-editor" v-bind="themeDataAttr" />
</template>
<script>
import { basename, dirname, extname, join } from 'path'
import { emit } from '@nextcloud/event-bus'
import { showError, showSuccess } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import FilerobotImageEditor from 'filerobot-image-editor'
import logger from '../services/logger.js'
import translations from '../models/editorTranslations.js'
const { TABS, TOOLS } = FilerobotImageEditor
export default {
name: 'ImageEditor',
props: {
fileid: {
type: [String, Number],
required: true,
},
mime: {
type: String,
required: true,
},
src: {
type: String,
required: true,
},
},
data() {
return {
imageEditor: null,
}
},
computed: {
config() {
return {
source: this.src,
defaultSavedImageName: this.defaultSavedImageName,
defaultSavedImageType: this.defaultSavedImageType,
// We use our own translations
useBackendTranslations: false,
// Watch resize
observePluginContainerSize: true,
// Default tab and tool
defaultTabId: TABS.ADJUST,
defaultToolId: TOOLS.CROP,
// Displayed tabs, disabling watermark
tabsIds: Object.values(TABS)
.filter(tab => tab !== TABS.WATERMARK)
.sort((a, b) => a.localeCompare(b)),
// onBeforeSave: this.onBeforeSave,
onClose: this.onClose,
// onModify: this.onModify,
onSave: this.onSave,
// Translations
translations,
theme: {
palette: {
'bg-secondary': 'var(--color-main-background)',
'bg-primary': 'var(--color-background-dark)',
// Accent
'accent-primary': 'var(--color-primary)',
// Use by the slider
'border-active-bottom': 'var(--color-primary)',
'icons-primary': 'var(--color-main-text)',
// Active state
'bg-primary-active': 'var(--color-background-dark)',
'bg-primary-hover': 'var(--color-background-hover)',
'accent-primary-active': 'var(--color-main-text)',
// Used by the save button
'accent-primary-hover': 'var(--color-primary)',
warning: 'var(--color-error)',
},
typography: {
fontFamily: 'var(--font-face)',
},
},
}
},
defaultSavedImageName() {
return basename(this.src, extname(this.src))
},
defaultSavedImageType() {
return extname(this.src).slice(1) || 'jpeg'
},
hasHighContrastEnabled() {
const themes = OCA?.Theming?.enabledThemes || []
return themes.find(theme => theme.indexOf('highcontrast') !== -1)
},
themeDataAttr() {
if (this.hasHighContrastEnabled) {
return {
'data-theme-dark-highcontrast': true,
}
}
return {
'data-theme-dark': true,
}
},
},
mounted() {
this.imageEditor = new FilerobotImageEditor(
this.$refs.editor,
this.config
)
this.imageEditor.render()
window.addEventListener('keydown', this.handleKeydown, true)
window.addEventListener('DOMNodeInserted', this.handleSfxModal)
},
beforeDestroy() {
if (this.imageEditor) {
this.imageEditor.terminate()
}
window.removeEventListener('keydown', this.handleKeydown, true)
},
methods: {
onClose(closingReason, haveNotSavedChanges) {
if (haveNotSavedChanges) {
this.onExitWithoutSaving()
return
}
window.removeEventListener('keydown', this.handleKeydown, true)
this.$emit('close')
},
/**
* User saved the image
*
* @see https://github.com/scaleflex/filerobot-image-editor#onsave
* @param {object} props destructuring object
* @param {string} props.fullName the file name
* @param {HTMLCanvasElement} props.imageCanvas the image canvas
* @param {string} props.mimeType the image mime type
* @param {number} props.quality the image saving quality
*/
async onSave({ fullName, imageCanvas, mimeType, quality }) {
const { origin, pathname } = new URL(this.src)
const putUrl = origin + join(dirname(pathname), fullName)
logger.debug('Saving image...', { putUrl, src: this.src, fullName })
// toBlob is not very smart...
mimeType = mimeType.replace('jpg', 'jpeg')
// Sanity check, 0 < quality < 1
quality = Math.max(Math.min(quality, 1), 0) || 1
try {
const blob = await new Promise(resolve => imageCanvas.toBlob(resolve, mimeType, quality))
const response = await axios.put(putUrl, new File([blob], fullName))
logger.info('Edited image saved!', { response })
showSuccess(t('viewer', 'Image saved'))
if (putUrl !== this.src) {
emit('files:file:created', { fileid: parseInt(response?.headers?.['oc-fileid']?.split('oc')[0]) || null })
} else {
this.$emit('updated')
emit('files:file:updated', { fileid: this.fileid })
}
} catch (error) {
logger.error('Error saving image', { error })
showError(t('viewer', 'Error saving image'))
}
},
/**
* Show warning if unsaved changes
*/
onExitWithoutSaving() {
OC.dialogs.confirmDestructive(
translations.changesLoseConfirmation + '\n\n' + translations.changesLoseConfirmationHint,
t('viewer', 'Unsaved changes'),
{
type: OC.dialogs.YES_NO_BUTTONS,
confirm: t('viewer', 'Drop changes'),
confirmClasses: 'error',
cancel: translations.cancel,
},
(decision) => {
if (!decision) {
return
}
this.onClose('warning-ignored', false)
}
)
},
// Key Handlers, override default Viewer arrow and escape key
handleKeydown(event) {
event.stopImmediatePropagation()
// escape key
if (event.key === 'Escape') {
// Since we cannot call the closeMethod and know if there
// are unsaved changes, let's fake a close button trigger.
event.preventDefault()
document.querySelector('.FIE_topbar-close-button').click()
}
// ctrl + S = save
if (event.ctrlKey && event.key === 's') {
event.preventDefault()
document.querySelector('.FIE_topbar-save-button').click()
}
// ctrl + Z = undo
if (event.ctrlKey && event.key === 'z') {
event.preventDefault()
document.querySelector('.FIE_topbar-undo-button').click()
}
},
/**
* Watch out for Modal inject in document root
* That way we can adjust the focusTrap
*
* @param {Event} event Dom insertion event
*/
handleSfxModal(event) {
if (event.target?.classList && event.target.classList.contains('SfxModal-Wrapper')) {
emit('viewer:trapElements:changed', event.target)
}
},
},
}
</script>
<style lang="scss" scoped>
// Take full screen size ()
.viewer__image-editor {
position: absolute;
z-index: 10100;
top: calc(var(--header-height) * -1);
bottom: calc(var(--header-height) * -1);
left: 0;
width: 100%;
height: 100vh;
}
</style>
<style lang="scss">
// Make sure the editor and its modals are above everything
.SfxModal-Wrapper {
z-index: 10101 !important;
}
.SfxPopper-wrapper {
z-index: 10102 !important;
}
// Default styling
.viewer__image-editor,
.SfxModal-Wrapper,
.SfxPopper-wrapper {
* {
// Fix font size for the entire image editor
font-size: var(--default-font-size) !important;
}
label,
button {
color: var(--color-main-text);
> span {
font-size: var(--default-font-size) !important;
}
}
// Fix button ratio and center content
button {
display: flex;
align-items: center;
justify-content: center;
min-width: 44px;
min-height: 44px;
padding: 6px 12px;
}
}
// Input styling
.SfxInput-root {
height: auto !important;
padding: 0 !important;
.SfxInput-Base {
margin: 0 !important;
}
}
// Select styling
.SfxSelect-root {
padding: 8px !important;
}
// Global buttons
.SfxButton-root {
min-height: 44px !important;
margin: 0 !important;
border: transparent !important;
&[color='error'] {
color: white !important;
background-color: var(--color-error) !important;
&:hover,
&:focus {
border-color: white !important;
background-color: var(--color-error-hover) !important;
}
}
&[color='primary'] {
color: var(--color-primary-text) !important;
background-color: var(--color-primary-element) !important;
&:hover,
&:focus {
background-color: var(--color-primary-element-hover) !important;
}
}
}
// Menu items
.SfxMenuItem-root {
height: 44px;
padding-left: 8px !important;
// Center the menu entry icon and fix width
> div {
margin-right: 0;
padding: 14px;
// Minus the parent padding-left
padding: calc(14px - 8px);
cursor: pointer;
}
// Disable jpeg saving (jpg is already here)
&[value='jpeg'] {
display: none;
}
}
// Modal
.SfxModal-Container {
min-height: 300px;
padding: 22px;
// Fill height
.SfxModal-root,
.SfxModalTitle-root {
flex: 1 1 100%;
justify-content: center;
color: var(--color-main-text);
}
.SfxModalTitle-Icon {
margin-bottom: 22px !important;
background: none !important;
// Fit EmptyContent styling
svg {
width: 64px;
height: 64px;
opacity: .4;
// Override all coloured icons
--color-primary: var(--color-main-text);
--color-error: var(--color-main-text);
}
}
// Hide close icon (use cancel button)
.SfxModalTitle-Close {
display: none !important;
}
// Modal actions buttons display
.SfxModalActions-root {
justify-content: space-evenly !important;
}
}
// Header buttons
.FIE_topbar-center-options > button,
.FIE_topbar-center-options > label {
margin-left: 6px !important;
}
// Tabs
.FIE_tabs {
padding: 6px !important;
overflow: hidden;
overflow-y: auto;
}
.FIE_tab {
width: 80px !important;
height: 80px !important;
padding: 8px;
border-radius: var(--border-radius-large) !important;
svg {
width: 16px;
height: 16px;
}
&-label {
margin-top: 8px !important;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
white-space: nowrap;
display: block !important;
}
&:hover,
&:focus {
background-color: var(--color-background-hover) !important;
}
&[aria-selected=true] {
color: var(--color-main-text);
background-color: var(--color-background-dark);
box-shadow: 0 0 0 2px var(--color-primary-element);
}
}
// Tools bar
.FIE_tools-bar {
&-wrapper {
max-height: max-content !important;
}
// Matching buttons tools
& > div[class$='-tool-button'],
& > div[class$='-tool'] {
display: flex;
align-items: center;
justify-content: center;
min-width: 44px;
height: 44px;
padding: 6px 16px;
border-radius: var(--border-radius-pill);
}
}
// Crop preset select button
.FIE_crop-presets-opener-button {
// override default button width
min-width: 0 !important;
padding: 5px !important;
padding-left: 10px !important;
border: none !important;
background-color: transparent !important;
}
// Force icon-only style
.FIE_topbar-history-buttons button,
.FIE_topbar-close-button,
.FIE_resize-ratio-locker {
border: none !important;
background-color: transparent !important;
&:hover,
&:focus {
background-color: var(--color-background-hover) !important;
}
svg {
width: 16px;
height: 16px;
}
}
// Left top bar buttons
.FIE_topbar-history-buttons button {
&.FIE_topbar-reset-button {
&::before {
content: attr(title);
font-weight: normal;
}
svg {
display: none;
}
}
}
// Save button fixes
.FIE_topbar-save-button {
color: var(--color-primary-text) !important;
border: none !important;
background-color: var(--color-primary-element) !important;
&:hover,
&:focus {
background-color: var(--color-primary-element-hover) !important;
}
}
// Save Modal fixes
.FIE_resize-tool-options {
.FIE_resize-width-option,
.FIE_resize-height-option {
flex: 1 1;
min-width: 0;
}
}
// Resize lock
.FIE_resize-ratio-locker {
margin-right: 8px !important;
// Icon is very thin
svg {
width: 20px;
height: 20px;
path {
stroke-width: 1;
stroke: var(--color-main-text);
fill: var(--color-main-text);
}
}
}
// Close editor button fixes
.FIE_topbar-close-button {
svg path {
// The path viewbox is weird and
// not correct, this fixes it
transform: scale(1.6);
}
}
// Canvas container
.FIE_canvas-container {
background-color: var(--color-main-background) !important;
}
// Loader
.FIE_spinner::after,
.FIE_spinner-label {
display: none !important;
}
.FIE_spinner-wrapper {
background-color: transparent !important;
}
.FIE_spinner::before {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
width: 28px;
height: 28px;
margin: -16px 0 0 -16px;
content: '';
-webkit-transform-origin: center;
-ms-transform-origin: center;
transform-origin: center;
-webkit-animation: rotate .8s infinite linear;
animation: rotate .8s infinite linear;
border: 2px solid var(--color-loading-light);
border-top-color: var(--color-loading-dark);
border-radius: 100%;
filter: var(--background-invert-if-dark);
}
</style>