[ 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: fckplugin.js 22 * Plugin to insert "Placeholders" in the editor. 23 * 24 * File Authors: 25 * Frederico Caldeira Knabben (www.fckeditor.net) 26 */ 27 28 // Register the related command. 29 FCKCommands.RegisterCommand( 'Placeholder', new FCKDialogCommand( 'Placeholder', FCKLang.PlaceholderDlgTitle, FCKPlugins.Items['placeholder'].Path + 'fck_placeholder.html', 340, 170 ) ) ; 30 31 // Create the "Plaholder" toolbar button. 32 var oPlaceholderItem = new FCKToolbarButton( 'Placeholder', FCKLang.PlaceholderBtn ) ; 33 oPlaceholderItem.IconPath = FCKPlugins.Items['placeholder'].Path + 'placeholder.gif' ; 34 35 FCKToolbarItems.RegisterItem( 'Placeholder', oPlaceholderItem ) ; 36 37 38 // The object used for all Placeholder operations. 39 var FCKPlaceholders = new Object() ; 40 41 // Add a new placeholder at the actual selection. 42 FCKPlaceholders.Add = function( name ) 43 { 44 var oSpan = FCK.CreateElement( 'SPAN' ) ; 45 this.SetupSpan( oSpan, name ) ; 46 } 47 48 FCKPlaceholders.SetupSpan = function( span, name ) 49 { 50 span.innerHTML = '[[ ' + name + ' ]]' ; 51 52 span.style.backgroundColor = '#ffff00' ; 53 span.style.color = '#000000' ; 54 55 if ( FCKBrowserInfo.IsGecko ) 56 span.style.cursor = 'default' ; 57 58 span._fckplaceholder = name ; 59 span.contentEditable = false ; 60 61 // To avoid it to be resized. 62 span.onresizestart = function() 63 { 64 FCK.EditorWindow.event.returnValue = false ; 65 return false ; 66 } 67 } 68 69 // On Gecko we must do this trick so the user select all the SPAN when clicking on it. 70 FCKPlaceholders._SetupClickListener = function() 71 { 72 FCKPlaceholders._ClickListener = function( e ) 73 { 74 if ( e.target.tagName == 'SPAN' && e.target._fckplaceholder ) 75 FCKSelection.SelectNode( e.target ) ; 76 } 77 78 FCK.EditorDocument.addEventListener( 'click', FCKPlaceholders._ClickListener, true ) ; 79 } 80 81 // Open the Placeholder dialog on double click. 82 FCKPlaceholders.OnDoubleClick = function( span ) 83 { 84 if ( span.tagName == 'SPAN' && span._fckplaceholder ) 85 FCKCommands.GetCommand( 'Placeholder' ).Execute() ; 86 } 87 88 FCK.RegisterDoubleClickHandler( FCKPlaceholders.OnDoubleClick, 'SPAN' ) ; 89 90 // Check if a Placholder name is already in use. 91 FCKPlaceholders.Exist = function( name ) 92 { 93 var aSpans = FCK.EditorDocument.getElementsByTagName( 'SPAN' ) ; 94 95 for ( var i = 0 ; i < aSpans.length ; i++ ) 96 { 97 if ( aSpans[i]._fckplaceholder == name ) 98 return true ; 99 } 100 101 return false ; 102 } 103 104 if ( FCKBrowserInfo.IsIE ) 105 { 106 FCKPlaceholders.Redraw = function() 107 { 108 if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG ) 109 return ; 110 111 var aPlaholders = FCK.EditorDocument.body.innerText.match( /\[\[[^\[\]]+\]\]/g ) ; 112 if ( !aPlaholders ) 113 return ; 114 115 var oRange = FCK.EditorDocument.body.createTextRange() ; 116 117 for ( var i = 0 ; i < aPlaholders.length ; i++ ) 118 { 119 if ( oRange.findText( aPlaholders[i] ) ) 120 { 121 var sName = aPlaholders[i].match( /\[\[\s*([^\]]*?)\s*\]\]/ )[1] ; 122 oRange.pasteHTML( '<span style="color: #000000; background-color: #ffff00" contenteditable="false" _fckplaceholder="' + sName + '">' + aPlaholders[i] + '</span>' ) ; 123 } 124 } 125 } 126 } 127 else 128 { 129 FCKPlaceholders.Redraw = function() 130 { 131 if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG ) 132 return ; 133 134 var oInteractor = FCK.EditorDocument.createTreeWalker( FCK.EditorDocument.body, NodeFilter.SHOW_TEXT, FCKPlaceholders._AcceptNode, true ) ; 135 136 var aNodes = new Array() ; 137 138 while ( ( oNode = oInteractor.nextNode() ) ) 139 { 140 aNodes[ aNodes.length ] = oNode ; 141 } 142 143 for ( var n = 0 ; n < aNodes.length ; n++ ) 144 { 145 var aPieces = aNodes[n].nodeValue.split( /(\[\[[^\[\]]+\]\])/g ) ; 146 147 for ( var i = 0 ; i < aPieces.length ; i++ ) 148 { 149 if ( aPieces[i].length > 0 ) 150 { 151 if ( aPieces[i].indexOf( '[[' ) == 0 ) 152 { 153 var sName = aPieces[i].match( /\[\[\s*([^\]]*?)\s*\]\]/ )[1] ; 154 155 var oSpan = FCK.EditorDocument.createElement( 'span' ) ; 156 FCKPlaceholders.SetupSpan( oSpan, sName ) ; 157 158 aNodes[n].parentNode.insertBefore( oSpan, aNodes[n] ) ; 159 } 160 else 161 aNodes[n].parentNode.insertBefore( FCK.EditorDocument.createTextNode( aPieces[i] ) , aNodes[n] ) ; 162 } 163 } 164 165 aNodes[n].parentNode.removeChild( aNodes[n] ) ; 166 } 167 168 FCKPlaceholders._SetupClickListener() ; 169 } 170 171 FCKPlaceholders._AcceptNode = function( node ) 172 { 173 if ( /\[\[[^\[\]]+\]\]/.test( node.nodeValue ) ) 174 return NodeFilter.FILTER_ACCEPT ; 175 else 176 return NodeFilter.FILTER_SKIP ; 177 } 178 } 179 180 FCK.Events.AttachEvent( 'OnAfterSetHTML', FCKPlaceholders.Redraw ) ; 181 182 // We must process the SPAN tags to replace then with the real resulting value of the placeholder. 183 FCKXHtml.TagProcessors['span'] = function( node, htmlNode ) 184 { 185 if ( htmlNode._fckplaceholder ) 186 node = FCKXHtml.XML.createTextNode( '[[' + htmlNode._fckplaceholder + ']]' ) ; 187 else 188 FCKXHtml._AppendChildNodes( node, htmlNode, false ) ; 189 190 return node ; 191 }
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 |