UNPKG

2.06 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/image/imageediting
8 */
9
10import { Plugin } from 'ckeditor5/src/core';
11import ImageLoadObserver from './imageloadobserver';
12import InsertImageCommand from './insertimagecommand';
13import ImageUtils from '../imageutils';
14
15/**
16 * The image engine plugin. This module loads common code shared between
17 * {@link module:image/image/imageinlineediting~ImageInlineEditing} and
18 * {@link module:image/image/imageblockediting~ImageBlockEditing} plugins.
19 *
20 * This plugin registers the {@link module:image/image/insertimagecommand~InsertImageCommand 'insertImage'} command.
21 *
22 * @extends module:core/plugin~Plugin
23 */
24export default class ImageEditing extends Plugin {
25 /**
26 * @inheritDoc
27 */
28 static get requires() {
29 return [ ImageUtils ];
30 }
31
32 /**
33 * @inheritDoc
34 */
35 static get pluginName() {
36 return 'ImageEditing';
37 }
38
39 /**
40 * @inheritDoc
41 */
42 init() {
43 const editor = this.editor;
44 const conversion = editor.conversion;
45
46 // See https://github.com/ckeditor/ckeditor5-image/issues/142.
47 editor.editing.view.addObserver( ImageLoadObserver );
48
49 conversion.for( 'upcast' )
50 .attributeToAttribute( {
51 view: {
52 name: 'img',
53 key: 'alt'
54 },
55 model: 'alt'
56 } )
57 .attributeToAttribute( {
58 view: {
59 name: 'img',
60 key: 'srcset'
61 },
62 model: {
63 key: 'srcset',
64 value: viewImage => {
65 const value = {
66 data: viewImage.getAttribute( 'srcset' )
67 };
68
69 if ( viewImage.hasAttribute( 'width' ) ) {
70 value.width = viewImage.getAttribute( 'width' );
71 }
72
73 return value;
74 }
75 }
76 } );
77
78 const insertImageCommand = new InsertImageCommand( editor );
79
80 // Register `insertImage` command and add `imageInsert` command as an alias for backward compatibility.
81 editor.commands.add( 'insertImage', insertImageCommand );
82 editor.commands.add( 'imageInsert', insertImageCommand );
83 }
84}