Skip to content
This repository was archived by the owner on May 31, 2020. It is now read-only.

Commit 86befa2

Browse files
committed
update core package a little
1 parent efd74d4 commit 86befa2

File tree

6 files changed

+167
-77
lines changed

6 files changed

+167
-77
lines changed

.bookignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**/node_modules
2+
**/*.js
3+
**/*.jsx
4+
**/*.png
5+
**/*.jpg
6+
**/package.json
7+
**/package-lock.json
8+
**/*.log
9+
**/.flowconfig
10+
dist
11+
examples
12+
**/src
13+
.vscode
14+
flow-lib
15+
.storybook
16+
.git
17+
.DS_Store

SUMMARY.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,4 @@
33
* [Introduction](README.md)
44

55
## Packages
6-
* [The Editor](packages/editor/README.md)
7-
* [Autocomplete](packages/autocomplete/README.md)
8-
* [Atomic Block](packages/atomic-block/README.md)
9-
* [Inline Style Toggle](packages/inline-style-toggle/README.md)
10-
* [Block Type Toggle](packages/block-type-toggle/README.md)
11-
* [Utils](packages/utils/README.md)
12-
6+
* [Core](packages/core/README.md)

packages/core/README.md

Lines changed: 135 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,147 @@
55
![file size](http://img.badgesize.io/https://unpkg.com/@djsp/core/dist/index.js?label=size&style=flat-square)
66
[![NPM](https://img.shields.io/npm/v/@djsp/core.svg)](https://www.npmjs.com/package/@djsp/core) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
77

8-
## License
8+
`@djsp/core` is the heart of `@djsp`. It contains these exports:
99

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
1120

21+
### Usage
1222

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.
1424

15-
### <Plugin />
25+
```js
26+
import { EditorContainer } from '@djsp/core'
1627

17-
```jsx
18-
import { Plugin } from '@djsp/core' }
28+
<EditorContainer onChange={this.onChange} editorState={this.state.editorState}>
29+
<Editor />
30+
</EditorContainer>
1931
```
2032

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>
6976
```
7077

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+

packages/core/src/EditorContainer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ const omitUndefined = obj =>
2828

2929
export const withEditorContext = <Props: {}>(
3030
Comp: ComponentType<Props>
31-
): ComponentType<Props & DraftEditorProps & { ref: Ref<typeof DraftEditor> }> =>
32-
function WithConsumer(props: Object) {
31+
): ComponentType<DraftEditorProps & { ref: Ref<typeof DraftEditor> }> =>
32+
function WithConsumer() {
3333
return (
3434
<Context.Consumer>
35-
{contextProps => <Comp {...props} {...contextProps.editorProps} />}
35+
{contextProps => <Comp {...contextProps.editorProps} />}
3636
</Context.Consumer>
3737
)
3838
}

packages/core/src/Plugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ class Plugin extends Component<Props> {
2626
componentWillUnmount = () => this.unsubscribe()
2727

2828
render() {
29-
const { setEditorState, editorState, setEditorProps } = this.props
29+
const {
30+
setEditorState,
31+
editorProps,
32+
editorRef,
33+
editorState,
34+
setEditorProps,
35+
} = this.props
3036

3137
if (this.props.children) {
3238
return this.props.children({
3339
setEditorState,
40+
editorProps,
41+
editorRef,
3442
editorState,
3543
setEditorProps,
3644
})

packages/core/src/index.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
// @flow
22

3-
import EditorContainer, {
4-
withEditorContext,
5-
withPluginContext,
6-
} from './EditorContainer'
3+
import EditorContainer, { withPluginContext } from './EditorContainer'
74
import Plugin from './Plugin'
85
import Editor from './Editor'
96
import * as constants from './constants'
107

11-
export {
12-
EditorContainer,
13-
withEditorContext,
14-
withPluginContext,
15-
Editor,
16-
Plugin,
17-
constants,
18-
}
8+
export { EditorContainer, Editor, Plugin, withPluginContext, constants }
199

2010
export type { PluginProps } from './types'

0 commit comments

Comments
 (0)