工作模块

onlyoffice分为客户端和服务端两大模块

客户端

Document manager

文档管理启,即在浏览器内展示文件列表和文件操作,需要自己实现和集成

Document editor

文档编辑器,在浏览器内查看和编辑文档的工具,通过调用Document Server提供的jssdk生成。编辑器通过document editing service来实现文档查看和编辑功能

服务端

Document storage service

  • 提供保存用户有访问权限的文件的能力
  • 提供文档id和链接给Document manager
  • 需要自己实现和集成

    Document editing service

    提供文档查看和编辑能力,主要给Document Editor使用

    Document command service

    提供通过命令的方式调用Document editing service的能力

    Document conversion service

    文档转换服务,提供将文档转换成适当的office open xml格式的能力,供编辑和下载

    Document builder service

    提供直接创建文档的能力

Document manager和Document storage service是不包含在OnlyOffice提供的Document Server内的,需要自己实现

下面将说明文档操作的常见步骤(打开、保存、协同编辑、下载、文档历史)的运作流程

打开文档

打开文档非常简单,引入jssdk,传入文档参数调用生成编辑器的api即可

  1. <div id="placeholder"></div>
  2. <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
  3. <script>
  4. new DocsAPI.DocEditor("placeholder", {
  5. "document": {
  6. "fileType": "docx",
  7. "key": "Khirz6zTPdfd7",
  8. "title": "Example Document Title.docx",
  9. "url": "https://example.com/url-to-example-document.docx"
  10. },
  11. "documentType": "text"
  12. });
  13. </script>

保存文档

参考以下代码配置

  1. new DocsAPI.DocEditor("placeholder", {
  2. "document": {
  3. "fileType": "docx",
  4. "key": "Khirz6zTPdfd7",
  5. "title": "Example Document Title.docx",
  6. "url": "https://example.com/url-to-example-document.docx"
  7. },
  8. "documentType": "text",
  9. "editorConfig": {
  10. "callbackUrl": "https://example.com/url-to-callback.ashx"
  11. }
  12. });

保存文件的关键在于callbackUrl,这是Document storage service提供的一个接口,内部实现保存文件的逻辑。保存默认在关闭编辑器5秒后出发,可修改savetimeoutdelay配置改变delay时间,也可通过force saving触发保存。
force saving三种方式:

  • 通过Document command service发送请求
  • 初始化编辑器时配置,用户点击保存按钮时会触发
  • 配置Document server自动保存,可配置循环时间

详细可查看官方文档:https://api.onlyoffice.com/editors/save

协同编辑

多人同时打开一个文档,其中一人修改文档内容后,Document editor会发送修改内容到Document editing serviceDocument editing service会将修改内容发送到其他用户的Document editor。当其中一人结束编辑,会触发文件保存,所以在Document storage service内需要处理多人编辑时的保存逻辑。在example内,当一人结束编辑时不会直接修改文档源文件,而是临时保存一个document文件,当所有人都结束编辑时才会修改文档源文件。
协同编辑需要在调用js api时传入用户信息

  1. new DocsAPI.DocEditor("placeholder", {
  2. "document": {
  3. "fileType": "docx",
  4. "key": "Khirz6zTPdfd7",
  5. "title": "Example Document Title.docx",
  6. "url": "https://example.com/url-to-example-document.docx"
  7. },
  8. "documentType": "text",
  9. "editorConfig": {
  10. "user": {
  11. "id": "78e1e841",
  12. "name": "John Smith"
  13. }
  14. }
  15. });

下载文档

https://api.onlyoffice.com/editors/conversion

文档历史

在js api内传入event handler可实现历史记录的查看,参考一下代码

  1. var onRequestHistory = function() {
  2. docEditor.refreshHistory({
  3. "currentVersion": 2,
  4. "history": [
  5. {
  6. "created": "2010-07-06 10:13 AM",
  7. "key": "af86C7e71Ca8",
  8. "user": {
  9. "id": "F89d8069ba2b",
  10. "name": "Kate Cage"
  11. },
  12. "version": 1
  13. },
  14. {
  15. "created": "2010-07-07 3:46 PM",
  16. "key": "Khirz6zTPdfd7",
  17. "user": {
  18. "id": "78e1e841",
  19. "name": "John Smith"
  20. },
  21. "version": 2
  22. },
  23. ...
  24. ]
  25. });
  26. };
  27. var onRequestHistoryData = function(event) {
  28. var version = event.data;
  29. docEditor.setHistoryData({
  30. "key": "Khirz6zTPdfd7",
  31. "url": "https://example.com/url-to-example-document.docx",
  32. "version": version
  33. })
  34. };
  35. var docEditor = new DocsAPI.DocEditor("placeholder", {
  36. "events": {
  37. "onRequestHistory": onRequestHistory,
  38. "onRequestHistoryData": onRequestHistoryData,
  39. ...
  40. },
  41. ...
  42. });

onRequestHistory

方法内调用编辑器实例的refreshHistory方法,初始化历史记录列表

onRequestHistoryData

方法内调用编辑器实例的setHistoryData方法,查看指定版本的文档。传参需要有文档链接、key和版本

获取历史记录需要Document storage service提供接口,同时保存文档时也要保存历史记录相关文件,具体可参考example内代码

鉴权

https://api.onlyoffice.com/editors/security