UNPKG

3.76 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/imagetoolbar
8 */
9
10import { Plugin } from 'ckeditor5/src/core';
11import { WidgetToolbarRepository } from 'ckeditor5/src/widget';
12import ImageUtils from './imageutils';
13import { isObject } from 'lodash-es';
14
15/**
16 * The image toolbar plugin. It creates and manages the image toolbar (the toolbar displayed when an image is selected).
17 *
18 * For an overview, check the {@glink features/images/images-overview#image-contextual-toolbar image contextual toolbar} documentation.
19 *
20 * Instances of toolbar components (e.g. buttons) are created using the editor's
21 * {@link module:ui/componentfactory~ComponentFactory component factory}
22 * based on the {@link module:image/image~ImageConfig#toolbar `image.toolbar` configuration option}.
23 *
24 * The toolbar uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}.
25 *
26 * @extends module:core/plugin~Plugin
27 */
28export default class ImageToolbar extends Plugin {
29 /**
30 * @inheritDoc
31 */
32 static get requires() {
33 return [ WidgetToolbarRepository, ImageUtils ];
34 }
35
36 /**
37 * @inheritDoc
38 */
39 static get pluginName() {
40 return 'ImageToolbar';
41 }
42
43 /**
44 * @inheritDoc
45 */
46 afterInit() {
47 const editor = this.editor;
48 const t = editor.t;
49 const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
50 const imageUtils = editor.plugins.get( 'ImageUtils' );
51
52 widgetToolbarRepository.register( 'image', {
53 ariaLabel: t( 'Image toolbar' ),
54 items: normalizeDeclarativeConfig( editor.config.get( 'image.toolbar' ) || [] ),
55 getRelatedElement: selection => imageUtils.getClosestSelectedImageWidget( selection )
56 } );
57 }
58}
59
60/**
61 * Items to be placed in the image toolbar.
62 * This option is used by the {@link module:image/imagetoolbar~ImageToolbar} feature.
63 *
64 * Assuming that you use the following features:
65 *
66 * * {@link module:image/imagestyle~ImageStyle} (with a default configuration),
67 * * {@link module:image/imagetextalternative~ImageTextAlternative},
68 * * {@link module:image/imagecaption~ImageCaption},
69 *
70 * the following toolbar items will be available in {@link module:ui/componentfactory~ComponentFactory}:
71 * * `'imageTextAlternative'`,
72 * * `'toggleImageCaption'`,
73 * * {@link module:image/image~ImageConfig#styles buttons provided by the `ImageStyle` plugin},
74 * * {@link module:image/imagestyle/utils~DEFAULT_DROPDOWN_DEFINITIONS drop-downs provided by the `ImageStyle` plugin},
75 *
76 * so you can configure the toolbar like this:
77 *
78 * const imageConfig = {
79 * toolbar: [
80 * 'imageStyle:inline', 'imageStyle:wrapText', 'imageStyle:breakText', '|',
81 * 'toggleImageCaption', 'imageTextAlternative'
82 * ]
83 * };
84 *
85 * Besides that, the `ImageStyle` plugin allows to define a
86 * {@link module:image/imagestyle/imagestyleui~ImageStyleDropdownDefinition custom drop-down} while configuring the toolbar.
87 *
88 * The same items can also be used in the {@link module:core/editor/editorconfig~EditorConfig#toolbar main editor toolbar}.
89 *
90 * Read more about configuring toolbar in {@link module:core/editor/editorconfig~EditorConfig#toolbar}.
91 *
92 * @member {Array.<String>} module:image/image~ImageConfig#toolbar
93 */
94
95// Convert the dropdown definitions to their keys registered in the ComponentFactory.
96// The registration precess should be handled by the plugin which handles the UI of a particular feature.
97//
98// @param {Array.<String|module:image/imagestyle/imagestyleui~ImageStyleDropdownDefinition>} config
99//
100// @returns {Array.<String>}
101function normalizeDeclarativeConfig( config ) {
102 return config.map( item => isObject( item ) ? item.name : item );
103}