yui.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * @file
  4. * Editor integration functions for YUI editor.
  5. */
  6. /**
  7. * Plugin implementation of hook_editor().
  8. */
  9. function wysiwyg_yui_editor() {
  10. $editor['yui'] = array(
  11. 'title' => 'YUI editor',
  12. 'vendor url' => 'http://developer.yahoo.com/yui/editor/',
  13. 'download url' => 'http://developer.yahoo.com/yui/download/',
  14. 'library path' => wysiwyg_get_path('yui') . '/build',
  15. 'libraries' => array(
  16. 'min' => array(
  17. 'title' => 'Minified',
  18. 'files' => array(
  19. 'yahoo-dom-event/yahoo-dom-event.js',
  20. 'animation/animation-min.js',
  21. 'element/element-min.js',
  22. 'container/container-min.js',
  23. 'menu/menu-min.js',
  24. 'button/button-min.js',
  25. 'editor/editor-min.js',
  26. ),
  27. ),
  28. 'src' => array(
  29. 'title' => 'Source',
  30. 'files' => array(
  31. 'yahoo-dom-event/yahoo-dom-event.js',
  32. 'animation/animation.js',
  33. 'element/element.js',
  34. 'container/container.js',
  35. 'menu/menu.js',
  36. 'button/button.js',
  37. 'editor/editor.js',
  38. ),
  39. ),
  40. ),
  41. 'version callback' => 'wysiwyg_yui_version',
  42. 'themes callback' => 'wysiwyg_yui_themes',
  43. 'load callback' => 'wysiwyg_yui_load',
  44. 'settings callback' => 'wysiwyg_yui_settings',
  45. 'plugin callback' => 'wysiwyg_yui_plugins',
  46. 'versions' => array(
  47. '2.7.0' => array(
  48. 'js files' => array('yui.js'),
  49. ),
  50. ),
  51. );
  52. return $editor;
  53. }
  54. /**
  55. * Detect editor version.
  56. *
  57. * @param $editor
  58. * An array containing editor properties as returned from hook_editor().
  59. *
  60. * @return
  61. * The installed editor version.
  62. */
  63. function wysiwyg_yui_version($editor) {
  64. $library = $editor['library path'] . '/editor/editor.js';
  65. if (!file_exists($library)) {
  66. return;
  67. }
  68. $library = fopen($library, 'r');
  69. $max_lines = 10;
  70. while ($max_lines && $line = fgets($library, 60)) {
  71. if (preg_match('@version:\s([0-9\.]+)@', $line, $version)) {
  72. fclose($library);
  73. return $version[1];
  74. }
  75. $max_lines--;
  76. }
  77. fclose($library);
  78. }
  79. /**
  80. * Determine available editor themes or check/reset a given one.
  81. *
  82. * @param $editor
  83. * A processed hook_editor() array of editor properties.
  84. * @param $profile
  85. * A wysiwyg editor profile.
  86. *
  87. * @return
  88. * An array of theme names. The first returned name should be the default
  89. * theme name.
  90. */
  91. function wysiwyg_yui_themes($editor, $profile) {
  92. return array('sam');
  93. }
  94. /**
  95. * Perform additional actions upon loading this editor.
  96. *
  97. * @param $editor
  98. * A processed hook_editor() array of editor properties.
  99. * @param $library
  100. * The internal library name (array key) to use.
  101. */
  102. function wysiwyg_yui_load($editor, $library) {
  103. drupal_add_css($editor['library path'] . '/menu/assets/skins/sam/menu.css');
  104. drupal_add_css($editor['library path'] . '/button/assets/skins/sam/button.css');
  105. drupal_add_css($editor['library path'] . '/fonts/fonts-min.css');
  106. drupal_add_css($editor['library path'] . '/container/assets/skins/sam/container.css');
  107. drupal_add_css($editor['library path'] . '/editor/assets/skins/sam/editor.css');
  108. }
  109. /**
  110. * Return runtime editor settings for a given wysiwyg profile.
  111. *
  112. * @param $editor
  113. * A processed hook_editor() array of editor properties.
  114. * @param $config
  115. * An array containing wysiwyg editor profile settings.
  116. * @param $theme
  117. * The name of a theme/GUI/skin to use.
  118. *
  119. * @return
  120. * A settings array to be populated in
  121. * Drupal.settings.wysiwyg.configs.{editor}
  122. */
  123. function wysiwyg_yui_settings($editor, $config, $theme) {
  124. $settings = array(
  125. 'theme' => $theme,
  126. 'animate' => TRUE,
  127. 'handleSubmit' => TRUE,
  128. 'markup' => 'xhtml',
  129. 'ptags' => TRUE,
  130. );
  131. if (isset($config['path_loc']) && $config['path_loc'] != 'none') {
  132. $settings['dompath'] = $config['path_loc'];
  133. }
  134. // Enable auto-height feature when editor should be resizable.
  135. if (!empty($config['resizing'])) {
  136. $settings['autoHeight'] = TRUE;
  137. }
  138. $settings += array(
  139. 'toolbar' => array(
  140. 'collapse' => FALSE,
  141. 'draggable' => TRUE,
  142. 'buttonType' => 'advanced',
  143. 'buttons' => array(),
  144. ),
  145. );
  146. if (!empty($config['buttons'])) {
  147. $buttons = array();
  148. foreach ($config['buttons'] as $plugin => $enabled_buttons) {
  149. foreach ($enabled_buttons as $button => $enabled) {
  150. $extra = array();
  151. if ($button == 'heading') {
  152. $extra = array('menu' => array(
  153. array('text' => 'Normal', 'value' => 'none', 'checked' => TRUE),
  154. ));
  155. if (!empty($config['block_formats'])) {
  156. $headings = array(
  157. 'p' => array('text' => 'Paragraph', 'value' => 'p'),
  158. 'h1' => array('text' => 'Heading 1', 'value' => 'h1'),
  159. 'h2' => array('text' => 'Heading 2', 'value' => 'h2'),
  160. 'h3' => array('text' => 'Heading 3', 'value' => 'h3'),
  161. 'h4' => array('text' => 'Heading 4', 'value' => 'h4'),
  162. 'h5' => array('text' => 'Heading 5', 'value' => 'h5'),
  163. 'h6' => array('text' => 'Heading 6', 'value' => 'h6'),
  164. );
  165. foreach (explode(',', $config['block_formats']) as $tag) {
  166. if (isset($headings[$tag])) {
  167. $extra['menu'][] = $headings[$tag];
  168. }
  169. }
  170. }
  171. }
  172. else if ($button == 'fontname') {
  173. $extra = array('menu' => array(
  174. array('text' => 'Arial', 'checked' => TRUE),
  175. array('text' => 'Arial Black'),
  176. array('text' => 'Comic Sans MS'),
  177. array('text' => 'Courier New'),
  178. array('text' => 'Lucida Console'),
  179. array('text' => 'Tahoma'),
  180. array('text' => 'Times New Roman'),
  181. array('text' => 'Trebuchet MS'),
  182. array('text' => 'Verdana'),
  183. ));
  184. }
  185. $buttons[] = wysiwyg_yui_button_setting($editor, $plugin, $button, $extra);
  186. }
  187. }
  188. // Group buttons in a dummy group.
  189. $buttons = array('group' => 'default', 'label' => '', 'buttons' => $buttons);
  190. $settings['toolbar']['buttons'] = array($buttons);
  191. }
  192. if (isset($config['css_setting'])) {
  193. if ($config['css_setting'] == 'theme') {
  194. $settings['extracss'] = wysiwyg_get_css();
  195. }
  196. else if ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  197. $settings['extracss'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
  198. $settings['extracss'] = explode(',', $settings['extracss']);
  199. }
  200. // YUI only supports inline CSS, so we need to use @import directives.
  201. // Syntax: '@import "/base/path/to/theme/style.css"; '
  202. if (!empty($settings['extracss'])) {
  203. $settings['extracss'] = '@import "' . implode('"; @import "', $settings['extracss']) . '";';
  204. }
  205. }
  206. return $settings;
  207. }
  208. /**
  209. * Create the JavaScript structure for a YUI button.
  210. *
  211. * @param $editor
  212. * A processed hook_editor() array of editor properties.
  213. * @param $plugin
  214. * The internal name of a plugin.
  215. * @param $button
  216. * The internal name of a button, defined by $plugin.
  217. * @param $extra
  218. * (optional) An array containing arbitrary other elements to add to the
  219. * resulting button.
  220. */
  221. function wysiwyg_yui_button_setting($editor, $plugin, $button, $extra = array()) {
  222. static $plugins;
  223. if (!isset($plugins)) {
  224. // @todo Invoke all enabled plugins, not just internals.
  225. $plugins = wysiwyg_yui_plugins($editor);
  226. }
  227. // Return a simple separator.
  228. if ($button === 'separator') {
  229. return array('type' => 'separator');
  230. }
  231. // Setup defaults.
  232. $type = 'push';
  233. $label = $plugins[$plugin]['buttons'][$button];
  234. // Special handling for certain buttons.
  235. if (in_array($button, array('heading', 'fontname'))) {
  236. $type = 'select';
  237. $label = $extra['menu'][0]['text'];
  238. }
  239. elseif (in_array($button, array('fontsize'))) {
  240. $type = 'spin';
  241. }
  242. elseif (in_array($button, array('forecolor', 'backcolor'))) {
  243. $type = 'color';
  244. }
  245. $button = array(
  246. 'type' => $type,
  247. 'label' => $label,
  248. 'value' => $button,
  249. );
  250. // Add arbitrary other elements, if defined.
  251. if (!empty($extra)) {
  252. $button = array_merge($button, $extra);
  253. }
  254. return $button;
  255. }
  256. /**
  257. * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
  258. */
  259. function wysiwyg_yui_plugins($editor) {
  260. return array(
  261. 'default' => array(
  262. 'buttons' => array(
  263. 'bold' => t('Bold'), 'italic' => t('Italic'), 'underline' => t('Underline'),
  264. 'strikethrough' => t('Strike-through'),
  265. 'justifyleft' => t('Align left'), 'justifycenter' => t('Align center'), 'justifyright' => t('Align right'), 'justifyfull' => t('Justify'),
  266. 'insertunorderedlist' => t('Bullet list'), 'insertorderedlist' => t('Numbered list'),
  267. 'outdent' => t('Outdent'), 'indent' => t('Indent'),
  268. 'undo' => t('Undo'), 'redo' => t('Redo'),
  269. 'createlink' => t('Link'),
  270. 'insertimage' => t('Image'),
  271. 'forecolor' => t('Font Color'), 'backcolor' => t('Background Color'),
  272. 'superscript' => t('Sup'), 'subscript' => t('Sub'),
  273. 'hiddenelements' => t('Show/hide hidden elements'),
  274. 'removeformat' => t('Remove format'),
  275. 'heading' => t('HTML block format'), 'fontname' => t('Font'), 'fontsize' => t('Font size'),
  276. ),
  277. 'internal' => TRUE,
  278. ),
  279. );
  280. }