distributeddatamgr_pasteboard:基于 OpenHarmony 的剪贴板服务项目

Pasteboard System Ability | 剪贴板服务

分支287Tags45
文件最后提交记录最后更新时间
1 年前
10 天前
9 天前
4 个月前
3 年前
3 天前
21 天前
7 个月前
2 天前
4 天前
1 个月前
4 个月前
1 年前
2 年前
1 年前
2 年前
4 年前
2 年前
2 年前
2 年前
4 天前
15 天前
3 年前

Clipboard Service

Introduction

As a functional component of the miscellaneous subsystem, the Clipboard Service provides the capability to manage the system clipboard, supporting copy and paste functionalities. The system clipboard handles operations for text, hypertext, URIs, and other content types.

Figure 1 Subsystem Architecture Diagram

The Clipboard Service offers functionalities to assist application developers in conveniently and efficiently utilizing clipboard-related operations. Its main components include the Clipboard Management Client and the Clipboard Service.

  • The Clipboard Management Client is responsible for managing clipboard interfaces, providing northbound JS APIs to applications. On the application framework side, it creates clipboard data and requests the Clipboard SA (Service Ability) to perform operations such as creating, deleting, querying, converting text, and configuring the clipboard.
  • The Clipboard Service manages clipboard events and oversees the lifecycle of the Clipboard SA (startup, destruction, multi-user support, etc.). It executes application requests, notifies clipboard data management, and returns results to the Clipboard Management Client.

Directory

/foundation/distributeddatamgr/pasteboard
├── etc                      # 组件包含的进程的配置文件
├── figures                  # 构架图
├── framework                # innerKit接口
├── interfaces               # 组件对外提供的接口代码
│   └── kits                 # 对应用提供的接口
├── profile                  # 组件包含的系统服务的配置文件
├── services                 # 剪贴板服务实现
│    └── core                # 核心代码实现
│    └── test                # native测试代码
│    └── zidl                # 跨进程通信代码实现
├── utils                    # 测试或服务使用mock的数据
└──README_zh.md              # 使用说明

Description

Interface Description

Table 1 Main Methods Exposed by PasteBoard

Method Name

Description

createData(mimeType: string, value: ValueType): PasteData;

Creates a PasteData object with the specified MIME type and value

createRecord(mimeType: string, value: ValueType): PasteDataRecord;

Creates a PasteDataRecord object with the specified MIME type and value

createHtmlData(htmlText: string): PasteData;

Creates a PasteData object of type MIMETYPE_TEXT_HTML for HTML data

createWantData(want: Want): PasteData;

Creates a PasteData object of type MIMETYPE_TEXT_WANT for Want data

createPlainTextData(text: string): PasteData;

Creates a PasteData object of type MIMETYPE_TEXT_PLAIN for plain text data

createUriData(uri: string): PasteData;

Creates a PasteData object of type MIMETYPE_TEXT_URI for URI data

createHtmlTextRecord(htmlText: string): PasteDataRecord;

Creates a PasteDataRecord object of type RecordMIMETYPE_TEXT_HTML for hypertext data

createWantRecord(want: Want): PasteDataRecord;

Creates a PasteDataRecord object of type MIMETYPE_TEXT_WANT for Want data

createPlainTextRecord(text: string): PasteDataRecord;

Creates a PasteDataRecord object of type MIMETYPE_TEXT_PLAIN for plain text data

createUriRecord(uri: string): PasteDataRecord;

Creates a PasteDataRecord object of type MIMETYPE_TEXT_URI for URI data

getSystemPasteboard(): SystemPasteboard;

Retrieves the system clipboard

Table 2 Main Methods Exposed by SystemPasteboard

接口名称

功能说明

clearData(callback: AsyncCallback<void>): void;

清空剪贴板内容

clearData(): Promise<void>;

清空剪贴板内容

getData(callback: AsyncCallback<PasteData>): void;

获取系统剪贴板中的PasteData对象

