[Mentions] How would load text which contains mentions?
I want to to use the mentions plugin to edit data which contains mentions. All of the examples use EditorState.createEmpty(). I want to load state
The output of the editor should be stored as plain text - so, plain text with @mentions in it will need to be parsed?
Maybe it already does this and im not aware?
Draft.js has a two utility functions that should be helpful to you. convertToRaw still compiles to a data datastructure, but it will only contain JSON valid types.
see https://facebook.github.io/draft-js/docs/api-reference-data-conversion.html#convertfromraw
Keep in mind to convert the mentions to Immutable Maps before converting them from a raw from the server.
Does that help?
So i should be storing raw data.. hmm ok. i guess that makes sense. just seemed messy.
thanks!
I am still having issues. When i convert from raw, it seems that the mention is not an immutablejs object (it is vanillajs). I am doing this to hydrate the mentions:
const raw = convertFromRaw(JSON.parse(this.props.value)); editorState = EditorState.createWithContent(raw);
https://github.com/draft-js-plugins/draft-js-plugins/blob/master/draft-js-mention-plugin/src/Mention/index.js#25 ... if (mention.has('link')) { ... errors : index.js?c479:24 Uncaught (in promise) TypeError: mention.has is not a function(…)
My mention object is just: { name: 'xxxxx', avatar: 'xxxxx' }
So, somehow it isnt an immutable object - have you had it working laoding from Raw?
am i missing a decorator in the createWithContent?
This is due the fact that we expect mentions to be an Immutable Map which nowadays I feel was a mistake …
See here a code example on how to fix it: https://github.com/draft-js-plugins/draft-js-plugins/blob/master/FAQ.md#why-are-mentions-broken-after-using-convertfromraw-and-throwing-an-error
@nikgraf +1 on removing dependency on Immutable Map.
Thanks guys! got it working. finally.
One thing I did find weird, was that the json.entityMap is not an array, but an object keyed with numeric values { "0": { ... entity... } } which further confused things (presume thats why the example uses some forEach function it iterate it. Its one thing after the next it seems... shakes head (ps: i know that is react code not related to this project)
Thanks again!
I used the emoji and the mention plugins together in my Editor. When I am using ,
convertToRaw(contentState)
to save my content data into the db, it shows an error message.
invariant.js:38 Uncaught Error: Unknown DraftEntity key.
But this error only comes when I have used mention or emoji content. it works fine with plan text.
@tonypee can you please share your solution? and did you have to store the state on the server?
I wasn't able to fix this. I cannot show mentions unless my editorState is empty.
My Draft.js:
"draft-js": "^0.10.5",
"draft-js-mention-plugin": "^2.0.1",
"draft-js-plugins-editor": "^2.0.4",
Here is a working sample:
class TextEditorComponent extends React.Component<Props, State> {
editor: EditorRef;
_dispatchChange = debounce((editorState: EditorState) => {
const currentContent = editorState.getCurrentContent();
const rawContent: RichTextRaw = convertToRaw(currentContent);
this.props.onChange(rawContent);
}, 1000);
constructor(props: Props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
suggestions: this.props.mentions
};
}
_handleChange = editorState => {
this.setState({ editorState: editorState });
this._dispatchChange(editorState);
};
onSearchChange = ({ value }) => {
this.setState({
suggestions: defaultSuggestionsFilter(value, this.props.mentions)
});
};
onAddMention = () => {
// get the mention object selected
};
focus = () => {
this.editor.focus();
};
render() {
const { MentionSuggestions } = mentionPlugin;
const plugins = [mentionPlugin];
return (
<div className="editor" onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this._handleChange}
plugins={plugins}
ref={element => {
this.editor = element;
}}
/>
<MentionSuggestions
onSearchChange={this.onSearchChange}
suggestions={this.state.suggestions}
onAddMention={this.onAddMention}
entryComponent={Entry}
/>
</div>
);
}
}
The moment I try any of the following, the mentions stop appearing or triggering. That means when I type my trigger '{{' characters, the dropdown does not appear, AND whatever I had saved before does not format itself as pretty mentions when displayed in the editor:
This is how I load my data and how it breaks the mention plugin:
componentDidMount() {
try {
if (this.props.defaultValue) {
const rawContent = this.props.defaultValue;
forEach(rawContent.entityMap, (value, key) => {
value.data.mention = fromJS(value.data.mention);
});
const content = EditorState.createWithContent(convertFromRaw(rawContent));
this.setState({
editorState: content,
suggestions: this.props.mentions,
});
}
} catch (error) {
logger.error('TextEditorComponent::', error);
}
}
Does anyone know what the problem is?
All good, guys! I managed to solve this. I was just not using Draft.JS properly.
All good, guys! I managed to solve this. I was just not using Draft.JS properly.
How did you manage this issue?
@effortlesscoding, how to solve this problem? Can you share with us?