#352295 by sun: Added markItUp editor support.
[project/wysiwyg.git] / editors / tinymce.inc
CommitLineData
14759316 1<?php
2// $Id$
3
4
5/**
6 * Plugin implementation of hook_editor().
7 *
8 * - Function signature and returned editor name nust match include filename.
9 * - Returns an array of supported editor versions along with support files.
10 *
11 * @todo wysiwyg_<editor>_alter() to add/inject optional libraries like gzip.
12 */
13function wysiwyg_tinymce_editor() {
14 $editor = array();
15 $editor['tinymce'] = array(
16 // Required properties
17 'title' => 'TinyMCE',
18 'vendor url' => 'http://tinymce.moxiecode.com',
19 'download url' => 'http://tinymce.moxiecode.com/download.php',
14759316 20 'library path' => wysiwyg_get_path('tinymce') . '/jscripts/tiny_mce',
21 'libraries' => array( // We cannot assume that all editors need just one js library.
22 '' => array( // Key may be used in wysiwyg_tinymce_settings() for exec mode.
23 'title' => 'Minified',
24 'files' => array('tiny_mce.js'),
25 ),
26 'src' => array(
27 'title' => 'Source',
28 'files' => array('tiny_mce_src.js'),
29 ),
30 ),
14759316 31 'version callback' => 'wysiwyg_tinymce_version',
32 'themes callback' => 'wysiwyg_tinymce_themes',
33 'settings callback' => 'wysiwyg_tinymce_settings',
34 'plugin callback' => 'wysiwyg_tinymce_plugins',
35 'plugin settings callback' => 'wysiwyg_tinymce_plugin_settings',
36 'versions' => array( // Each version can override global editor properties.
37 '2.1' => array(
38 // 'include files' => array('tinymce-2.inc'),
39 'js files' => array('tinymce-2.js'),
a09d7c1c 40 'css files' => array('tinymce-2.css'),
fb9108a1 41 'download url' => 'http://sourceforge.net/project/showfiles.php?group_id=103281&package_id=111430&release_id=557383',
14759316 42 ),
43 '3.2' => array(
44 // 'include files' => array('tinymce-3.inc'),
45 'js files' => array('tinymce-3.js'),
a09d7c1c 46 'css files' => array('tinymce-3.css'),
14759316 47 // 'plugin callback' => 'wysiwyg_tinymce_3_plugins',
48 'libraries' => array(
49 '' => array(
50 'title' => 'Minified',
51 'files' => array('tiny_mce.js'),
52 ),
53 'jquery' => array(
54 'title' => 'jQuery',
55 'files' => array('tiny_mce_jquery.js'),
56 ),
57 'src' => array(
58 'title' => 'Source',
59 'files' => array('tiny_mce_src.js'),
60 ),
61 ),
62 ),
63 ),
64 // Optional properties
166a12f1 65 // 'editor path' => wysiwyg_get_path('tinymce'), // Assumed by default.
66 // 'js path' => wysiwyg_get_path('editors/js'), // Assumed by default.
67 // 'css path' => wysiwyg_get_path('editors/css'), // Assumed by default.
14759316 68 );
69 return $editor;
70}
71
72/**
73 * Detect editor version.
74 *
75 * @param $editor
76 * An array containing editor properties as returned from hook_editor().
77 *
78 * @return
79 * The installed editor version.
80 */
81function wysiwyg_tinymce_version($editor) {
c0a227fc 82 $script = $editor['library path'] .'/tiny_mce.js';
83 $script = fopen($script, 'r');
84 // Version is contained in the first 80 chars.
85 $line = fgets($script, 80);
86 // 2.x: this.majorVersion="2";this.minorVersion="1.3"
87 // 3.x: majorVersion:'3',minorVersion:'2.0.1'
88 if (preg_match('@majorVersion[=:]["\'](\d).+?minorVersion[=:]["\']([\d\.]+)@', $line, $version)) {
89 fclose($script);
90 return $version[1] .'.'. $version[2];
14759316 91 }
92}
93
94/**
95 * Return runtime editor settings for a given wysiwyg profile.
96 *
97 * @param $editor
98 * A processed hook_editor() array of editor properties.
99 * @param $config
100 * An array containing wysiwyg editor profile settings.
101 * @param $theme
102 * The name of a theme/GUI/skin to use.
103 *
104 * @return
105 * A settings array to be populated in
f178028c 106 * Drupal.settings.wysiwyg.configs.{editor}
14759316 107 */
108function wysiwyg_tinymce_settings($editor, $config, $theme) {
109 $init = array(
14759316 110 'button_tile_map' => TRUE, // @todo Add a setting for this.
14759316 111 'document_base_url' => base_path(),
112 'entity_encoding' => 'raw',
14759316 113 'mode' => 'none',
14759316 114 'plugins' => array(),
14759316 115 'relative_urls' => FALSE,
14759316 116 'theme' => $theme,
7b70802f 117 'width' => '100%',
14759316 118 );
9e8e526a 119 if (isset($config['apply_source_formatting'])) {
120 $init['apply_source_formatting'] = $config['apply_source_formatting'];
121 }
122 if (isset($config['convert_fonts_to_spans'])) {
123 $init['convert_fonts_to_spans'] = $config['convert_fonts_to_spans'];
124 }
125 if (isset($config['language'])) {
126 $init['language'] = $config['language'];
127 }
128 if (isset($config['paste_auto_cleanup_on_paste'])) {
129 $init['paste_auto_cleanup_on_paste'] = $config['paste_auto_cleanup_on_paste'];
130 }
131 if (isset($config['preformatted'])) {
132 $init['preformatted'] = $config['preformatted'];
133 }
134 if (isset($config['remove_linebreaks'])) {
135 $init['remove_linebreaks'] = $config['remove_linebreaks'];
136 }
137 if (isset($config['verify_html'])) {
138 $init['verify_html'] = $config['verify_html'];
139 }
14759316 140
9e8e526a 141 if (!empty($config['css_classes'])) {
14759316 142 $init['theme_advanced_styles'] = implode(';', array_filter(explode("\n", str_replace("\r", '', $config['css_classes']))));
143 }
144
9e8e526a 145 if (isset($config['css_setting'])) {
146 if ($config['css_setting'] == 'theme') {
147 $css = path_to_theme() .'/style.css';
148 if (file_exists($css)) {
149 $init['content_css'] = base_path() . $css;
150 }
151 }
152 else if ($config['css_setting'] == 'self' && isset($config['css_path'])) {
153 $init['content_css'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
14759316 154 }
14759316 155 }
156
157 // Find the enabled buttons and the button row they belong on.
158 // Also map the plugin metadata for each button.
159 // @todo What follows is a pain; needs a rewrite.
160 if (!empty($config['buttons']) && is_array($config['buttons'])) {
161 // $init['buttons'] are stacked into $init['theme_advanced_buttons1'] later.
162 // @todo Add a toolbar designer based on jQuery UI.
163 $init['buttons'] = array();
164 // Only array keys in $init['extensions'] matter; added to $init['plugins']
165 // later.
166 $init['extensions'] = array();
167 // $init['extended_valid_elements'] are just stacked, unique'd later, and
eb714832 168 // transformed into a comma-separated string in wysiwyg_add_editor_settings().
14759316 169 // @todo Needs a complete plugin API redesign using arrays for
170 // tag => attributes definitions and array_merge_recursive().
171 $init['extended_valid_elements'] = array();
172
eb714832 173 $plugins = wysiwyg_get_plugins($editor['name']);
14759316 174 foreach ($config['buttons'] as $plugin => $buttons) {
175 foreach ($buttons as $button => $enabled) {
176 // Iterate separately over buttons and extensions properties.
177 foreach (array('buttons', 'extensions') as $type) {
178 // Skip unavailable plugins.
179 if (!isset($plugins[$plugin][$type][$button])) {
180 continue;
181 }
182 // Add buttons.
183 if ($type == 'buttons') {
184 $init['buttons'][] = $button;
185 }
186 // Add external plugins to the list of extensions.
c2eb2080 187 if ($type == 'buttons' && empty($plugins[$plugin]['internal'])) {
14759316 188 $init['extensions'][_wysiwyg_tinymce_plugin_name('add', $plugin)] = 1;
189 }
190 // Add internal buttons that also need to be loaded as extension.
c2eb2080 191 else if ($type == 'buttons' && !empty($plugins[$plugin]['load'])) {
14759316 192 $init['extensions'][$plugin] = 1;
193 }
194 // Add plain extensions.
c2eb2080 195 else if ($type == 'extensions' && !empty($plugins[$plugin]['load'])) {
14759316 196 $init['extensions'][$plugin] = 1;
197 }
198 // Allow plugins to add valid HTML elements.
199 if (!empty($plugins[$plugin]['extended_valid_elements'])) {
200 $init['extended_valid_elements'] = array_merge($init['extended_valid_elements'], $plugins[$plugin]['extended_valid_elements']);
201 }
202 // Allow plugins to add or override global configuration settings.
203 if (!empty($plugins[$plugin]['options'])) {
204 $init = array_merge($init, $plugins[$plugin]['options']);
205 }
206 }
207 }
208 }
209 // Clean-up.
210 $init['extended_valid_elements'] = array_unique($init['extended_valid_elements']);
211 if ($init['extensions']) {
212 $init['plugins'] = array_keys($init['extensions']);
213 unset($init['extensions']);
214 } else {
215 unset($init['extensions']);
216 }
217 }
218
219 // Add theme-specific settings.
220 switch ($theme) {
221 case 'advanced':
222 $init += array(
14759316 223 'theme_advanced_resize_horizontal' => FALSE,
224 'theme_advanced_resizing_use_cookie' => FALSE,
713eaa03 225 'theme_advanced_path_location' => isset($config['path_loc']) ? $config['path_loc'] : 'bottom',
226 'theme_advanced_resizing' => isset($config['resizing']) ? $config['resizing'] : 1,
227 'theme_advanced_toolbar_location' => isset($config['toolbar_loc']) ? $config['toolbar_loc'] : 'top',
228 'theme_advanced_toolbar_align' => isset($config['toolbar_align']) ? $config['toolbar_align'] : 'left',
14759316 229 );
9e8e526a 230 if (isset($config['block_formats'])) {
231 $init['theme_advanced_blockformats'] = $config['block_formats'];
232 }
9e8e526a 233 if (isset($init['buttons'])) {
713eaa03 234 // These rows explicitly need to be set to be empty, otherwise TinyMCE
235 // loads its default buttons of the advanced theme for each row.
9e8e526a 236 $init += array(
237 'theme_advanced_buttons1' => array(),
238 'theme_advanced_buttons2' => array(),
239 'theme_advanced_buttons3' => array(),
240 );
713eaa03 241 // @todo Allow to sort/arrange editor buttons.
9e8e526a 242 for ($i = 0; $i < count($init['buttons']); $i++) {
243 $init['theme_advanced_buttons1'][] = $init['buttons'][$i];
244 }
14759316 245 }
246 break;
247 }
248 unset($init['buttons']);
249
4c552b90 250 // Convert the config values into the form expected by TinyMCE.
251 foreach ($init as $key => $value) {
252 if (is_bool($value)) {
253 continue;
254 }
255 if (is_array($value)) {
256 $init[$key] = implode(',', $init[$key]);
257 }
258 }
259
14759316 260 return $init;
261}
262
263/**
264 * Determine available editor themes or check/reset a given one.
265 *
266 * @param $editor
267 * A processed hook_editor() array of editor properties.
268 * @param $profile
269 * A wysiwyg editor profile.
270 *
271 * @return
272 * An array of theme names. The first returned name should be the default
273 * theme name.
274 */
275function wysiwyg_tinymce_themes($editor, $profile) {
276 /*
277 $themes = array();
278 $dir = $editor['library path'] . '/themes/';
279 if (is_dir($dir) && $dh = opendir($dir)) {
280 while (($file = readdir($dh)) !== FALSE) {
281 if (!in_array($file, array('.', '..', 'CVS', '.svn')) && is_dir($dir . $file)) {
282 $themes[$file] = $file;
283 }
284 }
285 closedir($dh);
286 asort($themes);
287 }
288 return $themes;
289 */
290 return array('advanced', 'simple');
291}
292
293/**
294 * Build a JS settings array of external plugins that need to be loaded separately.
295 *
296 * TinyMCE requires that external plugins (i.e. not residing in the editor's
297 * directory) are loaded (once) after the editor has been initialized.
298 */
325352d7 299function wysiwyg_tinymce_plugin_settings($editor, $profile, $plugins) {
300 $settings = array();
301 foreach ($plugins as $name => $plugin) {
f012ff6d 302 if (empty($plugin['internal'])) {
325352d7 303 $settings[$name] = base_path() . $plugin['path'];
14759316 304 }
305 }
325352d7 306 return $settings;
14759316 307}
308
309/**
310 * Add or remove leading hiven to/of external plugin names.
311 *
312 * TinyMCE requires that external plugins, which should not be loaded from
313 * its own plugin repository are prefixed with a hiven in the name.
314 *
315 * @param string $op
316 * Operation to perform, 'add' or 'remove' (hiven).
317 * @param string $name
318 * A plugin name.
319 */
320function _wysiwyg_tinymce_plugin_name($op, $name) {
321 if ($op == 'add') {
322 if (strpos($name, '-') !== 0) {
323 return '-'. $name;
324 }
325 return $name;
326 }
327 else if ($op == 'remove') {
328 if (strpos($name, '-') === 0) {
329 return substr($name, 1);
330 }
331 return $name;
332 }
333}
334
335/**
336 * Return internal plugins for TinyMCE; semi-implementation of hook_wysiwyg_plugin().
337 */
338function wysiwyg_tinymce_plugins($editor) {
329c0500 339 $plugins = array(
14759316 340 'default' => array(
341 'path' => $editor['library path'] .'/themes/advanced',
342 'buttons' => array(
343 'bold' => t('Bold'), 'italic' => t('Italic'), 'underline' => t('Underline'),
344 'strikethrough' => t('Strike-through'),
345 'justifyleft' => t('Align left'), 'justifycenter' => t('Align center'), 'justifyright' => t('Align right'), 'justifyfull' => t('Justify'),
346 'bullist' => t('Bullet list'), 'numlist' => t('Numbered list'),
347 'outdent' => t('Outdent'), 'indent' => t('Indent'),
348 'undo' => t('Undo'), 'redo' => t('Redo'),
349 'link' => t('Link'), 'unlink' => t('Unlink'), 'anchor' => t('Anchor'),
350 'image' => t('Image'),
351 'cleanup' => t('Clean-up'),
352 'forecolor' => t('Forecolor'), 'backcolor' => t('Backcolor'),
353 'sup' => t('Sup'), 'sub' => t('Sub'),
da006eca 354 'blockquote' => t('Blockquote'), 'code' => t('Source code'),
14759316 355 'hr' => t('Horizontal rule'),
356 'cut' => t('Cut'), 'copy' => t('Copy'), 'paste' => t('Paste'),
357 'visualaid' => t('Visual aid'),
358 'removeformat' => t('Remove format'),
359 'charmap' => t('Character map'),
360 'help' => t('Help')),
361 'internal' => TRUE,
362 ),
363 'advhr' => array(
364 'path' => $editor['library path'] .'/plugins/advhr',
365 'buttons' => array('advhr' => t('Advanced horizontal rule')),
366 'extended_valid_elements' => array('hr[class|width|size|noshade]'),
367 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advhr',
368 'internal' => TRUE,
369 'load' => TRUE,
370 ),
371 'advimage' => array(
372 'path' => $editor['library path'] .'/plugins/advimage',
373 'extensions' => array('advimage' => t('Advanced image')),
c3bbf69b 374 'extended_valid_elements' => array('img[src|alt|title|align|width|height|hspace|vspace|border|style|class|onmouseover|onmouseout|id|name]'),
14759316 375 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advimage',
376 'internal' => TRUE,
377 'load' => TRUE,
378 ),
379 'advlink' => array(
380 'path' => $editor['library path'] .'/plugins/advlink',
381 'extensions' => array('advlink' => t('Advanced link')),
8942000a 382 'extended_valid_elements' => array('a[name|href|target|title|class|onfocus|onblur|onclick|ondlbclick|onmousedown|onmouseup|onmouseover|onmouseout|onkeypress|onkeydown|onkeyup|id|style|rel]'),
14759316 383 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlink',
384 'internal' => TRUE,
385 'load' => TRUE,
386 ),
387 'autosave' => array(
388 'path' => $editor['library path'] .'/plugins/autosave',
389 'extensions' => array('autosave' => t('Auto save')),
390 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autosave',
391 'internal' => TRUE,
392 'load' => TRUE,
393 ),
394 'contextmenu' => array(
395 'path' => $editor['library path'] .'/plugins/contextmenu',
396 'extensions' => array('contextmenu' => t('Context menu')),
397 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',
398 'internal' => TRUE,
c2eb2080 399 'load' => TRUE,
14759316 400 ),
401 'directionality' => array(
402 'path' => $editor['library path'] .'/plugins/directionality',
403 'buttons' => array('ltr' => t('Left-to-right'), 'rtl' => t('Right-to-left')),
404 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/directionality',
405 'internal' => TRUE,
406 'load' => TRUE,
407 ),
408 'emotions' => array(
409 'path' => $editor['library path'] .'/plugins/emotions',
410 'buttons' => array('emotions' => t('Emotions')),
411 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/emotions',
412 'internal' => TRUE,
413 'load' => TRUE,
414 ),
415 'flash' => array(
416 'path' => $editor['library path'] .'/plugins/flash',
417 'buttons' => array('flash' => t('Flash')),
418 'extended_valid_elements' => array('img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|obj|param|embed]'),
419 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/flash',
420 'internal' => TRUE,
421 'load' => TRUE,
422 ),
423 'font' => array(
424 'path' => $editor['library path'] .'/plugins/font',
425 'buttons' => array('formatselect' => t('HTML block format'), 'fontselect' => t('Font'), 'fontsizeselect' => t('Font size'), 'styleselect' => t('Font style')),
426 'extended_valid_elements' => array('font[face|size|color|style],span[class|align|style]'),
427 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/font',
428 'internal' => TRUE,
429 ),
430 'fullscreen' => array(
431 'path' => $editor['library path'] .'/plugins/fullscreen',
432 'buttons' => array('fullscreen' => t('Fullscreen')),
433 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullscreen',
434 'internal' => TRUE,
435 'load' => TRUE,
436 ),
437 'inlinepopups' => array(
438 'path' => $editor['library path'] .'/plugins/inlinepopups',
439 'extensions' => array('inlinepopups' => t('Inline popups')),
440 'options' => array(
441 'dialog_type' => array('modal'),
442 ),
443 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/inlinepopups',
444 'internal' => TRUE,
445 'load' => TRUE,
446 ),
447 'insertdatetime' => array(
448 'path' => $editor['library path'] .'/plugins/insertdatetime',
449 'buttons' => array('insertdate' => t('Insert date'), 'inserttime' => t('Insert time')),
450 'options' => array(
451 'plugin_insertdate_dateFormat' => array('%Y-%m-%d'),
452 'plugin_insertdate_timeFormat' => array('%H:%M:%S'),
453 ),
454 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/insertdatetime',
455 'internal' => TRUE,
456 'load' => TRUE,
457 ),
458 'layer' => array(
459 'path' => $editor['library path'] .'/plugins/layer',
460 'buttons' => array('insertlayer' => t('Insert layer'), 'moveforward' => t('Move forward'), 'movebackward' => t('Move backward'), 'absolute' => t('Absolute')),
461 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',
462 'internal' => TRUE,
463 'load' => TRUE,
464 ),
465 'paste' => array(
466 'path' => $editor['library path'] .'/plugins/paste',
467 'buttons' => array('pastetext' => t('Paste text'), 'pasteword' => t('Paste from Word'), 'selectall' => t('Select all')),
468 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',
469 'internal' => TRUE,
470 'load' => TRUE,
471 ),
472 'preview' => array(
473 'path' => $editor['library path'] .'/plugins/preview',
474 'buttons' => array('preview' => t('Preview')),
475 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/preview',
476 'internal' => TRUE,
477 'load' => TRUE,
478 ),
479 'print' => array(
480 'path' => $editor['library path'] .'/plugins/print',
481 'buttons' => array('print' => t('Print')),
482 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/print',
483 'internal' => TRUE,
484 'load' => TRUE,
485 ),
486 'searchreplace' => array(
487 'path' => $editor['library path'] .'/plugins/searchreplace',
488 'buttons' => array('search' => t('Search'), 'replace' => t('Replace')),
489 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/searchreplace',
490 'internal' => TRUE,
491 'load' => TRUE,
492 ),
493 'style' => array(
494 'path' => $editor['library path'] .'/plugins/style',
495 'buttons' => array('styleprops' => t('Style properties')),
496 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/style',
497 'internal' => TRUE,
498 'load' => TRUE,
499 ),
500 'table' => array(
501 'path' => $editor['library path'] .'/plugins/table',
502 'buttons' => array('tablecontrols' => t('Table')),
503 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
504 'internal' => TRUE,
505 'load' => TRUE,
506 ),
507 );
329c0500 508 if ($editor['installed version'] > 3) {
509 $plugins['safari'] = array(
510 'path' => $editor['library path'] .'/plugins/safari',
511 'extensions' => array('safari' => t('Safari compatibility')),
512 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',
513 'internal' => TRUE,
514 'load' => TRUE,
515 );
516 }
517 return $plugins;
14759316 518}
519