wymeditor.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 'version callback' => 'wysiwyg_wymeditor_version',
  30. 'themes callback' => 'wysiwyg_wymeditor_themes',
  31. 'settings callback' => 'wysiwyg_wymeditor_settings',
  32. 'plugin callback' => 'wysiwyg_wymeditor_plugins',
  33. 'versions' => array(
  34. '0.5-rc1' => array(
  35. 'js files' => array('wymeditor.js'),
  36. ),
  37. ),
  38. );
  39. return $editor;
  40. }
  41. /**
  42. * Detect editor version.
  43. *
  44. * @param $editor
  45. * An array containing editor properties as returned from hook_editor().
  46. *
  47. * @return
  48. * The installed editor version.
  49. */
  50. function wysiwyg_wymeditor_version($editor) {
  51. $script = $editor['library path'] . '/jquery.wymeditor.js';
  52. if (!file_exists($script)) {
  53. return;
  54. }
  55. $script = fopen($script, 'r');
  56. fgets($script);
  57. $line = fgets($script);
  58. if (preg_match('@version\s+([0-9a-z\.-]+)@', $line, $version)) {
  59. fclose($script);
  60. return $version[1];
  61. }
  62. fclose($script);
  63. }
  64. /**
  65. * Determine available editor themes or check/reset a given one.
  66. *
  67. * @param $editor
  68. * A processed hook_editor() array of editor properties.
  69. * @param $profile
  70. * A wysiwyg editor profile.
  71. *
  72. * @return
  73. * An array of theme names. The first returned name should be the default
  74. * theme name.
  75. */
  76. function wysiwyg_wymeditor_themes($editor, $profile) {
  77. return array('compact', 'default', 'minimal', 'silver', 'twopanels');
  78. }
  79. /**
  80. * Return runtime editor settings for a given wysiwyg profile.
  81. *
  82. * @param $editor
  83. * A processed hook_editor() array of editor properties.
  84. * @param $config
  85. * An array containing wysiwyg editor profile settings.
  86. * @param $theme
  87. * The name of a theme/GUI/skin to use.
  88. *
  89. * @return
  90. * A settings array to be populated in
  91. * Drupal.settings.wysiwyg.configs.{editor}
  92. */
  93. function wysiwyg_wymeditor_settings($editor, $config, $theme) {
  94. // @todo Setup $library in wysiwyg_load_editor() already.
  95. $library = (isset($editor['library']) ? $editor['library'] : key($editor['libraries']));
  96. $settings = array(
  97. 'basePath' => base_path() . $editor['library path'] . '/',
  98. 'wymPath' => $editor['libraries'][$library]['files'][0],
  99. // @todo Does not work in Drupal; jQuery can live anywhere.
  100. 'jQueryPath' => base_path() . 'misc/jquery.js',
  101. 'updateSelector' => '.form-submit',
  102. 'skin' => $theme,
  103. );
  104. if (isset($config['language'])) {
  105. $settings['lang'] = $config['language'];
  106. }
  107. // Add configured buttons.
  108. $settings['toolsItems'] = array();
  109. if (!empty($config['buttons'])) {
  110. $buttoninfo = _wysiwyg_wymeditor_button_info();
  111. $plugins = wysiwyg_get_plugins($editor['name']);
  112. foreach ($config['buttons'] as $plugin => $buttons) {
  113. foreach ($buttons as $button => $enabled) {
  114. // Iterate separately over buttons and extensions properties.
  115. foreach (array('buttons', 'extensions') as $type) {
  116. // Skip unavailable plugins.
  117. if (!isset($plugins[$plugin][$type][$button])) {
  118. continue;
  119. }
  120. // Add buttons.
  121. if ($type == 'buttons') {
  122. // Merge meta-data for internal default buttons.
  123. if (isset($buttoninfo[$button])) {
  124. $buttoninfo[$button] += array('name' => $button);
  125. $settings['toolsItems'][] = $buttoninfo[$button];
  126. }
  127. // For custom buttons, try to provide a valid button definition.
  128. else {
  129. $settings['toolsItems'][] = array(
  130. 'name' => $button,
  131. 'title' => $plugins[$plugin][$type][$button],
  132. 'css' => 'wym_tools_' . $button,
  133. );
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. if (!empty($config['block_formats'])) {
  141. $containers = array(
  142. 'p' => 'Paragraph',
  143. 'h1' => 'Heading_1',
  144. 'h2' => 'Heading_2',
  145. 'h3' => 'Heading_3',
  146. 'h4' => 'Heading_4',
  147. 'h5' => 'Heading_5',
  148. 'h6' => 'Heading_6',
  149. 'pre' => 'Preformatted',
  150. 'blockquote' => 'Blockquote',
  151. 'th' => 'Table_Header',
  152. );
  153. foreach (explode(',', $config['block_formats']) as $tag) {
  154. if (isset($containers[$tag])) {
  155. $settings['containersItems'][] = array(
  156. 'name' => strtoupper($tag),
  157. 'title' => $containers[$tag],
  158. 'css' => 'wym_containers_' . $tag,
  159. );
  160. }
  161. }
  162. }
  163. if (isset($config['css_setting'])) {
  164. if ($config['css_setting'] == 'theme') {
  165. // WYMeditor only supports one CSS file currently.
  166. $settings['stylesheet'] = reset(wysiwyg_get_css());
  167. }
  168. else if ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  169. $settings['stylesheet'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
  170. }
  171. }
  172. return $settings;
  173. }
  174. /**
  175. * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
  176. */
  177. function wysiwyg_wymeditor_plugins($editor) {
  178. $plugins = array(
  179. 'default' => array(
  180. 'buttons' => array(
  181. 'Bold' => t('Bold'), 'Italic' => t('Italic'),
  182. 'InsertOrderedList' => t('Bullet list'), 'InsertUnorderedList' => t('Numbered list'),
  183. 'Outdent' => t('Outdent'), 'Indent' => t('Indent'),
  184. 'Undo' => t('Undo'), 'Redo' => t('Redo'),
  185. 'CreateLink' => t('Link'), 'Unlink' => t('Unlink'),
  186. 'InsertImage' => t('Image'),
  187. 'Superscript' => t('Superscript'), 'Subscript' => t('Subscript'),
  188. 'ToggleHtml' => t('Source code'),
  189. 'Paste' => t('Paste'),
  190. 'InsertTable' => t('Table'),
  191. 'Preview' => t('Preview'),
  192. ),
  193. 'internal' => TRUE,
  194. ),
  195. );
  196. return $plugins;
  197. }
  198. /**
  199. * Helper function to provide additional meta-data for internal default buttons.
  200. */
  201. function _wysiwyg_wymeditor_button_info() {
  202. return array(
  203. 'Bold' => array('title'=> 'Strong', 'css'=> 'wym_tools_strong'),
  204. 'Italic' => array('title'=> 'Emphasis', 'css'=> 'wym_tools_emphasis'),
  205. 'Superscript' => array('title'=> 'Superscript', 'css'=> 'wym_tools_superscript'),
  206. 'Subscript' => array('title'=> 'Subscript', 'css'=> 'wym_tools_subscript'),
  207. 'InsertOrderedList' => array('title'=> 'Ordered_List', 'css'=> 'wym_tools_ordered_list'),
  208. 'InsertUnorderedList' => array('title'=> 'Unordered_List', 'css'=> 'wym_tools_unordered_list'),
  209. 'Indent' => array('title'=> 'Indent', 'css'=> 'wym_tools_indent'),
  210. 'Outdent' => array('title'=> 'Outdent', 'css'=> 'wym_tools_outdent'),
  211. 'Undo' => array('title'=> 'Undo', 'css'=> 'wym_tools_undo'),
  212. 'Redo' => array('title'=> 'Redo', 'css'=> 'wym_tools_redo'),
  213. 'CreateLink' => array('title'=> 'Link', 'css'=> 'wym_tools_link'),
  214. 'Unlink' => array('title'=> 'Unlink', 'css'=> 'wym_tools_unlink'),
  215. 'InsertImage' => array('title'=> 'Image', 'css'=> 'wym_tools_image'),
  216. 'InsertTable' => array('title'=> 'Table', 'css'=> 'wym_tools_table'),
  217. 'Paste' => array('title'=> 'Paste_From_Word', 'css'=> 'wym_tools_paste'),
  218. 'ToggleHtml' => array('title'=> 'HTML', 'css'=> 'wym_tools_html'),
  219. 'Preview' => array('title'=> 'Preview', 'css'=> 'wym_tools_preview'),
  220. );
  221. }