]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin.js
1 /**
2  * WordPress plugin.
3  */
4
5 (function() {
6         var DOM = tinymce.DOM;
7
8         // Load plugin specific language pack
9         tinymce.PluginManager.requireLangPack('wordpress');
10
11         tinymce.create('tinymce.plugins.WordPress', {
12                 init : function(ed, url) {
13                         var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2');
14                         var moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
15                         var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
16
17                         if ( tinymce.util.Cookie.get('kitchenSink') == '1' )
18                                 ed.settings.wordpress_adv_hidden = 0;
19
20                         // Hides the specified toolbar and resizes the iframe
21                         ed.onPostRender.add(function() {
22                                 if ( ed.getParam('wordpress_adv_hidden', 1) ) {
23                                         DOM.hide(ed.controlManager.get(tbId).id);
24                                         t._resizeIframe(ed, tbId, 28);
25                                 }
26                         });
27                         
28                         // Register commands
29                         ed.addCommand('WP_More', function() {
30                                 ed.execCommand('mceInsertContent', 0, moreHTML);
31                         });
32                         
33                         ed.addCommand('WP_Page', function() {
34                                 ed.execCommand('mceInsertContent', 0, nextpageHTML);
35                         });
36                         
37                         ed.addCommand('WP_Help', function() {
38                                         ed.windowManager.open({
39                                                 url : tinymce.baseURL + '/wp-mce-help.php',
40                                                 width : 450,
41                                                 height : 420,
42                                                 inline : 1
43                                         });
44                                 });
45
46                         ed.addCommand('WP_Adv', function() {
47                                 var id = ed.controlManager.get(tbId).id, cm = ed.controlManager, cook = tinymce.util.Cookie, date;
48
49                                 date = new Date();
50                                 date.setTime(date.getTime()+(10*365*24*60*60*1000));
51
52                                 if (DOM.isHidden(id)) {
53                                         cm.setActive('wp_adv', 1);
54                                         DOM.show(id);
55                                         t._resizeIframe(ed, tbId, -28);
56                                         ed.settings.wordpress_adv_hidden = 0;
57                                         cook.set('kitchenSink', '1', date);
58                                 } else {
59                                         cm.setActive('wp_adv', 0);
60                                         DOM.hide(id);
61                                         t._resizeIframe(ed, tbId, 28);
62                                         ed.settings.wordpress_adv_hidden = 1;
63                                         cook.set('kitchenSink', '0', date);
64                                 }
65                         });
66
67                         // Register buttons
68                         ed.addButton('wp_more', {
69                                 title : 'wordpress.wp_more_desc',
70                                 image : url + '/img/more.gif',
71                                 cmd : 'WP_More'
72                         });
73
74                         ed.addButton('wp_page', {
75                                 title : 'wordpress.wp_page_desc',
76                                 image : url + '/img/page.gif',
77                                 cmd : 'WP_Page'
78                         });
79
80                         ed.addButton('wp_help', {
81                                 title : 'wordpress.wp_help_desc',
82                                 image : url + '/img/help.gif',
83                                 cmd : 'WP_Help' 
84                         });
85
86                         ed.addButton('wp_adv', {
87                                 title : 'wordpress.wp_adv_desc',
88                                 image : url + '/img/toolbars.gif',
89                                 cmd : 'WP_Adv'
90                         });
91
92                         // Add class "alignleft", "alignright" and "aligncenter" when selecting align for images.
93                         ed.onExecCommand.add(function( editor, cmd ) {
94                                 var node, bl, dom = editor.dom;
95
96                                 if ( 'JustifyCenter' == cmd ) {
97                                         if ( ( node = editor.selection.getNode() ) && node.nodeName == 'IMG' ) {
98                                                 if ( ! dom.hasClass( node, "aligncenter" ) && ( bl = editor.forceBlocks.getParentBlock(node) ) && bl.childNodes.length == 1 )
99                                                         dom.setStyle(bl, 'text-align', '');
100                                         }
101                                         editor.execCommand('mceRepaint');
102                                 }
103                         });
104
105                         ed.onBeforeExecCommand.add(function( editor, cmd ) {
106                                 var node, dir, xdir, bl, dom = editor.dom;
107
108                                 if ( ( cmd.indexOf('Justify') != -1 ) && ( node = editor.selection.getNode() ) ) {
109                                         if ( 'JustifyFull' == cmd || node.nodeName !== 'IMG' ) return;
110                                         dir = cmd.substring(7).toLowerCase();
111
112                                         if (  editor.queryCommandState( cmd ) ) {
113                                                 dom.removeClass( node, "alignleft" );
114                                                 dom.removeClass( node, "alignright" );
115                                                 dom.removeClass( node, "aligncenter" );
116                                         } else if ( 'JustifyCenter' == cmd ) {
117                                                 dom.removeClass( node, "alignleft" );
118                                                 dom.removeClass( node, "alignright" );
119
120                                                 if ( dom.hasClass( node, "aligncenter" ) ) {
121                                                         dom.removeClass( node, "aligncenter" );
122                                                         if ( ( bl = editor.forceBlocks.getParentBlock(node) ) && bl.childNodes.length == 1 && tinymce.isGecko )
123                                                                 editor.selection.select(bl.firstChild);
124                                                 } else dom.addClass( node, "aligncenter" );
125
126                                         } else {
127                                                 xdir = ( dir == 'left' ) ? 'right' : 'left';
128                                                 dom.removeClass( node, "aligncenter" );
129                                                 dom.removeClass( node, "align"+xdir );
130                                                 dom.addClass( node, "align"+dir );
131                                         }
132                                 }
133                         });
134
135                         // Add listeners to handle more break
136                         t._handleMoreBreak(ed, url);
137
138                         // Add custom shortcuts
139                         ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
140                         ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
141                         ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
142                         ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
143                         ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
144                         ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
145                         ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
146                         ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
147                         ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
148                         ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
149                         ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
150                         ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'mceImage');
151                         ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
152                         ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
153                         ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
154                         ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
155                         ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
156
157                         if ( tinymce.isWebKit ) {
158                                 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
159                                 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
160                         }
161                 },
162
163                 getInfo : function() {
164                         return {
165                                 longname : 'WordPress Plugin',
166                                 author : 'WordPress', // add Moxiecode?
167                                 authorurl : 'http://wordpress.org',
168                                 infourl : 'http://wordpress.org',
169                                 version : '3.0'
170                         };
171                 },
172
173                 // Internal functions
174
175                 // Resizes the iframe by a relative height value
176                 _resizeIframe : function(ed, tb_id, dy) {
177                         var ifr = ed.getContentAreaContainer().firstChild;
178
179                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
180                         ed.theme.deltaHeight += dy; // For resize cookie
181                 },
182
183                 _handleMoreBreak : function(ed, url) {
184                         var moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
185                         var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
186
187                         // Load plugin specific CSS into editor
188                         ed.onInit.add(function() {
189                                 ed.dom.loadCSS(url + '/css/content.css');
190                         });
191
192                         // Display morebreak instead if img in element path
193                         ed.onPostRender.add(function() {
194                                 if (ed.theme.onResolveName) {
195                                         ed.theme.onResolveName.add(function(th, o) {
196                                                 if (o.node.nodeName == 'IMG') {
197                                                         if ( ed.dom.hasClass(o.node, 'mceWPmore') )
198                                                                 o.name = 'wpmore';
199                                                         if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
200                                                                 o.name = 'wppage';
201                                                 }
202
203                                         });
204                                 }
205                         });
206
207                         // Replace morebreak with images
208                         ed.onBeforeSetContent.add(function(ed, o) {
209                                 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
210                                 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
211                         });
212
213                         // Replace images with morebreak
214                         ed.onPostProcess.add(function(ed, o) {
215                                 if (o.get)
216                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
217                                                 if (im.indexOf('class="mceWPmore') !== -1) {
218                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
219                                                         im = '<!--more'+moretext+'-->';
220                                                 }
221                                                 if (im.indexOf('class="mceWPnextpage') !== -1)
222                                                         im = '<!--nextpage-->';
223
224                                                 return im;
225                                         });
226                         });
227
228                         // Set active buttons if user selected pagebreak or more break
229                         ed.onNodeChange.add(function(ed, cm, n) {
230                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
231                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
232                         });
233                 }
234         });
235
236         // Register plugin
237         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
238 })();