/
filedialogbuttonview.ts
186 lines (160 loc) · 3.84 KB
/
filedialogbuttonview.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
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
182
183
184
185
186
/**
* @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 ui/button/filedialogbuttonview
*/
import View from '../view.js';
import ButtonView from './buttonview.js';
import type { ButtonExecuteEvent } from './button.js';
import type { Locale } from '@ckeditor/ckeditor5-utils';
/**
* The file dialog button view.
*
* This component provides a button that opens the native file selection dialog.
* It can be used to implement the UI of a file upload feature.
*
* ```ts
* const view = new FileDialogButtonView( locale );
*
* view.set( {
* acceptedType: 'image/*',
* allowMultipleFiles: true
* label: t( 'Insert image' ),
* icon: imageIcon,
* tooltip: true
* } );
*
* view.on( 'done', ( evt, files ) => {
* for ( const file of Array.from( files ) ) {
* console.log( 'Selected file', file );
* }
* } );
* ```
*/
export default class FileDialogButtonView extends ButtonView {
/**
* The button view of the component.
*
* @deprecated
*/
public buttonView: ButtonView;
/**
* A hidden `<input>` view used to execute file dialog.
*/
private _fileInputView: FileInputView;
/**
* Accepted file types. Can be provided in form of file extensions, media type or one of:
* * `audio/*`,
* * `video/*`,
* * `image/*`.
*
* @observable
*/
declare public acceptedType: string;
/**
* Indicates if multiple files can be selected. Defaults to `true`.
*
* @observable
*/
declare public allowMultipleFiles: boolean;
/**
* @inheritDoc
*/
constructor( locale?: Locale ) {
super( locale );
// For backward compatibility.
this.buttonView = this;
this._fileInputView = new FileInputView( locale );
this._fileInputView.bind( 'acceptedType' ).to( this );
this._fileInputView.bind( 'allowMultipleFiles' ).to( this );
this._fileInputView.delegate( 'done' ).to( this );
this.on<ButtonExecuteEvent>( 'execute', () => {
this._fileInputView.open();
} );
this.extendTemplate( {
attributes: {
class: 'ck-file-dialog-button'
}
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.children.add( this._fileInputView );
}
}
/**
* The hidden file input view class.
*/
class FileInputView extends View<HTMLInputElement> {
/**
* Accepted file types. Can be provided in form of file extensions, media type or one of:
* * `audio/*`,
* * `video/*`,
* * `image/*`.
*
* @observable
*/
declare public acceptedType?: string;
/**
* Indicates if multiple files can be selected. Defaults to `false`.
*
* @observable
*/
declare public allowMultipleFiles: boolean;
/**
* @inheritDoc
*/
constructor( locale?: Locale ) {
super( locale );
this.set( 'acceptedType', undefined );
this.set( 'allowMultipleFiles', false );
const bind = this.bindTemplate;
this.setTemplate( {
tag: 'input',
attributes: {
class: [
'ck-hidden'
],
type: 'file',
tabindex: '-1',
accept: bind.to( 'acceptedType' ),
multiple: bind.to( 'allowMultipleFiles' )
},
on: {
// Removing from code coverage since we cannot programmatically set input element files.
change: bind.to( /* istanbul ignore next -- @preserve */ () => {
if ( this.element && this.element.files && this.element.files.length ) {
this.fire<FileInputViewDoneEvent>( 'done', this.element.files );
}
this.element!.value = '';
} )
}
} );
}
/**
* Opens file dialog.
*/
public open(): void {
this.element!.click();
}
}
/**
* Fired when file dialog is closed with file selected.
*
* ```ts
* view.on( 'done', ( evt, files ) => {
* for ( const file of files ) {
* console.log( 'Selected file', file );
* }
* }
* ```
*/
export type FileInputViewDoneEvent = {
name: 'done';
args: [ files: FileList ];
};