Skip to content

Commit bb22ca5

Browse files
committed
chore: copy cell content
1 parent 1a83cb6 commit bb22ca5

File tree

7 files changed

+76
-9
lines changed

7 files changed

+76
-9
lines changed

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ abstract class GridCellWidget extends StatefulWidget implements CellAccessory, C
6262
final GridCellFocusListener beginFocus = GridCellFocusListener();
6363

6464
@override
65-
final Map<CellKeyboardKey, CellKeyboardAction> keyboardActionHandlers = {};
65+
final Map<CellKeyboardKey, CellKeyboardAction> shortcutHandlers = {};
6666
}
6767

6868
abstract class GridCellState<T extends GridCellWidget> extends State<T> {
@@ -94,7 +94,7 @@ abstract class GridFocusNodeCellState<T extends GridCellWidget> extends GridCell
9494

9595
@override
9696
void initState() {
97-
widget.keyboardActionHandlers[CellKeyboardKey.onEnter] = () => focusNode.unfocus();
97+
widget.shortcutHandlers[CellKeyboardKey.onEnter] = () => focusNode.unfocus();
9898
_listenOnFocusNodeChanged();
9999
super.initState();
100100
}
@@ -109,7 +109,7 @@ abstract class GridFocusNodeCellState<T extends GridCellWidget> extends GridCell
109109

110110
@override
111111
void dispose() {
112-
widget.keyboardActionHandlers.remove(CellKeyboardKey.onEnter);
112+
widget.shortcutHandlers.clear();
113113
focusNode.removeAllListener();
114114
focusNode.dispose();
115115
super.dispose();

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33

4-
typedef CellKeyboardAction = VoidCallback;
4+
typedef CellKeyboardAction = dynamic Function();
55

66
enum CellKeyboardKey {
77
onEnter,
8+
onCopy,
9+
onInsert,
810
}
911

1012
abstract class CellShortcuts extends Widget {
1113
const CellShortcuts({Key? key}) : super(key: key);
1214

13-
Map<CellKeyboardKey, CellKeyboardAction> get keyboardActionHandlers;
15+
Map<CellKeyboardKey, CellKeyboardAction> get shortcutHandlers;
1416
}
1517

1618
class GridCellShortcuts extends StatelessWidget {
@@ -20,9 +22,17 @@ class GridCellShortcuts extends StatelessWidget {
2022
@override
2123
Widget build(BuildContext context) {
2224
return Shortcuts(
23-
shortcuts: {LogicalKeySet(LogicalKeyboardKey.enter): const GridCellEnterIdent()},
25+
shortcuts: {
26+
LogicalKeySet(LogicalKeyboardKey.enter): const GridCellEnterIdent(),
27+
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyC): const GridCellCopyIntent(),
28+
LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyV): const GridCellInsertIntent(),
29+
},
2430
child: Actions(
25-
actions: {GridCellEnterIdent: GridCellEnterAction(child: child)},
31+
actions: {
32+
GridCellEnterIdent: GridCellEnterAction(child: child),
33+
GridCellCopyIntent: GridCellCopyAction(child: child),
34+
GridCellInsertIntent: GridCellInsertAction(child: child),
35+
},
2636
child: child,
2737
),
2838
);
@@ -39,7 +49,46 @@ class GridCellEnterAction extends Action<GridCellEnterIdent> {
3949

4050
@override
4151
void invoke(covariant GridCellEnterIdent intent) {
42-
final callback = child.keyboardActionHandlers[CellKeyboardKey.onEnter];
52+
final callback = child.shortcutHandlers[CellKeyboardKey.onEnter];
53+
if (callback != null) {
54+
callback();
55+
}
56+
}
57+
}
58+
59+
class GridCellCopyIntent extends Intent {
60+
const GridCellCopyIntent();
61+
}
62+
63+
class GridCellCopyAction extends Action<GridCellCopyIntent> {
64+
final CellShortcuts child;
65+
GridCellCopyAction({required this.child});
66+
67+
@override
68+
void invoke(covariant GridCellCopyIntent intent) {
69+
final callback = child.shortcutHandlers[CellKeyboardKey.onCopy];
70+
if (callback == null) {
71+
return;
72+
}
73+
74+
final s = callback();
75+
if (s is String) {
76+
Clipboard.setData(ClipboardData(text: s));
77+
}
78+
}
79+
}
80+
81+
class GridCellInsertIntent extends Intent {
82+
const GridCellInsertIntent();
83+
}
84+
85+
class GridCellInsertAction extends Action<GridCellInsertIntent> {
86+
final CellShortcuts child;
87+
GridCellInsertAction({required this.child});
88+
89+
@override
90+
void invoke(covariant GridCellInsertIntent intent) {
91+
final callback = child.shortcutHandlers[CellKeyboardKey.onEnter];
4392
if (callback != null) {
4493
callback();
4594
}

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flowy_infra_ui/style_widget/icon_button.dart';
55
import 'package:flutter/widgets.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
77
import 'cell_builder.dart';
8+
import 'cell_shortcuts.dart';
89

910
class CheckboxCell extends GridCellWidget {
1011
final GridCellContextBuilder cellContextBuilder;
@@ -24,7 +25,13 @@ class _CheckboxCellState extends GridCellState<CheckboxCell> {
2425
void initState() {
2526
final cellContext = widget.cellContextBuilder.build();
2627
_cellBloc = getIt<CheckboxCellBloc>(param1: cellContext)..add(const CheckboxCellEvent.initial());
27-
28+
widget.shortcutHandlers[CellKeyboardKey.onCopy] = () {
29+
if (_cellBloc.state.isSelected) {
30+
return "Yes";
31+
} else {
32+
return "No";
33+
}
34+
};
2835
super.initState();
2936
}
3037

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart';
12
import 'package:flowy_infra_ui/style_widget/text.dart';
23
import 'package:flutter/widgets.dart';
34
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -45,6 +46,7 @@ class _DateCellState extends State<DateCell> {
4546
void initState() {
4647
final cellContext = widget.cellContextBuilder.build();
4748
_cellBloc = getIt<DateCellBloc>(param1: cellContext)..add(const DateCellEvent.initial());
49+
widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.dateStr;
4850
super.initState();
4951
}
5052

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
77

88
import 'cell_builder.dart';
9+
import 'cell_shortcuts.dart';
910

1011
class NumberCell extends GridCellWidget {
1112
final GridCellContextBuilder cellContextBuilder;
@@ -29,6 +30,9 @@ class _NumberCellState extends GridFocusNodeCellState<NumberCell> {
2930
final cellContext = widget.cellContextBuilder.build();
3031
_cellBloc = getIt<NumberCellBloc>(param1: cellContext)..add(const NumberCellEvent.initial());
3132
_controller = TextEditingController(text: contentFromState(_cellBloc.state));
33+
widget.shortcutHandlers[CellKeyboardKey.onCopy] = () {
34+
return _cellBloc.state.content.fold((content) => content, (r) => null);
35+
};
3236
super.initState();
3337
}
3438

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
44
import 'package:app_flowy/startup/startup.dart';
55
import 'package:app_flowy/workspace/application/grid/prelude.dart';
66
import 'cell_builder.dart';
7+
import 'cell_shortcuts.dart';
78

89
class GridTextCellStyle extends GridCellStyle {
910
String? placeholder;
@@ -44,6 +45,7 @@ class _GridTextCellState extends GridFocusNodeCellState<GridTextCell> {
4445
_cellBloc.add(const TextCellEvent.initial());
4546
_controller = TextEditingController(text: _cellBloc.state.content);
4647

48+
widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.content;
4749
super.initState();
4850
}
4951

frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/url_cell/url_cell.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:app_flowy/generated/locale_keys.g.dart';
33
import 'package:app_flowy/workspace/application/grid/cell/url_cell_bloc.dart';
44
import 'package:app_flowy/workspace/presentation/home/toast.dart';
55
import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_accessory.dart';
6+
import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart';
67
import 'package:easy_localization/easy_localization.dart';
78
import 'package:flowy_infra/image.dart';
89
import 'package:flowy_infra/theme.dart';
@@ -86,6 +87,8 @@ class _GridURLCellState extends GridCellState<GridURLCell> {
8687
final cellContext = widget.cellContextBuilder.build() as GridURLCellContext;
8788
_cellBloc = URLCellBloc(cellContext: cellContext);
8889
_cellBloc.add(const URLCellEvent.initial());
90+
91+
widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.content;
8992
super.initState();
9093
}
9194

0 commit comments

Comments
 (0)