draft-js-plugins icon indicating copy to clipboard operation
draft-js-plugins copied to clipboard

[Mentions] How would load text which contains mentions?

Open tonypee opened this issue 10 years ago • 13 comments

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?

tonypee avatar Jun 07 '16 05:06 tonypee

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?

nikgraf avatar Jun 07 '16 15:06 nikgraf

So i should be storing raw data.. hmm ok. i guess that makes sense. just seemed messy.

thanks!

tonypee avatar Jun 08 '16 05:06 tonypee

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?

tonypee avatar Jun 09 '16 07:06 tonypee

am i missing a decorator in the createWithContent?

tonypee avatar Jun 09 '16 07:06 tonypee

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 avatar Jun 09 '16 13:06 nikgraf

@nikgraf +1 on removing dependency on Immutable Map.

oyeanuj avatar Jun 09 '16 18:06 oyeanuj

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!

tonypee avatar Jun 09 '16 22:06 tonypee

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.

ErangaLakmal avatar Oct 26 '16 13:10 ErangaLakmal

@tonypee can you please share your solution? and did you have to store the state on the server?

evansque avatar Nov 30 '17 19:11 evansque

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?

effortlesscoding avatar Mar 18 '18 01:03 effortlesscoding

All good, guys! I managed to solve this. I was just not using Draft.JS properly.

effortlesscoding avatar Mar 18 '18 06:03 effortlesscoding

All good, guys! I managed to solve this. I was just not using Draft.JS properly.

How did you manage this issue?

mchaledg19 avatar Jun 27 '19 11:06 mchaledg19

@effortlesscoding, how to solve this problem? Can you share with us?

CXJoyce avatar Oct 15 '19 10:10 CXJoyce