node.dart 4.6 KB

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