Skip to content

Commit fd0647c

Browse files
committed
fix(cutandpaste): Cursor not moved
1 parent 1982f08 commit fd0647c

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

spec/dispatcher.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ describe('Dispatcher', function () {
383383
it(`replaces the last '&nbsp' with ' ' if text ends with a single '&nbsp'`, (done) => {
384384
on('paste', (block, blocks) => {
385385
expect(block.innerHTML).to
386-
.equal('some text that ends with a single a non breaking space ')
386+
.equal('some text that ends with a single a non breaking space ') // space has been removed
387+
expect(blocks.length).to.equal(1)
388+
expect(blocks[0]).to.equal('copied text') // copied text is still in the paste event
387389
done()
388390
})
389391
elem.innerHTML = 'some text that ends with a single a non breaking space '

src/dispatcher.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,11 @@ export default class Dispatcher {
136136
const {blocks, cursor} = clipboard.paste(block, selection, clipboardContent)
137137
if (blocks.length) {
138138
if (endsWithSingleSpace(evt.target.innerText)) {
139-
block.innerHTML = replaceLast(block.innerHTML, ' ', ' ')
140-
this.notify('paste', block, blocks, this.editable.createCursorAtEnd(block))
141-
} else {
142-
this.notify('paste', block, blocks, cursor)
139+
cursor.retainVisibleSelection(() => {
140+
block.innerHTML = replaceLast(block.innerHTML, ' ', ' ')
141+
})
143142
}
144-
143+
this.notify('paste', block, blocks, cursor)
145144
// The input event does not fire when we process the content manually
146145
// and insert it via script
147146
this.notify('change', block)

src/util/string.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function replaceLast (text, searchValue, replaceValue) {
6767
text = `${text}`
6868
if (!searchValue || replaceValue == null) return text
6969
const lastOccurrenceIndex = text.lastIndexOf(searchValue)
70+
if (lastOccurrenceIndex === -1) return text
7071
return `${
7172
text.slice(0, lastOccurrenceIndex)
7273
}${

0 commit comments

Comments
 (0)