wysiwyg.api.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. };
  93. /**
  94. * Because some editors add a lot of new elements with the id attribute set,
  95. * Wysiwyg provides a way to exclude such ids from the ajax_html_ids[] parameter
  96. * sent in AJAX requests. Serverside POST limits such as PHP's max_input_vars
  97. * could otherwise cause the request to be rejected.
  98. *
  99. * The filter gathers a list of jQuery selectors from a global list in
  100. * Drupal.wysiwyg.excludeIdSelectors, joins them with a comma separator and
  101. * wraps them in "[id]:not[...]", which is then run on the document the same way
  102. * Drupal core gathers the ids on every AJAX request.
  103. *
  104. * To add to the filter, set a unique key to Drupal.wysiwyg.excludeIdSelectors
  105. * and set its value to an Array holding one or more selector strings which
  106. * would match the element(s) to exclude.
  107. *
  108. * Beware not to match elements which are not removed before the actual request
  109. * is performed, or Drupal may accidentally reuse the same id for new elements.
  110. *
  111. * Below is a sample from ckeditor.inc matching every id starting with 'cke_'.
  112. */
  113. Drupal.wysiwyg.excludeIdSelectors.wysiwyg_ckeditor = ['[id^="cke_"]'];