Skip to content

Fix insertion of embeds when selection is not empty #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/notus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 17 additions & 3 deletions packages/notus/lib/src/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/notus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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:
Expand Down
12 changes: 12 additions & 0 deletions packages/notus/test/document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
}