Вход Регистрация
Файл: fckeditor/editor/_source/internals/fckundo.js
Строк: 251
<?php

/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2008 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 */

var FCKUndo = new Object() ;

FCKUndo.SavedData = new Array() ;
FCKUndo.CurrentIndex = -;
FCKUndo.TypesCount ;
FCKUndo.Changed false ;    // Is the document changed in respect to its initial image?
FCKUndo.MaxTypes 25 ;
FCKUndo.Typing false ;
FCKUndo.SaveLocked false ;

FCKUndo._GetBookmark = function()
{
    
FCKSelection.Restore() ;

    var 
range = new FCKDomRangeFCK.EditorWindow ) ;
    try
    {
        
// There are some tricky cases where this might fail (e.g. having a lone empty table in IE)
        
range.MoveToSelection() ;
    }
    catch ( 
)
    {
        return 
null ;
    }
    if ( 
FCKBrowserInfo.IsIE )
    {
        var 
bookmark range.CreateBookmark() ;
        var 
dirtyHtml FCK.EditorDocument.body.innerHTML ;
        
range.MoveToBookmarkbookmark ) ;
        return [ 
bookmarkdirtyHtml ] ;
    }
    return 
range.CreateBookmark2() ;
}

FCKUndo._SelectBookmark = function( bookmark )
{
    if ( ! 
bookmark )
        return ;

    var 
range = new FCKDomRangeFCK.EditorWindow ) ;
    if ( 
bookmark instanceof Object )
    {
        if ( 
FCKBrowserInfo.IsIE )
            
range.MoveToBookmarkbookmark[0] ) ;
        else
            
range.MoveToBookmark2bookmark ) ;
        try
        {
            
// this does not always succeed, there are still some tricky cases where it fails
            // e.g. add a special character at end of document, undo, redo -> error
            
range.Select() ;
        }
        catch ( 
)
        {
            
// if select restore fails, put the caret at the end of the document
            
range.MoveToPositionFCK.EditorDocument.body) ;
            
range.Select() ;
        }
    }
}

FCKUndo._CompareCursors = function( cursor1cursor2 )
{
    for ( var 
Math.mincursor1.lengthcursor2.length ) ; i++ )
    {
        if ( 
cursor1[i] < cursor2[i] )
            return -
1;
        else if (
cursor1[i] > cursor2[i] )
            return 
1;
    }
    if ( 
cursor1.length cursor2.length )
        return -
1;
    else if (
cursor1.length cursor2.length )
        return 
1;
    return 
0;
}

FCKUndo._CheckIsBookmarksEqual = function( bookmark1bookmark2 )
{
    if ( ! ( 
bookmark1 && bookmark2 ) )
        return 
false ;
    if ( 
FCKBrowserInfo.IsIE )
    {
        var 
startOffset1 bookmark1[1].searchbookmark1[0].StartId ) ;
        var 
startOffset2 bookmark2[1].searchbookmark2[0].StartId ) ;
        var 
endOffset1 bookmark1[1].searchbookmark1[0].EndId ) ;
        var 
endOffset2 bookmark2[1].searchbookmark2[0].EndId ) ;
        return 
startOffset1 == startOffset2 && endOffset1 == endOffset2 ;
    }
    else
    {
        return 
this._CompareCursorsbookmark1.Startbookmark2.Start ) == 0
            
&& this._CompareCursorsbookmark1.Endbookmark2.End ) == ;
    }
}

FCKUndo.SaveUndoStep = function()
{
    if ( 
FCK.EditMode != FCK_EDITMODE_WYSIWYG || this.SaveLocked )
        return ;

    
// Assume the editor content is changed when SaveUndoStep() is called after the first time.
    // This also enables the undo button in toolbar.
    
if ( this.SavedData.length )
        
this.Changed true ;

    
// Get the HTML content.
    
var sHtml FCK.EditorDocument.body.innerHTML ;
    var 
bookmark this._GetBookmark() ;

    
// Shrink the array to the current level.
    
this.SavedData this.SavedData.slice0this.CurrentIndex ) ;

    
// Cancel operation if the new step is identical to the previous one.
    
if ( this.CurrentIndex 0
            
&& sHtml == this.SavedDatathis.CurrentIndex ][0]
            && 
this._CheckIsBookmarksEqualbookmarkthis.SavedDatathis.CurrentIndex ][1] ) )
        return ;
    
// Save the selection and caret position in the first undo level for the first change.
    
else if ( this.CurrentIndex == && this.SavedData.length && sHtml == this.SavedData[0][0] )
    {
        
this.SavedData[0][1] = bookmark ;
        return ;
    }

    
// If we reach the Maximum number of undo levels, we must remove the first
    // entry of the list shifting all elements.
    
if ( this.CurrentIndex >= FCKConfig.MaxUndoLevels )
        
this.SavedData.shift() ;
    else
        
this.CurrentIndex++ ;

    
// Save the new level in front of the actual position.
    
this.SavedDatathis.CurrentIndex ] = [ sHtmlbookmark ] ;

    
FCK.Events.FireEvent"OnSelectionChange" ) ;
}

FCKUndo.CheckUndoState = function()
{
    return ( 
this.Changed || this.CurrentIndex ) ;
}

FCKUndo.CheckRedoState = function()
{
    return ( 
this.CurrentIndex < ( this.SavedData.length ) ) ;
}

FCKUndo.Undo = function()
{
    if ( 
this.CheckUndoState() )
    {
        
// If it is the first step.
        
if ( this.CurrentIndex == ( this.SavedData.length ) )
        {
            
// Save the actual state for a possible "Redo" call.
            
this.SaveUndoStep() ;
        }

        
// Go a step back.
        
this._ApplyUndoLevel( --this.CurrentIndex ) ;

        
FCK.Events.FireEvent"OnSelectionChange" ) ;
    }
}

FCKUndo.Redo = function()
{
    if ( 
this.CheckRedoState() )
    {
        
// Go a step forward.
        
this._ApplyUndoLevel( ++this.CurrentIndex ) ;

        
FCK.Events.FireEvent"OnSelectionChange" ) ;
    }
}

FCKUndo._ApplyUndoLevel = function( level )
{
    var 
oData this.SavedDatalevel ] ;

    if ( !
oData )
        return ;

    
// Update the editor contents with that step data.
    
if ( FCKBrowserInfo.IsIE )
    {
        if ( 
oData[1] && oData[1][1] )
            
FCK.SetInnerHtmloData[1][1] ) ;
        else
            
FCK.SetInnerHtmloData[0] ) ;
    }
    else
        
FCK.EditorDocument.body.innerHTML oData[0] ;

    
// Restore the selection
    
this._SelectBookmarkoData[1] ) ;

    
this.TypesCount ;
    
this.Changed false ;
    
this.Typing false ;
}
?>
Онлайн: 3
Реклама