UNPKG

2.43 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, 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/**
7 * @module image/imageinsert/utils
8 */
9
10import { LabeledFieldView, createLabeledInputText } from 'ckeditor5/src/ui';
11
12/**
13 * Creates integrations object that will be passed to the
14 * {@link module:image/imageinsert/ui/imageinsertpanelview~ImageInsertPanelView}.
15 *
16 * @param {module:core/editor/editor~Editor} editor Editor instance.
17 *
18 * @returns {Object.<String, module:ui/view~View>} Integrations object.
19 */
20export function prepareIntegrations( editor ) {
21 const panelItems = editor.config.get( 'image.insert.integrations' );
22 const imageInsertUIPlugin = editor.plugins.get( 'ImageInsertUI' );
23
24 const PREDEFINED_INTEGRATIONS = {
25 'insertImageViaUrl': createLabeledInputView( editor.locale )
26 };
27
28 if ( !panelItems ) {
29 return PREDEFINED_INTEGRATIONS;
30 }
31
32 // Prepares ckfinder component for the `openCKFinder` integration token.
33 if ( panelItems.find( item => item === 'openCKFinder' ) && editor.ui.componentFactory.has( 'ckfinder' ) ) {
34 const ckFinderButton = editor.ui.componentFactory.create( 'ckfinder' );
35 ckFinderButton.set( {
36 withText: true,
37 class: 'ck-image-insert__ck-finder-button'
38 } );
39
40 // We want to close the dropdown panel view when user clicks the ckFinderButton.
41 ckFinderButton.delegate( 'execute' ).to( imageInsertUIPlugin, 'cancel' );
42
43 PREDEFINED_INTEGRATIONS.openCKFinder = ckFinderButton;
44 }
45
46 // Creates integrations object of valid views to pass it to the ImageInsertPanelView.
47 return panelItems.reduce( ( object, key ) => {
48 if ( PREDEFINED_INTEGRATIONS[ key ] ) {
49 object[ key ] = PREDEFINED_INTEGRATIONS[ key ];
50 } else if ( editor.ui.componentFactory.has( key ) ) {
51 object[ key ] = editor.ui.componentFactory.create( key );
52 }
53
54 return object;
55 }, {} );
56}
57
58/**
59 * Creates labeled field view.
60 *
61 * @param {module:utils/locale~Locale} locale The localization services instance.
62 *
63 * @returns {module:ui/labeledfield/labeledfieldview~LabeledFieldView}
64 */
65export function createLabeledInputView( locale ) {
66 const t = locale.t;
67 const labeledInputView = new LabeledFieldView( locale, createLabeledInputText );
68
69 labeledInputView.set( {
70 label: t( 'Insert image via URL' )
71 } );
72 labeledInputView.fieldView.placeholder = 'https://example.com/image.png';
73
74 return labeledInputView;
75}