Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets/jseditor.js: Linting and Prettify #2812

Merged
merged 5 commits into from
Feb 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions js/widgets/jseditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* You should have received a copy of the GNU Affero General Public License along with this
* library; if not, write to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
* MA 02110-1335 USA.
*/
*/

/**
* @class
Expand All @@ -21,6 +21,11 @@
*
* Private members' names begin with underscore '_".
*/

/* global docById, MusicBlocks, hljs, CodeJar, JSGenerate, JS_API */

/* exported JSEditor */

class JSEditor {
/**
* @constructor
Expand All @@ -29,8 +34,11 @@ class JSEditor {
this.isOpen = true;
this._showingHelp = false;

this.widgetWindow =
window.widgetWindows.windowFor(this, "JavaScript Editor", "JavaScript Editor");
this.widgetWindow = window.widgetWindows.windowFor(
this,
"JavaScript Editor",
"JavaScript Editor"
);
this.widgetWindow.clear();
this.widgetWindow.show();
this.widgetWindow.setPosition(160, 132);
Expand All @@ -47,12 +55,7 @@ class JSEditor {

// setup editor window styles
this._currentStyle = 0;
this._styles = [
"dracula",
"github",
"railscasts",
"vs",
].map((name) => {
this._styles = ["dracula", "github", "railscasts", "vs"].map((name) => {
const link = document.createElement("link");
link.href = `././lib/codejar/styles/${name}.min.css`;
link.rel = "stylesheet";
Expand All @@ -75,10 +78,9 @@ class JSEditor {
this.widgetWindow.onmaximize = () => {
const editor = this.widgetWindow.getWidgetBody().childNodes[0];
editor.style.width = this.widgetWindow._maximized ? "100%" : "39rem";
editor.style.height =
this.widgetWindow._maximized ?
`calc(100vh - ${64 + 33}px` :
`${docById("overlayCanvas").height - 33 - 128 - 12}px`;
editor.style.height = this.widgetWindow._maximized
? `calc(100vh - ${64 + 33}px`
: `${docById("overlayCanvas").height - 33 - 128 - 12}px`;
};

this._editor.style.width = "39rem";
Expand Down Expand Up @@ -249,7 +251,7 @@ class JSEditor {
this._editor.appendChild(editorconsole);

const highlight = (editor) => {
editor.textContent = editor.textContent;
// editor.textContent = editor.textContent;
hljs.highlightBlock(editor);
};

Expand All @@ -261,13 +263,12 @@ class JSEditor {
this._jar.updateCode(this._code);
this._jar.updateOptions({
tab: " ".repeat(4), // default is '\t'
indentOn: /[(\[]$/, // default is /{$/
spellcheck: false, // default is false
addClosing: true // default is true
indentOn: /[(]$/, // default is /{$/
spellcheck: false, // default is false
addClosing: true // default is true
});
this._jar.onUpdate(code => {
if (!this._showingHelp)
this._code = code;
this._jar.onUpdate((code) => {
if (!this._showingHelp) this._code = code;
this._setLinesCount(this._code);
});

Expand All @@ -285,15 +286,15 @@ class JSEditor {
* @returns {void}
*/
static logConsole(message, color) {
if (color === undefined)
color = "midnightblue";
if (color === undefined) color = "midnightblue";
if (docById("editorConsole")) {
if (docById("editorConsole").innerHTML !== "")
docById("editorConsole").innerHTML += "</br>";
docById("editorConsole").innerHTML += `<span style="color: ${color}">${message}</span>`;
} else {
console.error("EDITOR MISSING!");
// console.error("EDITOR MISSING!");
}
// eslint-disable-next-line
console.log("%c" + message, `color: ${color}`);
}

Expand All @@ -304,13 +305,9 @@ class JSEditor {
* @returns {void}
*/
_runCode() {
if (this._showingHelp)
return;

if (docById("editorConsole"))
docById("editorConsole").innerHTML = "";
if (this._showingHelp) return;

console.debug("Run JavaScript");
if (docById("editorConsole")) docById("editorConsole").innerHTML = "";

try {
MusicBlocks.init(true);
Expand All @@ -327,8 +324,6 @@ class JSEditor {
* @returns {void}
*/
_generateCode() {
console.debug("Generate JavaScript");

JSGenerate.run(true);
this._code = JSGenerate.code;
this._jar.updateCode(this._code);
Expand All @@ -342,8 +337,7 @@ class JSEditor {
* @returns {void}
*/
_setLinesCount(code) {
if (!docById("editorLines"))
return;
if (!docById("editorLines")) return;

const linesCount = code.replace(/\n+$/, "\n").split("\n").length + 1;
let text = "";
Expand All @@ -364,13 +358,11 @@ class JSEditor {
const helpBtn = docById("js_editor_help_btn");

if (this._showingHelp) {
console.debug("Showing Help");
helpBtn.style.color = "gold";
this._codeBck = this._code;
this._jar.updateCode(JS_API);
this._setLinesCount(JS_API);
} else {
console.debug("Hiding Help");
helpBtn.style.color = "white";
this._jar.updateCode(this._codeBck);
this._setLinesCount(this._codeBck);
Expand Down