UNPKG

3.25 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 { first } from 'ckeditor5/src/utils';
6/**
7 * @module image/imagestyle/converters
8 */
9/**
10 * Returns a converter for the `imageStyle` attribute. It can be used for adding, changing and removing the attribute.
11 *
12 * @param styles An array containing available image style options.
13 * @returns A model-to-view attribute converter.
14 */
15export function modelToViewStyleAttribute(styles) {
16 return (evt, data, conversionApi) => {
17 if (!conversionApi.consumable.consume(data.item, evt.name)) {
18 return;
19 }
20 // Check if there is class name associated with given value.
21 const newStyle = getStyleDefinitionByName(data.attributeNewValue, styles);
22 const oldStyle = getStyleDefinitionByName(data.attributeOldValue, styles);
23 const viewElement = conversionApi.mapper.toViewElement(data.item);
24 const viewWriter = conversionApi.writer;
25 if (oldStyle) {
26 viewWriter.removeClass(oldStyle.className, viewElement);
27 }
28 if (newStyle) {
29 viewWriter.addClass(newStyle.className, viewElement);
30 }
31 };
32}
33/**
34 * Returns a view-to-model converter converting image CSS classes to a proper value in the model.
35 *
36 * @param styles Image style options for which the converter is created.
37 * @returns A view-to-model converter.
38 */
39export function viewToModelStyleAttribute(styles) {
40 // Convert only non–default styles.
41 const nonDefaultStyles = {
42 imageInline: styles.filter(style => !style.isDefault && style.modelElements.includes('imageInline')),
43 imageBlock: styles.filter(style => !style.isDefault && style.modelElements.includes('imageBlock'))
44 };
45 return (evt, data, conversionApi) => {
46 if (!data.modelRange) {
47 return;
48 }
49 const viewElement = data.viewItem;
50 const modelImageElement = first(data.modelRange.getItems());
51 // Run this converter only if an image has been found in the model.
52 // In some cases it may not be found (for example if we run this on a figure with different type than image).
53 if (!modelImageElement) {
54 return;
55 }
56 // ...and the `imageStyle` attribute is allowed for that element, otherwise stop conversion early.
57 if (!conversionApi.schema.checkAttribute(modelImageElement, 'imageStyle')) {
58 return;
59 }
60 // Convert styles one by one.
61 for (const style of nonDefaultStyles[modelImageElement.name]) {
62 // Try to consume class corresponding with the style.
63 if (conversionApi.consumable.consume(viewElement, { classes: style.className })) {
64 // And convert this style to model attribute.
65 conversionApi.writer.setAttribute('imageStyle', style.name, modelImageElement);
66 }
67 }
68 };
69}
70/**
71 * Returns the style with a given `name` from an array of styles.
72 */
73function getStyleDefinitionByName(name, styles) {
74 for (const style of styles) {
75 if (style.name === name) {
76 return style;
77 }
78 }
79}