javascript - 如何在没有 html 标记的情况下显示 React Quill 的内容?

标签 javascript reactjs quill

我设法让我的 Quill 正常工作,但现在我想制作一个漂亮的分屏,就像我们在这个论坛上所做的那样,但我无法弄清楚的一件事是如何将 Quill 的输入转换为漂亮的文本预览端。

我能够显示文本,但它仍然包含我当然不想要的所有 html 标签。

到目前为止,这是我的 Quill 设置:

export default class AddSpark extends Component {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);

    this.state ={
      content: '',
    };
  }

  onChange(html) {
    this.setState ({ content: html });
      console.log(html)
    }

  render() {
    return (
      <div>
      <Col xs={12} md={6}>
        <form ref={(input) => this.sparkForm = input} onSubmit={(e) => this.createSpark(e)}>

            <ControlLabel>Select your city</ControlLabel>
            <select id="formControlsCity" placeholder="Choose your city" onChange={this.onChange} className="form-control" onClick={ moreOptions } ref={(input) => this.city = input}>
              <option value="select">Choose your city</option>
              <option value="Beijing">Beijing</option>
              <option value="Shanghai">Shanghai</option>
              <option value="Chengdu & Chongqing">Chengdu & Chongqing</option>
            </select>
       
            <ControlLabel>Select your person</ControlLabel>
            <select id="formControlsPerson" placeholder="Choose your person" className="form-control" ref={(input) => this.person = input}>
              <option value="select">First select your city</option>
            </select>
    

            <ControlLabel>Select your location</ControlLabel>
            <select id="formControlsLocation" placeholder="Choose your location" className="form-control" ref={(input) => this.location = input}>
              <option value="select">First select your city</option>
            </select>

            <ControlLabel>Title</ControlLabel>
            <input type="text" label="Title" placeholder="Enter your title" className="form-control" ref={(input) => this.title = input}/>
          

            <ControlLabel>Content</ControlLabel>
              <div className='_quill'>
                <ReactQuill
                  ref='editor'
                  onChange={this.onChange}
                />
              </div>
              <br />

          <Button type="submit">Submit</Button>
        </form>
      </Col>
      <Col xs={12} md={6}>
      <h3>Preview</h3>
        {this.state.content}
      </Col>

      </div>
  )}
}

此刻我得到这个: enter image description here

非常感谢任何帮助!

最佳答案

最简单的方法是在禁用编辑的情况下使用相同的 react-quill 组件。

以下是可用于预览端的组件代码:

<ReactQuill
   value={this.state.content}
   readOnly={true}
   theme={"bubble"}
/>

它看起来像这样: sample image

注意:ReactQuill 带有两个内置主题(泡泡和雪)。

这是它在雪花主题中的样子。它的顶部有一个工具栏: bubble

这是它在气泡主题 中的样子。 snow theme

您应该通过在主题属性中传递字符串“bubble”来使用“bubble”主题来显示您的编辑器内容。这是因为它没有像“snow”主题那样顶部的工具栏。

为了使用气泡主题,您还必须 import a CSS stylesheet对于这个特定的主题如下:*

import 'react-quill/dist/quill.bubble.css'

关于javascript - 如何在没有 html 标记的情况下显示 React Quill 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952434/

相关文章:

javascript - Angular - 响应式(Reactive)表单,如何正确存储初始值,并检查表单中的更改?

javascript - Photoshop javascript SaveAsWeb 使用脚本不产生相同的结果

reactjs - 符号不是函数 react enzyme i18n 错误

angular - 羽毛笔 Angular 错误 : NullInjectorError: No provider for InjectionToken config

javascript - 从 cookie 设置 EXTjs 按钮/项目文本

javascript - 在 session 之间保存 apikey

python - 500错误: null value in column "timeline_id" violates not-null constraint

reactjs - React 测试无法解析动态需求路径

javascript - 在 Quilljs 错误 : addRange(): The given range isn't in document 中将文本动态包装在 anchor 标记中