diff -uprN markup_snippets_orig/markup_snippets.js markup_snippets/markup_snippets.js --- markup_snippets_orig/markup_snippets.js 1970-01-01 01:00:00.000000000 +0100 +++ markup_snippets/markup_snippets.js 2010-01-09 01:58:46.511586150 +0100 @@ -0,0 +1,49 @@ +// $Id$ + +/* + * Attach AJAX/AHAH event handlers for Wysiwyg integration. + * + * This code takes care of Wysiwyg integration in D6. + * Wysiwyg module needs to be able to detach any attached editor before the + * AJAX request is made or it cannot be re-attached afterwards. + * Drupal 6 does not have detach behaviors so we need to manually call + * Drupal.wysiwygDetach(). Wysiwyg's own behavior won't be able to + * re-attach the editor either because the context doesn't include the + * format selector so we call Drupal.wysiwygAttach() manually too. + * + * @param context + * A DOM element, supplied by Drupal.attachBehaviors(). + * + * This code is based on Drupal.behaviors.attachWysiwyg from Wysiwyg module. + * @see Drupal.behaviors.attachWysiwyg in Wysiwyg module. + */ +Drupal.behaviors.markup_snippets = function (context) { + // Add a global AJAX listener for attaching/detaching Wysiwyg module. + $('#edit-body-wrapper:not(.markup-snippets-processed)', context).each(function () { + $(this).addClass('markup-snippets-processed').bind('ajaxSend', function (event, XMLHttpRequest, ajaxOptions) { + // Detach the Wysiwyg module if this is our request. + if (ajaxOptions.url == '/markup_snippets/js') { + $('.wysiwyg-processed', $(this).parent()).each(function () { + var $this = $(this); + $this.removeClass('wysiwyg-processed'); + if (($this.is(':input') && $this.is(':checked')) || $this.is('div')) { + var params = Drupal.wysiwyg.getParams(this); + Drupal.wysiwygDetach($this.parent(), params); + } + }); + } + }).bind('ajaxComplete', function (event, XMLHttpRequest, ajaxOptions) { + // Re-attach the Wysiwyg module if this is our response. + if (ajaxOptions.url == '/markup_snippets/js') { + $('.wysiwyg:not(.wysiwyg-processed)', $(this).parent()).each(function () { + var $this = $(this); + $this.addClass('wysiwyg-processed'); + if (($this.is(':input') && $this.is(':checked')) || $this.is('div')) { + var params = Drupal.wysiwyg.getParams(this); + Drupal.wysiwygAttach($this.parent(), params); + } + }); + } + }); + }); +} diff -uprN markup_snippets_orig/markup_snippets.module markup_snippets/markup_snippets.module --- markup_snippets_orig/markup_snippets.module 2010-01-09 01:59:38.451586675 +0100 +++ markup_snippets/markup_snippets.module 2010-01-09 01:58:46.511586150 +0100 @@ -14,6 +14,11 @@ function markup_snippets_form_alter(&$fo // make sure we're on a node form and user has permission to use snippets if ($form['#id'] == 'node-form' && user_access('use snippets')) { + // Include the Wysiwyg module integration. + if (module_exists('wysiwyg')) { + drupal_add_js(drupal_get_path('module', 'markup_snippets') . '/markup_snippets.js'); + } + // enforce caching for ahah $form['#cache'] = TRUE; @@ -21,11 +26,11 @@ function markup_snippets_form_alter(&$fo $type = node_get_types('type', $form['#node']); // set up a new fieldset above the node body field - $form['body_field']['markup_snippets'] = array( + $form['markup_snippets'] = array( '#type' => 'fieldset', '#title' => t('Mark-up Snippets'), '#description' => t('Load saved snippets or save the contents of the current !body_label field.', array('!body_label' => $type->body_label)), - '#weight' => -100, + '#weight' => -1, '#collapsible' => TRUE, '#collapsed' => FALSE, ); @@ -47,7 +52,7 @@ function markup_snippets_form_alter(&$fo } // render the actual form field - $form['body_field']['markup_snippets']['markup_snippets_select'] = array( + $form['markup_snippets']['markup_snippets_select'] = array( '#type' => 'select', '#title' => t('Select a snippet to use'), '#description' => $description, @@ -61,15 +66,31 @@ function markup_snippets_form_alter(&$fo 'event' => 'change', ), ); - + + // During AHAH requests, markuo_snippets_select will have a value > 0. + if ($form_state['values']['markup_snippets_select'] > 0) { + // Change body value here to keep cache synced with the rendered form. + $mid = $form_state['values']['markup_snippets_select']; + $snippets = markup_snippets_get_snippets(array(), TRUE, $mid); + $form['body_field']['body']['#default_value'] = $snippets[$mid]['snippet']; + } + // we need a field for naming our snippet - $form['body_field']['markup_snippets']['markup_snippets_name'] = array( + $form['markup_snippets']['markup_snippets_name'] = array( '#type' => 'textfield', '#title' => t('Enter a name to save the current snippet'), '#size' => 30, '#description' => t('If you enter a name in this box then the contents of the !body_label field will be saved as a snippet when you click Save.', array('!body_label' => $type->body_label)) ); - + + // We need a custom submit handler for AHAH, must use a button for that, + // but it can have #access => FALSE so the user won't see it. + $form['markup_snippets']['markup_snippets_submit'] = array( + '#type' => 'submit', + '#access' => FALSE, + '#value' => 'submit_snippet', + '#submit' => array('markup_snippets_get_snippets_submit'), + ); } } @@ -391,6 +412,17 @@ function markup_snippets_get_snippets($s return $snippets; } +/* + * Custom submit handler for AHAH requests. + */ +function markup_snippets_get_snippets_submit($form, &$form_state) { + $node = $form_state['values']; + unset($form_state['submit_handlers']); + form_execute_handlers('submit', $form, $form_state); + $form_state['node'] = $node; + $form_state['rebuild'] = TRUE; +} + /** * Implementation of ahah callback for node form. */ @@ -407,10 +439,7 @@ function markup_snippets_js() { // causes an error because the node gets locked. // Preparing to retrieve form from cache - // Old line: - // $form_state = array('storage' => NULL, 'submitted' => FALSE); - // Our replacement: - $form_state = array('storage' => NULL, 'rebuild' => TRUE); + $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; // Retrieve the form from the cache @@ -430,27 +459,14 @@ function markup_snippets_js() { // CUSTOM CODE STARTS HERE // Now we have the latest $form, with the changes made by our - // AHAH enabled field - we can now build our mark-up to return - - // get the node type info we need for our body field - $node = $form['#node']; - $type = node_get_types('type', $node); - - // load the snippet from the posted data - $mid = $form_state['post']['markup_snippets_select']; - $snippet = markup_snippets_get_snippets(array(), TRUE, $mid); - - // build the returned mark-up - // we are mimicking the output of the node_get_body() function - $output = ''; - $output .= ''; - $output .= '