简体   繁体   English

创建一个 slate.js 编辑器组件,将其状态保持在 Markdown 中

[英]Creating a slate.js editor component that keeps its state in markdown

I want to create a slate.js -based editor component that keeps it's state in markdown.我想创建一个基于slate.js的编辑器组件,将其状态保持在slate.js中。 Slate.js docs keep repeating how simple serializing and deserializing state into md should be, but they don't provide an actual way to do it. Slate.js 文档不断重复将状态序列化和反序列化到 md 应该是多么简单,但他们没有提供实际的方法来做到这一点。

I tried achieving such editor with remark-slate-transformer in a very straight-forward way , based on these two examples: remark-slate-transformer , slate :我尝试使用remark-slate-transformer以一种非常直接的方式实现这样的编辑器,基于以下两个示例: remark-slate-transformerslate

import React, { useMemo, useState } from "react";
import { createEditor } from "slate";
import { Slate, Editable, withReact } from "slate-react";
import stringify from "remark-stringify";
import unified from "unified";
import markdownParser from "remark-parse";
import { remarkToSlate, slateToRemark } from "remark-slate-transformer";
import { withHistory } from "slate-history";

function markdown2slate(markdown) {
  const processor = unified().use(markdownParser).use(remarkToSlate);
  return processor.processSync(markdown).result;
}

function slate2markdown(slate) {
  const processor = unified().use(slateToRemark).use(stringify);
  const ast = processor.runSync({ type: "root", children: slate });
  return processor.stringify(ast);
}

export const App = () => {
  const editor = useMemo(() => withHistory(withReact(createEditor())), []);
  const [value, setValue] = useState("**initialText**");

  const onChange = (newVal) => {
    setValue(slate2markdown(newVal));
  };

  const editorValue = markdown2slate(value);

  return (
    <div className="wrapper">
      <Slate editor={editor} value={editorValue} onChange={onChange}>
        <Editable />
      </Slate>
    </div>
  );
};

export default App;

sandbox here沙箱在这里

But this doesn't work very well.但这效果不佳。 I expect the initial text to appear in bold, but it doesn't.我希望初始文本以粗体显示,但事实并非如此。 The cursor keeps jumping back to position 0 on every keystroke.每次击键时,光标都会跳回到位置 0。 Also, when I delete the string ( value becomes '' ), the editor breaks.此外,当我删除字符串( value变为'' )时,编辑器会中断。

What is the correct, hassle-free way of making an editor component with state stored as markdown?制作状态存储为 Markdown 的编辑器组件的正确、轻松的方法是什么?

I'm not sure why you would absolutely want to store the editor state as Markdown, but this just cannot and will not work: you can't simply swap Slate internal state to something different than what it expects, its own object model, and expect it to work.我不确定为什么你绝对想将编辑器状态存储为 Markdown,但这不能也不会起作用:你不能简单地将 Slate 内部状态交换到与它预期不同的东西,它自己的对象模型,以及期待它的工作。

What you can do is to deserialize Markdown content into a Slate state object, feed that to the editor, let Slate do its thing while you edit and then serialize back to Markdown to do whatever you need to do with it, store it, send it, etc.可以做的是 Markdown 内容 反序列化为 Slate 状态对象,将其提供给编辑器,让 Slate 在您编辑时完成它的工作,然后将其序列化回 Markdown 以执行您需要对其进行的任何操作、存储、发送, 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM