Where is the right place to add this function: http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onDblClick ?
[edit] link fixed

Comments

TwoD’s picture

That link didn't work for me, so I'm not sure exactly what you're trying to do. If you mean where to customize the settings, we've got a Drupal hook for that so you don't have to override or hack the init function used by Wysiwyg module.

To implement it, create these files:
sites/default/modules/MYMODULE/MYMODULE.info

name = Custom
description = My custom settings overrides
core = 7.x

sites/default/modules/MYMODULE/MYMODULE.module

/**
 * Implements hook_wysiwyg_editor_settings_alter().
 */
function MYMODULE_wysiwyg_editor_settings_alter(&$settings, $context) {
  if ($context['profile']->editor == 'tinymce') {
    $settings['some_setting'] = "Some string value";
    $settings['some_other_setting'] = TRUE;
    $settings['yet_another_setting'] = array('string1', 5, 'string 2');
    // etc...
  }
}

Anything put in the $settings object will be passed directly to TinyMCE upon init of an instance as the settings parameter (after being converted from PHP types to JavaScript types).
We can't however set JavaScript Functions, Function/Object references, or Regular Expressions at this point because those can't be accurately represented as JSON types. If you need that there are other ways, but I'd need to know exactly what part of TinyMCE you need to hook into so I can give an accurate answer which hopefully avoids modifying Wysiwyg or TinyMCE files - so upgrading them will still be easy.

raincloud’s picture

Sorry, I just copied pasted the link.. anyway, here is the working link to tinymce forum post defining the exact same problem that I have now, and third answer in that post contains that link to tinymce onDblClick event which I'd like to override.

http://www.tinymce.com/forum/viewtopic.php?id=28556

In short, the problem is that when you double click a word inside any browser (not necessarily in tinymce, just anywhere) then the browser selects the word and a space behind the word. I need to eliminate that space on doubleclick inside tinymce (it's a special case app). Otherwise, user will have to eliminate it manually each time, many times.

I have some idea how to reach this goal using the onDblClick in tinymce, but the first thing is to be able to catch this event at all, and with Wysiwyg, I don't know the way.

Please let me know of *any* way to define the ed.onDblClick.add(), the less hacking Wysiwyg the better ofc.

alanom’s picture

The basic question is, how to add a custom function to TinyMCE's initialisation.

If the example in the original post has too many other side issues for a clean answer, here's a nice simple one: how to apply this simple setting, taken from here-

paste_postprocess : function(pl, o) {
            // remove &nbsp
            o.node.innerHTML = o.node.innerHTML.replace(/ /ig, " ");
         }

One simple function. Should be easy. How to add it? Adding it as a text string doesn't work. So how?

Then that will be a good general piece of documentation on how to add functions to Drupal wysiwyg tinymce configuration.

edit the original question was almost as simple actually. It's just, how to apply this:

// Adds an observer to the onDblClick event using tinyMCE.init
tinyMCE.init({
   ...
   setup : function(ed) {
      ed.onDblClick.add(function(ed, e) {
         // Some custom JS logic here
      });
   }
});
TwoD’s picture

Ah, that makes it clearer, thanks.
Functions or function references can't be passed this way because everything gets passed to the client as a JSON object, which only handles basic data types.
However, TinyMCE also accepts the names of callback functions/methods as strings instead of directly specifying a function, so it is possible to get around this issue. The callback must be globally accessible so it can't be hidden inside closure, but it's possible to specify methods on global objects by typing out their full name: 'MyGlobalObject.SubArray.myCustomSetupCallback'.

First, put your callbacks in their own file:
sites/default/modules/MYMODULE/MYMODULE_tinymce_callbacks.js

function MYMODULE_tinymce_setup_callback(ed) {
  ed.onDblClick.add(function (ed, e) {
    // Some custom JS logic here.
  });
}

function MYMODULE_tinymce_paste_preprocess_callback(pl, o) {
  // Remove  
  o.node.innerHTML = o.node.innerHTML.replace(/ /ig, " ");
}

Then modify the settings alter hook to add that file and put the callback names in the settings.

/**
 * Implements hook_wysiwyg_editor_settings_alter().
 */
function MYMODULE_wysiwyg_editor_settings_alter(&$settings, $context) {
  if ($context['profile']->editor == 'tinymce') {
    drupal_add_js(drupal_get_path('module', 'MYMODULE') . '/MYMODULE_tinymce_callbacks.js');
    $settings['setup'] = 'MYMODULE_tinymce_setup_callback';
    $settings['paste_preprocess'] = 'MYMODULE_tinymce_paste_preprocess_callback';
  }
}

Note: I'm not sure exactly why you'd want to remove all   entities, they can be quite useful. Also, when this is done using .innerHTML, it can have some bad side-effects in browsers, especially IE. The browser doesn't care about white-spaces used for source formatting ("pretty printing") or whether what is read from there gets output using a certain coding style at all. For example, depending on the DocType used in the editing area, all your tags may end up in ALL CAPS and your self-closing tags may be missing the slash. I can't tell for sure if this will happen when .innerHTML is used in this particular callback, but our Teaser Break plugin suffers from this issue since it's being executed after the contents have been fetched from the editor. Just so you know in case your markup starts looking odd.

TwoD’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

fix link

will_leamon’s picture

Issue summary: View changes

Can you help me find out why the following code doesn't work? I'm trying to disable the disability warning in TinyMCE and turn the browser spellcheck back on:

<?php
/**
 * Implements hook_wysiwyg_editor_settings_alter().
 * $settings['some_setting'] = "Some string value";
 * $settings['yet_another_setting'] = array('string1', 5, 'string 2');
 * $settings['some_other_setting'] = TRUE;
 */
function MYMODULE_wysiwyg_editor_settings_alter(&$settings, $context) {
  if ($context['profile']->editor == 'tinymce') {
    $settings['accessibility_warnings'] = FALSE;
    $settings['browser_spellcheck'] = TRUE;
    // etc...
  }
}
?>

ACK my bad - forgot to change...

function MYMODULE_wysiwyg_editor_settings_alter(&$settings, $context) {

...to the actual name of the module. Dunce cap accepted.

TwoD’s picture

:)

rakshithtb’s picture

Very Helpfull. Thank you very much :)

myphi’s picture

I tried the function MYMODULE_tinymce_paste_preprocess_callback.

When I try to paste something nothing happens and I get the error message:

Error: TypeError: o.node is undefined
Source: http://...MYMODULE_tinymce_callbacks.js?nixmhy
Line: 9

What can I do?

Ali yah’s picture

Thank you so much :)