【问题标题】:how to set inistial state for html editor material ui?如何为 html 编辑器材质 ui 设置初始状态?
【发布时间】:2021-11-05 02:16:25
【问题描述】:

我有这个<h1>header</h1>,我想将它设置为 htlm 编辑器的初始状态。我使用这个库 react-mui-draft-wysiwyg https://openbase.com/js/react-mui-draft-wysiwyg/documentation#usage。有人可以帮助我吗?

【问题讨论】:

  • 如果你提到用一些文本初始化编辑器,这个link可能会帮助你,检查text: 'Hello World'
  • TypeError: draft_js__WEBPACK_IMPORTED_MODULE_12___default(...) 不是函数
  • 我应用了您发送的解决方案,但我会收到此错误
  • 也许你以错误的方式导入了一个 Draft-js 组件,答案部分的代码框可能会有所帮助
  • 是的,那是我的问题。但是将

    hello

    转换为 rawContent 格式?

标签: javascript reactjs react-hooks material-ui editor


【解决方案1】:

代码解决方案 https://codesandbox.io/s/estilos-editor-forked-gvef9?file=/src/editor.js

使用https://kelsier90.github.io/react-mui-draft-wysiwyg/examples/basic-with-content 来自文档

import React from "react";
import MUIEditor, { MUIEditorState } from "react-mui-draft-wysiwyg";
import { convertFromRaw } from "draft-js";

const Editor = () => {
  const editorConfig = {};
  const rawContent = {
    blocks: [
      {
        data: {},
        depth: 0,
        entityRanges: [],
        inlineStyleRanges: [],
        key: "2vm6d",
        text: "Header",
        type: "header-one"
      }
    ],
    entityMap: {}
  };
  const [editorState, setEditorState] = React.useState(
    MUIEditorState.createWithContent(editorConfig, convertFromRaw(rawContent))
  );
  const onChange = (newState) => {
    setEditorState(newState);
  };
  return <MUIEditor editorState={editorState} onChange={onChange} />;
};

export default Editor;

看看能不能帮到你,谢谢,

【讨论】:

  • 谢谢你。它工作。但是如何将

    hello

    转换为 rawContent ?
  • 只需替换块内的文本:[{text:"hello"}] 和

    将输入:"header-one"

  • 我是从服务器获取的。而且它并不总是那么简短
猜你喜欢
  • 2020-02-01
  • 2023-01-27
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多