UNPKG

4.38 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/imageupload/imageuploadui
7 */
8import { Plugin, icons } from 'ckeditor5/src/core.js';
9import { FileDialogButtonView, MenuBarMenuListItemFileDialogButtonView } from 'ckeditor5/src/ui.js';
10import { createImageTypeRegExp } from './utils.js';
11/**
12 * The image upload button plugin.
13 *
14 * For a detailed overview, check the {@glink features/images/image-upload/image-upload Image upload feature} documentation.
15 *
16 * Adds the `'uploadImage'` button to the {@link module:ui/componentfactory~ComponentFactory UI component factory}
17 * and also the `imageUpload` button as an alias for backward compatibility.
18 */
19export default class ImageUploadUI extends Plugin {
20 /**
21 * @inheritDoc
22 */
23 static get pluginName() {
24 return 'ImageUploadUI';
25 }
26 /**
27 * @inheritDoc
28 */
29 init() {
30 const editor = this.editor;
31 const t = editor.t;
32 const toolbarComponentCreator = () => {
33 const button = this._createButton(FileDialogButtonView);
34 button.set({
35 label: t('Upload image from computer'),
36 tooltip: true
37 });
38 return button;
39 };
40 // Setup `uploadImage` button and add `imageUpload` button as an alias for backward compatibility.
41 editor.ui.componentFactory.add('uploadImage', toolbarComponentCreator);
42 editor.ui.componentFactory.add('imageUpload', toolbarComponentCreator);
43 editor.ui.componentFactory.add('menuBar:uploadImage', () => {
44 const button = this._createButton(MenuBarMenuListItemFileDialogButtonView);
45 button.label = t('Image from computer');
46 return button;
47 });
48 if (editor.plugins.has('ImageInsertUI')) {
49 const imageInsertUI = editor.plugins.get('ImageInsertUI');
50 imageInsertUI.registerIntegration({
51 name: 'upload',
52 observable: () => editor.commands.get('uploadImage'),
53 buttonViewCreator: () => {
54 const uploadImageButton = editor.ui.componentFactory.create('uploadImage');
55 uploadImageButton.bind('label').to(imageInsertUI, 'isImageSelected', isImageSelected => isImageSelected ?
56 t('Replace image from computer') :
57 t('Upload image from computer'));
58 return uploadImageButton;
59 },
60 formViewCreator: () => {
61 const uploadImageButton = editor.ui.componentFactory.create('uploadImage');
62 uploadImageButton.withText = true;
63 uploadImageButton.bind('label').to(imageInsertUI, 'isImageSelected', isImageSelected => isImageSelected ?
64 t('Replace from computer') :
65 t('Upload from computer'));
66 uploadImageButton.on('execute', () => {
67 imageInsertUI.dropdownView.isOpen = false;
68 });
69 return uploadImageButton;
70 }
71 });
72 }
73 }
74 /**
75 * Creates a button for image upload command to use either in toolbar or in menu bar.
76 */
77 _createButton(ButtonClass) {
78 const editor = this.editor;
79 const locale = editor.locale;
80 const command = editor.commands.get('uploadImage');
81 const imageTypes = editor.config.get('image.upload.types');
82 const imageTypesRegExp = createImageTypeRegExp(imageTypes);
83 const view = new ButtonClass(editor.locale);
84 const t = locale.t;
85 view.set({
86 acceptedType: imageTypes.map(type => `image/${type}`).join(','),
87 allowMultipleFiles: true,
88 label: t('Upload image from computer'),
89 icon: icons.imageUpload
90 });
91 view.bind('isEnabled').to(command);
92 view.on('done', (evt, files) => {
93 const imagesToUpload = Array.from(files).filter(file => imageTypesRegExp.test(file.type));
94 if (imagesToUpload.length) {
95 editor.execute('uploadImage', { file: imagesToUpload });
96 editor.editing.view.focus();
97 }
98 });
99 return view;
100 }
101}