Skip to content

Commit 0c22eb0

Browse files
committed
fix(switch): trigger when on empty editable
1 parent 1f29f49 commit 0c22eb0

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/dispatcher.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import eventable from './eventable'
66
import SelectionWatcher from './selection-watcher'
77
import config from './config'
88
import 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)

src/util/element.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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}

0 commit comments

Comments
 (0)