node.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import 'dart:collection';
  2. import 'package:flowy_editor/document/path.dart';
  3. import 'package:flowy_editor/document/text_delta.dart';
  4. import 'package:flutter/material.dart';
  5. import './attributes.dart';
  6. class Node extends ChangeNotifier with LinkedListEntry<Node> {
  7. Node? parent;
  8. final String type;
  9. final LinkedList<Node> children;
  10. final Attributes attributes;
  11. String? get subtype {
  12. // TODO: make 'subtype' as a const value.
  13. if (attributes.containsKey('subtype')) {
  14. assert(attributes['subtype'] is String?,
  15. 'subtype must be a [String] or [null]');
  16. return attributes['subtype'] as String?;
  17. }
  18. return null;
  19. }
  20. Path get path => _path();
  21. Node({
  22. required this.type,
  23. required this.children,
  24. required this.attributes,
  25. this.parent,
  26. });
  27. factory Node.fromJson(Map<String, Object> json) {
  28. assert(json['type'] is String);
  29. // TODO: check the type that not exist on plugins.
  30. final jType = json['type'] as String;
  31. final jChildren = json['children'] as List?;
  32. final jAttributes = json['attributes'] != null
  33. ? Attributes.from(json['attributes'] as Map)
  34. : Attributes.from({});
  35. final LinkedList<Node> children = LinkedList();
  36. if (jChildren != null) {
  37. children.addAll(
  38. jChildren.map(
  39. (jChild) => Node.fromJson(
  40. Map<String, Object>.from(jChild),
  41. ),
  42. ),
  43. );
  44. }
  45. Node node;
  46. if (jType == "text") {
  47. final jDelta = json['delta'] as List<dynamic>?;
  48. final delta = jDelta == null ? Delta() : Delta.fromJson(jDelta);
  49. node = TextNode(
  50. type: jType,
  51. children: children,
  52. attributes: jAttributes,
  53. delta: delta);
  54. } else {
  55. node = Node(
  56. type: jType,
  57. children: children,
  58. attributes: jAttributes,
  59. );
  60. }
  61. for (final child in children) {
  62. child.parent = node;
  63. }
  64. return node;
  65. }
  66. void updateAttributes(Attributes attributes) {
  67. bool shouldNotifyParent =
  68. this.attributes['subtype'] != attributes['subtype'];
  69. for (final attribute in attributes.entries) {
  70. this.attributes[attribute.key] = attribute.value;
  71. }
  72. // Notify the new attributes
  73. // if attributes contains 'subtype', should notify parent to rebuild node
  74. // else, just notify current node.
  75. shouldNotifyParent ? parent?.notifyListeners() : notifyListeners();
  76. }
  77. Node? childAtIndex(int index) {
  78. if (children.length <= index) {
  79. return null;
  80. }
  81. return children.elementAt(index);
  82. }
  83. Node? childAtPath(Path path) {
  84. if (path.isEmpty) {
  85. return this;
  86. }
  87. return childAtIndex(path.first)?.childAtPath(path.sublist(1));
  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. super.unlink();
  106. parent?.notifyListeners();
  107. parent = null;
  108. }
  109. Map<String, Object> toJson() {
  110. var map = <String, Object>{
  111. 'type': type,
  112. };
  113. if (children.isNotEmpty) {
  114. map['children'] = children.map((node) => node.toJson());
  115. }
  116. if (attributes.isNotEmpty) {
  117. map['attributes'] = attributes;
  118. }
  119. return map;
  120. }
  121. Path _path([Path previous = const []]) {
  122. if (parent == null) {
  123. return previous;
  124. }
  125. var index = 0;
  126. for (var child in parent!.children) {
  127. if (child == this) {
  128. break;
  129. }
  130. index += 1;
  131. }
  132. return parent!._path([index, ...previous]);
  133. }
  134. }
  135. class TextNode extends Node {
  136. Delta _delta;
  137. TextNode({
  138. required super.type,
  139. required super.children,
  140. required super.attributes,
  141. required Delta delta,
  142. }) : _delta = delta;
  143. Delta get delta {
  144. return _delta;
  145. }
  146. set delta(Delta v) {
  147. _delta = v;
  148. notifyListeners();
  149. }
  150. }