@@ -21,7 +21,7 @@ export class DOMChange {
2121
2222 constructor ( view : EditorView , start : number , end : number , readonly typeOver : boolean ) {
2323 this . domChanged = start > - 1
24- let { impreciseHead : iHead , impreciseAnchor : iAnchor } = view . docView
24+ let { impreciseHead : iHead , impreciseAnchor : iAnchor } = view . docView , curSel = view . state . selection
2525 if ( view . state . readOnly && start > - 1 ) {
2626 // Ignore changes when the editor is read-only
2727 this . newSel = null
@@ -35,18 +35,18 @@ export class DOMChange {
3535 let domSel = view . observer . selectionRange
3636 let head = iHead && iHead . node == domSel . focusNode && iHead . offset == domSel . focusOffset ||
3737 ! contains ( view . contentDOM , domSel . focusNode )
38- ? view . state . selection . main . head
38+ ? curSel . main . head
3939 : view . docView . posFromDOM ( domSel . focusNode ! , domSel . focusOffset )
4040 let anchor = iAnchor && iAnchor . node == domSel . anchorNode && iAnchor . offset == domSel . anchorOffset ||
4141 ! contains ( view . contentDOM , domSel . anchorNode )
42- ? view . state . selection . main . anchor
42+ ? curSel . main . anchor
4343 : view . docView . posFromDOM ( domSel . anchorNode ! , domSel . anchorOffset )
4444 // iOS will refuse to select the block gaps when doing
4545 // select-all.
4646 // Chrome will put the selection *inside* them, confusing
4747 // posFromDOM
4848 let vp = view . viewport
49- if ( ( browser . ios || browser . chrome ) && view . state . selection . main . empty && head != anchor &&
49+ if ( ( browser . ios || browser . chrome ) && curSel . main . empty && head != anchor &&
5050 ( vp . from > 0 || vp . to < view . state . doc . length ) ) {
5151 let from = Math . min ( head , anchor ) , to = Math . max ( head , anchor )
5252 let offFrom = vp . from - from , offTo = vp . to - to
@@ -55,10 +55,19 @@ export class DOMChange {
5555 anchor = view . state . doc . length
5656 }
5757 }
58- if ( view . inputState . composing > - 1 && view . state . selection . ranges . length > 1 )
59- this . newSel = view . state . selection . replaceRange ( EditorSelection . range ( anchor , head ) )
60- else
58+ if ( view . inputState . composing > - 1 && curSel . ranges . length > 1 ) {
59+ this . newSel = curSel . replaceRange ( EditorSelection . range ( anchor , head ) )
60+ } else if ( view . lineWrapping && anchor == head && ! ( curSel . main . empty && curSel . main . head == head ) &&
61+ view . inputState . lastTouchTime > Date . now ( ) - 100 ) {
62+ // If this is a cursor selection change in a line-wrapping
63+ // editor that may have been a touch, use the last touch
64+ // position to assign a side to the cursor.
65+ let before = view . coordsAtPos ( head , - 1 ) , assoc = 0
66+ if ( before ) assoc = view . inputState . lastTouchY <= before . bottom ? - 1 : 1
67+ this . newSel = EditorSelection . create ( [ EditorSelection . cursor ( head , assoc ) ] )
68+ } else {
6169 this . newSel = EditorSelection . single ( anchor , head )
70+ }
6271 }
6372 }
6473}
0 commit comments