ソースを参照

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

Fix insertion of embeds when selection is not empty
Anatoly Pulyaevskiy 5 年 前
コミット
9a1a864188
No account linked to committer's email address
共有4 個のファイルを変更した36 個の追加6 個の削除を含む
  1. 4
    0
      packages/notus/CHANGELOG.md
  2. 17
    3
      packages/notus/lib/src/document.dart
  3. 3
    3
      packages/notus/pubspec.yaml
  4. 12
    0
      packages/notus/test/document_test.dart

+ 4
- 0
packages/notus/CHANGELOG.md ファイルの表示

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

+ 17
- 3
packages/notus/lib/src/document.dart ファイルの表示

@@ -161,10 +161,24 @@ class NotusDocument {
161 161
   /// unchanged and no [NotusChange] is published to [changes] stream.
162 162
   Delta format(int index, int length, NotusAttribute attribute) {
163 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 182
     return change;
169 183
   }
170 184
 

+ 3
- 3
packages/notus/pubspec.yaml ファイルの表示

@@ -1,16 +1,16 @@
1 1
 name: notus
2 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 4
 author: Anatoly Pulyaevskiy <anatoly.pulyaevskiy@gmail.com>
5 5
 homepage: https://github.com/memspace/zefyr
6 6
 
7 7
 environment:
8
-  sdk: '>=2.0.0-dev.58.0 <3.0.0'
8
+  sdk: '>=2.0.0 <3.0.0'
9 9
 
10 10
 dependencies:
11 11
   collection: ^1.14.6
12 12
   meta: ^1.1.0
13
-  quill_delta: ^1.0.0-dev
13
+  quill_delta: ^1.0.0
14 14
   quiver_hashcode: ^2.0.0
15 15
 
16 16
 dev_dependencies:

+ 12
- 0
packages/notus/test/document_test.dart ファイルの表示

@@ -328,5 +328,17 @@ void main() {
328 328
           '${EmbedNode.kPlainTextPlaceholder}\n');
329 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
 }