Merge pull request #764 from AppFlowy-IO/feat/cut-content

Feat: cut content
This commit is contained in:
Vincent Chan 2022-08-05 15:03:59 +08:00 committed by GitHub
commit 25475db6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 3 deletions

View File

@ -210,7 +210,7 @@ Operation transformOperation(Operation a, Operation b) {
if (a is InsertOperation) {
final newPath = transformPath(a.path, b.path);
return b.copyWithPath(newPath);
} else if (b is DeleteOperation) {
} else if (a is DeleteOperation) {
final newPath = transformPath(a.path, b.path, -1);
return b.copyWithPath(newPath);
}

View File

@ -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;