Handle multiple image inserts

pull/13/head
singerdmx 4 years ago
parent 4e2f28a18e
commit ce77729968
  1. 5
      CHANGELOG.md
  2. 27
      lib/models/documents/document.dart

@ -15,4 +15,7 @@
* Update example. * Update example.
## [0.0.6] ## [0.0.6]
* More toolbar functionality. * More toolbar functionality.
## [0.0.7]
* Handle multiple image inserts.

@ -29,14 +29,13 @@ 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();
final History _history = History(); final History _history = History();
Stream<Tuple3<Delta, Delta, ChangeSource>> get changes => _observer.stream; Stream<Tuple3<Delta, Delta, ChangeSource>> get changes => _observer.stream;
Document() : _delta = Delta() Document() : _delta = Delta()..insert('\n') {
..insert('\n') {
_loadDocument(_delta); _loadDocument(_delta);
} }
@ -128,7 +127,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);
@ -194,17 +193,21 @@ class Document {
assert((doc.last.data as String).endsWith('\n')); assert((doc.last.data as String).endsWith('\n'));
int offset = 0; int offset = 0;
for (final op in doc.toList()) { for (final op in doc.toList()) {
final style = if (!op.isInsert) {
op.attributes != null ? Style.fromJson(op.attributes) : null;
if (op.isInsert) {
final data = _normalize(op.data);
_root.insert(offset, data, style);
} else {
throw ArgumentError.value(doc, throw ArgumentError.value(doc,
'Document Delta can only contain insert operations but ${op 'Document Delta can only contain insert operations but ${op.key} found.');
.key} found.');
} }
final style =
op.attributes != null ? Style.fromJson(op.attributes) : null;
final data = _normalize(op.data);
_root.insert(offset, data, style);
offset += op.length; offset += op.length;
if (data is! String) {
// This case is 'insert embed'
// automatically append '\n' for image
_root.insert(offset, '\n', null);
offset++;
}
} }
final node = _root.last; final node = _root.last;
if (node is Line && if (node is Line &&

Loading…
Cancel
Save