1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin, icons } from 'ckeditor5/src/core.js';
|
9 | import { FileDialogButtonView, MenuBarMenuListItemFileDialogButtonView } from 'ckeditor5/src/ui.js';
|
10 | import { createImageTypeRegExp } from './utils.js';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export default class ImageUploadUI extends Plugin {
|
20 | |
21 |
|
22 |
|
23 | static get pluginName() {
|
24 | return 'ImageUploadUI';
|
25 | }
|
26 | |
27 |
|
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 |
|
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 |
|
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 | }
|