1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Plugin } from 'ckeditor5/src/core';
|
9 | import { createDropdown, CssTransitionDisablerMixin } from 'ckeditor5/src/ui';
|
10 | import MediaFormView from './ui/mediaformview';
|
11 | import MediaEmbedEditing from './mediaembedediting';
|
12 | import mediaIcon from '../theme/icons/media.svg';
|
13 |
|
14 |
|
15 |
|
16 | export default class MediaEmbedUI extends Plugin {
|
17 | |
18 |
|
19 |
|
20 | static get requires() {
|
21 | return [MediaEmbedEditing];
|
22 | }
|
23 | |
24 |
|
25 |
|
26 | static get pluginName() {
|
27 | return 'MediaEmbedUI';
|
28 | }
|
29 | |
30 |
|
31 |
|
32 | init() {
|
33 | const editor = this.editor;
|
34 | const command = editor.commands.get('mediaEmbed');
|
35 | editor.ui.componentFactory.add('mediaEmbed', locale => {
|
36 | const dropdown = createDropdown(locale);
|
37 | this._setUpDropdown(dropdown, command);
|
38 | return dropdown;
|
39 | });
|
40 | }
|
41 | _setUpDropdown(dropdown, command) {
|
42 | const editor = this.editor;
|
43 | const t = editor.t;
|
44 | const button = dropdown.buttonView;
|
45 | const registry = editor.plugins.get(MediaEmbedEditing).registry;
|
46 | dropdown.once('change:isOpen', () => {
|
47 | const form = new (CssTransitionDisablerMixin(MediaFormView))(getFormValidators(editor.t, registry), editor.locale);
|
48 | dropdown.panelView.children.add(form);
|
49 |
|
50 |
|
51 |
|
52 | button.on('open', () => {
|
53 | form.disableCssTransitions();
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | form.url = command.value || '';
|
60 | form.urlInputView.fieldView.select();
|
61 | form.enableCssTransitions();
|
62 | }, { priority: 'low' });
|
63 | dropdown.on('submit', () => {
|
64 | if (form.isValid()) {
|
65 | editor.execute('mediaEmbed', form.url);
|
66 | editor.editing.view.focus();
|
67 | }
|
68 | });
|
69 | dropdown.on('change:isOpen', () => form.resetFormStatus());
|
70 | dropdown.on('cancel', () => {
|
71 | editor.editing.view.focus();
|
72 | });
|
73 | form.delegate('submit', 'cancel').to(dropdown);
|
74 | form.urlInputView.fieldView.bind('value').to(command, 'value');
|
75 |
|
76 | form.urlInputView.bind('isEnabled').to(command, 'isEnabled');
|
77 | });
|
78 | dropdown.bind('isEnabled').to(command);
|
79 | button.set({
|
80 | label: t('Insert media'),
|
81 | icon: mediaIcon,
|
82 | tooltip: true
|
83 | });
|
84 | }
|
85 | }
|
86 | function getFormValidators(t, registry) {
|
87 | return [
|
88 | form => {
|
89 | if (!form.url.length) {
|
90 | return t('The URL must not be empty.');
|
91 | }
|
92 | },
|
93 | form => {
|
94 | if (!registry.hasMedia(form.url)) {
|
95 | return t('This media URL is not supported.');
|
96 | }
|
97 | }
|
98 | ];
|
99 | }
|