-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consolidate goLeft and goRight into one
- Loading branch information
1 parent
01a3d7b
commit 86f1edc
Showing
3 changed files
with
75 additions
and
86 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
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); | ||
} | ||
} |
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