File tree Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -217,6 +217,15 @@ export default class Editable {
217217 return new Cursor ( $host [ 0 ] , range )
218218 }
219219
220+ createRangyRange ( ) {
221+ return rangy . createRange ( )
222+ }
223+
224+ createCursor ( element , range ) {
225+ const $host = $ ( element ) . closest ( this . editableSelector )
226+ return new Cursor ( $host [ 0 ] , range )
227+ }
228+
220229 createCursorAtBeginning ( element ) {
221230 return this . createCursor ( element , 'beginning' )
222231 }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import eventable from './eventable'
66import SelectionWatcher from './selection-watcher'
77import config from './config'
88import Keyboard from './keyboard'
9+ import { getTotalCharCount } from './util/element'
910
1011// This will be set to true once we detect the input event is working.
1112// Input event description on MDN:
@@ -165,17 +166,17 @@ export default class Dispatcher {
165166
166167 dispatchSwitchEvent ( event , element , direction ) {
167168 if ( event . altKey || event . ctrlKey || event . metaKey || event . shiftKey ) return
168-
169169 const cursor = this . selectionWatcher . getSelection ( )
170170 if ( ! cursor || cursor . isSelection ) return
171-
172- if ( direction === 'up' && cursor . isAtFirstLine ( ) ) {
171+
172+ const totalCharCount = getTotalCharCount ( element )
173+ if ( direction === 'up' && ( cursor . isAtFirstLine ( ) || totalCharCount === 0 ) ) {
173174 event . preventDefault ( )
174175 event . stopPropagation ( )
175176 this . notify ( 'switch' , element , direction , cursor )
176177 }
177178
178- if ( direction === 'down' && cursor . isAtLastLine ( ) ) {
179+ if ( direction === 'down' && ( cursor . isAtLastLine ( ) || totalCharCount === 0 ) ) {
179180 event . preventDefault ( )
180181 event . stopPropagation ( )
181182 this . notify ( 'switch' , element , direction , cursor )
Original file line number Diff line number Diff line change 1+ 'use strict'
2+ function textNodesUnder ( node ) {
3+ let all = [ ]
4+ for ( node = node . firstChild ; node ; node = node . nextSibling ) {
5+ if ( node . nodeType === 3 ) {
6+ all . push ( node )
7+ } else {
8+ all = all . concat ( textNodesUnder ( node ) )
9+ }
10+ }
11+ return all
12+ }
13+
14+ function getTotalCharCount ( element ) {
15+ const textNodes = textNodesUnder ( element )
16+ const reducer = ( acc , node ) => acc + node . textContent . length
17+ return textNodes . reduce ( reducer , 0 )
18+ }
19+
20+ module . exports = { getTotalCharCount}
You can’t perform that action at this time.
0 commit comments