tinymce-3.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. (function($) {
  2. /**
  3. * Initialize editor instances.
  4. *
  5. * @todo Is the following note still valid for 3.x?
  6. * This function needs to be called before the page is fully loaded, as
  7. * calling tinyMCE.init() after the page is loaded breaks IE6.
  8. *
  9. * @param editorSettings
  10. * An object containing editor settings for each input format.
  11. */
  12. Drupal.wysiwyg.editor.init.tinymce = function(settings) {
  13. // If JS compression is enabled, TinyMCE is unable to autodetect its global
  14. // settinge, hence we need to define them manually.
  15. // @todo Move global library settings somewhere else.
  16. tinyMCE.baseURL = settings.global.editorBasePath;
  17. tinyMCE.srcMode = (settings.global.execMode == 'src' ? '_src' : '');
  18. tinyMCE.gzipMode = (settings.global.execMode == 'gzip');
  19. // Initialize editor configurations.
  20. for (var format in settings) {
  21. if (format == 'global') {
  22. continue;
  23. };
  24. tinyMCE.init(settings[format]);
  25. if (Drupal.settings.wysiwyg.plugins[format]) {
  26. // Load native external plugins.
  27. // Array syntax required; 'native' is a predefined token in JavaScript.
  28. for (var plugin in Drupal.settings.wysiwyg.plugins[format]['native']) {
  29. tinymce.PluginManager.load(plugin, Drupal.settings.wysiwyg.plugins[format]['native'][plugin]);
  30. }
  31. // Load Drupal plugins.
  32. for (var plugin in Drupal.settings.wysiwyg.plugins[format].drupal) {
  33. Drupal.wysiwyg.editor.instance.tinymce.addPlugin(plugin, Drupal.settings.wysiwyg.plugins[format].drupal[plugin], Drupal.settings.wysiwyg.plugins.drupal[plugin]);
  34. }
  35. }
  36. }
  37. };
  38. /**
  39. * Attach this editor to a target element.
  40. *
  41. * See Drupal.wysiwyg.editor.attach.none() for a full desciption of this hook.
  42. */
  43. Drupal.wysiwyg.editor.attach.tinymce = function(context, params, settings) {
  44. // Configure editor settings for this input format.
  45. var ed = new tinymce.Editor(params.field, settings);
  46. // Reset active instance id on any event.
  47. ed.onEvent.add(function(ed, e) {
  48. Drupal.wysiwyg.activeId = ed.id;
  49. });
  50. // Make toolbar buttons wrappable (required for IE).
  51. ed.onPostRender.add(function (ed) {
  52. var $toolbar = $('<div class="wysiwygToolbar"></div>');
  53. $('#' + ed.editorContainer + ' table.mceToolbar > tbody > tr > td').each(function () {
  54. $('<div></div>').addClass(this.className).append($(this).children()).appendTo($toolbar);
  55. });
  56. $('#' + ed.editorContainer + ' table.mceLayout td.mceToolbar').append($toolbar);
  57. $('#' + ed.editorContainer + ' table.mceToolbar').remove();
  58. });
  59. // Remove TinyMCE's internal mceItem class, which was incorrectly added to
  60. // submitted content by Wysiwyg <2.1. TinyMCE only temporarily adds the class
  61. // for placeholder elements. If preemptively set, the class prevents (native)
  62. // editor plugins from gaining an active state, so we have to manually remove
  63. // it prior to attaching the editor. This is done on the client-side instead
  64. // of the server-side, as Wysiwyg has no way to figure out where content is
  65. // stored, and the class only affects editing.
  66. $field = $('#' + params.field);
  67. $field.val($field.val().replace(/(<.+?\s+class=['"][\w\s]*?)\bmceItem\b([\w\s]*?['"].*?>)/ig, '$1$2'));
  68. // Attach editor.
  69. ed.render();
  70. };
  71. /**
  72. * Detach a single or all editors.
  73. *
  74. * See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook.
  75. */
  76. Drupal.wysiwyg.editor.detach.tinymce = function(context, params) {
  77. if (typeof params != 'undefined') {
  78. var instance = tinyMCE.get(params.field);
  79. if (instance) {
  80. instance.save();
  81. instance.remove();
  82. }
  83. }
  84. else {
  85. // Save contents of all editors back into textareas.
  86. tinyMCE.triggerSave();
  87. // Remove all editor instances.
  88. for (var instance in tinyMCE.editors) {
  89. tinyMCE.editors[instance].remove();
  90. }
  91. }
  92. };
  93. Drupal.wysiwyg.editor.instance.tinymce = {
  94. addPlugin: function(plugin, settings, pluginSettings) {
  95. if (typeof Drupal.wysiwyg.plugins[plugin] != 'object') {
  96. return;
  97. }
  98. tinymce.create('tinymce.plugins.' + plugin, {
  99. /**
  100. * Initialize the plugin, executed after the plugin has been created.
  101. *
  102. * @param ed
  103. * The tinymce.Editor instance the plugin is initialized in.
  104. * @param url
  105. * The absolute URL of the plugin location.
  106. */
  107. init: function(ed, url) {
  108. // Register an editor command for this plugin, invoked by the plugin's button.
  109. ed.addCommand(plugin, function() {
  110. if (typeof Drupal.wysiwyg.plugins[plugin].invoke == 'function') {
  111. var data = { format: 'html', node: ed.selection.getNode(), content: ed.selection.getContent() };
  112. // TinyMCE creates a completely new instance for fullscreen mode.
  113. var instanceId = ed.id == 'mce_fullscreen' ? ed.getParam('fullscreen_editor_id') : ed.id;
  114. Drupal.wysiwyg.plugins[plugin].invoke(data, pluginSettings, instanceId);
  115. }
  116. });
  117. // Register the plugin button.
  118. ed.addButton(plugin, {
  119. title : settings.iconTitle,
  120. cmd : plugin,
  121. image : settings.icon
  122. });
  123. // Load custom CSS for editor contents on startup.
  124. ed.onInit.add(function() {
  125. if (settings.css) {
  126. ed.dom.loadCSS(settings.css);
  127. }
  128. });
  129. // Attach: Replace plain text with HTML representations.
  130. ed.onBeforeSetContent.add(function(ed, data) {
  131. if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') {
  132. data.content = Drupal.wysiwyg.plugins[plugin].attach(data.content, pluginSettings, ed.id);
  133. data.content = Drupal.wysiwyg.editor.instance.tinymce.prepareContent(data.content);
  134. }
  135. });
  136. // Detach: Replace HTML representations with plain text.
  137. ed.onGetContent.add(function(ed, data) {
  138. if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') {
  139. data.content = Drupal.wysiwyg.plugins[plugin].detach(data.content, pluginSettings, ed.id);
  140. }
  141. });
  142. // isNode: Return whether the plugin button should be enabled for the
  143. // current selection.
  144. ed.onNodeChange.add(function(ed, command, node) {
  145. if (typeof Drupal.wysiwyg.plugins[plugin].isNode == 'function') {
  146. command.setActive(plugin, Drupal.wysiwyg.plugins[plugin].isNode(node));
  147. }
  148. });
  149. },
  150. /**
  151. * Return information about the plugin as a name/value array.
  152. */
  153. getInfo: function() {
  154. return {
  155. longname: settings.title
  156. };
  157. }
  158. });
  159. // Register plugin.
  160. tinymce.PluginManager.add(plugin, tinymce.plugins[plugin]);
  161. },
  162. openDialog: function(dialog, params) {
  163. var instanceId = this.isFullscreen() ? 'mce_fullscreen' : this.field;
  164. var editor = tinyMCE.get(instanceId);
  165. editor.windowManager.open({
  166. file: dialog.url + '/' + instanceId,
  167. width: dialog.width,
  168. height: dialog.height,
  169. inline: 1
  170. }, params);
  171. },
  172. closeDialog: function(dialog) {
  173. var instanceId = this.isFullscreen() ? 'mce_fullscreen' : this.field;
  174. var editor = tinyMCE.get(instanceId);
  175. editor.windowManager.close(dialog);
  176. },
  177. prepareContent: function(content) {
  178. // Certain content elements need to have additional DOM properties applied
  179. // to prevent this editor from highlighting an internal button in addition
  180. // to the button of a Drupal plugin.
  181. var specialProperties = {
  182. img: { 'class': 'mceItem' }
  183. };
  184. var $content = $('<div>' + content + '</div>'); // No .outerHTML() in jQuery :(
  185. // Find all placeholder/replacement content of Drupal plugins.
  186. $content.find('.drupal-content').each(function() {
  187. // Recursively process DOM elements below this element to apply special
  188. // properties.
  189. var $drupalContent = $(this);
  190. $.each(specialProperties, function(element, properties) {
  191. $drupalContent.find(element).andSelf().each(function() {
  192. for (var property in properties) {
  193. if (property == 'class') {
  194. $(this).addClass(properties[property]);
  195. }
  196. else {
  197. $(this).attr(property, properties[property]);
  198. }
  199. }
  200. });
  201. });
  202. });
  203. return $content.html();
  204. },
  205. insert: function(content) {
  206. content = this.prepareContent(content);
  207. var instanceId = this.isFullscreen() ? 'mce_fullscreen' : this.field;
  208. tinyMCE.execInstanceCommand(instanceId, 'mceInsertContent', false, content);
  209. },
  210. isFullscreen: function() {
  211. // TinyMCE creates a completely new instance for fullscreen mode.
  212. return tinyMCE.activeEditor.id == 'mce_fullscreen' && tinyMCE.activeEditor.getParam('fullscreen_editor_id') == this.field;
  213. }
  214. };
  215. })(jQuery);