Skip to content

Commit dd770fd

Browse files
committed
Place the cursor at the end on focus
1 parent 6f07409 commit dd770fd

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/richText.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ define([
526526
}
527527
});
528528

529+
inputElement.addEventListener('focus', function () {
530+
const lastChild = inputElement.lastChild;
531+
if (lastChild && lastChild.length > 0) { // should always be true because of the tailing ZWSP
532+
const selection = window.getSelection();
533+
const range = document.createRange();
534+
range.setStart(lastChild, lastChild.length - 1);
535+
range.collapse(true);
536+
selection.removeAllRanges();
537+
selection.addRange(range);
538+
}
539+
});
540+
529541
function insertHTML(html) {
530542
if (window.getSelection) {
531543
const sel = window.getSelection();

tests/atwho.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ define([
186186
}
187187

188188
it("should show questions with #form", function() {
189-
displayAtwho('#form', function(mug) {
189+
displayAtwho('#form\u200B', function(mug) {
190190
var atwhoEntries = getDisplayedAtwhoViews().find('li'),
191191
tags = _.map(atwhoEntries, function(li) {
192192
return $.trim($(li).text()).replace(/\n[^]*$/, "");
@@ -200,7 +200,7 @@ define([
200200
});
201201

202202
it("should show questions after a dash", function () {
203-
displayAtwho('#dash-dash', function(mug) {
203+
displayAtwho('#dash-dash\u200B', function(mug) {
204204
var atwhoEntries = getDisplayedAtwhoViews().find('li'),
205205
tags = _.map(atwhoEntries, function(li) {
206206
return $.trim($(li).text()).replace(/\n[^]*$/, "");
@@ -210,7 +210,7 @@ define([
210210
});
211211

212212
it("should show case properties with #case", function() {
213-
displayAtwho('#case', function(mug) {
213+
displayAtwho('#case\u200B', function(mug) {
214214
var atwhoEntries = getDisplayedAtwhoViews().find('li'),
215215
tags = _.map(atwhoEntries, function(li) {
216216
return $.trim($(li).text()).replace(/\n[^]*$/, "");
@@ -220,7 +220,7 @@ define([
220220
});
221221

222222
it("should show case properties and form questions with #", function() {
223-
displayAtwho('#', function(mug) {
223+
displayAtwho('#\u200B', function(mug) {
224224
var atwhoEntries = getDisplayedAtwhoViews().find('li'),
225225
tags = _.map(atwhoEntries, function(li) {
226226
return $.trim($(li).text()).replace(/\n[^]*$/, "");

tests/richText.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,18 +638,22 @@ define([
638638
util.assertXmlEqual(call('createXML'), BURPEE_XML);
639639
});
640640

641-
// it("cursor should be at end of input on focus", function () {
642-
// var editor = richText.editor(widget.input),
643-
// value = 'testing cursor';
644-
// widget.setValue(value);
645-
// // Make sure focus is elsewhere, then focus on the rich text input
646-
// // editor.on('instanceReady', function() {
647-
// $('[name=property-nodeID]').focus();
648-
// editor.focus();
649-
// var selection = editor.getSelection(true);
650-
// assert.strictEqual(selection.getNative().focusOffset, value.length);
651-
// // });
652-
// });
641+
it("cursor should be at end of input on focus", function () {
642+
var editor = richText.editor(widget.input),
643+
value = 'testing cursor';
644+
widget.setValue(value);
645+
// Make sure focus is elsewhere, then focus on the rich text input
646+
// editor.on('instanceReady', function() {
647+
$('[name=property-nodeID]').focus();
648+
editor.focus();
649+
var selection = window.getSelection();
650+
assert.exists(selection);
651+
var range = selection.getRangeAt(0);
652+
assert.exists(range);
653+
assert.isTrue(range.collapsed);
654+
assert.strictEqual(range.startContainer, range.startContainer.parentNode.lastChild);
655+
assert.strictEqual(range.startContainer.textContent, "\u200B");
656+
});
653657

654658

655659
it("should change output ref to output value", function () {

0 commit comments

Comments
 (0)