Browse Source

Merge pull request #161 from memspace/notus-fix-embed-in-selection

Fix insertion of embeds when selection is not empty
Anatoly Pulyaevskiy 5 years ago
parent
commit
9a1a864188
No account linked to committer's email address

+ 4
- 0
packages/notus/CHANGELOG.md View File

1
+## 0.1.4
2
+
3
+* Fixed insertion of embeds when selection is not empty (#115)
4
+
1
 ## 0.1.3
5
 ## 0.1.3
2
 
6
 
3
 * Fixed handling of user input around embeds
7
 * Fixed handling of user input around embeds

+ 17
- 3
packages/notus/lib/src/document.dart View File

161
   /// unchanged and no [NotusChange] is published to [changes] stream.
161
   /// unchanged and no [NotusChange] is published to [changes] stream.
162
   Delta format(int index, int length, NotusAttribute attribute) {
162
   Delta format(int index, int length, NotusAttribute attribute) {
163
     assert(index >= 0 && length >= 0 && attribute != null);
163
     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;
167
     }
173
     }
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
+
168
     return change;
182
     return change;
169
   }
183
   }
170
 
184
 

+ 3
- 3
packages/notus/pubspec.yaml View File

1
 name: notus
1
 name: notus
2
 description: Platform-agnostic rich text document model based on Delta format and used in Zefyr editor.
2
 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
4
 author: Anatoly Pulyaevskiy <anatoly.pulyaevskiy@gmail.com>
4
 author: Anatoly Pulyaevskiy <anatoly.pulyaevskiy@gmail.com>
5
 homepage: https://github.com/memspace/zefyr
5
 homepage: https://github.com/memspace/zefyr
6
 
6
 
7
 environment:
7
 environment:
8
-  sdk: '>=2.0.0-dev.58.0 <3.0.0'
8
+  sdk: '>=2.0.0 <3.0.0'
9
 
9
 
10
 dependencies:
10
 dependencies:
11
   collection: ^1.14.6
11
   collection: ^1.14.6
12
   meta: ^1.1.0
12
   meta: ^1.1.0
13
-  quill_delta: ^1.0.0-dev
13
+  quill_delta: ^1.0.0
14
   quiver_hashcode: ^2.0.0
14
   quiver_hashcode: ^2.0.0
15
 
15
 
16
 dev_dependencies:
16
 dev_dependencies:

+ 12
- 0
packages/notus/test/document_test.dart View File

328
           '${EmbedNode.kPlainTextPlaceholder}\n');
328
           '${EmbedNode.kPlainTextPlaceholder}\n');
329
       expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n');
329
       expect(doc.root.children.elementAt(2).toPlainText(), 'Los Angeles\n');
330
     });
330
     });
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
+    });
331
   });
343
   });
332
 }
344
 }