diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-03-18 13:03:10 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-03-18 13:03:10 -0700 |
commit | 81ded4fb78cf32a3d85882708e2f651608100815 (patch) | |
tree | 26ded510365650ba6516dfa03228a424984e0522 /content.js | |
parent | 3fd7ab16ac6f3c97822951eaddd7d9a090cea48a (diff) |
WIP
Diffstat (limited to 'content.js')
-rw-r--r-- | content.js | 37 |
1 files changed, 16 insertions, 21 deletions
@@ -20,32 +20,27 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -function insertBreakAtPoint(e) { - var range; - var textNode; - var offset; +function getText(e, length) { + let range = document.caretRangeFromPoint(e.clientX, e.clientY); - if (document.caretPositionFromPoint) { - range = document.caretPositionFromPoint(e.clientX, e.clientY); - textNode = range.offsetNode; - offset = range.offset; - } else if (document.caretRangeFromPoint) { - range = document.caretRangeFromPoint(e.clientX, e.clientY); - textNode = range.startContainer; - offset = range.startOffset; + let node = range.startContainer; + if (node.nodeType !== 3 /* TEXT_NODE */) { + return ''; } - // only split TEXT_NODEs - if (textNode.nodeType == 3) { - var replacement = textNode.splitText(offset); - var br = document.createElement('br'); - textNode.parentNode.insertBefore(br, replacement); - } + let scan = Math.min(- range.startOffset, length); + console.log(scan); + range.setEnd(node, range.startOffset + scan); + + return range.toString(); } -function onMouseDown(e) { - insertBreakAtPoint(e); +function onMouseMove(e) { + const text = getText(e); + if (text !== '') { + console.log(text); + } } -window.addEventListener('mousedown', onMouseDown, false); +window.addEventListener('mousemove', onMouseMove, false); |