[795] | 1 | /* |
---|
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
| 3 | * Copyright (C) 2003-2007 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 | * Creation and initialization of the "FCK" object. This is the main |
---|
| 22 | * object that represents an editor instance. |
---|
| 23 | * (Gecko specific implementations) |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | FCK.Description = "FCKeditor for Gecko Browsers" ; |
---|
| 27 | |
---|
| 28 | FCK.InitializeBehaviors = function() |
---|
| 29 | { |
---|
| 30 | // When calling "SetHTML", the editing area IFRAME gets a fixed height. So we must recaulculate it. |
---|
| 31 | if ( FCKBrowserInfo.IsGecko ) // Not for Safari/Opera. |
---|
| 32 | Window_OnResize() ; |
---|
| 33 | |
---|
| 34 | FCKFocusManager.AddWindow( this.EditorWindow ) ; |
---|
| 35 | |
---|
| 36 | this.ExecOnSelectionChange = function() |
---|
| 37 | { |
---|
| 38 | FCK.Events.FireEvent( "OnSelectionChange" ) ; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | this.ExecOnSelectionChangeTimer = function() |
---|
| 42 | { |
---|
| 43 | if ( FCK.LastOnChangeTimer ) |
---|
| 44 | window.clearTimeout( FCK.LastOnChangeTimer ) ; |
---|
| 45 | |
---|
| 46 | FCK.LastOnChangeTimer = window.setTimeout( FCK.ExecOnSelectionChange, 100 ) ; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | this.EditorDocument.addEventListener( 'mouseup', this.ExecOnSelectionChange, false ) ; |
---|
| 50 | |
---|
| 51 | // On Gecko, firing the "OnSelectionChange" event on every key press started to be too much |
---|
| 52 | // slow. So, a timer has been implemented to solve performance issues when tipying to quickly. |
---|
| 53 | this.EditorDocument.addEventListener( 'keyup', this.ExecOnSelectionChangeTimer, false ) ; |
---|
| 54 | |
---|
| 55 | this._DblClickListener = function( e ) |
---|
| 56 | { |
---|
| 57 | FCK.OnDoubleClick( e.target ) ; |
---|
| 58 | e.stopPropagation() ; |
---|
| 59 | } |
---|
| 60 | this.EditorDocument.addEventListener( 'dblclick', this._DblClickListener, true ) ; |
---|
| 61 | |
---|
| 62 | // Reset the context menu. |
---|
| 63 | FCK.ContextMenu._InnerContextMenu.SetMouseClickWindow( FCK.EditorWindow ) ; |
---|
| 64 | FCK.ContextMenu._InnerContextMenu.AttachToElement( FCK.EditorDocument ) ; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | FCK.MakeEditable = function() |
---|
| 68 | { |
---|
| 69 | this.EditingArea.MakeEditable() ; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // Disable the context menu in the editor (outside the editing area). |
---|
| 73 | function Document_OnContextMenu( e ) |
---|
| 74 | { |
---|
| 75 | if ( !e.target._FCKShowContextMenu ) |
---|
| 76 | e.preventDefault() ; |
---|
| 77 | } |
---|
| 78 | document.oncontextmenu = Document_OnContextMenu ; |
---|
| 79 | |
---|
| 80 | // GetNamedCommandState overload for Gecko. |
---|
| 81 | FCK._BaseGetNamedCommandState = FCK.GetNamedCommandState ; |
---|
| 82 | FCK.GetNamedCommandState = function( commandName ) |
---|
| 83 | { |
---|
| 84 | switch ( commandName ) |
---|
| 85 | { |
---|
| 86 | case 'Unlink' : |
---|
| 87 | return FCKSelection.HasAncestorNode('A') ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ; |
---|
| 88 | default : |
---|
| 89 | return FCK._BaseGetNamedCommandState( commandName ) ; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // Named commands to be handled by this browsers specific implementation. |
---|
| 94 | FCK.RedirectNamedCommands = |
---|
| 95 | { |
---|
| 96 | Print : true, |
---|
| 97 | Paste : true, |
---|
| 98 | Cut : true, |
---|
| 99 | Copy : true |
---|
| 100 | } ; |
---|
| 101 | |
---|
| 102 | // ExecuteNamedCommand overload for Gecko. |
---|
| 103 | FCK.ExecuteRedirectedNamedCommand = function( commandName, commandParameter ) |
---|
| 104 | { |
---|
| 105 | switch ( commandName ) |
---|
| 106 | { |
---|
| 107 | case 'Print' : |
---|
| 108 | FCK.EditorWindow.print() ; |
---|
| 109 | break ; |
---|
| 110 | case 'Paste' : |
---|
| 111 | try { if ( FCK.Paste() ) FCK.ExecuteNamedCommand( 'Paste', null, true ) ; } |
---|
| 112 | catch (e) { FCKDialog.OpenDialog( 'FCKDialog_Paste', FCKLang.Paste, 'dialog/fck_paste.html', 400, 330, 'Security' ) ; } |
---|
| 113 | break ; |
---|
| 114 | case 'Cut' : |
---|
| 115 | try { FCK.ExecuteNamedCommand( 'Cut', null, true ) ; } |
---|
| 116 | catch (e) { alert(FCKLang.PasteErrorCut) ; } |
---|
| 117 | break ; |
---|
| 118 | case 'Copy' : |
---|
| 119 | try { FCK.ExecuteNamedCommand( 'Copy', null, true ) ; } |
---|
| 120 | catch (e) { alert(FCKLang.PasteErrorCopy) ; } |
---|
| 121 | break ; |
---|
| 122 | default : |
---|
| 123 | FCK.ExecuteNamedCommand( commandName, commandParameter ) ; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | FCK.AttachToOnSelectionChange = function( functionPointer ) |
---|
| 128 | { |
---|
| 129 | this.Events.AttachEvent( 'OnSelectionChange', functionPointer ) ; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | FCK.Paste = function() |
---|
| 133 | { |
---|
| 134 | if ( FCKConfig.ForcePasteAsPlainText ) |
---|
| 135 | { |
---|
| 136 | FCK.PasteAsPlainText() ; |
---|
| 137 | return false ; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /* For now, the AutoDetectPasteFromWord feature is IE only. */ |
---|
| 141 | |
---|
| 142 | return true ; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | //** |
---|
| 146 | // FCK.InsertHtml: Inserts HTML at the current cursor location. Deletes the |
---|
| 147 | // selected content if any. |
---|
| 148 | FCK.InsertHtml = function( html ) |
---|
| 149 | { |
---|
| 150 | html = FCKConfig.ProtectedSource.Protect( html ) ; |
---|
| 151 | html = FCK.ProtectEvents( html ) ; |
---|
| 152 | html = FCK.ProtectUrls( html ) ; |
---|
| 153 | html = FCK.ProtectTags( html ) ; |
---|
| 154 | |
---|
| 155 | // Firefox can't handle correctly the editing of the STRONG and EM tags. |
---|
| 156 | // We must replace them with B and I. |
---|
| 157 | html = html.replace( FCKRegexLib.StrongOpener, '<b$1' ) ; |
---|
| 158 | html = html.replace( FCKRegexLib.StrongCloser, '<\/b>' ) ; |
---|
| 159 | html = html.replace( FCKRegexLib.EmOpener, '<i$1' ) ; |
---|
| 160 | html = html.replace( FCKRegexLib.EmCloser, '<\/i>' ) ; |
---|
| 161 | |
---|
| 162 | // Delete the actual selection. |
---|
| 163 | var oSel = FCKSelection.Delete() ; |
---|
| 164 | |
---|
| 165 | // Get the first available range. |
---|
| 166 | var oRange = oSel.getRangeAt(0) ; |
---|
| 167 | |
---|
| 168 | // Create a fragment with the input HTML. |
---|
| 169 | var oFragment = oRange.createContextualFragment( html ) ; |
---|
| 170 | |
---|
| 171 | // Get the last available node. |
---|
| 172 | var oLastNode = oFragment.lastChild ; |
---|
| 173 | |
---|
| 174 | // Insert the fragment in the range. |
---|
| 175 | oRange.insertNode(oFragment) ; |
---|
| 176 | |
---|
| 177 | // Set the cursor after the inserted fragment. |
---|
| 178 | FCKSelection.SelectNode( oLastNode ) ; |
---|
| 179 | FCKSelection.Collapse( false ) ; |
---|
| 180 | |
---|
| 181 | this.Focus() ; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | FCK.InsertElement = function( element ) |
---|
| 185 | { |
---|
| 186 | // Deletes the actual selection. |
---|
| 187 | var oSel = FCKSelection.Delete() ; |
---|
| 188 | |
---|
| 189 | // Gets the first available range. |
---|
| 190 | var oRange = oSel.getRangeAt(0) ; |
---|
| 191 | |
---|
| 192 | // Inserts the element in the range. |
---|
| 193 | oRange.insertNode( element ) ; |
---|
| 194 | |
---|
| 195 | // Set the cursor after the inserted fragment. |
---|
| 196 | FCKSelection.SelectNode( element ) ; |
---|
| 197 | FCKSelection.Collapse( false ) ; |
---|
| 198 | |
---|
| 199 | this.Focus() ; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | FCK.PasteAsPlainText = function() |
---|
| 203 | { |
---|
| 204 | // TODO: Implement the "Paste as Plain Text" code. |
---|
| 205 | |
---|
| 206 | // If the function is called inmediately Firefox 2 does automatically paste the contents as soon as the new dialog is created |
---|
| 207 | // so we run it in a Timeout and the paste event can be cancelled |
---|
| 208 | FCKTools.RunFunction( FCKDialog.OpenDialog, FCKDialog, ['FCKDialog_Paste', FCKLang.PasteAsText, 'dialog/fck_paste.html', 400, 330, 'PlainText'] ) ; |
---|
| 209 | |
---|
| 210 | /* |
---|
| 211 | var sText = FCKTools.HTMLEncode( clipboardData.getData("Text") ) ; |
---|
| 212 | sText = sText.replace( /\n/g, '<BR>' ) ; |
---|
| 213 | this.InsertHtml( sText ) ; |
---|
| 214 | */ |
---|
| 215 | } |
---|
| 216 | /* |
---|
| 217 | FCK.PasteFromWord = function() |
---|
| 218 | { |
---|
| 219 | // TODO: Implement the "Paste as Plain Text" code. |
---|
| 220 | |
---|
| 221 | FCKDialog.OpenDialog( 'FCKDialog_Paste', FCKLang.PasteFromWord, 'dialog/fck_paste.html', 400, 330, 'Word' ) ; |
---|
| 222 | |
---|
| 223 | // FCK.CleanAndPaste( FCK.GetClipboardHTML() ) ; |
---|
| 224 | } |
---|
| 225 | */ |
---|
| 226 | FCK.GetClipboardHTML = function() |
---|
| 227 | { |
---|
| 228 | return '' ; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | FCK.CreateLink = function( url ) |
---|
| 232 | { |
---|
| 233 | FCK.ExecuteNamedCommand( 'Unlink' ) ; |
---|
| 234 | |
---|
| 235 | if ( url.length > 0 ) |
---|
| 236 | { |
---|
| 237 | // Generate a temporary name for the link. |
---|
| 238 | var sTempUrl = 'javascript:void(0);/*' + ( new Date().getTime() ) + '*/' ; |
---|
| 239 | |
---|
| 240 | // Use the internal "CreateLink" command to create the link. |
---|
| 241 | FCK.ExecuteNamedCommand( 'CreateLink', sTempUrl ) ; |
---|
| 242 | |
---|
| 243 | // Retrieve the just created link using XPath. |
---|
| 244 | var oLink = this.EditorDocument.evaluate("//a[@href='" + sTempUrl + "']", this.EditorDocument.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue ; |
---|
| 245 | |
---|
| 246 | if ( oLink ) |
---|
| 247 | { |
---|
| 248 | oLink.href = url ; |
---|
| 249 | return oLink ; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | return null ; |
---|
| 254 | } |
---|