From ae6eda42d1f99ba5b9d33c39fffd9881ce048c51 Mon Sep 17 00:00:00 2001 From: Nurhan Turgut <50856934+nturgut@users.noreply.github.com> Date: Mon, 30 Sep 2019 08:26:28 -0700 Subject: [PATCH] Refactoring text_editing.dart (flutter/engine#12479) * Refacting text_editing.dart to use EditingState more efficiently and removing unused/unimplemented code. * Addressing PR comments. Comment changes. Removing unused methods. Method renaming. * commiting text_editing_test.dart changes. --- .../lib/src/engine/semantics/text_field.dart | 1 - .../web_ui/lib/src/engine/text_editing.dart | 309 +++++------------- .../lib/web_ui/test/text_editing_test.dart | 102 +++--- 3 files changed, 132 insertions(+), 280 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart index 580a83108a..107d9237c8 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart @@ -22,7 +22,6 @@ class TextField extends RoleManager { persistentTextEditingElement = PersistentTextEditingElement( textEditing, editableDomElement, - onDomElementSwap: _setupDomElement, ); _setupDomElement(); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing.dart index b7b2cf5a44..0a85acb861 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing.dart @@ -12,7 +12,7 @@ void _emptyCallback(dynamic _) {} /// These style attributes are constant throughout the life time of an input /// element. /// -/// They are assigned once during the creation of the dom element. +/// They are assigned once during the creation of the DOM element. void _setStaticStyleAttributes(html.HtmlElement domElement) { final html.CssStyleDeclaration elementStyle = domElement.style; elementStyle @@ -66,6 +66,29 @@ class EditingState { baseOffset = flutterEditingState['selectionBase'], extentOffset = flutterEditingState['selectionExtent']; + /// Creates an [EditingState] instance using values from the editing element + /// in the DOM. + /// + /// [domElement] can be a [InputElement] or a [TextAreaElement] depending on + /// the [InputType] of the text field. + factory EditingState.fromDomElement(html.HtmlElement domElement) { + if (domElement is html.InputElement) { + html.InputElement element = domElement; + return EditingState( + text: element.value, + baseOffset: element.selectionStart, + extentOffset: element.selectionEnd); + } else if (domElement is html.TextAreaElement) { + html.TextAreaElement element = domElement; + return EditingState( + text: element.value, + baseOffset: element.selectionStart, + extentOffset: element.selectionEnd); + } else { + throw UnsupportedError('Initialized with unsupported input type'); + } + } + /// The counterpart of [EditingState.fromFlutter]. It generates a Map that /// can be sent to Flutter. // TODO(mdebbar): Should we get `selectionAffinity` and other properties from flutter's editing state? @@ -110,6 +133,24 @@ class EditingState { ? 'EditingState("$text", base:$baseOffset, extent:$extentOffset)' : super.toString(); } + + /// Sets the selection values of a DOM element using this [EditingState]. + /// + /// [domElement] can be a [InputElement] or a [TextAreaElement] depending on + /// the [InputType] of the text field. + void applyToDomElement(html.HtmlElement domElement) { + if (domElement is html.InputElement) { + html.InputElement element = domElement; + element.value = text; + element.setSelectionRange(baseOffset, extentOffset); + } else if (domElement is html.TextAreaElement) { + html.TextAreaElement element = domElement; + element.value = text; + element.setSelectionRange(baseOffset, extentOffset); + } else { + throw UnsupportedError('Unsupported DOM element type'); + } + } } /// Various types of inputs used in text fields. @@ -163,33 +204,6 @@ class InputConfiguration { typedef _OnChangeCallback = void Function(EditingState editingState); -enum ElementType { - /// The backing element is an ``. - input, - - /// The backing element is a `