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

Commit

Permalink
Improve types for EditorState
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mrkev authored and facebook-github-bot committed Mar 20, 2020
1 parent fd16d8e commit 792bd3a
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/model/immutable/EditorState.js
Expand Up @@ -27,6 +27,36 @@ const Immutable = require('immutable');

const {OrderedSet, Record, Stack} = Immutable;

// When configuring an editor, the user can chose to provide or not provide
// basically all keys. `currentContent` varies, so this type doesn't include it.
// (See the types defined below.)
type BaseEditorStateConfig = {|
allowUndo?: boolean,
decorator?: ?DraftDecoratorType,
directionMap?: ?OrderedMap<string, string>,
forceSelection?: boolean,
inCompositionMode?: boolean,
inlineStyleOverride?: ?DraftInlineStyle,
lastChangeType?: ?EditorChangeType,
nativelyRenderedContent?: ?ContentState,
redoStack?: Stack<ContentState>,
selection?: ?SelectionState,
treeMap?: ?OrderedMap<string, List<any>>,
undoStack?: Stack<ContentState>,
|};

// When crating an editor, we want currentContent to be set.
type EditorStateCreationConfigType = {|
...BaseEditorStateConfig,
currentContent: ContentState,
|};

// When using EditorState.set(...), currentContent is optional
type EditorStateChangeConfigType = {|
...BaseEditorStateConfig,
currentContent?: ?ContentState,
|};

type EditorStateRecordType = {
allowUndo: boolean,
currentContent: ?ContentState,
Expand All @@ -41,7 +71,6 @@ type EditorStateRecordType = {
selection: ?SelectionState,
treeMap: ?OrderedMap<string, List<any>>,
undoStack: Stack<ContentState>,
...
};

const defaultRecord: EditorStateRecordType = {
Expand Down Expand Up @@ -92,7 +121,7 @@ class EditorState {
});
}

static create(config: Object): EditorState {
static create(config: EditorStateCreationConfigType): EditorState {
const {currentContent, decorator} = config;
const recordConfig = {
...config,
Expand All @@ -102,7 +131,10 @@ class EditorState {
return new EditorState(new EditorStateRecord(recordConfig));
}

static set(editorState: EditorState, put: Object): EditorState {
static set(
editorState: EditorState,
put: EditorStateChangeConfigType,
): EditorState {
const map = editorState.getImmutable().withMutations(state => {
const existingDecorator = state.get('decorator');
let decorator = existingDecorator;
Expand All @@ -115,7 +147,7 @@ class EditorState {
const newContent = put.currentContent || editorState.getCurrentContent();

if (decorator !== existingDecorator) {
const treeMap: OrderedMap<any, any> = state.get('treeMap');
const treeMap: OrderedMap<string, any> = state.get('treeMap');
let newTreeMap;
if (decorator && existingDecorator) {
newTreeMap = regenerateTreeForNewDecorator(
Expand Down

0 comments on commit 792bd3a

Please sign in to comment.