node.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'dart:collection';
  2. import 'package:flowy_editor/document/path.dart';
  3. import 'package:flutter/material.dart';
  4. typedef Attributes = Map<String, Object>;
  5. class Node extends ChangeNotifier with LinkedListEntry<Node> {
  6. Node? parent;
  7. final String type;
  8. final LinkedList<Node> children;
  9. final Attributes attributes;
  10. String? get subtype {
  11. // TODO: make 'subtype' as a const value.
  12. if (attributes.containsKey('subtype')) {
  13. assert(attributes['subtype'] is String, 'subtype must be a [String]');
  14. return attributes['subtype'] as String;
  15. }
  16. return null;
  17. }
  18. Node({
  19. required this.type,
  20. required this.children,
  21. required this.attributes,
  22. this.parent,
  23. });
  24. factory Node.fromJson(Map<String, Object> json) {
  25. assert(json['type'] is String);
  26. final jType = json['type'] as String;
  27. final jChildren = json['children'] as List?;
  28. final jAttributes = json['attributes'] != null
  29. ? Attributes.from(json['attributes'] as Map)
  30. : Attributes.from({});
  31. final LinkedList<Node> children = LinkedList();
  32. if (jChildren != null) {
  33. children.addAll(
  34. jChildren.map(
  35. (jChild) => Node.fromJson(
  36. Map<String, Object>.from(jChild),
  37. ),
  38. ),
  39. );
  40. }
  41. final node = Node(
  42. type: jType,
  43. children: children,
  44. attributes: jAttributes,
  45. );
  46. for (final child in children) {
  47. child.parent = node;
  48. }
  49. return node;
  50. }
  51. void updateAttributes(Attributes attributes) {
  52. for (final attribute in attributes.entries) {
  53. this.attributes[attribute.key] = attribute.value;
  54. }
  55. // Notify the new attributes
  56. notifyListeners();
  57. }
  58. Node? childAtIndex(int index) {
  59. if (children.length <= index) {
  60. return null;
  61. }
  62. return children.elementAt(index);
  63. }
  64. Node? childAtPath(Path path) {
  65. if (path.isEmpty) {
  66. return this;
  67. }
  68. return childAtIndex(path.first)?.childAtPath(path.sublist(1));
  69. }
  70. @override
  71. void insertAfter(Node entry) {
  72. entry.parent = parent;
  73. super.insertAfter(entry);
  74. // Notify the new node.
  75. parent?.notifyListeners();
  76. }
  77. @override
  78. void insertBefore(Node entry) {
  79. entry.parent = parent;
  80. super.insertBefore(entry);
  81. // Notify the new node.
  82. parent?.notifyListeners();
  83. }
  84. @override
  85. void unlink() {
  86. parent = null;
  87. super.unlink();
  88. }
  89. Map<String, Object> toJson() {
  90. var map = <String, Object>{
  91. 'type': type,
  92. };
  93. if (children.isNotEmpty) {
  94. map['children'] = children.map((node) => node.toJson());
  95. }
  96. if (attributes.isNotEmpty) {
  97. map['attributes'] = attributes;
  98. }
  99. return map;
  100. }
  101. }