Skip to content

Commit 08067c3

Browse files
authored
Merge pull request #172 from livingdocsIO/fix-newline-behavior
Shift enter at end of text adds data tag to the added <br> tag
2 parents 41df23f + 374e008 commit 08067c3

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

spec/cursor.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ describe('Cursor', function () {
4747
expect(this.range.endOffset).toEqual(1)
4848
})
4949

50+
describe('isAtTextEnd()', () => {
51+
it('returns true when at text end', () => {
52+
expect(this.cursor.isAtEnd()).toBe(true)
53+
})
54+
})
55+
5056
describe('isAtEnd()', () => {
5157
it('is true', () => {
52-
expect(this.cursor.isAtEnd()).toBe(true)
58+
expect(this.cursor.isAtTextEnd()).toBe(true)
5359
})
5460
})
5561

@@ -66,6 +72,7 @@ describe('Cursor', function () {
6672
// move the cursor so we can check the restore method.
6773
this.cursor.moveAtBeginning()
6874
expect(this.cursor.isAtBeginning()).toBe(true)
75+
expect(this.cursor.isAtTextEnd()).toBe(false)
6976

7077
this.cursor.restore()
7178
expect(this.cursor.isAtEnd()).toBe(true)

spec/dispatcher.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,18 @@ describe('Dispatcher', () => {
195195
$elem.trigger(event)
196196
})
197197
})
198+
199+
describe('on newline', () => {
200+
beforeEach(() => {
201+
event = $.Event('keydown')
202+
event.shiftKey = true
203+
event.keyCode = 13
204+
})
205+
206+
it('fires newline when shift + enter is pressed', (done) => {
207+
on('newline', done)
208+
$elem.trigger(event)
209+
})
210+
})
198211
})
199212
})

src/create-default-behavior.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,13 @@ export default function createDefaultBehavior (editable) {
4848
},
4949

5050
newline (element, cursor) {
51-
cursor.insertBefore(document.createElement('br'))
5251

53-
if (cursor.isAtEnd()) {
54-
log('at the end')
55-
56-
const noWidthSpace = document.createTextNode('\u200B')
57-
cursor.insertAfter(noWidthSpace)
58-
59-
// var trailingBr = document.createElement('br')
60-
// trailingBr.setAttribute('type', '-editablejs')
61-
// cursor.insertAfter(trailingBr)
52+
if (cursor.isAtTextEnd()) {
53+
const trailingBr = document.createElement('br')
54+
trailingBr.setAttribute('data-editable', 'remove')
55+
cursor.insertBefore(trailingBr)
6256
} else {
63-
log('not at the end')
57+
cursor.insertBefore(document.createElement('br'))
6458
}
6559

6660
cursor.setVisibleSelection()

0 commit comments

Comments
 (0)