Skip to content

Instantly share code, notes, and snippets.

@DiGi
Last active August 29, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiGi/40158 to your computer and use it in GitHub Desktop.
Save DiGi/40158 to your computer and use it in GitHub Desktop.
Fixed version from http://azarask.in/verbs/ + replace-all
// Fixed version from http://azarask.in/verbs/ + replace-all - digi@qr.cz
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 "{$what}" with {$with} in {$in}.';
var subs = {what: what.text, with: mods.with.text, in: 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, mods.with.text, "i");
CmdUtils.setSelection( newText );
}
});
CmdUtils.CreateCommand({
name: "replace-all",
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 all "{$what}" with {$with} in {$in}.';
var subs = {what: what.text, with: mods.with.text, in: 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, mods.with.text, "gi");
CmdUtils.setSelection( newText );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment