mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-08 09:17:06 +08:00
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:
@@ -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 });
|
||||
|
||||
5
types/draft-js/index.d.ts
vendored
5
types/draft-js/index.d.ts
vendored
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user