|
1 // $Id: fckplugin.js,v 1.2.2.3 2009/01/28 14:52:27 wwalc Exp $ |
|
2 /* |
|
3 * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
|
4 * Copyright (C) 2003-2007 Frederico Caldeira Knabben |
|
5 * |
|
6 * == BEGIN LICENSE == |
|
7 * |
|
8 * Licensed under the terms of any of the following licenses at your |
|
9 * choice: |
|
10 * |
|
11 * - GNU General Public License Version 2 or later (the "GPL") |
|
12 * http://www.gnu.org/licenses/gpl.html |
|
13 * |
|
14 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
|
15 * http://www.gnu.org/licenses/lgpl.html |
|
16 * |
|
17 * - Mozilla Public License Version 1.1 or later (the "MPL") |
|
18 * http://www.mozilla.org/MPL/MPL-1.1.html |
|
19 * |
|
20 * == END LICENSE == |
|
21 * |
|
22 * Plugin: add support for <!--pagebreak--> tag inside Drupal |
|
23 * Source: http://drupal.org/node/81893 |
|
24 */ |
|
25 |
|
26 // Define the command. |
|
27 var FCKDrupalPageBreak = function( name ) |
|
28 { |
|
29 this.Name = name ; |
|
30 this.EditMode = FCK.EditMode; |
|
31 } |
|
32 |
|
33 FCKDrupalPageBreak.prototype.Execute = function() |
|
34 { |
|
35 if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG ) |
|
36 return ; |
|
37 |
|
38 FCKUndo.SaveUndoStep() ; |
|
39 |
|
40 switch ( this.Name ) |
|
41 { |
|
42 case 'Break' : |
|
43 var e = FCK.EditorDocument.createComment( 'pagebreak' ) ; |
|
44 var oFakeImage = FCK.InsertElement( FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', e ) ) ; |
|
45 oFakeImage.setAttribute( "_drupalpagebreak", "true" ) ; |
|
46 this.MoveBreakOutsideElement(); |
|
47 break; |
|
48 default : |
|
49 break; |
|
50 } |
|
51 } |
|
52 |
|
53 FCKDrupalPageBreak.prototype.MoveBreakOutsideElement = function() |
|
54 { |
|
55 FCK.FixBody(); |
|
56 // get all elements in FCK document |
|
57 var elements = FCK.EditorDocument.getElementsByTagName( 'img' ) ; |
|
58 |
|
59 // check every element for childNodes |
|
60 var i = 0; |
|
61 var next ; |
|
62 while ( element = elements[i++] ) |
|
63 { |
|
64 if ( element.getAttribute( '_drupalpagebreak' ) == "true" ) |
|
65 { |
|
66 while( ( next = element.parentNode.nodeName.toLowerCase() ) != 'body' ) |
|
67 { |
|
68 //if we are inside p or div, close immediately this tag, insert break tag, |
|
69 //create new element and move remaining siblings to the next element |
|
70 if ( ( next == 'div' || next == 'p' ) && ( element.parentNode.parentNode.nodeName.toLowerCase() == 'body' ) ) |
|
71 { |
|
72 var oParent = element.parentNode ; |
|
73 var oDiv = FCK.EditorDocument.createElement( next.toUpperCase() ) ; |
|
74 var bDivEmpty = true ; |
|
75 var sibling ; |
|
76 |
|
77 while( sibling = element.nextSibling ) |
|
78 { |
|
79 if (!((sibling.nodeType == 3 && !sibling.nodeValue.length) || (sibling.nodeType == 1 && sibling.nodeName.toLowerCase() == 'br' && sibling.getAttribute( 'type' ) == '_moz'))) { |
|
80 bDivEmpty = false ; |
|
81 } |
|
82 |
|
83 oDiv.appendChild( sibling ) ; |
|
84 } |
|
85 |
|
86 if ( oDiv.childNodes.length ) |
|
87 { |
|
88 if ( oParent.nextSibling ) |
|
89 FCK.EditorDocument.body.insertBefore( oDiv, oParent.nextSibling ) ; |
|
90 else |
|
91 FCK.EditorDocument.body.appendChild( oDiv ) ; |
|
92 } |
|
93 |
|
94 if ( element.parentNode.nextSibling ) |
|
95 element.parentNode.parentNode.insertBefore( element, element.parentNode.nextSibling ) ; |
|
96 else |
|
97 element.parentNode.parentNode.appendChild( element ) ; |
|
98 |
|
99 if ( !oParent.childNodes.length ) |
|
100 FCK.EditorDocument.body.removeChild( oParent ) ; |
|
101 |
|
102 //if we put pagebreak next to another pagrebreak, remove it |
|
103 if ( element.nextSibling && element.nextSibling.getAttribute( '_drupalpagebreak' ) == "true") |
|
104 element.parentNode.removeChild( element.nextSibling ) ; |
|
105 |
|
106 //we must be sure the bogus node is available to make cursor blinking |
|
107 if ( FCKBrowserInfo.IsGeckoLike ) |
|
108 FCKTools.AppendBogusBr( oParent ) ; |
|
109 |
|
110 if ( bDivEmpty ) |
|
111 oDiv.parentNode.removeChild( oDiv ); |
|
112 |
|
113 break ; |
|
114 } |
|
115 else |
|
116 { |
|
117 if ( element.parentNode.nextSibling ) |
|
118 element.parentNode.parentNode.insertBefore( element, element.parentNode.nextSibling ) ; |
|
119 else |
|
120 element.parentNode.parentNode.appendChild( element ) ; |
|
121 } |
|
122 } |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 FCKDrupalPageBreak.prototype.GetState = function() |
|
128 { |
|
129 return ( FCK.EditMode == FCK_EDITMODE_WYSIWYG ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ) ; |
|
130 } |
|
131 |
|
132 // Register the Drupal tag commands. |
|
133 FCKCommands.RegisterCommand( 'DrupalPageBreak', new FCKDrupalPageBreak( 'Break' ) ) ; |
|
134 // Create the Drupal tag buttons. |
|
135 var oDrupalItem = new FCKToolbarButton( 'DrupalPageBreak', FCKLang.DrupalPageBreakTitle, FCKLang.DrupalPageBreakTooltip, FCK_TOOLBARITEM_ICONTEXT, true, true ) ; |
|
136 oDrupalItem.IconPath = FCKConfig.PluginsPath + 'drupalpagebreak/drupalpagebreak.gif'; |
|
137 FCKToolbarItems.RegisterItem( 'DrupalPageBreak', oDrupalItem ) ; |
|
138 |
|
139 // after switch in to source mode and back proccess page and insert fake |
|
140 // image for break again |
|
141 // Drupal Page Breaks Processor |
|
142 |
|
143 var FCKDrupalPageBreaksProcessor = FCKDocumentProcessor.AppendNew() ; |
|
144 FCKDrupalPageBreaksProcessor.ProcessDocument = function( document ) |
|
145 { |
|
146 // get all elements in FCK document |
|
147 var elements = document.getElementsByTagName( '*' ) ; |
|
148 |
|
149 // check every element for childNodes |
|
150 var i = 0; |
|
151 while (element = elements[i++]) { |
|
152 var nodes = element.childNodes; |
|
153 |
|
154 var j = 0; |
|
155 while (node = nodes[j++]) { |
|
156 if (node.nodeName == '#comment') { |
|
157 var re = /\{\d+\}/ ; |
|
158 var PContent; |
|
159 if (re.test(node.nodeValue)) |
|
160 PContent = FCKConfig.ProtectedSource.Revert('<!--' + node.nodeValue + '-->', false); |
|
161 |
|
162 if (node.nodeValue == 'pagebreak' || PContent == '<!--pagebreak-->') { |
|
163 var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', node.cloneNode(true) ) ; |
|
164 oFakeImage.setAttribute( "_drupalpagebreak", "true" ) ; |
|
165 node.parentNode.insertBefore( oFakeImage, node ) ; |
|
166 node.parentNode.removeChild( node ) ; |
|
167 } |
|
168 } |
|
169 } |
|
170 } |
|
171 FCKDrupalBreak.prototype.MoveBreakOutsideElement(); |
|
172 } |
|
173 |
|
174 if ( !FCK.Config.ProtectedSource._RevertOld ) |
|
175 FCK.Config.ProtectedSource._RevertOld = FCK.Config.ProtectedSource.Revert ; |
|
176 |
|
177 FCK.Config.ProtectedSource.Revert = function( html, clearBin ) |
|
178 { |
|
179 // Call the original code. |
|
180 var result = FCK.Config.ProtectedSource._RevertOld ( html, clearBin ) ; |
|
181 |
|
182 if ( typeof FCKDrupalPageBreak !="undefined" && typeof FCKDrupalBreak !="undefined" ) |
|
183 var re = /<(p|div)>((?:<!--pagebreak-->|<!--break-->)+)<\/\1>/gi ; |
|
184 else if ( typeof FCKDrupalBreak !="undefined" ) |
|
185 var re = /<(p|div)>(<!--break-->)+<\/\1>/gi ; |
|
186 else if ( typeof FCKDrupalPageBreak !="undefined" ) |
|
187 var re = /<(p|div)>(<!--pagebreak-->)+<\/\1>/gi ; |
|
188 |
|
189 result = result.replace( re, '$2' ); |
|
190 return result ; |
|
191 } |