首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SlateJS将选定内容扩展到当前单词

使用SlateJS将选定内容扩展到当前单词
EN

Stack Overflow用户
提问于 2019-07-03 18:49:02
回答 1查看 727关注 0票数 0

使用Slatejs (v0.47),我如何扩展当前选择(从折叠开始)来包含当前单词?

因此(假设[]表示当前部分)

如果我从

代码语言:javascript
复制
this is some te[]xt

如何以编程方式将所选内容扩展为

代码语言:javascript
复制
this is some [text]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-03 19:17:17

最终让它工作起来了。

代码语言:javascript
复制
export const word_selection = (editor) => {
  const {value} = editor
  const {selection, startText} = value
  const {start, isExpanded} = selection

  if (isExpanded) {
    return value.fragment.text
  }
  const {text} = startText
  const {offset} = start
  let left = text.slice(0, offset).search(/\S+$/)
  if (left === -1) {
    // character before the cursor is a space, selection starts at the cursor
    left = offset
  }
  let right = text.slice(offset).search(/\s/)
  if (right < 0) {
    // character after teh cursor is a space, selection ends at the cursor
    right = text.length
  } else {
    right = right + offset
  }
  if (left === right) {
    // nothing to select
    return ''
  }
  editor.moveAnchorBackward(offset - left).moveFocusForward(right - offset)
  return value.fragment.text
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56868423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档