Skip to content

Commit d44722c

Browse files
fix(change-event-highlight): update model on remove and add highlight
fix: on setVisibleSelection don't set the focus again It should work also with Firefox. On Comments otherwise the focus is set on the element instead on the comment writing the reason is the spellcheck which use the retainVisibleCollection
1 parent 56215ea commit d44722c

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

spec/highlighting.spec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ function setupHighlightEnv (context, text) {
1111
context.$div = $('<div>' + context.text + '</div>').appendTo(document.body)
1212
context.editable = new Editable()
1313
context.editable.add(context.$div)
14-
context.highlightRange = (highlightId, start, end) => {
14+
context.highlightRange = (highlightId, start, end, dispatcher) => {
1515
return highlightSupport.highlightRange(
1616
context.$div[0],
1717
highlightId,
18-
start, end
18+
start,
19+
end,
20+
dispatcher
21+
)
22+
}
23+
24+
context.removeHighlight = (highlightId, dispatcher) => {
25+
return highlightSupport.removeHighlight(
26+
context.$div[0],
27+
highlightId,
28+
dispatcher
1929
)
2030
}
2131

@@ -538,5 +548,22 @@ o Round</span>`)
538548
expect(this.getHtml()).toEqual(expectedHtml)
539549
expect(this.extract()).toEqual(expectedRanges)
540550
})
551+
552+
it('notify change on add highlight when dispatcher is given', () => {
553+
let called = 0
554+
const dispatcher = {notify: () => called++}
555+
this.highlightRange('first', 0, 20, dispatcher)
556+
557+
expect(called).toEqual(1)
558+
})
559+
560+
it('notify change on remove highlight when dispatcher is given', () => {
561+
let called = 0
562+
const dispatcher = {notify: () => called++}
563+
this.highlightRange('first', 0, 20)
564+
this.removeHighlight('first', dispatcher)
565+
566+
expect(called).toEqual(1)
567+
})
541568
})
542569
})

src/core.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ const Editable = module.exports = class Editable {
356356
* @param {Object} [options.textRange] An optional range which gets used to set the markers.
357357
* @param {Number} options.textRange.start
358358
* @param {Number} options.textRange.end
359+
* @param {Boolean} options.raiseEvents do throw change events
359360
* @return {Number} The text-based start offset of the newly applied highlight or `-1` if the range was considered invalid.
360361
*/
361-
highlight ({editableHost, text, highlightId, textRange}) {
362+
highlight ({editableHost, text, highlightId, textRange, raiseEvents}) {
362363
if (!textRange) {
363364
return highlightSupport.highlightText(editableHost, text, highlightId)
364365
}
@@ -374,7 +375,7 @@ const Editable = module.exports = class Editable {
374375
)
375376
return -1
376377
}
377-
return highlightSupport.highlightRange(editableHost, highlightId, textRange.start, textRange.end)
378+
return highlightSupport.highlightRange(editableHost, highlightId, textRange.start, textRange.end, raiseEvents ? this.dispatcher : undefined)
378379
}
379380

380381
/**
@@ -396,8 +397,8 @@ const Editable = module.exports = class Editable {
396397
)
397398
}
398399

399-
removeHighlight ({editableHost, highlightId}) {
400-
highlightSupport.removeHighlight(editableHost, highlightId)
400+
removeHighlight ({editableHost, highlightId, raiseEvents}) {
401+
highlightSupport.removeHighlight(editableHost, highlightId, raiseEvents ? this.dispatcher : undefined)
401402
}
402403

403404
decorateHighlight ({editableHost, highlightId, addCssClass, removeCssClass}) {

src/cursor.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ export default class Cursor {
9090
}
9191

9292
setVisibleSelection () {
93-
// Without setting focus() Firefox is not happy (seems setting a selection is not enough.
94-
// Probably because Firefox can handle multiple selections).
9593
if (this.win.document.activeElement !== this.host) {
9694
const {x, y} = viewport.getScrollPosition(this.win)
97-
$(this.host).focus()
9895
this.win.scrollTo(x, y)
9996
}
10097
rangy.getSelection(this.win).setSingleRange(this.range)

src/highlight-support.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const highlightSupport = {
3131
}
3232
},
3333

34-
highlightRange (editableHost, highlightId, startIndex, endIndex) {
34+
highlightRange (editableHost, highlightId, startIndex, endIndex, dispatcher) {
3535
if (this.hasHighlight(editableHost, highlightId)) {
3636
this.removeHighlight(editableHost, highlightId)
3737
}
@@ -52,6 +52,9 @@ const highlightSupport = {
5252
range.deleteContents()
5353
range.insertNode(marker)
5454
highlightSupport.cleanupStaleMarkerNodes(editableHost, 'comment')
55+
if (dispatcher) {
56+
dispatcher.notify('change', editableHost)
57+
}
5558
return startIndex
5659
},
5760

@@ -65,10 +68,13 @@ const highlightSupport = {
6568
})
6669
},
6770

68-
removeHighlight (editableHost, highlightId) {
71+
removeHighlight (editableHost, highlightId, dispatcher) {
6972
$(editableHost).find(`[data-word-id="${highlightId}"]`)
7073
.each((index, elem) => {
7174
content.unwrap(elem)
75+
if (dispatcher) {
76+
dispatcher.notify('change', editableHost)
77+
}
7278
})
7379
},
7480

0 commit comments

Comments
 (0)