UNPKG

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