wymeditor.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @file
  4. * Editor integration functions for WYMeditor.
  5. */
  6. /**
  7. * Plugin implementation of hook_editor().
  8. */
  9. function wysiwyg_wymeditor_editor() {
  10. $editor['wymeditor'] = array(
  11. 'title' => 'WYMeditor',
  12. 'vendor url' => 'http://www.wymeditor.org/',
  13. 'download url' => 'http://www.wymeditor.org/download/',
  14. 'library path' => wysiwyg_get_path('wymeditor') . '/wymeditor',
  15. 'libraries' => array(
  16. 'min' => array(
  17. 'title' => 'Minified',
  18. 'files' => array('jquery.wymeditor.min.js'),
  19. ),
  20. 'pack' => array(
  21. 'title' => 'Packed',
  22. 'files' => array('jquery.wymeditor.pack.js'),
  23. ),
  24. 'src' => array(
  25. 'title' => 'Source',
  26. 'files' => array('jquery.wymeditor.js'),
  27. ),
  28. ),
  29. 'verified version range' => array('0.5', '1.0.0b5'),
  30. 'version callback' => 'wysiwyg_wymeditor_version',
  31. 'themes callback' => 'wysiwyg_wymeditor_themes',
  32. 'settings form callback' => 'wysiwyg_wymeditor_settings_form',
  33. 'settings callback' => 'wysiwyg_wymeditor_settings',
  34. 'plugin callback' => '_wysiwyg_wymeditor_plugins',
  35. 'versions' => array(
  36. '0.5-rc1' => array(
  37. 'js files' => array('wymeditor.js'),
  38. ),
  39. ),
  40. );
  41. return $editor;
  42. }
  43. /**
  44. * Detect editor version.
  45. *
  46. * @param $editor
  47. * An array containing editor properties as returned from hook_editor().
  48. *
  49. * @return
  50. * The installed editor version.
  51. */
  52. function wysiwyg_wymeditor_version($editor) {
  53. $script = $editor['library path'] . '/jquery.wymeditor.js';
  54. if (!file_exists($script)) {
  55. return;
  56. }
  57. $script = fopen($script, 'r');
  58. fgets($script);
  59. $line = fgets($script);
  60. if (preg_match('@version\s+([0-9a-z\.-]+)@', $line, $version)) {
  61. fclose($script);
  62. return $version[1];
  63. }
  64. fclose($script);
  65. }
  66. /**
  67. * Determine available editor themes or check/reset a given one.
  68. *
  69. * @param $editor
  70. * A processed hook_editor() array of editor properties.
  71. * @param $profile
  72. * A wysiwyg editor profile.
  73. *
  74. * @return
  75. * An array of theme names. The first returned name should be the default
  76. * theme name.
  77. */
  78. function wysiwyg_wymeditor_themes($editor, $profile) {
  79. return array('compact', 'default', 'minimal', 'silver', 'twopanels');
  80. }
  81. /**
  82. * Enhances the editor profile settings form for WYMeditor.
  83. *
  84. * @see http://wymeditor.readthedocs.org/en/latest/version_1.0_and_0.5/getting_started/customize.html
  85. */
  86. function wysiwyg_wymeditor_settings_form(&$form, &$form_state) {
  87. $profile = $form_state['wysiwyg_profile'];
  88. $settings = $profile->settings;
  89. $settings += array(
  90. 'block_formats' => 'p,blockquote,pre,h2,h3,h4,h5,h6,div',
  91. );
  92. $form['css']['#description'] = t('Note: WYMeditor can only load a single stylesheet into the editor.');
  93. $form['css']['block_formats'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('Block formats'),
  96. '#default_value' => $settings['block_formats'],
  97. '#size' => 40,
  98. '#maxlength' => 250,
  99. '#description' => t('Comma separated list of HTML block formats. Possible values: <code>@format-list</code>.', array('@format-list' => 'p,h1,h2,h3,h4,h5,h6,blockquote,pre,th')),
  100. );
  101. }
  102. /**
  103. * Return runtime editor settings for a given wysiwyg profile.
  104. *
  105. * @param $editor
  106. * A processed hook_editor() array of editor properties.
  107. * @param $config
  108. * An array containing wysiwyg editor profile settings.
  109. * @param $theme
  110. * The name of a theme/GUI/skin to use.
  111. *
  112. * @return
  113. * A settings array to be populated in
  114. * Drupal.settings.wysiwyg.configs.{editor}
  115. */
  116. function wysiwyg_wymeditor_settings($editor, $config, $theme) {
  117. // @todo Setup $library in wysiwyg_load_editor() already.
  118. $library = (isset($editor['library']) ? $editor['library'] : key($editor['libraries']));
  119. $settings = array(
  120. 'basePath' => base_path() . $editor['library path'] . '/',
  121. 'wymPath' => $editor['libraries'][$library]['files'][0],
  122. // @todo Does not work in Drupal; jQuery can live anywhere.
  123. 'jQueryPath' => base_path() . 'misc/jquery.js',
  124. // WYMeditor's update event handler will revert the field contents if
  125. // changes were made after it was detached. Wysiwyg takes care of submit
  126. // events anyway so make sure WYMeditor does not bind it anywhere.
  127. 'updateSelector' => '#wysiwyg-no-element',
  128. 'updateEvent' => 'wysiwyg-no-event',
  129. 'skin' => $theme,
  130. );
  131. if (isset($config['language'])) {
  132. $settings['lang'] = $config['language'];
  133. }
  134. // Add configured buttons.
  135. $settings['toolsItems'] = array();
  136. if (!empty($config['buttons'])) {
  137. $buttoninfo = _wysiwyg_wymeditor_button_info();
  138. $plugins = wysiwyg_get_plugins($editor['name']);
  139. foreach ($config['buttons'] as $plugin => $buttons) {
  140. foreach ($buttons as $button => $enabled) {
  141. // Iterate separately over buttons and extensions properties.
  142. foreach (array('buttons', 'extensions') as $type) {
  143. // Skip unavailable plugins.
  144. if (!isset($plugins[$plugin][$type][$button])) {
  145. continue;
  146. }
  147. // Add buttons.
  148. if ($type == 'buttons') {
  149. // Merge meta-data for internal default buttons.
  150. if (isset($buttoninfo[$button])) {
  151. $buttoninfo[$button] += array('name' => $button);
  152. $settings['toolsItems'][] = $buttoninfo[$button];
  153. }
  154. // For custom buttons, try to provide a valid button definition.
  155. else {
  156. $settings['toolsItems'][] = array(
  157. 'name' => $button,
  158. 'title' => $plugins[$plugin][$type][$button],
  159. 'css' => 'wym_tools_' . $button,
  160. );
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. if (!empty($config['block_formats'])) {
  168. $containers = array(
  169. 'p' => 'Paragraph',
  170. 'h1' => 'Heading_1',
  171. 'h2' => 'Heading_2',
  172. 'h3' => 'Heading_3',
  173. 'h4' => 'Heading_4',
  174. 'h5' => 'Heading_5',
  175. 'h6' => 'Heading_6',
  176. 'pre' => 'Preformatted',
  177. 'blockquote' => 'Blockquote',
  178. 'th' => 'Table_Header',
  179. );
  180. foreach (explode(',', preg_replace('@\s+@', '', $config['block_formats'])) as $tag) {
  181. if (isset($containers[$tag])) {
  182. $settings['containersItems'][] = array(
  183. 'name' => strtoupper($tag),
  184. 'title' => $containers[$tag],
  185. 'css' => 'wym_containers_' . $tag,
  186. );
  187. }
  188. }
  189. }
  190. // Add editor content stylesheet.
  191. if (isset($config['css_setting'])) {
  192. if ($config['css_setting'] == 'theme') {
  193. // WYMeditor only supports one CSS file currently.
  194. $css = wysiwyg_get_css(isset($config['css_theme']) ? $config['css_theme'] : '');
  195. $settings['stylesheet'] = reset($css);
  196. }
  197. elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  198. $settings['stylesheet'] = strtr($config['css_path'], array(
  199. '%b' => base_path(),
  200. '%t' => drupal_get_path('theme', variable_get('theme_default', NULL)),
  201. '%q' => variable_get('css_js_query_string', ''),
  202. ));
  203. }
  204. }
  205. return $settings;
  206. }
  207. /**
  208. * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
  209. */
  210. function _wysiwyg_wymeditor_plugins($editor) {
  211. $plugins = array(
  212. 'default' => array(
  213. 'buttons' => array(
  214. 'Bold' => t('Bold'), 'Italic' => t('Italic'),
  215. 'InsertOrderedList' => t('Numbered list'), 'InsertUnorderedList' => t('Bullet list'),
  216. 'Outdent' => t('Outdent'), 'Indent' => t('Indent'),
  217. 'Undo' => t('Undo'), 'Redo' => t('Redo'),
  218. 'CreateLink' => t('Link'), 'Unlink' => t('Unlink'),
  219. 'InsertImage' => t('Image'),
  220. 'Superscript' => t('Superscript'), 'Subscript' => t('Subscript'),
  221. 'ToggleHtml' => t('Source code'),
  222. 'Paste' => t('Paste'),
  223. 'InsertTable' => t('Table'),
  224. 'Preview' => t('Preview'),
  225. ),
  226. 'internal' => TRUE,
  227. ),
  228. );
  229. return $plugins;
  230. }
  231. /**
  232. * Helper function to provide additional meta-data for internal default buttons.
  233. */
  234. function _wysiwyg_wymeditor_button_info() {
  235. return array(
  236. 'Bold' => array('title' => 'Strong', 'css' => 'wym_tools_strong'),
  237. 'Italic' => array('title' => 'Emphasis', 'css' => 'wym_tools_emphasis'),
  238. 'Superscript' => array('title' => 'Superscript', 'css' => 'wym_tools_superscript'),
  239. 'Subscript' => array('title' => 'Subscript', 'css' => 'wym_tools_subscript'),
  240. 'InsertOrderedList' => array('title' => 'Ordered_List', 'css' => 'wym_tools_ordered_list'),
  241. 'InsertUnorderedList' => array('title' => 'Unordered_List', 'css' => 'wym_tools_unordered_list'),
  242. 'Indent' => array('title' => 'Indent', 'css' => 'wym_tools_indent'),
  243. 'Outdent' => array('title' => 'Outdent', 'css' => 'wym_tools_outdent'),
  244. 'Undo' => array('title' => 'Undo', 'css' => 'wym_tools_undo'),
  245. 'Redo' => array('title' => 'Redo', 'css' => 'wym_tools_redo'),
  246. 'CreateLink' => array('title' => 'Link', 'css' => 'wym_tools_link'),
  247. 'Unlink' => array('title' => 'Unlink', 'css' => 'wym_tools_unlink'),
  248. 'InsertImage' => array('title' => 'Image', 'css' => 'wym_tools_image'),
  249. 'InsertTable' => array('title' => 'Table', 'css' => 'wym_tools_table'),
  250. 'Paste' => array('title' => 'Paste_From_Word', 'css' => 'wym_tools_paste'),
  251. 'ToggleHtml' => array('title' => 'HTML', 'css' => 'wym_tools_html'),
  252. 'Preview' => array('title' => 'Preview', 'css' => 'wym_tools_preview'),
  253. );
  254. }