双击选择连字符的单词

问题描述 投票:0回答:2

更新:根据下面的建议,我具体想做的是:如果我在“b”到“n”的任意位置的“blue-green”一词上双击鼠标,我想要整个单词“蓝绿”要突出显示。如何才能做到这一点?目前,根据您单击的位置,它将“blue-green”视为三个单独的字符串:如果您在“blue”的“b”和“e”之间双击,它将仅突出显示“blue”而不是“-green”。 ”如果双击连字符,它会单独突出显示连字符。如果您在“green”的“g”和“n”之间双击,它只会突出显示“green”而不是“blue-”。

原始:当我双击连字符的单词或字符集(例如“123-abc”或“blue-green”等)时,只有我双击的单词部分会突出显示。我希望突出显示整个单词。

我使用的是 Windows 7 专业版。如果需要在每个应用程序的基础上完成,我最感兴趣的是针对 Google Chrome 修复它,但任何与 Windows 兼容的网络浏览器都可以。

windows google-chrome windows-7
2个回答
3
投票

老问题,但我碰巧一直在研究同样的问题。这是我的解决方案,根据 @David 的评论进行了更新,因此它现在也可以在 Windows 上正常工作。

它不仅处理

<input>
元素中的选择,还修复了 Firefox 中撇号的错误,并处理一系列可用于表示连字符或撇号的字形。

您可以在此处找到具有更全面的功能演示的存储库。

"use strict"

// Tweak to make a double-click select words with hyphens or
// apostrophes.
//
// As of 2023-04-12, None of the major browsers selects whole
// words with hyphens, like "ad-lib". Only the text before or
// after the hyphen, or the hyphen on its own, will be selected.
// This tweak fixes the hypen issue.
//
// Note: Firefox (at least until version 111.0.1) also doesn't
// automatically select whole words with apostrophes like the word
// "doesn't".
//
// In Safari (at least until version 16.3), a double-click that
// lands precisely on an apostrophe will select only the
// apostrophe. However, a double-click on any *letter* in a word
// that contains an apostrophe will select the entire word,
// including the apostrophe.
//
// This tweak also treats these issues.

// On Windows, a double-click on a word will also select the space
// after the word, if there is one. On MacOS and Ubuntu, only the
// word itself is selected. This tweak respects these
// platform-specific differences.

// In the comments below, I'll use the word "join" to mean any of
// the following hyphen and apostrophe characters:
//
//  * - (hyphen: &#8208;)
//  * ‑ (non‑breaking hypen: &8209;)
//  * &shy; (soft hyphen, which only appears at a line break)
//  * ' (apostrophe: &#39;)
//  * ’ (right single quotation mark: &#8217;).
//
// NOTE 1: It is not trivial to distinguish between a final
// apostrophe, which is an integral part of a word, that is used
// to indicate possession)...
//
//   She said, "Those books are Jodi's, but these are my kids'".
//
// ... from a closing single quote:
//
//   He said, "She said, 'Meet Jo and Di. These are my kids'".
//
// For simplicity, this script ignores both cases. As of 2023-04-12,
// all major browsers behave in exactly the same way.
//
// NOTE 2: Two hyphens can be used to indicate a dash—a character
// which indicates a secondary thought–and some writers leave no
// spaces around a dash. However it is never used to make compound
// words. "Two consecutive hypens should be ignored--at least I
// think they should."




