diff --git a/packages/notus/CHANGELOG.md b/packages/notus/CHANGELOG.md index 0d59db349..60057f650 100644 --- a/packages/notus/CHANGELOG.md +++ b/packages/notus/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.4 + +* Fixed insertion of embeds when selection is not empty (#115) + ## 0.1.3 * Fixed handling of user input around embeds diff --git a/packages/notus/lib/src/document.dart b/packages/notus/lib/src/document.dart index 8a4162d1d..0b807494f 100644 --- a/packages/notus/lib/src/document.dart +++ b/packages/notus/lib/src/document.dart @@ -161,10 +161,24 @@ class NotusDocument { /// unchanged and no [NotusChange] is published to [changes] stream. Delta format(int index, int length, NotusAttribute attribute) { assert(index >= 0 && length >= 0 && attribute != null); - final change = _heuristics.applyFormatRules(this, index, length, attribute); - if (change.isNotEmpty) { - compose(change, ChangeSource.local); + + Delta change = Delta(); + + if (attribute is EmbedAttribute && length > 0) { + // Must delete selected length of text before applying embed attribute + // since inserting an embed in non-empty selection is essentially a + // replace operation. + change = delete(index, length); + length = 0; } + + final formatChange = + _heuristics.applyFormatRules(this, index, length, attribute); + if (formatChange.isNotEmpty) { + compose(formatChange, ChangeSource.local); + change = change.compose(formatChange); + } + return change; } diff --git a/packages/notus/pubspec.yaml b/packages/notus/pubspec.yaml index af8d04938..f3ac7a5a5 100644 --- a/packages/notus/pubspec.yaml +++ b/packages/notus/pubspec.yaml @@ -1,16 +1,16 @@ name: notus description: Platform-agnostic rich text document model based on Delta format and used in Zefyr editor. -version: 0.1.3 +version: 0.1.4 author: Anatoly Pulyaevskiy homepage: https://github.com/memspace/zefyr environment: - sdk: '>=2.0.0-dev.58.0 <3.0.0' + sdk: '>=2.0.0 <3.0.0' dependencies: collection: ^1.14.6 meta: ^1.1.0 - quill_delta: ^1.0.0-dev + quill_delta: ^1.0.0 quiver_hashcode: ^2.0.0 dev_dependencies: diff --git a/packages/notus/test/document_test.dart b/packages/notus/test/document_test.dart index 8cdbd39fb..11f930e47 100644 --- a/packages/notus/test/document_test.dart +++ b/packages/notus/test/document_test.dart @@ -328,5 +328,17 @@ void main() { '${EmbedNode.kPlainTextPlaceholder}\n'); expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n'); }); + + test('replace embed with embed', () { + final doc = dartconfDoc(); + doc.format(4, 4, NotusAttribute.embed.horizontalRule); + doc.format(5, 1, NotusAttribute.embed.horizontalRule); + + expect(doc.root.children, hasLength(3)); + expect(doc.root.children.elementAt(0).toPlainText(), 'Dart\n'); + expect(doc.root.children.elementAt(1).toPlainText(), + '${EmbedNode.kPlainTextPlaceholder}\n'); + expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n'); + }); }); }