UNPKG

7.09 kBTypeScriptView Raw
1import type { EditorState, EditorView, PrimitiveSelection, Shape, Transaction } from '@remirror/core-types';
2import type { BuiltinPreset, UpdatableViewProps } from '../builtins';
3import type { AnyExtension } from '../extension';
4import type { RemirrorManager } from '../manager';
5import type { FocusType } from '../types';
6import type { AddFrameworkHandler, BaseFramework, CreateStateFromContent, FrameworkOptions, FrameworkOutput, FrameworkProps, ListenerProps, RemirrorEventListenerProps, UpdatableViewPropsObject, UpdateStateProps } from './base-framework';
7/**
8 * This is the `Framework` class which is used to create an abstract class for
9 * implementing `Remirror` into the framework of your choice.
10 *
11 * The best way to learn how to use it is to take a look at the [[`DomFramework`]]
12 * and [[`ReactFramework`]] implementations.
13 *
14 * @remarks
15 *
16 * There are two methods and one getter property which must be implemented for this
17 */
18export declare abstract class Framework<Extension extends AnyExtension = BuiltinPreset, Props extends FrameworkProps<Extension> = FrameworkProps<Extension>, Output extends FrameworkOutput<Extension> = FrameworkOutput<Extension>> implements BaseFramework<Extension> {
19 #private;
20 /**
21 * A previous state that can be overridden by the framework implementation.
22 */
23 protected previousStateOverride?: EditorState;
24 /**
25 * The event listener which allows consumers to subscribe to the different
26 * events taking place in the editor. Events currently supported are:
27 *
28 * - `destroy`
29 * - `focus`
30 * - `blur`
31 * - `updated`
32 */
33 protected get addHandler(): AddFrameworkHandler<Extension>;
34 /**
35 * The updatable view props.
36 */
37 protected get updatableViewProps(): UpdatableViewPropsObject;
38 /**
39 * True when this is the first render of the editor.
40 */
41 protected get firstRender(): boolean;
42 /**
43 * Store the name of the framework.
44 */
45 abstract get name(): string;
46 /**
47 * The props passed in when creating or updating the `Framework` instance.
48 */
49 get props(): Props;
50 /**
51 * Returns the previous editor state. On the first render it defaults to
52 * returning the current state. For the first render the previous state and
53 * current state will always be equal.
54 */
55 protected get previousState(): EditorState;
56 /**
57 * The instance of the [[`RemirrorManager`]].
58 */
59 protected get manager(): RemirrorManager<Extension>;
60 /**
61 * The ProseMirror [[`EditorView`]].
62 */
63 protected get view(): EditorView;
64 /**
65 * A unique id for the editor. Can be used to differentiate between editors.
66 *
67 * Please note that this ID is only locally unique, it should not be used as a
68 * database key.
69 */
70 protected get uid(): string;
71 /**
72 * The initial editor state from when the editor was first created.
73 */
74 get initialEditorState(): EditorState;
75 constructor(options: FrameworkOptions<Extension, Props>);
76 /**
77 * Setup the manager event listeners which are disposed of when the manager is
78 * destroyed.
79 */
80 private updateListener;
81 /**
82 * Update the constructor props passed in. Useful for frameworks like react
83 * where props are constantly changing and when using hooks function closures
84 * can become stale.
85 *
86 * You can call the update method with the new `props` to update the internal
87 * state of this instance.
88 */
89 update(options: FrameworkOptions<Extension, Props>): this;
90 /**
91 * Retrieve the editor state.
92 */
93 protected readonly getState: () => EditorState;
94 /**
95 * Retrieve the previous editor state.
96 */
97 protected readonly getPreviousState: () => EditorState;
98 /**
99 * This method must be implement by the extending framework class. It returns
100 * an [[`EditorView`]] which is added to the [[`RemirrorManager`]].
101 */
102 protected abstract createView(state: EditorState, element?: Element): EditorView;
103 /**
104 * This is used to implement how the state updates are used within your
105 * application instance.
106 *
107 * It must be implemented.
108 */
109 protected abstract updateState(props: UpdateStateProps): void;
110 /**
111 * Update the view props.
112 */
113 protected updateViewProps(...keys: UpdatableViewProps[]): void;
114 /**
115 * This sets the attributes for the ProseMirror Dom node.
116 */
117 protected getAttributes(ssr?: false): Record<string, string>;
118 protected getAttributes(ssr: true): Shape;
119 /**
120 * Part of the Prosemirror API and is called whenever there is state change in
121 * the editor.
122 *
123 * @internalremarks
124 * How does it work when transactions are dispatched one after the other.
125 */
126 protected readonly dispatchTransaction: (tr: Transaction) => void;
127 /**
128 * Adds `onBlur` and `onFocus` listeners.
129 *
130 * When extending this class make sure to call this method once
131 * `ProsemirrorView` has been added to the dom.
132 */
133 protected addFocusListeners(): void;
134 /**
135 * Remove `onBlur` and `onFocus` listeners.
136 *
137 * When extending this class in your framework, make sure to call this just
138 * before the view is destroyed.
139 */
140 protected removeFocusListeners(): void;
141 /**
142 * Called when the component unmounts and is responsible for cleanup.
143 *
144 * @remarks
145 *
146 * - Removes listeners for the editor `blur` and `focus` events
147 */
148 destroy(): void;
149 /**
150 * Use this method in the `onUpdate` event to run all change handlers.
151 */
152 readonly onChange: (props?: ListenerProps) => void;
153 /**
154 * Listener for editor 'blur' events
155 */
156 private readonly onBlur;
157 /**
158 * Listener for editor 'focus' events
159 */
160 private readonly onFocus;
161 /**
162 * Sets the content of the editor. This bypasses the update function.
163 *
164 * @param content
165 * @param triggerChange
166 */
167 private readonly setContent;
168 /**
169 * Clear the content of the editor (reset to the default empty node).
170 *
171 * @param triggerChange - whether to notify the onChange handler that the
172 * content has been reset
173 */
174 private readonly clearContent;
175 /**
176 * Creates the props passed into all event listener handlers. e.g.
177 * `onChange`
178 */
179 protected eventListenerProps(props?: ListenerProps): RemirrorEventListenerProps<Extension>;
180 protected readonly createStateFromContent: CreateStateFromContent;
181 /**
182 * Focus the editor.
183 */
184 protected readonly focus: (position?: FocusType) => void;
185 /**
186 * Blur the editor.
187 */
188 protected readonly blur: (position?: PrimitiveSelection) => void;
189 /**
190 * Methods and properties which are made available to all consumers of the
191 * `Framework` class.
192 */
193 protected get baseOutput(): FrameworkOutput<Extension>;
194 /**
195 * Every framework implementation must provide it's own custom output.
196 */
197 abstract get frameworkOutput(): Output;
198}