Signed-off-by: swx1285818 <suoqilong@huawei.com>
50 KiB
剪贴板服务
简介
剪贴板服务作为杂散子系统的功能组件,提供管理系统剪贴板的能力,为系统复制、粘贴功能提供支持。系统剪切板支持包文本、超本文、URIs等内容操作。
剪贴板服务,提供支撑应用开发者方便、高效的使用剪贴板相关业务的功能。其主要组件包括剪贴板管理客户端和剪贴板服务。剪贴板管理客户端负责剪贴板接口管理,提供剪贴板北向JS API给应用;在应用框架侧创建剪贴板数据、请求剪贴板SA执行剪贴板的新建、删除、查询、转换文本、配置等。剪贴板服务负责剪贴板事件管理,管理剪贴板SA的生命周期(启动、销毁、多用户等);执行应用请求,通知剪贴板数据管理,并将结果返回给剪贴板管理客户端。
目录
/foundation/distributeddatamgr/pasteboard
├── etc # 组件包含的进程的配置文件
├── figures # 构架图
├── framework # innerKit接口
├── interfaces # 组件对外提供的接口代码
│ └── kits # 对应用提供的接口
├── profile # 组件包含的系统服务的配置文件
├── services # 剪贴板服务实现
│ └── core # 核心代码实现
│ └── test # native测试代码
│ └── zidl # 跨进程通信代码实现
├── utils # 测试或服务使用mock的数据
└──README_zh.md # 使用说明
说明
接口说明
表 1 PasteBoard开放的主要方法
createRecord(mimeType: string, value: ValueType): PasteDataRecord; |
|
表 2 SystemPasteboard开放的主要方法
setData(data: PasteData, callback: AsyncCallback<void>): void; |
|
setPasteData(data: PasteData, callback: AsyncCallback<void>:void; |
|
表 3 PasteData开放的主要方法
表 4 PasteDataRecord开放的主要方法
convertToTextV9(): Promise<string>; |
|
表 5 PasteDataProperty参数说明
[ShareOption]( |
表 6 pasteboard主要属性说明
表 7 PasteDataRecord主要属性说明
表 8 ShareOption枚举类型说明
使用说明
剪贴板模块使用示例:
// 导入模块
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);
});
相关仓
分布式数据管理子系统