UNPKG

4.97 kBTypeScriptView Raw
1import type { AttributesProps, CommandFunction, CommandFunctionProps, FromToProps, MarkType, MarkTypeProps, PrimitiveSelection, ProsemirrorAttributes } from '@remirror/core-types';
2/**
3 * The parameter that is passed into `DelayedCommand`s.
4 */
5interface DelayedCommandProps<Value> {
6 /**
7 * Runs as soon as the command is triggered. For most delayed commands within
8 * the `remirror` codebase this is used to add a position tracker to the
9 * document.
10 */
11 immediate?: CommandFunction;
12 /**
13 * The promise that provides the value that the `onDone` callback uses to
14 * complete the delayed command.
15 */
16 promise: DelayedValue<Value>;
17 /**
18 * Called when the provided promise resolves.
19 */
20 onDone: CommandFunction<{
21 value: Value;
22 }>;
23 /**
24 * Called when the promise fails. This could be used to cleanup the active
25 * position trackers when the delayed command fails.
26 */
27 onFail?: CommandFunction;
28}
29export type DelayedValue<Type> = Promise<Type> | (() => Promise<Type>);
30/**
31 * Returns `true` when the provided value is a delayed value.
32 */
33export declare function isDelayedValue<Type>(value: unknown): value is DelayedValue<Type>;
34/**
35 * Add tentative support for delayed commands in the editor.
36 *
37 * Delayed commands are commands that run an immediate action, like adding a
38 * tracker to a position in the document. Once the promise that is provided is
39 * returned the `onDone` parameter is run with the document in the current
40 * state. The tracker that was added can now be used to insert content, delete
41 * content or replace content.
42 *
43 * @experimental This is still being worked on and the API is subject to changes
44 * in structure going forward.
45 *
46 * @deprecated use [[`DelayedCommand`]] instead.
47 *
48 */
49export declare function delayedCommand<Value>({ immediate, promise, onDone, onFail, }: DelayedCommandProps<Value>): CommandFunction;
50export type DelayedPromiseCreator<Value> = (props: CommandFunctionProps) => Promise<Value>;
51export declare class DelayedCommand<Value> {
52 private readonly promiseCreator;
53 private readonly failureHandlers;
54 private readonly successHandlers;
55 private readonly validateHandlers;
56 constructor(promiseCreator: DelayedPromiseCreator<Value>);
57 /**
58 * The commands that will immediately be run and used to evaluate whether to
59 * proceed.
60 */
61 validate(handler: CommandFunction, method?: 'push' | 'unshift'): this;
62 /**
63 * Add a success callback to the handler.
64 */
65 success(handler: CommandFunction<{
66 value: Value;
67 }>, method?: 'push' | 'unshift'): this;
68 /**
69 * Add a failure callback to the handler.
70 */
71 failure(handler: CommandFunction<{
72 error: any;
73 }>, method?: 'push' | 'unshift'): this;
74 private runHandlers;
75 /**
76 * Generate the `remirror` command.
77 */
78 readonly generateCommand: () => CommandFunction;
79}
80export interface ToggleMarkProps extends MarkTypeProps, Partial<AttributesProps> {
81 /**
82 * @deprecated use `selection` property instead.
83 */
84 range?: FromToProps;
85 /**
86 * The selection point for toggling the chosen mark.
87 */
88 selection?: PrimitiveSelection;
89}
90/**
91 * A custom `toggleMark` function that works for the `remirror` codebase.
92 *
93 * Create a command function that toggles the given mark with the given
94 * attributes. Will return `false` when the current selection doesn't support
95 * that mark. This will remove the mark if any marks of that type exist in the
96 * selection, or add it otherwise. If the selection is empty, this applies to
97 * the [stored marks](#state.EditorState.storedMarks) instead of a range of the
98 * document.
99 *
100 * The differences from the `prosemirror-commands` version.
101 * - Acts on the transaction rather than the state to allow for commands to be
102 * chained together.
103 * - Uses the ONE parameter function signature for compatibility with remirror.
104 * - Supports passing a custom range.
105 */
106export declare function toggleMark(props: ToggleMarkProps): CommandFunction;
107/**
108 * Apply the provided mark type and attributes.
109 *
110 * @param markType - the mark to apply.
111 * @param attrs - the attributes to set on the applied mark.
112 * @param selectionPoint - optionally specify where the mark should be applied.
113 * Defaults to the current selection.
114 */
115export declare function applyMark(type: string | MarkType, attrs?: ProsemirrorAttributes, selectionPoint?: PrimitiveSelection): CommandFunction;
116export interface InsertTextOptions extends Partial<FromToProps> {
117 /**
118 * Marks can be added to the inserted text.
119 */
120 marks?: Record<string, ProsemirrorAttributes>;
121}
122/**
123 * Insert text into the dom at the current location by default. If a promise is
124 * provided then the text will be inserted at the tracked position when the
125 * promise is resolved.
126 */
127export declare function insertText(text: string, options?: InsertTextOptions): CommandFunction;
128export {};