cycleccc / wangEditor-next

wangEditor-next —— 基于 slate.js 的富文本编辑器。wangEditor-next —— rich text editor, based on slate.js.
https://cycleccc.github.io/docs/
MIT License
117 stars 17 forks source link

Bug:当光标在`table`内时`setValue`,会出现`table`嵌套的问题 #148

Open hsuna opened 2 months ago

hsuna commented 2 months ago

bug 描述

当光标在table内时setValue,会出现table嵌套的问题

image

感觉是 #133 导致的

你预期的样子是?

正常实现table功能

系统和浏览器及版本号

wangEditor-next 版本

demo 能否复现该 bug ?

import "@wangeditor-next/editor/dist/css/style.css"; // 引入 css

import { useState, useEffect } from "react";
import { Editor, Toolbar } from "@wangeditor-next/editor-for-react";

const MyEditor = () => {
  const [value, setValue] = useState("");

  // editor 实例
  const [editor, setEditor] = useState(null); // JS 语法

  // 工具栏配置
  const toolbarConfig = {}; // JS 语法

  // 编辑器配置
  const editorConfig = {
    // JS 语法
    placeholder: "请输入内容...",
  };

  const hanleChange = (editor) => {
    setValue(editor.getHtml());
  };

  // 及时销毁 editor ,重要!
  useEffect(() => {
    setValue(
      `<table style="width: auto;table-layout: fixed;height:56"><colgroup contentEditable="false"><col width=60></col><col width=60></col><col width=60></col></colgroup><tbody><tr><th colSpan="1" rowSpan="1" width="auto" style="">1</th><th colSpan="1" rowSpan="1" width="auto" style="">2</th><th colSpan="1" rowSpan="1" width="auto" style=""></th></tr><tr><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td></tr></tbody></table><p><br></p>`
    );

    setTimeout(() => {
      setValue(
        `<table style="width: auto;table-layout: fixed;height:56"><colgroup contentEditable="false"><col width=60></col><col width=60></col><col width=60></col></colgroup><tbody><tr><th colSpan="1" rowSpan="1" width="auto" style="">2</th><th colSpan="1" rowSpan="1" width="auto" style=""></th><th colSpan="1" rowSpan="1" width="auto" style=""></th></tr><tr><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td><td colspan="1" rowspan="1" width="auto" style="border-width: 1px; border-style: solid; border-color: rgb(204, 204, 204);"></td></tr></tbody></table><p><br></p>`
      );
    }, 3000);

    return () => {
      if (editor == null) return;
      editor.destroy();
      setEditor(null);
    };
  }, [editor]);

  return (
    <div>
      <Toolbar editor={editor} defaultConfig={toolbarConfig} mode="default" />
      <Editor
        defaultConfig={editorConfig}
        value={value}
        onCreated={setEditor}
        onChange={hanleChange}
        mode="default"
      />
    </div>
  );
};

const App = () => {
  return <MyEditor />;
};

export default App;

最小成本的复现步骤

如果是失焦或者是不切光标,则setValue是能复写成功的,样式也是正常显示的。