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