UNPKG

2.18 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 image/imagetoolbar
7 */
8import { Plugin } from 'ckeditor5/src/core';
9import { WidgetToolbarRepository } from 'ckeditor5/src/widget';
10import ImageUtils from './imageutils';
11import { isObject } from 'lodash-es';
12/**
13 * The image toolbar plugin. It creates and manages the image toolbar (the toolbar displayed when an image is selected).
14 *
15 * For an overview, check the {@glink features/images/images-overview#image-contextual-toolbar image contextual toolbar} documentation.
16 *
17 * Instances of toolbar components (e.g. buttons) are created using the editor's
18 * {@link module:ui/componentfactory~ComponentFactory component factory}
19 * based on the {@link module:image/imageconfig~ImageConfig#toolbar `image.toolbar` configuration option}.
20 *
21 * The toolbar uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}.
22 */
23export default class ImageToolbar extends Plugin {
24 /**
25 * @inheritDoc
26 */
27 static get requires() {
28 return [WidgetToolbarRepository, ImageUtils];
29 }
30 /**
31 * @inheritDoc
32 */
33 static get pluginName() {
34 return 'ImageToolbar';
35 }
36 /**
37 * @inheritDoc
38 */
39 afterInit() {
40 const editor = this.editor;
41 const t = editor.t;
42 const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
43 const imageUtils = editor.plugins.get('ImageUtils');
44 widgetToolbarRepository.register('image', {
45 ariaLabel: t('Image toolbar'),
46 items: normalizeDeclarativeConfig(editor.config.get('image.toolbar') || []),
47 getRelatedElement: selection => imageUtils.getClosestSelectedImageWidget(selection)
48 });
49 }
50}
51/**
52 * Convert the dropdown definitions to their keys registered in the ComponentFactory.
53 * The registration precess should be handled by the plugin which handles the UI of a particular feature.
54 */
55function normalizeDeclarativeConfig(config) {
56 return config.map(item => isObject(item) ? item.name : item);
57}