I'd like to write a tinyMCE plugin that allows the nesting of sub and sup tags.
Right now I've written a command that inserts raw HTML 开发者_如何学Python'' at the cursor when you push the button / hit the shortcut keys. However, this will only work if I can then move the cursor to the middle of the new tag.
I can't seem to find any documentation on how to move the cursor. There are some suggested hacks, but they are very hacky. This can't be that hard because, as I understand it, this is how the [b]
and [i]
buttons work too.
Is there a better way to be doing this? How can I write a tinyMCE function that puts the user in "sub mode" or "sup mode" and allows them to nest "sub" and "sup" modes?
Thanks!
This function will set the cursor to the specified html element.
// sets the cursor to the specified element, ed ist the editor instance
// start defines if the cursor is to be set at the start or at the end
setCursor: function (ed, element, start) {
var doc = ed.getDoc();
if (typeof doc.createRange != "undefined") {
var range = doc.createRange();
range.selectNodeContents(element);
range.collapse(start);
var win = doc.defaultView || doc.parentWindow;
var sel = win.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof doc.body.createTextRange != "undefined") {
var textRange = doc.body.createTextRange();
textRange.moveToElementText(element);
textRange.collapse(start);
textRange.select();
}
},
In order to nest sups you need to make sure that sups and subs may be nested. Please check the extended_valid_elements and valid_elements configuration parameter. sub and sup may not be nested by default! You need to overwrite this part of the ruleset.
精彩评论