JavaScript Documentation / editor

This object contains methods for interacting with 1Writer's text editor.

editor.getSelectedRange()

Returns the range of the current selection in the editor. The returned value is an array of two integer values, contains the positions of the first and last characters.

Example

var range = editor.getSelectedRange(); //value of range is [5, 10]

editor.setSelectedRange(start[, end])

Selects a range of text in the editor. You can move the caret by setting the start parameter to the position where you want the caret to move to and omit the end parameter.

Parameters

  • start required The position of the first character.
  • end optional The position of the last character.

Example

editor.setSelectedRange(10, 15);
editor.setSelectedRange(20);

editor.getSelectedLineRange()

Returns the range of lines that includes the current selection.

editor.getSelectedText()

Returns the current selected text in the editor.

editor.getTextInRange(start, end)

Returns the text in the given range.

Parameters

  • start required The position of the first character.
  • end required The position of the last character.

editor.replaceTextInRange(start, end, replacement)

Replaces the text in the given range with replacement.

Parameters

  • start required The position of the first character in the text to be replaced.
  • end required The position of the last character in the text to be replaced.
  • replacement required The text that replaces the text between start and end.

editor.replaceSelection(replacement)

Replaces the text in the current selection with replacement.

Parameters

  • replacement required The text that replaces the text in the current selection.

editor.getText()

Returns the entire text in the editor.

editor.setText(text)

Replaces the entire text in the editor with text.

Parameters

  • text required The text that replaces the entire text in the editor.

editor.getFolderPath()

Returns the folder path of the file that is currently opened in the editor.

editor.getFileName()

Returns the name of the file that is currently opened in the editor.

editor.getTodos([completed])

Returns an array of to-do items in the file that is currently opened in the editor. Each element in the array is an array that contains the title and status of the to-do item.

Parameters

  • completed optional Indicates status of the to-do items will be returned. If this parameter is omitted, all to-do items will be returned.

Example

var allTodos = editor.getTodos();
var result = [];
for (var [title, status] of allTodos) {
    result.push(formatString('{0} - {1}', title, status));
}
ui.alert(result.join('\n'));

editor.newFile([text], [name])

Creates a new file with the given text and name.

Parameters

  • text optional Initial text for the new file.
  • name optional Name for the new file.

editor.openFile(path, [mode])

Opens an existing file. This method is not supported in extension.

Parameters

  • path required The path to the file to be opened.
  • mode optional Valid values are edit and preview. If this parameter is omitted, file will be opened in the preferred mode specified in the app settings.

Example

editor.openFile('Dropbox/Documents/Notes.txt');

editor.close()

Close the file that is currently opened in the editor.

editor.isClosed()

Returns a boolean indicating whether a file is currently opened in the editor.