node.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. GlobalKey? key;
  12. // TODO: abstract a selectable node??
  13. final layerLink = LayerLink();
  14. String? get subtype {
  15. // TODO: make 'subtype' as a const value.
  16. if (attributes.containsKey('subtype')) {
  17. assert(attributes['subtype'] is String?,
  18. 'subtype must be a [String] or [null]');
  19. return attributes['subtype'] as String?;
  20. }
  21. return null;
  22. }
  23. Path get path => _path();
  24. Node({
  25. required this.type,
  26. required this.children,
  27. required this.attributes,
  28. this.parent,
  29. }) {
  30. for (final child in children) {
  31. child.parent = this;
  32. }
  33. }
  34. factory Node.fromJson(Map<String, Object> json) {
  35. assert(json['type'] is String);
  36. // TODO: check the type that not exist on plugins.
  37. final jType = json['type'] as String;
  38. final jChildren = json['children'] as List?;
  39. final jAttributes = json['attributes'] != null
  40. ? Attributes.from(json['attributes'] as Map)
  41. : Attributes.from({});
  42. final LinkedList<Node> children = LinkedList();
  43. if (jChildren != null) {
  44. children.addAll(
  45. jChildren.map(
  46. (jChild) => Node.fromJson(
  47. Map<String, Object>.from(jChild),
  48. ),
  49. ),
  50. );
  51. }
  52. Node node;
  53. if (jType == "text") {
  54. final jDelta = json['delta'] as List<dynamic>?;
  55. final delta = jDelta == null ? Delta() : Delta.fromJson(jDelta);
  56. node = TextNode(
  57. type: jType,
  58. children: children,
  59. attributes: jAttributes,
  60. delta: delta);
  61. } else {
  62. node = Node(
  63. type: jType,
  64. children: children,
  65. attributes: jAttributes,
  66. );
  67. }
  68. for (final child in children) {
  69. child.parent = node;
  70. }
  71. return node;
  72. }
  73. void updateAttributes(Attributes attributes) {
  74. bool shouldNotifyParent =
  75. this.attributes['subtype'] != attributes['subtype'];
  76. for (final attribute in attributes.entries) {
  77. this.attributes[attribute.key] = attribute.value;
  78. }
  79. // Notify the new attributes
  80. // if attributes contains 'subtype', should notify parent to rebuild node
  81. // else, just notify current node.
  82. shouldNotifyParent ? parent?.notifyListeners() : notifyListeners();
  83. }
  84. Node? childAtIndex(int index) {
  85. if (children.length <= index) {
  86. return null;
  87. }
  88. return children.elementAt(index);
  89. }
  90. Node? childAtPath(Path path) {
  91. if (path.isEmpty) {
  92. return this;
  93. }
  94. return childAtIndex(path.first)?.childAtPath(path.sublist(1));
  95. }
  96. @override
  97. void insertAfter(Node entry) {
  98. entry.parent = parent;
  99. super.insertAfter(entry);
  100. // Notify the new node.
  101. parent?.notifyListeners();
  102. }
  103. @override
  104. void insertBefore(Node entry) {
  105. entry.parent = parent;
  106. super.insertBefore(entry);
  107. // Notify the new node.
  108. parent?.notifyListeners();
  109. }
  110. @override
  111. void unlink() {
  112. super.unlink();
  113. parent?.notifyListeners();
  114. parent = null;
  115. }
  116. Map<String, Object> toJson() {
  117. var map = <String, Object>{
  118. 'type': type,
  119. };
  120. if (children.isNotEmpty) {
  121. map['children'] = children.map((node) => node.toJson());
  122. }
  123. if (attributes.isNotEmpty) {
  124. map['attributes'] = attributes;
  125. }
  126. return map;
  127. }
  128. Path _path([Path previous = const []]) {
  129. if (parent == null) {
  130. return previous;
  131. }
  132. var index = 0;
  133. for (var child in parent!.children) {
  134. if (child == this) {
  135. break;
  136. }
  137. index += 1;
  138. }
  139. return parent!._path([index, ...previous]);
  140. }
  141. }
  142. class TextNode extends Node {
  143. Delta _delta;
  144. TextNode({
  145. required super.type,
  146. required super.children,
  147. required super.attributes,
  148. required Delta delta,
  149. }) : _delta = delta;
  150. Delta get delta {
  151. return _delta;
  152. }
  153. set delta(Delta v) {
  154. _delta = v;
  155. notifyListeners();
  156. }
  157. @override
  158. Map<String, Object> toJson() {
  159. final map = super.toJson();
  160. map['delta'] = _delta.toJson();
  161. return map;
  162. }
  163. // TODO: It's unneccesry to compute everytime.
  164. String toRawString() =>
  165. _delta.operations.whereType<TextInsert>().map((op) => op.content).join();
  166. }