• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Uniform Data Structs
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#dragevent). 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 app 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'          | HyperText Markup Language (HTML). |
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
26  ```ts
27  // 1. Import the unifiedDataChannel and uniformTypeDescriptor modules.
28  import { uniformDataStruct, uniformTypeDescriptor, unifiedDataChannel } from '@kit.ArkData';
29
30  // 2. Create a data record for a hyperlink.
31  let hyperlinkDetails : Record<string, string> = {
32    'attr1': 'value1',
33    'attr2': 'value2',
34  }
35  let hyperlink : uniformDataStruct.Hyperlink = {
36    uniformDataType:'general.hyperlink',
37    url : 'www.XXX.com',
38    description : 'This is the description of this hyperlink',
39    details : hyperlinkDetails,
40  }
41
42  hyperlink.description = '...'; // Set description of the hyperlink.
43
44  console.info(`hyperlink url = ${hyperlink.url}`); // Access object attributes.
45
46  // 3. Create a data record for a plain text and add it to the UnifiedData instance created.
47  let plainTextDetails : Record<string, string> = {
48    'attr1': 'value1',
49    'attr2': 'value2',
50  }
51  let plainText : uniformDataStruct.PlainText = {
52    uniformDataType: 'general.plain-text',
53    textContent : 'This is plainText textContent example',
54    abstract : 'this is abstract',
55    details : plainTextDetails,
56  }
57  // 4. Create a UnifiedData instance.
58  let unifiedData = new unifiedDataChannel.UnifiedData();
59  let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
60  let plainTextRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText);
61
62  // 5. Add a plainText data record.
63  unifiedData.addRecord(hyperlinkRecord);
64  unifiedData.addRecord(plainTextRecord);
65
66  // 6. Obtain all data records in this UnifiedData instance.
67  let records = unifiedData.getRecords();
68
69  // 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.
70  for (let i = 0; i < records.length; i ++) {
71    let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord;
72    let record = unifiedDataRecord.getValue() as object;
73    if (record != undefined) {
74      // Obtain the type of each data record.
75      let type : string = record["uniformDataType"];
76      switch (type) {
77        case uniformTypeDescriptor.UniformDataType.HYPERLINK:
78          Object.keys(record).forEach(key => {
79            console.info('show records: ' + key + ', value:' + record[key]);
80          });
81          break;
82        case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
83          Object.keys(record).forEach(key => {
84            console.info('show records: ' + key + ', value:' + record[key]);
85          });
86          break;
87        default:
88          break;
89      }
90    }
91  }
92  ```
93