# pasteBoardService ## Introduction ​ As a functional component of the stray subsystem, the clipboard service provides the ability to manage the system clipboard and supports the system copy and paste functions. The system clipboard supports package text, hypertext, URIs and other content operations. **picture 1** Subsystem Architecture Diagram ![](figures/subsystem_architecture.png "Subsystem architecture") ​ The clipboard service provides functions that support application developers to use clipboard-related services conveniently and efficiently. Its main components include clipboard management client and clipboard service. The clipboard management client is responsible for clipboard interface management, providing clipboard northbound JS API to applications; creating clipboard data on the application framework side, requesting clipboard SA to perform clipboard creation, deletion, query, text conversion, configuration, etc. The clipboard service is responsible for clipboard event management, manages the life cycle of clipboard SA (startup, destruction, multi-user, etc.); executes application requests, notifies clipboard data management, and returns the results to the clipboard management client. ## Directory Structure ``` /foundation/distributeddatamgr/pasteboard ├── etc # Configuration files for the processes contained in the component ├── figures # Framework diagram ├── framework # innerKit interface ├── interfaces # The interface code provided by the component externally │ └── kits # Interface provided to the application ├── profile # Configuration files for system services contained in the component ├── services # clipboard service implementation │ └── core # Core code implementation │ └── test # native test code │ └── zidl # Cross-process communication code implementation ├── utils # Tests or services use mocked data └──README.md # Instructions for use ``` ## illustrate ### Interface Description **list 1** PasteBoard open main method

interface name

describe

createHtmlData(htmlText: string): PasteData;

Create a PasteData object of type MIMETYPE_TEXT_HTML for HTML type data

createWantData(want: Want): PasteData;

Create a PasteData object of type MIMETYPE_TEXT_WANT for data of type want

createPlainTextData(text: string): PasteData

Create a PasteData object of type MIMETYPE_TEXT_PLAIN for plain text data

createUriData(uri: string): PasteData;

Create a PasteData object of type MIMETYPE_TEXT_URI for data of type URI

createHtmlTextRecord(htmlText: string): PasteDataRecord;

Create a PasteDataRecord object of type RecordMIMETYPE_TEXT_HTML for hypertext type data

createWantRecord(want: Want): PasteDataRecord;

Create a PasteDataRecord object of type MIMETYPE_TEXT_WANT for data of type want

createPlainTextRecord(text: string): PasteDataRecord;

Create a PasteDataRecord object of type MIMETYPE_TEXT_PLAIN for plain text data

createUriRecord(uri: string): PasteDataRecord;

Create a PasteDataRecord object of type MIMETYPE_TEXT_URI for data of type URI

getSystemPasteboard(): SystemPasteboard

Get system clipboard

**list 2** SystemPasteboard open main method

interface name

describe

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

Callback called when the open pasteboard content changes

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

Callback called when the content of the pasteboard is closed

clear(callback: AsyncCallback): void

clear clipboard

clear(): Promise;

clear clipboard

getPasteData(callback: AsyncCallback<PasteData>): void

Get system clipboard data object

getPasteData():Promise<PasteData>

Get system clipboard data object

hasPasteData(callback: AsyncCallback<boolean>:void;

Check if there is content in the pasteboard

hasPasteData(): Promise<boolean>

Check if there is content in the pasteboard

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

Write PasteData to the pasteboard

setPasteData(data: PasteData): Promise<void>

Write PasteData to the pasteboard

**list 3** PasteData open main method

interface name

describe

addHtmlRecord(htmlText: string): void;

Add the HTML text record to the PasteData object and update the MIME type to PasteData#MIMETYPE_TEXT_HTML in the DataProperty.

addWantRecord(want: Want): void;

Add want record to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_WANT in DataProperty

addRecord(record: PasteDataRecord): void;

Add PasteRecord to paste data object and update MIME type in data attribute

addTextRecord(text: string): void;;

Add plain text records to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_PLAIN in DataProperty

addUriRecord(uri: string): void;

Add URI record to PasteData object and update MIME type to PasteData#MIMETYPE_TEXT_URI in DataProperty

getMimeTypes(): Array<string>

MIME type of everything on the pasteboard

getPrimaryHtml(): string;

HTML text of the main record in the PasteData object

getPrimaryWant(): Want;

The want of the main record in the PasteData object

getPrimaryMimeType(): string;

The MIME type of the main record in the PasteData object

getPrimaryUri(): string;

URI of the main record in the PasteData object

getProperty(): PasteDataProperty;

Get the properties of the clipboard data object

getRecordAt(index: number): PasteDataRecord;

records based on the specified index

getRecordCount(): number;

Number of records in PasteData object

hasMimeType(mimeType: string): boolean;

Checks if data of the specified MIME type exists in the DataProperty

removeRecordAt(index: number): boolean;

Delete records based on specified index

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

Replace the specified record with a new record

**list 4** PasteDataRecord open main method

interface name

describe

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

Convert PasteData to text content

convertToText(): Promise<void>

Convert PasteData to text content

**list 5** PasteDataProperty Parameter Description

name

type

illustrate

additions{[key:string]}

object

Additional property data key-value pair

mimeTypes

Array

Distinct MIME types for all records in PasteData

tag

string

User-defined labels for PasteData objects

timestamp

number

Timestamp indicating when the data was written to the system clipboard.

localOnly

boolean

Check if PasteData is set for local access only.

### Instructions for use Clipboard module usage example: ``` // import module import pasteboard from '@ohos.pasteboard' //text copy 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) //clipboard change listener 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) ``` ## Related warehouse **Distributed Data Management subsystem** [distributeddatamgr\_pasteboard](https://gitee.com/openharmony/distributeddatamgr_pasteboard)