UNPKG

1.8 kBJavaScriptView Raw
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/image/imageloadobserver
7 */
8import { Observer } from 'ckeditor5/src/engine.js';
9/**
10 * Observes all new images added to the {@link module:engine/view/document~Document},
11 * fires {@link module:engine/view/document~Document#event:imageLoaded} and
12 * {@link module:engine/view/document~Document#event:layoutChanged} event every time when the new image
13 * has been loaded.
14 *
15 * **Note:** This event is not fired for images that has been added to the document and rendered as `complete` (already loaded).
16 */
17export default class ImageLoadObserver extends Observer {
18 /**
19 * @inheritDoc
20 */
21 observe(domRoot) {
22 this.listenTo(domRoot, 'load', (event, domEvent) => {
23 const domElement = domEvent.target;
24 if (this.checkShouldIgnoreEventFromTarget(domElement)) {
25 return;
26 }
27 if (domElement.tagName == 'IMG') {
28 this._fireEvents(domEvent);
29 }
30 // Use capture phase for better performance (#4504).
31 }, { useCapture: true });
32 }
33 /**
34 * @inheritDoc
35 */
36 stopObserving(domRoot) {
37 this.stopListening(domRoot);
38 }
39 /**
40 * Fires {@link module:engine/view/document~Document#event:layoutChanged} and
41 * {@link module:engine/view/document~Document#event:imageLoaded}
42 * if observer {@link #isEnabled is enabled}.
43 *
44 * @param domEvent The DOM event.
45 */
46 _fireEvents(domEvent) {
47 if (this.isEnabled) {
48 this.document.fire('layoutChanged');
49 this.document.fire('imageLoaded', domEvent);
50 }
51 }
52}