I have a list of items with varying indents like so:
- List item 1
- List item 2
- List item 3
- List item 2
I turn on contentEditable
when the user clicks a list item. Now, when the user presses the up
arrow key, I want to move the caret/word cursor from, say List item 3
to List item 2
, while maintaining the caret's x position.
To show this graphically,
intial caret pos:
- List item 2
- Li | st item 3
final caret pos after user presses up arrow. x offset remains same despite the difference in indent levels:
- List ite | m 2
- List item 3
The aim is to simulate the behavior of a multi-line text area where moving between lines using the arrow keys preserves the caret offset. You can also see this behavior while moving between list items in Google Tasks.
After referencing a number of related SO questions, I managed to get half way to solving the problem before giving up.
Anyone aware of an elegant solution?