This topic describes how to enable event listening for a component. When a specific event occurs on the component, the system automatically performs the corresponding operation.
Implementation methods
In WebOffice SDK for JavaScript, use the on
method of the instance to enable event listening.
instance.on ('Event name', (data) => {
//do something...
});
Events
The following table describes the events to which you can listen.
Event name | Description | Earliest SDK version |
---|---|---|
fileOpen | The callback of a file open event. | 1.0.0 |
error | The callback of an error event. | 1.1.2 |
tabSwitch | The callback of a tab switch event. | 1.1.2 |
fileStatus | The callback of a file save event. The event is triggered when a file is saved. | 1.1.5 |
previewLimit | The callback of a preview limit event. The event is triggered when you scroll to the bottom of the limited page. This event is valid only when the number of preview pages is limited and supports only Word, PPT, and PDF components. | 1.1.5 |
hasDocMap | The callback of an event where the file has a table of content. | 1.1.5 |
fullscreenChange | The callback of an event where the file enters or exits the full screen mode. | 1.1.5 |
fileNameChange | The callback of a file rename event. | 1.1.9 |
fileOpen
The callback of a file open event.
Example
// The callback of a file open event. instance.on('fileOpen', (data) => { console.log ('File open:', data); });
If the file is opened successfully, the following information is returned:
{ duration: 812, fileInfo: { createTime: 1606461829, id: "94749723688", modifyTime: 1606461829, name: "userName", officeType: "s", }, stageTime: 1614, success: true, time: 1614, ts: 1607858260164, }
If the file fails to be opened, the following information is returned:
{ msg: "Fail", result: "Fail" }
error
The callback of an error event.
For example, if you change the file name extension of a Microsoft Word file from doc
to xls
, an error occurs.
Example
// The callback of an error event. instance.on('error',(err)=>{ console.log ('An error occurred:',err); });
Response parameters
{ reason:"Fail" }
tabSwitch
The callback of a tab switch event. The event is triggered when the tab switches.
Example
instance.on('tabSwitch', function(data) { //do something... })
Response parameters
{ tabKey: 1 // The number of the current tab. }
fileStatus
The callback of a file save event.
Example
// The callback of a file save event. jssdk.on('fileStatus', (data) => { console.log ('File save:', data); });
Response parameters
{ status: 0, // The file is not updated. status: 1, // The version is saved. Trigger condition: manual saving, saving the file at a scheduled time, and automatic saving when the web page is closed. status: 2, // The file is empty and cannot be saved. Trigger condition: The file is empty after the kernel is saved. status: 3, // The storage space is used up. status: 4, // Frequent file saving operations are not allowed when the file is being saved. Trigger condition: The saving operation queue is full on the server. status: 5, // Failed to save the file. status: 6, // The file is being saved. Trigger condition: You update the file. status: 7, // The file is successfully saved. Trigger condition: You modify the file content. }
hasDocMap
The callback of an event where the file has a table of content.
instance.on('hasDocMap', function() {
// A callback is invoked when the file has a table of content.
})
fullscreenchange
The callback of an event where the file enters or exits the full screen mode.
Example
instance.on('fullscreenchange', , function(result) { //do something... })
Response parameters
{ status: 0, // The file exits the full screen mode. status: 1, // The file enters the full screen mode. }
fileNameChange
The callback of a file rename event.
Example
// The callback of a file rename event. jssdk.on('fileNameChange', (e) => { console.log ('New file name:', e.fileName); });
Response parameters
After the file is successfully renamed, the new file name is returned.
{ fileName: "NewWebOffice" }
Error codes
Error code | Description |
---|---|
Unknown | An unknown error occurred. |
userNotLogin | The user has not logged on. |
InvalidLink | The link is invalid. |
SessionFull | The number of collaborators has reached the upper limit. |
Fail | The file failed to be opened. |
PermissionDenied | You do not have the permissions to perform this operation. |
GetTokenFailed | Failed to obtain the request token. |
SessionExpired | The session expired. |
fileTooLarge | The file cannot be previewed because the file size is too large. |
GetFileInfoFailed | Failed to obtain the file information. |