node.dart 4.3 KB

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