瀏覽代碼

增加reportInfo的json序列化功能

loki.wu 2 年之前
父節點
當前提交
b72dc53187

+ 2 - 1
lib/main.dart

@@ -59,13 +59,14 @@ class _MyHomePageState extends State<MyHomePage> {
         _jsonStr = jsonStr;
       });
     });
-    super.initState();
+
     rootBundle.loadString('assets/single_image.json').then((jsonStr) {
       _aiJson = jsonStr;
     });
     rootBundle.loadString('assets/pet.json').then((jsonStr) {
       _petStr = jsonStr;
     });
+    super.initState();
   }
 
   @override

+ 5 - 0
lib/report/cellPostion.dart

@@ -11,4 +11,9 @@ class CellPostion {
     rowSpan = json['RowSpan'];
     columnSpan = json['ColumnSpan'];
   }
+
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    return map;
+  }
 }

+ 4 - 0
lib/report/element_type.dart

@@ -23,4 +23,8 @@ class ElementType {
     name = json['Name'];
     //TODO(Loki):init rtGrid and rtTaable
   }
+
+  Map<String, dynamic> toJson() => {
+        'Name': name,
+      };
 }

+ 4 - 0
lib/report/measure_tag.dart

@@ -34,4 +34,8 @@ class MeasureTag {
   MeasureTag.fromJson(Map<String, dynamic> json) {
     //TODO(Loki): init MeasureTag
   }
+
+  Map<String, dynamic> toJson() => {
+        //TODO(Loki): MeasureTag toJson
+      };
 }

+ 10 - 0
lib/report_info/date_time_info.dart

@@ -12,4 +12,14 @@ class DateTimeInfo extends TextElementInfo {
       : super.fromElement(element) {
     dateTimeFormat = element.dateTimeFormat;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+
+    map['Text'] = text;
+    map['DateTimeFormat'] = dateTimeFormat;
+    return map;
+  }
 }

+ 7 - 0
lib/report_info/element_info.dart

@@ -37,4 +37,11 @@ abstract class ElementInfo {
     tag = element.tag;
     id = element.id;
   }
+
+  Map<String, dynamic> toJson() => {
+        'Index': index,
+        'Id': id,
+        'ElementType': elementType!.toJson(),
+        'MeasureTag': measureTag == null ? null : measureTag!.toJson(),
+      };
 }

+ 11 - 0
lib/report_info/input_image_info.dart

@@ -27,4 +27,15 @@ class InputImageInfo extends ElementInfo implements IBlockElementInfo {
   void addImage(String imageUrl) {
     onSelect!.emit(this, imageUrl);
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+
+    map['HasGap'] = hasGap;
+    map['IsReadOnly'] = isReadOnly;
+    map['IsSelected'] = isSelected;
+    return map;
+  }
 }

+ 12 - 0
lib/report_info/input_image_list_info.dart

@@ -33,4 +33,16 @@ class InputImageListInfo extends ElementInfo implements IBlockElementInfo {
   void addImage(String imageUrl) {
     onSelect!.emit(this, imageUrl);
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    map['Column'] = column;
+    map['HasGap'] = hasGap;
+    map['IsReadOnly'] = isReadOnly;
+    map['IsSelected'] = isSelected;
+    map['SelectedImages'] = selectedImages;
+    return map;
+  }
 }

+ 8 - 0
lib/report_info/input_text_info.dart

@@ -15,4 +15,12 @@ class InputTextInfo extends TextElementInfo {
     isListening = false;
     text = '';
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    map['IsReadOnly'] = isReadOnly;
+    return map;
+  }
 }

+ 7 - 3
lib/report_info/line_info.dart

@@ -10,9 +10,6 @@ class LineInfo extends ElementInfo {
   @override
   double? thickness;
 
-  @override
-  RTBorderStyle? borderStyle;
-
   @override
   double? width;
 
@@ -21,4 +18,11 @@ class LineInfo extends ElementInfo {
     thickness = element.thickness;
     width = element.width;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    return map;
+  }
 }

+ 9 - 0
lib/report_info/multi_selected_info.dart

@@ -14,4 +14,13 @@ class MulitiSelectedInfo extends TextElementInfo {
     items = element.items;
     isReadOnly = element.isReadOnly;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    map['Items'] = items;
+    map['SelectedItems'] = selectedItems;
+    return map;
+  }
 }

+ 8 - 0
lib/report_info/page_number_info.dart

@@ -8,4 +8,12 @@ class PageNumberInfo extends TextElementInfo {
   PageNumberInfo.fromElement(PageNumber element) : super.fromElement(element) {
     // text = element.te;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    map['Text'] = text;
+    return map;
+  }
 }

+ 41 - 0
lib/report_info/paragraph_info.dart

@@ -64,4 +64,45 @@ class ParagraphInfo extends ElementInfo implements IBlockElementInfo {
       }
     }
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    final elementInfosmap = <String, dynamic>{};
+    elementInfosmap.addAll(super.toJson());
+    for (var element in elementInfos!) {
+      if (element.elementType!.name == ElementType.inputText!.name) {
+        final inputText = element as InputTextInfo;
+        elementInfosmap.addAll(inputText.toJson());
+      } else if (element.elementType!.name == ElementType.staticText!.name) {
+        final staticText = element as StaticTextInfo;
+        elementInfosmap.addAll(staticText.toJson());
+      } else if (element.elementType!.name ==
+          ElementType.singleSelected!.name) {
+        final singleSelected = element as SingleSelectedInfo;
+        elementInfosmap.addAll(singleSelected.toJson());
+      } else if (element.elementType!.name == ElementType.dateTime!.name) {
+        final dateTime = element as DateTimeInfo;
+        elementInfosmap.addAll(dateTime.toJson());
+      } else if (element.elementType!.name == ElementType.multiSelected!.name) {
+        final elementInfo = element as MulitiSelectedInfo;
+        elementInfosmap.addAll(elementInfo.toJson());
+      } else if (element.elementType!.name == ElementType.inputImage!.name) {
+        final elementInfo = element as InputImageInfo;
+        elementInfosmap.addAll(elementInfo.toJson());
+      } else if (element.elementType!.name == ElementType.staticImage!.name) {
+        final elementInfo = element as StaticImageInfo;
+        elementInfosmap.addAll(elementInfo.toJson());
+      } else if (element.elementType!.name == ElementType.pageNumber!.name) {
+        final elementInfo = element as PageNumberInfo;
+        elementInfosmap.addAll(elementInfo.toJson());
+      } else if (element.elementType!.name == ElementType.line!.name) {
+        final elementInfo = element as LineInfo;
+        elementInfosmap.addAll(elementInfo.toJson());
+      }
+    }
+    map['ElementInfos'] = elementInfos;
+    return map;
+  }
 }

+ 38 - 0
lib/report_info/report_info.dart

@@ -89,6 +89,30 @@ class ReportInfo {
     }
   }
 
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    try {
+      final headInfos = [];
+      for (var element in headers) {
+        headInfos.add(_getElementInfo(element));
+      }
+      map['HeadInfos'] = headInfos;
+      final blockInfos = [];
+      for (var block in blocks) {
+        blockInfos.add(_getElementInfo(block));
+      }
+      map['BlockInfos'] = blockInfos;
+      final footerInfos = [];
+      for (var block in footers) {
+        footerInfos.add(_getElementInfo(block));
+      }
+      map['FooterInfos'] = footerInfos;
+    } catch (e) {
+      print(e);
+    }
+    return map;
+  }
+
   ///重新渲染UI&重构reportInfo
   void reload(String reporter, DateTime reportDate, String jsonStr,
       FEventHandler<String> onSelect) {
@@ -225,6 +249,20 @@ class ReportInfo {
 
     return result;
   }
+
+  Map<String, dynamic> _getElementInfo(IBlockElementInfo element) {
+    final _type = element.elementType!;
+    if (_type.name == ElementType.rtTable!.name) {
+      final table = element as RTTableInfo;
+      return table.toJson();
+    } else if (_type.name == ElementType.paragraph!.name) {
+      final paragraph = element as ParagraphInfo;
+      return paragraph.toJson();
+    } else {
+      final inputImageList = element as InputImageListInfo;
+      return inputImageList.toJson();
+    }
+  }
 }
 
 class ReportEventArgs {

+ 15 - 0
lib/report_info/rt_cell_info.dart

@@ -17,4 +17,19 @@ class RTCellInfo extends ElementInfo {
       }
     }
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    final blockInfos = <String, dynamic>{};
+    for (var b in blocks!) {
+      if (b.elementType!.name == ElementType.paragraph!.name) {
+        final paragraphInfo = b as ParagraphInfo;
+        blockInfos.addAll(paragraphInfo.toJson());
+      }
+    }
+    map['Blocks'] = blockInfos;
+    return map;
+  }
 }

+ 13 - 0
lib/report_info/rt_grid.dart

@@ -45,4 +45,17 @@ class RTGridInfo extends ElementInfo {
       cells![key] = RTCellInfo.fromElement(value);
     });
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    final blockInfos = [];
+    cells!.forEach((key, value) {
+      blockInfos.add(key.toJson());
+      blockInfos.add(value.toJson());
+    });
+    map['Cells'] = blockInfos;
+    return map;
+  }
 }

+ 8 - 1
lib/report_info/rt_table_info.dart

@@ -10,6 +10,9 @@ class RTTableInfo extends RTGridInfo implements IBlockElementInfo {
   bool? isAverageColumnWidth;
   bool? allowBreakAcrossPages;
 
+  @override
+  List<ElementInfo>? elementInfos;
+
   RTTableInfo.fromElement(RTTable table) : super.fromElement(table) {
     autoHide = table.autoHide;
     isAverageColumnWidth = table.isAverageColumnWidth;
@@ -17,5 +20,9 @@ class RTTableInfo extends RTGridInfo implements IBlockElementInfo {
   }
 
   @override
-  List<ElementInfo>? elementInfos;
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    return map;
+  }
 }

+ 8 - 0
lib/report_info/single_selected_info.dart

@@ -16,4 +16,12 @@ class SingleSelectedInfo extends TextElementInfo {
     isReadOnly = element.isReadOnly;
     items = element.items;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    map['SelectedItem'] = selectedItem;
+    return map;
+  }
 }

+ 7 - 0
lib/report_info/static_image_info.dart

@@ -30,4 +30,11 @@ class StaticImageInfo extends ElementInfo implements IBlockElementInfo {
 
   @override
   List<ElementInfo>? elementInfos;
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    return map;
+  }
 }

+ 7 - 0
lib/report_info/static_text_info.dart

@@ -7,4 +7,11 @@ class StaticTextInfo extends TextElementInfo {
   StaticTextInfo.fromElement(StaticText element) : super.fromElement(element) {
     text = element.text;
   }
+
+  @override
+  Map<String, dynamic> toJson() {
+    final map = <String, dynamic>{};
+    map.addAll(super.toJson());
+    return map;
+  }
 }