atom-lupa
Version:
Navigation bar and breadcrumbs
44 lines (37 loc) • 1.44 kB
JavaScript
"use babel";
export default function domMiddleware() {
return next => action => {
if (action.type == 'goTo') {
if (action.to) {
let el;
// Which type is `action.to` of? Duck typing detection.
if (action.to.target) { // event
el = action.to.target.closest('.lupa-entity');
} else if (action.to.getAttribute) { // dom element
el = action.to;
}
if (el) {
const loc = {start:{}, end:{}};
loc.start.line = ~~el.getAttribute('data-line');
loc.start.column = ~~el.getAttribute('data-column');
loc.end.line = ~~el.getAttribute('data-line-end');
loc.end.column = ~~el.getAttribute('data-column-end');
const path = el.getAttribute('data-path');
const newAction = Object.assign({}, action, {
type: 'goTo',
loc: loc,
path: path,
});
return next(newAction);
}
if (action.to.loc) {
return next(Object.assign({}, action, {
loc: action.to.loc
}));
}
return;
}
}
next(action);
}
}