fix: could not delete character when using IME

This commit is contained in:
Lucas.Xu 2022-08-08 16:08:01 +08:00
parent 19fc154681
commit dc018bc7e4

View File

@ -2,8 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flowy_editor/document/node.dart'; import 'package:flowy_editor/document/node.dart';
import 'package:flowy_editor/document/path.dart';
import 'package:flowy_editor/document/position.dart';
import 'package:flowy_editor/document/selection.dart'; import 'package:flowy_editor/document/selection.dart';
import 'package:flowy_editor/editor_state.dart'; import 'package:flowy_editor/editor_state.dart';
import 'package:flowy_editor/extensions/node_extensions.dart'; import 'package:flowy_editor/extensions/node_extensions.dart';
@ -83,22 +81,29 @@ class _FlowyInputState extends State<FlowyInput>
void apply(List<TextEditingDelta> deltas) { void apply(List<TextEditingDelta> deltas) {
// TODO: implement the detail // TODO: implement the detail
for (final delta in deltas) { for (final delta in deltas) {
if (delta is TextEditingDeltaInsertion) { _updateComposing(delta);
if (_composingTextRange != null) {
_composingTextRange = TextRange(
start: _composingTextRange!.start,
end: delta.composing.end,
);
} else {
_composingTextRange = delta.composing;
}
if (delta is TextEditingDeltaInsertion) {
_applyInsert(delta); _applyInsert(delta);
} else if (delta is TextEditingDeltaDeletion) { } else if (delta is TextEditingDeltaDeletion) {
_applyDelete(delta);
} else if (delta is TextEditingDeltaReplacement) { } else if (delta is TextEditingDeltaReplacement) {
_applyReplacement(delta); _applyReplacement(delta);
} else if (delta is TextEditingDeltaNonTextUpdate) { } else if (delta is TextEditingDeltaNonTextUpdate) {}
_composingTextRange = null; }
}
void _updateComposing(TextEditingDelta delta) {
if (delta is! TextEditingDeltaNonTextUpdate) {
if (_composingTextRange != null &&
delta.composing.end != -1 &&
_composingTextRange!.start != -1) {
_composingTextRange = TextRange(
start: _composingTextRange!.start,
end: delta.composing.end,
);
} else {
_composingTextRange = delta.composing;
} }
} }
} }
@ -123,6 +128,23 @@ class _FlowyInputState extends State<FlowyInput>
} }
} }
void _applyDelete(TextEditingDeltaDeletion delta) {
final selectionService = _editorState.service.selectionService;
final currentSelection = selectionService.currentSelection.value;
if (currentSelection == null) {
return;
}
if (currentSelection.isSingle) {
final textNode = selectionService.currentSelectedNodes.first as TextNode;
final length = delta.deletedRange.end - delta.deletedRange.start;
TransactionBuilder(_editorState)
..deleteText(textNode, delta.deletedRange.start, length)
..commit();
} else {
// TODO: implement
}
}
void _applyReplacement(TextEditingDeltaReplacement delta) { void _applyReplacement(TextEditingDeltaReplacement delta) {
final selectionService = _editorState.service.selectionService; final selectionService = _editorState.service.selectionService;
final currentSelection = selectionService.currentSelection.value; final currentSelection = selectionService.currentSelection.value;