Skip to content

Commit 9a1a864

Browse files
authored
Merge pull request #161 from memspace/notus-fix-embed-in-selection
Fix insertion of embeds when selection is not empty
2 parents 454146a + b5bcec3 commit 9a1a864

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

packages/notus/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.4
2+
3+
* Fixed insertion of embeds when selection is not empty (#115)
4+
15
## 0.1.3
26

37
* Fixed handling of user input around embeds

packages/notus/lib/src/document.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,24 @@ class NotusDocument {
161161
/// unchanged and no [NotusChange] is published to [changes] stream.
162162
Delta format(int index, int length, NotusAttribute attribute) {
163163
assert(index >= 0 && length >= 0 && attribute != null);
164-
final change = _heuristics.applyFormatRules(this, index, length, attribute);
165-
if (change.isNotEmpty) {
166-
compose(change, ChangeSource.local);
164+
165+
Delta change = Delta();
166+
167+
if (attribute is EmbedAttribute && length > 0) {
168+
// Must delete selected length of text before applying embed attribute
169+
// since inserting an embed in non-empty selection is essentially a
170+
// replace operation.
171+
change = delete(index, length);
172+
length = 0;
167173
}
174+
175+
final formatChange =
176+
_heuristics.applyFormatRules(this, index, length, attribute);
177+
if (formatChange.isNotEmpty) {
178+
compose(formatChange, ChangeSource.local);
179+
change = change.compose(formatChange);
180+
}
181+
168182
return change;
169183
}
170184

packages/notus/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
name: notus
22
description: Platform-agnostic rich text document model based on Delta format and used in Zefyr editor.
3-
version: 0.1.3
3+
version: 0.1.4
44
author: Anatoly Pulyaevskiy <[email protected]>
55
homepage: https://github.com/memspace/zefyr
66

77
environment:
8-
sdk: '>=2.0.0-dev.58.0 <3.0.0'
8+
sdk: '>=2.0.0 <3.0.0'
99

1010
dependencies:
1111
collection: ^1.14.6
1212
meta: ^1.1.0
13-
quill_delta: ^1.0.0-dev
13+
quill_delta: ^1.0.0
1414
quiver_hashcode: ^2.0.0
1515

1616
dev_dependencies:

packages/notus/test/document_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,17 @@ void main() {
328328
'${EmbedNode.kPlainTextPlaceholder}\n');
329329
expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n');
330330
});
331+
332+
test('replace embed with embed', () {
333+
final doc = dartconfDoc();
334+
doc.format(4, 4, NotusAttribute.embed.horizontalRule);
335+
doc.format(5, 1, NotusAttribute.embed.horizontalRule);
336+
337+
expect(doc.root.children, hasLength(3));
338+
expect(doc.root.children.elementAt(0).toPlainText(), 'Dart\n');
339+
expect(doc.root.children.elementAt(1).toPlainText(),
340+
'${EmbedNode.kPlainTextPlaceholder}\n');
341+
expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n');
342+
});
331343
});
332344
}

0 commit comments

Comments
 (0)