【问题标题】:Draft js saving and rendering or displaying content草稿js保存渲染或显示内容
【发布时间】:2017-01-13 12:16:26
【问题描述】:

问题是:如何将draft-js 内容保存为html,然后在页面上呈现内容(此时为html 字符串)。

我想我会分享我学到的东西。

请在解决方案中找到一种使用 Draft.js 保存和呈现内容的方法。

还请发布您自己的解决方案,以便我们大家学习。

【问题讨论】:

    标签: javascript reactjs meteor blogs draftjs


    【解决方案1】:

    在互联网上无休止地搜索和搜索如何将 Draft.js 用于我们正在构建的博客之后,我想我会分享我所学到的。 Draft.js 非常棒,但由于没有官方的渲染解决方案,因此保存后如何渲染数据还不是很清楚。

    这是一个关于如何做到这一点的抽象演示。

    插件用户是draft-jsdraft-convertreact-render-html。使用的数据库是mongo

    创建帖子:

    import React, {Component} from 'react';
    import {
        Block,
        Editor,
        createEditorState
    } from 'medium-draft';
    import { convertToHTML } from 'draft-convert';
    
    class PostEditor extends Component {
        constructor(props) {
            super(props);
    
            this.state = {
                stateEditor: createEditorState()
            }
    
            this.onChange = (editorState) => {
                this.setState({ editorState });
            };
    
            this.publishPost = this.publishPost.bind(this);
        }
        publishPost() {
            // turn the state to html
            const html = convertToHTML(this.state.editorState.getCurrentContent());
    
            const post = {
                content: html,
                createdAt: new Date()
                // more stuff...
            }
    
            // post the data to you mongo storage.
        }
        render() {
            // get the editorState from the component State
            const { editorState } = this.state;
            return (
                <div>
                    <Editor
                        ref="editor"
                        editorState={editorState}
                        placeholder="Write your blog post..."
                        onChange={this.onChange} />
                    <div>
                        <button onClick={this.publishPost} type="button">Submit</button>
                    </div>
                </div>
            )
        }
    }
    

    渲染帖子:

    import React, { Component } from 'react';
    import renderHTML from 'react-render-html';
    
    class PostArticle extends Component {
        constructor(props) {
            super(props);
    
            this.state = {
                text: null
            }
        }
        componentWillMount() {
            // get the data from the database
            const post = getMyBlogPostFunction(); // replace getMyBlogPostFunction with own logic to fetch data
            this.setState({ text: post.content })
        }
        render() {
            return (
                <div className='article-container'>
                    {renderHTML(this.state.text)}
                </div>
            )
        }
    }
    

    注意:Html 脚本标签已被转义。

    虽然上述解决方案可能并不适合每个用例,但我希望有人能发现它对基本用法很有用。

    免责声明:对于因使用上述代码而造成的任何损害或损害,我概不负责。

    【讨论】:

    【解决方案2】:

    the documentation 中有一个很好的例子来演示这个过程。这是link to the source code on Github

    基本上你正在寻找的代码 sn-p 是这样的:

    const sampleMarkup =
      '<b>Bold text</b>, <i>Italic text</i><br/ ><br />' +
      '<a href="http://www.facebook.com">Example link</a>';
    
    const blocksFromHTML = convertFromHTML(sampleMarkup);
    const state = ContentState.createFromBlockArray(blocksFromHTML);
    
    this.state = {
      editorState: EditorState.createWithContent(state),
    };
    

    实用函数被称为convertFromHTML

    【讨论】:

    • 文档链接已损坏。
    • 谢谢@CullenBond——我已经更新了new documentation site的链接。感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多