Skip to content

Commit

Permalink
fix(occur-high): selection doesn't jump to a previous instance of the…
Browse files Browse the repository at this point in the history
… word (#58)

fix(occur-high): selection doesn't jump to a previous word

In the previous behavior, when double-clicking a word that exists multiple times in the same HTML node, the selection always jumped to the first occurrence of the word in that element.

Closes issue #57
  • Loading branch information
reyronald committed Oct 16, 2017
1 parent 7487c16 commit dbb6642
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Next Version

### Bug fixes

* **Ocurrences-Highlighter**: When double-clicking a word that exists multiple times in the same HTML node, the selection remains in the clicked word, closes [issue #57](https://github.com/refined-bitbucket/refined-bitbucket/issues/57), [pull request 58](https://github.com/refined-bitbucket/refined-bitbucket/pull/58).

# 2.6.3 (2017-10-15)

### Bug fixes
Expand All @@ -13,7 +19,7 @@

# 2.6.1 (2017-09-27)

### Bug Fixes
### Bug fixes

* **Ocurrences-Highlighter**: Now the selection is maintained when highlighting word occurrences, closes [issue #38](https://github.com/refined-bitbucket/refined-bitbucket/issues/38), [pull request 50](https://github.com/refined-bitbucket/refined-bitbucket/pull/50).

Expand Down
23 changes: 19 additions & 4 deletions src/occurrences-highlighter/occurrences-highlighter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global $, window */
/* global $, window, Text */

'use strict';

Expand Down Expand Up @@ -32,9 +32,9 @@ function highlightOnDoubleClick() {
const $this = $(this);

// <pre> for lines of code
// <p> for comments
// <div class="comment-content"> for comments
// <span class="description"> for tasks
const code = $($this.closest('.diff-content-container')[0]).find('pre, p, span.description');
const code = $($this.closest('.diff-content-container')[0]).find('pre, div.comment-content, span.description');
const selection = getSelectedText();
const selectionIsInTextArea = selection.anchorNode.getElementsByTagName && selection.anchorNode.getElementsByTagName('textarea').length;
const text = selection.toString();
Expand All @@ -44,7 +44,8 @@ function highlightOnDoubleClick() {
if (selectionIsInTextArea) {
highlightOcurrences(code, text);
} else {
const span = wrapInSpan(selection.anchorNode, SELECTION_TEMPORARY_ID);
const selectedNode = getSelectionAsNode(selection);
const span = wrapInSpan(selectedNode, SELECTION_TEMPORARY_ID);
highlightOcurrences(code, text);
const children = unwrapChildren(span);
const highlightedNode = getHighlightedNode(children);
Expand Down Expand Up @@ -84,6 +85,20 @@ function highlightOcurrences(code, text) {
});
}

/**
* Gets the selection as a HTML element node.
* @param {Selection} selection
* @returns {HTMLElement}
*/
function getSelectionAsNode(selection) {
if (selection.anchorNode instanceof Text) {
const word = selection.anchorNode.splitText(selection.anchorOffset);
word.splitText(selection.focusOffset);
return word;
}
return selection.anchorNode;
}

/**
* Wraps the given DOM Element in a span with the given ID
* @param {HTMLElement} el
Expand Down

0 comments on commit dbb6642

Please sign in to comment.