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

const 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

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

editor.replaceTextInRange(start, end, replacement)

Replaces the text in the given range with replacement.

Parameters

editor.replaceSelection(replacement)

Replaces the text in the current selection with replacement.

Parameters

editor.getText()

Returns the entire text in the editor.

editor.setText(text)

Replaces the entire text in the editor with text.

Parameters

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

Example

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

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

Creates a new file with the given text and name.

Parameters

editor.openFile(path, [mode], [callback])

Opens an existing file.

Parameters

Example

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

editor.close()

Closes the file that is currently opened in the editor.

editor.isClosed()

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