|
5 | 5 |  |
6 | 6 | [](https://www.npmjs.com/package/@djsp/core) [](https://standardjs.com) |
7 | 7 |
|
8 | | -## License |
| 8 | +`@djsp/core` is the heart of `@djsp`. It contains these exports: |
9 | 9 |
|
10 | | -MIT © [juliankrispel](https://github.com/juliankrispel) |
| 10 | +```js |
| 11 | +import { |
| 12 | + EditorContainer, |
| 13 | + Editor, |
| 14 | + Plugin, |
| 15 | + withPluginContext, |
| 16 | +} from '@djsp/core' |
| 17 | +``` |
| 18 | + |
| 19 | +# The `<EditorContainer />` component |
11 | 20 |
|
| 21 | +### Usage |
12 | 22 |
|
13 | | -## API |
| 23 | +`EditorContainer` contains all props for the draft js Editor which it passes down to plugins and the `Editor` via the React context api. |
14 | 24 |
|
15 | | -### <Plugin /> |
| 25 | +```js |
| 26 | +import { EditorContainer } from '@djsp/core' |
16 | 27 |
|
17 | | -```jsx |
18 | | -import { Plugin } from '@djsp/core' } |
| 28 | +<EditorContainer onChange={this.onChange} editorState={this.state.editorState}> |
| 29 | + <Editor /> |
| 30 | +</EditorContainer> |
19 | 31 | ``` |
20 | 32 |
|
21 | | -#### Props |
22 | | -A plugin inherits the same props as as the [Draft js Editor](https://draftjs.org/docs/api-reference-editor) including a few exceptions. Here's the full list of props: |
23 | | -```jsx |
24 | | -export type PluginProps = { |
25 | | - blockRendererFn: (block: BlockNodeRecord) => ?Object, |
26 | | - blockStyleFn: (block: BlockNodeRecord) => string, |
27 | | - keyBindingFn: (e: SyntheticKeyboardEvent<>) => ?string, |
28 | | - handleReturn?: ( |
29 | | - e: SyntheticKeyboardEvent<>, |
30 | | - editorState: EditorState, |
31 | | - ) => DraftHandleValue, |
32 | | - handleKeyCommand?: ( |
33 | | - command: DraftEditorCommand | string, |
34 | | - editorState: EditorState, |
35 | | - eventTimeStamp: number, |
36 | | - ) => DraftHandleValue, |
37 | | - handleBeforeInput?: ( |
38 | | - chars: string, |
39 | | - editorState: EditorState, |
40 | | - eventTimeStamp: number, |
41 | | - ) => DraftHandleValue, |
42 | | - handlePastedText?: ( |
43 | | - text: string, |
44 | | - html?: string, |
45 | | - editorState: EditorState, |
46 | | - ) => DraftHandleValue, |
47 | | - handlePastedFiles?: (files: Array<Blob>) => DraftHandleValue, |
48 | | - handleDroppedFiles?: ( |
49 | | - selection: SelectionState, |
50 | | - files: Array<Blob>, |
51 | | - ) => DraftHandleValue, |
52 | | - handleDrop?: ( |
53 | | - selection: SelectionState, |
54 | | - dataTransfer: Object, |
55 | | - isInternal: DraftDragType, |
56 | | - ) => DraftHandleValue, |
57 | | - onEscape?: (e: SyntheticKeyboardEvent<>) => void, |
58 | | - onTab?: (e: SyntheticKeyboardEvent<>) => void, |
59 | | - onUpArrow?: (e: SyntheticKeyboardEvent<>) => void, |
60 | | - onRightArrow?: (e: SyntheticKeyboardEvent<>) => void, |
61 | | - onDownArrow?: (e: SyntheticKeyboardEvent<>) => void, |
62 | | - onLeftArrow?: (e: SyntheticKeyboardEvent<>) => void, |
63 | | - onBlur?: (e: SyntheticEvent<>) => void, |
64 | | - onFocus?: (e: SyntheticEvent<>) => void, |
65 | | - customStyleMap?: Object, |
66 | | - customStyleFn?: (style: DraftInlineStyle, block: BlockNodeRecord) => ?Object, |
67 | | - blockRenderMap: DraftBlockRenderMap, |
68 | | -}; |
| 33 | +Here are the props that `EditorContainer` accepts, all of the below props are inherted from the [draft js editor component](https://draftjs.org/docs/api-reference-editor#props) |
| 34 | + |
| 35 | +### Props |
| 36 | + |
| 37 | +| Property | Type | required? | Description | |
| 38 | +| - | - | - | - | |
| 39 | +| `editorState` | `EditorState` | required | The state of the editor. Identical to the [draft js editorState prop](https://draftjs.org/docs/api-reference-editor#editorstate) | |
| 40 | +| `onChange` | `(EditorState) => void` | required | Fired when content changes. Identical to the [draft js onChange prop](https://draftjs.org/docs/api-reference-editor#onchange) | |
| 41 | +| `textAlignment` | `"left" or "center" or "right"` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 42 | +| `textDirectionality` | `"LTR" or "RTL"` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 43 | +| `placeholder` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 44 | +| `readOnly` | `boolean` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 45 | +| `spellCheck` | `boolean` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 46 | +| `stripPastedStyles` | `boolean` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 47 | +| `tabIndex` | `number` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 48 | +| `autoCapitalize` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 49 | +| `autoComplete` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 50 | +| `autoCorrect` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 51 | +| `ariaActiveDescendantID` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 52 | +| `ariaAutoComplete` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 53 | +| `ariaControls` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 54 | +| `ariaDescribedBy` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 55 | +| `ariaExpanded` | `boolean` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 56 | +| `ariaLabel` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 57 | +| `ariaLabelledBy` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 58 | +| `ariaMultiline` | `boolean` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 59 | +| `webDriverTestID` | `string` | optional | Overrides [props set via plugin](#setEditorProps) | |
| 60 | + |
| 61 | +# The `<Editor>` Component |
| 62 | + |
| 63 | +The `Editor` component does not accept any props, since it inherits all of it's props from `EditorContainer`. It must be rendered inside `EditorContainer` |
| 64 | + |
| 65 | +To configure editor props, use `EditorContainer` and `Plugin` |
| 66 | + |
| 67 | +### Usage |
| 68 | + |
| 69 | +```js |
| 70 | +<EditorContainer |
| 71 | + onChange={editorState => this.setState({ editorState })} |
| 72 | + editorState={this.state.editorState} |
| 73 | +> |
| 74 | + <Editor /> |
| 75 | +</EditorContainer> |
69 | 76 | ``` |
70 | 77 |
|
| 78 | +# The `<Plugin>` Component |
| 79 | + |
| 80 | + |
| 81 | +The `Plugin` can only be renderered inside `EditorContainer`. It's props will be plugged in to the editorState. This way you can compose and isolate editor functionality like `decorators`, `blockRendererFn` or any of the props listed below: |
| 82 | + |
| 83 | +### Props |
| 84 | + |
| 85 | +| Property | Type | required? | Description | |
| 86 | +| - | - | - | - | |
| 87 | +| `children` | `({ setEditorState, editorState, setEditorProps, editorRef, editorProps }) => React.Node` | optional | Render Prop, [for usage see here](#plugin-render-prop) | |
| 88 | +| `customStyleMap` | `Object` | optional | Identical to the [draft js `customStyleMap` prop](https://draftjs.org/docs/api-reference-editor#customstylemap) | |
| 89 | +| `blockRenderMap` | `Immutable.Map<{\nelement: string, wrapper?: React.Node, aliasedElements?: Array<string> }>` | optional | Identical to the [draft js `blockRenderMap` prop](https://draftjs.org/docs/api-reference-editor#blockrenderermap) | |
| 90 | +| `blockRendererFn` | `(block: BlockNodeRecord) => { component: Component, props: Object, editable: boolean }` | optional | Identical to the [draft js `blockRendererFn` prop](https://draftjs.org/docs/api-reference-editor#blockrendererfn) | |
| 91 | +| `blockStyleFn` | `(block: BlockNodeRecord) => cssClassName: string` | optional | Identical to the [draft js `blockStyleFn` prop](https://draftjs.org/docs/api-reference-editor#blockstylefn) | |
| 92 | +| `customStyleFn` | `(style: Immutable.OrderedSet<string>, block: BlockNodeRecord) => ?Object` | optional | Identical to the [draft js `customStyleFn` prop](https://draftjs.org/docs/api-reference-editor#customstylefn) | |
| 93 | +| `keyBindingFn` | `(e: SyntheticKeyboardEvent<>) => ?string` | optional | Identical to the [draft js `keyBindingFn` prop](https://draftjs.org/docs/api-reference-editor#keybindingfn) | |
| 94 | +| `handleKeyCommand` | `(command: string, editorState: EditorState) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handleKeyCommand` prop](https://draftjs.org/docs/api-reference-editor#handlekeycommand) | |
| 95 | +| `handleBeforeInput` | `(chars: string, editorState: EditorState) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handleBeforeInput` prop](https://draftjs.org/docs/api-reference-editor#handlebeforeinput) | |
| 96 | +| `handlePastedText` | `(text: string, html?: string, editorState: EditorState) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handlePastedText` prop](https://draftjs.org/docs/api-reference-editor#handlepastedtext) | |
| 97 | +| `handlePastedFiles` | `(files: Array<Blob>) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handlePastedFiles` prop](https://draftjs.org/docs/api-reference-editor#handlepastedfiles) | |
| 98 | +| `handleDroppedFiles` | `(selection: SelectionState, files: Array<Blob>) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handleDroppedFiles` prop](https://draftjs.org/docs/api-reference-editor#handledroppedfiles) | |
| 99 | +| `handleDrop` | `(selection: SelectionState, dataTransfer: Object, isInternal: 'internal' or 'external') => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handleDrop` prop](https://draftjs.org/docs/api-reference-editor#handledrop) | |
| 100 | +| `handleReturn` | `(e: SyntheticKeyboardEvent<>, editorState: EditorState) => 'handled' or 'not-handled'` | optional | Identical to the [draft js `handleReturn` prop](https://draftjs.org/docs/api-reference-editor#handlereturn) | |
| 101 | +| `onDownArrow` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onDownArrow` prop](https://draftjs.org/docs/api-reference-editor#ondownarrow) | |
| 102 | +| `onUpArrow` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onUpArrow` prop](https://draftjs.org/docs/api-reference-editor#onuparrow) | |
| 103 | +| `onLeftArrow` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onLeftArrow` prop](https://draftjs.org/docs/api-reference-editor#onleftarrow) | |
| 104 | +| `onRightArrow` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onRightArrow` prop](https://draftjs.org/docs/api-reference-editor#onrightarrow) | |
| 105 | +| `onTab` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onTab` prop](https://draftjs.org/docs/api-reference-editor#ontab) | |
| 106 | +| `onEscape` | `(e: SyntheticKeyboardEvent<>) => void` | optional | Identical to the [draft js `onEscape` prop](https://draftjs.org/docs/api-reference-editor#onescape) | |
| 107 | +| `onFocus` | `(e: SyntheticEvent<>) => void` | optional | Identical to the [draft js `onFocus` prop](https://draftjs.org/docs/api-reference-editor#onfocus) | |
| 108 | +| `onBlur` | `(e: SyntheticEvent<>) => void` | optional | Identical to the [draft js `onBlur` prop](https://draftjs.org/docs/api-reference-editor#onblur) | |
| 109 | + |
| 110 | +### Plugin render prop |
| 111 | + |
| 112 | +The `Plugin` also accepts an optional render prop which exposes the plugin context. |
| 113 | +```js |
| 114 | +<Plugin> |
| 115 | + {({ |
| 116 | + setEditorState, |
| 117 | + editorProps, |
| 118 | + editorRef, |
| 119 | + editorState, |
| 120 | + setEditorProps |
| 121 | + }) => ( |
| 122 | + <button |
| 123 | + onClick={() => ( |
| 124 | + setEditorState( |
| 125 | + RichUtils.toggleBlockType(editorState, 'header-one') |
| 126 | + ) |
| 127 | + )} |
| 128 | + >H1</button> |
| 129 | + )} |
| 130 | +</Plugin> |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +### <Plugin>(RenderProps)</Plugin> |
| 137 | +| Property | Type | Description | |
| 138 | +| `editorState` | `EditorState` | The `EditorState` object | |
| 139 | +| `setEditorState` | `(editorState: EditorState) => void` | Lets you update the editorState | |
| 140 | +| `editorProps` | `Object` | Contains props that can be set via `setEditorProps`, these are `editorKey` `placeholder` `textAlignment` `textDirectionality` `readOnly` `spellCheck` `stripPastedStyles` `tabIndex` `autoCapitalize` `autoComplete` `autoCorrect` `ariaActiveDescendantID` `ariaAutoComplete` `ariaControls` `ariaDescribedBy` `ariaExpanded` `ariaLabel` `ariaLabelledBy` `ariaMultiline` `webDriverTestID` | |
| 141 | +| `setEditorProps` | `(editorProps: Object) => void` | lets you set the above editorProps. Be aware that editor props set via Plugins are overridden by `EditorContainer` props | |
| 142 | +| `editorRef` | `Ref<DraftEditor>` | A React reference to the draft js editor | |
| 143 | + |
| 144 | +# `withPluginContext` |
| 145 | + |
| 146 | +Another way to create a plugin is with the `withPluginContext` HOC. This has the advantage of letting you handle plugin subscription. |
| 147 | + |
| 148 | +## License |
| 149 | + |
| 150 | +MIT © [juliankrispel](https://github.com/juliankrispel) |
| 151 | + |
0 commit comments