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