canmove, Confirmed users
2,056
edits
Dandonkulous (talk | contribs) |
(basic selection JEP) |
||
Line 1: | Line 1: | ||
= JEP 111 - Selection = | |||
* Champion: Myk Melez | * Champion: Myk Melez <myk@mozilla.org> | ||
* Status: Accepted/In-Queue | * Status: Accepted/In-Queue | ||
* Bug Ticket: | * Bug Ticket: | ||
* Type: API | * Type: API | ||
= Introduction = | |||
Jetpacks should be able to set, get, and listen for selection events. | |||
= selection Module = | |||
The <code>selection</code> module gives extensions access to the selection. | |||
The module defines two properties, <code>text</code> and <code>html</code>, which represent plaintext and HTML versions of the selection: | |||
let selection = require("selection"); | |||
selection.text; // the text version | |||
selection.html; // the HTML version | |||
=== | Neither property is guaranteed to be defined, as there may not be a selection, and a selection may not have an HTML version. | ||
* | |||
= Getting the Selection = | |||
Extensions get the selection by accessing the module's <code>text</code> or <code>html</code> properties: | |||
let text = selection.text; | |||
let html = selection.html; | |||
= Setting the Selection = | |||
Extensions set the selection by setting the module's <code>text</code> or <code>html</code> properties: | |||
selection.text = "Hello"; | |||
selection.html = "<b>Hello</b>"; | |||
Setting the <code>text</code> property automatically updates the <code>html</code> property to be undefined. Setting the <code>html</code> property automatically updates the <code>text</code> property to be the plaintext version of the HTML to which the <code>html</code> property was set. | |||
= onSelect = | |||
The onSelect method enables registration of a callback to be notified when the user makes a selection. | |||
selection.onSelect(callback); | |||
''Arguments'' | |||
* '''callback''': Is the function to be called when a selection is made. It receives no arguments. | |||
To remove an event handler is done the normal Jetpack way: | |||
selection.onSelect.unbind( callback ); | |||
''Arguments'' | |||
* '''callback''': A pointer to the event handler to be unbound. | |||
= Example = | |||
let selection = require("selection"); | |||
selection.onSelect(function() { | |||
let html = selection.html; | |||
selection.html = ">>>" + html + "<<<"; | |||
}); | |||
= References = | |||
* [[Labs/Jetpack/JEP/12]] |