File API Commands
file-new
Create an empty document.
Synopsys
Command "file-new" {} => true
Example
WebShellApp.command( "file-new", async function () {
await myEditor.emptyContent();
return true;
})
Workflow
- The user clicks the File > New button
- WebShell sends the command to the app
- The app creates a new empty document
- The app returns
true
- WebShell sets the current file in accordance with the manifest
The file is named
New [label].[extension]
file-open
The app receives the content of the file to be opened, as well as its extension and the ID of its format (this will be one of the formats specified in the app's manifest). The type of content
depends on the encoding specified in the format.
Synopsis
Command "file-open" { String extension, String format, Varying content } => true
Errors
Error "file-cannot-open" { String reason }
Throw if the received content does not match the supplied expected format.
Example
WebShellApp.command("file-open", async function ({format, content, extension}) {
try {
await myEditor.setContent(content);
} catch (error) {
this.throw("file-cannot-open", {
reason: String(error)
})
}
return true;
})
Workflow
- The user clicks the File > Open button
- WebShell displays the File Open dialog to allow the user tp choose a file.
- WebShell sends the command to the app
- The app creates a new document from the file's contents.
- The app returns
true
If the app throws an error, WebShell display it and skips the following steps.
- WebShell sets the current file to the opened file
file-save
The app receives the format and the extension of the file that is to be saved, and returns the content of the file that will be saved. The expected type of content
depends on the encoding specified in the manifest. The default mime type specified in the format in the manifest will be used for the saved file.
Synopsis
Command "file-save" { String extension, String format } => { content }
Example
WebShellApp.command("file-open", async function ({format, extension}) {
switch (format) {
case "text": return ({
content: await myEditor.getText()
})
case "myFormat": return ({
content: await myEditor.getContent()
})
}
})
Workflow
Save
- The user clicks the File > Save
- WebShell sends the command to the app, with
format
andextension
set according to the current file. - The app creates the file content from its current document
- The app returns the content.
- WebShell saves the file.
Save As
- The user clicks File > Save As button
- WebShell displays the File Save As dialog to allow the user to choose a file name and format.
- WebShell sends the command to the app, setting
format
andextension
from the dialog. - The app creates the file content from its current document.
- The app returns the content.
- WebShell saves the file
- WebShell sets the current file to the new file name and format.