Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draftjs components with props

I'm new to draftjs and I was wondering if there was a way to render my custom components inline in the editor.

I have a string with twitter handles. I use the decorator to detect regex @[{handle}] which replaces the handle and renders the component inline. However my handle component needs properties such as a callback function and a URL.

I'm not too sure how to pass my component the URL and callback function which I pass into my ContentEditable component.

I'm sure I'm just missing something. I've checked the contentState.getEntity(entityKey).getType() but it only sees the content I pass into the composite decorator as unstyled and not the decorated parts as separate blocks.

I've seen that you can modify the entity map, but I'm not sure if this is the right approach or how to define my own entity in the entity map

Does anyone know what I am missing to give properties to my component?

const decorator = new CompositeDecorator([
  {
    strategy: handleStrategy,
    component: Handle,
  },
]);


export default class ContentEditable extends component {
    const content = 'some messages and my handle @[handle]';
    if (this.props.content.trim() !== '') {
      const processedHTML = DraftPasteProcessor.processHTML(content);
      const entityMap = processedHTML.entityMap;
      const contentState = ContentState.createFromBlockArray(processedHTML.contentBlocks, entityMap);
      // Create with content with decorator
      editorState = EditorState.createWithContent(contentState, decorator);
    } else {
      // Create empty content with decorator
      editorState = EditorState.createEmpty(decorator);
    }
    this.state = {
      editorState,
    }
}

render() {
    return (
        <Editor
          editorState={this.state.editorState}
          onChange={this.onChange}
          ref="editor"
        />
    );
  }
like image 925
diepjy Avatar asked Mar 09 '17 15:03

diepjy


People also ask

Does draft js support markdown?

Unfortunately, Draft. js doesn't natively Markdown and even if there are good initiatives, I didn't find a correct Draft/Markdown converter.

Does Facebook use draft js?

Draft. js is used in production on Facebook, including status and comment inputs, Notes, and messenger.com.

What is Draftjs?

Draft. js is a framework for building rich text editors in React, powered by an immutable model and abstracting over cross-browser differences.


1 Answers

I'm sorry the document is missing it. You can provide props in CompositeDecorator like CompositeDecorator({strategy:xxx,component:xxx,props:{...}}) Checking the source

like image 98
Jiang YD Avatar answered Nov 07 '22 11:11

Jiang YD