Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 792bd3a

Browse files
mrkevfacebook-github-bot
authored andcommitted
Improve types for EditorState
Summary: I'm on a quest to both understand Draft better and make it more reliable. The types for `EditorState` are not the best, so lets improve them. Reviewed By: claudiopro Differential Revision: D20545884 fbshipit-source-id: a4dde2952e1b981732b9923b3912acd5e965cfd4
1 parent fd16d8e commit 792bd3a

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/model/immutable/EditorState.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ const Immutable = require('immutable');
2727

2828
const {OrderedSet, Record, Stack} = Immutable;
2929

30+
// When configuring an editor, the user can chose to provide or not provide
31+
// basically all keys. `currentContent` varies, so this type doesn't include it.
32+
// (See the types defined below.)
33+
type BaseEditorStateConfig = {|
34+
allowUndo?: boolean,
35+
decorator?: ?DraftDecoratorType,
36+
directionMap?: ?OrderedMap<string, string>,
37+
forceSelection?: boolean,
38+
inCompositionMode?: boolean,
39+
inlineStyleOverride?: ?DraftInlineStyle,
40+
lastChangeType?: ?EditorChangeType,
41+
nativelyRenderedContent?: ?ContentState,
42+
redoStack?: Stack<ContentState>,
43+
selection?: ?SelectionState,
44+
treeMap?: ?OrderedMap<string, List<any>>,
45+
undoStack?: Stack<ContentState>,
46+
|};
47+
48+
// When crating an editor, we want currentContent to be set.
49+
type EditorStateCreationConfigType = {|
50+
...BaseEditorStateConfig,
51+
currentContent: ContentState,
52+
|};
53+
54+
// When using EditorState.set(...), currentContent is optional
55+
type EditorStateChangeConfigType = {|
56+
...BaseEditorStateConfig,
57+
currentContent?: ?ContentState,
58+
|};
59+
3060
type EditorStateRecordType = {
3161
allowUndo: boolean,
3262
currentContent: ?ContentState,
@@ -41,7 +71,6 @@ type EditorStateRecordType = {
4171
selection: ?SelectionState,
4272
treeMap: ?OrderedMap<string, List<any>>,
4373
undoStack: Stack<ContentState>,
44-
...
4574
};
4675

4776
const defaultRecord: EditorStateRecordType = {
@@ -92,7 +121,7 @@ class EditorState {
92121
});
93122
}
94123

95-
static create(config: Object): EditorState {
124+
static create(config: EditorStateCreationConfigType): EditorState {
96125
const {currentContent, decorator} = config;
97126
const recordConfig = {
98127
...config,
@@ -102,7 +131,10 @@ class EditorState {
102131
return new EditorState(new EditorStateRecord(recordConfig));
103132
}
104133

105-
static set(editorState: EditorState, put: Object): EditorState {
134+
static set(
135+
editorState: EditorState,
136+
put: EditorStateChangeConfigType,
137+
): EditorState {
106138
const map = editorState.getImmutable().withMutations(state => {
107139
const existingDecorator = state.get('decorator');
108140
let decorator = existingDecorator;
@@ -115,7 +147,7 @@ class EditorState {
115147
const newContent = put.currentContent || editorState.getCurrentContent();
116148

117149
if (decorator !== existingDecorator) {
118-
const treeMap: OrderedMap<any, any> = state.get('treeMap');
150+
const treeMap: OrderedMap<string, any> = state.get('treeMap');
119151
let newTreeMap;
120152
if (decorator && existingDecorator) {
121153
newTreeMap = regenerateTreeForNewDecorator(

0 commit comments

Comments
 (0)