当前位置: 首页 > 工具软件 > Slate.js > 使用案例 >

slate.js的使用记录

许振海
2023-12-01

示例 获取选中的节点:

import { SlateEditor, SlateElement, SlateNode } from '@wangeditor/editor'

const nodeEntries = SlateEditor.nodes(editor, {
    match: (node: SlateNode) => {  // TS syntax
    // match: (node) => {          // JS syntax
        if (SlateElement.isElement(node)) {
            if (node.type === 'paragraph') {
                return true // 匹配 paragraph
            }
        }
        return false
    },
    universal: true,
})

if (nodeEntries == null) {
    console.log('当前未选中的 paragraph')
} else {
    for (let nodeEntry of nodeEntries) {
        const [node, path] = nodeEntry
        console.log('选中了 paragraph 节点', node)
        console.log('节点 path 是', path)
    }
}

这个示例是wangedit的 编辑器 API | wangEditor

毕竟中国人写的文档 会比较好懂一些 看懂了这个api  就知道match 的基本用法了

于是我们通过api知道 有

getFragment

这个方法能拿到选中的json 知道了这两个的用法  slate基本就能写自己想要的功能了

建议用react 因为支持jsx 好处是 什么dom都能往里面写。功能就会十分强大 

最后 建议学好ts。这样方便你看源码的时候 查api特别的方便

 类似资料: