• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Uniform Data Structs (ArkTS)
2
3
4## When to Use
5
6Uniform data structs are provided to define data of common [uniform data types](../reference/apis-arkdata/js-apis-data-uniformTypeDescriptor.md#uniformdatatype). For example, the data struct for the system-defined home screen icon (the uniform data type is **openharmony.app-item**) is provided to help you easily define the data.
7
8Applications can directly use the uniform data structs in certain scenarios. For example, in the drag-and-drop operation across applications, you can write the data (encapsulated in a uniform struct) to be dragged to [DragEvent](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#dragevent7). The target application (application requesting the data) reads the data from **DragEvent** and parses the data in the uniform data struct. Using uniform data structs for data interaction between applications effectively reduces the development workload in your application experience.
9
10## Uniform Data Structs
11
12The following table lists the uniform data structs provided by the UDMF.
13
14| Data Struct                                                                                               |       Data Type       | Description  |
15|-----------------------------------------------------------------------------------------------------| :-------------------: |------|
16| [PlainText](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#plaintext)                  |      'general.plain-text'        | Plain text. |
17| [Hyperlink](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#hyperlink)                  |       'general.hyperlink'       | Hyperlink. |
18| [HTML](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#html)                            |         'general.html'          | Rich text. |
19| [OpenHarmonyAppItem](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#openharmonyappitem) | 'openharmony.app-item'    | Icon.  |
20| [ContentForm](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#contentform14)            | 'general.content-form'    | Content widget.|
21
22## How to Develop
23
24The following describes how to use the uniform data structs to define a hyperlink and a plaint text.
25
26The data provider can call **addRecord()** provided by the UMDF to add data records and call **getRecords()** to obtain all data records in the current data object.
27
28  ```ts
29  // 1. Import the unifiedDataChannel and uniformTypeDescriptor modules.
30  import { uniformDataStruct, uniformTypeDescriptor, unifiedDataChannel } from '@kit.ArkData';
31
32  // 2. Create a data record for a hyperlink.
33  let hyperlinkDetails : Record<string, string> = {
34    'attr1': 'value1',
35    'attr2': 'value2',
36  }
37  let hyperlink : uniformDataStruct.Hyperlink = {
38    uniformDataType:'general.hyperlink',
39    url : 'www.XXX.com',
40    description : 'This is the description of this hyperlink',
41    details : hyperlinkDetails,
42  }
43
44  hyperlink.description = '...'; // Set description of the hyperlink.
45
46  console.info(`hyperlink url = ${hyperlink.url}`); // Access object attributes.
47
48  // 3. Create a data record for a plain text and add it to the UnifiedData instance created.
49  let plainTextDetails : Record<string, string> = {
50    'attr1': 'value1',
51    'attr2': 'value2',
52  }
53  let plainText : uniformDataStruct.PlainText = {
54    uniformDataType: 'general.plain-text',
55    textContent : 'This is plainText textContent example',
56    abstract : 'this is abstract',
57    details : plainTextDetails,
58  }
59  // 4. Create a UnifiedData instance.
60  let unifiedData = new unifiedDataChannel.UnifiedData();
61  let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
62  let plainTextRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText);
63
64  // 5. Add a plainText data record.
65  unifiedData.addRecord(hyperlinkRecord);
66  unifiedData.addRecord(plainTextRecord);
67
68  // 6. Obtain all data records in this UnifiedData instance.
69  let records = unifiedData.getRecords();
70
71  // 7. Traverse all records, determine the data type of the record, and convert the record into a child class object to obtain the original data record.
72  for (let i = 0; i < records.length; i ++) {
73    let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord;
74    let record = unifiedDataRecord.getValue() as object;
75    if (record != undefined) {
76      // Obtain the type of each data record.
77      let type : string = record["uniformDataType"];
78      switch (type) {
79        case uniformTypeDescriptor.UniformDataType.HYPERLINK:
80          Object.keys(record).forEach(key => {
81            console.info('show records: ' + key + ', value:' + record[key]);
82          });
83          break;
84        case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
85          Object.keys(record).forEach(key => {
86            console.info('show records: ' + key + ', value:' + record[key]);
87          });
88          break;
89        default:
90          break;
91      }
92    }
93  }
94  ```
95