slate.js 使用

221 阅读1分钟

slate 的基本楖念

元素中必需要有一个子节点

interface Element {

 children: Node[]

}

文本节点的文本填充为text

interface Text {

    text: string

}

在slate中有几种元素类型

段落

const paragraph = {

    type: 'paragraph',// 段落类型

    children: [...],// 段落子内容

}

引用

const quote = {

    type: 'quote',

    children: [...],

}

链接

const link = {

    type: 'link',

    url: 'https://example.com',

    children: [...]

}

slate 基本用法

此时就能通过slate调用api功能

const editor = createEditor();

editor.children = [
  {
    type: 'paragraph',
    children: [
      {
        text: 'A line of text!',
      },
    ],
  },
  {
    type: 'paragraph',
    children: [
      {
        text: 'A line of !',
      },
    ],
  },
];

const elems = Editor.nodes(editor, {
  at: [], // Path of Editor
  match: (node, path) => node
  // mode defaults to "all", so this also searches the Editor's children
})


console.log(elems, "elems");

for (const nodeEntry of elems) {
  console.log(nodeEntry,"nodeEntry")
};

由于slate还处理测试版在这里不做过多的API展示,这里只做基本概念展示,即editor 编辑处理对象,以及Editor获取选择方法具做API详查