add shortcuts

pull/874/head
joahyan 3 years ago
parent 1321190926
commit d7bf271bd0
  1. 13
      example/lib/main.dart
  2. 5
      example/lib/pages/home_page.dart
  3. 25
      lib/src/widgets/editor.dart
  4. 129
      lib/src/widgets/shortcuts/edit_shortcuts.dart
  5. 7
      lib/src/widgets/toolbar/emoji_button.dart
  6. 1
      lib/src/widgets/toolbar/toggle_style_button.dart

@ -1,9 +1,22 @@
/*
* @Author: joahyan joahyan@163.com
* @Date: 2022-07-18 10:10:08
* @LastEditors: joahyan joahyan@163.com
* @LastEditTime: 2022-07-19 10:16:42
* @FilePath: \flutter-quill\example\lib\main.dart
* @Description: ,`customMade`, koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'pages/home_page.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isMacOS) {
}
runApp(MyApp());
}

@ -71,6 +71,7 @@ class _HomePageState extends State<HomePage> {
body: RawKeyboardListener(
focusNode: FocusNode(),
onKey: (event) {
// print(event);
if (event.data.isControlPressed && event.character == 'b') {
if (_controller!
.getSelectionStyle()
@ -84,7 +85,9 @@ class _HomePageState extends State<HomePage> {
}
}
},
child: _buildWelcomeEditor(context),
child:
// GridEditShortcuts
_buildWelcomeEditor(context),
),
);
}

@ -22,6 +22,7 @@ import 'embeds/default_embed_builder.dart';
import 'float_cursor.dart';
import 'link.dart';
import 'raw_editor.dart';
import 'shortcuts/edit_shortcuts.dart';
import 'text_selection.dart';
/// Base interface for the editor state which defines contract used by
@ -468,21 +469,21 @@ class QuillEditorState extends State<QuillEditor>
initialLocale: widget.locale,
child: _selectionGestureDetectorBuilder.build(
behavior: HitTestBehavior.translucent,
child: child,
child: GridEditShortcuts(child: child,controller: widget.controller,),
));
if (kIsWeb) {
// Intercept RawKeyEvent on Web to prevent it from propagating to parents
// that might interfere with the editor key behavior, such as
// SingleChildScrollView. Thanks to @wliumelb for the workaround.
// See issue https://github.com/singerdmx/flutter-quill/issues/304
return RawKeyboardListener(
onKey: (_) {},
focusNode: FocusNode(
onKey: (node, event) => KeyEventResult.skipRemainingHandlers,
),
child: editor,
);
// Intercept RawKeyEvent on Web to prevent it from propagating to parents
// that might interfere with the editor key behavior, such as
// SingleChildScrollView. Thanks to @wliumelb for the workaround.
// See issue https://github.com/singerdmx/flutter-quill/issues/304
return RawKeyboardListener(
onKey: (_) {},
focusNode: FocusNode(
onKey: (node, event) => KeyEventResult.skipRemainingHandlers,
),
child: editor,
);
}
return editor;

@ -0,0 +1,129 @@
/*
* @Author: joahyan joahyan@163.com
* @Date: 2022-07-19 14:26:22
* @LastEditors: joahyan joahyan@163.com
* @LastEditTime: 2022-07-19 17:05:52
* @FilePath: \flutter-quill\lib\src\widgets\shortcuts\edit_shortcuts.dart
* @Description: ,`customMade`, koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../flutter_quill.dart';
typedef EditKeyboardAction = dynamic Function();
enum EditKeyboardKey {
onEnter,
onOverstriking,
onRecover,
}
abstract class EditShortcuts extends Widget {
const EditShortcuts({Key? key}) : super(key: key);
Map<EditKeyboardKey, EditKeyboardAction> get shortcutHandlers;
}
class GridEditShortcuts extends StatelessWidget {
final Widget child;
final QuillController controller;
LogicalKeyboardKey controlKey =
Platform.isMacOS ? LogicalKeyboardKey.meta : LogicalKeyboardKey.control;
GridEditShortcuts({
required this.child,
required this.controller,
});
@override
Widget build(BuildContext context) {
return Shortcuts(
shortcuts: {
// Ctrl + B
LogicalKeySet(
controlKey,
LogicalKeyboardKey.keyB,
): const OverstrikingIntent(),
// Ctrl + Z
LogicalKeySet(
controlKey,
LogicalKeyboardKey.keyZ,
): const RecoverIntent(),
// Ctrl + Alt + Z
LogicalKeySet(
controlKey,
LogicalKeyboardKey.alt,
LogicalKeyboardKey.keyZ,
): const UnRecoverIntent(),
},
child: Actions(
actions: {
OverstrikingIntent: OverstrikingAction(controller: controller),
RecoverIntent: RecoverAction(controller: controller),
UnRecoverIntent: UnRecoverAction(controller: controller),
},
child: child,
),
);
}
}
//
class OverstrikingIntent extends Intent {
const OverstrikingIntent();
}
class OverstrikingAction extends Action<OverstrikingIntent> {
OverstrikingAction({required this.controller});
final QuillController controller;
@override
void invoke(covariant OverstrikingIntent intent) {
if (controller.getSelectionStyle().attributes.keys.contains('bold')) {
controller.formatSelection(Attribute.clone(Attribute.bold, null));
} else {
controller.formatSelection(Attribute.bold);
}
}
}
//
class RecoverIntent extends Intent {
const RecoverIntent();
}
class RecoverAction extends Action<RecoverIntent> {
RecoverAction({required this.controller});
final QuillController controller;
@override
void invoke(covariant RecoverIntent intent) {
if (controller.hasUndo) {
controller.undo();
}
}
}
//
class UnRecoverIntent extends Intent {
const UnRecoverIntent();
}
class UnRecoverAction extends Action<UnRecoverIntent> {
UnRecoverAction({required this.controller});
final QuillController controller;
@override
void invoke(covariant UnRecoverIntent intent) {
if (controller.hasRedo) {
controller.redo();
}
}
}

@ -2,7 +2,7 @@
* @Author: joahyan joahyan@163.com
* @Date: 2022-07-18 12:34:30
* @LastEditors: joahyan joahyan@163.com
* @LastEditTime: 2022-07-18 16:39:08
* @LastEditTime: 2022-07-19 16:41:46
* @FilePath: \flutter-quill\lib\src\widgets\toolbar\emoji_button.dart
* @Description: ,`customMade`, koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
@ -102,10 +102,7 @@ class _EmojiButtonState extends State<EmojiButton> {
Widget build(BuildContext context) {
return QuillIconButton(
icon: Icon(widget.icon, size: widget.iconSize),
onPressed: () {
_showEmojiDialog();
print('aa');
},
onPressed: _showEmojiDialog,
);
}
}

@ -108,6 +108,7 @@ class _ToggleStyleButtonState extends State<ToggleStyleButton> {
widget.controller.formatSelection(_isToggled!
? Attribute.clone(widget.attribute, null)
: widget.attribute);
print(widget.controller.document.toDelta().toJson());
}
}

Loading…
Cancel
Save