guide文档复制

# 概述

文档复制功能允许用户从活动的协作编辑会话或从 文档存储 中复制整个文档。文档的副本可以独立于原始文档进行编辑,但仍将包含来自源文档的所有评论和建议。

# 先决条件

  • 需要上传 编辑器捆绑包
  • 源文档需要在上传编辑器捆绑包后创建。

# 使用

复制文档意味着使用来自 CKEditor 协作服务器上已存在文档的数据创建一个新的协作编辑会话。这是通过向 POST /documents/{source_document_id} 方法 发送请求完成的,该请求来自我们的 REST API
您还可以使用此端点快速创建文档副本,而无需修改原始文档即可排除评论和建议标记。

# 示例

以下示例已在 Node.js 中准备。

  1. 运行以下命令
mkdir cs-copy-example && cd cs-copy-example && npm init -y && npm i axios && touch copy.js
  1. 打开 cs-copy-example/copy.js 并粘贴以下代码片段
const crypto = require( 'crypto' );
const axios = require( 'axios' );

// Update with your credentials and application endpoint
const environmentId = 'txQ9sTfqmXUyWU5LmDbr';
const apiSecret = '4zZBCQoPfRZ7Rr7TEnGAuRsGgbfF58Eg0PA8xcLD2kvPhjGjy4VGgB8k0hXn';
const applicationEndpoint = 'https://33333.cke-cs.com';
const apiEndpoint = `${ applicationEndpoint }/api/v5/${ environmentId }/documents/${ sourceDocumentId }`;

// Set source and target document id
const sourceDocumentId = 'my_document_id_source';
const targetDocumentId = 'my_document_id_target';

const body = {
    'target_document_id': targetDocumentId,
    'omit_comments': false,
    'omit_suggestions': false
};

const CSTimestamp = Date.now();
const config = {
   headers: {
      'X-CS-Timestamp': CSTimestamp,
      'X-CS-Signature': generateSignature( apiSecret, 'POST', apiEndpoint, CSTimestamp, body )
   },
};

axios.post( apiEndpoint, body, config )
   .then( response => {
      console.log ( response.status );
   } ).catch( error => {
      console.log( error.message );
      console.log( error.response.data );
   } );

function generateSignature( apiSecret, method, uri, timestamp, body ) {
   const url = new URL( uri );
   const path = url.pathname + url.search;
   const hmac = crypto.createHmac( 'SHA256', apiSecret );

   hmac.update( `${ method.toUpperCase() }${ path }${ timestamp }` );

   if ( body ) {
      hmac.update( Buffer.from( JSON.stringify( body ) ) );
   };

   return hmac.digest( 'hex' );
}
  1. 更新您的凭据,代码片段中的 sourceDocumentIdtargetDocumentId 值。

  2. 执行 node copy.js 命令。

成功响应后,状态代码为 201,您可以使用与片段中设置的相同的 targetDocumentId 打开编辑器,以查看复制的内容。

# 故障排除

  • 如果在复制过程中任何操作失败,例如在复制评论时失败,则目标文档的所有数据都将被撤销。源文档的数据不会改变。
  • 我们建议您启用 洞察面板,您将在其中找到复制过程的详细日志,包括任何失败请求的确切原因。

# 更多信息

您将在我们的 REST API 文档 中找到更多信息。