;(function selectWholeWordsWithHyphens(){
  var selection = window.getSelection()
  // Regex designed to detect if the selection is just a series of
  // join characters.
  var ignoreRegex = /^[\u00AD‑'’-]{2,}$/

  // Regex designed to find a word+join before the selected word.
  // Examples: ad-|lib|  seven-o'|clock|
  // It finds the last chunk with no non-word characters (except for
  // joins) before the first selected character.
  var startRegex = /(\w+[\u00AD‑'’-]?)+$/g

  // Regex designed to find a join character after the selected word.
  // Examples: |ad|-lib  |seven|-o'clock
  var endRegex = /^([\u00AD‑'’-]?\w+)+/

  // Edge case: check if the selection contains no word characters
  // or - or '. If so, then don't do anything to extend it.
  var edgeRegex = /\w|-|‑|'|’|\u00AD/

  document.body.ondblclick = selectHyphenatedWords

  function selectHyphenatedWords(event) {
var target = event.target
var isInput = target.tagName === "INPUT"

// In browsers on Windows, a double-click on a word will
// select the word _and_ a space character that immediately
// follows it. We will need to adjust for this.
var lastSelectedCharIsSpace = 0

if (isInput) {
  var start = target.selectionStart
  var end = target.selectionEnd
  var string = target.value
  lastSelectedCharIsSpace = (
    string.substring(end-1, end) === " "
  )
  end -= lastSelectedCharIsSpace // true → 1, false → 0

} else if (!selection.rangeCount) {
  return

} else {
  var range = selection.getRangeAt(0)
  // If the selection is at the boundary of a tag – for example:
  // <p>The selection word is one of <em>these-words</em></p> —
  // then range.startContainer and range.endContainer will be
  // different.
  var container = range.endContainer
  var end = range.endOffset
  lastSelectedCharIsSpace = (
    container.textContent.substring(end-1, end) === " "
  )
  end -= lastSelectedCharIsSpace // true → 1, false → 0
  if (!end ) {
    // The selection extends to the end of the startContainer
    // and ends at char index 0 in the endContainer. Use the
    // startContainer instead
    container = range.startContainer
    end = container.length
  }
  var string = container.textContent
  var start = (container === range.startContainer)
    ? range.startOffset
    : 0 // The selection starts at the very end of the
        // startContainer, or at char index 0 of the
        // endContainer
}

var selectionUpdated = false
var chunk = string.substring(start, end)
var ignore = ignoreRegex.test(chunk)
          || chunk.search(edgeRegex) < 0

if (ignore) {
  // The selection contains neither word nor join characters
  // or is nothing but a series of join characters
  return
}

extendSelectionBackBeforeHypen(string, start)
extendSelectionForwardAfterHyphen(string, end)

if (selectionUpdated) {
  if (isInput) {
    end += lastSelectedCharIsSpace
    target.setSelectionRange(start, end)
  } else {
    selection.removeAllRanges()
    selection.addRange(range)
  }
}

function extendSelectionBackBeforeHypen(string, offset) {
  var lastIndex = 0
  var result
    , index
  string = string.substring(0, offset)

  while (result = startRegex.exec(string)) {
    index = result.index
    lastIndex = startRegex.lastIndex
  }

  if (lastIndex === offset) {
    if (isInput) {
      start = index
    } else {
      range.setStart(container, index)
    }
    selectionUpdated = true
  }
}

function extendSelectionForwardAfterHyphen(string, offset) {
  if (!offset) {
    return
  }

  string = string.substring(offset)
  var result = endRegex.exec(string)

  if (result) {
    end = offset + result[0].length
    if (!isInput) {
      range.setEnd(container, end)
    }
    selectionUpdated = true
  }
}
  }
})()
small {
  color: grey;
 }
  <p>Here is the nine-o'clock news.
  <br><small>with standard hyphen (&amp;#8208;) and neutral vertical apostrophe (&amp;#39;)</small></p>
  <p>Here is the nine‑o’clock news.
  <br><small>with non-breaking hyphen (&amp;8209;) and right single quotation mark (&amp;#8217;)</small></p>
  <p>A word containing soft hyphens — un&shy;predict&shy;able.
  <br><small>(un&amp;shy;predict&amp;shy;able)</small>
  <br><small>Soft hyphens are invisible unless they appear at a line break.</small>
  </p>
  <p>A double hyphen--which is rare--can be used for a dash, but does not create a compound word.</p>
  <hr>
  <input type="text" name="" id="text" value="The ninety-nine-o'clock news">
  <br>
  <input type="text" name="" id="text" value="The ninety‑nine‑o’clock news">


-3
投票

这是所有程序都会这样做的标准,因为它们都运行操作系统的打字配置/程序。要修复它,您需要在 System32 中执行某些操作。我不知道你需要做什么,但我怀疑这是你的问题。您可能应该更详细地了解您想要什么。

© www.soinside.com 2019 - 2024. All rights reserved.