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.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
- 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
andend
.
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.newFile([text], [name], [callback])
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.
- callback optional 1Writer 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
- path required The path to the file to be opened.
-
mode
optional
Valid values are
edit
andpreview
. If this parameter is omitted, file will be opened in the preferred mode specified in the app settings. - callback optional 1Writer 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.