Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity Source Tip Author Tutorial

1,773 bytes removed, 05:24, 22 June 2009
Replace: Commands With Multiple Arguments
== Replace: Commands With Multiple Arguments ==
Commands, like the translate command, can take multiple (and possibly optional) arguments. Ubiquity takes care of the parsing—you don't have to worry about what order the user types them in, you'll just get passed a dictionary with the appropriate entries.
 
To illustrate that, let's make a simple regular-expression-based "replace" command. It will take three arguments: the thing to replace, the replacement, and the scope-text to do the replacing in. Here's the command:
 
<pre>
CmdUtils.CreateCommand({
name: "replace",
takes: {"what": noun_arb_text},
modifiers: {with: noun_arb_text, in: noun_arb_text},
preview: function( pblock, what, mods ) {
// args contains .with and .in, both of which are input objects.
var msg = 'Replaces "${whatText}" with ${withText} in ${inText}.';
var subs = {whatText: what.text, withText: mods.with.text, inText: mods.in.text};
pblock.innerHTML = CmdUtils.renderTemplate( msg, subs );
},
execute: function( what, mods ) {
// If the scope text isn't specified, use the current selection.
var text = mods.in.text || CmdUtils.getTextSelection();
var newText = text.replace( what.text, args.with.text, "i");
CmdUtils.setSelection( newText );
}
});
 
</pre>
 
(In earlier prototypes, modifier arguments could only accept a single-word value, but this has now been fixed.)
 
The modifiers argument takes a dictionary, where the key is the name of the argument and the value is the noun-type of the argument. In later releases we may include further options, like the ability to specify an argument as required/optional, etc.
 
The translate command is a good place to learn more about modifiers and the <code>noun_type_language</code>.
== Specifying Defaults for your Arguments ==
1,007
edits

Navigation menu