From bb22ca5d93138b7347c54c89a94cfae808a6ee9a Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 4 Jun 2022 11:44:38 +0800 Subject: [PATCH] chore: copy cell content --- .../grid/src/widgets/cell/cell_builder.dart | 6 +- .../grid/src/widgets/cell/cell_shortcuts.dart | 59 +++++++++++++++++-- .../grid/src/widgets/cell/checkbox_cell.dart | 9 ++- .../src/widgets/cell/date_cell/date_cell.dart | 2 + .../grid/src/widgets/cell/number_cell.dart | 4 ++ .../grid/src/widgets/cell/text_cell.dart | 2 + .../src/widgets/cell/url_cell/url_cell.dart | 3 + 7 files changed, 76 insertions(+), 9 deletions(-) diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart index 894eed2ac8..9a1911f003 100755 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_builder.dart @@ -62,7 +62,7 @@ abstract class GridCellWidget extends StatefulWidget implements CellAccessory, C final GridCellFocusListener beginFocus = GridCellFocusListener(); @override - final Map keyboardActionHandlers = {}; + final Map shortcutHandlers = {}; } abstract class GridCellState extends State { @@ -94,7 +94,7 @@ abstract class GridFocusNodeCellState extends GridCell @override void initState() { - widget.keyboardActionHandlers[CellKeyboardKey.onEnter] = () => focusNode.unfocus(); + widget.shortcutHandlers[CellKeyboardKey.onEnter] = () => focusNode.unfocus(); _listenOnFocusNodeChanged(); super.initState(); } @@ -109,7 +109,7 @@ abstract class GridFocusNodeCellState extends GridCell @override void dispose() { - widget.keyboardActionHandlers.remove(CellKeyboardKey.onEnter); + widget.shortcutHandlers.clear(); focusNode.removeAllListener(); focusNode.dispose(); super.dispose(); diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart index 7c122f17ba..2146c939d2 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart @@ -1,16 +1,18 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -typedef CellKeyboardAction = VoidCallback; +typedef CellKeyboardAction = dynamic Function(); enum CellKeyboardKey { onEnter, + onCopy, + onInsert, } abstract class CellShortcuts extends Widget { const CellShortcuts({Key? key}) : super(key: key); - Map get keyboardActionHandlers; + Map get shortcutHandlers; } class GridCellShortcuts extends StatelessWidget { @@ -20,9 +22,17 @@ class GridCellShortcuts extends StatelessWidget { @override Widget build(BuildContext context) { return Shortcuts( - shortcuts: {LogicalKeySet(LogicalKeyboardKey.enter): const GridCellEnterIdent()}, + shortcuts: { + LogicalKeySet(LogicalKeyboardKey.enter): const GridCellEnterIdent(), + LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyC): const GridCellCopyIntent(), + LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyV): const GridCellInsertIntent(), + }, child: Actions( - actions: {GridCellEnterIdent: GridCellEnterAction(child: child)}, + actions: { + GridCellEnterIdent: GridCellEnterAction(child: child), + GridCellCopyIntent: GridCellCopyAction(child: child), + GridCellInsertIntent: GridCellInsertAction(child: child), + }, child: child, ), ); @@ -39,7 +49,46 @@ class GridCellEnterAction extends Action { @override void invoke(covariant GridCellEnterIdent intent) { - final callback = child.keyboardActionHandlers[CellKeyboardKey.onEnter]; + final callback = child.shortcutHandlers[CellKeyboardKey.onEnter]; + if (callback != null) { + callback(); + } + } +} + +class GridCellCopyIntent extends Intent { + const GridCellCopyIntent(); +} + +class GridCellCopyAction extends Action { + final CellShortcuts child; + GridCellCopyAction({required this.child}); + + @override + void invoke(covariant GridCellCopyIntent intent) { + final callback = child.shortcutHandlers[CellKeyboardKey.onCopy]; + if (callback == null) { + return; + } + + final s = callback(); + if (s is String) { + Clipboard.setData(ClipboardData(text: s)); + } + } +} + +class GridCellInsertIntent extends Intent { + const GridCellInsertIntent(); +} + +class GridCellInsertAction extends Action { + final CellShortcuts child; + GridCellInsertAction({required this.child}); + + @override + void invoke(covariant GridCellInsertIntent intent) { + final callback = child.shortcutHandlers[CellKeyboardKey.onEnter]; if (callback != null) { callback(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart index 6f22a7d977..21f44bf2ab 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/checkbox_cell.dart @@ -5,6 +5,7 @@ import 'package:flowy_infra_ui/style_widget/icon_button.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'cell_builder.dart'; +import 'cell_shortcuts.dart'; class CheckboxCell extends GridCellWidget { final GridCellContextBuilder cellContextBuilder; @@ -24,7 +25,13 @@ class _CheckboxCellState extends GridCellState { void initState() { final cellContext = widget.cellContextBuilder.build(); _cellBloc = getIt(param1: cellContext)..add(const CheckboxCellEvent.initial()); - + widget.shortcutHandlers[CellKeyboardKey.onCopy] = () { + if (_cellBloc.state.isSelected) { + return "Yes"; + } else { + return "No"; + } + }; super.initState(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart index eed45464bf..ecc297e4c4 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/date_cell/date_cell.dart @@ -1,3 +1,4 @@ +import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart'; import 'package:flowy_infra_ui/style_widget/text.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -45,6 +46,7 @@ class _DateCellState extends State { void initState() { final cellContext = widget.cellContextBuilder.build(); _cellBloc = getIt(param1: cellContext)..add(const DateCellEvent.initial()); + widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.dateStr; super.initState(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart index 88aaf5cb3b..3983a7d3c3 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/number_cell.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'cell_builder.dart'; +import 'cell_shortcuts.dart'; class NumberCell extends GridCellWidget { final GridCellContextBuilder cellContextBuilder; @@ -29,6 +30,9 @@ class _NumberCellState extends GridFocusNodeCellState { final cellContext = widget.cellContextBuilder.build(); _cellBloc = getIt(param1: cellContext)..add(const NumberCellEvent.initial()); _controller = TextEditingController(text: contentFromState(_cellBloc.state)); + widget.shortcutHandlers[CellKeyboardKey.onCopy] = () { + return _cellBloc.state.content.fold((content) => content, (r) => null); + }; super.initState(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart index 6a9fcde98f..48b5628bbd 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/text_cell.dart @@ -4,6 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:app_flowy/startup/startup.dart'; import 'package:app_flowy/workspace/application/grid/prelude.dart'; import 'cell_builder.dart'; +import 'cell_shortcuts.dart'; class GridTextCellStyle extends GridCellStyle { String? placeholder; @@ -44,6 +45,7 @@ class _GridTextCellState extends GridFocusNodeCellState { _cellBloc.add(const TextCellEvent.initial()); _controller = TextEditingController(text: _cellBloc.state.content); + widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.content; super.initState(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/url_cell/url_cell.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/url_cell/url_cell.dart index 5caa014df0..4b4e671901 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/url_cell/url_cell.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/grid/src/widgets/cell/url_cell/url_cell.dart @@ -3,6 +3,7 @@ import 'package:app_flowy/generated/locale_keys.g.dart'; import 'package:app_flowy/workspace/application/grid/cell/url_cell_bloc.dart'; import 'package:app_flowy/workspace/presentation/home/toast.dart'; import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_accessory.dart'; +import 'package:app_flowy/workspace/presentation/plugins/grid/src/widgets/cell/cell_shortcuts.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra/image.dart'; import 'package:flowy_infra/theme.dart'; @@ -86,6 +87,8 @@ class _GridURLCellState extends GridCellState { final cellContext = widget.cellContextBuilder.build() as GridURLCellContext; _cellBloc = URLCellBloc(cellContext: cellContext); _cellBloc.add(const URLCellEvent.initial()); + + widget.shortcutHandlers[CellKeyboardKey.onCopy] = () => _cellBloc.state.content; super.initState(); }