Skip to content

Return useful methods for the selected area #7

@felquis

Description

@felquis

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions