Browse Source

Preserve inline style when replacing formatted text from the first character (#201)

Bai 5 years ago
parent
commit
7f6d91c27d

+ 1
- 2
packages/notus/lib/src/document.dart View File

140
     // We have to compose before applying delete rules
140
     // We have to compose before applying delete rules
141
     // Otherwise delete would be operating on stale document snapshot.
141
     // Otherwise delete would be operating on stale document snapshot.
142
     if (text.isNotEmpty) {
142
     if (text.isNotEmpty) {
143
-      delta = insert(index, text);
144
-      index = delta.transformPosition(index);
143
+      delta = insert(index + length, text);
145
     }
144
     }
146
 
145
 
147
     if (length > 0) {
146
     if (length > 0) {

+ 3
- 3
packages/zefyr/lib/src/widgets/controller.dart View File

124
       // some style, then we apply it to our document.
124
       // some style, then we apply it to our document.
125
       if (delta != null &&
125
       if (delta != null &&
126
           toggledStyles.isNotEmpty &&
126
           toggledStyles.isNotEmpty &&
127
-          delta.length == 2 &&
128
-          delta[1].isInsert) {
127
+          delta.length <=2 &&
128
+          delta.last.isInsert) {
129
         // Apply it.
129
         // Apply it.
130
         Delta retainDelta = Delta()
130
         Delta retainDelta = Delta()
131
           ..retain(index)
131
           ..retain(index)
132
-          ..retain(1, toggledStyles.toJson());
132
+          ..retain(text.length, toggledStyles.toJson());
133
         document.compose(retainDelta, ChangeSource.local);
133
         document.compose(retainDelta, ChangeSource.local);
134
       }
134
       }
135
     }
135
     }

+ 14
- 0
packages/zefyr/test/widgets/controller_test.dart View File

170
       var result = controller.getSelectionStyle();
170
       var result = controller.getSelectionStyle();
171
       expect(result.values, [NotusAttribute.bold]);
171
       expect(result.values, [NotusAttribute.bold]);
172
     });
172
     });
173
+    test('formatText with toggled style for first charater', () {
174
+      bool notified = false;
175
+      controller.addListener(() {
176
+        notified = true;
177
+      });
178
+      controller.formatText(0, 0, NotusAttribute.bold);
179
+      controller.replaceText(0, 0, 'Word');
180
+      expect(notified, isTrue);
181
+      expect(
182
+        controller.document.toDelta(),
183
+        Delta()..insert('Word', NotusAttribute.bold.toJson())..insert('\n'),
184
+      );
185
+      expect(controller.lastChangeSource, ChangeSource.local);
186
+    });
173
   });
187
   });
174
 }
188
 }