node.dart 3.0 KB

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