node.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'dart:collection';
  2. import 'package:flowy_editor/document/path.dart';
  3. import 'package:flutter/material.dart';
  4. typedef Attributes = Map<String, dynamic>;
  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. Path path([Path previous = const []]) {
  71. if (parent == null) {
  72. return previous;
  73. }
  74. var index = 0;
  75. for (var child in parent!.children) {
  76. if (child == this) {
  77. break;
  78. }
  79. index += 1;
  80. }
  81. return parent!.path([index, ...previous]);
  82. }
  83. @override
  84. void insertAfter(Node entry) {
  85. entry.parent = parent;
  86. super.insertAfter(entry);
  87. // Notify the new node.
  88. parent?.notifyListeners();
  89. }
  90. @override
  91. void insertBefore(Node entry) {
  92. entry.parent = parent;
  93. super.insertBefore(entry);
  94. // Notify the new node.
  95. parent?.notifyListeners();
  96. }
  97. @override
  98. void unlink() {
  99. parent = null;
  100. super.unlink();
  101. }
  102. Map<String, Object> toJson() {
  103. var map = <String, Object>{
  104. 'type': type,
  105. };
  106. if (children.isNotEmpty) {
  107. map['children'] = children.map((node) => node.toJson());
  108. }
  109. if (attributes.isNotEmpty) {
  110. map['attributes'] = attributes;
  111. }
  112. return map;
  113. }
  114. }