node.dart 3.0 KB

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