-
Notifications
You must be signed in to change notification settings - Fork 6k
TextEditingDelta Support for the Web #28527
Changes from 1 commit
86885dd
9ef2c43
b93b043
d3201a1
9f99a66
8d69317
3ba145c
728055a
efbdf5b
1c330f3
0dd249a
37b757e
fd1396f
cb6ef96
4f610e0
f772bf1
f6e4d32
704d4f9
577aa59
0dcf1f2
a2e5525
95fe97b
5921215
a2a8b9d
1fbab86
928f186
03d63c3
51eabc8
125580e
26668cb
5a33dbf
e47a3ed
04020c2
13ca351
73c1411
be578c3
2df95b0
5e2b294
ac3407b
fa1d886
6ced401
15868f6
059fe74
26e676a
9196226
38bc244
cc0bb16
b7a91da
a600e9e
98fad47
786a528
3f260ce
b9db35a
41765f8
8ff4f2d
f4a858a
cbb6b6b
b596600
5a8a5f7
6645a0b
da90e99
6217ea9
b5e0b55
7a5aff9
c4c9cf6
9d97c88
0fc279d
fc823e9
6bf2ca7
acbc0de
7722380
0bb7f30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -440,6 +440,15 @@ class AutofillInfo { | |
| } | ||
| } | ||
|
|
||
| /// Replaces a range of text in the original string with the text given in the | ||
| /// replacement string. | ||
| String _replace(String originalText, String replacementText, int start, int end) { | ||
| final String textStart = originalText.substring(0, start); | ||
| final String textEnd = originalText.substring(end, originalText.length); | ||
| final String newText = textStart + replacementText + textEnd; | ||
| return newText; | ||
| } | ||
|
|
||
| class TextEditingDeltaState { | ||
| const TextEditingDeltaState({ | ||
| this.oldText = '', | ||
|
|
@@ -452,6 +461,80 @@ class TextEditingDeltaState { | |
| this.composingExtent = -1, | ||
|
||
| }); | ||
|
|
||
| static TextEditingDeltaState inferDeltaState(EditingState newEditingState, EditingState? lastEditingState, TextEditingDeltaState? lastTextEditingDeltaState) { | ||
|
||
| TextEditingDeltaState newTextEditingDeltaState = lastTextEditingDeltaState ?? TextEditingDeltaState(oldText: lastEditingState!.text!); | ||
|
|
||
| final bool previousSelectionWasCollapsed = lastEditingState?.baseOffset == lastEditingState?.extentOffset; | ||
|
|
||
| if (newTextEditingDeltaState.deltaText.isEmpty && newTextEditingDeltaState.deltaEnd != -1) { | ||
| // We are removing text. | ||
|
||
| final int deletedLength = newTextEditingDeltaState.oldText.length - newEditingState.text!.length; | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: newTextEditingDeltaState.deltaEnd - deletedLength); | ||
| } else if (newTextEditingDeltaState.deltaText.isNotEmpty && !previousSelectionWasCollapsed) { | ||
| // We are replacing text at a selection. | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: lastEditingState!.baseOffset); | ||
| } | ||
|
|
||
| // If we are composing then set the delta range to the composing region we captured in compositionupdate. | ||
|
||
| final bool isCurrentlyComposing = newTextEditingDeltaState.composingOffset != -1 && newTextEditingDeltaState.composingOffset != newTextEditingDeltaState.composingExtent; | ||
| if (newTextEditingDeltaState.deltaText.isNotEmpty && previousSelectionWasCollapsed && isCurrentlyComposing) { | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: newTextEditingDeltaState.composingOffset, deltaEnd: newTextEditingDeltaState.composingExtent); | ||
| } | ||
|
|
||
| final bool isDeltaRangeEmpty = newTextEditingDeltaState.deltaStart == -1 && newTextEditingDeltaState.deltaStart == newTextEditingDeltaState.deltaEnd; | ||
| if (!isDeltaRangeEmpty) { | ||
| // To verify the range of our delta we should compare the newEditingState's | ||
| // text with the delta applied to the oldText. If they differ then capture | ||
| // the correct delta range from the newEditingState's text value. | ||
| // | ||
| // We can assume the deltaText for additions and replacements to the text value | ||
| // are accurate. What may not be accurate is the range of the delta. | ||
| // | ||
| // We can think of the newEditingState as our source of truth. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this whole comment explaining the situation of double space inserting a period? Maybe add that as an example in the comment somewhere. And/or, be sure that this is thoroughly tested so that future developers can understand your intention (and not break it).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for this case and the accent menu case. |
||
| final String textAfterDelta = _replace( | ||
| newTextEditingDeltaState.oldText, newTextEditingDeltaState.deltaText, | ||
| newTextEditingDeltaState.deltaStart, | ||
| newTextEditingDeltaState.deltaEnd); | ||
| final bool isDeltaVerified = textAfterDelta == newEditingState.text!; | ||
|
|
||
| if (!isDeltaVerified) { | ||
| // 1. Find all matches for deltaText. | ||
| // 2. Apply matches/replacement to oldText until oldText matches the | ||
| // new editing state's text value. | ||
| final RegExp deltaTextPattern = RegExp(r'' + newTextEditingDeltaState.deltaText + r''); | ||
| for (final Match match in deltaTextPattern.allMatches(newEditingState.text!)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So in the example of double space inserting a period, the matches here will be all periods in the text, and you're trying to find the one that is new?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the case of double space inserting a period the When we have the matched ranges we iterate through them and apply the |
||
| String textAfterMatch; | ||
| int actualEnd; | ||
| final bool isMatchWithinOldTextBounds = match.start >= 0 && match.end <= newTextEditingDeltaState.oldText.length; | ||
| if (!isMatchWithinOldTextBounds) { | ||
| actualEnd = match.start + newTextEditingDeltaState.deltaText.length - 1; | ||
| textAfterMatch = _replace( | ||
| newTextEditingDeltaState.oldText, | ||
| newTextEditingDeltaState.deltaText, | ||
| match.start, | ||
| actualEnd, | ||
| ); | ||
| } else { | ||
| actualEnd = match.end; | ||
| textAfterMatch = _replace( | ||
| newTextEditingDeltaState.oldText, | ||
| newTextEditingDeltaState.deltaText, | ||
| match.start, | ||
| actualEnd, | ||
| ); | ||
| } | ||
|
|
||
| if (textAfterMatch == newEditingState.text!) { | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: match.start, deltaEnd: actualEnd); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return newTextEditingDeltaState; | ||
| } | ||
|
|
||
| final String oldText; | ||
| final String deltaText; | ||
| final int deltaStart; | ||
|
|
@@ -1083,88 +1166,11 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy { | |
| _appendedToForm = true; | ||
| } | ||
|
|
||
| /// Replaces a range of text in the original string with the text given in the | ||
| /// replacement string. | ||
| String _replace(String originalText, String replacementText, int start, int end) { | ||
| final String textStart = originalText.substring(0, start); | ||
| final String textEnd = originalText.substring(end, originalText.length); | ||
| final String newText = textStart + replacementText + textEnd; | ||
| return newText; | ||
| } | ||
|
|
||
| void handleChange(html.Event event) { | ||
| assert(isEnabled); | ||
|
|
||
| final EditingState newEditingState = EditingState.fromDomElement(activeDomElement); | ||
| TextEditingDeltaState newTextEditingDeltaState = lastTextEditingDeltaState ?? TextEditingDeltaState(oldText: lastEditingState!.text!); | ||
|
|
||
| final bool previousSelectionWasCollapsed = lastEditingState!.baseOffset == lastEditingState!.extentOffset; | ||
|
|
||
| if (newTextEditingDeltaState.deltaText.isEmpty && newTextEditingDeltaState.deltaEnd != -1) { | ||
| // We are removing text. | ||
| final int deletedLength = newTextEditingDeltaState.oldText.length - newEditingState.text!.length; | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: newTextEditingDeltaState.deltaEnd - deletedLength); | ||
| } else if (newTextEditingDeltaState.deltaText.isNotEmpty && !previousSelectionWasCollapsed) { | ||
| // We are replacing text at a selection. | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: lastEditingState!.baseOffset); | ||
| } | ||
|
|
||
| // If we are composing then set the delta range to the composing region we captured in compositionupdate. | ||
| final bool isCurrentlyComposing = newTextEditingDeltaState.composingOffset != -1 && newTextEditingDeltaState.composingOffset != newTextEditingDeltaState.composingExtent; | ||
| if (newTextEditingDeltaState.deltaText.isNotEmpty && previousSelectionWasCollapsed && isCurrentlyComposing) { | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: newTextEditingDeltaState.composingOffset, deltaEnd: newTextEditingDeltaState.composingExtent); | ||
| } | ||
|
|
||
| final bool isDeltaRangeEmpty = newTextEditingDeltaState.deltaStart == -1 && newTextEditingDeltaState.deltaStart == newTextEditingDeltaState.deltaEnd; | ||
| if (!isDeltaRangeEmpty) { | ||
| // To verify the range of our delta we should compare the newEditingState's | ||
| // text with the delta applied to the oldText. If they differ then capture | ||
| // the correct delta range from the newEditingState's text value. | ||
| // | ||
| // We can assume the deltaText for additions and replacements to the text value | ||
| // are accurate. What may not be accurate is the range of the delta. | ||
| // | ||
| // We can think of the newEditingState as our source of truth. | ||
| final String textAfterDelta = _replace( | ||
| newTextEditingDeltaState.oldText, newTextEditingDeltaState.deltaText, | ||
| newTextEditingDeltaState.deltaStart, | ||
| newTextEditingDeltaState.deltaEnd); | ||
| final bool isDeltaVerified = textAfterDelta == newEditingState.text!; | ||
|
|
||
| if (!isDeltaVerified) { | ||
| // 1. Find all matches for deltaText. | ||
| // 2. Apply matches/replacement to oldText until oldText matches the | ||
| // new editing state's text value. | ||
| final RegExp deltaTextPattern = RegExp(r'' + newTextEditingDeltaState.deltaText + r''); | ||
| for (final Match match in deltaTextPattern.allMatches(newEditingState.text!)) { | ||
| String textAfterMatch; | ||
| int actualEnd; | ||
| final bool isMatchWithinOldTextBounds = match.start >= 0 && match.end <= newTextEditingDeltaState.oldText.length; | ||
| if (!isMatchWithinOldTextBounds) { | ||
| actualEnd = match.start + newTextEditingDeltaState.deltaText.length - 1; | ||
| textAfterMatch = _replace( | ||
| newTextEditingDeltaState.oldText, | ||
| newTextEditingDeltaState.deltaText, | ||
| match.start, | ||
| actualEnd, | ||
| ); | ||
| } else { | ||
| actualEnd = match.end; | ||
| textAfterMatch = _replace( | ||
| newTextEditingDeltaState.oldText, | ||
| newTextEditingDeltaState.deltaText, | ||
| match.start, | ||
| actualEnd, | ||
| ); | ||
| } | ||
|
|
||
| if (textAfterMatch == newEditingState.text!) { | ||
| newTextEditingDeltaState = newTextEditingDeltaState.copyWith(deltaStart: match.start, deltaEnd: actualEnd); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| final TextEditingDeltaState newTextEditingDeltaState = TextEditingDeltaState.inferDeltaState(newEditingState, lastEditingState, lastTextEditingDeltaState); | ||
|
|
||
| if (newEditingState != lastEditingState) { | ||
| lastEditingState = newEditingState; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment explaining this class, and also a comment below explaining inferDeltaState?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the point of this class is to represent the json that will be sent to the framework, so maybe mention that in the comment.