【问题标题】:Expanding selection to the current word with SlateJS使用 SlateJS 将选择扩展到当前单词
【发布时间】:2019-07-03 10:49:01
【问题描述】:

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

所以(假设[]表示当前段)

如果我开始

this is some te[]xt

如何以编程方式将选择扩展为

this is some [text]

?

【问题讨论】:

    标签: reactjs wysiwyg slatejs


    【解决方案1】:

    终于搞定了。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2014-12-07
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      相关资源
      最近更新 更多