question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple draftjs editors cannot be used on the same page using plugins

See original GitHub issue

Multiple draftjs editors cannot be used on the same page using plugins. All interactions are sent to the last instance on the page.

Clone the repo, install dependencies and change line 77 in /stories/index.js

from

.add('CustomToolbarEditor', () => <CustomToolbarEditor />)

to

.add('CustomToolbarEditor', () => <div><CustomToolbarEditor /><CustomToolbarEditor /></div>)

Now run yarn storybook and open the correct example

image

Test functionality of both editors and you will see the controls misbehave and target the last editor.

screenrecording20190222at3

A similar issue is https://github.com/draft-js-plugins/draft-js-plugins/issues/548

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

63reactions
MartinSeelercommented, Feb 27, 2019

Thank you, I have converted it to something similar with useState. With this, each editor get’s its own plugins.

const CustomEditor = () => {
  const [editorState, setEditorState] = useState(() => EditorState.createEmpty());

  const [{ plugins, Toolbar }] = useState(() => {
    const toolbarPlugin = createToolbarPlugin();
    const { Toolbar } = toolbarPlugin;
    const plugins = [toolbarPlugin];
    return {
      plugins,
      Toolbar
    };
  });

  const editorRef = useRef(null);

  return (
    <div>
      <div
        className="editor"
        onClick={() => editorRef.current && editorRef.current.focus()}
      >
        <Editor
          editorState={editorState}
          onChange={(newEditorState: EditorState) => setEditorState(newEditorState)}
          plugins={plugins}
          ref={(editor: any) => (editorRef.current = editor)}
        />
        <Toolbar>
          {// may be use React.Fragment instead of div to improve perfomance after React 16
          (externalProps: any) => (
            <div>
              <BoldButton {...externalProps} />
              <ItalicButton {...externalProps} />
              <UnderlineButton {...externalProps} />
              <CodeButton {...externalProps} />
              <Separator {...externalProps} />
              <UnorderedListButton {...externalProps} />
              <OrderedListButton {...externalProps} />
            </div>
          )}
        </Toolbar>
      </div>
    </div>
  );
};
5reactions
MartinSeelercommented, Feb 26, 2019

This is really an issue for me. Any suggestions or workarounds?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple draft-js-plugins editors on the same page don't work
Each editor get's its own plugins. You can solve this issue ether by creating different plugin for each editor instance and pass them...
Read more >
Issues and Pitfalls - Draft.js
This article addresses some known issues with the Draft editor framework, as well as some common pitfalls that we have encountered while ...
Read more >
Building a Rich Text Editor with React and Draft.js, Pt. 2.2
In this post, I will review how to build both a key command and button to embed hyperlinks within your text editor using...
Read more >
draft-js-styletoprops-plugin - npm
Cusotom styles to props plugin for DraftJS Plugins Editor. ... having to use multiple 3rd-party plugins to build the most simple functions ...
Read more >
Getting started with draft.js - React Rocket
Draft.js is a powerful framework for creating text based editors. ... started with draft.js (current); Getting started with draft.js plugins ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found