Skip to content

Commit

Permalink
wrap key handling to allow plugins to stop key evts
Browse files Browse the repository at this point in the history
similar to the way web browsers allow event propagation to be
stopped, this now allows plguins to return true to signal that
the plguin has consumed the key event and it should not be sent to
any subsequent key event handlers.
  • Loading branch information
maks committed Aug 27, 2012
1 parent 2647274 commit 118db42
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = Hipster
function Hipster (rc, doc) {

//internal representation of our text file
doc = doc || new Document()
doc = doc || new Document()

render = render(doc, rc)
var input
Expand All @@ -33,8 +33,6 @@ function Hipster (rc, doc) {
}
}

key(input)

if(rc.record) {
process.stdin.pipe(es.stringify()).pipe(fs.createWriteStream(rc.record))
}
Expand Down Expand Up @@ -67,7 +65,7 @@ function Hipster (rc, doc) {
init: function () {
var self = this
this.plugins.forEach(function (plug) {
plug.call(self, doc, keys, render)
plug.call(self, doc, keys, render)
})
render.redraw()
return this
Expand Down
18 changes: 13 additions & 5 deletions lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ var listeners = []
keypress(process.stdin)

process.stdin.on('keypress', function (ch, key) {
listeners.forEach(function(l) {
if (l(ch, key) !== true) return
});
});
listeners.some(function(l) {
return l(ch, key)
})
})

module.exports.on = function(foo, listener) {
listeners.push(listener);
listeners.push(listener)
}

module.exports.listeners = function(evt) {
return listeners
}

module.exports.removeAllListeners = function(evt) {
listeners = []
}
5 changes: 0 additions & 5 deletions plugins/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

module.exports = function (doc, keys, cursor) {
keys.on('keypress', function (ch, key) {

//Refactor this when there is another command that needs to veto something.
//vvv TOTALLY HIDEOUS WAY TO PREVENT OTHER COMMANDS TRIGGERING.
if (key.name == 'VETOED')
return

if(key.shift && key.name.length === 1)
key.name = key.name.toUpperCase()
Expand Down
5 changes: 1 addition & 4 deletions plugins/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ module.exports = function (doc, keys) {

// doc.move()//something is putting the cursor in the wrong place.
}

//Refactor this when there is another command that needs to veto something.
//vvv TOTALLY HIDEOUS WAY TO PREVENT OTHER COMMANDS TRIGGERING.
key.name = 'VETOED'
return true
})
}

5 changes: 1 addition & 4 deletions plugins/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ module.exports = function (doc, keys, render) {
console.error('term is:'+term)
render.updateFooter('find:'+term)
}
//Refactor this when there is another command that needs to veto something.
//vvv TOTALLY HIDEOUS WAY TO PREVENT OTHER COMMANDS TRIGGERING.
key.name = 'VETOED'
return
return true
} else {
console.error('not acc');
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = function (doc, keys, cursor) {
//add our first listener, then readd the listeners
//then add our last listener.

//~ var listeners = keys.listeners('keypress')
//~ keys.removeAllListeners('keypress')
var listeners = keys.listeners('keypress')
keys.removeAllListeners('keypress')

function startSelection (ch, key) {
//only start selection if it's a movement key.
Expand All @@ -41,11 +41,11 @@ module.exports = function (doc, keys, cursor) {

keys.on('keypress', startSelection)

//~ listeners.forEach(function (listener) {
//~ keys.on('keypress', listener)
//~ })
listeners.forEach(function (listener) {
keys.on('keypress', listener)
})

//~ keys.on('keypress', endSelection)
keys.on('keypress', endSelection)


this.renderers.push(function (q, x, y) {
Expand Down

0 comments on commit 118db42

Please sign in to comment.