Browse Source

Transform caret position when a new embed is inserted

Anatoly Pulyaevskiy 6 years ago
parent
commit
45ce0b0c85
1 changed files with 12 additions and 1 deletions
  1. 12
    1
      packages/zefyr/lib/src/widgets/controller.dart

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

@@ -131,8 +131,19 @@ class ZefyrController extends ChangeNotifier {
131 131
   }
132 132
 
133 133
   void formatText(int index, int length, NotusAttribute attribute) {
134
-    document.format(index, length, attribute);
134
+    final change = document.format(index, length, attribute);
135 135
     _lastChangeSource = ChangeSource.local;
136
+    // Transform selection against the composed change and give priority to
137
+    // the change. This is needed in cases when format operation actually
138
+    // inserts data into the document (e.g. embeds).
139
+    final base = change.transformPosition(_selection.baseOffset);
140
+    final extent =
141
+        change.transformPosition(_selection.extentOffset);
142
+    final adjustedSelection =
143
+        _selection.copyWith(baseOffset: base, extentOffset: extent);
144
+    if (_selection != adjustedSelection) {
145
+      _updateSelectionSilent(adjustedSelection, source: _lastChangeSource);
146
+    }
136 147
     notifyListeners();
137 148
   }
138 149