Skip to content

Commit

Permalink
Always keep natural order of selection boundaries (#401)
Browse files Browse the repository at this point in the history
* Always keep natural order of selection boundaries when setting clipboard data
  • Loading branch information
amantoux authored Aug 15, 2024
1 parent 096cd1c commit e395141
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,9 @@ class RawEditorState extends EditorState
final TextSelection selection = textEditingValue.selection;
widget.clipboardManager.setData(FleatherClipboardData(
plainText: selection.textInside(textEditingValue.text),
delta: controller.document
.toDelta()
.slice(selection.baseOffset, selection.extentOffset),
delta: controller.document.toDelta().slice(
math.min(selection.baseOffset, selection.extentOffset),
math.max(selection.baseOffset, selection.extentOffset)),
));
}

Expand Down
29 changes: 29 additions & 0 deletions packages/fleather/test/widgets/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,35 @@ void main() {
expect(sentData?.delta, Delta()..insert('t T'));
});

testWidgets(
'Copy sends correct data to clipboard manager when selection extents are inverted',
(tester) async {
prepareClipboard();
FleatherClipboardData? sentData;
final editor = EditorSandBox(
tester: tester,
document: ParchmentDocument.fromJson([
{'insert': 'Test Text\n'}
]),
autofocus: true,
clipboardManager: FleatherCustomClipboardManager(
getData: () => throw UnimplementedError(),
setData: (data) async => sentData = data,
),
);
await editor.pump();
final RawEditorState state =
tester.state<RawEditorState>(find.byType(RawEditor));
await editor.updateSelection(base: 6, extent: 3);
state.showToolbar(createIfNull: true);
await tester.pump();
final finder = find.text('Copy');
await tester.tap(finder);
await tester.pumpAndSettle(throttleDuration);
expect(sentData?.plainText, 't T');
expect(sentData?.delta, Delta()..insert('t T'));
});

testWidgets('Cut intent sends data to clipboard manager', (tester) async {
prepareClipboard();
FleatherClipboardData? sentData;
Expand Down

0 comments on commit e395141

Please sign in to comment.