|
@@ -9,9 +9,10 @@ final List<CharacterShortcutEvent> codeBlockCharacterEvents = [
|
|
|
|
|
|
final List<CommandShortcutEvent> codeBlockCommands = [
|
|
|
insertNewParagraphNextToCodeBlockCommand,
|
|
|
+ pasteInCodeblock,
|
|
|
+ selectAllInCodeBlockCommand,
|
|
|
tabToInsertSpacesInCodeBlockCommand,
|
|
|
tabToDeleteSpacesInCodeBlockCommand,
|
|
|
- selectAllInCodeBlockCommand,
|
|
|
];
|
|
|
|
|
|
/// press the enter key in code block to insert a new line in it.
|
|
@@ -97,6 +98,18 @@ final CommandShortcutEvent selectAllInCodeBlockCommand = CommandShortcutEvent(
|
|
|
handler: _selectAllInCodeBlockCommandHandler,
|
|
|
);
|
|
|
|
|
|
+/// ctrl + v to paste text in code block.
|
|
|
+///
|
|
|
+/// - support
|
|
|
+/// - desktop
|
|
|
+/// - web
|
|
|
+final CommandShortcutEvent pasteInCodeblock = CommandShortcutEvent(
|
|
|
+ key: 'paste in codeblock',
|
|
|
+ command: 'ctrl+v',
|
|
|
+ macOSCommand: 'cmd+v',
|
|
|
+ handler: _pasteInCodeBlock,
|
|
|
+);
|
|
|
+
|
|
|
CharacterShortcutEventHandler _enterInCodeBlockCommandHandler =
|
|
|
(editorState) async {
|
|
|
final selection = editorState.selection;
|
|
@@ -267,3 +280,49 @@ CommandShortcutEventHandler _selectAllInCodeBlockCommandHandler =
|
|
|
|
|
|
return KeyEventResult.handled;
|
|
|
};
|
|
|
+
|
|
|
+CommandShortcutEventHandler _pasteInCodeBlock = (editorState) {
|
|
|
+ var selection = editorState.selection;
|
|
|
+
|
|
|
+ if (selection == null) {
|
|
|
+ return KeyEventResult.ignored;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (editorState.getNodesInSelection(selection).length != 1) {
|
|
|
+ return KeyEventResult.ignored;
|
|
|
+ }
|
|
|
+
|
|
|
+ final node = editorState.getNodeAtPath(selection.end.path);
|
|
|
+ if (node == null || node.type != CodeBlockKeys.type) {
|
|
|
+ return KeyEventResult.ignored;
|
|
|
+ }
|
|
|
+
|
|
|
+ // delete the selection first.
|
|
|
+ if (!selection.isCollapsed) {
|
|
|
+ editorState.deleteSelection(selection);
|
|
|
+ }
|
|
|
+
|
|
|
+ // fetch selection again.
|
|
|
+ selection = editorState.selection;
|
|
|
+ if (selection == null) {
|
|
|
+ return KeyEventResult.skipRemainingHandlers;
|
|
|
+ }
|
|
|
+ assert(selection.isCollapsed);
|
|
|
+
|
|
|
+ () async {
|
|
|
+ final data = await AppFlowyClipboard.getData();
|
|
|
+ final text = data.text;
|
|
|
+ if (text != null && text.isNotEmpty) {
|
|
|
+ final transaction = editorState.transaction
|
|
|
+ ..insertText(
|
|
|
+ node,
|
|
|
+ selection!.end.offset,
|
|
|
+ text,
|
|
|
+ );
|
|
|
+
|
|
|
+ await editorState.apply(transaction);
|
|
|
+ }
|
|
|
+ }();
|
|
|
+
|
|
|
+ return KeyEventResult.handled;
|
|
|
+};
|