|
|
@@ -70,10 +70,10 @@ var Document = function(textOrLines) {
|
|
|
oop.implement(this, EventEmitter);
|
|
|
|
|
|
/**
|
|
|
- * Replaces all the lines in the current `Document` with the value of `text`.
|
|
|
- *
|
|
|
- * @param {String} text The text to use
|
|
|
- **/
|
|
|
+ * Replaces all the lines in the current `Document` with the value of `text`.
|
|
|
+ *
|
|
|
+ * @param {String} text The text to use
|
|
|
+ **/
|
|
|
this.setValue = function(text) {
|
|
|
var len = this.getLength() - 1;
|
|
|
this.remove(new Range(0, 0, len, this.getLine(len).length));
|
|
|
@@ -81,30 +81,30 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns all the lines in the document as a single string, joined by the new line character.
|
|
|
- **/
|
|
|
+ * Returns all the lines in the document as a single string, joined by the new line character.
|
|
|
+ **/
|
|
|
this.getValue = function() {
|
|
|
return this.getAllLines().join(this.getNewLineCharacter());
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Creates a new `Anchor` to define a floating point in the document.
|
|
|
- * @param {Number} row The row number to use
|
|
|
- * @param {Number} column The column number to use
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Creates a new `Anchor` to define a floating point in the document.
|
|
|
+ * @param {Number} row The row number to use
|
|
|
+ * @param {Number} column The column number to use
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.createAnchor = function(row, column) {
|
|
|
return new Anchor(this, row, column);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Splits a string of text on any newline (`\n`) or carriage-return (`\r`) characters.
|
|
|
- *
|
|
|
- * @method $split
|
|
|
- * @param {String} text The text to work with
|
|
|
- * @returns {String} A String array, with each index containing a piece of the original `text` string.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Splits a string of text on any newline (`\n`) or carriage-return (`\r`) characters.
|
|
|
+ *
|
|
|
+ * @method $split
|
|
|
+ * @param {String} text The text to work with
|
|
|
+ * @returns {String} A String array, with each index containing a piece of the original `text` string.
|
|
|
+ *
|
|
|
+ **/
|
|
|
|
|
|
// check for IE split bug
|
|
|
if ("aaa".split(/a/).length === 0) {
|
|
|
@@ -125,12 +125,12 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns the newline character that's being used, depending on the value of `newLineMode`.
|
|
|
- * @returns {String} If `newLineMode == windows`, `\r\n` is returned.
|
|
|
- * If `newLineMode == unix`, `\n` is returned.
|
|
|
- * If `newLineMode == auto`, the value of `autoNewLine` is returned.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Returns the newline character that's being used, depending on the value of `newLineMode`.
|
|
|
+ * @returns {String} If `newLineMode == windows`, `\r\n` is returned.
|
|
|
+ * If `newLineMode == unix`, `\n` is returned.
|
|
|
+ * If `newLineMode == auto`, the value of `autoNewLine` is returned.
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.getNewLineCharacter = function() {
|
|
|
switch (this.$newLineMode) {
|
|
|
case "windows":
|
|
|
@@ -158,71 +158,71 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * [Returns the type of newlines being used; either `windows`, `unix`, or `auto`]{: #Document.getNewLineMode}
|
|
|
- * @returns {String}
|
|
|
- **/
|
|
|
+ * [Returns the type of newlines being used; either `windows`, `unix`, or `auto`]{: #Document.getNewLineMode}
|
|
|
+ * @returns {String}
|
|
|
+ **/
|
|
|
this.getNewLineMode = function() {
|
|
|
return this.$newLineMode;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns `true` if `text` is a newline character (either `\r\n`, `\r`, or `\n`).
|
|
|
- * @param {String} text The text to check
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Returns `true` if `text` is a newline character (either `\r\n`, `\r`, or `\n`).
|
|
|
+ * @param {String} text The text to check
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.isNewLine = function(text) {
|
|
|
return (text == "\r\n" || text == "\r" || text == "\n");
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns a verbatim copy of the given line as it is in the document
|
|
|
- * @param {Number} row The row index to retrieve
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Returns a verbatim copy of the given line as it is in the document
|
|
|
+ * @param {Number} row The row index to retrieve
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.getLine = function(row) {
|
|
|
return this.$lines[row] || "";
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns an array of strings of the rows between `firstRow` and `lastRow`. This function is inclusive of `lastRow`.
|
|
|
- * @param {Number} firstRow The first row index to retrieve
|
|
|
- * @param {Number} lastRow The final row index to retrieve
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Returns an array of strings of the rows between `firstRow` and `lastRow`. This function is inclusive of `lastRow`.
|
|
|
+ * @param {Number} firstRow The first row index to retrieve
|
|
|
+ * @param {Number} lastRow The final row index to retrieve
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.getLines = function(firstRow, lastRow) {
|
|
|
return this.$lines.slice(firstRow, lastRow + 1);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns all lines in the document as string array.
|
|
|
- **/
|
|
|
+ * Returns all lines in the document as string array.
|
|
|
+ **/
|
|
|
this.getAllLines = function() {
|
|
|
return this.getLines(0, this.getLength());
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns the number of rows in the document.
|
|
|
- **/
|
|
|
+ * Returns the number of rows in the document.
|
|
|
+ **/
|
|
|
this.getLength = function() {
|
|
|
return this.$lines.length;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns all the text within `range` as a single string.
|
|
|
- * @param {Range} range The range to work with.
|
|
|
- *
|
|
|
- * @returns {String}
|
|
|
- **/
|
|
|
+ * Returns all the text within `range` as a single string.
|
|
|
+ * @param {Range} range The range to work with.
|
|
|
+ *
|
|
|
+ * @returns {String}
|
|
|
+ **/
|
|
|
this.getTextRange = function(range) {
|
|
|
return this.getLinesForRange(range).join(this.getNewLineCharacter());
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns all the text within `range` as an array of lines.
|
|
|
- * @param {Range} range The range to work with.
|
|
|
- *
|
|
|
- * @returns {Array}
|
|
|
- **/
|
|
|
+ * Returns all the text within `range` as an array of lines.
|
|
|
+ * @param {Range} range The range to work with.
|
|
|
+ *
|
|
|
+ * @returns {Array}
|
|
|
+ **/
|
|
|
this.getLinesForRange = function(range) {
|
|
|
var lines;
|
|
|
if (range.start.row === range.end.row) {
|
|
|
@@ -254,12 +254,12 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Inserts a block of `text` at the indicated `position`.
|
|
|
- * @param {Object} position The position to start inserting at; it's an object that looks like `{ row: row, column: column}`
|
|
|
- * @param {String} text A chunk of text to insert
|
|
|
- * @returns {Object} The position ({row, column}) of the last line of `text`. If the length of `text` is 0, this function simply returns `position`.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Inserts a block of `text` at the indicated `position`.
|
|
|
+ * @param {Object} position The position to start inserting at; it's an object that looks like `{ row: row, column: column}`
|
|
|
+ * @param {String} text A chunk of text to insert
|
|
|
+ * @returns {Object} The position ({row, column}) of the last line of `text`. If the length of `text` is 0, this function simply returns `position`.
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.insert = function(position, text) {
|
|
|
// Only detect new lines if the document has no line break yet.
|
|
|
if (this.getLength() <= 1)
|
|
|
@@ -269,19 +269,19 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Inserts `text` into the `position` at the current row. This method also triggers the `"change"` event.
|
|
|
- *
|
|
|
- * This differs from the `insert` method in two ways:
|
|
|
- * 1. This does NOT handle newline characters (single-line text only).
|
|
|
- * 2. This is faster than the `insert` method for single-line text insertions.
|
|
|
- *
|
|
|
- * @param {Object} position The position to insert at; it's an object that looks like `{ row: row, column: column}`
|
|
|
- * @param {String} text A chunk of text
|
|
|
- * @returns {Object} Returns an object containing the final row and column, like this:
|
|
|
- * ```
|
|
|
- * {row: endRow, column: 0}
|
|
|
- * ```
|
|
|
- **/
|
|
|
+ * Inserts `text` into the `position` at the current row. This method also triggers the `"change"` event.
|
|
|
+ *
|
|
|
+ * This differs from the `insert` method in two ways:
|
|
|
+ * 1. This does NOT handle newline characters (single-line text only).
|
|
|
+ * 2. This is faster than the `insert` method for single-line text insertions.
|
|
|
+ *
|
|
|
+ * @param {Object} position The position to insert at; it's an object that looks like `{ row: row, column: column}`
|
|
|
+ * @param {String} text A chunk of text
|
|
|
+ * @returns {Object} Returns an object containing the final row and column, like this:
|
|
|
+ * ```
|
|
|
+ * {row: endRow, column: 0}
|
|
|
+ * ```
|
|
|
+ **/
|
|
|
this.insertInLine = function(position, text) {
|
|
|
var start = this.clippedPos(position.row, position.column);
|
|
|
var end = this.pos(position.row, position.column + text.length);
|
|
|
@@ -334,36 +334,36 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Fires whenever the document changes.
|
|
|
- *
|
|
|
- * Several methods trigger different `"change"` events. Below is a list of each action type, followed by each property that's also available:
|
|
|
- *
|
|
|
- * * `"insert"`
|
|
|
- * * `range`: the [[Range]] of the change within the document
|
|
|
- * * `lines`: the lines being added
|
|
|
- * * `"remove"`
|
|
|
- * * `range`: the [[Range]] of the change within the document
|
|
|
- * * `lines`: the lines being removed
|
|
|
- *
|
|
|
- * @event change
|
|
|
- * @param {Object} e Contains at least one property called `"action"`. `"action"` indicates the action that triggered the change. Each action also has a set of additional properties.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Fires whenever the document changes.
|
|
|
+ *
|
|
|
+ * Several methods trigger different `"change"` events. Below is a list of each action type, followed by each property that's also available:
|
|
|
+ *
|
|
|
+ * * `"insert"`
|
|
|
+ * * `range`: the [[Range]] of the change within the document
|
|
|
+ * * `lines`: the lines being added
|
|
|
+ * * `"remove"`
|
|
|
+ * * `range`: the [[Range]] of the change within the document
|
|
|
+ * * `lines`: the lines being removed
|
|
|
+ *
|
|
|
+ * @event change
|
|
|
+ * @param {Object} e Contains at least one property called `"action"`. `"action"` indicates the action that triggered the change. Each action also has a set of additional properties.
|
|
|
+ *
|
|
|
+ **/
|
|
|
|
|
|
/**
|
|
|
- * Inserts the elements in `lines` into the document as full lines (does not merge with existing line), starting at the row index given by `row`. This method also triggers the `"change"` event.
|
|
|
- * @param {Number} row The index of the row to insert at
|
|
|
- * @param {Array} lines An array of strings
|
|
|
- * @returns {Object} Contains the final row and column, like this:
|
|
|
- * ```
|
|
|
- * {row: endRow, column: 0}
|
|
|
- * ```
|
|
|
- * If `lines` is empty, this function returns an object containing the current row, and column, like this:
|
|
|
- * ```
|
|
|
- * {row: row, column: 0}
|
|
|
- * ```
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Inserts the elements in `lines` into the document as full lines (does not merge with existing line), starting at the row index given by `row`. This method also triggers the `"change"` event.
|
|
|
+ * @param {Number} row The index of the row to insert at
|
|
|
+ * @param {Array} lines An array of strings
|
|
|
+ * @returns {Object} Contains the final row and column, like this:
|
|
|
+ * ```
|
|
|
+ * {row: endRow, column: 0}
|
|
|
+ * ```
|
|
|
+ * If `lines` is empty, this function returns an object containing the current row, and column, like this:
|
|
|
+ * ```
|
|
|
+ * {row: row, column: 0}
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.insertFullLines = function(row, lines) {
|
|
|
// Clip to document.
|
|
|
// Allow one past the document end.
|
|
|
@@ -387,19 +387,19 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `"change"` event.
|
|
|
- * @param {Number} row The index of the row to insert at
|
|
|
- * @param {Array} lines An array of strings
|
|
|
- * @returns {Object} Contains the final row and column, like this:
|
|
|
- * ```
|
|
|
- * {row: endRow, column: 0}
|
|
|
- * ```
|
|
|
- * If `lines` is empty, this function returns an object containing the current row, and column, like this:
|
|
|
- * ```
|
|
|
- * {row: row, column: 0}
|
|
|
- * ```
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Inserts the elements in `lines` into the document, starting at the position index given by `row`. This method also triggers the `"change"` event.
|
|
|
+ * @param {Number} row The index of the row to insert at
|
|
|
+ * @param {Array} lines An array of strings
|
|
|
+ * @returns {Object} Contains the final row and column, like this:
|
|
|
+ * ```
|
|
|
+ * {row: endRow, column: 0}
|
|
|
+ * ```
|
|
|
+ * If `lines` is empty, this function returns an object containing the current row, and column, like this:
|
|
|
+ * ```
|
|
|
+ * {row: row, column: 0}
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.insertMergedLines = function(position, lines) {
|
|
|
var start = this.clippedPos(position.row, position.column);
|
|
|
var end = {
|
|
|
@@ -418,11 +418,11 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Removes the `range` from the document.
|
|
|
- * @param {Range} range A specified Range to remove
|
|
|
- * @returns {Object} Returns the new `start` property of the range, which contains `startRow` and `startColumn`. If `range` is empty, this function returns the unmodified value of `range.start`.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Removes the `range` from the document.
|
|
|
+ * @param {Range} range A specified Range to remove
|
|
|
+ * @returns {Object} Returns the new `start` property of the range, which contains `startRow` and `startColumn`. If `range` is empty, this function returns the unmodified value of `range.start`.
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.remove = function(range) {
|
|
|
var start = this.clippedPos(range.start.row, range.start.column);
|
|
|
var end = this.clippedPos(range.end.row, range.end.column);
|
|
|
@@ -458,12 +458,12 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Removes a range of full lines. This method also triggers the `"change"` event.
|
|
|
- * @param {Number} firstRow The first row to be removed
|
|
|
- * @param {Number} lastRow The last row to be removed
|
|
|
- * @returns {[String]} Returns all the removed lines.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Removes a range of full lines. This method also triggers the `"change"` event.
|
|
|
+ * @param {Number} firstRow The first row to be removed
|
|
|
+ * @param {Number} lastRow The last row to be removed
|
|
|
+ * @returns {[String]} Returns all the removed lines.
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.removeFullLines = function(firstRow, lastRow) {
|
|
|
// Clip to document.
|
|
|
firstRow = Math.min(Math.max(0, firstRow), this.getLength() - 1);
|
|
|
@@ -495,10 +495,10 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Removes the new line between `row` and the row immediately following it. This method also triggers the `"change"` event.
|
|
|
- * @param {Number} row The row to check
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Removes the new line between `row` and the row immediately following it. This method also triggers the `"change"` event.
|
|
|
+ * @param {Number} row The row to check
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.removeNewLine = function(row) {
|
|
|
if (row < this.getLength() - 1 && row >= 0) {
|
|
|
this.applyDelta({
|
|
|
@@ -511,17 +511,17 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Replaces a range in the document with the new `text`.
|
|
|
- * @param {Range} range A specified Range to replace
|
|
|
- * @param {String} text The new text to use as a replacement
|
|
|
- * @returns {Object} Returns an object containing the final row and column, like this:
|
|
|
- * {row: endRow, column: 0}
|
|
|
- * If the text and range are empty, this function returns an object containing the current `range.start` value.
|
|
|
- * If the text is the exact same as what currently exists, this function returns an object containing the current `range.end` value.
|
|
|
- *
|
|
|
- **/
|
|
|
+ * Replaces a range in the document with the new `text`.
|
|
|
+ * @param {Range} range A specified Range to replace
|
|
|
+ * @param {String} text The new text to use as a replacement
|
|
|
+ * @returns {Object} Returns an object containing the final row and column, like this:
|
|
|
+ * {row: endRow, column: 0}
|
|
|
+ * If the text and range are empty, this function returns an object containing the current `range.start` value.
|
|
|
+ * If the text is the exact same as what currently exists, this function returns an object containing the current `range.end` value.
|
|
|
+ *
|
|
|
+ **/
|
|
|
this.replace = function(range, text) {
|
|
|
- if (!range instanceof Range)
|
|
|
+ if (!(range instanceof Range))
|
|
|
range = Range.fromPoints(range.start, range.end);
|
|
|
if (text.length === 0 && range.isEmpty())
|
|
|
return range.start;
|
|
|
@@ -544,9 +544,9 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Applies all changes in `deltas` to the document.
|
|
|
- * @param {Array} deltas An array of delta objects (can include "insert" and "remove" actions)
|
|
|
- **/
|
|
|
+ * Applies all changes in `deltas` to the document.
|
|
|
+ * @param {Array} deltas An array of delta objects (can include "insert" and "remove" actions)
|
|
|
+ **/
|
|
|
this.applyDeltas = function(deltas) {
|
|
|
for (var i=0; i<deltas.length; i++) {
|
|
|
this.applyDelta(deltas[i]);
|
|
|
@@ -554,9 +554,9 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Reverts all changes in `deltas` from the document.
|
|
|
- * @param {Array} deltas An array of delta objects (can include "insert" and "remove" actions)
|
|
|
- **/
|
|
|
+ * Reverts all changes in `deltas` from the document.
|
|
|
+ * @param {Array} deltas An array of delta objects (can include "insert" and "remove" actions)
|
|
|
+ **/
|
|
|
this.revertDeltas = function(deltas) {
|
|
|
for (var i=deltas.length-1; i>=0; i--) {
|
|
|
this.revertDelta(deltas[i]);
|
|
|
@@ -564,9 +564,9 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Applies `delta` to the document.
|
|
|
- * @param {Object} delta A delta object (can include "insert" and "remove" actions)
|
|
|
- **/
|
|
|
+ * Applies `delta` to the document.
|
|
|
+ * @param {Object} delta A delta object (can include "insert" and "remove" actions)
|
|
|
+ **/
|
|
|
this.applyDelta = function(delta, doNotValidate) {
|
|
|
var isInsert = delta.action == "insert";
|
|
|
// An empty range is a NOOP.
|
|
|
@@ -620,9 +620,9 @@ var Document = function(textOrLines) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Reverts `delta` from the document.
|
|
|
- * @param {Object} delta A delta object (can include "insert" and "remove" actions)
|
|
|
- **/
|
|
|
+ * Reverts `delta` from the document.
|
|
|
+ * @param {Object} delta A delta object (can include "insert" and "remove" actions)
|
|
|
+ **/
|
|
|
this.revertDelta = function(delta) {
|
|
|
this.applyDelta({
|
|
|
start: this.clonePos(delta.start),
|