The problem with the AttributedTextEditingController API #544
Replies: 2 comments 4 replies
-
@venkatd @miguelcmedeiros - I'd like to get your thoughts on this proposal. Do you think a use-case-oriented text field controller makes sense? Or do you think the API surface might be too constraining, or otherwise create new problems? |
Beta Was this translation helpful? Give feedback.
-
Speaking for SuperTextField, but I think it can apply to all of the editors. Taking an event-sourced approach with commands/edits may allow the kind of flexibility needed without introducing a large surface API. So I'd consider #49 and #67 related to this. The summary would be:
Some implications of this:
We've implemented this pattern in our own app and it has been straightforward for us to add a layer on top of SuperTextField that works as expected on multiple platforms (keyboard navigation, shortcuts, etc) and implement undo/redo. I can document link to it this month. On a related note, are there any performance implications to making |
Beta Was this translation helpful? Give feedback.
-
There's a problem with the API for
AttributedTextEditingController
. Here's an example of the problem.Imagine a text field with the following content, and caret position:
In this example "
@John
" is a user name token. It's a string that's treated as a single character, from the perspective of text selection. As such, if the caret is ever placed within a token, the caret needs to be immediately moved outside the token. Imagine that we've implemented a sub-class ofAttributedTextEditingController
that enforces these selection rules. What should that subclass do when the caret appears as follows?The caret sits between "@" and "John", which is illegal. The controller needs to move that caret somewhere. Where should it move? Well, it depends...
If the user tapped at that location, then the caret needs to move back to the beginning of the token:
However, if the user pressed the right arrow key, then the caret should "jump over" the token, placing the caret on the trailing end of the token:
How do we know which action to take?
A problem with
AttributedTextEditingController
is that it combines use-case oriented behaviors, likemoveCaret
, with direct property manipulation, likeset selection
. If a client changes a selection withcontroller.selection = mySelection
, then the controller can't possibly know which use-case rules to enforce.Proposal
I think we should remove the setters for
text
andselection
and force all controller changes to include some useful amount of context. Here's a possible API surface for the controller:Beta Was this translation helpful? Give feedback.
All reactions