Skip to content

Commit a9d4df7

Browse files
Garry Yaooleq
authored andcommitted
Put initial selection on first editable focus.
1 parent db1bfe8 commit a9d4df7

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

core/selection.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,36 @@
196196

197197
var isInline = editable.isInline();
198198

199+
var restoreSel;
200+
201+
// Give the editable an initial selection on first focus,
202+
// put selection at a consistent position at the start
203+
// of the contents. (#5156)
204+
editable.attachListener( 'focus', function( evt ) {
205+
evt.removeListener();
206+
207+
if ( restoreSel !== 0 ) {
208+
var rng = editor.createRange();
209+
rng.moveToElementEditStart( editable );
210+
rng.select();
211+
}
212+
}, null, null, -2 );
213+
214+
// Plays the magic here to restore/save dom selection on editable focus/blur.
215+
editable.attachListener( editable, 'focus', function() {
216+
editor.unlockSelection( restoreSel );
217+
restoreSel = 0;
218+
}, null, null, -1 );
219+
220+
// Disable selection restoring when clicking in.
221+
editable.attachListener( editable, 'mousedown', function() {
222+
restoreSel = 0;
223+
});
224+
199225
// Browsers could loose the selection once the editable lost focus,
200226
// in such case we need to reproduce it by saving a locked selection
201227
// and restoring it upon focus gain.
202228
if ( CKEDITOR.env.ie || CKEDITOR.env.opera || isInline ) {
203-
var restoreSel;
204-
205-
// Plays the magic here to restore/save dom selection on editable focus/blur.
206-
editable.attachListener( editable, 'focus', function() {
207-
editor.unlockSelection( restoreSel );
208-
restoreSel = 0;
209-
}, null, null, -1 );
210-
211229
var lastSel;
212230
// Save a fresh copy of the selection.
213231
function saveSel() {
@@ -226,11 +244,6 @@
226244
editor.lockSelection( lastSel );
227245
restoreSel = 1;
228246
}, null, null, -1 );
229-
230-
// Disable selection restoring when clicking in.
231-
editable.attachListener( editable, 'mousedown', function() {
232-
restoreSel = 0;
233-
});
234247
}
235248

236249
// The following selection related fixes applies to only framed editable.

0 commit comments

Comments
 (0)