From 208f066e0e83063dea749260cfd6c7582257139e Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 12:42:01 +0300 Subject: [PATCH 1/6] eslintrc: Sort eslint rules --- .eslintrc.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 9e7ab34d..4d4a2424 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,9 +25,14 @@ module.exports = { "linebreak-style": ["error", "unix"], "no-extra-semi": "error", "no-mixed-spaces-and-tabs": "error", + "no-multi-spaces": "error", + "no-redeclare": "off", "no-trailing-spaces": "error", + "no-unused-vars": "off", "no-whitespace-before-property": "error", "object-curly-newline": ["error", {consistent: true, multiline: true}], + "object-curly-spacing": ["error", "never"], + "operator-linebreak": ["error", "after"], "quote-props": ["error", "consistent-as-needed"], "semi": ["error", "always"], "semi-spacing": "error", @@ -40,11 +45,6 @@ module.exports = { "switch-colon-spacing": "error", "template-curly-spacing": ["error", "never"], "unicode-bom": "error", - "no-multi-spaces": "error", - "object-curly-spacing": ["error", "never"], - "operator-linebreak": ["error", "after"], - "no-unused-vars": "off", - "no-redeclare": "off", }, globals: { // this file From 958d68fb146f84e7df8fca115a35847f862a2e9c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 12:46:33 +0300 Subject: [PATCH 2/6] eslintrc: Use a file-local `global` comment for module --- .eslintrc.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 4d4a2424..e33a22c3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,3 +1,4 @@ +/* global module */ module.exports = { env: { browser: true, @@ -47,8 +48,6 @@ module.exports = { "unicode-bom": "error", }, globals: { - // this file - module: "writable", //script.js gradioApp: "writable", onUiLoaded: "writable", From 247f371d3eaa9807d1f5986937cd10bf467ef9ca Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 12:50:47 +0300 Subject: [PATCH 3/6] eslintrc: mark most globals read-only --- .eslintrc.js | 58 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e33a22c3..1591795b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -49,41 +49,41 @@ module.exports = { }, globals: { //script.js - gradioApp: "writable", - onUiLoaded: "writable", - onUiUpdate: "writable", - onOptionsChanged: "writable", + gradioApp: "readonly", + onUiLoaded: "readonly", + onUiUpdate: "readonly", + onOptionsChanged: "readonly", uiCurrentTab: "writable", - uiElementIsVisible: "writable", - uiElementInSight: "writable", - executeCallbacks: "writable", + uiElementIsVisible: "readonly", + uiElementInSight: "readonly", + executeCallbacks: "readonly", //ui.js opts: "writable", - all_gallery_buttons: "writable", - selected_gallery_button: "writable", - selected_gallery_index: "writable", - args_to_array: "writable", - switch_to_txt2img: "writable", - switch_to_img2img_tab: "writable", - switch_to_img2img: "writable", - switch_to_sketch: "writable", - switch_to_inpaint: "writable", - switch_to_inpaint_sketch: "writable", - switch_to_extras: "writable", - get_tab_index: "writable", - create_submit_args: "writable", - restart_reload: "writable", - updateInput: "writable", + all_gallery_buttons: "readonly", + selected_gallery_button: "readonly", + selected_gallery_index: "readonly", + args_to_array: "readonly", + switch_to_txt2img: "readonly", + switch_to_img2img_tab: "readonly", + switch_to_img2img: "readonly", + switch_to_sketch: "readonly", + switch_to_inpaint: "readonly", + switch_to_inpaint_sketch: "readonly", + switch_to_extras: "readonly", + get_tab_index: "readonly", + create_submit_args: "readonly", + restart_reload: "readonly", + updateInput: "readonly", //extraNetworks.js - requestGet: "writable", - popup: "writable", + requestGet: "readonly", + popup: "readonly", // from python - localization: "writable", + localization: "readonly", // progrssbar.js - randomId: "writable", - requestProgress: "writable", + randomId: "readonly", + requestProgress: "readonly", // imageviewer.js - modalPrevImage: "writable", - modalNextImage: "writable", + modalPrevImage: "readonly", + modalNextImage: "readonly", } }; From 3909c2b2a08eff2334020b7c8484569f8891a80c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 12:55:06 +0300 Subject: [PATCH 4/6] eslintrc: enable no-redeclare but with builtinGlobals: false --- .eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 1591795b..fe9978fa 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -27,7 +27,7 @@ module.exports = { "no-extra-semi": "error", "no-mixed-spaces-and-tabs": "error", "no-multi-spaces": "error", - "no-redeclare": "off", + "no-redeclare": ["error", {builtinGlobals: false}], "no-trailing-spaces": "error", "no-unused-vars": "off", "no-whitespace-before-property": "error", From 563e88dd91c80856697ec4f879de9a0516ebef5b Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 13:01:36 +0300 Subject: [PATCH 5/6] Replace args_to_array (and facsimiles) with Array.from --- .eslintrc.js | 1 - javascript/textualInversion.js | 2 +- javascript/ui.js | 38 +++++++++++----------------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index fe9978fa..944cc869 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -62,7 +62,6 @@ module.exports = { all_gallery_buttons: "readonly", selected_gallery_button: "readonly", selected_gallery_index: "readonly", - args_to_array: "readonly", switch_to_txt2img: "readonly", switch_to_img2img_tab: "readonly", switch_to_img2img: "readonly", diff --git a/javascript/textualInversion.js b/javascript/textualInversion.js index 37e3d075..20443fcc 100644 --- a/javascript/textualInversion.js +++ b/javascript/textualInversion.js @@ -9,7 +9,7 @@ function start_training_textual_inversion() { gradioApp().getElementById('ti_progress').innerHTML = progress.textinfo; }); - var res = args_to_array(arguments); + var res = Array.from(arguments); res[0] = id; diff --git a/javascript/ui.js b/javascript/ui.js index c7316ddb..f13e7b6c 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -61,18 +61,12 @@ function extract_image_from_gallery(gallery) { return [gallery[index]]; } -function args_to_array(args) { - var res = []; - for (var i = 0; i < args.length; i++) { - res.push(args[i]); - } - return res; -} +window.args_to_array = Array.from; // Compatibility with e.g. extensions that may expect this to be around function switch_to_txt2img() { gradioApp().querySelector('#tabs').querySelectorAll('button')[0].click(); - return args_to_array(arguments); + return Array.from(arguments); } function switch_to_img2img_tab(no) { @@ -81,28 +75,28 @@ function switch_to_img2img_tab(no) { } function switch_to_img2img() { switch_to_img2img_tab(0); - return args_to_array(arguments); + return Array.from(arguments); } function switch_to_sketch() { switch_to_img2img_tab(1); - return args_to_array(arguments); + return Array.from(arguments); } function switch_to_inpaint() { switch_to_img2img_tab(2); - return args_to_array(arguments); + return Array.from(arguments); } function switch_to_inpaint_sketch() { switch_to_img2img_tab(3); - return args_to_array(arguments); + return Array.from(arguments); } function switch_to_extras() { gradioApp().querySelector('#tabs').querySelectorAll('button')[2].click(); - return args_to_array(arguments); + return Array.from(arguments); } function get_tab_index(tabId) { @@ -118,28 +112,20 @@ function get_tab_index(tabId) { } function create_tab_index_args(tabId, args) { - var res = []; - for (var i = 0; i < args.length; i++) { - res.push(args[i]); - } - + var res = Array.from(args); res[0] = get_tab_index(tabId); - return res; } function get_img2img_tab_index() { - let res = args_to_array(arguments); + let res = Array.from(arguments); res.splice(-2); res[0] = get_tab_index('mode_img2img'); return res; } function create_submit_args(args) { - var res = []; - for (var i = 0; i < args.length; i++) { - res.push(args[i]); - } + var res = Array.from(args); // As it is currently, txt2img and img2img send back the previous output args (txt2img_gallery, generation_info, html_info) whenever you generate a new image. // This can lead to uploading a huge gallery of previously generated images, which leads to an unnecessary delay between submitting and beginning to generate. @@ -275,13 +261,13 @@ function recalculatePromptTokens(name) { function recalculate_prompts_txt2img() { recalculatePromptTokens('txt2img_prompt'); recalculatePromptTokens('txt2img_neg_prompt'); - return args_to_array(arguments); + return Array.from(arguments); } function recalculate_prompts_img2img() { recalculatePromptTokens('img2img_prompt'); recalculatePromptTokens('img2img_neg_prompt'); - return args_to_array(arguments); + return Array.from(arguments); } From 67d4360453e0316b494a43320e2e5df53a93d5ea Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 19 May 2023 13:06:12 +0300 Subject: [PATCH 6/6] get_tab_index(): use a for loop with early-exit for performance --- javascript/ui.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/javascript/ui.js b/javascript/ui.js index f13e7b6c..648a5290 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -100,15 +100,13 @@ function switch_to_extras() { } function get_tab_index(tabId) { - var res = 0; - - gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button').forEach(function(button, i) { - if (button.className.indexOf('selected') != -1) { - res = i; + let buttons = gradioApp().getElementById(tabId).querySelector('div').querySelectorAll('button'); + for (let i = 0; i < buttons.length; i++) { + if (buttons[i].classList.contains('selected')) { + return i; } - }); - - return res; + } + return 0; } function create_tab_index_args(tabId, args) {