node.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (attribute.value == null) {
  79. this.attributes.remove(attribute.key);
  80. } else {
  81. this.attributes[attribute.key] = attribute.value;
  82. }
  83. }
  84. // Notify the new attributes
  85. // if attributes contains 'subtype', should notify parent to rebuild node
  86. // else, just notify current node.
  87. shouldNotifyParent ? parent?.notifyListeners() : notifyListeners();
  88. }
  89. Node? childAtIndex(int index) {
  90. if (children.length <= index) {
  91. return null;
  92. }
  93. return children.elementAt(index);
  94. }
  95. Node? childAtPath(Path path) {
  96. if (path.isEmpty) {
  97. return this;
  98. }
  99. return childAtIndex(path.first)?.childAtPath(path.sublist(1));
  100. }
  101. @override
  102. void insertAfter(Node entry) {
  103. entry.parent = parent;
  104. super.insertAfter(entry);
  105. // Notify the new node.
  106. parent?.notifyListeners();
  107. }
  108. @override
  109. void insertBefore(Node entry) {
  110. entry.parent = parent;
  111. super.insertBefore(entry);
  112. // Notify the new node.
  113. parent?.notifyListeners();
  114. }
  115. @override
  116. void unlink() {
  117. super.unlink();
  118. parent?.notifyListeners();
  119. parent = null;
  120. }
  121. Map<String, Object> toJson() {
  122. var map = <String, Object>{
  123. 'type': type,
  124. };
  125. if (children.isNotEmpty) {
  126. map['children'] = children.map((node) => node.toJson());
  127. }
  128. if (attributes.isNotEmpty) {
  129. map['attributes'] = attributes;
  130. }
  131. return map;
  132. }
  133. Path _path([Path previous = const []]) {
  134. if (parent == null) {
  135. return previous;
  136. }
  137. var index = 0;
  138. for (var child in parent!.children) {
  139. if (child == this) {
  140. break;
  141. }
  142. index += 1;
  143. }
  144. return parent!._path([index, ...previous]);
  145. }
  146. }
  147. class TextNode extends Node {
  148. Delta _delta;
  149. TextNode({
  150. required super.type,
  151. required Delta delta,
  152. LinkedList<Node>? children,
  153. Attributes? attributes,
  154. }) : _delta = delta,
  155. super(children: children ?? LinkedList(), attributes: attributes ?? {});
  156. TextNode.empty()
  157. : _delta = Delta([TextInsert('')]),
  158. super(
  159. type: 'text',
  160. children: LinkedList(),
  161. attributes: {},
  162. );
  163. Delta get delta {
  164. return _delta;
  165. }
  166. set delta(Delta v) {
  167. _delta = v;
  168. notifyListeners();
  169. }
  170. @override
  171. Map<String, Object> toJson() {
  172. final map = super.toJson();
  173. map['delta'] = _delta.toJson();
  174. return map;
  175. }
  176. TextNode copyWith({
  177. String? type,
  178. LinkedList<Node>? children,
  179. Attributes? attributes,
  180. Delta? delta,
  181. }) =>
  182. TextNode(
  183. type: type ?? this.type,
  184. children: children ?? this.children,
  185. attributes: attributes ?? this.attributes,
  186. delta: delta ?? this.delta,
  187. );
  188. // TODO: It's unneccesry to compute everytime.
  189. String toRawString() =>
  190. _delta.operations.whereType<TextInsert>().map((op) => op.content).join();
  191. }