joseph / Monocle

A silky, tactile browser-based ebook JavaScript library.
http://monocle.inventivelabs.com.au
MIT License
743 stars 200 forks source link

How to use Gala's 'onContact'? #243

Closed joloco closed 10 years ago

joloco commented 10 years ago

I'm trying to trigger my own context menu when the user selects some text in the reader.

I'm currently using the 'monocle:selection' event but that triggers multiple times while the user is selecting text, whereas I want it to trigger only when the user has finished selecting.

I think that Gala's onContact function might be the right way to do this, but I can't find any documentation, nor can I figure out how to use it. (I have been trying though!)

Can anyone point me in the right direction?

Thanks in advance!

joseph commented 10 years ago

There's a bit of documentation here:

https://github.com/joseph/Monocle/blob/master/src/compat/gala.js#L191-L206

Essentially this:

Gala.onContact(document.body, {
  start: function (evt) { ... },
  end: function (evt) { ... }
});

is the same as:

document.body.addEventListener('mousedown', function (evt) { ... });
document.body.addEventListener('mouseup', function (evt) { ... });

But done in a way that generalizes mouse/touch/pointer events.

The monocle:selection fires every time the selection changes because actually, a selection can change at any time, and it can change via keyboard shortcuts as well as pointer movement, so onContact would be an incomplete solution. You may not mind that, but you might also try to refactor your code or UI to accomodate a possibly dynamic selection. In some ways that might be easier too.

joloco commented 10 years ago

Thanks Joseph, much appreciated!