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