Create a gist now

Instantly share code, notes, and snippets.

@DiGi /Fixed-Replace
Last active Aug 29, 2015

What would you like to do?
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