1# Using the Delayed Copy and Paste Function of the Pasteboard 2 3## When to Use 4 5The [pasteboard service](../../reference/apis-basic-services-kit/js-apis-pasteboard.md) provides the capability of managing the pasteboard of the system to support the copy and paste functions of the system. 6 7When copy is performed repeatedly, redundant data is stored in the pasteboard cache, which increases the memory usage. To optimize the memory and support the paste of specified data types, the pasteboard provides the delayed copy and paste function. 8 9When a user copies data in an application that uses the delayed paste technology, the real data is not immediately written into the cache of the pasteboard service, but is obtained from the application when the data needs to be pasted. 10 11## Constraints 12 13- This function is supported when the pasteboard content size is less than 128 MB. 14 15## Available APIs 16 17| Name| Description | 18| -------- |----------------------------------------------------------------------| 19| setUnifiedData(data: udc.UnifiedData): Promise\<void> | Writes data of the unified data type to the system pasteboard. This API cannot be called in the same thread as **getUnifiedDataSync** when the delayed copy and paste function is used.| 20| setUnifiedDataSync(data: udc.UnifiedData): void | Writes data of the unified data type to the system pasteboard. This API returns the result synchronously and cannot be called in the same thread as **getUnifiedDataSync** when the delayed copy and paste function is used.| 21| getUnifiedData(): Promise\<udc.UnifiedData> | Reads data of the unified data type from the system pasteboard.| 22| getUnifiedDataSync(): udc.UnifiedData | Reads data of the unified data type from the pasteboard. This API returns the result synchronously and cannot be called in the same thread as **setUnifiedData** and **setUnifiedDataSync** when the delayed copy and paste function is used.| 23 24## How to Develop 25 261. Import the **pasteboard**, **unifiedDataChannel**, and **uniformTypeDescriptor** modules. 27 28 ```ts\ 29 import {unifiedDataChannel, uniformTypeDescriptor} from '@kit.ArkData'; 30 import {BusinessError, pasteboard} from '@kit.BasicServicesKit' 31 ``` 32 332. Construct a piece of PlainText data and write the function for obtaining the delay data. 34 35 ```ts 36 let plainTextData = new unifiedDataChannel.UnifiedData(); 37 let GetDelayPlainText = ((dataType:string) => { 38 let plainText = new unifiedDataChannel.PlainText(); 39 plainText.details = { 40 Key: 'delayPlaintext', 41 Value: 'delayPlaintext', 42 }; 43 plainText.textContent = 'delayTextContent'; 44 plainText.abstract = 'delayTextContent'; 45 plainTextData.addRecord(plainText); 46 return plainTextData; 47 }); 48 ``` 49 503. Save a piece of PlainText data to the system pasteboard. 51 52 ```ts 53 let SetDelayPlainText = (() => { 54 plainTextData.properties.shareOptions = unifiedDataChannel.ShareOptions.CROSS_APP; 55 // For cross-application use, set this parameter to CROSS_APP. For intra-application use, set this parameter to IN_APP. 56 plainTextData.properties.getDelayData = GetDelayPlainText; 57 pasteboard.getSystemPasteboard().setUnifiedData(plainTextData).then(()=>{ 58 // The data is successfully saved, which is a normal case. 59 }).catch((error: BusinessError) => { 60 // Error case 61 }); 62 }) 63 ``` 64 654. Read the text data from the system pasteboard. 66 67 ```ts 68 let GetPlainTextUnifiedData = (() => { 69 pasteboard.getSystemPasteboard().getUnifiedData().then((data) => { 70 let outputData = data; 71 let records = outputData.getRecords(); 72 if (records[0].getType() == uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 73 let record = records[0] as unifiedDataChannel.PlainText; 74 console.log('GetPlainText success, type:' + records[0].getType + ', details:' + 75 JSON.stringify(record.details) + ', textContent:' + record.textContent + ', abstract:' + record.abstract); 76 } else { 77 console.log('Get Plain Text Data No Success, Type is: ' + records[0].getType()); 78 } 79 }).catch((error: BusinessError) => { 80 // Error case 81 }) 82 }) 83 ``` 84