UNPKG

54.1 kBTypeScriptView Raw
1import * as Immutable from "immutable";
2import * as React from "react";
3
4type SyntheticClipboardEvent = React.ClipboardEvent<{}>;
5type SyntheticKeyboardEvent = React.KeyboardEvent<{}>;
6type SyntheticEvent = React.SyntheticEvent<{}>;
7export as namespace Draft;
8declare namespace Draft {
9 namespace Component {
10 namespace Base {
11 import DraftEditorCommand = Draft.Model.Constants.DraftEditorCommand;
12 import DraftBlockType = Draft.Model.Constants.DraftBlockType;
13 import DraftDragType = Draft.Model.Constants.DraftDragType;
14 import DraftHandleValue = Draft.Model.Constants.DraftHandleValue;
15
16 import EditorState = Draft.Model.ImmutableData.EditorState;
17 import ContentBlock = Draft.Model.ImmutableData.ContentBlock;
18 import SelectionState = Draft.Model.ImmutableData.SelectionState;
19 import DraftInlineStyle = Draft.Model.ImmutableData.DraftInlineStyle;
20
21 import DraftBlockRenderConfig = Draft.Model.ImmutableData.DraftBlockRenderConfig;
22
23 type DraftBlockRenderMap = Immutable.Map<DraftBlockType, DraftBlockRenderConfig>;
24
25 type DraftStyleMap = {
26 [styleName: string]: React.CSSProperties;
27 };
28
29 type EditorCommand = DraftEditorCommand | string;
30
31 /**
32 * `DraftEditor` is the root editor component. It composes a `contentEditable`
33 * div, and provides a wide variety of useful function props for managing the
34 * state of the editor. See `DraftEditorProps` for details.
35 */
36 class DraftEditor extends React.Component<DraftEditorProps, {}> {
37 editor: HTMLElement | null | undefined;
38 editorContainer: HTMLElement | null | undefined;
39 getEditorKey(): string;
40
41 /** Force focus back onto the editor node. */
42 focus(): void;
43 /** Remove focus from the editor node. */
44 blur(): void;
45 }
46
47 /**
48 * The two most critical props are `editorState` and `onChange`.
49 *
50 * The `editorState` prop defines the entire state of the editor, while the
51 * `onChange` prop is the method in which all state changes are propagated
52 * upward to higher-level components.
53 *
54 * These props are analagous to `value` and `onChange` in controlled React
55 * text inputs.
56 */
57 export interface DraftEditorProps {
58 editorState: EditorState;
59 onChange(editorState: EditorState): void;
60
61 placeholder?: string | undefined;
62
63 /**
64 * Specify whether text alignment should be forced in a direction
65 * regardless of input characters.
66 */
67 textAlignment?: DraftTextAlignment | undefined;
68
69 /**
70 * Specify whether text directionality should be forced in a direction
71 * regardless of input characters.
72 */
73 textDirectionality?: DraftTextDirectionality | undefined;
74
75 /**
76 * For a given `ContentBlock` object, return an object that specifies
77 * a custom block component and/or props. If no object is returned,
78 * the default `TextEditorBlock` is used.
79 */
80 blockRendererFn?(block: ContentBlock): any;
81
82 /**
83 * Provide a map of block rendering configurations. Each block type maps to
84 * an element tag and an optional react element wrapper. This configuration
85 * is used for both rendering and paste processing.
86 */
87 blockRenderMap?: DraftBlockRenderMap | undefined;
88
89 /**
90 * Function that allows to define class names to apply to the given block when it is rendered.
91 */
92 blockStyleFn?(block: ContentBlock): string;
93
94 /**
95 * Provide a map of inline style names corresponding to CSS style objects
96 * that will be rendered for matching ranges.
97 */
98 customStyleMap?: DraftStyleMap | undefined;
99
100 /**
101 * Define a function to transform inline styles to CSS objects
102 * that are applied to spans of text.
103 */
104 customStyleFn?: ((style: DraftInlineStyle, block: ContentBlock) => React.CSSProperties) | undefined;
105
106 /**
107 * A function that accepts a synthetic key event and returns
108 * the matching DraftEditorCommand constant, or null if no command should
109 * be invoked.
110 */
111 keyBindingFn?(e: SyntheticKeyboardEvent): EditorCommand | null;
112
113 /**
114 * Set whether the `DraftEditor` component should be editable. Useful for
115 * temporarily disabling edit behavior or allowing `DraftEditor` rendering
116 * to be used for consumption purposes.
117 */
118 readOnly?: boolean | undefined;
119
120 /**
121 * Note: spellcheck is always disabled for IE. If enabled in Safari, OSX
122 * autocorrect is enabled as well.
123 */
124 spellCheck?: boolean | undefined;
125
126 /**
127 * When the Editor loses focus (blurs) text selections are cleared
128 * by default to mimic <textarea> behaviour, however in some situations
129 * users may wish to preserve native behaviour.
130 */
131 preserveSelectionOnBlur?: boolean | undefined;
132
133 /**
134 * Set whether to remove all style information from pasted content. If your
135 * use case should not have any block or inline styles, it is recommended
136 * that you set this to `true`.
137 */
138 stripPastedStyles?: boolean | undefined;
139 formatPastedText?:
140 | ((text: string, html?: string) => { text: string; html: string | undefined })
141 | undefined;
142
143 tabIndex?: number | undefined;
144
145 // exposed especially to help improve mobile web behaviors
146 autoCapitalize?: string | undefined;
147 autoComplete?: string | undefined;
148 autoCorrect?: string | undefined;
149
150 ariaActiveDescendantID?: string | undefined;
151 ariaAutoComplete?: string | undefined;
152 ariaControls?: string | undefined;
153 ariaDescribedBy?: string | undefined;
154 ariaExpanded?: boolean | undefined;
155 ariaLabel?: string | undefined;
156 ariaLabelledBy?: string | undefined;
157 ariaMultiline?: boolean | undefined;
158 ariaOwneeID?: string | undefined;
159
160 role?: string | undefined;
161
162 webDriverTestID?: string | undefined;
163
164 /**
165 * If using server-side rendering, this prop is required to be set to
166 * avoid client/server mismatches.
167 */
168 editorKey?: string | undefined;
169
170 // Cancelable event handlers, handled from the top level down. A handler
171 // that returns `handled` will be the last handler to execute for that event.
172
173 /**
174 * Useful for managing special behavior for pressing the `Return` key. E.g.
175 * removing the style from an empty list item.
176 */
177 handleReturn?(e: SyntheticKeyboardEvent, editorState: EditorState): DraftHandleValue;
178
179 /**
180 * Map a key command string provided by your key binding function to a
181 * specified behavior.
182 */
183 handleKeyCommand?(
184 command: EditorCommand,
185 editorState: EditorState,
186 eventTimeStamp: number,
187 ): DraftHandleValue;
188
189 /**
190 * Handle intended text insertion before the insertion occurs. This may be
191 * useful in cases where the user has entered characters that you would like
192 * to trigger some special behavior. E.g. immediately converting `:)` to an
193 * emoji Unicode character, or replacing ASCII quote characters with smart
194 * quotes.
195 */
196 handleBeforeInput?(chars: string, editorState: EditorState, eventTimeStamp: number): DraftHandleValue;
197
198 handlePastedText?(text: string, html: string | undefined, editorState: EditorState): DraftHandleValue;
199
200 handlePastedFiles?(files: Blob[]): DraftHandleValue;
201
202 /** Handle dropped files */
203 handleDroppedFiles?(selection: SelectionState, files: Blob[]): DraftHandleValue;
204
205 /** Handle other drops to prevent default text movement/insertion behaviour */
206 handleDrop?(
207 selection: SelectionState,
208 dataTransfer: Object,
209 isInternal: DraftDragType,
210 ): DraftHandleValue;
211
212 // Non-cancelable event triggers.
213 onEscape?(e: SyntheticKeyboardEvent): void;
214 onTab?(e: SyntheticKeyboardEvent): void;
215 onUpArrow?(e: SyntheticKeyboardEvent): void;
216 onDownArrow?(e: SyntheticKeyboardEvent): void;
217 onRightArrow?(e: SyntheticKeyboardEvent): void;
218 onLeftArrow?(e: SyntheticKeyboardEvent): void;
219
220 onBlur?(e: SyntheticEvent): void;
221 onFocus?(e: SyntheticEvent): void;
222 onCopy?(editor: Editor, e: SyntheticClipboardEvent): void;
223 onCut?(editor: Editor, e: SyntheticClipboardEvent): void;
224 }
225
226 type DraftTextAlignment = "left" | "center" | "right";
227
228 type DraftTextDirectionality = "LTR" | "RTL" | "NEUTRAL";
229 }
230
231 namespace Components {
232 class DraftEditorBlock extends React.Component<any, {}> {}
233 }
234
235 namespace Selection {
236 interface FakeClientRect {
237 left: number;
238 width: number;
239 right: number;
240 top: number;
241 bottom: number;
242 height: number;
243 }
244
245 /**
246 * Return the bounding ClientRect for the visible DOM selection, if any.
247 * In cases where there are no selected ranges or the bounding rect is
248 * temporarily invalid, return null.
249 */
250 function getVisibleSelectionRect(global: any): FakeClientRect;
251 }
252
253 namespace Utils {
254 import DraftEditorCommand = Draft.Model.Constants.DraftEditorCommand;
255
256 class KeyBindingUtil {
257 /**
258 * Check whether the ctrlKey modifier is *not* being used in conjunction with
259 * the altKey modifier. If they are combined, the result is an `altGraph`
260 * key modifier, which should not be handled by this set of key bindings.
261 */
262 static isCtrlKeyCommand(e: SyntheticKeyboardEvent): boolean;
263
264 static isOptionKeyCommand(e: SyntheticKeyboardEvent): boolean;
265
266 static hasCommandModifier(e: SyntheticKeyboardEvent): boolean;
267
268 static isSoftNewlineEvent(e: SyntheticKeyboardEvent): boolean;
269
270 /**
271 * Check whether heuristics that only apply to macOS are used
272 * internally, for example when determining the key combination
273 * used as command modifier.
274 */
275 static usesMacOSHeuristics(): boolean;
276 }
277
278 /**
279 * Retrieve a bound key command for the given event.
280 */
281 function getDefaultKeyBinding(e: SyntheticKeyboardEvent): DraftEditorCommand | null;
282 }
283 }
284
285 namespace Model {
286 namespace Constants {
287 /**
288 * A set of editor commands that may be invoked by keyboard commands or UI
289 * controls. These commands should map to operations that modify content or
290 * selection state and update the editor state accordingly.
291 */
292 type DraftEditorCommand =
293 /**
294 * Self-explanatory.
295 */
296 | "undo"
297 | "redo"
298 /**
299 * Perform a forward deletion.
300 */
301 | "delete"
302 /**
303 * Perform a forward deletion to the next word boundary after the selection.
304 */
305 | "delete-word"
306 /**
307 * Perform a backward deletion.
308 */
309 | "backspace"
310 /**
311 * Perform a backward deletion to the previous word boundary before the
312 * selection.
313 */
314 | "backspace-word"
315 /**
316 * Perform a backward deletion to the beginning of the current line.
317 */
318 | "backspace-to-start-of-line"
319 /**
320 * Toggle styles. Commands may be intepreted to modify inline text ranges
321 * or block types.
322 */
323 | "bold"
324 | "code"
325 | "italic"
326 | "strikethrough"
327 | "underline"
328 /**
329 * Split a block in two.
330 */
331 | "split-block"
332 /**
333 * Self-explanatory.
334 */
335 | "transpose-characters"
336 | "move-selection-to-start-of-block"
337 | "move-selection-to-end-of-block"
338 /**
339 * Commands to support the "secondary" clipboard provided by certain
340 * browsers and operating systems.
341 */
342 | "secondary-cut"
343 | "secondary-paste";
344
345 /**
346 * A type that allows us to avoid passing boolean arguments
347 * around to indicate whether a drag type is internal or external.
348 */
349 type DraftDragType = "internal" | "external";
350
351 /**
352 * The list of [default valid block types](https://draftjs.org/docs/advanced-topics-custom-block-render-map#draft-default-block-render-map),
353 * according to the [`DefaultDraftBlockRenderMap`](https://github.com/facebook/draft-js/blob/main/src/model/immutable/DefaultDraftBlockRenderMap.js)
354 */
355 type CoreDraftBlockType =
356 | "header-one"
357 | "header-two"
358 | "header-three"
359 | "header-four"
360 | "header-five"
361 | "header-six"
362 | "section"
363 | "article"
364 | "unordered-list-item"
365 | "ordered-list-item"
366 | "blockquote"
367 | "atomic"
368 | "code-block"
369 | "unstyled";
370
371 type CustomBlockType = string;
372
373 type DraftBlockType = CoreDraftBlockType | CustomBlockType;
374
375 /**
376 * A type that allows us to avoid passing boolean arguments
377 * around to indicate whether a deletion is forward or backward.
378 */
379 type DraftRemovalDirection = "backward" | "forward";
380
381 /**
382 * A type that allows us to avoid returning boolean values
383 * to indicate whether an event was handled or not.
384 */
385 type DraftHandleValue = "handled" | "not-handled";
386
387 /**
388 * A type that defines if an fragment shall be inserted before or after
389 * another fragment or if the selected fragment shall be replaced
390 */
391 type DraftInsertionType = "replace" | "before" | "after";
392
393 /**
394 * Valid inline styles.
395 */
396 type DraftInlineStyleType = "BOLD" | "CODE" | "ITALIC" | "STRIKETHROUGH" | "UNDERLINE";
397
398 /**
399 * Possible entity types, like 'LINK', 'IMAGE', or custom ones.
400 */
401 type DraftEntityType = string;
402
403 /**
404 * Possible "mutability" options for an entity. This refers to the behavior
405 * that should occur when inserting or removing characters in a text range
406 * with an entity applied to it.
407 *
408 * `MUTABLE`:
409 * The text range can be modified freely. Generally used in cases where
410 * the text content and the entity do not necessarily have a direct
411 * relationship. For instance, the text and URI for a link may be completely
412 * different. The user is allowed to edit the text as needed, and the entity
413 * is preserved and applied to any characters added within the range.
414 *
415 * `IMMUTABLE`:
416 * Not to be confused with immutable data structures used to represent the
417 * state of the editor. Immutable entity ranges cannot be modified in any
418 * way. Adding characters within the range will remove the entity from the
419 * entire range. Deleting characters will delete the entire range. Example:
420 * Facebook Page mentions.
421 *
422 * `SEGMENTED`:
423 * Segmented entities allow the removal of partial ranges of text, as
424 * separated by a delimiter. Adding characters wihin the range will remove
425 * the entity from the entire range. Deleting characters within a segmented
426 * entity will delete only the segments affected by the deletion. Example:
427 * Facebook User mentions.
428 */
429 type DraftEntityMutability = "MUTABLE" | "IMMUTABLE" | "SEGMENTED";
430 }
431
432 namespace Decorators {
433 import ContentBlock = Draft.Model.ImmutableData.ContentBlock;
434
435 /**
436 * An interface for document decorator classes, allowing the creation of
437 * custom decorator classes.
438 *
439 * See `CompositeDraftDecorator` for the most common use case.
440 */
441 interface DraftDecoratorType {
442 /**
443 * Given a `ContentBlock`, return an immutable List of decorator keys.
444 */
445 getDecorations(block: ContentBlock, contentState: ContentState): Immutable.List<string>;
446
447 /**
448 * Given a decorator key, return the component to use when rendering
449 * this decorated range.
450 */
451 getComponentForKey(key: string): Function;
452
453 /**
454 * Given a decorator key, optionally return the props to use when rendering
455 * this decorated range.
456 */
457 getPropsForKey(key: string): any;
458 }
459
460 /**
461 * DraftDecoratorComponentProps are the core set of props that will be
462 * passed to all DraftDecoratorComponents if a Custom Block Component is not used.
463 * Note that a component may also accept additional props outside of this list.
464 */
465 interface DraftDecoratorComponentProps {
466 blockKey: string;
467 children?: React.ReactNode[];
468 contentState: ContentState;
469 decoratedText: string;
470 dir: "ltr" | "rtl" | undefined;
471 end: number;
472 // Many folks mistakenly assume that there will always be an 'entityKey'
473 // passed to a DecoratorComponent.
474 // To find the `entityKey`, Draft calls
475 // `contentBlock.getEntityKeyAt(leafNode)` and in many cases the leafNode does
476 // not have an entityKey. In those cases the entityKey will be null or
477 // undefined. That's why `getEntityKeyAt()` is typed to return `?string`.
478 // See https://github.com/facebook/draft-js/blob/2da3dcb1c4c106d1b2a0f07b3d0275b8d724e777/src/model/immutable/BlockNode.js#L51
479 entityKey: string | undefined;
480 offsetKey: string;
481 start: number;
482 }
483
484 /**
485 * A DraftDecorator is a strategy-component pair intended for use when
486 * rendering content.
487 *
488 * - A "strategy": A function that accepts a ContentBlock object and
489 * continuously executes a callback with start/end values corresponding to
490 * relevant matches in the document text. For example, getHashtagMatches
491 * uses a hashtag regex to find hashtag strings in the block, and
492 * for each hashtag match, executes the callback with start/end pairs.
493 *
494 * - A "component": A React component that will be used to render the
495 * "decorated" section of text.
496 *
497 * - "props": Props to be passed into the React component that will be used
498 * merged with DraftDecoratorComponentProps
499 */
500 interface DraftDecorator<P = any> {
501 strategy: (
502 block: ContentBlock,
503 callback: (start: number, end: number) => void,
504 contentState: ContentState,
505 ) => void;
506 component:
507 | React.Component
508 | React.ComponentClass<any>
509 | ((props: DraftDecoratorComponentProps & P) => React.ReactNode);
510 props?: P | undefined;
511 }
512
513 /**
514 * A CompositeDraftDecorator traverses through a list of DraftDecorator
515 * instances to identify sections of a ContentBlock that should be rendered
516 * in a "decorated" manner. For example, hashtags, mentions, and links may
517 * be intended to stand out visually, be rendered as anchors, etc.
518 *
519 * The list of decorators supplied to the constructor will be used in the
520 * order they are provided. This allows the caller to specify a priority for
521 * string matching, in case of match collisions among decorators.
522 *
523 * For instance, I may have a link with a `#` in its text. Though this section
524 * of text may match our hashtag decorator, it should not be treated as a
525 * hashtag. I should therefore list my link DraftDecorator
526 * before my hashtag DraftDecorator when constructing this composite
527 * decorator instance.
528 *
529 * Thus, when a collision like this is encountered, the earlier match is
530 * preserved and the new match is discarded.
531 */
532 class CompositeDraftDecorator {
533 constructor(decorators: DraftDecorator[]);
534
535 getDecorations(block: ContentBlock, contentState: ContentState): Immutable.List<string>;
536 getComponentForKey(key: string): Function;
537 getPropsForKey(key: string): Object;
538 }
539 }
540
541 namespace Encoding {
542 import DraftInlineStyleType = Draft.Model.Constants.DraftInlineStyleType;
543 import DraftBlockType = Draft.Model.Constants.DraftBlockType;
544 import DraftEntityMutability = Draft.Model.Constants.DraftEntityMutability;
545 import DraftEntityType = Draft.Model.Constants.DraftEntityType;
546
547 import ContentBlock = Draft.Model.ImmutableData.ContentBlock;
548 import ContentState = Draft.Model.ImmutableData.ContentState;
549
550 import DraftBlockRenderMap = Draft.Component.Base.DraftBlockRenderMap;
551
552 /**
553 * A plain object representation of an inline style range.
554 */
555 interface RawDraftInlineStyleRange {
556 style: DraftInlineStyleType;
557 offset: number;
558 length: number;
559 }
560
561 /**
562 * A plain object representation of an entity attribution.
563 *
564 * The `key` value corresponds to the key of the entity in the `entityMap` of
565 * a `ComposedText` object, not for use with `DraftEntity.get()`.
566 */
567 interface RawDraftEntityRange {
568 key: number;
569 offset: number;
570 length: number;
571 }
572
573 /**
574 * A plain object representation of an EntityInstance.
575 */
576 interface RawDraftEntity<T = { [key: string]: any }> {
577 type: DraftEntityType;
578 mutability: DraftEntityMutability;
579 data: T;
580 }
581
582 /**
583 * A plain object representation of a ContentBlock, with all style and entity
584 * attribution repackaged as range objects.
585 */
586 interface RawDraftContentBlock {
587 key: string;
588 type: DraftBlockType;
589 text: string;
590 depth: number;
591 inlineStyleRanges: RawDraftInlineStyleRange[];
592 entityRanges: RawDraftEntityRange[];
593 data?: { [key: string]: any } | undefined;
594 }
595
596 /**
597 * A type that represents a composed document as vanilla JavaScript objects,
598 * with all styles and entities represented as ranges. Corresponding entity
599 * objects are packaged as objects as well.
600 *
601 * This object is especially useful when sending the document state to the
602 * server for storage, as its representation is more concise than our
603 * immutable objects.
604 */
605 interface RawDraftContentState {
606 blocks: RawDraftContentBlock[];
607 entityMap: { [key: string]: RawDraftEntity };
608 }
609
610 function convertFromHTMLtoContentBlocks(
611 html: string,
612 DOMBuilder?: Function,
613 blockRenderMap?: DraftBlockRenderMap,
614 ): { contentBlocks: ContentBlock[]; entityMap: any };
615 function convertFromRawToDraftState(rawState: RawDraftContentState): ContentState;
616 function convertFromDraftStateToRaw(contentState: ContentState): RawDraftContentState;
617 }
618
619 namespace Entity {
620 import DraftEntityMutability = Draft.Model.Constants.DraftEntityMutability;
621 import DraftEntityType = Draft.Model.Constants.DraftEntityType;
622
623 /**
624 * A "document entity" is an object containing metadata associated with a
625 * piece of text in a ContentBlock.
626 *
627 * For example, a `link` entity might include a `uri` property. When a
628 * ContentBlock is rendered in the browser, text that refers to that link
629 * entity may be rendered as an anchor, with the `uri` as the href value.
630 *
631 * In a ContentBlock, every position in the text may correspond to zero
632 * or one entities. This correspondence is tracked using a key string,
633 * generated via DraftEntity.create() and used to obtain entity metadata
634 * via DraftEntity.get().
635 */
636 class DraftEntity {
637 /**
638 * Create a DraftEntityInstance and store it for later retrieval.
639 *
640 * A random key string will be generated and returned. This key may
641 * be used to track the entity's usage in a ContentBlock, and for
642 * retrieving data about the entity at render time.
643 */
644 static create(type: DraftEntityType, mutability: DraftEntityMutability, data?: Object): string;
645
646 /**
647 * Add an existing DraftEntityInstance to the DraftEntity map. This is
648 * useful when restoring instances from the server.
649 */
650 static add(instance: DraftEntityInstance): string;
651
652 /**
653 * Retrieve the entity corresponding to the supplied key string.
654 */
655 static get(key: string): DraftEntityInstance;
656
657 /**
658 * Entity instances are immutable. If you need to update the data for an
659 * instance, this method will merge your data updates and return a new
660 * instance.
661 */
662 static mergeData(key: string, toMerge: { [key: string]: any }): DraftEntityInstance;
663
664 /**
665 * Completely replace the data for a given instance.
666 */
667 static replaceData(key: string, newData: { [key: string]: any }): DraftEntityInstance;
668 }
669
670 /**
671 * An instance of a document entity, consisting of a `type` and relevant
672 * `data`, metadata about the entity.
673 *
674 * For instance, a "link" entity might provide a URI, and a "mention"
675 * entity might provide the mentioned user's ID. These pieces of data
676 * may be used when rendering the entity as part of a ContentBlock DOM
677 * representation. For a link, the data would be used as an href for
678 * the rendered anchor. For a mention, the ID could be used to retrieve
679 * a hovercard.
680 */
681 interface DraftEntityInstance {
682 getType(): DraftEntityType;
683 getMutability(): DraftEntityMutability;
684 getData(): any;
685 }
686 }
687
688 namespace ImmutableData {
689 import DraftBlockType = Draft.Model.Constants.DraftBlockType;
690 import DraftEntityMutability = Draft.Model.Constants.DraftEntityMutability;
691 import DraftEntityType = Draft.Model.Constants.DraftEntityType;
692 import DraftEntityInstance = Draft.Model.Entity.DraftEntityInstance;
693
694 import DraftDecoratorType = Draft.Model.Decorators.DraftDecoratorType;
695
696 type DraftInlineStyle = Immutable.OrderedSet<string>;
697 type BlockMap = Immutable.OrderedMap<string, Draft.Model.ImmutableData.ContentBlock>;
698
699 var Record: Immutable.Record.Class;
700
701 interface DraftBlockRenderConfig {
702 element: string;
703 wrapper?: React.ReactNode | undefined;
704 }
705
706 class EditorState extends Record {
707 static createEmpty(decorator?: DraftDecoratorType): EditorState;
708 static createWithContent(contentState: ContentState, decorator?: DraftDecoratorType): EditorState;
709 static create(config: Object): EditorState;
710 static set(editorState: EditorState, put: Object): EditorState;
711
712 /**
713 * Incorporate native DOM selection changes into the EditorState. This
714 * method can be used when we simply want to accept whatever the DOM
715 * has given us to represent selection, and we do not need to re-render
716 * the editor.
717 *
718 * To forcibly move the DOM selection, see `EditorState.forceSelection`.
719 */
720 static acceptSelection(editorState: EditorState, selection: SelectionState): EditorState;
721
722 /**
723 * At times, we need to force the DOM selection to be where we
724 * need it to be. This can occur when the anchor or focus nodes
725 * are non-text nodes, for instance. In this case, we want to trigger
726 * a re-render of the editor, which in turn forces selection into
727 * the correct place in the DOM. The `forceSelection` method
728 * accomplishes this.
729 *
730 * This method should be used in cases where you need to explicitly
731 * move the DOM selection from one place to another without a change
732 * in ContentState.
733 */
734 static forceSelection(editorState: EditorState, selection: SelectionState): EditorState;
735
736 /**
737 * Move selection to the end of the editor without forcing focus.
738 */
739 static moveSelectionToEnd(editorState: EditorState): EditorState;
740
741 /**
742 * Force focus to the end of the editor. This is useful in scenarios
743 * where we want to programmatically focus the input and it makes sense
744 * to allow the user to continue working seamlessly.
745 */
746 static moveFocusToEnd(editorState: EditorState): EditorState;
747
748 /**
749 * Push the current ContentState onto the undo stack if it should be
750 * considered a boundary state, and set the provided ContentState as the
751 * new current content.
752 */
753 static push(
754 editorState: EditorState,
755 contentState: ContentState,
756 changeType: EditorChangeType,
757 ): EditorState;
758
759 /**
760 * Make the top ContentState in the undo stack the new current content and
761 * push the current content onto the redo stack.
762 */
763 static undo(editorState: EditorState): EditorState;
764
765 /**
766 * Make the top ContentState in the redo stack the new current content and
767 * push the current content onto the undo stack.
768 */
769 static redo(editorState: EditorState): EditorState;
770
771 toJS(): Object;
772 getAllowUndo(): boolean;
773 getCurrentContent(): ContentState;
774 getUndoStack(): Immutable.Stack<ContentState>;
775 getRedoStack(): Immutable.Stack<ContentState>;
776 getSelection(): SelectionState;
777 getDecorator(): DraftDecoratorType;
778 isInCompositionMode(): boolean;
779 mustForceSelection(): boolean;
780 getNativelyRenderedContent(): ContentState;
781 getLastChangeType(): EditorChangeType;
782
783 /**
784 * While editing, the user may apply inline style commands with a collapsed
785 * cursor, intending to type text that adopts the specified style. In this
786 * case, we track the specified style as an "override" that takes precedence
787 * over the inline style of the text adjacent to the cursor.
788 *
789 * If null, there is no override in place.
790 */
791 getInlineStyleOverride(): DraftInlineStyle;
792
793 static setInlineStyleOverride(
794 editorState: EditorState,
795 inlineStyleOverride: DraftInlineStyle,
796 ): EditorState;
797
798 /**
799 * Get the appropriate inline style for the editor state. If an
800 * override is in place, use it. Otherwise, the current style is
801 * based on the location of the selection state.
802 */
803 getCurrentInlineStyle(): DraftInlineStyle;
804
805 getBlockTree(blockKey: string): Immutable.List<any>;
806 isSelectionAtStartOfContent(): boolean;
807 isSelectionAtEndOfContent(): boolean;
808 getDirectionMap(): Immutable.OrderedMap<any, any>;
809 }
810
811 class ContentBlock extends Record {
812 getKey(): string;
813
814 getType(): DraftBlockType;
815
816 getText(): string;
817 getCharacterList(): Immutable.List<CharacterMetadata>;
818 getLength(): number;
819 getDepth(): number;
820 getData(): Immutable.Map<any, any>;
821 getInlineStyleAt(offset: number): DraftInlineStyle;
822 getEntityAt(offset: number): string;
823
824 /**
825 * Execute a callback for every contiguous range of styles within the block.
826 */
827 findStyleRanges(
828 filterFn: (value: CharacterMetadata) => boolean,
829 callback: (start: number, end: number) => void,
830 ): void;
831
832 /**
833 * Execute a callback for every contiguous range of entities within the block.
834 */
835 findEntityRanges(
836 filterFn: (value: CharacterMetadata) => boolean,
837 callback: (start: number, end: number) => void,
838 ): void;
839 }
840
841 class ContentState extends Record {
842 static createFromBlockArray(blocks: ContentBlock[], entityMap?: any): ContentState;
843 static createFromText(text: string, delimiter?: string): ContentState;
844
845 createEntity(type: DraftEntityType, mutability: DraftEntityMutability, data?: Object): ContentState;
846 getEntity(key: string): EntityInstance;
847 getEntityMap(): any;
848 getAllEntities(): Immutable.OrderedMap<string, DraftEntityInstance>;
849 getLastCreatedEntityKey(): string;
850 mergeEntityData(key: string, toMerge: { [key: string]: any }): ContentState;
851 replaceEntityData(key: string, toMerge: { [key: string]: any }): ContentState;
852 addEntity(instance: DraftEntityInstance): ContentState;
853
854 getBlockMap(): BlockMap;
855 getSelectionBefore(): SelectionState;
856 getSelectionAfter(): SelectionState;
857 getBlockForKey(key: string): ContentBlock;
858
859 getKeyBefore(key: string): string;
860 getKeyAfter(key: string): string;
861 getBlockAfter(key: string): ContentBlock | undefined;
862 getBlockBefore(key: string): ContentBlock | undefined;
863
864 getBlocksAsArray(): ContentBlock[];
865 getFirstBlock(): ContentBlock;
866 getLastBlock(): ContentBlock;
867 getPlainText(delimiter?: string): string;
868 hasText(): boolean;
869 }
870
871 interface SelectionStateProperties {
872 anchorKey: string;
873 anchorOffset: number;
874 focusKey: string;
875 focusOffset: number;
876 isBackward: boolean;
877 hasFocus: boolean;
878 }
879
880 class SelectionState extends Record {
881 static createEmpty(key: string): SelectionState;
882
883 merge(
884 ...iterables: Array<
885 Immutable.Iterable<
886 keyof SelectionStateProperties,
887 SelectionStateProperties[keyof SelectionStateProperties]
888 >
889 >
890 ): SelectionState;
891 merge(...iterables: Array<Partial<SelectionStateProperties>>): SelectionState;
892
893 serialize(): string;
894 getAnchorKey(): string;
895 getAnchorOffset(): number;
896 getFocusKey(): string;
897 getFocusOffset(): number;
898 getIsBackward(): boolean;
899 getHasFocus(): boolean;
900 /**
901 * Return whether the specified range overlaps with an edge of the
902 * SelectionState.
903 */
904 hasEdgeWithin(blockKey: string, start: number, end: number): boolean;
905 isCollapsed(): boolean;
906 getStartKey(): string;
907 getStartOffset(): number;
908 getEndKey(): string;
909 getEndOffset(): number;
910 }
911
912 class CharacterMetadata {
913 static applyStyle(record: CharacterMetadata, style: string): CharacterMetadata;
914 static removeStyle(record: CharacterMetadata, style: string): CharacterMetadata;
915 static applyEntity(record: CharacterMetadata, entityKey: string | null): CharacterMetadata;
916 static applyEntity(record: CharacterMetadata): CharacterMetadata;
917 /**
918 * Use this function instead of the `CharacterMetadata` constructor.
919 * Since most content generally uses only a very small number of
920 * style/entity permutations, we can reuse these objects as often as
921 * possible.
922 */
923 static create(config?: CharacterMetadataConfig): CharacterMetadata;
924 static create(): CharacterMetadata;
925
926 getStyle(): DraftInlineStyle;
927 getEntity(): string;
928 hasStyle(style: string): boolean;
929 }
930
931 interface CharacterMetadataConfig {
932 style?: DraftInlineStyle | undefined;
933 entity?: string | undefined;
934 }
935
936 type EditorChangeType =
937 | "adjust-depth"
938 | "apply-entity"
939 | "backspace-character"
940 | "change-block-data"
941 | "change-block-type"
942 | "change-inline-style"
943 | "move-block"
944 | "delete-character"
945 | "insert-characters"
946 | "insert-fragment"
947 | "redo"
948 | "remove-range"
949 | "spellcheck-change"
950 | "split-block"
951 | "undo";
952
953 class BlockMapBuilder {
954 static createFromArray(blocks: ContentBlock[]): BlockMap;
955 }
956
957 const DefaultDraftBlockRenderMap: Immutable.Map<any, any>;
958 const DefaultDraftInlineStyle: Immutable.Map<any, any>;
959 }
960
961 namespace Keys {
962 function generateRandomKey(): string;
963 }
964
965 namespace Modifier {
966 import EditorState = Draft.Model.ImmutableData.EditorState;
967 import ContentState = Draft.Model.ImmutableData.ContentState;
968 import SelectionState = Draft.Model.ImmutableData.SelectionState;
969
970 import BlockMap = Draft.Model.ImmutableData.BlockMap;
971 import DraftInlineStyle = Draft.Model.ImmutableData.DraftInlineStyle;
972
973 import DraftRemovalDirection = Draft.Model.Constants.DraftRemovalDirection;
974 import DraftBlockType = Draft.Model.Constants.DraftBlockType;
975
976 import DraftEditorCommand = Draft.Model.Constants.DraftEditorCommand;
977
978 type URI = any;
979
980 class AtomicBlockUtils {
981 static insertAtomicBlock(editorState: EditorState, entityKey: string, character: string): EditorState;
982 static moveAtomicBlock(
983 editorState: EditorState,
984 atomicBlock: ContentBlock,
985 targetRange: SelectionState,
986 insertionMode?: DraftInsertionType,
987 ): EditorState;
988 }
989
990 /**
991 * `DraftModifier` provides a set of convenience methods that apply
992 * modifications to a `ContentState` object based on a target `SelectionState`.
993 *
994 * Any change to a `ContentState` should be decomposable into a series of
995 * transaction functions that apply the required changes and return output
996 * `ContentState` objects.
997 *
998 * These functions encapsulate some of the most common transaction sequences.
999 */
1000 class DraftModifier {
1001 static replaceText(
1002 contentState: ContentState,
1003 rangeToReplace: SelectionState,
1004 text: string,
1005 inlineStyle?: DraftInlineStyle,
1006 entityKey?: string,
1007 ): ContentState;
1008 static insertText(
1009 contentState: ContentState,
1010 targetRange: SelectionState,
1011 text: string,
1012 inlineStyle?: DraftInlineStyle,
1013 entityKey?: string,
1014 ): ContentState;
1015 static moveText(
1016 contentState: ContentState,
1017 removalRange: SelectionState,
1018 targetRange: SelectionState,
1019 ): ContentState;
1020 static replaceWithFragment(
1021 contentState: ContentState,
1022 targetRange: SelectionState,
1023 fragment: BlockMap,
1024 ): ContentState;
1025
1026 static removeRange(
1027 contentState: ContentState,
1028 rangeToRemove: SelectionState,
1029 removalDirection: DraftRemovalDirection,
1030 ): ContentState;
1031
1032 static splitBlock(contentState: ContentState, selectionState: SelectionState): ContentState;
1033 static applyInlineStyle(
1034 contentState: ContentState,
1035 selectionState: SelectionState,
1036 inlineStyle: string,
1037 ): ContentState;
1038 static removeInlineStyle(
1039 contentState: ContentState,
1040 selectionState: SelectionState,
1041 inlineStyle: string,
1042 ): ContentState;
1043
1044 static setBlockType(
1045 contentState: ContentState,
1046 selectionState: SelectionState,
1047 blockType: DraftBlockType,
1048 ): ContentState;
1049 static setBlockType(
1050 contentState: ContentState,
1051 selectionState: SelectionState,
1052 blockType: string,
1053 ): ContentState;
1054
1055 static setBlockData(
1056 contentState: ContentState,
1057 selectionState: SelectionState,
1058 blockData: Immutable.Map<any, any>,
1059 ): ContentState;
1060 static mergeBlockData(
1061 contentState: ContentState,
1062 selectionState: SelectionState,
1063 blockData: Immutable.Map<any, any>,
1064 ): ContentState;
1065 static applyEntity(
1066 contentState: ContentState,
1067 selectionState: SelectionState,
1068 entityKey: string | null,
1069 ): ContentState;
1070 }
1071
1072 class RichTextEditorUtil {
1073 static currentBlockContainsLink(editorState: EditorState): boolean;
1074 static getCurrentBlockType(editorState: EditorState): DraftBlockType;
1075 static getCurrentBlockType(editorState: EditorState): string;
1076 static getDataObjectForLinkURL(uri: URI): Object;
1077
1078 static handleKeyCommand(editorState: EditorState, command: DraftEditorCommand): EditorState | null;
1079 static handleKeyCommand(editorState: EditorState, command: string): null;
1080
1081 static insertSoftNewline(editorState: EditorState): EditorState;
1082
1083 /**
1084 * For collapsed selections at the start of styled blocks, backspace should
1085 * just remove the existing style.
1086 */
1087 static onBackspace(editorState: EditorState): EditorState | null;
1088 static onDelete(editorState: EditorState): EditorState | null;
1089 static onTab(event: SyntheticKeyboardEvent, editorState: EditorState, maxDepth: number): EditorState;
1090
1091 static toggleBlockType(editorState: EditorState, blockType: DraftBlockType): EditorState;
1092 static toggleBlockType(editorState: EditorState, blockType: string): EditorState;
1093
1094 static toggleCode(editorState: EditorState): EditorState;
1095
1096 /**
1097 * Toggle the specified inline style for the selection. If the
1098 * user's selection is collapsed, apply or remove the style for the
1099 * internal state. If it is not collapsed, apply the change directly
1100 * to the document state.
1101 */
1102 static toggleInlineStyle(editorState: EditorState, inlineStyle: string): EditorState;
1103
1104 static toggleLink(
1105 editorState: EditorState,
1106 targetSelection: SelectionState,
1107 entityKey: string | null,
1108 ): EditorState;
1109
1110 /**
1111 * When a collapsed cursor is at the start of an empty styled block, allow
1112 * certain key commands (newline, backspace) to simply change the
1113 * style of the block instead of the default behavior.
1114 */
1115 static tryToRemoveBlockStyle(editorState: EditorState): ContentState | null;
1116 }
1117 }
1118 }
1119}
1120
1121import Editor = Draft.Component.Base.DraftEditor;
1122import EditorProps = Draft.Component.Base.DraftEditorProps;
1123import EditorBlock = Draft.Component.Components.DraftEditorBlock;
1124import EditorState = Draft.Model.ImmutableData.EditorState;
1125import EditorChangeType = Draft.Model.ImmutableData.EditorChangeType;
1126import EditorCommand = Draft.Component.Base.EditorCommand;
1127
1128import DraftDecoratorComponentProps = Draft.Model.Decorators.DraftDecoratorComponentProps;
1129import DraftDecoratorType = Draft.Model.Decorators.DraftDecoratorType;
1130import DraftDecorator = Draft.Model.Decorators.DraftDecorator;
1131import CompositeDecorator = Draft.Model.Decorators.CompositeDraftDecorator;
1132import Entity = Draft.Model.Entity.DraftEntity;
1133import EntityInstance = Draft.Model.Entity.DraftEntityInstance;
1134
1135import BlockMapBuilder = Draft.Model.ImmutableData.BlockMapBuilder;
1136import CharacterMetadata = Draft.Model.ImmutableData.CharacterMetadata;
1137import ContentBlock = Draft.Model.ImmutableData.ContentBlock;
1138import ContentState = Draft.Model.ImmutableData.ContentState;
1139import SelectionState = Draft.Model.ImmutableData.SelectionState;
1140import DraftInlineStyle = Draft.Model.ImmutableData.DraftInlineStyle;
1141import BlockMap = Draft.Model.ImmutableData.BlockMap;
1142
1143import AtomicBlockUtils = Draft.Model.Modifier.AtomicBlockUtils;
1144import KeyBindingUtil = Draft.Component.Utils.KeyBindingUtil;
1145import Modifier = Draft.Model.Modifier.DraftModifier;
1146import RichUtils = Draft.Model.Modifier.RichTextEditorUtil;
1147
1148import DefaultDraftBlockRenderMap = Draft.Model.ImmutableData.DefaultDraftBlockRenderMap;
1149import DefaultDraftInlineStyle = Draft.Model.ImmutableData.DefaultDraftInlineStyle;
1150
1151import RawDraftInlineStyleRange = Draft.Model.Encoding.RawDraftInlineStyleRange;
1152import RawDraftEntityRange = Draft.Model.Encoding.RawDraftEntityRange;
1153import RawDraftEntity = Draft.Model.Encoding.RawDraftEntity;
1154import RawDraftContentBlock = Draft.Model.Encoding.RawDraftContentBlock;
1155import RawDraftContentState = Draft.Model.Encoding.RawDraftContentState;
1156import convertFromRaw = Draft.Model.Encoding.convertFromRawToDraftState;
1157import convertToRaw = Draft.Model.Encoding.convertFromDraftStateToRaw;
1158import convertFromHTML = Draft.Model.Encoding.convertFromHTMLtoContentBlocks;
1159
1160import genKey = Draft.Model.Keys.generateRandomKey;
1161import getDefaultKeyBinding = Draft.Component.Utils.getDefaultKeyBinding;
1162import getVisibleSelectionRect = Draft.Component.Selection.getVisibleSelectionRect;
1163
1164import DraftEditorCommand = Draft.Model.Constants.DraftEditorCommand;
1165import DraftDragType = Draft.Model.Constants.DraftDragType;
1166import DraftBlockType = Draft.Model.Constants.DraftBlockType;
1167import DraftBlockRenderConfig = Draft.Model.ImmutableData.DraftBlockRenderConfig;
1168import DraftBlockRenderMap = Draft.Component.Base.DraftBlockRenderMap;
1169import DraftInlineStyleType = Draft.Model.Constants.DraftInlineStyleType;
1170import DraftEntityMutability = Draft.Model.Constants.DraftEntityMutability;
1171import DraftEntityType = Draft.Model.Constants.DraftEntityType;
1172import DraftRemovalDirection = Draft.Model.Constants.DraftRemovalDirection;
1173import DraftHandleValue = Draft.Model.Constants.DraftHandleValue;
1174import DraftInsertionType = Draft.Model.Constants.DraftInsertionType;
1175import DraftStyleMap = Draft.Component.Base.DraftStyleMap;
1176
1177import DraftModel = Draft.Model;
1178import DraftComponent = Draft.Component;
1179
1180export {
1181 AtomicBlockUtils,
1182 BlockMap,
1183 BlockMapBuilder,
1184 CharacterMetadata,
1185 CompositeDecorator,
1186 ContentBlock,
1187 ContentState,
1188 convertFromHTML,
1189 convertFromRaw,
1190 convertToRaw,
1191 DefaultDraftBlockRenderMap,
1192 DefaultDraftInlineStyle,
1193 DraftBlockRenderConfig,
1194 DraftBlockRenderMap,
1195 DraftBlockType,
1196 DraftComponent,
1197 DraftDecorator,
1198 DraftDecoratorComponentProps,
1199 DraftDecoratorType,
1200 DraftDragType,
1201 DraftEditorCommand,
1202 DraftEntityMutability,
1203 DraftEntityType,
1204 DraftHandleValue,
1205 DraftInlineStyle,
1206 DraftInlineStyleType,
1207 DraftInsertionType,
1208 DraftModel,
1209 DraftRemovalDirection,
1210 DraftStyleMap,
1211 Editor,
1212 EditorBlock,
1213 EditorChangeType,
1214 EditorCommand,
1215 EditorProps,
1216 EditorState,
1217 Entity,
1218 EntityInstance,
1219 genKey,
1220 getDefaultKeyBinding,
1221 getVisibleSelectionRect,
1222 KeyBindingUtil,
1223 Modifier,
1224 RawDraftContentBlock,
1225 RawDraftContentState,
1226 RawDraftEntity,
1227 RawDraftEntityRange,
1228 RawDraftInlineStyleRange,
1229 RichUtils,
1230 SelectionState,
1231};
1232
\No newline at end of file