mirror of
https://github.com/AppFlowy-IO/AppFlowy
synced 2026-05-24 01:28:24 +00:00
feat: overwrite paste logic in code block
This commit is contained in:
parent
e476337a6a
commit
4fa2d6dc2e
2 changed files with 34 additions and 0 deletions
|
|
@ -50,6 +50,7 @@ class SimpleEditor extends StatelessWidget {
|
||||||
// Code Block
|
// Code Block
|
||||||
enterInCodeBlock,
|
enterInCodeBlock,
|
||||||
ignoreKeysInCodeBlock,
|
ignoreKeysInCodeBlock,
|
||||||
|
pasteInCodeBlock,
|
||||||
],
|
],
|
||||||
selectionMenuItems: [
|
selectionMenuItems: [
|
||||||
// Divider
|
// Divider
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import 'package:appflowy_editor/appflowy_editor.dart';
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
||||||
import 'package:appflowy_editor_plugins/src/code_block/code_block_node_widget.dart';
|
import 'package:appflowy_editor_plugins/src/code_block/code_block_node_widget.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
ShortcutEvent enterInCodeBlock = ShortcutEvent(
|
ShortcutEvent enterInCodeBlock = ShortcutEvent(
|
||||||
key: 'Press Enter In Code Block',
|
key: 'Press Enter In Code Block',
|
||||||
|
|
@ -14,6 +15,14 @@ ShortcutEvent ignoreKeysInCodeBlock = ShortcutEvent(
|
||||||
handler: _ignorekHandler,
|
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) {
|
ShortcutEventHandler _enterInCodeBlockHandler = (editorState, event) {
|
||||||
final selection = editorState.service.selectionService.currentSelection.value;
|
final selection = editorState.service.selectionService.currentSelection.value;
|
||||||
final nodes = editorState.service.selectionService.currentSelectedNodes;
|
final nodes = editorState.service.selectionService.currentSelectedNodes;
|
||||||
|
|
@ -45,6 +54,30 @@ ShortcutEventHandler _ignorekHandler = (editorState, event) {
|
||||||
return KeyEventResult.ignored;
|
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(
|
SelectionMenuItem codeBlockMenuItem = SelectionMenuItem(
|
||||||
name: () => 'Code Block',
|
name: () => 'Code Block',
|
||||||
icon: (editorState, onSelected) => Icon(
|
icon: (editorState, onSelected) => Icon(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue