Recipe ‹ Chocolat API

Recipe.run(callback)

Run a text recipe on the current editor.

  • callback Function(Recipe) – A callback function in which to make the changes. The function will be given an instance of Recipe as its argument, and may return false at any time in order to cancel the changes.

Example

Recipe.run(function(recipe) {
    recipe.insertTextAtLocation(recipe.length, "Hello World!");
});

Recipe.runOn(editor, callback)

Run a text recipe on a specified Editor.

  • editor Editor – An instance of Editor to run the recipe on.
  • callback Function(Recipe) – A callback function in which to make the changes. The function will be given an instance of Recipe as its argument, and may return false at any time in order to cancel the changes.

Example

Recipe.runOn(Editor.current(), function(recipe) {
    recipe.insertTextAtLocation(recipe.length, "Hello World!");
});

.selection

Get or set the editor's selected range.

Returns Range – the selected character range.

Note

Text cursors are represented as selections with length == 0.

.length

Get the length of the document.

Returns Number – the length of the document.

.text

Get or set the text of the document.

Returns String – the content of the document.

.textInRange(rng)

Returns text in range.

  • rng Range – the desired range.

.rangeOfLinesInRange(rng)

Expands a given range to cover the entire range of each line, including ending newlines.

  • rng Range – a range of characters to expand.

.contentRangeOfLinesInRange(rng)

Expands a given range to cover the entire range of each line, excluding ending newlines.

  • rng Range – a range of characters to expand.

.lineIndexesForCharacterRange(rng)

Gives a range of line indexes (line numbers starting from zero) for the given character range.

  • rng Range – a range of characters.

.characterRangeForLineIndexes(rng)

Gives a character range for the given range of line indexes (line numbers starting from zero).

  • rng Range – a range of line indexes.

.wordRangeForRange(rng)

Gives a character range for the nearest word to the given character range.

  • rng Range – a character range.

.eachLine(f, rng)

Execute function f on lines in character range rng. If no range is passed, execute the function
on all the lines in the document.

  • f Function – a function that is given a LineMarker. Return a new string to modify the line, null to delete the line, or undefined to keep the line the same.
  • rng Range – an optional range. Defaults to the whole document if omitted.

Returns Number – the total number of characters added or deleted (0 if nothing was changed).

Example

// Wrap each line in brackets
Recipe.run(function(recipe) {

    recipe.eachLine(function(marker) {
        return "(" + marker.text + ")";
    });
});

.replaceTextInRange(rng, replacement, recordUndo)

Replace text in rng with replacement, optionally recording the operation for undo.

  • rng Range – the range of text you want to replace.
  • replacement String – the text to replace it with.
  • recordUndo Bool – (optional) whether or not to record the operation for undo. The default is true.

.deleteTextInRange(rng, recordUndo)

Delete text in range rng, optionally recording the operation for undo.

  • rng Range – the range of text to delete.
  • recordUndo Bool – (optional) whether or not to record the operation for undo. The default is true.

.insertTextAtLocation(location, newText, recordUndo)

Insert text at specified location, optionally recording the operation for undo.

  • location Number – the location at which to insert the new text.
  • newText String – the text to insert at location.
  • recordUndo Bool – (optional) whether or not to record the operation for undo. The default is true.