Fix Container insert

pull/13/head
singerdmx 4 years ago
parent 63056546b2
commit 4d4cdab520
  1. 22
      lib/models/documents/document.dart
  2. 1
      lib/models/documents/nodes/container.dart

@ -28,11 +28,12 @@ class Document {
final Rules _rules = Rules.getInstance(); final Rules _rules = Rules.getInstance();
final StreamController<Tuple3<Delta, Delta, ChangeSource>> _observer = final StreamController<Tuple3<Delta, Delta, ChangeSource>> _observer =
StreamController.broadcast(); StreamController.broadcast();
Stream<Tuple3> get changes => _observer.stream; Stream<Tuple3> get changes => _observer.stream;
Document() : _delta = Delta()..insert('\n') { Document() : _delta = Delta()
..insert('\n') {
_loadDocument(_delta); _loadDocument(_delta);
} }
@ -124,7 +125,7 @@ class Document {
Delta originalDelta = toDelta(); Delta originalDelta = toDelta();
for (Operation op in delta.toList()) { for (Operation op in delta.toList()) {
Style style = Style style =
op.attributes != null ? Style.fromJson(op.attributes) : null; op.attributes != null ? Style.fromJson(op.attributes) : null;
if (op.isInsert) { if (op.isInsert) {
_root.insert(offset, _normalize(op.data), style); _root.insert(offset, _normalize(op.data), style);
@ -138,7 +139,11 @@ class Document {
offset += op.length; offset += op.length;
} }
} }
_delta = _delta.compose(delta); try {
_delta = _delta.compose(delta);
} catch (e) {
throw ('_delta compose failed');
}
if (_delta != _root.toDelta()) { if (_delta != _root.toDelta()) {
throw ('Compose failed'); throw ('Compose failed');
@ -158,8 +163,8 @@ class Document {
return data is String return data is String
? data ? data
: data is Embeddable : data is Embeddable
? data ? data
: Embeddable.fromJson(data); : Embeddable.fromJson(data);
} }
close() { close() {
@ -173,13 +178,14 @@ class Document {
int offset = 0; int offset = 0;
for (final op in doc.toList()) { for (final op in doc.toList()) {
final style = final style =
op.attributes != null ? Style.fromJson(op.attributes) : null; op.attributes != null ? Style.fromJson(op.attributes) : null;
if (op.isInsert) { if (op.isInsert) {
final data = _normalize(op.data); final data = _normalize(op.data);
_root.insert(offset, data, style); _root.insert(offset, data, style);
} else { } else {
throw ArgumentError.value(doc, throw ArgumentError.value(doc,
'Document Delta can only contain insert operations but ${op.key} found.'); 'Document Delta can only contain insert operations but ${op
.key} found.');
} }
offset += op.length; offset += op.length;
} }

@ -84,6 +84,7 @@ abstract class Container<T extends Node> extends Node {
if (isNotEmpty) { if (isNotEmpty) {
ChildQuery child = queryChild(index, false); ChildQuery child = queryChild(index, false);
child.node.insert(child.offset, data, style); child.node.insert(child.offset, data, style);
return;
} }
// empty // empty

Loading…
Cancel
Save