最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Draft.js 's RichUtils.toggleInlineStyle doesn't work - Stack Overflow

programmeradmin0浏览0评论

Draft.js 's RichUtils.toggleInlineStyle doesn't work properly. please help! My code is on JSfiddle.

Is there any misunderstood?

var TextArea = React.createClass({
  ...
  toggleBlockStyle: function(blockType) {
    this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)); // don't work!
  },

  toggleInlineStyle: function(inlineStyle) {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle)); // don't work!
  },

  handleClear: function() {
    this.onChange(EditorState.push(this.state.editorState, 
        ContentState.createFromText(''), 'remove-range')); // don't work!
  },
  ...
  render: function() {
    return (
      <div onClick={this.onFocus}>
        {this.renderButtons()}
        <Editor editorState={this.state.editorState}
          className={this.props.className}
          name={this.props.name} ref="editor"
          placeholder={this.props.placeholder}
          handleKeyCommand={this.handleKeyCommand}
          onChange={this.onChange}
          spellCheck={true}
          stripPastedStyles={true}
          customStyleMap={myStyleMap}/>
      </div>);
   }
}

Draft.js 's RichUtils.toggleInlineStyle doesn't work properly. please help! My code is on JSfiddle.

Is there any misunderstood?

var TextArea = React.createClass({
  ...
  toggleBlockStyle: function(blockType) {
    this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)); // don't work!
  },

  toggleInlineStyle: function(inlineStyle) {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle)); // don't work!
  },

  handleClear: function() {
    this.onChange(EditorState.push(this.state.editorState, 
        ContentState.createFromText(''), 'remove-range')); // don't work!
  },
  ...
  render: function() {
    return (
      <div onClick={this.onFocus}>
        {this.renderButtons()}
        <Editor editorState={this.state.editorState}
          className={this.props.className}
          name={this.props.name} ref="editor"
          placeholder={this.props.placeholder}
          handleKeyCommand={this.handleKeyCommand}
          onChange={this.onChange}
          spellCheck={true}
          stripPastedStyles={true}
          customStyleMap={myStyleMap}/>
      </div>);
   }
}
Share Improve this question asked May 11, 2016 at 11:38 森田昌宏森田昌宏 511 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

When you use a button to toggle the styles, it causes the editor to e out of focus and the style is not applied. Instead of onClick, use onMouseDown which fires before the editor unfocuses.

I found this fix in a github thread here. Quoted for expedience:

However, using onClick causes the text area to go out of focus and the style isn't toggled. I did some digging and found other solutions that suggested using preventDefault on the onClick function, but that doesn't do anything. With this event handler, the style is toggled only if the text is highlighted first and doesn't allow for the text to be styled prior to typing. I saw another solution that suggested replacing onClick with onMouseDown as it doesn't cause the text area to lose focus. I tried this, and it worked. I also delved into the source code for Draft.js, and in the demo editor code I saw this code:

 //...
  render() {
//...
    return (
      <span className={className} onMouseDown={this.onToggle}>
        {this.props.label}
      </span>
    );
  }
}; 

The reason why its not ing is you need to include the css file available. Include the css file and it will fine. (Draft.css)

https://draftjs/docs/advanced-topics-issues-and-pitfalls/#missing-draftcss

see the last line of the page.

Include the Draft.css file in your app by including the following line where your Draft JS Editor is running.

import "draft-js/dist/Draft.css";

Draft.css should be included when rendering the editor. Learn more about why.

发布评论

评论列表(0)

  1. 暂无评论