node.dart 3.0 KB

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