fix: emoji position

This commit is contained in:
Vincent Chan 2022-08-10 17:27:23 +08:00
parent bee1a04329
commit c554720dff
2 changed files with 37 additions and 4 deletions

View File

@ -475,10 +475,35 @@ class Delta extends Iterable<TextOperation> {
return _operations.map((e) => e.toJson()).toList();
}
String toRawString() {
int prevRunePosition(int pos) {
if (pos == 0) {
return pos;
}
_rawString ??=
_operations.whereType<TextInsert>().map((op) => op.content).join();
_runeIndexes ??= stringIndexes(_rawString!);
return _runeIndexes![pos - 1];
}
int nextRunePosition(int pos) {
final stringContent = toRawString();
if (pos >= stringContent.length - 1) {
return stringContent.length;
}
_runeIndexes ??= stringIndexes(_rawString!);
for (var i = pos + 1; i < _runeIndexes!.length; i++) {
if (_runeIndexes![i] != pos) {
return _runeIndexes![i];
}
}
return pos;
}
String toRawString() {
_rawString ??=
_operations.whereType<TextInsert>().map((op) => op.content).join();
return _rawString!;
}

View File

@ -12,8 +12,8 @@ int _endOffsetOfNode(Node node) {
extension on Position {
Position? goLeft(EditorState editorState) {
final node = editorState.document.nodeAtPath(path)!;
if (offset == 0) {
final node = editorState.document.nodeAtPath(path)!;
final prevNode = node.previous;
if (prevNode != null) {
return Position(
@ -22,7 +22,11 @@ extension on Position {
return null;
}
return Position(path: path, offset: offset - 1);
if (node is TextNode) {
return Position(path: path, offset: node.delta.prevRunePosition(offset));
} else {
return Position(path: path, offset: offset);
}
}
Position? goRight(EditorState editorState) {
@ -36,7 +40,11 @@ extension on Position {
return null;
}
return Position(path: path, offset: offset + 1);
if (node is TextNode) {
return Position(path: path, offset: node.delta.nextRunePosition(offset));
} else {
return Position(path: path, offset: offset);
}
}
}