1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { icons } from 'ckeditor5/src/core.js';
|
9 | import { ButtonView, View, ViewCollection, FocusCycler, LabeledFieldView, createLabeledInputText } from 'ckeditor5/src/ui.js';
|
10 | import { FocusTracker, KeystrokeHandler } from 'ckeditor5/src/utils.js';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export default class ImageInsertUrlView extends View {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 | constructor(locale) {
|
23 | super(locale);
|
24 | this.set('imageURLInputValue', '');
|
25 | this.set('isImageSelected', false);
|
26 | this.set('isEnabled', true);
|
27 | this.focusTracker = new FocusTracker();
|
28 | this.keystrokes = new KeystrokeHandler();
|
29 | this._focusables = new ViewCollection();
|
30 | this.focusCycler = new FocusCycler({
|
31 | focusables: this._focusables,
|
32 | focusTracker: this.focusTracker,
|
33 | keystrokeHandler: this.keystrokes,
|
34 | actions: {
|
35 |
|
36 | focusPrevious: 'shift + tab',
|
37 |
|
38 | focusNext: 'tab'
|
39 | }
|
40 | });
|
41 | this.urlInputView = this._createUrlInputView();
|
42 | this.insertButtonView = this._createInsertButton();
|
43 | this.cancelButtonView = this._createCancelButton();
|
44 | this._focusables.addMany([
|
45 | this.urlInputView,
|
46 | this.insertButtonView,
|
47 | this.cancelButtonView
|
48 | ]);
|
49 | this.setTemplate({
|
50 | tag: 'div',
|
51 | attributes: {
|
52 | class: [
|
53 | 'ck',
|
54 | 'ck-image-insert-url'
|
55 | ]
|
56 | },
|
57 | children: [
|
58 | this.urlInputView,
|
59 | {
|
60 | tag: 'div',
|
61 | attributes: {
|
62 | class: [
|
63 | 'ck',
|
64 | 'ck-image-insert-url__action-row'
|
65 | ]
|
66 | },
|
67 | children: [
|
68 | this.insertButtonView,
|
69 | this.cancelButtonView
|
70 | ]
|
71 | }
|
72 | ]
|
73 | });
|
74 | }
|
75 | |
76 |
|
77 |
|
78 | render() {
|
79 | super.render();
|
80 | for (const view of this._focusables) {
|
81 | this.focusTracker.add(view.element);
|
82 | }
|
83 |
|
84 | this.keystrokes.listenTo(this.element);
|
85 | }
|
86 | |
87 |
|
88 |
|
89 | destroy() {
|
90 | super.destroy();
|
91 | this.focusTracker.destroy();
|
92 | this.keystrokes.destroy();
|
93 | }
|
94 | |
95 |
|
96 |
|
97 | _createUrlInputView() {
|
98 | const locale = this.locale;
|
99 | const t = locale.t;
|
100 | const urlInputView = new LabeledFieldView(locale, createLabeledInputText);
|
101 | urlInputView.bind('label').to(this, 'isImageSelected', value => value ? t('Update image URL') : t('Insert image via URL'));
|
102 | urlInputView.bind('isEnabled').to(this);
|
103 | urlInputView.fieldView.placeholder = 'https://example.com/image.png';
|
104 | urlInputView.fieldView.bind('value').to(this, 'imageURLInputValue', (value) => value || '');
|
105 | urlInputView.fieldView.on('input', () => {
|
106 | this.imageURLInputValue = urlInputView.fieldView.element.value.trim();
|
107 | });
|
108 | return urlInputView;
|
109 | }
|
110 | |
111 |
|
112 |
|
113 | _createInsertButton() {
|
114 | const locale = this.locale;
|
115 | const t = locale.t;
|
116 | const insertButtonView = new ButtonView(locale);
|
117 | insertButtonView.set({
|
118 | icon: icons.check,
|
119 | class: 'ck-button-save',
|
120 | type: 'submit',
|
121 | withText: true
|
122 | });
|
123 | insertButtonView.bind('label').to(this, 'isImageSelected', value => value ? t('Update') : t('Insert'));
|
124 | insertButtonView.bind('isEnabled').to(this, 'imageURLInputValue', this, 'isEnabled', (...values) => values.every(value => value));
|
125 | insertButtonView.delegate('execute').to(this, 'submit');
|
126 | return insertButtonView;
|
127 | }
|
128 | |
129 |
|
130 |
|
131 | _createCancelButton() {
|
132 | const locale = this.locale;
|
133 | const t = locale.t;
|
134 | const cancelButtonView = new ButtonView(locale);
|
135 | cancelButtonView.set({
|
136 | label: t('Cancel'),
|
137 | icon: icons.cancel,
|
138 | class: 'ck-button-cancel',
|
139 | withText: true
|
140 | });
|
141 | cancelButtonView.bind('isEnabled').to(this);
|
142 | cancelButtonView.delegate('execute').to(this, 'cancel');
|
143 | return cancelButtonView;
|
144 | }
|
145 | |
146 |
|
147 |
|
148 | focus(direction) {
|
149 | if (direction === -1) {
|
150 | this.focusCycler.focusLast();
|
151 | }
|
152 | else {
|
153 | this.focusCycler.focusFirst();
|
154 | }
|
155 | }
|
156 | }
|