-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
popupAddImage.js
181 lines (153 loc) · 4.91 KB
/
popupAddImage.js
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
/**
* @fileoverview Implements PopupAddImage
* @author NHN FE Development Lab <dl_javascript@nhn.com>
*/
import extend from 'tui-code-snippet/object/extend';
import LayerPopup from './layerpopup';
import Tab from './tab';
import i18n from '../i18n';
import domUtils from '../utils/dom';
const CLASS_IMAGE_URL_INPUT = 'te-image-url-input';
const CLASS_IMAGE_FILE_INPUT = 'te-image-file-input';
const CLASS_ALT_TEXT_INPUT = 'te-alt-text-input';
const CLASS_OK_BUTTON = 'te-ok-button';
const CLASS_CLOSE_BUTTON = 'te-close-button';
const CLASS_FILE_TYPE = 'te-file-type';
const CLASS_URL_TYPE = 'te-url-type';
const CLASS_TAB_SECTION = 'te-tab-section';
const TYPE_UI = 'ui';
/**
* Class PopupAddImage
* It implements a Image Add Popup
* @param {LayerPopupOption} options - layer popup option
* @ignore
*/
class PopupAddImage extends LayerPopup {
constructor(options) {
const POPUP_CONTENT = `
<div class="${CLASS_TAB_SECTION}"></div>
<div class="${CLASS_URL_TYPE}">
<label for="">${i18n.get('Image URL')}</label>
<input type="text" class="${CLASS_IMAGE_URL_INPUT}" />
</div>
<form enctype="multipart/form-data" class="${CLASS_FILE_TYPE}">
<label for="">${i18n.get('Select image file')}</label>
<input type="file" class="${CLASS_IMAGE_FILE_INPUT}" accept="image/*" />
</form>
<label for="url">${i18n.get('Description')}</label>
<input type="text" class="${CLASS_ALT_TEXT_INPUT}" />
<div class="te-button-section">
<button type="button" class="${CLASS_OK_BUTTON}">${i18n.get('OK')}</button>
<button type="button" class="${CLASS_CLOSE_BUTTON}">${i18n.get('Cancel')}</button>
</div>
`;
options = extend(
{
header: true,
title: i18n.get('Insert image'),
className: 'te-popup-add-image tui-editor-popup',
content: POPUP_CONTENT
},
options
);
super(options);
}
/**
* init instance.
* store properties & prepare before initialize DOM
* @param {LayerPopupOption} options - layer popup options
* @private
* @override
*/
_initInstance(options) {
super._initInstance(options);
this.eventManager = options.eventManager;
}
/**
* initialize DOM, render popup
* @private
* @override
*/
_initDOM() {
super._initDOM();
const popup = this.el;
this._imageUrlInput = popup.querySelector(`.${CLASS_IMAGE_URL_INPUT}`);
this._imageFileInput = popup.querySelector(`.${CLASS_IMAGE_FILE_INPUT}`);
this._altTextInput = popup.querySelector(`.${CLASS_ALT_TEXT_INPUT}`);
const fileTypeSection = popup.querySelector(`.${CLASS_FILE_TYPE}`);
const urlTypeSection = popup.querySelector(`.${CLASS_URL_TYPE}`);
const tabSection = this.body.querySelector(`.${CLASS_TAB_SECTION}`);
this.tab = new Tab({
initName: i18n.get('File'),
items: [i18n.get('File'), i18n.get('URL')],
sections: [fileTypeSection, urlTypeSection]
});
tabSection.appendChild(this.tab.el);
}
/**
* bind DOM events
* @private
* @override
*/
_initDOMEvent() {
super._initDOMEvent();
this.on('shown', () => this._imageUrlInput.focus());
this.on('hidden', () => this._resetInputs());
this.on(`change .${CLASS_IMAGE_FILE_INPUT}`, () => {
const filename = this._imageFileInput.value.split('\\').pop();
this._altTextInput.value = filename;
});
this.on(`click .${CLASS_CLOSE_BUTTON}`, () => this.hide());
this.on(`click .${CLASS_OK_BUTTON}`, () => {
const imageUrl = this._imageUrlInput.value;
const altText = this._altTextInput.value;
if (imageUrl) {
this._applyImage(imageUrl, altText);
} else {
const { files } = this._imageFileInput;
if (files.length) {
const imageFile = files.item(0);
const hookCallback = (url, text) => this._applyImage(url, text || altText);
this.eventManager.emit('addImageBlobHook', imageFile, hookCallback, TYPE_UI);
}
}
this.hide();
});
this.tab.on('itemClick', () => this._resetInputs());
}
/**
* bind editor events
* @private
* @override
*/
_initEditorEvent() {
super._initEditorEvent();
this.eventManager.listen('focus', () => this.hide());
this.eventManager.listen('closeAllPopup', () => this.hide());
this.eventManager.listen('openPopupAddImage', () => {
this.eventManager.emit('closeAllPopup');
this.show();
});
}
_applyImage(imageUrl, altText) {
this.eventManager.emit('command', 'AddImage', {
imageUrl,
altText: altText || 'image'
});
this.hide();
}
_resetInputs() {
domUtils.findAll(this.el, 'input').forEach(input => {
input.value = '';
});
}
/**
* Remove popup
* @override
*/
remove() {
this.tab.remove();
super.remove();
}
}
export default PopupAddImage;