UNPKG

3.02 kBTypeScriptView Raw
1import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
2import ContextPlugin from './contextplugin';
3
4/**
5 * The list of pending editor actions.
6 *
7 * This plugin should be used to synchronise plugins that execute long-lasting actions
8 * (e.g. file upload) with the editor integration. It gives the developer who integrates the editor
9 * an easy way to check if there are any actions pending whenever such information is needed.
10 * All plugins that register a pending action also provide a message about the action that is ongoing
11 * which can be displayed to the user. This lets them decide if they want to interrupt the action or wait.
12 *
13 * Adding and updating a pending action:
14 *
15 * const pendingActions = editor.plugins.get( 'PendingActions' );
16 * const action = pendingActions.add( 'Upload in progress: 0%.' );
17 *
18 * // You can update the message:
19 * action.message = 'Upload in progress: 10%.';
20 *
21 * Removing a pending action:
22 *
23 * const pendingActions = editor.plugins.get( 'PendingActions' );
24 * const action = pendingActions.add( 'Unsaved changes.' );
25 *
26 * pendingActions.remove( action );
27 *
28 * Getting pending actions:
29 *
30 * const pendingActions = editor.plugins.get( 'PendingActions' );
31 *
32 * const action1 = pendingActions.add( 'Action 1' );
33 * const action2 = pendingActions.add( 'Action 2' );
34 *
35 * pendingActions.first; // Returns action1
36 * Array.from( pendingActions ); // Returns [ action1, action2 ]
37 *
38 * This plugin is used by features like {@link module:upload/filerepository~FileRepository} to register their ongoing actions
39 * and by features like {@link module:autosave/autosave~Autosave} to detect whether there are any ongoing actions.
40 * Read more about saving the data in the {@glink builds/guides/integration/saving-data Saving and getting data} guide.
41 */
42export default class PendingActions extends ContextPlugin implements Iterable<Observable & { message: string }> {
43 static readonly pluginName: 'PendingActions';
44 init(): void;
45 /**
46 * Defines whether there is any registered pending action.
47 */
48 get hasAny(): boolean;
49 protected set hasAny(value: boolean);
50 /**
51 * Adds an action to the list of pending actions.
52 *
53 * This method returns an action object with an observable message property.
54 * The action object can be later used in the {@link #remove} method. It also allows you to change the message.
55 */
56 add(message: string): Observable & { message: string };
57 /**
58 * Removes an action from the list of pending actions.
59 */
60 remove(action: Observable & { message: string }): void;
61 /**
62 * Returns the first action from the list or null when list is empty
63 */
64 readonly first: null | (Observable & { message: string });
65 [Symbol.iterator](): Iterator<Observable & { message: string }>;
66}
67
68declare module '@ckeditor/ckeditor5-core/src/plugincollection' {
69 interface Plugins {
70 PendingActions: PendingActions;
71 }
72}