summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.txt1
-rw-r--r--wysiwyg.init.js2
-rw-r--r--wysiwyg.js74
3 files changed, 46 insertions, 31 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 46d0975..3428a55 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -6,6 +6,7 @@ Wysiwyg x.x-x.x, xxxx-xx-xx
Wysiwyg 5.x-x.x, xxxx-xx-xx
---------------------------
+#328052 by sun: Fixed switching input formats leads to wrong editor/state.
#337569 by sun: Fixed different profiles for same editor are not respected.
#340195 by sun: Fixed #after_build function not invoked on all forms.
#333521 by sun: Fixed TinyMCE version detection to look at the actual script.
diff --git a/wysiwyg.init.js b/wysiwyg.init.js
index d1780d2..53ba744 100644
--- a/wysiwyg.init.js
+++ b/wysiwyg.init.js
@@ -2,7 +2,7 @@
Drupal.behaviors = Drupal.behaviors || {}; // D5 only.
-Drupal.wysiwyg = Drupal.wysiwyg || {};
+Drupal.wysiwyg = Drupal.wysiwyg || { 'instances': {} };
Drupal.wysiwyg.editor = Drupal.wysiwyg.editor || { 'init': {}, 'attach': {}, 'detach': {} };
diff --git a/wysiwyg.js b/wysiwyg.js
index d531618..526a557 100644
--- a/wysiwyg.js
+++ b/wysiwyg.js
@@ -34,19 +34,7 @@ Drupal.wysiwygInit = function() {
*/
Drupal.behaviors.attachWysiwyg = function(context) {
$('.wysiwyg:not(.wysiwyg-processed)', context).each(function() {
- // Parse the element's CSS classes into parameters.
- // Format is wysiwyg-name-value.
- var classes = this.className.split(' ');
- var params = {};
- for (var i in classes) {
- if (classes[i].substr(0, 8) == 'wysiwyg-') {
- var parts = classes[i].split('-');
- var value = parts.slice(2).join('-');
- params[parts[1]] = value;
- }
- }
- // Convert format id to string.
- params.format = 'format' + params.format;
+ var params = Drupal.wysiwyg.getParams(this);
$this = $(this);
// Directly attach this editor, if the input format is enabled or there is
// only one input format at all.
@@ -55,12 +43,13 @@ Drupal.behaviors.attachWysiwyg = function(context) {
Drupal.wysiwygAttach(context, params);
}
// Attach onChange handlers to input format selector elements.
- // @todo To support different editors on the same page, we need to store
- // the last attached editor of each target element separately.
if ($this.is(':input')) {
$this.change(function() {
- Drupal.wysiwygDetach(context, params);
- Drupal.wysiwygAttach(context, params);
+ // If not disabled, detach the current and attach a new editor.
+ if (Drupal.wysiwyg.instances[params.field].status) {
+ Drupal.wysiwygDetach(context, params);
+ Drupal.wysiwygAttach(context, params);
+ }
});
}
$this.addClass('wysiwyg-processed');
@@ -84,6 +73,11 @@ Drupal.wysiwygAttach = function(context, params) {
if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
// Attach editor.
Drupal.wysiwyg.editor.attach[params.editor](context, params, (Drupal.settings.wysiwyg.configs[params.editor] ? Drupal.wysiwyg.clone(Drupal.settings.wysiwyg.configs[params.editor][params.format]) : {}));
+ // (Re-)initialize field instance.
+ Drupal.wysiwyg.instances[params.field] = {};
+ // Store new editor name and status for this field.
+ Drupal.wysiwyg.instances[params.field].editor = params.editor;
+ Drupal.wysiwyg.instances[params.field].status = true;
// Display toggle link.
$('#wysiwyg-toggle-' + params.field).show();
}
@@ -96,19 +90,16 @@ Drupal.wysiwygAttach = function(context, params) {
/**
* Detach all editors from a target element.
*
- * Until there is a central registry of target elements storing the currently
- * attached editor, we simply invoke the detach hook of all editors to ensure
- * that no editor is attached to the target element.
- *
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
*/
Drupal.wysiwygDetach = function(context, params) {
- jQuery.each(Drupal.wysiwyg.editor.detach, function(editor) {
- this(context, params);
- });
+ var editor = Drupal.wysiwyg.instances[params.field].editor;
+ if (jQuery.isFunction(Drupal.wysiwyg.editor.detach[editor])) {
+ Drupal.wysiwyg.editor.detach[editor](context, params);
+ }
}
/**
@@ -122,19 +113,22 @@ Drupal.wysiwygDetach = function(context, params) {
Drupal.wysiwygAttachToggleLink = function(context, params) {
var text = document.createTextNode(Drupal.settings.wysiwyg.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
var a = document.createElement('a');
- $(a).toggle(
- function() {
+ $(a).click(function() {
+ if (Drupal.wysiwyg.instances[params.field].status) {
Drupal.wysiwygDetach(context, params);
+ Drupal.wysiwyg.instances[params.field].status = false;
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.enable).blur();
// After disabling the editor, re-attach default behaviors.
Drupal.wysiwyg.editor.attach.none(context, params);
- },
- function() {
+ }
+ else {
// Before enabling the editor, detach default behaviors.
Drupal.wysiwyg.editor.detach.none(context, params);
- Drupal.wysiwygAttach(context, params);
+ // Attach the editor using parameters of the currently selected input format.
+ Drupal.wysiwygAttach(context, Drupal.wysiwyg.getParams($('.wysiwyg-field-' + params.field + ':checked', context).get(0)));
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.disable).blur();
- })
+ }
+ })
.attr('id', 'wysiwyg-toggle-' + params.field)
.attr('href', 'javascript:void(0);')
.append(text);
@@ -144,6 +138,26 @@ Drupal.wysiwygAttachToggleLink = function(context, params) {
}
/**
+ * Parse the CSS classes of an input format DOM element into parameters.
+ *
+ * Format is "wysiwyg-name-value".
+ */
+Drupal.wysiwyg.getParams = function(element) {
+ var classes = element.className.split(' ');
+ var params = {};
+ for (var i in classes) {
+ if (classes[i].substr(0, 8) == 'wysiwyg-') {
+ var parts = classes[i].split('-');
+ var value = parts.slice(2).join('-');
+ params[parts[1]] = value;
+ }
+ }
+ // Convert format id into string.
+ params.format = 'format' + params.format;
+ return params;
+}
+
+/**
* Clone a configuration object recursively.
*
* @param obj