getData(): Promise<PasteData>;

获取系统剪贴板中的PasteData对象

hasData(callback: AsyncCallback<boolean>): void;

检测剪贴板是否含有内容

hasData(): Promise<boolean>;

检测剪贴板是否含有内容

setData(data: PasteData, callback: AsyncCallback<void>): void;

将PasteData写入系统剪贴板

setData(data: PasteData): Promise<void>;

将PasteData写入系统剪贴板

on(type:'update', callback: () => void): void;

注册剪贴板内容变更回调

off(type: 'update', callback?: () => void): void;

注销剪贴板内容变更回调

clear(callback: AsyncCallback<void>): void;

清空剪贴板内容

clear(): Promise<void>;

清空剪贴板内容

getPasteData(callback: AsyncCallback<PasteData>): void;

获取剪贴板数据对象

getPasteData():Promise<PasteData>;

获取剪贴板数据对象

hasPasteData(callback: AsyncCallback<boolean>:void;

检查剪贴板是否含有数据

hasPasteData(): Promise<boolean>;

检查剪贴板是否含有数据

setPasteData(data: PasteData, callback: AsyncCallback<void>:void;

向剪贴板写入PasteData数据

setPasteData(data: PasteData): Promise<void>;

向剪贴板写入PasteData数据

表 3 PasteData核心方法一览

接口名称

功能说明

addRecord(mimeType: string, value: ValueType): void;

向PasteData对象添加指定MIME类型及对应值

getRecord(index: number): PasteDataRecord;

按索引获取PasteData中的记录项

hasType(mimeType: string): boolean;

检测DataProperty是否包含指定MIME类型数据

removeRecord(index: number): void;

根据索引删除PasteData中的记录

replaceRecord(index: number, record: PasteDataRecord): void;

替换PasteData中指定索引的记录

addHtmlRecord(htmlText: string): void;

添加HTML文本记录,并更新MIME类型为PasteData#MIMETYPE_TEXT_HTML

addWantRecord(want: Want): void;

添加Want记录,并更新MIME类型为PasteData#MIMETYPE_TEXT_WANT

addRecord(record: PasteDataRecord): void;

添加粘贴记录并更新数据属性中的MIME类型

addTextRecord(text: string): void;

添加纯文本记录,并更新MIME类型为PasteData#MIMETYPE_TEXT_PLAIN

addUriRecord(uri: string): void;

添加URI记录,并更新MIME类型为PasteData#MIMETYPE_TEXT_URI

getMimeTypes(): Array<string>;

获取剪贴板内容的所有MIME类型

getPrimaryHtml(): string;

获取主记录的HTML文本内容

getPrimaryWant(): Want;

获取主记录的Want对象

getPrimaryMimeType(): string;

获取主记录的MIME类型

getPrimaryUri(): string;

获取主记录的URI地址

getPrimaryPixelMap(): image.PixelMap;

获取主记录的PixelMap图像数据

getProperty(): PasteDataProperty;

获取剪贴板数据属性对象

setProperty(property: PasteDataProperty): void;

设置属性描述对象(当前仅支持设置shareOption属性)

getRecordAt(index: number): PasteDataRecord;

通过索引获取指定记录

getRecordCount(): number;

获取记录总数

hasMimeType(mimeType: string): boolean;

检查是否存在指定MIME类型的数据

removeRecordAt(index: number): boolean;

按索引删除记录

replaceRecordAt(index: number, record: PasteDataRecord): boolean;

替换指定位置的记录为新记录

**表 4** PasteDataRecord开放的主要方法

接口名称

功能描述

convertToTextV9(callback: AsyncCallback<string>): void;

将PasteData内容转换为文本格式

convertToTextV9(): Promise<string>;

将PasteData内容转换为文本格式

convertToText(callback: AsyncCallback<string>): void;

将PasteData内容转换为文本

convertToText(): Promise<void>;

将PasteData内容转换为文本

表 5 PasteDataProperty参数说明

参数名称

参数类型

参数说明

additions{[key:string]}

object

附加属性键值对数据

mimeTypes

Array

PasteData中所有记录的去重MIME类型集合

tag

string

PasteData对象的用户自定义标签

timestamp

number

时间戳,标识数据写入系统剪贴板的时间

localOnly

boolean

检查PasteData是否设置为仅限本地访问

shareOption

[ShareOption](https://gitee.com/openharmony/docs/blob/a4ead7d2fe7ac381133ba47c70c9df8cdfa95325/zh-cn/application-dev/reference/apis/js-apis-pasteboard.md#shareoption9)

标识剪贴板数据可被粘贴的范围

表 6 剪贴板核心属性说明

Name

Type

Description

MAX_RECORD_NUM

number

Maximum number of Records that can be contained in a single PasteData.

MIMETYPE_TEXT_HTML

string

MIME type definition for HTML text.

MIMETYPE_TEXT_WANT

string

MIME type definition for Want.

MIMETYPE_TEXT_PLAIN

string

MIME type definition for plain text.

MIMETYPE_TEXT_URI

string

MIME type definition for URI text.

MIMETYPE_PIXELMAP

string

MIME type definition for PixelMap.

ValueType

string | image.PixelMap | Want | ArrayBuffer

Type definition for value.

Table 7 Description of PasteDataRecord Key Attributes

Name

Type

Description

htmlText

string

HTML text content.

want

[Want](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-application-want.md)

Want text content.

mimeType

string

Data type.

plainText

string

Text content.

uri

string

URI content.

pixelMap

[image.PixelMap](https://gitee.com/openharmony/docs/blob/a4ead7d2fe7ac381133ba47c70c9df8cdfa95325/zh-cn/application-dev/reference/apis/js-apis-image.md#pixelmap7)

PixelMap content.

Table 8 ShareOption Enumeration Description

Name

Description

InApp

InApp indicates that pasting is only allowed within the same application.

LocalDevice

LocalDevice indicates that pasting is only allowed on this device.

Usage Instructions

Example of using the clipboard module:

// 导入模块
import pasteboard from '@ohos.pasteboard'

//文本拷贝
console.log('Get SystemPasteboard')
var systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.clear()
        
var textData = 'Hello World!'
console.log('createPlainTextData = ' + textData)
var pasteData = pasteboard.createPlainTextData(textData)
        
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
        
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
        
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
        
console.log('Checks the pasteboard content')
assert.equal(pasteData.getPrimaryText(), textData)
        
console.log('Checks there is a MIMETYPE_TEXT_PLAIN MIME type of data')
assert.equal(pasteData.hasMimeType(MIMETYPE_TEXT_PLAIN), true)
assert.equal(pasteData.getPrimaryMimeType(), MIMETYPE_TEXT_PLAIN)

//剪贴板变化监听
console.log('Off the content changes')
var systemPasteboard = pasteboard.getSystemPasteboard()
systemPasteboard.off(contentChanges)
systemPasteboard.clear()
        
var textData = 'Hello World!'
console.log('createUriData = ' + textData)
var pasteData = pasteboard.createUriData(textData)
        
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
        
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
        
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
        
console.log('On the content changes')
systemPasteboard.on(contentChanges)
        
console.log('Removes the Record')
assert.equal(pasteData.removeRecordAt(0), true)
        
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
        
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 0)
        
console.log('Checks there is  no content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), false)
        
var textDataNew = 'Hello World!-New'
console.log('createUriData = ' + textDataNew)
var pasteData = pasteboard.createUriData(textDataNew)
        
console.log('Writes PasteData to the pasteboard')
systemPasteboard.setPasteData(pasteData)
        
console.log('Checks there is content in the pasteboard')
assert.equal(systemPasteboard.hasPasteData(), true)
        
console.log('Checks the number of records')
pasteData = systemPasteboard.getPasteData()
assert.equal(pasteData.getRecordCount(), 1)
        
console.log('Checks the pasteboard content')
assert.equal(pasteData.getRecordAt(0).plainText, textDataNew)

//构建一个自定义类型的剪贴板内容对象
var dataXml = new ArrayBuffer(256);
var pasteData = pasteboard.createData('app/xml', dataXml);

//创建一条自定义数据内容条目
var dataXml = new ArrayBuffer(256);
var pasteDataRecord = pasteboard.createRecord('app/xml', dataXml);

//将一个PasteData中的内容强制转换为文本内容,使用callback异步回调
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
record.convertToTextV9((err, data) => {    
    if (err) {        
        console.error('Failed to convert to text. Cause: ' + JSON.stringify(err));        
        return;   
      }
    console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data));
});

//将一个PasteData中的内容强制转换为文本内容,使用Promise异步回调
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
record.convertToTextV9().then((data) => {
    console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data));
}).catch((err) => {
    console.error('Failed to convert to text. Cause: ' + JSON.stringify(err));
});

