From ce777299684c03cf87eb3a89bff7ad9074708b18 Mon Sep 17 00:00:00 2001 From: singerdmx Date: Sat, 26 Dec 2020 22:47:43 -0800 Subject: [PATCH] Handle multiple image inserts --- CHANGELOG.md | 5 ++++- lib/models/documents/document.dart | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c4be5c..09652bdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,4 +15,7 @@ * Update example. ## [0.0.6] -* More toolbar functionality. \ No newline at end of file +* More toolbar functionality. + +## [0.0.7] +* Handle multiple image inserts. \ No newline at end of file diff --git a/lib/models/documents/document.dart b/lib/models/documents/document.dart index 79efcd9f..9db9a4ff 100644 --- a/lib/models/documents/document.dart +++ b/lib/models/documents/document.dart @@ -29,14 +29,13 @@ class Document { final Rules _rules = Rules.getInstance(); final StreamController> _observer = - StreamController.broadcast(); + StreamController.broadcast(); final History _history = History(); Stream> get changes => _observer.stream; - Document() : _delta = Delta() - ..insert('\n') { + Document() : _delta = Delta()..insert('\n') { _loadDocument(_delta); } @@ -128,7 +127,7 @@ class Document { Delta originalDelta = toDelta(); for (Operation op in delta.toList()) { Style style = - op.attributes != null ? Style.fromJson(op.attributes) : null; + op.attributes != null ? Style.fromJson(op.attributes) : null; if (op.isInsert) { _root.insert(offset, _normalize(op.data), style); @@ -194,17 +193,21 @@ class Document { assert((doc.last.data as String).endsWith('\n')); int offset = 0; for (final op in doc.toList()) { - final style = - op.attributes != null ? Style.fromJson(op.attributes) : null; - if (op.isInsert) { - final data = _normalize(op.data); - _root.insert(offset, data, style); - } else { + if (!op.isInsert) { 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.'); } + final style = + op.attributes != null ? Style.fromJson(op.attributes) : null; + final data = _normalize(op.data); + _root.insert(offset, data, style); 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; if (node is Line &&