This post is a "translation" of this one in the french forum. May it help to get a quicker response (if I don't make too many mistakes )
I'm trying to improve usability and user-friendliness of the WYMeditor.
In the editor's toolbar, we have a "Paste (from Word)" tool to import text and keep it a minimum formated. But users don't use this tool. Instead they use the OS built-in copy-paste function they are regular with. So I decide to catch all paste command hover the editor and call automatically the paste dialog. For this, I wrote a small plugin based on jquery-hotkeys.js (I know, it's a bit heavy for this purpose but I think I'll use it in other places).
- Code: Select all
WYMeditor.editor.prototype.catchPaste = function(options) {
var wym = this;
var doc = this._doc;
catchPaste = function(e) {
e.stopPropagation();
e.preventDefault();
wym.exec(WYMeditor.PASTE);
}
$(doc).bind('keydown', 'Ctrl+V', catchPaste );
$(doc).bind('keydown', 'Meta+V', catchPaste );
$(doc).bind('keydown', 'Shift+Insert', catchPaste );
$(doc).bind('paste', catchPaste );
};
Note 1 : Meta+V is the keyboard shortcut for paste in Mac OS. WYMeditor offers some shortcut for bold and italic, but only with the Control modifier. It won't be complicated to make them work on Mac OS by switching all the "if(evt.ctrlKey)" tests into "if(evt.ctrlKey || evt.metaKey)".
Note 2 : "Paste" event support depends on the client browser, so it's possible that some users won't see the paste dialog automatically and fallback on the default behaviour.
My problem is that the paste dialog call the WYMeditor.editor.paste() method, which automatically insert content after the current bloc, without considering the position of the cursor in it. So I decide to change the paste() function to make it more "clever". For the moment, it insert content as a classic copy/paste will when it contains no new line character and behave like the normal paste() method when dealing with multiline content.
- Code: Select all
// Backup the original paste method
WYMeditor.editor.prototype._paste = WYMeditor.editor.prototype.paste;
WYMeditor.editor.prototype.paste = function(sData) {
var rExp = new RegExp(this._newLine, "g");
if(sData.search(rExp) != -1) {
/** @todo: split current bloc at cursor position and select the first part **/
this._paste(sData);
} else {
this.insert(sData);
}
}
Now I would like to split the current container at the cursor position and paste content after the first part when content is multiline. But as far as I search in API documentation and in WYMeditor source code, I don't find a way to reproduce programmatically what append when the Enter key is pressed.
Is there any function to do this ?
Thanks.