如何使用Next.js实现富文本编辑器,例如Quill或Draft.js?

时间:2019-03-25 15:01:46

标签: javascript reactjs next.js quill draftjs

我有一个大型应用程序,该应用程序是用Next.js构建的,用于SEO和性能目的,并且该应用程序的超级交互部分需要一个文本编辑器(例如Quill.js或Draft.js)来存储数据。使用Socket.io在两个用户之间同步。

问题是我无法让文本编辑器与Next.js一起使用。

当我导入Quill时,它说“未定义文档”,因为服务器上没有文档。使用Draft.js,它根本不呈现任何内容,但是没有错误。

这是我的Draft.js代码:

import { EditorState, convertToRaw, convertFromRaw } from 'draft-js'


class TextEditor extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: EditorState.createWithContent(convertFromRaw(props.contentState))
    }
  }
  static async getInitialProps ({ query, req }) {
    return { contentState: convertToRaw(EditorState.createEmpty().getCurrentContent()) }
  }
  render() {
    console.log('init',this.props.editorState);
    return (
      <div>
        <h1>Test Editor</h1>
        <Editor
          editorState={ this.props.editorState }
        />
      </div>
    );
  }
}

有帮助吗?

3 个答案:

答案 0 :(得分:3)

我认为这个nextjs-with-draftjs示例可以帮助您将Draft.js集成到Next.js应用程序中。

答案 1 :(得分:0)

这是我使用 NextJS 和 React Hook Form 的简单组件

import React, { useEffect } from "react";
import { Editor, EditorState, ContentState, convertFromRaw } from "draft-js";
import "draft-js/dist/Draft.css";

export { EditorState, ContentState };

interface PropTypes {
  name?: string;
  value?: string;
  onChange?: Function;
}

// Trick to fix issue with NextJS https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/universal/editor.js
const emptyContentState = convertFromRaw({
  entityMap: {},
  blocks: [
    {
      text: "",
      key: "foo",
      type: "unstyled",
      entityRanges: [],
    },
  ],
});

function RichTextEditor({ name, value, onChange }: PropTypes) {
  const [editorState, setEditorState] = React.useState(
    EditorState.createWithContent(emptyContentState)
  );

  useEffect(() => {
    setEditorState(
      EditorState.createWithContent(ContentState.createFromText(value))
    );
  }, []);

  return (
    <Editor
      editorKey={name}
      editorState={editorState}
      onChange={(editorState) => {
        setEditorState(editorState);

        onChange(editorState.getCurrentContent().getPlainText());
      }}
      userSelect="none"
      contentEditable={false}
    />
  );
}

export default RichTextEditor;

<Controller
 as={RichTextEditor}
 name="description"
 control={control}
 defaultValue=""
/>

答案 2 :(得分:0)

使用太阳编辑器。这个非常灵活并且与nextjs兼容。 https://www.npmjs.com/package/suneditor-react npm i suneditor suneditor-react

import dynamic from "next/dynamic";
import 'suneditor/dist/css/suneditor.min.css'; // Import Sun Editor's CSS File to the _app.js

const SunEditor = dynamic(() => import("suneditor-react"), { //besure to import dynamically
  ssr: false,
});

const MyComponent = props => {
  return (
    <div>
      <p> My Other Contents </p>
      <SunEditor />
    </div>
  );
};
export default MyComponent;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>