账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    slatejs 里如何通过 api 选择文本范围并添加 mark
    42
    0
    比如有一段文本:segmentfault,如何能直接通过 api 为 index 从 2 至 3 的文本(也就是 gm)加粗,不是通过光标选中 gm,而是直接通过数据模型里的 position,该如何实现?查了一下,slate 文档里的 addMark api 不适合此场景:addMark(key: string, value: any) => voidAdd a custom property to the leaf text nodes in the current selection. If the selection is currently collapsed, the marks will be added to the editor.marks property instead, and applied when text is inserted next.谢谢谢谢!
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 笑的歇斯里底ヘ 普通会员 1楼

      在SlateJS中,你可以通过API选择文本范围并添加标记。以下是一个简单的示例:

      首先,你需要安装SlateJS的API库。你可以使用npm来安装:

      bash npm install slate-js

      然后,你可以使用以下代码来选择文本范围并添加标记:

      ```javascript const slate = require('slate');

      // 创建一个文档 const doc = slate.create();

      // 选择一个文本范围 doc.append({ text: 'Hello, world!', range: [ { start: 0, end: 5 }, { start: 7, end: 12 }, ] });

      // 添加标记 doc.append({ text: 'Hello, world!', mark: 'red', });

      // 打印文档 doc.render(); ```

      在这个例子中,我们首先创建了一个文档。然后,我们使用append方法选择了一个文本范围,这个范围是从索引0到索引5的字符串。然后,我们使用append方法添加了一个标记,这个标记的颜色是红色。最后,我们使用render方法打印了文档。

      注意:你需要确保你的文本范围和标记是有效的。例如,文本范围必须是数字,而标记必须是颜色字符串。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部