|
196 | 196 |
|
197 | 197 | var isInline = editable.isInline();
|
198 | 198 |
|
| 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 | + |
199 | 225 | // Browsers could loose the selection once the editable lost focus,
|
200 | 226 | // in such case we need to reproduce it by saving a locked selection
|
201 | 227 | // and restoring it upon focus gain.
|
202 | 228 | 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 |
| - |
211 | 229 | var lastSel;
|
212 | 230 | // Save a fresh copy of the selection.
|
213 | 231 | function saveSel() {
|
|
226 | 244 | editor.lockSelection( lastSel );
|
227 | 245 | restoreSel = 1;
|
228 | 246 | }, null, null, -1 );
|
229 |
| - |
230 |
| - // Disable selection restoring when clicking in. |
231 |
| - editable.attachListener( editable, 'mousedown', function() { |
232 |
| - restoreSel = 0; |
233 |
| - }); |
234 | 247 | }
|
235 | 248 |
|
236 | 249 | // The following selection related fixes applies to only framed editable.
|
|
0 commit comments