UNPKG

5.41 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module image/imageinsert/ui/imageinserturlview
7 */
8import { icons } from 'ckeditor5/src/core.js';
9import { ButtonView, View, ViewCollection, FocusCycler, LabeledFieldView, createLabeledInputText } from 'ckeditor5/src/ui.js';
10import { FocusTracker, KeystrokeHandler } from 'ckeditor5/src/utils.js';
11/**
12 * The insert an image via URL view.
13 *
14 * See {@link module:image/imageinsert/imageinsertviaurlui~ImageInsertViaUrlUI}.
15 */
16export default class ImageInsertUrlView extends View {
17 /**
18 * Creates a view for the dropdown panel of {@link module:image/imageinsert/imageinsertui~ImageInsertUI}.
19 *
20 * @param locale The localization services instance.
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 // Navigate form fields backwards using the Shift + Tab keystroke.
36 focusPrevious: 'shift + tab',
37 // Navigate form fields forwards using the Tab key.
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 * @inheritDoc
77 */
78 render() {
79 super.render();
80 for (const view of this._focusables) {
81 this.focusTracker.add(view.element);
82 }
83 // Start listening for the keystrokes coming from #element.
84 this.keystrokes.listenTo(this.element);
85 }
86 /**
87 * @inheritDoc
88 */
89 destroy() {
90 super.destroy();
91 this.focusTracker.destroy();
92 this.keystrokes.destroy();
93 }
94 /**
95 * Creates the {@link #urlInputView}.
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 * Creates the {@link #insertButtonView}.
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 * Creates the {@link #cancelButtonView}.
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 * Focuses the view.
147 */
148 focus(direction) {
149 if (direction === -1) {
150 this.focusCycler.focusLast();
151 }
152 else {
153 this.focusCycler.focusFirst();
154 }
155 }
156}