From b667c912a7109248f75c3b759c10ce12bcefe215 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Thu, 4 Aug 2022 17:51:20 +0800 Subject: [PATCH] feat: cut content --- .../copy_paste_handler.dart | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart index cde1c4e122..8cab09b293 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart @@ -212,8 +212,66 @@ _handlePastePlainText(EditorState editorState, String plainText) { } } -_handleCut() { +/// 1. copy the selected content +/// 2. delete selected content +_handleCut(EditorState editorState) { debugPrint('cut'); + final selection = editorState.cursorSelection; + if (selection == null) { + return; + } + + if (selection.isCollapsed) { + return; + } + + _handleCopy(editorState); + _deleteSelectedContent(editorState); +} + +_deleteSelectedContent(EditorState editorState) { + final selection = editorState.cursorSelection; + if (selection == null) { + return; + } + final beginNode = editorState.document.nodeAtPath(selection.start.path)!; + final endNode = editorState.document.nodeAtPath(selection.end.path)!; + if (pathEquals(selection.start.path, selection.end.path) && + beginNode.type == "text") { + final textItem = beginNode as TextNode; + final tb = TransactionBuilder(editorState); + final len = selection.end.offset - selection.start.offset; + tb.textEdit( + textItem, () => Delta().retain(selection.start.offset).delete(len)); + tb.setAfterSelection(Selection.collapsed(selection.start)); + tb.commit(); + return; + } + final traverser = NodeIterator(editorState.document, beginNode, endNode); + + final tb = TransactionBuilder(editorState); + while (traverser.moveNext()) { + final item = traverser.current; + if (item.type == "text" && beginNode == item) { + final textItem = item as TextNode; + final deleteLen = textItem.delta.length - selection.start.offset; + tb.textEdit(textItem, () { + final delta = Delta(); + delta.retain(selection.start.offset).delete(deleteLen); + + if (endNode is TextNode) { + final remain = endNode.delta.slice(selection.end.offset); + delta.addAll(remain.operations); + } + + return delta; + }); + tb.setAfterSelection(Selection.collapsed(selection.start)); + } else { + tb.deleteNode(item); + } + } + tb.commit(); } FlowyKeyEventHandler copyPasteKeysHandler = (editorState, event) { @@ -226,7 +284,7 @@ FlowyKeyEventHandler copyPasteKeysHandler = (editorState, event) { return KeyEventResult.handled; } if (event.isMetaPressed && event.logicalKey == LogicalKeyboardKey.keyX) { - _handleCut(); + _handleCut(editorState); return KeyEventResult.handled; } return KeyEventResult.ignored;