simplify Jump next/previous

pull/1/head
yann300 7 years ago
parent 2f087c9893
commit 6e8e7af961
  1. 61
      src/app/editor/contextView.js

@ -59,6 +59,7 @@ class ContextView {
this._nodes this._nodes
this._current this._current
this.sourceMappingDecoder = new SourceMappingDecoder() this.sourceMappingDecoder = new SourceMappingDecoder()
this.previousElement = null
event.contextualListener.register('contextChanged', nodes => { event.contextualListener.register('contextChanged', nodes => {
this._nodes = nodes this._nodes = nodes
this.update() this.update()
@ -98,7 +99,7 @@ class ContextView {
} }
_renderTarget () { _renderTarget () {
this._current = null var previous = this._current
if (this._nodes && this._nodes.length) { if (this._nodes && this._nodes.length) {
var last = this._nodes[this._nodes.length - 1] var last = this._nodes[this._nodes.length - 1]
if (isDefinition(last)) { if (isDefinition(last)) {
@ -107,64 +108,50 @@ class ContextView {
var target = this._api.contextualListener.declarationOf(last) var target = this._api.contextualListener.declarationOf(last)
if (target) { if (target) {
this._current = target this._current = target
} else {
this._current = last
}
} }
} }
if (!this._current || !previous || previous.id !== this._current.id) {
this.previousElement = this._render(this._current, last)
} }
return this._render(this._current) return this.previousElement
} }
_render (node) { _render (node, nodeAtCursorPosition) {
if (!node) return yo`<div></div>` if (!node) return yo`<div></div>`
var self = this var self = this
var references = this._api.contextualListener.referencesOf(node) var references = this._api.contextualListener.referencesOf(node)
var type = node.attributes.type ? node.attributes.type : node.name var type = node.attributes.type ? node.attributes.type : node.name
references = `${references ? references.length : '0'} reference(s)` references = `${references ? references.length : '0'} reference(s)`
function jumpTo () { var ref = 0
if (node && node.src) { var nodes = self._api.contextualListener.getActiveHighlights()
var position = self.sourceMappingDecoder.decode(node.src) for (var k in nodes) {
if (position) { if (nodeAtCursorPosition.id === nodes[k].nodeId) {
self._api.jumpTo(position) ref = k
} break
} }
} }
// JUMP BETWEEN REFERENCES // JUMP BETWEEN REFERENCES
function jump (e) { function jump (e) {
var nodes = self._api.contextualListener.getActiveHighlights() e.target.dataset.action === 'next' ? ref++ : ref--
var searchTerm = node.attributes.name if (ref < 0) ref = nodes.length - 1
var currentAction = e.target.dataset.action if (ref >= nodes.length) ref = 0
self._api.jumpTo(nodes[ref].position)
if (currentAction === 'next') {
next(searchTerm, nodes, currentAction)
} else if (currentAction === 'previous') {
previous(searchTerm, nodes, currentAction)
}
self.refName = searchTerm
self.action = currentAction
} }
function next (searchTerm, nodes, currentAction) { function jumpTo () {
if (searchTerm !== self.refName) self.ref = 0 if (node && node.src) {
if (currentAction !== self.action) self.ref = (nodes.length - 1) - self.ref // adapting self.ref to switching between previous() and next() var position = self.sourceMappingDecoder.decode(node.src)
self.ref = (self.ref + 1) % nodes.length if (position) {
self._api.jumpTo(getPos(nodes, self.ref)) self._api.jumpTo(position)
} }
function previous (searchTerm, nodes, currentAction) {
if (searchTerm !== self.refName) self.ref = nodes.length - 1
if (currentAction !== self.action) self.ref = (nodes.length - 1) - self.ref // adapting self.ref to switching between previous() and next()
self.ref = (self.ref + 1) % nodes.length
self._api.jumpTo(getPos(nodes, nodes.length - 1 - self.ref))
} }
function getPos (nodes, k) {
var i = (k + (nodes.length - 1)) % nodes.length // to get to nodes[0] position, jumpTo function needs nodes[node.length-1], for nodes[1], jumpTo(nodes[0].position) etc.
return nodes[i].position
} }
return yo`<div class=${css.line}> return yo`<div class=${css.line}>
<div title=${type} class=${css.type}>${type}</div> <div title=${type} class=${css.type}>${type}</div>
<div title=${node.attributes.name} class=${css.name}>${node.attributes.name}</div> <div title=${node.attributes.name} class=${css.name}>${node.attributes.name}</div>

Loading…
Cancel
Save