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
|
<script>
import {
GlDropdown,
GlDropdownForm,
GlButton,
GlFormInputGroup,
GlDropdownDivider,
GlDropdownItem,
GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { acceptedMimes } from '../services/upload_helpers';
import { extractFilename } from '../services/utils';
export default {
components: {
GlDropdown,
GlDropdownForm,
GlFormInputGroup,
GlDropdownDivider,
GlDropdownItem,
GlButton,
},
directives: {
GlTooltip,
},
inject: ['tiptapEditor'],
data() {
return {
imgSrc: '',
};
},
methods: {
resetFields() {
this.imgSrc = '';
this.$refs.fileSelector.value = '';
},
insertImage() {
this.tiptapEditor
.chain()
.focus()
.setImage({
src: this.imgSrc,
canonicalSrc: this.imgSrc,
alt: extractFilename(this.imgSrc),
})
.run();
this.resetFields();
this.emitExecute();
},
emitExecute(source = 'url') {
this.$emit('execute', { contentType: 'image', value: source });
},
openFileUpload() {
this.$refs.fileSelector.click();
},
onFileSelect(e) {
this.tiptapEditor
.chain()
.focus()
.uploadAttachment({
file: e.target.files[0],
})
.run();
this.resetFields();
this.emitExecute('upload');
},
},
acceptedMimes: acceptedMimes.image,
};
</script>
<template>
<span class="gl-display-inline-flex">
<gl-dropdown
v-gl-tooltip
:text="__('Insert image')"
:title="__('Insert image')"
size="small"
category="tertiary"
icon="media"
lazy
text-sr-only
data-testid="insert-image-toolbar-button"
@hidden="resetFields()"
>
<gl-dropdown-form class="gl-px-3!">
<gl-form-input-group v-model="imgSrc" :placeholder="__('Image URL')">
<template #append>
<gl-button variant="confirm" @click="insertImage">{{ __('Insert') }}</gl-button>
</template>
</gl-form-input-group>
</gl-dropdown-form>
<gl-dropdown-divider />
<gl-dropdown-item @click="openFileUpload">
{{ __('Upload image') }}
</gl-dropdown-item>
</gl-dropdown>
<input
ref="fileSelector"
type="file"
name="content_editor_image"
:accept="$options.acceptedMimes"
class="gl-display-none"
data-qa-selector="file_upload_field"
@change="onFileSelect"
/>
</span>
</template>
|