diff --git a/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-pasteboard.md b/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-pasteboard.md index 3212f8bd9a0d658de3a042c09ab949ad2c158a3c..c4fcd76234a65fe7a46cab70d762b2d167c6f130 100644 --- a/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-pasteboard.md +++ b/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-pasteboard.md @@ -1516,6 +1516,195 @@ systemPasteboard.hasData((err: BusinessError, data: boolean) => { }); ``` + + + + + + + +### setUnifiedData12+ + +setData(data: unifiedDataChannel.UnifiedData): Promise<void> + +将数据写入系统剪贴板,使用Promise异步回调。 + +**系统能力:** SystemCapability.MiscServices.Pasteboard + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------- | -------- | -------- | +| data | unifiedDataChannel.UnifiedData | 是 | UnifiedData对象。 | + +**返回值:** + +| 类型 | 说明 | +| -------- | -------- | +| Promise<void> | 无返回结果的Promise对象。 | + +**错误码:** + +以下错误码的详细介绍请参见[剪贴板错误码](errorcode-pasteboard.md)。 + +| 错误码ID | 错误信息 | +| -------- | -------- | +| 12900003 | Another copy or paste is in progress. | +| 12900004 | Replication is prohibited. | + +**示例:** + +```ts +import { BusinessError } from '@ohos.base'; +import pasteboard from '@ohos.pasteboard.d.ts'; +import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; + +let plainTextData = new unifiedDataChannel.unifiedData(); +let plainText = new unifiedDataChannel.PlainText(); +plainText.details = { + Key: 'delayPlaintext', + Value: 'delayPlaintext', +}; +plainText.textContext = 'delayTextContent'; +plainText.abstract = 'delayTextContent'; +plainTextData.addRecord(plainText); + +let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); +systemPasteboard.setUnifiedData(plainTextData).then((data: void) => { + console.info('Succeeded in setting PasteData.'); +}).catch((err: BusinessError) => { + console.error('Failed to set PasteData. Cause: ' + err.message); +}); +``` + +### getUnifiedData12+ + +getUnifiedData(): Promise<PasteData> + +读取系统剪贴板内容,使用Promise异步回调。 + +**需要权限**:ohos.permission.READ_PASTEBOARD + +**系统能力:** SystemCapability.MiscServices.Pasteboard + +**返回值:** + +| 类型 | 说明 | +| -------- | -------- | +| Promise<[UnifiedData](#pastedata)> | Promise对象,返回系统剪贴板数据。 | + +**错误码:** + +以下错误码的详细介绍请参见[剪贴板错误码](errorcode-pasteboard.md)。 + +| 错误码ID | 错误信息 | +| -------- | -------- | +| 12900003 | Another copy or paste is in progress. | + +**示例:** + +```ts +import { BusinessError } from '@ohos.base'; +import pasteboard from '@ohos.pasteboard.d.ts'; +import unifiedDataChannel from '@ohos.data.unifiedDataChannel'; + +let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); +systemPasteboard.getUnifiedData().then((data: void) => { + let records = data.getRecords(); + for (let j = 0; j < records.length; j++) { + if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { + let text = records[j] as unifiedDataChannel.PlainText; + console.info(`${i + 1}.${text.textContent}`); + } + } +}).catch((err: BusinessError) => { + console.error('Failed to get UnifiedData. Cause: ' + err.message); +}); +``` + + +### getUnifiedDataSync12+ + +getUnifiedDataSync(): UnifiedData + +读取系统剪贴板内容, 此接口为同步接口。 + +**需要权限**:ohos.permission.READ_PASTEBOARD + +**系统能力:** SystemCapability.MiscServices.Pasteboard + +**返回值:** + +| 类型 | 说明 | +| ----------------------- | -------------------- | +| [UnifiedDataSync](#pastedata) | 返回系统剪贴板数据。 | + +**错误码:** + +以下错误码的详细介绍请参见[剪贴板错误码](errorcode-pasteboard.md)。 + +| 错误码ID | 错误信息 | +| -------- | -------- | +| 12900005 | Request time out. | + +**示例:** + +```ts +let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); +try { + let result: unifiedDataChannel.UnifiedData = systemPasteboard.getUnifiedDataSyncSync(); + console.info('Succeeded in getting PasteData.'); +} catch (err) { + console.error('Failed to get PasteData. Cause:' + err.message); +}; +``` + +### setUnifiedDataSync12+ + +setUnifiedDataSync(data: UnifiedData): void + +将数据写入系统剪贴板, 此接口为同步接口。 + +**系统能力:** SystemCapability.MiscServices.Pasteboard + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ----------------------- | ---- | ---------------- | +| data | [UnifiedData](#pastedata) | 是 | 需要写入剪贴板中的数据。 | + +**错误码:** + +以下错误码的详细介绍请参见[剪贴板错误码](errorcode-pasteboard.md)。 + +| 错误码ID | 错误信息 | +| -------- | -------- | +| 401 | Type of data is not UnifiedData. | +| 12900005 | Request time out. | + +**示例:** + +```ts +let plainTextData = new unifiedDataChannel.unifiedData(); +let plainText = new unifiedDataChannel.PlainText(); +plainText.details = { + Key: 'delayPlaintext', + Value: 'delayPlaintext', +}; +plainText.textContext = 'delayTextContent'; +plainText.abstract = 'delayTextContent'; +plainTextData.addRecord(plainText); + +let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); +try { + systemPasteboard.setUnifiedDataSync(pasteData); + console.info('Succeeded in setting PasteData.'); +} catch (err) { + console.error('Failed to set PasteData. Cause:' + err.message); +}; +``` + + ### hasData9+ hasData(): Promise<boolean>