node.dart 4.2 KB

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