I need to have access to the jqconsole variable outside of the anonymously bound function. How do i instantiate it outside of the JQuery habitat? If possible, i would just save it globally or to my state. I did not manage to get it to run this way.
componentDidMount = () => {
// Execute after fully loaded
$(window).bind('load', () => {
const jqconsole = $('#console').jqconsole(
'Welcome to the console!\n',
'>'
);
// register some workarounds
jqconsole.RegisterMatching('{', '}', 'brace');
jqconsole.RegisterMatching('(', ')', 'paran');
jqconsole.RegisterMatching('[', ']', 'bracket');
const startPrompt = () => {
// scroll to bottom -- NOT WORKING YET
$('#console').scrollTop($('#console')[0].scrollHeight);
// Start the prompt with history enabled.
jqconsole.Prompt(true, input => {
let transformedString;
// Manage Output
try {
transformedString = window.eval(input);
if (typeof transformedString === 'object') {
this.writeToConsole(jqconsole, 'object', transformedString);
} else {
this.writeToConsole(jqconsole, 'message', transformedString);
}
// Restart the input prompt.
startPrompt();
} catch (error) {
this.writeToConsole(jqconsole, 'error', error);
// Restart the input prompt.
startPrompt();
}
});
};
// Restart the input prompt.
startPrompt();
});
};
To be precise: I have this class method writeToConsole
, and want to call it without having to deliver the instance of jqconsole:
// Manage Output
writeToConsole = (jqconsole, type, message) => {
console.log('writing to console @ console');
if (type === 'error') {
jqconsole.Write(message + '\n', 'jqconsole-output-error');
} else if (type === 'message') {
jqconsole.Write(message + '\n', 'jqconsole-output');
} else if (type === 'object') {
jqconsole.Write(JSON.stringify(message) + '\n', 'jqconsole-output');
} else {
console.log('error @ writeToConsole');
return null;
}
};
Thank you for your time and help.