//向当前剪贴板内容中添加一条自定义数据内容条目
var pasteData = pasteboard.createUriData("dataability:///com.example.myapplication1/user.txt");
var dataXml = new ArrayBuffer(256);
pasteData.addRecord('app/xml', dataXml);

//获取剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
var record = pasteData.getRecord(0);

//检查剪贴板内容中是否有指定的MIME数据类型
var pasteData = pasteboard.createPlainTextData("hello");
var hasType = pasteData.hasType(pasteboard.MIMETYPE_TEXT_PLAIN);

//移除剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
pasteData.removeRecord(0);

//替换剪贴板内容中指定下标的条目
var pasteData = pasteboard.createPlainTextData("hello");
var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt");
pasteData.replaceRecord(0, record);

//清空系统剪贴板内容,使用callback异步回调
systemPasteboard.clearData((err, data) => { 
    if (err) {        
        console.error('Failed to clear the pasteboard. Cause: ' + JSON.stringify(err));
        return;    
    }
    console.info('Succeeded in clearing the pasteboard.');
});

//清空系统剪贴板内容,使用Promise异步回调
systemPasteboard.clearData().then((data) => { 
    console.info('Succeeded in clearing the pasteboard.');
}).catch((err) => {    
    console.error('Failed to clear the pasteboard. Cause: ' + JSON.stringify(err));
});

