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,8 +140,7 @@ class NotusDocument {
140 140
     // We have to compose before applying delete rules
141 141
     // Otherwise delete would be operating on stale document snapshot.
142 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 146
     if (length > 0) {

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

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

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

@@ -170,5 +170,19 @@ void main() {
170 170
       var result = controller.getSelectionStyle();
171 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
 }