UNPKG

3.86 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, 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 media-embed/mediaembedui
7 */
8import { Plugin } from 'ckeditor5/src/core';
9import { createDropdown, CssTransitionDisablerMixin } from 'ckeditor5/src/ui';
10import MediaFormView from './ui/mediaformview';
11import MediaEmbedEditing from './mediaembedediting';
12import mediaIcon from '../theme/icons/media.svg';
13/**
14 * The media embed UI plugin.
15 */
16export default class MediaEmbedUI extends Plugin {
17 /**
18 * @inheritDoc
19 */
20 static get requires() {
21 return [MediaEmbedEditing];
22 }
23 /**
24 * @inheritDoc
25 */
26 static get pluginName() {
27 return 'MediaEmbedUI';
28 }
29 /**
30 * @inheritDoc
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 // Note: Use the low priority to make sure the following listener starts working after the
50 // default action of the drop-down is executed (i.e. the panel showed up). Otherwise, the
51 // invisible form/input cannot be focused/selected.
52 button.on('open', () => {
53 form.disableCssTransitions();
54 // Make sure that each time the panel shows up, the URL field remains in sync with the value of
55 // the command. If the user typed in the input, then canceled (`urlInputView#fieldView#value` stays
56 // unaltered) and re-opened it without changing the value of the media command (e.g. because they
57 // didn't change the selection), they would see the old value instead of the actual value of the
58 // command.
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 // Form elements should be read-only when corresponding commands are disabled.
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}
86function 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}