1 | /**
|
2 | * @license Copyright (c) 2003-2024, 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/imageutils
|
7 | */
|
8 | import type { Element, ViewElement, DocumentSelection, ViewDocumentSelection, Selection, ViewSelection, DowncastWriter, Position, ViewContainerElement } from 'ckeditor5/src/engine.js';
|
9 | import { Plugin } from 'ckeditor5/src/core.js';
|
10 | /**
|
11 | * A set of helpers related to images.
|
12 | */
|
13 | export default class ImageUtils extends Plugin {
|
14 | /**
|
15 | * DOM Emitter.
|
16 | */
|
17 | private _domEmitter;
|
18 | /**
|
19 | * @inheritDoc
|
20 | */
|
21 | static get pluginName(): "ImageUtils";
|
22 | /**
|
23 | * Checks if the provided model element is an `image` or `imageInline`.
|
24 | */
|
25 | isImage(modelElement?: Element | null): modelElement is Element & {
|
26 | name: 'imageInline' | 'imageBlock';
|
27 | };
|
28 | /**
|
29 | * Checks if the provided view element represents an inline image.
|
30 | *
|
31 | * Also, see {@link module:image/imageutils~ImageUtils#isImageWidget}.
|
32 | */
|
33 | isInlineImageView(element?: ViewElement | null): boolean;
|
34 | /**
|
35 | * Checks if the provided view element represents a block image.
|
36 | *
|
37 | * Also, see {@link module:image/imageutils~ImageUtils#isImageWidget}.
|
38 | */
|
39 | isBlockImageView(element?: ViewElement | null): boolean;
|
40 | /**
|
41 | * Handles inserting single file. This method unifies image insertion using {@link module:widget/utils~findOptimalInsertionRange}
|
42 | * method.
|
43 | *
|
44 | * ```ts
|
45 | * const imageUtils = editor.plugins.get( 'ImageUtils' );
|
46 | *
|
47 | * imageUtils.insertImage( { src: 'path/to/image.jpg' } );
|
48 | * ```
|
49 | *
|
50 | * @param attributes Attributes of the inserted image.
|
51 | * This method filters out the attributes which are disallowed by the {@link module:engine/model/schema~Schema}.
|
52 | * @param selectable Place to insert the image. If not specified,
|
53 | * the {@link module:widget/utils~findOptimalInsertionRange} logic will be applied for the block images
|
54 | * and `model.document.selection` for the inline images.
|
55 | *
|
56 | * **Note**: If `selectable` is passed, this helper will not be able to set selection attributes (such as `linkHref`)
|
57 | * and apply them to the new image. In this case, make sure all selection attributes are passed in `attributes`.
|
58 | *
|
59 | * @param imageType Image type of inserted image. If not specified,
|
60 | * it will be determined automatically depending of editor config or place of the insertion.
|
61 | * @param options.setImageSizes Specifies whether the image `width` and `height` attributes should be set automatically.
|
62 | * The default is `true`.
|
63 | * @return The inserted model image element.
|
64 | */
|
65 | insertImage(attributes?: Record<string, unknown>, selectable?: Selection | Position | null, imageType?: ('imageBlock' | 'imageInline' | null), options?: {
|
66 | setImageSizes?: boolean;
|
67 | }): Element | null;
|
68 | /**
|
69 | * Reads original image sizes and sets them as `width` and `height`.
|
70 | *
|
71 | * The `src` attribute may not be available if the user is using an upload adapter. In such a case,
|
72 | * this method is called again after the upload process is complete and the `src` attribute is available.
|
73 | */
|
74 | setImageNaturalSizeAttributes(imageElement: Element): void;
|
75 | /**
|
76 | * Returns an image widget editing view element if one is selected or is among the selection's ancestors.
|
77 | */
|
78 | getClosestSelectedImageWidget(selection: ViewSelection | ViewDocumentSelection): ViewElement | null;
|
79 | /**
|
80 | * Returns a image model element if one is selected or is among the selection's ancestors.
|
81 | */
|
82 | getClosestSelectedImageElement(selection: Selection | DocumentSelection): Element | null;
|
83 | /**
|
84 | * Returns an image widget editing view based on the passed image view.
|
85 | */
|
86 | getImageWidgetFromImageView(imageView: ViewElement): ViewContainerElement | null;
|
87 | /**
|
88 | * Checks if image can be inserted at current model selection.
|
89 | *
|
90 | * @internal
|
91 | */
|
92 | isImageAllowed(): boolean;
|
93 | /**
|
94 | * Converts a given {@link module:engine/view/element~Element} to an image widget:
|
95 | * * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the image widget
|
96 | * element.
|
97 | * * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
|
98 | *
|
99 | * @param writer An instance of the view writer.
|
100 | * @param label The element's label. It will be concatenated with the image `alt` attribute if one is present.
|
101 | */
|
102 | toImageWidget(viewElement: ViewElement, writer: DowncastWriter, label: string): ViewElement;
|
103 | /**
|
104 | * Checks if a given view element is an image widget.
|
105 | */
|
106 | protected isImageWidget(viewElement: ViewElement): boolean;
|
107 | /**
|
108 | * Checks if the provided model element is an `image`.
|
109 | */
|
110 | isBlockImage(modelElement?: Element | null): boolean;
|
111 | /**
|
112 | * Checks if the provided model element is an `imageInline`.
|
113 | */
|
114 | isInlineImage(modelElement?: Element | null): boolean;
|
115 | /**
|
116 | * Get the view `<img>` from another view element, e.g. a widget (`<figure class="image">`), a link (`<a>`).
|
117 | *
|
118 | * The `<img>` can be located deep in other elements, so this helper performs a deep tree search.
|
119 | */
|
120 | findViewImgElement(figureView: ViewElement): ViewElement | undefined;
|
121 | /**
|
122 | * @inheritDoc
|
123 | */
|
124 | destroy(): void;
|
125 | }
|