w3c / selection-api

Selection API
http://w3c.github.io/selection-api/
Other
47 stars 30 forks source link

Pass the original events or add events types in selectionchange #65

Open caub opened 8 years ago

caub commented 8 years ago

selectionchange is really useful, it would be even better if the event parameter of the callback could say more about what triggered the selection change, either passing a MouseEvent, a KeyEvent etc.. or giving a type property to the event

document.addEventListener('selectionchange', e=>{
  if (e instanceof KeyEvent) console.log(e.keyCode) // very useful if I want to act on undo/redo..
  else if (e instanceof MouseEvent) console.log(e.target)// e.target is not really needed
             // since I can use the current Range to know in what elements the selection is
  // ...
});

the idea is to have something like that http://jsfiddle.net/crl/rk6f74tw/12/

caub commented 8 years ago

http://jsfiddle.net/crl/rk6f74tw/14/ for me this is actually better than the current selectionchange event

caub commented 8 years ago

update: I've aslso been in a situation where I need to know a 'target' for the selection change, or other times I needed an event type that triggered that change in selection.

Since selection change is always triggered by another event (I think), wouldn't it be better to include more information in this event?

I've done things like http://jsfiddle.net/rk6f74tw/34/ (check console and select content either in the blue menu or elsewhere) as also a polyfill, but it's of course more heavy

When using user-select:none CSS property, it doesn't work the same on all browsers, I posted bugs on edge and chrome, only Firefox (nightly, because they forgot to enable selectionchange on current releases) doesn't send selectionchange event on element with user-select:none, which makes sense to me.