[1575] | 1 | /*
|
---|
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net
|
---|
| 3 | * Copyright (C) 2003-2009 Frederico Caldeira Knabben
|
---|
| 4 | *
|
---|
| 5 | * == BEGIN LICENSE ==
|
---|
| 6 | *
|
---|
| 7 | * Licensed under the terms of any of the following licenses at your
|
---|
| 8 | * choice:
|
---|
| 9 | *
|
---|
| 10 | * - GNU General Public License Version 2 or later (the "GPL")
|
---|
| 11 | * http://www.gnu.org/licenses/gpl.html
|
---|
| 12 | *
|
---|
| 13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
|
---|
| 14 | * http://www.gnu.org/licenses/lgpl.html
|
---|
| 15 | *
|
---|
| 16 | * - Mozilla Public License Version 1.1 or later (the "MPL")
|
---|
| 17 | * http://www.mozilla.org/MPL/MPL-1.1.html
|
---|
| 18 | *
|
---|
| 19 | * == END LICENSE ==
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | var FCKUndo = new Object() ;
|
---|
| 23 |
|
---|
| 24 | FCKUndo.SavedData = new Array() ;
|
---|
| 25 | FCKUndo.CurrentIndex = -1 ;
|
---|
| 26 | FCKUndo.TypesCount = 0 ;
|
---|
| 27 | FCKUndo.Changed = false ; // Is the document changed in respect to its initial image?
|
---|
| 28 | FCKUndo.MaxTypes = 25 ;
|
---|
| 29 | FCKUndo.Typing = false ;
|
---|
| 30 | FCKUndo.SaveLocked = false ;
|
---|
| 31 |
|
---|
| 32 | FCKUndo._GetBookmark = function()
|
---|
| 33 | {
|
---|
| 34 | FCKSelection.Restore() ;
|
---|
| 35 |
|
---|
| 36 | var range = new FCKDomRange( FCK.EditorWindow ) ;
|
---|
| 37 | try
|
---|
| 38 | {
|
---|
| 39 | // There are some tricky cases where this might fail (e.g. having a lone empty table in IE)
|
---|
| 40 | range.MoveToSelection() ;
|
---|
| 41 | }
|
---|
| 42 | catch ( e )
|
---|
| 43 | {
|
---|
| 44 | return null ;
|
---|
| 45 | }
|
---|
| 46 | if ( FCKBrowserInfo.IsIE )
|
---|
| 47 | {
|
---|
| 48 | var bookmark = range.CreateBookmark() ;
|
---|
| 49 | var dirtyHtml = FCK.EditorDocument.body.innerHTML ;
|
---|
| 50 | range.MoveToBookmark( bookmark ) ;
|
---|
| 51 | return [ bookmark, dirtyHtml ] ;
|
---|
| 52 | }
|
---|
| 53 | return range.CreateBookmark2() ;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | FCKUndo._SelectBookmark = function( bookmark )
|
---|
| 57 | {
|
---|
| 58 | if ( ! bookmark )
|
---|
| 59 | return ;
|
---|
| 60 |
|
---|
| 61 | var range = new FCKDomRange( FCK.EditorWindow ) ;
|
---|
| 62 | if ( bookmark instanceof Object )
|
---|
| 63 | {
|
---|
| 64 | if ( FCKBrowserInfo.IsIE )
|
---|
| 65 | range.MoveToBookmark( bookmark[0] ) ;
|
---|
| 66 | else
|
---|
| 67 | range.MoveToBookmark2( bookmark ) ;
|
---|
| 68 | try
|
---|
| 69 | {
|
---|
| 70 | // this does not always succeed, there are still some tricky cases where it fails
|
---|
| 71 | // e.g. add a special character at end of document, undo, redo -> error
|
---|
| 72 | range.Select() ;
|
---|
| 73 | }
|
---|
| 74 | catch ( e )
|
---|
| 75 | {
|
---|
| 76 | // if select restore fails, put the caret at the end of the document
|
---|
| 77 | range.MoveToPosition( FCK.EditorDocument.body, 4 ) ;
|
---|
| 78 | range.Select() ;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | FCKUndo._CompareCursors = function( cursor1, cursor2 )
|
---|
| 84 | {
|
---|
| 85 | for ( var i = 0 ; i < Math.min( cursor1.length, cursor2.length ) ; i++ )
|
---|
| 86 | {
|
---|
| 87 | if ( cursor1[i] < cursor2[i] )
|
---|
| 88 | return -1;
|
---|
| 89 | else if (cursor1[i] > cursor2[i] )
|
---|
| 90 | return 1;
|
---|
| 91 | }
|
---|
| 92 | if ( cursor1.length < cursor2.length )
|
---|
| 93 | return -1;
|
---|
| 94 | else if (cursor1.length > cursor2.length )
|
---|
| 95 | return 1;
|
---|
| 96 | return 0;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | FCKUndo._CheckIsBookmarksEqual = function( bookmark1, bookmark2 )
|
---|
| 100 | {
|
---|
| 101 | if ( ! ( bookmark1 && bookmark2 ) )
|
---|
| 102 | return false ;
|
---|
| 103 | if ( FCKBrowserInfo.IsIE )
|
---|
| 104 | {
|
---|
| 105 | var startOffset1 = bookmark1[1].search( bookmark1[0].StartId ) ;
|
---|
| 106 | var startOffset2 = bookmark2[1].search( bookmark2[0].StartId ) ;
|
---|
| 107 | var endOffset1 = bookmark1[1].search( bookmark1[0].EndId ) ;
|
---|
| 108 | var endOffset2 = bookmark2[1].search( bookmark2[0].EndId ) ;
|
---|
| 109 | return startOffset1 == startOffset2 && endOffset1 == endOffset2 ;
|
---|
| 110 | }
|
---|
| 111 | else
|
---|
| 112 | {
|
---|
| 113 | return this._CompareCursors( bookmark1.Start, bookmark2.Start ) == 0
|
---|
| 114 | && this._CompareCursors( bookmark1.End, bookmark2.End ) == 0 ;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | FCKUndo.SaveUndoStep = function()
|
---|
| 119 | {
|
---|
| 120 | if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || this.SaveLocked )
|
---|
| 121 | return ;
|
---|
| 122 |
|
---|
| 123 | // Assume the editor content is changed when SaveUndoStep() is called after the first time.
|
---|
| 124 | // This also enables the undo button in toolbar.
|
---|
| 125 | if ( this.SavedData.length )
|
---|
| 126 | this.Changed = true ;
|
---|
| 127 |
|
---|
| 128 | // Get the HTML content.
|
---|
| 129 | var sHtml = FCK.EditorDocument.body.innerHTML ;
|
---|
| 130 | var bookmark = this._GetBookmark() ;
|
---|
| 131 |
|
---|
| 132 | // Shrink the array to the current level.
|
---|
| 133 | this.SavedData = this.SavedData.slice( 0, this.CurrentIndex + 1 ) ;
|
---|
| 134 |
|
---|
| 135 | // Cancel operation if the new step is identical to the previous one.
|
---|
| 136 | if ( this.CurrentIndex > 0
|
---|
| 137 | && sHtml == this.SavedData[ this.CurrentIndex ][0]
|
---|
| 138 | && this._CheckIsBookmarksEqual( bookmark, this.SavedData[ this.CurrentIndex ][1] ) )
|
---|
| 139 | return ;
|
---|
| 140 | // Save the selection and caret position in the first undo level for the first change.
|
---|
| 141 | else if ( this.CurrentIndex == 0 && this.SavedData.length && sHtml == this.SavedData[0][0] )
|
---|
| 142 | {
|
---|
| 143 | this.SavedData[0][1] = bookmark ;
|
---|
| 144 | return ;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | // If we reach the Maximum number of undo levels, we must remove the first
|
---|
| 148 | // entry of the list shifting all elements.
|
---|
| 149 | if ( this.CurrentIndex + 1 >= FCKConfig.MaxUndoLevels )
|
---|
| 150 | this.SavedData.shift() ;
|
---|
| 151 | else
|
---|
| 152 | this.CurrentIndex++ ;
|
---|
| 153 |
|
---|
| 154 | // Save the new level in front of the actual position.
|
---|
| 155 | this.SavedData[ this.CurrentIndex ] = [ sHtml, bookmark ] ;
|
---|
| 156 |
|
---|
| 157 | FCK.Events.FireEvent( "OnSelectionChange" ) ;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | FCKUndo.CheckUndoState = function()
|
---|
| 161 | {
|
---|
| 162 | return ( this.Changed || this.CurrentIndex > 0 ) ;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | FCKUndo.CheckRedoState = function()
|
---|
| 166 | {
|
---|
| 167 | return ( this.CurrentIndex < ( this.SavedData.length - 1 ) ) ;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | FCKUndo.Undo = function()
|
---|
| 171 | {
|
---|
| 172 | if ( this.CheckUndoState() )
|
---|
| 173 | {
|
---|
| 174 | // If it is the first step.
|
---|
| 175 | if ( this.CurrentIndex == ( this.SavedData.length - 1 ) )
|
---|
| 176 | {
|
---|
| 177 | // Save the actual state for a possible "Redo" call.
|
---|
| 178 | this.SaveUndoStep() ;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | // Go a step back.
|
---|
| 182 | this._ApplyUndoLevel( --this.CurrentIndex ) ;
|
---|
| 183 |
|
---|
| 184 | FCK.Events.FireEvent( "OnSelectionChange" ) ;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | FCKUndo.Redo = function()
|
---|
| 189 | {
|
---|
| 190 | if ( this.CheckRedoState() )
|
---|
| 191 | {
|
---|
| 192 | // Go a step forward.
|
---|
| 193 | this._ApplyUndoLevel( ++this.CurrentIndex ) ;
|
---|
| 194 |
|
---|
| 195 | FCK.Events.FireEvent( "OnSelectionChange" ) ;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | FCKUndo._ApplyUndoLevel = function( level )
|
---|
| 200 | {
|
---|
| 201 | var oData = this.SavedData[ level ] ;
|
---|
| 202 |
|
---|
| 203 | if ( !oData )
|
---|
| 204 | return ;
|
---|
| 205 |
|
---|
| 206 | // Update the editor contents with that step data.
|
---|
| 207 | if ( FCKBrowserInfo.IsIE )
|
---|
| 208 | {
|
---|
| 209 | if ( oData[1] && oData[1][1] )
|
---|
| 210 | FCK.SetInnerHtml( oData[1][1] ) ;
|
---|
| 211 | else
|
---|
| 212 | FCK.SetInnerHtml( oData[0] ) ;
|
---|
| 213 | }
|
---|
| 214 | else
|
---|
| 215 | FCK.EditorDocument.body.innerHTML = oData[0] ;
|
---|
| 216 |
|
---|
| 217 | // Restore the selection
|
---|
| 218 | this._SelectBookmark( oData[1] ) ;
|
---|
| 219 |
|
---|
| 220 | this.TypesCount = 0 ;
|
---|
| 221 | this.Changed = false ;
|
---|
| 222 | this.Typing = false ;
|
---|
| 223 | }
|
---|