Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(occur-high): selection doesn't jump to a previous instance of the word #58

Merged
merged 6 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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