Handling copy and paste in certain mobile environments

更新时间:
复制 MD 格式

This document describes how to use a custom clipboard listener.

WebOffice cannot directly access the system clipboard in certain environments. If you can retrieve clipboard data using an external method, you can inject this data into the internal WebOffice clipboard. This enables you to paste external data.

Prerequisite: Get system clipboard data

/**
* The method to get system clipboard data is usually provided by the runtime environment.
* For example, a WeChat mini program provides wx.getClipboardData. This process is independent of WebOffice logic.
*/
const getData = () => {
  // Get clipboard data using a method from the external environment.
  // For example, let data = wx.getClipboardData(...)
  let data = {text: '123'}
  return data
}

Inject data when pasting

Use the getClipboardData parameter in the config object to retrieve data from the system clipboard in a mobile app.

When pasting into a document, pass the getClipboardData parameter. This calls the function you provide to retrieve data from the system clipboard. The function must return a Promise or an Object. The following code shows an example.

// Define the callback function to inject system clipboard data.
const listenPasteAndInsertData = () => {

  const clipboardData = getData() // Get clipboard data using an external method.
  return Promise.resolve({ 
    text: clipboardData.text,
    html: clipboardData.html,
    updateExternal: true,
  })
}


// Configure this as an initialization parameter.
aliyun.config({
  ...
  getClipboardData: listenPasteAndInsertData,
  ...
})