Adding missing ContentState argument to draft decorators

See https://github.com/facebook/draft-js/blob/master/src/model/decorators/DraftDecoratorType.js
This commit is contained in:
Ulf Schwekendiek
2018-03-11 16:44:49 -07:00
parent 9bb6dc2f8b
commit c26e70977e
2 changed files with 29 additions and 5 deletions

View File

@@ -22,7 +22,8 @@ import {
DraftEntityMutability,
DraftEntityType,
convertFromHTML,
convertToRaw
convertToRaw,
CompositeDecorator,
} from 'draft-js';
const SPLIT_HEADER_BLOCK = 'split-header-block';
@@ -38,6 +39,14 @@ export const KEYCODES: Record<KeyName, KeyCode> = {
type SyntheticKeyboardEvent = React.KeyboardEvent<{}>;
const HANDLE_REGEX = /\@[\w]+/g;
class HandleSpan extends React.Component {
render() {
return <span>{this.props.children}</span>
}
}
class RichEditorExample extends React.Component<{}, { editorState: EditorState }> {
constructor() {
super({});
@@ -51,8 +60,22 @@ class RichEditorExample extends React.Component<{}, { editorState: EditorState }
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap,
);
this.state = { editorState: EditorState.createWithContent(state) };
const decorator = new CompositeDecorator([{
strategy: (
block: ContentBlock,
callback: (start: number, end: number) => void,
contentState: ContentState
) => {
const text = block.getText();
let matchArr, start;
while ((matchArr = HANDLE_REGEX.exec(text)) !== null) {
start = matchArr.index;
callback(start, start + matchArr[0].length);
}
},
component: HandleSpan,
}]);
this.state = { editorState: EditorState.createWithContent(state, decorator) };
}
onChange: (editorState: EditorState) => void = (editorState: EditorState) => this.setState({ editorState });

View File

@@ -7,6 +7,7 @@
// Michael Wu <https://github.com/michael-yx-wu>
// Willis Plummer <https://github.com/willisplummer>
// Santiago Vilar <https://github.com/smvilar>
// Ulf Schwekendiek <https://github.com/sulf>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
@@ -398,7 +399,7 @@ declare namespace Draft {
/**
* Given a `ContentBlock`, return an immutable List of decorator keys.
*/
getDecorations(block: ContentBlock): Immutable.List<string>;
getDecorations(block: ContentBlock, contentState: ContentState): Immutable.List<string>;
/**
* Given a decorator key, return the component to use when rendering
@@ -456,7 +457,7 @@ declare namespace Draft {
class CompositeDraftDecorator {
constructor(decorators: Array<DraftDecorator>);
getDecorations(block: ContentBlock): Immutable.List<string>;
getDecorations(block: ContentBlock, contentState: ContentState): Immutable.List<string>;
getComponentForKey(key: string): Function;
getPropsForKey(key: string): Object;
}