-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improves cursor left word delete (#88)
* refactor: add position_extension file * feat: move word/sentence del to new file * test: cursor left word delete * test: for cursor left sentence delete * chore: formatting arrow keys test * refactor: consolidate goLeft and goRight into one
- Loading branch information
1 parent
32ffcf4
commit 58ea6b0
Showing
7 changed files
with
496 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
enum SelectionRange { | ||
character, | ||
word, | ||
} | ||
|
||
extension PositionExtension on Position { | ||
Position? moveHorizontal( | ||
EditorState editorState, { | ||
bool moveLeft = true, | ||
SelectionRange selectionRange = SelectionRange.character, | ||
}) { | ||
final node = editorState.document.nodeAtPath(path); | ||
if (node == null) { | ||
return null; | ||
} | ||
|
||
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: moveLeft | ||
? node.delta.prevRunePosition(offset) | ||
: node.delta.nextRunePosition(offset), | ||
); | ||
} | ||
|
||
return Position(path: path, offset: offset); | ||
case SelectionRange.word: | ||
if (node is TextNode) { | ||
final result = moveLeft | ||
? node.selectable?.getWordBoundaryInPosition( | ||
Position( | ||
path: path, | ||
offset: node.delta.prevRunePosition(offset), | ||
), | ||
) | ||
: node.selectable?.getWordBoundaryInPosition(this); | ||
if (result != null) { | ||
return moveLeft ? result.start : result.end; | ||
} | ||
} | ||
|
||
return Position(path: path, offset: offset); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.