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