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

Clear selection on page change #1631

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import NavButton from 'src/scripts/viewer/components/inline-nav-button'

jest.mock('src/scripts/viewer/util/nav-util', () => ({
goNext: jest.fn(),
goPrev: jest.fn()
goPrev: jest.fn(),
clearSelection: jest.fn()
}))

const getProps = (type, disabled = false, onClick = null) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,33 @@ describe('NavUtil', () => {
NavUtil.resetContext()
expect(Common.flux.Dispatcher.trigger).toHaveBeenCalledWith('nav:resetContext')
})

test('clearSelection clears the current selection', () => {
const mockRemoveAllRanges = jest.fn()
const mockEmpty = jest.fn()

// Need to make sure removeAllRanges gets called by default
// while empty gets called if removeAllRanges isn't available
window.getSelection = jest
.fn()
.mockReturnValueOnce({
removeAllRanges: mockRemoveAllRanges,
empty: null
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: mockEmpty
})
.mockReturnValueOnce({
removeAllRanges: null,
empty: null
})

NavUtil.clearSelection()
NavUtil.clearSelection()
NavUtil.clearSelection()

expect(mockRemoveAllRanges).toHaveBeenCalledTimes(1)
expect(mockEmpty).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class InlineNavButton extends React.Component {
}

onClick(event) {
// Make sure any highlighted text gets reset
// when navigating to different pages
NavUtil.clearSelection()

if (this.props.onClick) {
this.props.onClick()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ const NavUtil = {

isNavOpen(state) {
return state.open
},

clearSelection() {
const selection = window.getSelection()

if (selection.removeAllRanges) {
selection.removeAllRanges()
} else if (selection.empty) {
selection.empty()
}
}
}

Expand Down