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