UNPKG

3.93 kBTypeScriptView 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 core/pendingactions
7 */
8import ContextPlugin from './contextplugin.js';
9import { type CollectionAddEvent, type CollectionRemoveEvent, type Observable } from '@ckeditor/ckeditor5-utils';
10/**
11 * The list of pending editor actions.
12 *
13 * This plugin should be used to synchronise plugins that execute long-lasting actions
14 * (e.g. file upload) with the editor integration. It gives the developer who integrates the editor
15 * an easy way to check if there are any actions pending whenever such information is needed.
16 * All plugins that register a pending action also provide a message about the action that is ongoing
17 * which can be displayed to the user. This lets them decide if they want to interrupt the action or wait.
18 *
19 * Adding and updating a pending action:
20 *
21 * ```ts
22 * const pendingActions = editor.plugins.get( 'PendingActions' );
23 * const action = pendingActions.add( 'Upload in progress: 0%.' );
24 *
25 * // You can update the message:
26 * action.message = 'Upload in progress: 10%.';
27 * ```
28 *
29 * Removing a pending action:
30 *
31 * ```ts
32 * const pendingActions = editor.plugins.get( 'PendingActions' );
33 * const action = pendingActions.add( 'Unsaved changes.' );
34 *
35 * pendingActions.remove( action );
36 * ```
37 *
38 * Getting pending actions:
39 *
40 * ```ts
41 * const pendingActions = editor.plugins.get( 'PendingActions' );
42 *
43 * const action1 = pendingActions.add( 'Action 1' );
44 * const action2 = pendingActions.add( 'Action 2' );
45 *
46 * pendingActions.first; // Returns action1
47 * Array.from( pendingActions ); // Returns [ action1, action2 ]
48 * ```
49 *
50 * This plugin is used by features like {@link module:upload/filerepository~FileRepository} to register their ongoing actions
51 * and by features like {@link module:autosave/autosave~Autosave} to detect whether there are any ongoing actions.
52 * Read more about saving the data in the
53 * {@glink getting-started/setup/getting-and-setting-data Saving and getting data} guide.
54 */
55export default class PendingActions extends ContextPlugin implements Iterable<PendingAction> {
56 /**
57 * Defines whether there is any registered pending action.
58 *
59 * @readonly
60 * @observable
61 */
62 hasAny: boolean;
63 /**
64 * A list of pending actions.
65 */
66 private _actions;
67 /**
68 * @inheritDoc
69 */
70 static get pluginName(): "PendingActions";
71 /**
72 * @inheritDoc
73 */
74 init(): void;
75 /**
76 * Adds an action to the list of pending actions.
77 *
78 * This method returns an action object with an observable message property.
79 * The action object can be later used in the {@link #remove} method. It also allows you to change the message.
80 *
81 * @param message The action message.
82 * @returns An observable object that represents a pending action.
83 */
84 add(message: string): PendingAction;
85 /**
86 * Removes an action from the list of pending actions.
87 *
88 * @param action An action object.
89 */
90 remove(action: PendingAction): void;
91 /**
92 * Returns the first action from the list or null if the list is empty
93 *
94 * @returns The pending action object.
95 */
96 get first(): PendingAction | null;
97 /**
98 * Iterable interface.
99 */
100 [Symbol.iterator](): Iterator<PendingAction>;
101}
102export interface PendingAction extends Observable {
103 message: string;
104}
105/**
106 * Fired when an action is added to the list.
107 *
108 * @eventName ~PendingActions#add
109 * @param action The added action.
110 */
111export type PendingActionsAddEvent = CollectionAddEvent<PendingAction>;
112/**
113 * Fired when an action is removed from the list.
114 *
115 * @eventName ~PendingActions#remove
116 * @param action The removed action.
117 */
118export type PendingActionsRemoveEvent = CollectionRemoveEvent<PendingAction>;