node.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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?,
  14. 'subtype must be a [String] or [null]');
  15. return attributes['subtype'] as String?;
  16. }
  17. return null;
  18. }
  19. Path get path => _path();
  20. Node({
  21. required this.type,
  22. required this.children,
  23. required this.attributes,
  24. this.parent,
  25. });
  26. factory Node.fromJson(Map<String, Object> json) {
  27. assert(json['type'] is String);
  28. // TODO: check the type that not exist on plugins.
  29. final jType = json['type'] as String;
  30. final jChildren = json['children'] as List?;
  31. final jAttributes = json['attributes'] != null
  32. ? Attributes.from(json['attributes'] as Map)
  33. : Attributes.from({});
  34. final LinkedList<Node> children = LinkedList();
  35. if (jChildren != null) {
  36. children.addAll(
  37. jChildren.map(
  38. (jChild) => Node.fromJson(
  39. Map<String, Object>.from(jChild),
  40. ),
  41. ),
  42. );
  43. }
  44. final node = Node(
  45. type: jType,
  46. children: children,
  47. attributes: jAttributes,
  48. );
  49. for (final child in children) {
  50. child.parent = node;
  51. }
  52. return node;
  53. }
  54. void updateAttributes(Attributes attributes) {
  55. bool shouldNotifyParent =
  56. this.attributes['subtype'] != attributes['subtype'];
  57. for (final attribute in attributes.entries) {
  58. this.attributes[attribute.key] = attribute.value;
  59. }
  60. // Notify the new attributes
  61. // if attributes contains 'subtype', should notify parent to rebuild node
  62. // else, just notify current node.
  63. shouldNotifyParent ? parent?.notifyListeners() : notifyListeners();
  64. }
  65. Node? childAtIndex(int index) {
  66. if (children.length <= index) {
  67. return null;
  68. }
  69. return children.elementAt(index);
  70. }
  71. Node? childAtPath(Path path) {
  72. if (path.isEmpty) {
  73. return this;
  74. }
  75. return childAtIndex(path.first)?.childAtPath(path.sublist(1));
  76. }
  77. @override
  78. void insertAfter(Node entry) {
  79. entry.parent = parent;
  80. super.insertAfter(entry);
  81. // Notify the new node.
  82. parent?.notifyListeners();
  83. }
  84. @override
  85. void insertBefore(Node entry) {
  86. entry.parent = parent;
  87. super.insertBefore(entry);
  88. // Notify the new node.
  89. parent?.notifyListeners();
  90. }
  91. @override
  92. void unlink() {
  93. super.unlink();
  94. parent?.notifyListeners();
  95. parent = null;
  96. }
  97. Map<String, Object> toJson() {
  98. var map = <String, Object>{
  99. 'type': type,
  100. };
  101. if (children.isNotEmpty) {
  102. map['children'] = children.map((node) => node.toJson());
  103. }
  104. if (attributes.isNotEmpty) {
  105. map['attributes'] = attributes;
  106. }
  107. return map;
  108. }
  109. Path _path([Path previous = const []]) {
  110. if (parent == null) {
  111. return previous;
  112. }
  113. var index = 0;
  114. for (var child in parent!.children) {
  115. if (child == this) {
  116. break;
  117. }
  118. index += 1;
  119. }
  120. return parent!._path([index, ...previous]);
  121. }
  122. }