Skip to content

Commit

Permalink
Merge branch 't/11098' into major
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Nov 8, 2013
2 parents b118c02 + 2a98224 commit 700ffd9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -50,6 +50,7 @@ Fixed Issues:
* [#11083](http://dev.ckeditor.com/ticket/11083): Fixed lists and divs application to block widgets.
* [#10887](http://dev.ckeditor.com/ticket/10887): Internet Explorer 8 compatibility issues related to the Widget System.
* [#11074](http://dev.ckeditor.com/ticket/11074): Temporarily disabled inline widgets drag and drop, because of seriously buggy native `range#moveToPoint` method.
* [#11098](http://dev.ckeditor.com/ticket/11098): Fixed wrong selection position after undoing widget drag and drop.

## CKEditor 4.3 Beta

Expand Down
47 changes: 31 additions & 16 deletions plugins/undo/plugin.js
Expand Up @@ -157,8 +157,13 @@
* @event lockSnapshot
* @member CKEDITOR.editor
* @param {CKEDITOR.editor} editor This editor instance.
* @param data
* @param {Boolean} [data.dontUpdate] When set to `true` the last snapshot will not be updated
* with the current contents and selection. Read more in the {@link CKEDITOR.plugins.undo.UndoManager#lock} method.
*/
editor.on( 'lockSnapshot', undoManager.lock, undoManager );
editor.on( 'lockSnapshot', function( evt ) {
undoManager.lock( evt.data && evt.data.dontUpdate );
} );

/**
* Unlocks the undo manager and updates the latest snapshot.
Expand Down Expand Up @@ -616,22 +621,32 @@
* **Note:** For every `lock` call you must call {@link #unlock} once to unlock the undo manager.
*
* @since 4.0
* @param {Boolean} [dontUpdate] When set to `true` the last snapashot will not be updated
* with the current contents and selection. By default, if undo manager was up to date when lock started,
* the last snapshot will be updated to the current state when unlocking. This means that all changes
* done during lock will be merged into the previous snapshot or the next one. Use this option, to gain
* more control over this behavior. For example, it is possible to group changes done during lock into
* separate snapshot.
*/
lock: function() {
lock: function( dontUpdate ) {
if ( !this.locked ) {
// Make a contents image. Don't include bookmarks, because:
// * we don't compare them,
// * there's a chance that DOM has been changed since
// locked (e.g. fake) selection was made, so createBookmark2 could fail.
// http://dev.ckeditor.com/ticket/11027#comment:3
var imageBefore = new Image( this.editor, true );

// If current editor content matches the tip of snapshot stack,
// the stack tip must be updated by unlock, to include any changes made
// during this period.
var matchedTip = this.currentImage && this.currentImage.equalsContent( imageBefore );

this.locked = { update: matchedTip ? imageBefore : null, level: 1 };
if ( dontUpdate )
this.locked = { level: 1 };
else {
// Make a contents image. Don't include bookmarks, because:
// * we don't compare them,
// * there's a chance that DOM has been changed since
// locked (e.g. fake) selection was made, so createBookmark2 could fail.
// http://dev.ckeditor.com/ticket/11027#comment:3
var imageBefore = new Image( this.editor, true );

// If current editor content matches the tip of snapshot stack,
// the stack tip must be updated by unlock, to include any changes made
// during this period.
var matchedTip = this.currentImage && this.currentImage.equalsContent( imageBefore );

this.locked = { update: matchedTip ? imageBefore : null, level: 1 };
}
}
// Increase the level of lock.
else
Expand All @@ -650,7 +665,7 @@
// Decrease level of lock and check if equals 0, what means that undoM is completely unlocked.
if ( !--this.locked.level ) {
var updateImage = this.locked.update,
newImage = new Image( this.editor, true );
newImage = updateImage && new Image( this.editor, true );

this.locked = null;

Expand Down
35 changes: 18 additions & 17 deletions plugins/widget/plugin.js
Expand Up @@ -1703,16 +1703,8 @@

function moveWidget( editor, sourceWidget ) {
var widgetHtml = sourceWidget.wrapper.getOuterHtml();

sourceWidget.wrapper.remove();
editor.widgets.destroy( sourceWidget, true );

// Create snapshot for the removed widget.
editor.fire( 'saveSnapshot' );

// Lock snapshot while pasting to merge those changes with the previous snapshot.
// This way we are grouping all changes done by moveWidget into one snapshot.
editor.fire( 'lockSnapshot' );
editor.execCommand( 'paste', widgetHtml );
editor.fire( 'unlockSnapshot' );
}
Expand Down Expand Up @@ -1938,10 +1930,13 @@
return;

// Save the snapshot with the state before moving widget.
// TODO unfortunately at this stage widget is not focused any more so
// undoing will not select widget which was moved.
// Focus widget, so when we'll undo the DnD, widget will be focused.
sourceWidget.focus();
editor.fire( 'saveSnapshot' );

// Lock snapshot to group all steps of moving widget from the original place to the new one.
editor.fire( 'lockSnapshot', { dontUpdate: true } );

moveSelectionToDropPosition( editor, evt );

// Hack to prevent cursor loss on Firefox. Without timeout widget is
Expand Down Expand Up @@ -2566,9 +2561,6 @@

locations, y;

// This will change DOM, save undo snapshot.
editor.fire( 'saveSnapshot' );

// Let's have the "dragging cursor" over entire editable.
editable.addClass( 'cke_widget_dragging' );

Expand Down Expand Up @@ -2609,6 +2601,13 @@
// Retrieve range for the closest location.
var range = finder.getRange( sorted[ 0 ] );

// Focus widget (it could lost focus after mousedown+mouseup)
// and save this state as the one where we want to be taken back when undoing.
this.focus();
editor.fire( 'saveSnapshot' );
// Group all following operations in one snapshot.
editor.fire( 'lockSnapshot', { dontUpdate: 1 } );

// Reset the fake selection, which will be invalidated by insertElementIntoRange.
// This avoids a situation when getSelection() still returns a fake selection made
// on widget which in the meantime has been moved to other place. That could cause
Expand All @@ -2618,13 +2617,15 @@
// Attach widget at the place determined by range.
editable.insertElementIntoRange( this.wrapper, range );

// DOM structure has been altered, save undo snapshot.
// Focus again the dropped widget.
this.focus();

// Unlock snapshot and save new one, which will contain all changes done
// in this method.
editor.fire( 'unlockSnapshot' );
editor.fire( 'saveSnapshot' );
}

// Focus again the dropped widget.
this.focus();

// Clean-up custom cursor for editable.
editable.removeClass( 'cke_widget_dragging' );

Expand Down

0 comments on commit 700ffd9

Please sign in to comment.