UNPKG

3.34 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/imageresize/imageresizeediting
8 */
9
10import { Plugin } from 'ckeditor5/src/core';
11import ImageUtils from '../imageutils';
12import ResizeImageCommand from './resizeimagecommand';
13
14/**
15 * The image resize editing feature.
16 *
17 * It adds the ability to resize each image using handles or manually by
18 * {@link module:image/imageresize/imageresizebuttons~ImageResizeButtons} buttons.
19 *
20 * @extends module:core/plugin~Plugin
21 */
22export default class ImageResizeEditing extends Plugin {
23 /**
24 * @inheritDoc
25 */
26 static get requires() {
27 return [ ImageUtils ];
28 }
29
30 /**
31 * @inheritDoc
32 */
33 static get pluginName() {
34 return 'ImageResizeEditing';
35 }
36
37 /**
38 * @inheritDoc
39 */
40 constructor( editor ) {
41 super( editor );
42
43 editor.config.define( 'image', {
44 resizeUnit: '%',
45 resizeOptions: [ {
46 name: 'resizeImage:original',
47 value: null,
48 icon: 'original'
49 },
50 {
51 name: 'resizeImage:25',
52 value: '25',
53 icon: 'small'
54 },
55 {
56 name: 'resizeImage:50',
57 value: '50',
58 icon: 'medium'
59 },
60 {
61 name: 'resizeImage:75',
62 value: '75',
63 icon: 'large'
64 } ]
65 } );
66 }
67
68 /**
69 * @inheritDoc
70 */
71 init() {
72 const editor = this.editor;
73 const resizeImageCommand = new ResizeImageCommand( editor );
74
75 this._registerSchema();
76 this._registerConverters( 'imageBlock' );
77 this._registerConverters( 'imageInline' );
78
79 // Register `resizeImage` command and add `imageResize` command as an alias for backward compatibility.
80 editor.commands.add( 'resizeImage', resizeImageCommand );
81 editor.commands.add( 'imageResize', resizeImageCommand );
82 }
83
84 /**
85 * @private
86 */
87 _registerSchema() {
88 if ( this.editor.plugins.has( 'ImageBlockEditing' ) ) {
89 this.editor.model.schema.extend( 'imageBlock', { allowAttributes: 'width' } );
90 }
91
92 if ( this.editor.plugins.has( 'ImageInlineEditing' ) ) {
93 this.editor.model.schema.extend( 'imageInline', { allowAttributes: 'width' } );
94 }
95 }
96
97 /**
98 * Registers image resize converters.
99 *
100 * @private
101 * @param {'imageBlock'|'imageInline'} imageType The type of the image.
102 */
103 _registerConverters( imageType ) {
104 const editor = this.editor;
105
106 // Dedicated converter to propagate image's attribute to the img tag.
107 editor.conversion.for( 'downcast' ).add( dispatcher =>
108 dispatcher.on( `attribute:width:${ imageType }`, ( evt, data, conversionApi ) => {
109 if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
110 return;
111 }
112
113 const viewWriter = conversionApi.writer;
114 const figure = conversionApi.mapper.toViewElement( data.item );
115
116 if ( data.attributeNewValue !== null ) {
117 viewWriter.setStyle( 'width', data.attributeNewValue, figure );
118 viewWriter.addClass( 'image_resized', figure );
119 } else {
120 viewWriter.removeStyle( 'width', figure );
121 viewWriter.removeClass( 'image_resized', figure );
122 }
123 } )
124 );
125
126 editor.conversion.for( 'upcast' )
127 .attributeToAttribute( {
128 view: {
129 name: imageType === 'imageBlock' ? 'figure' : 'img',
130 styles: {
131 width: /.+/
132 }
133 },
134 model: {
135 key: 'width',
136 value: viewElement => viewElement.getStyle( 'width' )
137 }
138 } );
139 }
140}