mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: handle shift keys
This commit is contained in:
parent
883740d79a
commit
1d3e5a9e8b
@ -1,7 +1,5 @@
|
|||||||
import 'package:flowy_editor/document/node.dart';
|
import 'package:flowy_editor/flowy_editor.dart';
|
||||||
import 'package:flowy_editor/document/position.dart';
|
|
||||||
import 'package:flowy_editor/service/keyboard_service.dart';
|
import 'package:flowy_editor/service/keyboard_service.dart';
|
||||||
import 'package:flowy_editor/document/selection.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
@ -12,14 +10,65 @@ int _endOffsetOfNode(Node node) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
FlowyKeyEventHandler arrowKeysHandler = (editorState, event) {
|
KeyEventResult _handleShiftKey(EditorState editorState, RawKeyEvent event) {
|
||||||
if (event.logicalKey != LogicalKeyboardKey.arrowUp &&
|
final currentSelection = editorState.cursorSelection;
|
||||||
event.logicalKey != LogicalKeyboardKey.arrowDown &&
|
if (currentSelection == null) {
|
||||||
event.logicalKey != LogicalKeyboardKey.arrowLeft &&
|
|
||||||
event.logicalKey != LogicalKeyboardKey.arrowRight) {
|
|
||||||
return KeyEventResult.ignored;
|
return KeyEventResult.ignored;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
||||||
|
final leftPosition = _leftPosition(editorState, currentSelection.start);
|
||||||
|
if (leftPosition != null) {
|
||||||
|
editorState.updateCursorSelection(
|
||||||
|
Selection(start: leftPosition, end: currentSelection.end));
|
||||||
|
}
|
||||||
|
return KeyEventResult.handled;
|
||||||
|
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
|
||||||
|
final rightPosition = _rightPosition(editorState, currentSelection.end);
|
||||||
|
if (rightPosition != null) {
|
||||||
|
editorState.updateCursorSelection(
|
||||||
|
Selection(start: currentSelection.start, end: rightPosition));
|
||||||
|
}
|
||||||
|
return KeyEventResult.handled;
|
||||||
|
}
|
||||||
|
return KeyEventResult.ignored;
|
||||||
|
}
|
||||||
|
|
||||||
|
Position? _leftPosition(EditorState editorState, Position position) {
|
||||||
|
final offset = position.offset;
|
||||||
|
if (offset == 0) {
|
||||||
|
final node = editorState.document.nodeAtPath(position.path)!;
|
||||||
|
final prevNode = node.previous;
|
||||||
|
if (prevNode != null) {
|
||||||
|
editorState.updateCursorSelection(Selection.collapsed(
|
||||||
|
Position(path: prevNode.path, offset: _endOffsetOfNode(prevNode))));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position(path: position.path, offset: offset - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Position? _rightPosition(EditorState editorState, Position position) {
|
||||||
|
final offset = position.offset;
|
||||||
|
final node = editorState.document.nodeAtPath(position.path)!;
|
||||||
|
final lengthOfNode = _endOffsetOfNode(node);
|
||||||
|
if (offset >= lengthOfNode) {
|
||||||
|
final nextNode = node.next;
|
||||||
|
if (nextNode != null) {
|
||||||
|
Position(path: nextNode.path, offset: 0);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Position(path: position.path, offset: offset + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
FlowyKeyEventHandler arrowKeysHandler = (editorState, event) {
|
||||||
|
if (event.isShiftPressed) {
|
||||||
|
return _handleShiftKey(editorState, event);
|
||||||
|
}
|
||||||
|
|
||||||
final currentSelection = editorState.cursorSelection;
|
final currentSelection = editorState.cursorSelection;
|
||||||
if (currentSelection == null) {
|
if (currentSelection == null) {
|
||||||
return KeyEventResult.ignored;
|
return KeyEventResult.ignored;
|
||||||
@ -27,19 +76,10 @@ FlowyKeyEventHandler arrowKeysHandler = (editorState, event) {
|
|||||||
|
|
||||||
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
||||||
if (currentSelection.isCollapsed) {
|
if (currentSelection.isCollapsed) {
|
||||||
final end = currentSelection.end;
|
final leftPosition = _leftPosition(editorState, currentSelection.start);
|
||||||
final offset = end.offset;
|
if (leftPosition != null) {
|
||||||
if (offset == 0) {
|
editorState.updateCursorSelection(Selection.collapsed(leftPosition));
|
||||||
final node = editorState.document.nodeAtPath(end.path)!;
|
|
||||||
final prevNode = node.previous;
|
|
||||||
if (prevNode != null) {
|
|
||||||
editorState.updateCursorSelection(Selection.collapsed(Position(
|
|
||||||
path: prevNode.path, offset: _endOffsetOfNode(prevNode))));
|
|
||||||
}
|
|
||||||
return KeyEventResult.handled;
|
|
||||||
}
|
}
|
||||||
editorState.updateCursorSelection(
|
|
||||||
Selection.collapsed(Position(path: end.path, offset: offset - 1)));
|
|
||||||
} else {
|
} else {
|
||||||
editorState
|
editorState
|
||||||
.updateCursorSelection(currentSelection.collapse(atStart: true));
|
.updateCursorSelection(currentSelection.collapse(atStart: true));
|
||||||
@ -47,21 +87,10 @@ FlowyKeyEventHandler arrowKeysHandler = (editorState, event) {
|
|||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
|
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
|
||||||
if (currentSelection.isCollapsed) {
|
if (currentSelection.isCollapsed) {
|
||||||
final end = currentSelection.end;
|
final rightPosition = _rightPosition(editorState, currentSelection.end);
|
||||||
final offset = end.offset;
|
if (rightPosition != null) {
|
||||||
final node = editorState.document.nodeAtPath(end.path)!;
|
editorState.updateCursorSelection(Selection.collapsed(rightPosition));
|
||||||
final lengthOfNode = _endOffsetOfNode(node);
|
|
||||||
if (offset >= lengthOfNode) {
|
|
||||||
final nextNode = node.next;
|
|
||||||
if (nextNode != null) {
|
|
||||||
editorState.updateCursorSelection(
|
|
||||||
Selection.collapsed(Position(path: nextNode.path, offset: 0)));
|
|
||||||
}
|
|
||||||
return KeyEventResult.handled;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
editorState.updateCursorSelection(
|
|
||||||
Selection.collapsed(Position(path: end.path, offset: offset + 1)));
|
|
||||||
} else {
|
} else {
|
||||||
editorState.updateCursorSelection(currentSelection.collapse());
|
editorState.updateCursorSelection(currentSelection.collapse());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user