wysiwyg.api.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Wysiwyg plugin button implementation for Awesome plugin.
  3. */
  4. Drupal.wysiwyg.plugins.awesome = {
  5. /**
  6. * Return whether the passed node belongs to this plugin.
  7. *
  8. * @param node
  9. * The currently focused DOM element in the editor content.
  10. */
  11. isNode: function(node) {
  12. return ($(node).is('img.mymodule-awesome'));
  13. },
  14. /**
  15. * Execute the button.
  16. *
  17. * @param data
  18. * An object containing data about the current selection:
  19. * - format: 'html' when the passed data is HTML content, 'text' when the
  20. * passed data is plain-text content.
  21. * - node: When 'format' is 'html', the focused DOM element in the editor.
  22. * - content: The textual representation of the focused/selected editor
  23. * content.
  24. * @param settings
  25. * The plugin settings, as provided in the plugin's PHP include file.
  26. * @param instanceId
  27. * The ID of the current editor instance.
  28. */
  29. invoke: function(data, settings, instanceId) {
  30. // Generate HTML markup.
  31. if (data.format == 'html') {
  32. // Prevent duplicating a teaser break.
  33. if ($(data.node).is('img.mymodule-awesome')) {
  34. return;
  35. }
  36. var content = this._getPlaceholder(settings);
  37. }
  38. // Generate plain text.
  39. else {
  40. var content = '<!--break-->';
  41. }
  42. // Insert new content into the editor.
  43. if (typeof content != 'undefined') {
  44. Drupal.wysiwyg.instances[instanceId].insert(content);
  45. }
  46. },
  47. /**
  48. * Prepare all plain-text contents of this plugin with HTML representations.
  49. *
  50. * Optional; only required for "inline macro tag-processing" plugins.
  51. *
  52. * @param content
  53. * The plain-text contents of a textarea.
  54. * @param settings
  55. * The plugin settings, as provided in the plugin's PHP include file.
  56. * @param instanceId
  57. * The ID of the current editor instance.
  58. */
  59. attach: function(content, settings, instanceId) {
  60. content = content.replace(/<!--break-->/g, this._getPlaceholder(settings));
  61. return content;
  62. },
  63. /**
  64. * Process all HTML placeholders of this plugin with plain-text contents.
  65. *
  66. * Optional; only required for "inline macro tag-processing" plugins.
  67. *
  68. * @param content
  69. * The HTML content string of the editor.
  70. * @param settings
  71. * The plugin settings, as provided in the plugin's PHP include file.
  72. * @param instanceId
  73. * The ID of the current editor instance.
  74. */
  75. detach: function(content, settings, instanceId) {
  76. var $content = $('<div>' + content + '</div>');
  77. $.each($('img.mymodule-awesome', $content), function (i, elem) {
  78. //...
  79. });
  80. return $content.html();
  81. },
  82. /**
  83. * Helper function to return a HTML placeholder.
  84. *
  85. * The 'drupal-content' CSS class is required for HTML elements in the editor
  86. * content that shall not trigger any editor's native buttons (such as the
  87. * image button for this example placeholder markup).
  88. */
  89. _getPlaceholder: function (settings) {
  90. return '<img src="' + settings.path + '/images/spacer.gif" alt="&lt;--break-&gt;" title="&lt;--break--&gt;" class="wysiwyg-break drupal-content" />';
  91. }
  92. };