//读取系统剪贴板内容,使用callback异步回调
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err, pasteData) => {  
    if (err) {
        console.error('Failed to get PasteData. Cause: ' + err.message);
        return;
    }
    var text = pasteData.getPrimaryText();
});

//读取系统剪贴板内容,使用Promise异步回调
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData().then((pasteData) => { 
    var text = pasteData.getPrimaryText();
}).catch((err) => {
    console.error('Failed to get PasteData. Cause: ' + err.message);
})

//判断系统剪贴板中是否有内容,使用callback异步回调
systemPasteboard.hasData((err, data) => {
    if (err) {
        console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data));
});

//判断系统剪贴板中是否有内容,使用Promise异步回调
systemPasteboard.hasData().then((data) => { 
    console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data));
}).catch((err) => {
    console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err));
});

//将数据写入系统剪贴板,使用callback异步回调
var pasteData = pasteboard.createPlainTextData("content");
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.setData(pasteData, (err, data) => { 
    if (err) {
        console.error('Failed to set PasteData. Cause: ' + err.message);
        return;
    }
    console.info('Succeeded in setting PasteData.');
});

//将数据写入系统剪贴板,使用Promise异步回调
var pasteData = pasteboard.createPlainTextData("content");
var systemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.setData(pasteData).then((data) => {
    console.info('Succeeded in setting PasteData.');
}).catch((err) => {
    console.error('Failed to set PasteData. Cause: ' + err.message);
});

Distributed Data Management Subsystem

distributeddatamgr_pasteboard

项目介绍

Pasteboard System Ability | 剪贴板服务

定制我的领域