2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Plugin to insert "Placeholders" in the editor.
24
// Register the related command.
25
FCKCommands.RegisterCommand( 'Placeholder', new FCKDialogCommand( 'Placeholder', FCKLang.PlaceholderDlgTitle, FCKPlugins.Items['placeholder'].Path + 'fck_placeholder.html', 340, 160 ) ) ;
27
// Create the "Plaholder" toolbar button.
28
var oPlaceholderItem = new FCKToolbarButton( 'Placeholder', FCKLang.PlaceholderBtn ) ;
29
oPlaceholderItem.IconPath = FCKPlugins.Items['placeholder'].Path + 'placeholder.gif' ;
31
FCKToolbarItems.RegisterItem( 'Placeholder', oPlaceholderItem ) ;
34
// The object used for all Placeholder operations.
35
var FCKPlaceholders = new Object() ;
37
// Add a new placeholder at the actual selection.
38
FCKPlaceholders.Add = function( name )
40
var oSpan = FCK.InsertElement( 'span' ) ;
41
this.SetupSpan( oSpan, name ) ;
44
FCKPlaceholders.SetupSpan = function( span, name )
46
span.innerHTML = '[[ ' + name + ' ]]' ;
48
span.style.backgroundColor = '#ffff00' ;
49
span.style.color = '#000000' ;
51
if ( FCKBrowserInfo.IsGecko )
52
span.style.cursor = 'default' ;
54
span._fckplaceholder = name ;
55
span.contentEditable = false ;
57
// To avoid it to be resized.
58
span.onresizestart = function()
60
FCK.EditorWindow.event.returnValue = false ;
65
// On Gecko we must do this trick so the user select all the SPAN when clicking on it.
66
FCKPlaceholders._SetupClickListener = function()
68
FCKPlaceholders._ClickListener = function( e )
70
if ( e.target.tagName == 'SPAN' && e.target._fckplaceholder )
71
FCKSelection.SelectNode( e.target ) ;
74
FCK.EditorDocument.addEventListener( 'click', FCKPlaceholders._ClickListener, true ) ;
77
// Open the Placeholder dialog on double click.
78
FCKPlaceholders.OnDoubleClick = function( span )
80
if ( span.tagName == 'SPAN' && span._fckplaceholder )
81
FCKCommands.GetCommand( 'Placeholder' ).Execute() ;
84
FCK.RegisterDoubleClickHandler( FCKPlaceholders.OnDoubleClick, 'SPAN' ) ;
86
// Check if a Placholder name is already in use.
87
FCKPlaceholders.Exist = function( name )
89
var aSpans = FCK.EditorDocument.getElementsByTagName( 'SPAN' ) ;
91
for ( var i = 0 ; i < aSpans.length ; i++ )
93
if ( aSpans[i]._fckplaceholder == name )
100
if ( FCKBrowserInfo.IsIE )
102
FCKPlaceholders.Redraw = function()
104
if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG )
107
var aPlaholders = FCK.EditorDocument.body.innerText.match( /\[\[[^\[\]]+\]\]/g ) ;
111
var oRange = FCK.EditorDocument.body.createTextRange() ;
113
for ( var i = 0 ; i < aPlaholders.length ; i++ )
115
if ( oRange.findText( aPlaholders[i] ) )
117
var sName = aPlaholders[i].match( /\[\[\s*([^\]]*?)\s*\]\]/ )[1] ;
118
oRange.pasteHTML( '<span style="color: #000000; background-color: #ffff00" contenteditable="false" _fckplaceholder="' + sName + '">' + aPlaholders[i] + '</span>' ) ;
125
FCKPlaceholders.Redraw = function()
127
if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG )
130
var oInteractor = FCK.EditorDocument.createTreeWalker( FCK.EditorDocument.body, NodeFilter.SHOW_TEXT, FCKPlaceholders._AcceptNode, true ) ;
132
var aNodes = new Array() ;
134
while ( ( oNode = oInteractor.nextNode() ) )
136
aNodes[ aNodes.length ] = oNode ;
139
for ( var n = 0 ; n < aNodes.length ; n++ )
141
var aPieces = aNodes[n].nodeValue.split( /(\[\[[^\[\]]+\]\])/g ) ;
143
for ( var i = 0 ; i < aPieces.length ; i++ )
145
if ( aPieces[i].length > 0 )
147
if ( aPieces[i].indexOf( '[[' ) == 0 )
149
var sName = aPieces[i].match( /\[\[\s*([^\]]*?)\s*\]\]/ )[1] ;
151
var oSpan = FCK.EditorDocument.createElement( 'span' ) ;
152
FCKPlaceholders.SetupSpan( oSpan, sName ) ;
154
aNodes[n].parentNode.insertBefore( oSpan, aNodes[n] ) ;
157
aNodes[n].parentNode.insertBefore( FCK.EditorDocument.createTextNode( aPieces[i] ) , aNodes[n] ) ;
161
aNodes[n].parentNode.removeChild( aNodes[n] ) ;
164
FCKPlaceholders._SetupClickListener() ;
167
FCKPlaceholders._AcceptNode = function( node )
169
if ( /\[\[[^\[\]]+\]\]/.test( node.nodeValue ) )
170
return NodeFilter.FILTER_ACCEPT ;
172
return NodeFilter.FILTER_SKIP ;
176
FCK.Events.AttachEvent( 'OnAfterSetHTML', FCKPlaceholders.Redraw ) ;
178
// We must process the SPAN tags to replace then with the real resulting value of the placeholder.
179
FCKXHtml.TagProcessors['span'] = function( node, htmlNode )
181
if ( htmlNode._fckplaceholder )
182
node = FCKXHtml.XML.createTextNode( '[[' + htmlNode._fckplaceholder + ']]' ) ;
184
FCKXHtml._AppendChildNodes( node, htmlNode, false ) ;