工作模块
客户端
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即可
<div id="placeholder"></div>
<script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
<script>
new DocsAPI.DocEditor("placeholder", {
"document": {
"fileType": "docx",
"key": "Khirz6zTPdfd7",
"title": "Example Document Title.docx",
"url": "https://example.com/url-to-example-document.docx"
},
"documentType": "text"
});
</script>
保存文档
参考以下代码配置
new DocsAPI.DocEditor("placeholder", {
"document": {
"fileType": "docx",
"key": "Khirz6zTPdfd7",
"title": "Example Document Title.docx",
"url": "https://example.com/url-to-example-document.docx"
},
"documentType": "text",
"editorConfig": {
"callbackUrl": "https://example.com/url-to-callback.ashx"
}
});
保存文件的关键在于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 service,Document editing service会将修改内容发送到其他用户的Document editor。当其中一人结束编辑,会触发文件保存,所以在Document storage service内需要处理多人编辑时的保存逻辑。在example内,当一人结束编辑时不会直接修改文档源文件,而是临时保存一个document文件,当所有人都结束编辑时才会修改文档源文件。
协同编辑需要在调用js api时传入用户信息
new DocsAPI.DocEditor("placeholder", {
"document": {
"fileType": "docx",
"key": "Khirz6zTPdfd7",
"title": "Example Document Title.docx",
"url": "https://example.com/url-to-example-document.docx"
},
"documentType": "text",
"editorConfig": {
"user": {
"id": "78e1e841",
"name": "John Smith"
}
}
});
下载文档
https://api.onlyoffice.com/editors/conversion
文档历史
在js api内传入event handler可实现历史记录的查看,参考一下代码
var onRequestHistory = function() {
docEditor.refreshHistory({
"currentVersion": 2,
"history": [
{
"created": "2010-07-06 10:13 AM",
"key": "af86C7e71Ca8",
"user": {
"id": "F89d8069ba2b",
"name": "Kate Cage"
},
"version": 1
},
{
"created": "2010-07-07 3:46 PM",
"key": "Khirz6zTPdfd7",
"user": {
"id": "78e1e841",
"name": "John Smith"
},
"version": 2
},
...
]
});
};
var onRequestHistoryData = function(event) {
var version = event.data;
docEditor.setHistoryData({
"key": "Khirz6zTPdfd7",
"url": "https://example.com/url-to-example-document.docx",
"version": version
})
};
var docEditor = new DocsAPI.DocEditor("placeholder", {
"events": {
"onRequestHistory": onRequestHistory,
"onRequestHistoryData": onRequestHistoryData,
...
},
...
});
onRequestHistory
方法内调用编辑器实例的refreshHistory方法,初始化历史记录列表
onRequestHistoryData
方法内调用编辑器实例的setHistoryData方法,查看指定版本的文档。传参需要有文档链接、key和版本
获取历史记录需要Document storage service提供接口,同时保存文档时也要保存历史记录相关文件,具体可参考example内代码