跳到主要内容

打开编辑器的生命周期

opening编辑器的生命周期可以通过一系列事件来定义。

添加用于初始化文档编辑器的脚本,并配置你要打开的文档:

const docEditor = new DocsAPI.DocEditor("placeholder", {
document: {
fileType: "docx",
key: "Khirz6zTPdfd7",
title: "Example Document Title.docx",
url: "https://example.com/url-to-example-document.docx",
},
documentType: "word",
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2N1bWVudCI6eyJmaWxlVHlwZSI6ImRvY3giLCJrZXkiOiJLaGlyejZ6VFBkZmQ3IiwidGl0bGUiOiJFeGFtcGxlIERvY3VtZW50IFRpdGxlLmRvY3giLCJ1cmwiOiJodHRwczovL2V4YW1wbGUuY29tL3VybC10by1leGFtcGxlLWRvY3VtZW50LmRvY3gifSwiZG9jdW1lbnRUeXBlIjoid29yZCJ9.7IpEJxdOvBQ0kJ8l6ZegIV4tX5vsPbZZCDDVmcFROXc",
})

在其中指定以下事件:

  1. 当应用程序加载到浏览器中时,会执行onAppReady事件:

    function onAppReady() {
    console.log("ONLYOFFICE Document Editor is ready")
    }

    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onAppReady,
    },
    })

    之后,可以调用showMessage方法,该方法会显示一个带有消息的工具提示:

    docEditor.showMessage(message)

    消息

    定义消息文本。

    类型:字符串

    是否必需:是

请注意,在嵌入式平台type中不支持显示带有消息的工具提示。

  1. 当发生错误或其他特定事件时,会执行onError事件:

    function onError(event) {
    console.log(`ONLYOFFICE Document Editor reports an error: code ${event.data.errorCode}, description ${event.data.errorDescription}`)
    }

    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onError,
    },
    })

    例如,可能是转换错误或加载某个编辑器组件时出错。此时将无法继续进行后续操作。

  2. 当使用旧的document.key值打开文档进行编辑时(该值曾用于编辑上一版本的文档并已成功保存),会执行onOutdatedVersion事件:

    function onOutdatedVersion() {
    location.reload(true)
    }

    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onOutdatedVersion,
    },
    })

    会出现一个error,并且无法继续进行后续操作。如果不处理此事件,文件将仅以只读模式打开。必须使用新的密钥重新初始化编辑器。

    此事件从8.3版本开始已弃用。请使用onRequestRefreshFile事件代替。

    当使用已成功保存文件的key打开编辑器时,将执行onRequestRefreshFile事件,而不是onOutdatedVersion事件:

    function onRequestRefreshFile() {
    refreshFile({
    document: {
    key: "Khirz6zTPdfd7",
    title: "Example Document Title.docx",
    url: "https://example.com/url-to-example-document.docx",
    permissions: {},
    },
    editorConfig: {
    callbackUrl: "https://example.com/url-to-callback.ashx",
    mode: "edit",
    },
    token: "...",
    })
    }
    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onRequestHistory,
    },
    })

    在这种情况下,会调用refreshFile方法,并使用新的密钥值更新文件版本,而无需重新加载编辑器。

  3. 当需要用户采取操作才能打开文档时,会执行onUserActionRequired事件:

    function onUserActionRequired() {
    console.log("Enter a password")
    }
    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onUserActionRequired,
    },
    })

    当用户需要输入密码才能打开受保护的文档,或者需要为txtcsv文件选择编码或分隔符时,就会发生这种情况。

  4. 当文档加载完成且编辑器准备好工作时,会执行onDocumentReady事件:

    function onDocumentReady() {
    console.log("Document is loaded")
    }

    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onDocumentReady,
    },
    })

    之后,可以向Automation API发送请求。

    在以下情况下,onDocumentReady事件之后也可能会调用onOutdatedVersiononRequestRefreshFile事件:

    1. 文档已被修改;
    2. 网络连接中断;
    3. 文档已成功保存;
    4. 编辑器已重新连接到服务器。
  5. 当用户试图结束与编辑器的交互并通过点击关闭按钮关闭编辑器时,会执行onRequestClose事件:

    function onRequestClose() {
    if (window.opener) {
    window.close()
    return
    }
    docEditor.destroyEditor()
    }

    const docEditor = new DocsAPI.DocEditor("placeholder", {
    events: {
    onRequestClose,
    },
    })

    在调用requestClose方法之后,也可能会执行onRequestClose事件。建议在调用destroyEditor方法之前调用此方法,以检查编辑器中是否有未保存的数据。如果存在未保存的数据,将显示一个对话框,询问用户是要继续编辑还是关闭编辑器并丢失所有未保存的数据。如果选择关闭选项,则会调用onRequestClose事件:

    docEditor.requestClose()

    destroyEditor 方法用于销毁 docEditor 对象。当需要使用其他配置重新初始化文档编辑器时,可以调用此方法:

    docEditor.destroyEditor()