-
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import 'dart:async'; | ||
| import 'dart:html' as html; | ||
| import 'dart:js_util' as js_util; | ||
| import 'dart:math' as math; | ||
| import 'dart:typed_data'; | ||
|
|
||
|
|
@@ -439,6 +440,51 @@ class AutofillInfo { | |
| } | ||
| } | ||
|
|
||
| class TextEditingDeltaState { | ||
| const TextEditingDeltaState({ | ||
| this.oldText = '', | ||
| this.deltaText = '', | ||
| this.deltaStart = -1, | ||
| this.deltaEnd = -1, | ||
| this.baseOffset = -1, | ||
| this.extentOffset = -1, | ||
| }); | ||
|
|
||
| final String oldText; | ||
| final String deltaText; | ||
| final int deltaStart; | ||
| final int deltaEnd; | ||
| final int baseOffset; | ||
| final int extentOffset; | ||
|
|
||
| Map<String, dynamic> toFlutter() => <String, dynamic>{ | ||
| 'oldText': oldText, | ||
| 'deltaText': deltaText, | ||
| 'deltaStart': deltaStart, | ||
| 'deltaEnd': deltaEnd, | ||
| 'selectionBase': baseOffset, | ||
| 'selectionExtent': extentOffset, | ||
| }; | ||
|
|
||
| TextEditingDeltaState copyWith({ | ||
| String? oldText, | ||
| String? deltaText, | ||
| int? deltaStart, | ||
| int? deltaEnd, | ||
| int? baseOffset, | ||
| int? extentOffset, | ||
| }) { | ||
| return TextEditingDeltaState( | ||
| oldText: oldText ?? this.oldText, | ||
| deltaText: deltaText ?? this.deltaText, | ||
| deltaStart: deltaStart ?? this.deltaStart, | ||
| deltaEnd: deltaEnd ?? this.deltaEnd, | ||
| baseOffset: baseOffset ?? this.baseOffset, | ||
| extentOffset: extentOffset ?? this.extentOffset, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// The current text and selection state of a text field. | ||
| class EditingState { | ||
| EditingState({this.text, int? baseOffset, int? extentOffset}) : | ||
|
|
@@ -610,6 +656,7 @@ class InputConfiguration { | |
| const TextCapitalizationConfig.defaultCapitalization(), | ||
| this.autofill, | ||
| this.autofillGroup, | ||
| this.enableDeltaModel = false, | ||
| }); | ||
|
|
||
| InputConfiguration.fromFrameworkMessage( | ||
|
|
@@ -633,7 +680,8 @@ class InputConfiguration { | |
| autofillGroup = EngineAutofillForm.fromFrameworkMessage( | ||
| flutterInputConfiguration.tryJson('autofill'), | ||
| flutterInputConfiguration.tryList('fields'), | ||
| ); | ||
| ), | ||
| enableDeltaModel = flutterInputConfiguration.tryBool('enableDeltaModel') ?? false; | ||
|
|
||
| /// The type of information being edited in the input control. | ||
| final EngineInputType inputType; | ||
|
|
@@ -658,14 +706,16 @@ class InputConfiguration { | |
| /// supported by Safari. | ||
| final bool autocorrect; | ||
|
|
||
| final bool enableDeltaModel; | ||
|
|
||
| final AutofillInfo? autofill; | ||
|
|
||
| final EngineAutofillForm? autofillGroup; | ||
|
|
||
| final TextCapitalizationConfig textCapitalization; | ||
| } | ||
|
|
||
| typedef OnChangeCallback = void Function(EditingState? editingState); | ||
| typedef OnChangeCallback = void Function(EditingState? editingState, TextEditingDeltaState? editingDeltaState); | ||
|
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. I think it makes sense to add this parameter rather than creating a new type as long as nothing is broken by it. |
||
| typedef OnActionCallback = void Function(String? inputAction); | ||
|
|
||
| /// Provides HTML DOM functionality for editable text. | ||
|
|
@@ -849,6 +899,8 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy { | |
| late InputConfiguration inputConfiguration; | ||
| EditingState? lastEditingState; | ||
|
|
||
| TextEditingDeltaState? lastTextEditingDeltaState; | ||
|
||
|
|
||
| /// Styles associated with the editable text. | ||
| EditableTextStyle? style; | ||
|
|
||
|
|
@@ -947,6 +999,22 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy { | |
|
|
||
| subscriptions.add(html.document.onSelectionChange.listen(handleChange)); | ||
|
|
||
| activeDomElement.addEventListener('beforeinput', (event) { | ||
| String eventData = js_util.getProperty(event, 'data').toString(); | ||
| if (eventData == 'null') { | ||
| eventData = ''; | ||
| } | ||
| final TextEditingDeltaState newDeltaState = lastTextEditingDeltaState ?? TextEditingDeltaState(); | ||
|
|
||
| if (eventData == '') { | ||
| lastTextEditingDeltaState = newDeltaState.copyWith(oldText: lastEditingState!.text, deltaText: eventData, deltaStart: (lastEditingState!.extentOffset ?? -1) - 1, deltaEnd: lastEditingState!.extentOffset); | ||
| } else { | ||
| lastTextEditingDeltaState = newDeltaState.copyWith(oldText: lastEditingState!.text, deltaText: eventData, deltaStart: lastEditingState!.extentOffset, deltaEnd: lastEditingState!.extentOffset); | ||
| } | ||
| print('beforeinput delta: ' + js_util.getProperty(event, 'data').toString() + ' delta length: ' + js_util.getProperty(event, 'data').toString().length.toString()); | ||
| print('target ranges: ' + js_util.callMethod(event, 'getTargetRanges', []).toString()); | ||
| }); | ||
|
|
||
| // Refocus on the activeDomElement after blur, so that user can keep editing the | ||
| // text field. | ||
| subscriptions.add(activeDomElement.onBlur.listen((_) { | ||
|
|
@@ -978,6 +1046,7 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy { | |
|
|
||
| isEnabled = false; | ||
| lastEditingState = null; | ||
| lastTextEditingDeltaState = null; | ||
| style = null; | ||
| geometry = null; | ||
|
|
||
|
|
@@ -1022,10 +1091,12 @@ abstract class DefaultTextEditingStrategy implements TextEditingStrategy { | |
| assert(isEnabled); | ||
|
|
||
| final EditingState newEditingState = EditingState.fromDomElement(activeDomElement); | ||
| final TextEditingDeltaState newTextEditingDeltaState = lastTextEditingDeltaState ?? TextEditingDeltaState(); | ||
|
|
||
| if (newEditingState != lastEditingState) { | ||
| lastEditingState = newEditingState; | ||
| onChange!(lastEditingState); | ||
| lastTextEditingDeltaState = newTextEditingDeltaState; | ||
| onChange!(lastEditingState, lastTextEditingDeltaState); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1731,6 +1802,20 @@ class TextEditingChannel { | |
| ); | ||
| } | ||
|
|
||
| /// Sends the 'TextInputClient.updateEditingStateWithDeltas' message to the framework. | ||
| void updateEditingStateWithDelta(int? clientId, TextEditingDeltaState? editingDeltaState) { | ||
| EnginePlatformDispatcher.instance.invokeOnPlatformMessage( | ||
| 'flutter/textinput', | ||
| const JSONMethodCodec().encodeMethodCall( | ||
| MethodCall('TextInputClient.updateEditingStateWithDeltas', <dynamic>[ | ||
| clientId, | ||
| editingDeltaState!.toFlutter(), | ||
| ]), | ||
| ), | ||
| _emptyCallback, | ||
| ); | ||
| } | ||
|
|
||
| /// Sends the 'TextInputClient.performAction' message to the framework. | ||
| void performAction(int? clientId, String? inputAction) { | ||
| EnginePlatformDispatcher.instance.invokeOnPlatformMessage( | ||
|
|
@@ -1824,8 +1909,15 @@ class HybridTextEditing { | |
| isEditing = true; | ||
| strategy.enable( | ||
| configuration!, | ||
| onChange: (EditingState? editingState) { | ||
| channel.updateEditingState(_clientId, editingState); | ||
| onChange: (EditingState? editingState, TextEditingDeltaState? editingDeltaState) { | ||
| if (configuration!.enableDeltaModel) { | ||
| print('delta model is enabled'); | ||
| editingDeltaState = editingDeltaState!.copyWith(baseOffset: editingState!.baseOffset, extentOffset: editingState.extentOffset); | ||
| channel.updateEditingStateWithDelta(_clientId, editingDeltaState); | ||
| } else { | ||
| print('we should never be here'); | ||
| channel.updateEditingState(_clientId, editingState); | ||
| } | ||
| }, | ||
| onAction: (String? inputAction) { | ||
| channel.performAction(_clientId, inputAction); | ||
|
|
||
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.