Skip to content

Commit 7dcf471

Browse files
authored
Merge pull request #186 from livingdocsIO/fix-switch
Fix switch event
2 parents 3cba692 + 864dc63 commit 7dcf471

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

examples/events.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export default function (editable) {
183183
if (!isFromFirstExample(elem)) return
184184
showEvent({
185185
name: 'split',
186-
content: 'Split this block'
186+
content: <span>Split this block</span>
187187
})
188188
})
189189

@@ -195,7 +195,7 @@ export default function (editable) {
195195

196196
showEvent({
197197
name: 'merge',
198-
content: content
198+
content: <span>{content}</span>
199199
})
200200
})
201201

@@ -207,7 +207,7 @@ export default function (editable) {
207207

208208
showEvent({
209209
name: 'switch',
210-
content: content
210+
content: <span>{content}</span>
211211
})
212212
})
213213

src/cursor.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ export default class Cursor {
4545
)
4646
}
4747

48+
isAtLastLine () {
49+
const range = rangy.createRange()
50+
range.selectNodeContents(this.host)
51+
52+
const hostCoords = range.nativeRange.getBoundingClientRect()
53+
const cursorCoords = this.range.nativeRange.getBoundingClientRect()
54+
55+
return hostCoords.bottom === cursorCoords.bottom
56+
}
57+
58+
isAtFirstLine () {
59+
const range = rangy.createRange()
60+
range.selectNodeContents(this.host)
61+
62+
const hostCoords = range.nativeRange.getBoundingClientRect()
63+
const cursorCoords = this.range.nativeRange.getBoundingClientRect()
64+
65+
return hostCoords.top === cursorCoords.top
66+
}
67+
4868
isAtBeginning () {
4969
return parser.isBeginningOfHost(
5070
this.host,

src/dispatcher.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,18 @@ export default class Dispatcher {
168168

169169
const cursor = this.selectionWatcher.getSelection()
170170
if (!cursor || cursor.isSelection) return
171-
// Detect if the browser moved the cursor in the next tick.
172-
// If the cursor stays at its position, fire the switch event.
173-
setTimeout(() => {
174-
var newCursor = this.selectionWatcher.forceCursor()
175-
if (newCursor.equals(cursor)) {
176-
event.preventDefault()
177-
event.stopPropagation()
178-
this.notify('switch', element, direction, newCursor)
179-
}
180-
}, 0)
171+
172+
if (direction === 'up' && cursor.isAtFirstLine()) {
173+
event.preventDefault()
174+
event.stopPropagation()
175+
this.notify('switch', element, direction, cursor)
176+
}
177+
178+
if (direction === 'down' && cursor.isAtLastLine()) {
179+
event.preventDefault()
180+
event.stopPropagation()
181+
this.notify('switch', element, direction, cursor)
182+
}
181183
}
182184

183185
/**
@@ -205,16 +207,14 @@ export default class Dispatcher {
205207
const self = this
206208

207209
this.keyboard
208-
.on('left up', function (event) {
209-
self.dispatchSwitchEvent(event, this, 'before')
210+
.on('up', function (event) {
211+
self.dispatchSwitchEvent(event, this, 'up')
210212
})
211213

212-
.on('right down', function (event) {
213-
self.dispatchSwitchEvent(event, this, 'after')
214+
.on('down', function (event) {
215+
self.dispatchSwitchEvent(event, this, 'down')
214216
})
215217

216-
.on('tab shiftTab esc', () => {})
217-
218218
.on('backspace', function (event) {
219219
const range = self.selectionWatcher.getFreshRange()
220220
if (!range.isCursor) return self.triggerChangeEvent(this)

0 commit comments

Comments
 (0)