Skip to content

Commit

Permalink
use less javascript for this non-js-only implementation of the clear …
Browse files Browse the repository at this point in the history
…prompt button.
  • Loading branch information
AUTOMATIC1111 committed Dec 10, 2022
1 parent 2028aa0 commit d065922
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 49 deletions.
41 changes: 5 additions & 36 deletions javascript/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,44 +131,13 @@ function ask_for_style_name(_, prompt_text, negative_prompt_text) {
return [name_, prompt_text, negative_prompt_text]
}

// returns css id for currently selected tab in ui
function selected_tab_id() {
tabs = gradioApp().querySelectorAll('#tabs div.tabitem')

for(var tab = 0; tab < tabs.length; tab++) {
if (tabs[tab].style.display != "none") return tabs[tab].id

}

}

function clear_prompt() {

if(confirm("Delete prompt?")) {

let pos_prompt = gradioApp().querySelector("#txt2img_prompt > label > textarea");
let neg_prompt = gradioApp().querySelector("#txt2img_neg_prompt > label > textarea");

if (selected_tab_id() == "tab_txt2img") {
} else {
pos_prompt = gradioApp().querySelector("#img2img_prompt > label > textarea");
neg_prompt = gradioApp().querySelector("#img2img_neg_prompt > label > textarea");
function confirm_clear_prompt(prompt, negative_prompt) {
if(confirm("Delete prompt?")) {
prompt = ""
negative_prompt = ""
}

pos_prompt.value = ""
neg_prompt.value = ""

//update prompt values on server-side
pos_prompt.dispatchEvent(
new Event("input", {bubbles: true})
)
neg_prompt.dispatchEvent(
new Event("input", {bubbles: true})
)

return true
} else return false

return [prompt, negative_prompt]
}


Expand Down
1 change: 0 additions & 1 deletion modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ def list_samplers():
"js_modal_lightbox": OptionInfo(True, "Enable full page image viewer"),
"js_modal_lightbox_initially_zoomed": OptionInfo(True, "Show images zoomed in by default in full page image viewer"),
"show_progress_in_title": OptionInfo(True, "Show generation progress in window title."),
"clear_prompt_visible": OptionInfo(True, "Show clear prompt button"),
'quicksettings': OptionInfo("sd_model_checkpoint", "Quicksettings list"),
'localization': OptionInfo("None", "Localization (requires restart)", gr.Dropdown, lambda: {"choices": ["None"] + list(localization.localizations.keys())}, refresh=lambda: localization.list_localizations(cmd_opts.localizations_dir)),
}))
Expand Down
23 changes: 11 additions & 12 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,17 @@ def create_toprow(is_img2img):
paste = gr.Button(value=paste_symbol, elem_id="paste")
save_style = gr.Button(value=save_style_symbol, elem_id="style_create")
prompt_style_apply = gr.Button(value=apply_style_symbol, elem_id="style_apply")

clear_prompt_button = gr.Button(
value=clear_prompt_symbol,
elem_id="clear_prompt",
visible=opts.clear_prompt_visible
)

clear_prompt_button = gr.Button(value=clear_prompt_symbol, elem_id=f"{id_part}_clear_prompt")
token_counter = gr.HTML(value="<span></span>", elem_id=f"{id_part}_token_counter")
token_button = gr.Button(visible=False, elem_id=f"{id_part}_token_button")

clear_prompt_button.click(
fn=lambda *x: x,
_js="confirm_clear_prompt",
inputs=[prompt, negative_prompt],
outputs=[prompt, negative_prompt],
)

button_interrogate = None
button_deepbooru = None
if is_img2img:
Expand Down Expand Up @@ -447,7 +448,7 @@ def create_toprow(is_img2img):
prompt_style2 = gr.Dropdown(label="Style 2", elem_id=f"{id_part}_style2_index", choices=[k for k, v in shared.prompt_styles.styles.items()], value=next(iter(shared.prompt_styles.styles.keys())))
prompt_style2.save_to_config = True

return prompt, roll, prompt_style, negative_prompt, prompt_style2, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, token_counter, token_button, clear_prompt_button
return prompt, roll, prompt_style, negative_prompt, prompt_style2, submit, button_interrogate, button_deepbooru, prompt_style_apply, save_style, paste, token_counter, token_button


def setup_progressbar(progressbar, preview, id_part, textinfo=None):
Expand Down Expand Up @@ -634,7 +635,7 @@ def create_ui():
modules.scripts.scripts_txt2img.initialize_scripts(is_img2img=False)

with gr.Blocks(analytics_enabled=False) as txt2img_interface:
txt2img_prompt, roll, txt2img_prompt_style, txt2img_negative_prompt, txt2img_prompt_style2, submit, _, _,txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, token_counter, token_button, clear_prompt_button = create_toprow(is_img2img=False)
txt2img_prompt, roll, txt2img_prompt_style, txt2img_negative_prompt, txt2img_prompt_style2, submit, _, _,txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, token_counter, token_button = create_toprow(is_img2img=False)

dummy_component = gr.Label(visible=False)
txt_prompt_img = gr.File(label="", elem_id="txt2img_prompt_image", file_count="single", type="bytes", visible=False)
Expand Down Expand Up @@ -686,7 +687,6 @@ def create_ui():

connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
connect_clear_prompt(clear_prompt_button)

txt2img_args = dict(
fn=wrap_gradio_gpu_call(modules.txt2img.txt2img),
Expand Down Expand Up @@ -793,7 +793,7 @@ def create_ui():
modules.scripts.scripts_img2img.initialize_scripts(is_img2img=True)

with gr.Blocks(analytics_enabled=False) as img2img_interface:
img2img_prompt, roll, img2img_prompt_style, img2img_negative_prompt, img2img_prompt_style2, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste,token_counter, token_button, clear_prompt_button = create_toprow(is_img2img=True)
img2img_prompt, roll, img2img_prompt_style, img2img_negative_prompt, img2img_prompt_style2, submit, img2img_interrogate, img2img_deepbooru, img2img_prompt_style_apply, img2img_save_style, img2img_paste,token_counter, token_button = create_toprow(is_img2img=True)


with gr.Row(elem_id='img2img_progress_row'):
Expand Down Expand Up @@ -884,7 +884,6 @@ def update_orig(image, state):

connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
connect_clear_prompt(clear_prompt_button)

img2img_prompt_img.change(
fn=modules.images.image_data,
Expand Down

0 comments on commit d065922

Please sign in to comment.