JavaScript Documentation / editor
This object contains methods for interacting with 1Writer's text editor.
- editor.getSelectedRange()
- editor.setSelectedRange(start[, end])
- editor.getSelectedLineRange()
- editor.getSelectedText()
- editor.getTextInRange(start, end)
- editor.replaceTextInRange(start, end, replacement)
- editor.replaceSelection(replacement)
- editor.getText()
- editor.setText(text)
- editor.getFolderPath()
- editor.getFileName()
- editor.getTodos([completed])
- editor.newFile([text], [name], [callback])
- editor.openFile(path, [mode], [callback])
- editor.close()
- editor.isClosed()
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
- startrequiredThe position of the first character.
- endoptionalThe 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
- startrequiredThe position of the first character.
- endrequiredThe position of the last character.
editor.replaceTextInRange(start, end, replacement)
Replaces the text in the given range with replacement
.
Parameters
- startrequiredThe position of the first character in the text to be replaced.
- endrequiredThe position of the last character in the text to be replaced.
- replacementrequiredThe text that replaces the text between
start
andend
.
editor.replaceSelection(replacement)
Replaces the text in the current selection with replacement
.
Parameters
- replacementrequiredThe 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
- textrequiredThe 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
- completedoptionalIndicates status of the to-do items will be returned. If this parameter is omitted, all to-do items will be returned.
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
- textoptionalInitial text for the new file.
- nameoptionalName for the new file.
- callbackoptional1Writer creates and opens file asynchronously. This function will be executed when the file has been opened.
editor.openFile(path, [mode], [callback])
Opens an existing file.
Parameters
- pathrequiredThe path to the file to be opened.
- modeoptionalValid values are
edit
andpreview
. If this parameter is omitted, file will be opened in the preferred mode specified in the app settings. - callbackoptional1Writer opens file asynchronously. This function will be executed when the file has been opened.
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.