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.

Update SelectionState on readOnly Editor component

See original GitHub issue

Do you want to request a feature or report a bug? Feature request, I presume.

What is the current behavior? When the Editor component is set to readOnly, no events are fired at all (e.g. the onChange method is ignored). This seems to be by design: “Set whether the editor should be rendered as static DOM, with all editability disabled.” However, not even selection events are triggered.

What is the expected behavior? My use case: I want to render non-editable text within an Editor component (taking advantage of the Entity rendering), but still get an updated SelectionState on each new selection to display a tooltip near the selected text + save the current selection to a Redux store. I can implement this easily without the readOnly attribute: ezgif com-video-to-gif But once I set the readOnly attribute, I have no way of retrieving the current selection from the Editor model. My current workaround is not to set the attribute but instead discard all editor changes apart from selection state updates.

Would be nice to be able to keep onSelect on the Editor component while disabling all editing with readOnly, like requested in #467 .

// in constructor
this.onChange = editorState => {
  if (this.state.editorState.getCurrentContent() != editorState.getCurrentContent()) {
      this.setState( EditorState.undo(editorState) );
  } else {
      this.props.dispatch(/* action to save raw ContentState + SelectionState to Redux store */);
      this.setState( EditorState.undo(editorState) );
  }
}  

Which versions of Draft.js, and which browser / OS are affected by this issue? Did this work in previous versions of Draft.js? Working with Draft.js 0.9.1, macOS 10.12 + Google Chrome 53.0.2785.116. Didn’t work previously afaik.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:22
  • Comments:8

github_iconTop GitHub Comments

10reactions
terencechowcommented, Oct 17, 2017

I was able to solve this with a bit of a hack:

In read only mode:

<Editor
    keyBindingFn={() => 'not-handled-command'}
/>

Basically this causes the Editor to return a custom ‘not-handled-command’ for all input including backspace, pasting etc. https://draftjs.org/docs/advanced-topics-key-bindings.html

Then Draft js looks for this not-handled-command in the handleKeyCommand prop. Since we don’t pass a custom handleKeyCommand to handle this custom command, nothing seems to change. However selecting stuff still calls the onChange handler in draft-js so I am able to get onChange events to fire even in this ‘readOnly’ mode

5reactions
tpaicommented, Mar 14, 2018

For someone who finding this solution, the hack from @terencechow is fully block the most of characters, but sadly the composition input method such as Chinese or Japanese could not work at all, and the handleBeforeInput also could not catch any of them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Update SelectionState on readOnly Editor component -
Update SelectionState on readOnly Editor component. ... When the Editor component is set to readOnly , no events are fired at all (e.g....
Read more >
Editor Component - Draft.js
The EditorState object to be rendered by the Editor . ... See Advanced Topics: Block Components for details on usage. ... readOnly?: boolean....
Read more >
DraftJS handle focus when page has multiple editor (editable ...
I'm facing issue with follow DraftJS editor component. ... (state: EditorState) => void; readOnly?: boolean; } export const Editor: React.
Read more >
lightning-input-rich-text - documentation - Salesforce Developers
A lightning-input-rich-text component creates a rich text editor based on the ... text as described, and return to the previous selection state. editor....
Read more >
SelectionManager - ApacheFlex API Reference
[read-only] The editing mode. SelectionManager. focused : Boolean. [read-only] Indicates whether a container in the text flow has the focus.
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