Skip to content

Commit

Permalink
refactor: consolidate goLeft and goRight into one
Browse files Browse the repository at this point in the history
  • Loading branch information
MayurSMahajan committed Apr 22, 2023
1 parent 01a3d7b commit 86f1edc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 86 deletions.
80 changes: 43 additions & 37 deletions lib/src/extensions/position_extension.dart
Original file line number Diff line number Diff line change
@@ -1,88 +1,94 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';

enum SelectionRange {
character,
word,
}

extension PositionExtension on Position {
Position? goLeft(
Position? moveHorizontal(
EditorState editorState, {
bool moveLeft = true,
SelectionRange selectionRange = SelectionRange.character,
}) {
final node = editorState.document.nodeAtPath(path);
if (node == null) {
return null;
}

if (offset == 0) {
if (moveLeft && offset == 0) {
final previousEnd = node.previous?.selectable?.end();
if (previousEnd != null) {
return previousEnd;
}
return null;
} else if (!moveLeft) {
final end = node.selectable?.end();
if (end != null && offset >= end.offset) {
return node.next?.selectable?.start();
}
}

switch (selectionRange) {
case SelectionRange.character:
if (node is TextNode) {
return Position(
path: path,
offset: node.delta.prevRunePosition(offset),
offset: moveLeft
? node.delta.prevRunePosition(offset)
: node.delta.nextRunePosition(offset),
);
}

return Position(path: path, offset: offset);
case SelectionRange.word:
if (node is TextNode) {
final result = node.selectable?.getWordBoundaryInPosition(
Position(
path: path,
offset: node.delta.prevRunePosition(offset),
),
);
final result = moveLeft
? node.selectable?.getWordBoundaryInPosition(
Position(
path: path,
offset: node.delta.prevRunePosition(offset),
),
)
: node.selectable?.getWordBoundaryInPosition(this);
if (result != null) {
return result.start;
return moveLeft ? result.start : result.end;
}
}

return Position(path: path, offset: offset);
}
}

Position? goRight(
Position? moveVertical(
EditorState editorState, {
SelectionRange selectionRange = SelectionRange.character,
bool upwards = true,
}) {
final node = editorState.document.nodeAtPath(path);
if (node == null) {
final selection =
editorState.service.selectionService.currentSelection.value;
final rects = editorState.service.selectionService.selectionRects;
if (rects.isEmpty || selection == null) {
return null;
}

final end = node.selectable?.end();
if (end != null && offset >= end.offset) {
return node.next?.selectable?.start();
Offset offset;
if (selection.isBackward) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
offset = upwards
? rect.topRight.translate(0, -rect.height)
: rect.bottomRight.translate(0, rect.height);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
offset = upwards
? rect.topLeft.translate(0, -rect.height)
: rect.bottomLeft.translate(0, rect.height);
}

switch (selectionRange) {
case SelectionRange.character:
if (node is TextNode) {
return Position(
path: path,
offset: node.delta.nextRunePosition(offset),
);
}

return Position(path: path, offset: offset);
case SelectionRange.word:
if (node is TextNode) {
final result = node.selectable?.getWordBoundaryInPosition(this);
if (result != null) {
return result.end;
}
}

return Position(path: path, offset: offset);
}
return editorState.service.selectionService.getPositionInOffset(offset);
}
}
73 changes: 27 additions & 46 deletions lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ShortcutEventHandler cursorLeftSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = selection.end.goLeft(editorState);
final end = selection.end.moveHorizontal(editorState);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -23,7 +23,7 @@ ShortcutEventHandler cursorRightSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = selection.end.goRight(editorState);
final end = selection.end.moveHorizontal(editorState, moveLeft: false);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -39,7 +39,7 @@ ShortcutEventHandler cursorUpSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = _moveVertical(editorState);
final end = selection.end.moveVertical(editorState);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -55,7 +55,7 @@ ShortcutEventHandler cursorDownSelect = (editorState, event) {
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}
final end = _moveVertical(editorState, upwards: false);
final end = selection.end.moveVertical(editorState, upwards: false);
if (end == null) {
return KeyEventResult.ignored;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ ShortcutEventHandler cursorUp = (editorState, event) {
return KeyEventResult.ignored;
}

final upPosition = _moveVertical(editorState);
final upPosition = selection.end.moveVertical(editorState);
editorState.updateCursorSelection(
upPosition == null ? null : Selection.collapsed(upPosition),
);
Expand All @@ -241,7 +241,7 @@ ShortcutEventHandler cursorDown = (editorState, event) {
return KeyEventResult.ignored;
}

final downPosition = _moveVertical(editorState, upwards: false);
final downPosition = selection.end.moveVertical(editorState, upwards: false);
editorState.updateCursorSelection(
downPosition == null ? null : Selection.collapsed(downPosition),
);
Expand All @@ -258,7 +258,7 @@ ShortcutEventHandler cursorLeft = (editorState, event) {
}

Position newPosition = selection.isCollapsed
? selection.start.goLeft(editorState) ?? selection.start
? selection.start.moveHorizontal(editorState) ?? selection.start
: selection.start;

editorState.service.selectionService.updateSelection(
Expand All @@ -277,7 +277,8 @@ ShortcutEventHandler cursorRight = (editorState, event) {
}

final newPosition = selection.isCollapsed
? selection.start.goRight(editorState) ?? selection.end
? selection.start.moveHorizontal(editorState, moveLeft: false) ??
selection.end
: selection.end;

editorState.service.selectionService.updateSelection(
Expand All @@ -294,8 +295,10 @@ ShortcutEventHandler cursorLeftWordSelect = (editorState, event) {
return KeyEventResult.ignored;
}

final end =
selection.end.goLeft(editorState, selectionRange: SelectionRange.word);
final end = selection.end.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -316,8 +319,10 @@ ShortcutEventHandler cursorLeftWordMove = (editorState, event) {
return KeyEventResult.ignored;
}

final newPosition = selection.start
.goLeft(editorState, selectionRange: SelectionRange.word) ??
final newPosition = selection.start.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
) ??
selection.start;

editorState.service.selectionService.updateSelection(
Expand All @@ -336,8 +341,11 @@ ShortcutEventHandler cursorRightWordMove = (editorState, event) {
return KeyEventResult.ignored;
}

final newPosition = selection.start
.goRight(editorState, selectionRange: SelectionRange.word) ??
final newPosition = selection.start.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
moveLeft: false,
) ??
selection.end;

editorState.service.selectionService.updateSelection(
Expand All @@ -354,8 +362,11 @@ ShortcutEventHandler cursorRightWordSelect = (editorState, event) {
return KeyEventResult.ignored;
}

final end =
selection.end.goRight(editorState, selectionRange: SelectionRange.word);
final end = selection.end.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
moveLeft: false,
);
if (end == null) {
return KeyEventResult.ignored;
}
Expand All @@ -366,33 +377,3 @@ ShortcutEventHandler cursorRightWordSelect = (editorState, event) {

return KeyEventResult.handled;
};

Position? _moveVertical(
EditorState editorState, {
bool upwards = true,
}) {
final selection = editorState.service.selectionService.currentSelection.value;
final rects = editorState.service.selectionService.selectionRects;
if (rects.isEmpty || selection == null) {
return null;
}

Offset offset;
if (selection.isBackward) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
offset = upwards
? rect.topRight.translate(0, -rect.height)
: rect.bottomRight.translate(0, rect.height);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
offset = upwards
? rect.topLeft.translate(0, -rect.height)
: rect.bottomLeft.translate(0, rect.height);
}

return editorState.service.selectionService.getPositionInOffset(offset);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ ShortcutEventHandler cursorLeftWordDelete = (editorState, event) {
final textNode = textNodes.first;

//we store the position where the current word starts.
var startOfWord =
selection.end.goLeft(editorState, selectionRange: SelectionRange.word);
var startOfWord = selection.end.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
);

if (startOfWord == null) {
return KeyEventResult.ignored;
Expand All @@ -35,7 +37,7 @@ ShortcutEventHandler cursorLeftWordDelete = (editorState, event) {
);

//we need to check if this position is not null
final newStartOfWord = newSelection.end.goLeft(
final newStartOfWord = newSelection.end.moveHorizontal(
editorState,
selectionRange: SelectionRange.word,
);
Expand Down

0 comments on commit 86f1edc

Please sign in to comment.