Skip to content

Commit

Permalink
code(cursor-search): introduce switch context to mimic browser behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
peyerluk authored and gabrielhase committed Jun 28, 2021
1 parent 82c54fc commit 8e477fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,27 @@ export default class Dispatcher {
const cursor = this.selectionWatcher.getFreshSelection()
if (!cursor || cursor.isSelection) return

// store position
if (!this.switchContext) {
this.switchContext = {
positionX: cursor.getBoundingClientRect().left,
events: ['cursor']
}
} else {
this.switchContext.events = ['cursor']
}

if (direction === 'up' && cursor.isAtFirstLine()) {
event.preventDefault()
event.stopPropagation()
this.switchContext.events = ['switch', 'blur', 'focus', 'cursor']
this.notify('switch', element, direction, cursor)
}

if (direction === 'down' && cursor.isAtLastLine()) {
event.preventDefault()
event.stopPropagation()
this.switchContext.events = ['switch', 'blur', 'focus', 'cursor']
this.notify('switch', element, direction, cursor)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/eventable.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ function getEventableModule (notifyContext) {
args.splice(0, 2)
}

if (this.switchContext) {
const nextEvent = this.switchContext.events.shift()
if (event !== nextEvent) this.switchContext = undefined
}

const eventListeners = listeners[event]
if (!eventListeners) return

Expand Down

0 comments on commit 8e477fa

Please sign in to comment.