Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: overwrite paste logic in code block

Lucas.Xu4fa2d6dc2ecb
diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart
index b998d0d4c0..1eca47a1b4 100644
--- a/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/pages/simple_editor.dart
@@ -50,6 +50,7 @@ class SimpleEditor extends StatelessWidget {
// Code Block
enterInCodeBlock,
ignoreKeysInCodeBlock,
+ pasteInCodeBlock,
],
selectionMenuItems: [
// Divider
diff --git a/frontend/app_flowy/packages/appflowy_editor_plugins/lib/src/code_block/code_block_shortcut_event.dart b/frontend/app_flowy/packages/appflowy_editor_plugins/lib/src/code_block/code_block_shortcut_event.dart
index a001698ec1..62ae1e44c5 100644
--- a/frontend/app_flowy/packages/appflowy_editor_plugins/lib/src/code_block/code_block_shortcut_event.dart
+++ b/frontend/app_flowy/packages/appflowy_editor_plugins/lib/src/code_block/code_block_shortcut_event.dart
@@ -1,6 +1,7 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor_plugins/src/code_block/code_block_node_widget.dart';
import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
ShortcutEvent enterInCodeBlock = ShortcutEvent(
key: 'Press Enter In Code Block',
@@ -14,6 +15,14 @@ ShortcutEvent ignoreKeysInCodeBlock = ShortcutEvent(
handler: _ignorekHandler,
);
+ShortcutEvent pasteInCodeBlock = ShortcutEvent(
+ key: 'Paste in code block',
+ command: 'meta+v',
+ windowsCommand: 'ctrl+v',
+ linuxCommand: 'ctrl+v',
+ handler: _pasteHandler,
+);
+
ShortcutEventHandler _enterInCodeBlockHandler = (editorState, event) {
final selection = editorState.service.selectionService.currentSelection.value;
final nodes = editorState.service.selectionService.currentSelectedNodes;
@@ -45,6 +54,30 @@ ShortcutEventHandler _ignorekHandler = (editorState, event) {
return KeyEventResult.ignored;
};
+ShortcutEventHandler _pasteHandler = (editorState, event) {
+ final selection = editorState.service.selectionService.currentSelection.value;
+ final nodes = editorState.service.selectionService.currentSelectedNodes;
+ final codeBlockNodes =
+ nodes.whereType<TextNode>().where((node) => node.id == kCodeBlockType);
+ if (selection != null &&
+ selection.isCollapsed &&
+ codeBlockNodes.length == 1) {
+ Clipboard.getData(Clipboard.kTextPlain).then((value) {
+ final text = value?.text;
+ if (text == null) return;
+ final transaction = editorState.transaction;
+ transaction.insertText(
+ codeBlockNodes.first,
+ selection.startIndex,
+ text,
+ );
+ editorState.apply(transaction);
+ });
+ return KeyEventResult.handled;
+ }
+ return KeyEventResult.ignored;
+};
+
SelectionMenuItem codeBlockMenuItem = SelectionMenuItem(
name: () => 'Code Block',
icon: (editorState, onSelected) => Icon(