margox / braft-editor

美观易用的React富文本编辑器,基于draft-js开发
MIT License
4.61k stars 593 forks source link

editorState.toHtml() is empty #847

Open Prottoy2938 opened 4 years ago

Prottoy2938 commented 4 years ago

editorState.toHtml() returns empty

my code:

import React, { useState } from "react";
import BraftEditor from "braft-editor";
import { Box } from "@chakra-ui/core";
import "braft-editor/dist/index.css";
import { Props } from "./custom-layout-cf.model";

const CustomLayoutCF: React.FC<Props> = (props: Props) => {
  const [editorState, setEditorState] = useState(
    BraftEditor.createEditorState(null)
  );

  const handleChange = (newEditorState) => {
    console.log(newEditorState.toHTML()); //Issue here
    console.log(newEditorState.toText()); //this works fine

    setEditorState(newEditorState);
  };

  return (
    <Box maxWidth="96%" m="0 auto" mt={5} mb={5}>
      <BraftEditor value={editorState} onChange={handleChange} language="en" />
    </Box>
  );
};

export default CustomLayoutCF;

Note that, I'm using: "braft-editor": "^2.3.9" and "draft-js": "^0.11.7"

What to do?

usamabinnadeem-10 commented 3 years ago

I am having the exact same issue, toHtml() returns nothing.

zseed87 commented 3 years ago

I have the same problem

tonyyxliu commented 3 years ago

I got the same problem too, but I finally solved it by npm uninstall draft-js as I saw someone saying that this problem was due to draft-js version. After "npm uninstall", you may need to npm install braft-editor --save back again, just execute it and things may be all right. Hope this will help.

wuwhs commented 3 years ago

I also encountered the same problem. My solution is to reduce the version of braft-editor and lock the version, that is, change braft-editor to the original ^2.3.9 in package.json Change to 2.3.8. Analyze the reasons: The braft-editor depends on the updated version of draft-js in the package. If the version of draft-js is reduced, the version of the dependency package draft-js will also be reduced (to 0.10.5), so that it is back to the normal usability before. The version is locked so that the latest minor version dependency package is not actually downloaded.

liangxp commented 3 years ago

这么好的一个编辑器,因为无法toHtml()就这样停摆了?