Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Document

Contains the text of the document. Document can be attached to several EditSessions. At its core, Documents are just an array of strings, with each row in the document matching up to the array index.

Hierarchy

  • Document

Implements

Index

Constructors

constructor

  • new Document(textOrLines: String | String[]): Document
  • Creates a new Document. If text is included, the Document contains those strings; otherwise, it's empty.

    Parameters

    • textOrLines: String | String[]

      text The starting text

    Returns Document

Events

on

  • on(name: "change", callback: (e: Object) => void): void
  • on(name: string, callback: Function, capturing?: boolean): void
  • 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

    Parameters

    • name: "change"
    • callback: (e: Object) => void
        • (e: Object): void
        • Parameters

          • e: Object

          Returns void

    Returns void

  • Parameters

    • name: string
    • callback: Function
    • Optional capturing: boolean

    Returns void

Methods

applyDelta

  • applyDelta(delta: Delta, doNotValidate?: boolean): void
  • Applies delta to the document.

    Parameters

    • delta: Delta

      A delta object (can include "insert" and "remove" actions)

    • Optional doNotValidate: boolean

    Returns void

applyDeltas

  • applyDeltas(deltas: Delta[]): void
  • Applies all changes in deltas to the document.

    Parameters

    • deltas: Delta[]

      An array of delta objects (can include "insert" and "remove" actions)

    Returns void

createAnchor

  • createAnchor(row: number, column: number): Anchor
  • Creates a new Anchor to define a floating point in the document.

    Parameters

    • row: number

      The row number to use

    • column: number

      The column number to use

    Returns Anchor

getAllLines

  • getAllLines(): string[]
  • Returns all lines in the document as string array.

    Returns string[]

getLength

  • getLength(): number

getLine

  • getLine(row: number): string
  • Returns a verbatim copy of the given line as it is in the document

    Parameters

    • row: number

      The row index to retrieve

    Returns string

getLines

  • getLines(firstRow: number, lastRow: number): string[]
  • Returns an array of strings of the rows between firstRow and lastRow. This function is inclusive of lastRow.

    Parameters

    • firstRow: number

      The first row index to retrieve

    • lastRow: number

      The final row index to retrieve

    Returns string[]

getLinesForRange

  • getLinesForRange(range: Range): string[]
  • Returns all the text within range as an array of lines.

    Parameters

    • range: Range

      The range to work with.

    Returns string[]

getNewLineCharacter

  • getNewLineCharacter(): string
  • 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.

getNewLineMode

  • [Returns the type of newlines being used; either windows, unix, or auto]{: #Document.getNewLineMode}

    Returns NewLineMode

getTextRange

  • getTextRange(range: Range): string
  • Returns all the text within range as a single string.

    Parameters

    • range: Range

      The range to work with.

    Returns string

getValue

  • getValue(): string
  • Returns all the lines in the document as a single string, joined by the new line character.

    Returns string

indexToPosition

  • indexToPosition(index: number, startRow: number): Position
  • Converts an index position in a document to a {row, column} object.

    Index refers to the "absolute position" of a character in the document. For example:

    var x = 0; // 10 characters, plus one for newline
    var y = -1;
    

    Here, y is an index 15: 11 characters for the first row, and 5 characters until y in the second.

    Parameters

    • index: number

      An index to convert

    • startRow: number

    Returns Position

    A {row, column} object of the index position

insert

  • insert(position: Position, text: string): Position
  • insert(position: { column: number; row: number }, text: string): Position
  • Inserts a block of text at the indicated position.

    Parameters

    • position: Position

      The position to start inserting at; it's an object that looks like { row: row, column: column}

    • text: string

      A chunk of text to insert

    Returns Position

    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.

    Parameters

    • position: { column: number; row: number }

      The position to start inserting at; it's an object that looks like { row: row, column: column}

      • column: number
      • row: number
    • text: string

      A chunk of text to insert

    Returns Position

    The position ({row, column}) of the last line of text. If the length of text is 0, this function simply returns position.

insertFullLines

  • insertFullLines(row: number, lines: string[]): void
  • 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.

    Parameters

    • row: number

      The index of the row to insert at

    • lines: string[]

      An array of strings

    Returns void

    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}
    
    

insertInLine

  • 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.

    Parameters

    • position: Position

      The position to insert at; it's an object that looks like { row: row, column: column}

    • text: string

      A chunk of text

    Returns Position

    Returns an object containing the final row and column, like this:

    
    {row: endRow, column: 0}
    
    

insertLines

  • insertLines(row: any, lines: any): any
  • Deprecated methods retained for backwards compatibility.

    Parameters

    • row: any
    • lines: any

    Returns any

insertMergedLines

  • Inserts the elements in lines into the document, starting at the position index given by row. This method also triggers the "change" event.

    Parameters

    • position: Position
    • lines: string[]

      An array of strings

    Returns Point

    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}
    
    

isNewLine

  • isNewLine(text: string): boolean
  • Returns true if text is a newline character (either \r\n, \r, or \n).

    Parameters

    • text: string

      The text to check

    Returns boolean

positionToIndex

  • positionToIndex(pos: Position, startRow?: number): number
  • Converts the {row, column} position in a document to the character's index.

    Index refers to the "absolute position" of a character in the document. For example:

    var x = 0; // 10 characters, plus one for newline
    var y = -1;
    

    Here, y is an index 15: 11 characters for the first row, and 5 characters until y in the second.

    Parameters

    • pos: Position

      The {row, column} to convert

    • Optional startRow: number

    Returns number

    The index position in the document

remove

  • Removes the range from the document.

    Parameters

    • range: Range

      A specified Range to remove

    Returns Position

    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.

removeFullLines

  • removeFullLines(firstRow: number, lastRow: number): string[]
  • Removes a range of full lines. This method also triggers the "change" event.

    Parameters

    • firstRow: number

      The first row to be removed

    • lastRow: number

      The last row to be removed

    Returns string[]

    Returns all the removed lines.

removeInLine

  • removeInLine(row: number, startColumn: number, endColumn: number): Position
  • Removes the specified columns from the row. This method also triggers a "change" event.

    Parameters

    • row: number

      The row to remove from

    • startColumn: number

      The column to start removing at

    • endColumn: number

      The column to stop removing at

    Returns Position

    Returns an object containing startRow and startColumn, indicating the new row and column values.
    If startColumn is equal to endColumn, this function returns nothing.

removeNewLine

  • removeNewLine(row: number): void
  • Removes the new line between row and the row immediately following it. This method also triggers the "change" event.

    Parameters

    • row: number

      The row to check

    Returns void

replace

  • Replaces a range in the document with the new text.

    Parameters

    • range: Range

      A specified Range to replace

    • text: string

      The new text to use as a replacement

    Returns Position

    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.

revertDelta

  • revertDelta(delta: Delta): void
  • Reverts delta from the document.

    Parameters

    • delta: Delta

      A delta object (can include "insert" and "remove" actions)

    Returns void

revertDeltas

  • revertDeltas(deltas: Delta[]): void
  • Reverts all changes in deltas from the document.

    Parameters

    • deltas: Delta[]

      An array of delta objects (can include "insert" and "remove" actions)

    Returns void

setNewLineMode

  • [Sets the new line mode.]{: #Document.setNewLineMode.desc}

    Parameters

    • newLineMode: NewLineMode

      {: #Document.setNewLineMode.param}

    Returns void

setValue

  • setValue(text: string): void
  • Replaces all the lines in the current Document with the value of text.

    Parameters

    • text: string

      The text to use

    Returns void

Generated using TypeDoc