diff --git a/includes/media.filter.inc b/includes/media.filter.inc
index e34b6ec..d5c8a04 100644
--- a/includes/media.filter.inc
+++ b/includes/media.filter.inc
@@ -167,39 +167,62 @@ function media_token_to_markup($match, $wysiwyg = FALSE) {
// Track the fid of this file in the {media_filter_usage} table.
media_filter_track_usage($file->fid);
- $attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
- $attribute_whitelist = media_variable_get('wysiwyg_allowed_attributes');
- $settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
-
- // Many media formatters will want to apply width and height independently
- // of the style attribute or the corresponding HTML attributes, so pull
- // these two out into top-level settings. Different WYSIWYG editors have
- // different behavior with respect to whether they store user-specified
- // dimensions in the HTML attributes or the style attribute, so check both.
- // Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
- // HTML attributes are merely hints: CSS takes precedence.
- if (isset($settings['attributes']['style'])) {
- $css_properties = media_parse_css_declarations($settings['attributes']['style']);
- foreach (array('width', 'height') as $dimension) {
- if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
- $settings[$dimension] = substr($css_properties[$dimension], 0, -2);
- }
- elseif (isset($settings['attributes'][$dimension])) {
- $settings[$dimension] = $settings['attributes'][$dimension];
+ if (isset($tag_info['attributes'])) {
+ $attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
+ $attribute_whitelist = media_variable_get('wysiwyg_allowed_attributes');
+ $settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
+
+ // Many media formatters will want to apply width and height independently
+ // of the style attribute or the corresponding HTML attributes, so pull
+ // these two out into top-level settings. Different WYSIWYG editors have
+ // different behavior with respect to whether they store user-specified
+ // dimensions in the HTML attributes or the style attribute, so check both.
+ // Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
+ // HTML attributes are merely hints: CSS takes precedence.
+ if (isset($settings['attributes']['style'])) {
+ $css_properties = media_parse_css_declarations($settings['attributes']['style']);
+ foreach (array('width', 'height') as $dimension) {
+ if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
+ $settings[$dimension] = substr($css_properties[$dimension], 0, -2);
+ }
+ elseif (isset($settings['attributes'][$dimension])) {
+ $settings[$dimension] = $settings['attributes'][$dimension];
+ }
}
}
}
- if ($wysiwyg) {
- $settings['wysiwyg'] = $wysiwyg;
- }
}
catch (Exception $e) {
watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
return '';
}
- $element = media_get_file_without_label($file, $tag_info['view_mode'], $settings);
+ if ($wysiwyg) {
+ $settings['wysiwyg'] = $wysiwyg;
+ // If sending markup to a WYSIWYG, we need to pass the file infomation so
+ // that a inline macro can be generated when the WYSIWYG is detached.
+ // The WYSIWYG plugin is expecting this information in the format of a
+ // urlencoded JSON string stored in the data-file_info attribute of the
+ // element.
+ //
+ // The view-mode of the media representation is hard-coded to be
+ // 'media_preview'. This code assumes that the 'media_preview' view-mode
+ // display has been configured to output a representation of the
+ // inline media item to be shown in the WYSIWYG.
+ $element = media_get_file_without_label($file, 'media_preview', $settings);
+ $data = drupal_json_encode(array(
+ 'type' => 'media',
+ 'fid' => $file->fid,
+ 'view_mode' => $tag_info['view_mode'],
+ ));
+ $element['#attributes']['data-file_info'] = urlencode($data);
+ $element['#attributes']['class'][] = 'media-element';
+ }
+ else {
+ $element = media_get_file_without_label($file, $tag_info['view_mode'], $settings);
+ }
+
return drupal_render($element);
}
diff --git a/js/media.format_form.js b/js/media.format_form.js
index 446cb54..4e77d63 100644
--- a/js/media.format_form.js
+++ b/js/media.format_form.js
@@ -37,7 +37,10 @@ Drupal.media.formatForm.getOptions = function () {
Drupal.media.formatForm.getFormattedMedia = function () {
var formatType = $("select#edit-format option:selected").val();
- return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
+ // NOTE: Hard-coded to grab 'media-preview' display format.
+ // @see wysiwyg-media.js Drupal.wysiwyg.plugins.media.detach() and
+ // media.filter.inc media_token_to_markup().
+ return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats.media_preview };
};
Drupal.media.formatForm.submit = function () {
diff --git a/js/wysiwyg-media.js b/js/wysiwyg-media.js
index 97697f0..d3e938a 100644
--- a/js/wysiwyg-media.js
+++ b/js/wysiwyg-media.js
@@ -6,158 +6,126 @@
(function ($) {
-Drupal.media = Drupal.media || {};
+/**
+ * Defining InsertMedia object to manage the sequence of actions involved in
+ * inserting a media element into the WYSIWYG.
+ * Keeps track of the WYSIWYG instance id.
+ */
+var InsertMedia = function (instance_id) {
+ this.instanceId = instance_id;
+ return this;
+};
-// Define the behavior.
-Drupal.wysiwyg.plugins.media = {
+InsertMedia.prototype = {
/**
- * Initializes the tag map.
+ * Prompt user to select a media item with the media browser.
+ *
+ * @param settings
+ * Settings object to pass on to the media browser.
+ * TODO: Determine if this is actually necessary.
*/
- initializeTagMap: function () {
- if (typeof Drupal.settings.tagmap == 'undefined') {
- Drupal.settings.tagmap = { };
- }
+ prompt: function (settings) {
+ Drupal.media.popups.mediaBrowser($.proxy(this, 'onSelect'), settings);
},
+
/**
- * Execute the button.
- * @TODO: Debug calls from this are never called. What's its function?
+ * On selection of a media item, display item's display configuration form.
*/
- invoke: function (data, settings, instanceId) {
- if (data.format == 'html') {
- Drupal.media.popups.mediaBrowser(function (mediaFiles) {
- Drupal.wysiwyg.plugins.media.mediaBrowserOnSelect(mediaFiles, instanceId);
- }, settings['global']);
- }
+ onSelect: function (media_files) {
+ this.mediaFile = media_files[0];
+ Drupal.media.popups.mediaStyleSelector(this.mediaFile, $.proxy(this, 'insert'), {});
},
/**
- * Respond to the mediaBrowser's onSelect event.
- * @TODO: Debug calls from this are never called. What's its function?
+ * When display config has been set, insert the placeholder markup into the
+ * wysiwyg and generate its corresponding json macro pair to be added to the
+ * tagmap.
*/
- mediaBrowserOnSelect: function (mediaFiles, instanceId) {
- var mediaFile = mediaFiles[0];
- var options = {};
- Drupal.media.popups.mediaStyleSelector(mediaFile, function (formattedMedia) {
- Drupal.wysiwyg.plugins.media.insertMediaFile(mediaFile, formattedMedia.type, formattedMedia.html, formattedMedia.options, Drupal.wysiwyg.instances[instanceId]);
- }, options);
-
- return;
- },
-
- insertMediaFile: function (mediaFile, viewMode, formattedMedia, options, wysiwygInstance) {
+ insert: function (formatted_media) {
+ var element = create_element(formatted_media.html, {
+ fid: this.mediaFile.fid,
+ view_mode: formatted_media.type
+ });
+
+ var markup = outerHTML(element),
+ macro = create_macro(element);
+
+ // Insert placeholder markup into wysiwyg.
+ Drupal.wysiwyg.instances[this.instanceId].insert(markup);
+
+ // Store macro/markup pair in the tagmap.
+ ensure_tagmap();
+ Drupal.settings.tagmap[macro] = markup;
+ }
- this.initializeTagMap();
- // @TODO: the folks @ ckeditor have told us that there is no way
- // to reliably add wrapper divs via normal HTML.
- // There is some method of adding a "fake element"
- // But until then, we're just going to embed to img.
- // This is pretty hacked for now.
- //
- var imgElement = $(this.stripDivs(formattedMedia));
- this.addImageAttributes(imgElement, mediaFile.fid, viewMode, options);
+};
- var toInsert = this.outerHTML(imgElement);
- // Create an inline tag
- var inlineTag = Drupal.wysiwyg.plugins.media.createTag(imgElement);
- // Add it to the tag map in case the user switches input formats
- Drupal.settings.tagmap[inlineTag] = toInsert;
- wysiwygInstance.insert(toInsert);
- },
- /**
- * Gets the HTML content of an element
- *
- * @param jQuery element
- */
- outerHTML: function (element) {
- return $('