-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathimageinserturlview.ts
146 lines (121 loc) · 3.12 KB
/
imageinserturlview.ts
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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imageinsert/ui/imageinserturlview
*/
import {
View,
LabeledFieldView,
createLabeledInputText,
type InputTextView
} from 'ckeditor5/src/ui.js';
import { KeystrokeHandler, type Locale } from 'ckeditor5/src/utils.js';
/**
* The insert an image via URL view.
*
* See {@link module:image/imageinsert/imageinsertviaurlui~ImageInsertViaUrlUI}.
*/
export default class ImageInsertUrlView extends View {
/**
* The URL input field view.
*/
public urlInputView: LabeledFieldView<InputTextView>;
/**
* The value of the URL input.
*
* @observable
*/
declare public imageURLInputValue: string;
/**
* Observable property used to alter labels while some image is selected and when it is not.
*
* @observable
*/
declare public isImageSelected: boolean;
/**
* Observable property indicating whether the form interactive elements should be enabled.
*
* @observable
*/
declare public isEnabled: boolean;
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* Creates a view for the dropdown panel of {@link module:image/imageinsert/imageinsertui~ImageInsertUI}.
*
* @param locale The localization services instance.
*/
constructor( locale: Locale ) {
super( locale );
this.set( 'imageURLInputValue', '' );
this.set( 'isImageSelected', false );
this.set( 'isEnabled', true );
this.keystrokes = new KeystrokeHandler();
this.urlInputView = this._createUrlInputView();
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-image-insert-url'
]
},
children: [
this.urlInputView,
{
tag: 'div',
attributes: {
class: [
'ck',
'ck-image-insert-url__action-row'
]
}
}
]
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
// Start listening for the keystrokes coming from #element.
this.keystrokes.listenTo( this.element! );
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.keystrokes.destroy();
}
/**
* Creates the {@link #urlInputView}.
*/
private _createUrlInputView() {
const locale = this.locale!;
const t = locale.t;
const urlInputView = new LabeledFieldView( locale, createLabeledInputText );
urlInputView.bind( 'label' ).to( this, 'isImageSelected',
value => value ? t( 'Update image URL' ) : t( 'Insert image via URL' )
);
urlInputView.bind( 'isEnabled' ).to( this );
urlInputView.fieldView.inputMode = 'url';
urlInputView.fieldView.placeholder = 'https://example.com/image.png';
urlInputView.fieldView.bind( 'value' ).to( this, 'imageURLInputValue', ( value: string ) => value || '' );
urlInputView.fieldView.on( 'input', () => {
this.imageURLInputValue = urlInputView.fieldView.element!.value.trim();
} );
return urlInputView;
}
/**
* Focuses the view.
*/
public focus(): void {
this.urlInputView.focus();
}
}