EvandroLG / selecting

:fishing_pole_and_fish: A library that allows you to access the text selected by the user
92 stars 12 forks source link

Return useful methods for the selected area #7

Open felquis opened 9 years ago

felquis commented 9 years ago

What about provide an API wrapper for the selection object? For example, provide the size of the selected area, and other things that is possible to do using this API getSelection, Selection and Range

Also, this fokus plugin use this getSelection API, we can learn something from his code and learn more about this getSelection object, and turn selecting a lib to help people work with text selection in some way.

var selection = selecting('.selector-to-watch'); // otherwise watch in all document/window

// Fired when the user start selecting something
selection.on('selectionStart', function (text, selectionObject) {

});

// Fired when the selected text is changed
selection.on('selectionChange', function (newText, oldText, selectionObject) {

});

// Fired when the user remove the selection of text, return the selected text before the user remove the text selection
selection.on('selectionEnd', function (oldText, selectionObject) {

});

/*
  We can do something to the developer just play around with getSelection object if available
*/ 
selection.hasSelection(); // return true or false

selection.runif(function (api) {
  // `api` is the getSelection objet + some useful shortcut functions included by selecting
  // This function is only executed if there's some active text selection 
});

getSelection is supported by most of browsers, including smartTVs and consoles, just IE7,8, for these we can turn selection in a kind of basic polyfill to normalize something (but I don't think that is useful spend much time wondering about old browsers)

EvandroLG commented 9 years ago

I think that this idea is great. And we can open a branch to work in these new implementations ;)