For debug, watch jquery.wymeditor.js
.
I need to execute commands like "Strong", "Indent", etc. from my custom toolbar (to be precise, ribbon) via JavaScript and without errors. 80% are done, I can execute commands, but have a strange error.
So, what I do, when document is ready (with contained textarea "#doc"):
myDoc = $("#doc").wymeditor()[0];
myDoc_wym = $.getWymeditorByTextarea(myDoc);
setTimeout(function() {
console.log(myDoc_wym._iframe.contentWindow); // contentWindow returns proper Window object of editor IFrame
$(myDoc_wym._doc).keydown(function(e) {
if (e.which == 112) {
console.log(myDoc_wym); // must return extended jQuery editor object
myDoc_wym.__proto__._exec("Strong"); // BREAKPOINT
console.log("It might be done!"); // no luck today
return false;
}
});
}, 1000); // to be sure, that myDoc_wym._doc exists, I just enforced timeout
- WYMeditor on half way to be fully prepared.
- Get its extended
editor
object withgetWymeditorByTextarea
. - Wait a second.
- Log
Window
object of editor's IFrame. When we press
F1
:- Log
myDoc_wym
. myDoc_wym.__proto__._exec("Strong");
- execute prototyped function_exec
with command"Strong"
...
- Log
Here goes a crash of keydown
. What I get in Chromium (above last released NW.JS) console:
Error is caused in prototyped function hasSelection
. So, it's saying that _iframe
is undefined
in that code:
WYMeditor.editor.prototype.hasSelection = function () {
var wym = this;
if (
// `isSelectionValid` is undocumented in current Rangy (`1.2.2`).
// It seems to be required because the `rangeCount` in `IE <= 8` seems
// to be misleading.
rangy.isSelectionValid(wym._iframe.contentWindow) !== true
) {
return false;
}
if (wym.selection().rangeCount === 0) {
return false;
}
return true;
};
After this tried to enable my own call for this function by pasting in console input similar code, but with debug needs (console.log(wym);
):
WYMeditor.editor.prototype.hasSelection = function () {
var wym = this;
console.log(wym);
//console.log(wym._iframe);
//console.log(wym._iframe.contentWindow);
if (rangy.isSelectionValid(wym._iframe.contentWindow) !== true) return false;
if (wym.selection().rangeCount === 0) return false;
return true;
};
- First object is what I get before executing WYMeditor command.
- Second has no
_iframe
, so it's returning undefined. - Accordingly, cannot read from undefined.
- It seems like
hasSelection
is called twice (for example,selectedContainer
, that callshasSelection
, may be called 3 times by_exec
). At this time we see_iframe
, but it isn't longer needed.
It turns out that:
- at first time
hasSelection
called,wym
returned as prototyped object, but... - at second time
wym
returned as full "editor object" with needed properties (such as_iframe
).
Something weird going up there and I don't know what. When pressing on default toolbar buttons all works perfectly.