h-yabi2 / react-quill

react-quill × Next.js
0 stars 0 forks source link

delta形式の編集後データを state に保存する方法 #8

Open h-yabi2 opened 4 months ago

h-yabi2 commented 4 months ago
const deltaData = {
  ops: [
    { insert: "Hello " },
    { insert: "World!", attributes: { bold: true } },
    { insert: "\n" },
  ],
};
const [data, setData] = useState(deltaData);

~

const handleOnChange = (content: string) => {
  console.log(content);
  // html to delta
  const delta = quillRef.current?.editor?.clipboard.convert(content);
  console.log(delta);

  // setData データ時に { insert: "\n" } を末尾に追加
  // if (delta && delta.ops) {
  //   const lastData = delta.ops[delta.ops.length - 1] ?? { insert: "" };
  //   if (lastData.insert !== "\n") {
  //     delta.ops.push({ insert: "\n" });
  //   }
  // }

  // TODO: 型エラーが出るので、修正が必要
  setData(delta as any);
};