1# Using the Pasteboard to Copy and Paste 2 3## When to Use 4 5The [pasteboard](../../reference/apis-basic-services-kit/js-apis-pasteboard.md) allows you to copy and paste data. 6For example, you can copy text content and paste it to Notepad, or copy a photo from Gallery and paste it to Files. 7 8## Constraints 9 10- Pasteboard content size < 128 MB. 11- To ensure the accuracy of the pasteboard data, only one copy can be performed at a time. 12- Currently, the pasteboard supports the following data types: text, HTML, URI, Want, and PixelMap. 13- In API version 12 and later, [permission control](get-pastedata-permission-guidelines.md) is added to the pasteboard reading API to enhance user privacy protection. 14 15## Using a Unified Data Object 16 17To facilitate data interactions between the pasteboard and other applications and reduce the workload of data type adaptation, the pasteboard supports a unified data object for copying and pasting. For details about the unified data object, see [Unified Data Channel](../../reference/apis-arkdata/js-apis-data-unifiedDataChannel.md). 18 19You are advised to use the solution discussed here to implement the copy and paste functions for new applications. 20 21### Available APIs 22 23For details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#getunifieddata12). 24 25| Name| Description | 26| -------- |----------------------------------------------------------------------------------------------------------------------------------------| 27| setUnifiedData(data: udc.UnifiedData): Promise\<void\> | Writes the data of a unified data object to the system pasteboard. 28| setUnifiedDataSync(data: udc.UnifiedData): void | Writes the data of a unified data object to the system pasteboard. This API returns the result synchronously. | 29| getUnifiedData(): Promise\<udc.UnifiedData\> | Reads the data of a unified data object from the system pasteboard. | 30| getUnifiedDataSync(): udc.UnifiedData | Reads the data of a unified data object from the system pasteboard. This API returns the result synchronously. 31 32### Example 33```ts 34import {unifiedDataChannel, uniformTypeDescriptor} from '@kit.ArkData'; 35import {BusinessError, pasteboard} from '@kit.BasicServicesKit'; 36 37// Construct a piece of PlainText data and write the function for obtaining the delay data. 38let plainTextData = new unifiedDataChannel.UnifiedData(); 39let GetDelayPlainText = ((dataType:string) => { 40 let plainText = new unifiedDataChannel.PlainText(); 41 plainText.details = { 42 Key: 'delayPlaintext', 43 Value: 'delayPlaintext', 44 }; 45 plainText.textContent = 'delayTextContent'; 46 plainText.abstract = 'delayTextContent'; 47 plainTextData.addRecord(plainText); 48 return plainTextData; 49}); 50 51// Save a piece of PlainText data to the system pasteboard. 52let SetDelayPlainText = (() => { 53 plainTextData.properties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP; 54 // For cross-application use, set this parameter to CROSS_APP. For intra-application use, set this parameter to IN_APP. 55 plainTextData.properties.getDelayData = GetDelayPlainText; 56 pasteboard.getSystemPasteboard().setUnifiedData(plainTextData).then(()=>{ 57 // The data is successfully saved, which is a normal case. 58 }).catch((error: BusinessError) => { 59 // Error case 60 }); 61}) 62 63// Read the text data from the system pasteboard. 64let GetPlainTextUnifiedData = (() => { 65 pasteboard.getSystemPasteboard().getUnifiedData().then((data) => { 66 let outputData = data; 67 let records = outputData.getRecords(); 68 if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 69 let record = records[0] as unifiedDataChannel.PlainText; 70 console.log('GetPlainText success, type:' + records[0].getType + ', details:' + 71 JSON.stringify(record.details) + ', textContent:' + record.textContent + ', abstract:' + record.abstract); 72 } else { 73 console.log('Get Plain Text Data No Success, Type is: ' + records[0].getType()); 74 } 75 }).catch((error: BusinessError) => { 76 // Error case 77 }) 78}) 79``` 80 81## Using Basic Types 82 83### Available APIs 84 85For details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-pasteboard.md#getdata9). 86 87After obtaining URI data using the **getData** API, use the [fs.copy](../../reference/apis-core-file-kit/js-apis-file-fs.md#fscopy11) API of File Manager to obtain the file. 88 89| Name| Description | 90| -------- |----------------------------------------------------------------------------------------------------------------------------------------| 91| setData(data: PasteData, callback: AsyncCallback<void>): void | Writes a **PasteData** object to the pasteboard. This API uses an asynchronous callback to return the result.| 92| setData(data: PasteData): Promise<void> | Writes a **PasteData** object to the pasteboard. This API uses a promise to return the result.| 93| getData( callback: AsyncCallback<PasteData>): void | Reads a **PasteData** object from the pasteboard. This API uses an asynchronous callback to return the result.| 94| getData(): Promise<PasteData> | Reads a **PasteData** object from the pasteboard. This API uses a promise to return the result.| 95| getDataSync(): PasteData | Reads data from the system pasteboard. This API returns the result synchronously.| 96 97### Example 98```ts 99import {BusinessError, pasteboard} from '@kit.BasicServicesKit'; 100// Obtain the system pasteboard object. 101let text = "test"; 102// Create a pasteboard content object of the plain text type. 103let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text); 104// Write data to the system pasteboard. 105let systemPasteboard = pasteboard.getSystemPasteboard(); 106await systemPasteboard.setData(pasteData); 107// Read data from the system pasteboard. 108systemPasteboard.getData().then((data) => { 109 let outputData = data; 110 // Obtain the number of records from the pasteboard. 111 let recordCount = outputData.getRecordCount(); 112 // Obtain the corresponding record information from the pasteboard data. 113 for (let i = 0; i < recordCount; i++) { 114 let record = outputData.getRecord(i).toPlainText(); 115 console.log('Get data success, record:' + record); 116 } 117}).catch((error: BusinessError) => { 118 // Error case 119}) 120``` 121