[ Index ] |
|
Code source de FCKeditor 2.4 |
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 * File Name: fckeditingarea.js 22 * FCKEditingArea Class: renders an editable area. 23 * 24 * File Authors: 25 * Frederico Caldeira Knabben (www.fckeditor.net) 26 */ 27 28 /** 29 * @constructor 30 * @param {String} targetElement The element that will hold the editing area. Any child element present in the target will be deleted. 31 */ 32 var FCKEditingArea = function( targetElement ) 33 { 34 this.TargetElement = targetElement ; 35 this.Mode = FCK_EDITMODE_WYSIWYG ; 36 37 if ( FCK.IECleanup ) 38 FCK.IECleanup.AddItem( this, FCKEditingArea_Cleanup ) ; 39 } 40 41 42 /** 43 * @param {String} html The complete HTML for the page, including DOCTYPE and the <html> tag. 44 */ 45 FCKEditingArea.prototype.Start = function( html, secondCall ) 46 { 47 var eTargetElement = this.TargetElement ; 48 var oTargetDocument = FCKTools.GetElementDocument( eTargetElement ) ; 49 50 // Remove all child nodes from the target. 51 while( eTargetElement.childNodes.length > 0 ) 52 eTargetElement.removeChild( eTargetElement.childNodes[0] ) ; 53 54 if ( this.Mode == FCK_EDITMODE_WYSIWYG ) 55 { 56 // Create the editing area IFRAME. 57 var oIFrame = this.IFrame = oTargetDocument.createElement( 'iframe' ) ; 58 oIFrame.src = 'javascript:void(0)' ; 59 oIFrame.frameBorder = 0 ; 60 oIFrame.width = oIFrame.height = '100%' ; 61 62 // Append the new IFRAME to the target. 63 eTargetElement.appendChild( oIFrame ) ; 64 65 // IE has a bug with the <base> tag... it must have a </base> closer, 66 // otherwise the all sucessive tags will be set as children nodes of the <base>. 67 if ( FCKBrowserInfo.IsIE ) 68 html = html.replace( /(<base[^>]*?)\s*\/?>(?!\s*<\/base>)/gi, '$1></base>' ) ; 69 else if ( !secondCall ) 70 { 71 // If nothing in the body, place a BOGUS tag so the cursor will appear. 72 if ( FCKBrowserInfo.IsGecko ) 73 html = html.replace( /(<body[^>]*>)\s*(<\/body>)/i, '$1' + GECKO_BOGUS + '$2' ) ; 74 75 // Gecko moves some tags out of the body to the head, so we must use 76 // innerHTML to set the body contents (SF BUG 1526154). 77 78 // Extract the BODY contents from the html. 79 var oMatch = html.match( FCKRegexLib.BodyContents ) ; 80 81 if ( oMatch ) 82 { 83 html = 84 oMatch[1] + // This is the HTML until the <body...> tag, inclusive. 85 ' ' + 86 oMatch[3] ; // This is the HTML from the </body> tag, inclusive. 87 88 this._BodyHTML = oMatch[2] ; // This is the BODY tag contents. 89 } 90 else 91 this._BodyHTML = html ; // Invalid HTML input. 92 } 93 94 // Get the window and document objects used to interact with the newly created IFRAME. 95 this.Window = oIFrame.contentWindow ; 96 97 // IE: Avoid JavaScript errors thrown by the editing are source (like tags events). 98 // TODO: This error handler is not being fired. 99 // this.Window.onerror = function() { alert( 'Error!' ) ; return true ; } 100 101 var oDoc = this.Document = this.Window.document ; 102 103 oDoc.open() ; 104 oDoc.write( html ) ; 105 oDoc.close() ; 106 107 // Firefox 1.0.x is buggy... ohh yes... so let's do it two times and it 108 // will magicaly work. 109 if ( FCKBrowserInfo.IsGecko10 && !secondCall ) 110 { 111 this.Start( html, true ) ; 112 return ; 113 } 114 115 this.Window._FCKEditingArea = this ; 116 117 // FF 1.0.x is buggy... we must wait a lot to enable editing because 118 // sometimes the content simply disappears, for example when pasting 119 // "bla1!<img src='some_url'>!bla2" in the source and then switching 120 // back to design. 121 if ( FCKBrowserInfo.IsGecko10 ) 122 this.Window.setTimeout( FCKEditingArea_CompleteStart, 500 ) ; 123 else 124 FCKEditingArea_CompleteStart.call( this.Window ) ; 125 } 126 else 127 { 128 var eTextarea = this.Textarea = oTargetDocument.createElement( 'textarea' ) ; 129 eTextarea.className = 'SourceField' ; 130 eTextarea.dir = 'ltr' ; 131 eTextarea.style.width = eTextarea.style.height = '100%' ; 132 eTextarea.style.border = 'none' ; 133 eTargetElement.appendChild( eTextarea ) ; 134 135 eTextarea.value = html ; 136 137 // Fire the "OnLoad" event. 138 FCKTools.RunFunction( this.OnLoad ) ; 139 } 140 } 141 142 // "this" here is FCKEditingArea.Window 143 function FCKEditingArea_CompleteStart() 144 { 145 // Of Firefox, the DOM takes a little to become available. So we must wait for it in a loop. 146 if ( !this.document.body ) 147 { 148 this.setTimeout( FCKEditingArea_CompleteStart, 50 ) ; 149 return ; 150 } 151 152 var oEditorArea = this._FCKEditingArea ; 153 oEditorArea.MakeEditable() ; 154 155 // Fire the "OnLoad" event. 156 FCKTools.RunFunction( oEditorArea.OnLoad ) ; 157 } 158 159 FCKEditingArea.prototype.MakeEditable = function() 160 { 161 var oDoc = this.Document ; 162 163 if ( FCKBrowserInfo.IsIE ) 164 { 165 oDoc.body.contentEditable = true ; 166 167 /* The following commands don't throw errors, but have no effect. 168 oDoc.execCommand( 'AutoDetect', false, false ) ; 169 oDoc.execCommand( 'KeepSelection', false, true ) ; 170 */ 171 } 172 else 173 { 174 try 175 { 176 // Disable Firefox 2 Spell Checker. 177 oDoc.body.spellcheck = ( this.FFSpellChecker !== false ) ; 178 179 if ( this._BodyHTML ) 180 { 181 oDoc.body.innerHTML = this._BodyHTML ; 182 this._BodyHTML = null ; 183 } 184 185 oDoc.designMode = 'on' ; 186 187 // Tell Gecko to use or not the <SPAN> tag for the bold, italic and underline. 188 try 189 { 190 oDoc.execCommand( 'styleWithCSS', false, FCKConfig.GeckoUseSPAN ) ; 191 } 192 catch (e) 193 { 194 // As evidenced here, useCSS is deprecated in favor of styleWithCSS: 195 // http://www.mozilla.org/editor/midas-spec.html 196 oDoc.execCommand( 'useCSS', false, !FCKConfig.GeckoUseSPAN ) ; 197 } 198 199 // Analysing Firefox 1.5 source code, it seams that there is support for a 200 // "insertBrOnReturn" command. Applying it gives no error, but it doesn't 201 // gives the same behavior that you have with IE. It works only if you are 202 // already inside a paragraph and it doesn't render correctly in the first enter. 203 // oDoc.execCommand( 'insertBrOnReturn', false, false ) ; 204 205 // Tell Gecko (Firefox 1.5+) to enable or not live resizing of objects (by Alfonso Martinez) 206 oDoc.execCommand( 'enableObjectResizing', false, !FCKConfig.DisableObjectResizing ) ; 207 208 // Disable the standard table editing features of Firefox. 209 oDoc.execCommand( 'enableInlineTableEditing', false, !FCKConfig.DisableFFTableHandles ) ; 210 } 211 catch (e) {} 212 } 213 } 214 215 FCKEditingArea.prototype.Focus = function() 216 { 217 try 218 { 219 if ( this.Mode == FCK_EDITMODE_WYSIWYG ) 220 { 221 if ( FCKBrowserInfo.IsIE && this.Document.hasFocus() ) 222 return ; 223 224 if ( FCKBrowserInfo.IsSafari ) 225 this.IFrame.focus() ; 226 else 227 { 228 this.Window.focus() ; 229 } 230 } 231 else 232 { 233 var oDoc = FCKTools.GetElementDocument( this.Textarea ) ; 234 if ( (!oDoc.hasFocus || oDoc.hasFocus() ) && oDoc.activeElement == this.Textarea ) 235 return ; 236 237 this.Textarea.focus() ; 238 } 239 } 240 catch(e) {} 241 } 242 243 function FCKEditingArea_Cleanup() 244 { 245 this.TargetElement = null ; 246 this.IFrame = null ; 247 this.Document = null ; 248 this.Textarea = null ; 249 250 if ( this.Window ) 251 { 252 this.Window._FCKEditingArea = null ; 253 this.Window = null ; 254 } 255 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:28:05 2007 | par Balluche grâce à PHPXref 0.7 |