UNPKG

3.97 kBTypeScriptView Raw
1import {
2 ContentBlock,
3 DraftDecorator,
4 DraftDragType,
5 DraftEditorCommand,
6 DraftHandleValue,
7 DraftInlineStyle,
8 DraftStyleMap,
9 Editor,
10 EditorProps,
11 EditorState,
12 SelectionState
13} from "draft-js";
14import { Component, Ref, SyntheticEvent, KeyboardEvent } from "react";
15
16export interface PluginFunctions {
17 getPlugins(): EditorPlugin[]; // a function returning a list of all the plugins
18 getProps(): any; // a function returning a list of all the props pass into the Editor
19 setEditorState(editorState: EditorState): void; // a function to update the EditorState
20 getEditorState(): EditorState; // a function to get the current EditorState
21 getReadOnly(): boolean; // a function returning of the Editor is set to readOnly
22 setReadOnly(readOnly: boolean): void; // a function which allows to set the Editor to readOnly
23 getEditorRef(): Ref<any>; // a function to get the editor reference
24}
25
26export interface EditorPlugin {
27 decorators?: DraftDecorator[];
28 getAccessibilityProps?: () => {
29 ariaHasPopup: string;
30 ariaExpanded: string;
31 };
32 initialize?: (pluginFunctions: PluginFunctions) => void;
33 onChange?: (
34 editorState: EditorState,
35 pluginFunctions: PluginFunctions
36 ) => EditorState;
37 willUnmount?: (pluginFunctions: PluginFunctions) => void;
38
39 // Events passed from the draft-js editor back to all plugins
40 blockRendererFn?(block: ContentBlock, pluginFunctions: PluginFunctions): any;
41 blockStyleFn?(block: ContentBlock, pluginFunctions: PluginFunctions): string;
42 customStyleFn?: (
43 style: DraftInlineStyle,
44 block: ContentBlock,
45 pluginFunctions: PluginFunctions
46 ) => DraftStyleMap;
47 keyBindingFn?(
48 e: KeyboardEvent,
49 pluginFunctions: PluginFunctions
50 ): DraftEditorCommand | null;
51 handleReturn?(
52 e: KeyboardEvent,
53 editorState: EditorState,
54 pluginFunctions: PluginFunctions
55 ): DraftHandleValue;
56 handleKeyCommand?(
57 command: DraftEditorCommand,
58 editorState: EditorState,
59 eventTimeStamp: number,
60 pluginFunctions: PluginFunctions
61 ): DraftHandleValue;
62 handleBeforeInput?(
63 chars: string,
64 editorState: EditorState,
65 eventTimeStamp: number,
66 pluginFunctions: PluginFunctions
67 ): DraftHandleValue;
68 handlePastedText?(
69 text: string,
70 html: string | undefined,
71 editorState: EditorState,
72 pluginFunctions: PluginFunctions
73 ): DraftHandleValue;
74 handlePastedFiles?(
75 files: Array<Blob>,
76 pluginFunctions: PluginFunctions
77 ): DraftHandleValue;
78 handleDroppedFiles?(
79 selection: SelectionState,
80 files: Array<Blob>,
81 pluginFunctions: PluginFunctions
82 ): DraftHandleValue;
83 handleDrop?(
84 selection: SelectionState,
85 dataTransfer: Object,
86 isInternal: DraftDragType,
87 pluginFunctions: PluginFunctions
88 ): DraftHandleValue;
89 onEscape?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
90 onTab?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
91 onUpArrow?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
92 onDownArrow?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
93 onRightArrow?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
94 onLeftArrow?(e: KeyboardEvent, pluginFunctions: PluginFunctions): void;
95 onBlur?(e: SyntheticEvent, pluginFunctions: PluginFunctions): void;
96 onFocus?(e: SyntheticEvent, pluginFunctions: PluginFunctions): void;
97}
98
99export const composeDecorators: (
100 ...decorators: DraftDecorator[]
101) => DraftDecorator;
102
103export interface PluginEditorProps extends EditorProps {
104 plugins?: EditorPlugin[];
105 defaultKeyBindings?: boolean;
106 defaultKeyCommands?: boolean;
107 defaultBlockRenderMap?: boolean;
108
109 // eslint-disable-next-line react/no-unused-prop-types
110 decorators?: DraftDecorator[];
111}
112
113declare class PluginEditor extends Component<PluginEditorProps> {
114 focus(): void;
115 blur(): void;
116 getPlugins(): EditorPlugin[];
117 getPluginMethods(): PluginFunctions;
118 getEditorRef(): Editor | undefined;
119}
120
121export default PluginEditor;