I'm trying to write my own plugin to enable language switching in the content. What it needs to do is that when a user selects a piece of text, it has to be wrapped with a span and a a language tag (eg <span lang="nl">Nederlands</span>.

The wysiwyg.api.js file has some great example code, but the example only shows how to insert a tag:

    var content = '<!--break-->';
    
    // Insert new content into the editor.
    if (typeof content != 'undefined') {
      Drupal.wysiwyg.instances[instanceId].insert(content);
    }

In my use case, it needs to wrap the element around the selected text. How can this be achieved?

Comments

twod’s picture

Wysiwyg's API is currently lacking a cross-editor and cross-browser way to do that, so you'll have to fetch the selection/range on your own. I had to do something similar for our NicEdit integration a while back (wysiwyg/editors/js/nicedit.js) which I based on PPK's Introduction to Ranges.PPK's Introduction to Ranges.

There's a feature request to add selection handling to the API over at #1220802: Selection API: Allow plugins to alter and set a custom selection context for themselves. If you're interested in getting that into Wysiwyg, please join the discussion.