mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-01 13:14:16 +08:00
fix(spellcheck): On Windows, right clicking a word doesn't select it
Summary: Minor tweaks so that the word is selected and spellcheck / replacement works Test Plan: No test coverage at the moment - need to think about ways to test selection Reviewers: evan, dillon Reviewed By: dillon Differential Revision: https://phab.nylas.com/D2006
This commit is contained in:
parent
be7b52cb98
commit
ee7266ffb5
2 changed files with 32 additions and 1 deletions
|
@ -2,7 +2,7 @@ _ = require 'underscore'
|
|||
React = require 'react'
|
||||
classNames = require 'classnames'
|
||||
sanitizeHtml = require 'sanitize-html'
|
||||
{Utils, QuotedHTMLParser, DraftStore} = require 'nylas-exports'
|
||||
{DOMUtils, Utils, QuotedHTMLParser, DraftStore} = require 'nylas-exports'
|
||||
FloatingToolbar = require './floating-toolbar'
|
||||
|
||||
linkUUID = 0
|
||||
|
@ -627,6 +627,13 @@ class ContenteditableComponent extends React.Component
|
|||
|
||||
selection = document.getSelection()
|
||||
range = selection.getRangeAt(0)
|
||||
|
||||
# On Windows, right-clicking a word does not select it at the OS-level.
|
||||
# We need to implement this behavior locally for the rest of the logic here.
|
||||
if range.collapsed
|
||||
DOMUtils.selectWordContainingRange(range)
|
||||
range = selection.getRangeAt(0)
|
||||
|
||||
text = range.toString()
|
||||
|
||||
remote = require('remote')
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
_ = require 'underscore'
|
||||
_s = require 'underscore.string'
|
||||
|
||||
DOMUtils =
|
||||
|
||||
|
@ -85,6 +86,29 @@ DOMUtils =
|
|||
nodes.unshift(node) while node = node.parentNode
|
||||
return nodes
|
||||
|
||||
# This method finds the bounding points of the word that the range
|
||||
# is currently within and selects that word.
|
||||
selectWordContainingRange: (range) ->
|
||||
selection = document.getSelection()
|
||||
node = selection.focusNode
|
||||
text = node.textContent
|
||||
wordStart = _s.reverse(text.substring(0, selection.focusOffset)).search(/\s/)
|
||||
if wordStart is -1
|
||||
wordStart = 0
|
||||
else
|
||||
wordStart = selection.focusOffset - wordStart
|
||||
wordEnd = text.substring(selection.focusOffset).search(/\s/)
|
||||
if wordEnd is -1
|
||||
wordEnd = text.length
|
||||
else
|
||||
wordEnd += selection.focusOffset
|
||||
|
||||
selection.removeAllRanges()
|
||||
range = new Range()
|
||||
range.setStart(node, wordStart)
|
||||
range.setEnd(node, wordEnd)
|
||||
selection.addRange(range)
|
||||
|
||||
commonAncestor: (nodes=[]) ->
|
||||
nodes = Array::slice.call(nodes)
|
||||
|
||||
|
|
Loading…
Reference in a new issue