Skip to content

Commit 4086b04

Browse files
author
Garry Yao
committed
Revised editor selection lock.
1 parent ee14287 commit 4086b04

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

core/selection.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
if ( sel.getType() == CKEDITOR.SELECTION_NONE )
1616
return;
1717

18-
// Before doing anything, save a locked copy to be used for later recovery.
19-
sel.lock();
20-
this._.lastSelection = sel;
18+
this.fire( 'selectionCheck', sel );
2119

2220
var currentPath = this.elementPath();
2321
if ( !currentPath.compare( this._.selectionPreviousPath ) ) {
@@ -206,8 +204,22 @@
206204
restoreSel = 0;
207205
}, null, null, -1 );
208206

207+
var lastSel;
208+
// Save a fresh copy of the selection.
209+
function saveSel() {
210+
lastSel = editor.getSelection( 1 );
211+
lastSel.lock();
212+
}
213+
214+
// For IE, we can retrieve the last correct DOM selection upon the "beforedeactivate" event.
215+
// For the rest, a more frequent check is required for each selection change made.
216+
if ( CKEDITOR.env.ie )
217+
editable.attachListener( editable, 'beforedeactivate', saveSel, null, null, -1 );
218+
else
219+
editable.attachListener( editor, 'selectionCheck', saveSel, null, null, -1 );
220+
209221
editable.attachListener( editable, 'blur', function() {
210-
editor.lockSelection();
222+
editor.lockSelection( lastSel );
211223
restoreSel = 1;
212224
}, null, null, -1 );
213225

@@ -497,19 +509,16 @@
497509
*
498510
* @method
499511
* @member CKEDITOR.editor
512+
* @param {CKEDITOR.dom.selection} [sel] Specify the selection to be locked.
500513
* @returns {Boolean} `true` if selection was locked.
501514
*/
502-
CKEDITOR.editor.prototype.lockSelection = function() {
503-
if ( !this._.savedSelection ) {
504-
var sel = this.getSelection( 1 );
505-
sel = sel.getType() == CKEDITOR.SELECTION_NONE ? this._.lastSelection : sel;
506-
if ( sel ) {
515+
CKEDITOR.editor.prototype.lockSelection = function( sel ) {
516+
sel = sel || this.getSelection( 1 );
517+
if ( sel.getType() != CKEDITOR.SELECTION_NONE ) {
507518
!sel.isLocked && sel.lock();
508519
this._.savedSelection = sel;
509520
return true;
510521
}
511-
}
512-
513522
return false;
514523
};
515524

@@ -731,7 +740,7 @@
731740
// object even when type == 'None' is returned by IE.
732741
// So we'd better check the object returned by
733742
// createRange() rather than by looking at the type.
734-
if ( sel.createRange().parentElement )
743+
if ( sel.createRange().parentElement() )
735744
type = CKEDITOR.SELECTION_TEXT;
736745
} catch ( e ) {}
737746

@@ -1519,10 +1528,10 @@
15191528
}
15201529
}
15211530

1522-
// Fakes the IE DOM event "selectionchange" on editable.
1523-
this.root.fire( 'selectionchange' );
15241531
this.reset();
15251532

1533+
// Fakes the IE DOM event "selectionchange" on editable.
1534+
this.root.fire( 'selectionchange' );
15261535
},
15271536

15281537
/**

0 commit comments

Comments
 (0)