2022-10-31 04:21:39 +01:00
|
|
|
"use strict" // Opt in to a restricted variant of JavaScript
|
|
|
|
|
2022-11-09 10:51:41 +01:00
|
|
|
const EXT_REGEX = /(?:\.([^.]+))?$/
|
2023-04-27 19:56:56 +02:00
|
|
|
const TEXT_EXTENSIONS = ["txt", "json"]
|
|
|
|
const IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "bmp", "tiff", "tif", "tga", "webp"]
|
2022-11-09 10:51:41 +01:00
|
|
|
|
2022-11-04 01:34:12 +01:00
|
|
|
function parseBoolean(stringValue) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof stringValue === "boolean") {
|
2022-11-04 01:34:12 +01:00
|
|
|
return stringValue
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof stringValue === "number") {
|
2022-11-04 01:34:12 +01:00
|
|
|
return stringValue !== 0
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (typeof stringValue !== "string") {
|
2022-11-04 01:34:12 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
switch (stringValue?.toLowerCase()?.trim()) {
|
2022-11-04 01:34:12 +01:00
|
|
|
case "true":
|
|
|
|
case "yes":
|
2022-11-05 18:33:38 +01:00
|
|
|
case "on":
|
2022-11-04 01:34:12 +01:00
|
|
|
case "1":
|
2023-04-27 19:56:56 +02:00
|
|
|
return true
|
2022-11-04 01:34:12 +01:00
|
|
|
|
|
|
|
case "false":
|
|
|
|
case "no":
|
2022-11-05 18:33:38 +01:00
|
|
|
case "off":
|
2022-11-04 01:34:12 +01:00
|
|
|
case "0":
|
2022-12-24 15:11:36 +01:00
|
|
|
case "none":
|
2022-11-04 01:34:12 +01:00
|
|
|
case null:
|
|
|
|
case undefined:
|
2023-04-27 19:56:56 +02:00
|
|
|
return false
|
2022-11-04 01:34:12 +01:00
|
|
|
}
|
|
|
|
try {
|
2023-04-27 19:56:56 +02:00
|
|
|
return Boolean(JSON.parse(stringValue))
|
2022-11-04 01:34:12 +01:00
|
|
|
} catch {
|
|
|
|
return Boolean(stringValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 01:05:32 +02:00
|
|
|
// keep in sync with `ui/easydiffusion/utils/save_utils.py`
|
2022-10-31 04:21:39 +01:00
|
|
|
const TASK_MAPPING = {
|
2023-04-27 19:56:56 +02:00
|
|
|
prompt: {
|
|
|
|
name: "Prompt",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (prompt) => {
|
|
|
|
promptField.value = prompt
|
|
|
|
},
|
|
|
|
readUI: () => promptField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
negative_prompt: {
|
|
|
|
name: "Negative Prompt",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (negative_prompt) => {
|
|
|
|
negativePromptField.value = negative_prompt
|
|
|
|
},
|
|
|
|
readUI: () => negativePromptField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
active_tags: {
|
|
|
|
name: "Image Modifiers",
|
2022-11-22 09:06:43 +01:00
|
|
|
setUI: (active_tags) => {
|
|
|
|
refreshModifiersState(active_tags)
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
readUI: () => activeTags.map((x) => x.name),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-11-22 09:06:43 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
inactive_tags: {
|
|
|
|
name: "Inactive Image Modifiers",
|
2022-12-28 22:41:36 +01:00
|
|
|
setUI: (inactive_tags) => {
|
|
|
|
refreshInactiveTags(inactive_tags)
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
readUI: () => activeTags.filter((tag) => tag.inactive === true).map((x) => x.name),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-12-28 22:41:36 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
width: {
|
|
|
|
name: "Width",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (width) => {
|
2022-10-31 04:35:42 +01:00
|
|
|
const oldVal = widthField.value
|
2022-10-31 04:21:39 +01:00
|
|
|
widthField.value = width
|
2022-10-31 04:35:42 +01:00
|
|
|
if (!widthField.value) {
|
|
|
|
widthField.value = oldVal
|
|
|
|
}
|
2023-05-25 13:36:45 +02:00
|
|
|
widthField.dispatchEvent(new Event("change"))
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => parseInt(widthField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseInt(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
height: {
|
|
|
|
name: "Height",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (height) => {
|
2022-10-31 04:35:42 +01:00
|
|
|
const oldVal = heightField.value
|
2022-10-31 04:21:39 +01:00
|
|
|
heightField.value = height
|
2022-10-31 04:35:42 +01:00
|
|
|
if (!heightField.value) {
|
|
|
|
heightField.value = oldVal
|
|
|
|
}
|
2023-05-25 13:36:45 +02:00
|
|
|
heightField.dispatchEvent(new Event("change"))
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => parseInt(heightField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseInt(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
seed: {
|
|
|
|
name: "Seed",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (seed) => {
|
|
|
|
if (!seed) {
|
|
|
|
randomSeedField.checked = true
|
2022-11-03 08:03:50 +01:00
|
|
|
seedField.disabled = true
|
2022-11-30 04:04:42 +01:00
|
|
|
seedField.value = 0
|
2022-10-31 04:21:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
randomSeedField.checked = false
|
2023-04-27 19:56:56 +02:00
|
|
|
randomSeedField.dispatchEvent(new Event("change")) // let plugins know that the state of the random seed toggle changed
|
2022-11-03 08:03:50 +01:00
|
|
|
seedField.disabled = false
|
2022-10-31 04:21:39 +01:00
|
|
|
seedField.value = seed
|
|
|
|
},
|
2022-11-30 04:04:42 +01:00
|
|
|
readUI: () => parseInt(seedField.value), // just return the value the user is seeing in the UI
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseInt(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
num_inference_steps: {
|
|
|
|
name: "Steps",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (num_inference_steps) => {
|
|
|
|
numInferenceStepsField.value = num_inference_steps
|
|
|
|
},
|
|
|
|
readUI: () => parseInt(numInferenceStepsField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseInt(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
guidance_scale: {
|
|
|
|
name: "Guidance Scale",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (guidance_scale) => {
|
|
|
|
guidanceScaleField.value = guidance_scale
|
2022-11-02 15:53:48 +01:00
|
|
|
updateGuidanceScaleSlider()
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => parseFloat(guidanceScaleField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseFloat(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
prompt_strength: {
|
|
|
|
name: "Prompt Strength",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (prompt_strength) => {
|
|
|
|
promptStrengthField.value = prompt_strength
|
2022-11-02 15:53:48 +01:00
|
|
|
updatePromptStrengthSlider()
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => parseFloat(promptStrengthField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseFloat(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
init_image: {
|
|
|
|
name: "Initial Image",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (init_image) => {
|
|
|
|
initImagePreview.src = init_image
|
|
|
|
},
|
|
|
|
readUI: () => initImagePreview.src,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
mask: {
|
|
|
|
name: "Mask",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (mask) => {
|
2023-04-27 19:56:56 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
// add a delay to insure this happens AFTER the main image loads (which reloads the inpainter)
|
2022-12-06 09:26:51 +01:00
|
|
|
imageInpainter.setImg(mask)
|
|
|
|
}, 250)
|
2022-10-31 04:21:39 +01:00
|
|
|
maskSetting.checked = Boolean(mask)
|
|
|
|
},
|
2022-12-06 09:26:51 +01:00
|
|
|
readUI: () => (maskSetting.checked ? imageInpainter.getImg() : undefined),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
preserve_init_image_color_profile: {
|
|
|
|
name: "Preserve Color Profile",
|
2022-12-29 01:43:35 +01:00
|
|
|
setUI: (preserve_init_image_color_profile) => {
|
|
|
|
applyColorCorrectionField.checked = parseBoolean(preserve_init_image_color_profile)
|
|
|
|
},
|
|
|
|
readUI: () => applyColorCorrectionField.checked,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseBoolean(val),
|
2022-12-29 01:43:35 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
|
|
|
|
use_face_correction: {
|
|
|
|
name: "Use Face Correction",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (use_face_correction) => {
|
2023-01-19 20:49:54 +01:00
|
|
|
const oldVal = gfpganModelField.value
|
2023-05-28 01:43:58 +02:00
|
|
|
console.log("use face correction", use_face_correction)
|
|
|
|
if (use_face_correction == null || use_face_correction == "None") {
|
2023-01-19 20:49:54 +01:00
|
|
|
gfpganModelField.disabled = true
|
|
|
|
useFaceCorrectionField.checked = false
|
2023-05-28 01:43:58 +02:00
|
|
|
} else {
|
|
|
|
gfpganModelField.value = getModelPath(use_face_correction, [".pth"])
|
|
|
|
if (gfpganModelField.value) {
|
|
|
|
// Is a valid value for the field.
|
|
|
|
useFaceCorrectionField.checked = true
|
|
|
|
gfpganModelField.disabled = false
|
|
|
|
} else {
|
|
|
|
// Not a valid value, restore the old value and disable the filter.
|
|
|
|
gfpganModelField.disabled = true
|
|
|
|
gfpganModelField.value = oldVal
|
|
|
|
useFaceCorrectionField.checked = false
|
|
|
|
}
|
2023-01-19 20:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//useFaceCorrectionField.checked = parseBoolean(use_face_correction)
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-01-19 20:49:54 +01:00
|
|
|
readUI: () => (useFaceCorrectionField.checked ? gfpganModelField.value : undefined),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
use_upscale: {
|
|
|
|
name: "Use Upscaling",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (use_upscale) => {
|
2022-11-04 01:34:51 +01:00
|
|
|
const oldVal = upscaleModelField.value
|
2023-04-27 19:56:56 +02:00
|
|
|
upscaleModelField.value = getModelPath(use_upscale, [".pth"])
|
|
|
|
if (upscaleModelField.value) {
|
|
|
|
// Is a valid value for the field.
|
2022-11-04 01:34:51 +01:00
|
|
|
useUpscalingField.checked = true
|
2022-11-08 14:16:55 +01:00
|
|
|
upscaleModelField.disabled = false
|
2022-12-27 11:50:16 +01:00
|
|
|
upscaleAmountField.disabled = false
|
2023-04-27 19:56:56 +02:00
|
|
|
} else {
|
|
|
|
// Not a valid value, restore the old value and disable the filter.
|
2022-11-08 14:16:55 +01:00
|
|
|
upscaleModelField.disabled = true
|
2022-12-27 11:50:16 +01:00
|
|
|
upscaleAmountField.disabled = true
|
2022-11-04 01:34:51 +01:00
|
|
|
upscaleModelField.value = oldVal
|
|
|
|
useUpscalingField.checked = false
|
|
|
|
}
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => (useUpscalingField.checked ? upscaleModelField.value : undefined),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
upscale_amount: {
|
|
|
|
name: "Upscale By",
|
2022-12-27 11:50:16 +01:00
|
|
|
setUI: (upscale_amount) => {
|
|
|
|
upscaleAmountField.value = upscale_amount
|
|
|
|
},
|
|
|
|
readUI: () => upscaleAmountField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-12-27 11:50:16 +01:00
|
|
|
},
|
2023-05-27 01:51:30 +02:00
|
|
|
latent_upscaler_steps: {
|
|
|
|
name: "Latent Upscaler Steps",
|
|
|
|
setUI: (latent_upscaler_steps) => {
|
|
|
|
latentUpscalerStepsField.value = latent_upscaler_steps
|
|
|
|
},
|
|
|
|
readUI: () => latentUpscalerStepsField.value,
|
|
|
|
parse: (val) => val,
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
sampler_name: {
|
|
|
|
name: "Sampler",
|
2022-12-14 05:51:44 +01:00
|
|
|
setUI: (sampler_name) => {
|
|
|
|
samplerField.value = sampler_name
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => samplerField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
use_stable_diffusion_model: {
|
|
|
|
name: "Stable Diffusion model",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (use_stable_diffusion_model) => {
|
2022-10-31 04:35:42 +01:00
|
|
|
const oldVal = stableDiffusionModelField.value
|
2022-10-31 04:41:26 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
use_stable_diffusion_model = getModelPath(use_stable_diffusion_model, [".ckpt", ".safetensors"])
|
2022-10-31 04:21:39 +01:00
|
|
|
stableDiffusionModelField.value = use_stable_diffusion_model
|
2022-10-31 04:41:26 +01:00
|
|
|
|
2022-10-31 04:35:42 +01:00
|
|
|
if (!stableDiffusionModelField.value) {
|
|
|
|
stableDiffusionModelField.value = oldVal
|
|
|
|
}
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => stableDiffusionModelField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-05-18 13:55:45 +02:00
|
|
|
clip_skip: {
|
|
|
|
name: "Clip Skip",
|
|
|
|
setUI: (value) => {
|
|
|
|
clip_skip.checked = value
|
|
|
|
},
|
|
|
|
readUI: () => clip_skip.checked,
|
|
|
|
parse: (val) => Boolean(val),
|
|
|
|
},
|
2023-05-25 00:16:14 +02:00
|
|
|
tiling: {
|
|
|
|
name: "Tiling",
|
|
|
|
setUI: (val) => {
|
|
|
|
tilingField.value = val
|
|
|
|
},
|
|
|
|
readUI: () => tilingField.value,
|
|
|
|
parse: (val) => val,
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
use_vae_model: {
|
|
|
|
name: "VAE model",
|
2022-11-19 09:56:44 +01:00
|
|
|
setUI: (use_vae_model) => {
|
|
|
|
const oldVal = vaeModelField.value
|
2023-04-27 19:56:56 +02:00
|
|
|
use_vae_model =
|
|
|
|
use_vae_model === undefined || use_vae_model === null || use_vae_model === "None" ? "" : use_vae_model
|
2022-11-19 09:56:44 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
if (use_vae_model !== "") {
|
|
|
|
use_vae_model = getModelPath(use_vae_model, [".vae.pt", ".ckpt"])
|
|
|
|
use_vae_model = use_vae_model !== "" ? use_vae_model : oldVal
|
2022-11-19 09:56:44 +01:00
|
|
|
}
|
|
|
|
vaeModelField.value = use_vae_model
|
|
|
|
},
|
|
|
|
readUI: () => vaeModelField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-11-19 09:56:44 +01:00
|
|
|
},
|
2023-08-18 14:23:16 +02:00
|
|
|
use_controlnet_model: {
|
|
|
|
name: "ControlNet model",
|
|
|
|
setUI: (use_controlnet_model) => {
|
|
|
|
controlnetModelField.value = getModelPath(use_controlnet_model, [".pth", ".safetensors"])
|
|
|
|
},
|
|
|
|
readUI: () => controlnetModelField.value,
|
|
|
|
parse: (val) => val,
|
|
|
|
},
|
|
|
|
control_filter_to_apply: {
|
|
|
|
name: "ControlNet Filter",
|
|
|
|
setUI: (control_filter_to_apply) => {
|
|
|
|
controlImageFilterField.value = control_filter_to_apply
|
|
|
|
},
|
|
|
|
readUI: () => controlImageFilterField.value,
|
|
|
|
parse: (val) => val,
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
use_lora_model: {
|
|
|
|
name: "LoRA model",
|
2023-03-28 12:07:50 +02:00
|
|
|
setUI: (use_lora_model) => {
|
2023-08-18 10:46:23 +02:00
|
|
|
let modelPaths = []
|
2023-08-18 13:48:01 +02:00
|
|
|
use_lora_model = Array.isArray(use_lora_model) ? use_lora_model : [use_lora_model]
|
2023-08-18 10:46:23 +02:00
|
|
|
use_lora_model.forEach((m) => {
|
|
|
|
if (m.includes("models\\lora\\")) {
|
|
|
|
m = m.split("models\\lora\\")[1]
|
|
|
|
} else if (m.includes("models\\\\lora\\\\")) {
|
|
|
|
m = m.split("models\\\\lora\\\\")[1]
|
|
|
|
} else if (m.includes("models/lora/")) {
|
|
|
|
m = m.split("models/lora/")[1]
|
2023-07-15 16:03:49 +02:00
|
|
|
}
|
2023-08-18 10:46:23 +02:00
|
|
|
m = m.replaceAll("\\\\", "/")
|
|
|
|
m = getModelPath(m, [".ckpt", ".safetensors"])
|
|
|
|
modelPaths.push(m)
|
2023-07-15 16:03:49 +02:00
|
|
|
})
|
2023-08-18 10:46:23 +02:00
|
|
|
loraModelField.modelNames = modelPaths
|
2023-03-28 12:07:50 +02:00
|
|
|
},
|
2023-07-15 16:03:49 +02:00
|
|
|
readUI: () => {
|
2023-08-18 10:46:23 +02:00
|
|
|
return loraModelField.modelNames
|
2023-07-15 16:03:49 +02:00
|
|
|
},
|
|
|
|
parse: (val) => {
|
|
|
|
val = !val || val === "None" ? "" : val
|
2023-08-18 10:46:23 +02:00
|
|
|
if (typeof val === "string" && val.includes(",")) {
|
|
|
|
val = val.split(",")
|
|
|
|
val = val.map((v) => v.trim())
|
|
|
|
val = val.map((v) => v.replaceAll("\\", "\\\\"))
|
|
|
|
val = val.map((v) => v.replaceAll('"', ""))
|
|
|
|
val = val.map((v) => v.replaceAll("'", ""))
|
|
|
|
val = val.map((v) => '"' + v + '"')
|
|
|
|
val = "[" + val + "]"
|
|
|
|
val = JSON.parse(val)
|
|
|
|
}
|
2023-07-15 16:03:49 +02:00
|
|
|
val = Array.isArray(val) ? val : [val]
|
|
|
|
return val
|
|
|
|
},
|
2023-03-28 12:07:50 +02:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
lora_alpha: {
|
|
|
|
name: "LoRA Strength",
|
2023-04-01 17:42:20 +02:00
|
|
|
setUI: (lora_alpha) => {
|
2023-08-18 13:48:01 +02:00
|
|
|
lora_alpha = Array.isArray(lora_alpha) ? lora_alpha : [lora_alpha]
|
2023-08-18 10:46:23 +02:00
|
|
|
loraModelField.modelWeights = lora_alpha
|
2023-07-15 16:03:49 +02:00
|
|
|
},
|
|
|
|
readUI: () => {
|
2023-08-18 10:46:23 +02:00
|
|
|
return loraModelField.modelWeights
|
2023-07-15 16:03:49 +02:00
|
|
|
},
|
|
|
|
parse: (val) => {
|
2023-08-18 10:46:23 +02:00
|
|
|
if (typeof val === "string" && val.includes(",")) {
|
|
|
|
val = "[" + val.replaceAll("'", '"') + "]"
|
|
|
|
val = JSON.parse(val)
|
|
|
|
}
|
2023-07-15 16:03:49 +02:00
|
|
|
val = Array.isArray(val) ? val : [val]
|
|
|
|
val = val.map((e) => parseFloat(e))
|
|
|
|
return val
|
2023-04-01 17:42:20 +02:00
|
|
|
},
|
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
use_hypernetwork_model: {
|
|
|
|
name: "Hypernetwork model",
|
2022-12-12 15:01:59 +01:00
|
|
|
setUI: (use_hypernetwork_model) => {
|
|
|
|
const oldVal = hypernetworkModelField.value
|
2023-04-27 19:56:56 +02:00
|
|
|
use_hypernetwork_model =
|
|
|
|
use_hypernetwork_model === undefined ||
|
|
|
|
use_hypernetwork_model === null ||
|
|
|
|
use_hypernetwork_model === "None"
|
|
|
|
? ""
|
|
|
|
: use_hypernetwork_model
|
|
|
|
|
|
|
|
if (use_hypernetwork_model !== "") {
|
|
|
|
use_hypernetwork_model = getModelPath(use_hypernetwork_model, [".pt"])
|
|
|
|
use_hypernetwork_model = use_hypernetwork_model !== "" ? use_hypernetwork_model : oldVal
|
2022-12-12 15:01:59 +01:00
|
|
|
}
|
|
|
|
hypernetworkModelField.value = use_hypernetwork_model
|
2023-04-27 19:56:56 +02:00
|
|
|
hypernetworkModelField.dispatchEvent(new Event("change"))
|
2022-12-12 15:01:59 +01:00
|
|
|
},
|
|
|
|
readUI: () => hypernetworkModelField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-12-12 15:01:59 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
hypernetwork_strength: {
|
|
|
|
name: "Hypernetwork Strength",
|
2022-12-12 15:01:59 +01:00
|
|
|
setUI: (hypernetwork_strength) => {
|
|
|
|
hypernetworkStrengthField.value = hypernetwork_strength
|
|
|
|
updateHypernetworkStrengthSlider()
|
|
|
|
},
|
|
|
|
readUI: () => parseFloat(hypernetworkStrengthField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => parseFloat(val),
|
2022-12-12 15:01:59 +01:00
|
|
|
},
|
2022-10-31 04:21:39 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
num_outputs: {
|
|
|
|
name: "Parallel Images",
|
2022-11-24 23:15:27 +01:00
|
|
|
setUI: (num_outputs) => {
|
|
|
|
numOutputsParallelField.value = num_outputs
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => parseInt(numOutputsParallelField.value),
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
use_cpu: {
|
|
|
|
name: "Use CPU",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (use_cpu) => {
|
|
|
|
useCPUField.checked = use_cpu
|
|
|
|
},
|
|
|
|
readUI: () => useCPUField.checked,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
stream_image_progress: {
|
|
|
|
name: "Stream Image Progress",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (stream_image_progress) => {
|
2023-04-27 19:56:56 +02:00
|
|
|
streamImageProgressField.checked = parseInt(numOutputsTotalField.value) > 50 ? false : stream_image_progress
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
|
|
|
readUI: () => streamImageProgressField.checked,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => Boolean(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
show_only_filtered_image: {
|
|
|
|
name: "Show only the corrected/upscaled image",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (show_only_filtered_image) => {
|
|
|
|
showOnlyFilteredImageField.checked = show_only_filtered_image
|
|
|
|
},
|
|
|
|
readUI: () => showOnlyFilteredImageField.checked,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => Boolean(val),
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
output_format: {
|
|
|
|
name: "Output Format",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (output_format) => {
|
|
|
|
outputFormatField.value = output_format
|
|
|
|
},
|
|
|
|
readUI: () => outputFormatField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
2022-10-31 04:21:39 +01:00
|
|
|
},
|
2023-04-27 19:56:56 +02:00
|
|
|
save_to_disk_path: {
|
|
|
|
name: "Save to disk path",
|
2022-10-31 04:21:39 +01:00
|
|
|
setUI: (save_to_disk_path) => {
|
|
|
|
saveToDiskField.checked = Boolean(save_to_disk_path)
|
|
|
|
diskPathField.value = save_to_disk_path
|
|
|
|
},
|
|
|
|
readUI: () => diskPathField.value,
|
2023-04-28 12:20:44 +02:00
|
|
|
parse: (val) => val,
|
|
|
|
},
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-12-29 10:16:44 +01:00
|
|
|
|
2022-11-18 11:08:17 +01:00
|
|
|
function restoreTaskToUI(task, fieldsToSkip) {
|
|
|
|
fieldsToSkip = fieldsToSkip || []
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
if ("numOutputsTotal" in task) {
|
2022-10-31 04:21:39 +01:00
|
|
|
numOutputsTotalField.value = task.numOutputsTotal
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if ("seed" in task) {
|
2022-10-31 04:21:39 +01:00
|
|
|
randomSeedField.checked = false
|
|
|
|
seedField.value = task.seed
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("reqBody" in task)) {
|
2022-10-31 04:21:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for (const key in TASK_MAPPING) {
|
2022-11-18 11:08:17 +01:00
|
|
|
if (key in task.reqBody && !fieldsToSkip.includes(key)) {
|
2022-10-31 04:21:39 +01:00
|
|
|
TASK_MAPPING[key].setUI(task.reqBody[key])
|
|
|
|
}
|
|
|
|
}
|
2022-11-18 11:08:17 +01:00
|
|
|
|
2022-12-29 00:55:59 +01:00
|
|
|
// properly reset fields not present in the task
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("use_hypernetwork_model" in task.reqBody)) {
|
2022-12-29 00:55:59 +01:00
|
|
|
hypernetworkModelField.value = ""
|
|
|
|
hypernetworkModelField.dispatchEvent(new Event("change"))
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
|
|
|
|
if (!("use_lora_model" in task.reqBody)) {
|
2023-08-18 13:48:01 +02:00
|
|
|
loraModelField.modelNames = []
|
|
|
|
loraModelField.modelWeights = []
|
2023-04-02 11:04:03 +02:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
|
2022-12-29 03:52:18 +01:00
|
|
|
// restore the original prompt if provided (e.g. use settings), fallback to prompt as needed (e.g. copy/paste or d&d)
|
|
|
|
promptField.value = task.reqBody.original_prompt
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("original_prompt" in task.reqBody)) {
|
2022-12-29 03:52:18 +01:00
|
|
|
promptField.value = task.reqBody.prompt
|
|
|
|
}
|
2023-05-27 09:26:25 +02:00
|
|
|
promptField.dispatchEvent(new Event("input"))
|
2023-04-27 19:56:56 +02:00
|
|
|
|
2022-11-18 11:08:17 +01:00
|
|
|
// properly reset checkboxes
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("use_face_correction" in task.reqBody)) {
|
2022-11-18 11:08:17 +01:00
|
|
|
useFaceCorrectionField.checked = false
|
2023-01-19 20:49:54 +01:00
|
|
|
gfpganModelField.disabled = true
|
2022-11-18 11:08:17 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("use_upscale" in task.reqBody)) {
|
2022-11-18 11:08:17 +01:00
|
|
|
useUpscalingField.checked = false
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("mask" in task.reqBody) && maskSetting.checked) {
|
2022-11-18 11:08:17 +01:00
|
|
|
maskSetting.checked = false
|
2022-12-29 01:43:35 +01:00
|
|
|
maskSetting.dispatchEvent(new Event("click"))
|
2022-11-18 11:08:17 +01:00
|
|
|
}
|
|
|
|
upscaleModelField.disabled = !useUpscalingField.checked
|
2022-12-27 11:50:16 +01:00
|
|
|
upscaleAmountField.disabled = !useUpscalingField.checked
|
2022-11-18 11:08:17 +01:00
|
|
|
|
2022-12-29 10:16:44 +01:00
|
|
|
// hide/show source picture as needed
|
|
|
|
if (IMAGE_REGEX.test(initImagePreview.src) && task.reqBody.init_image == undefined) {
|
|
|
|
// hide source image
|
2022-12-29 02:00:38 +01:00
|
|
|
initImageClearBtn.dispatchEvent(new Event("click"))
|
2023-04-27 19:56:56 +02:00
|
|
|
} else if (task.reqBody.init_image !== undefined) {
|
2022-12-29 10:16:44 +01:00
|
|
|
// listen for inpainter loading event, which happens AFTER the main image loads (which reloads the inpainter)
|
2023-04-27 19:56:56 +02:00
|
|
|
initImagePreview.addEventListener(
|
|
|
|
"load",
|
|
|
|
function() {
|
|
|
|
if (Boolean(task.reqBody.mask)) {
|
|
|
|
imageInpainter.setImg(task.reqBody.mask)
|
|
|
|
maskSetting.checked = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ once: true }
|
|
|
|
)
|
2022-12-29 10:16:44 +01:00
|
|
|
initImagePreview.src = task.reqBody.init_image
|
2022-11-18 11:08:17 +01:00
|
|
|
}
|
2023-08-18 14:29:00 +02:00
|
|
|
|
|
|
|
// hide/show controlnet picture as needed
|
|
|
|
if (IMAGE_REGEX.test(controlImagePreview.src) && task.reqBody.control_image == undefined) {
|
|
|
|
// hide source image
|
|
|
|
controlImageClearBtn.dispatchEvent(new Event("click"))
|
|
|
|
} else if (task.reqBody.control_image !== undefined) {
|
|
|
|
// listen for inpainter loading event, which happens AFTER the main image loads (which reloads the inpai
|
|
|
|
controlImagePreview.src = task.reqBody.control_image
|
|
|
|
}
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-10-31 06:13:04 +01:00
|
|
|
function readUI() {
|
2022-10-31 06:02:23 +01:00
|
|
|
const reqBody = {}
|
|
|
|
for (const key in TASK_MAPPING) {
|
|
|
|
reqBody[key] = TASK_MAPPING[key].readUI()
|
|
|
|
}
|
|
|
|
return {
|
2023-04-27 19:56:56 +02:00
|
|
|
numOutputsTotal: parseInt(numOutputsTotalField.value),
|
|
|
|
seed: TASK_MAPPING["seed"].readUI(),
|
2023-04-28 12:20:44 +02:00
|
|
|
reqBody: reqBody,
|
2022-10-31 06:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
function getModelPath(filename, extensions) {
|
2023-02-18 03:49:51 +01:00
|
|
|
if (typeof filename !== "string") {
|
2023-02-14 11:31:13 +01:00
|
|
|
return
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
|
2023-02-07 07:19:57 +01:00
|
|
|
let pathIdx
|
2023-04-27 19:56:56 +02:00
|
|
|
if (filename.includes("/models/stable-diffusion/")) {
|
|
|
|
pathIdx = filename.indexOf("/models/stable-diffusion/") + 25 // Linux, Mac paths
|
|
|
|
} else if (filename.includes("\\models\\stable-diffusion\\")) {
|
|
|
|
pathIdx = filename.indexOf("\\models\\stable-diffusion\\") + 25 // Linux, Mac paths
|
2022-11-19 09:56:44 +01:00
|
|
|
}
|
|
|
|
if (pathIdx >= 0) {
|
2023-02-07 07:19:57 +01:00
|
|
|
filename = filename.slice(pathIdx)
|
2022-11-19 09:56:44 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
extensions.forEach((ext) => {
|
2022-11-19 09:56:44 +01:00
|
|
|
if (filename.endsWith(ext)) {
|
|
|
|
filename = filename.slice(0, filename.length - ext.length)
|
|
|
|
}
|
|
|
|
})
|
2022-11-24 22:47:35 +01:00
|
|
|
return filename
|
2022-11-19 09:56:44 +01:00
|
|
|
}
|
2022-10-31 06:02:23 +01:00
|
|
|
|
2022-10-31 04:21:39 +01:00
|
|
|
const TASK_TEXT_MAPPING = {
|
2023-04-27 19:56:56 +02:00
|
|
|
prompt: "Prompt",
|
|
|
|
width: "Width",
|
|
|
|
height: "Height",
|
|
|
|
seed: "Seed",
|
|
|
|
num_inference_steps: "Steps",
|
|
|
|
guidance_scale: "Guidance Scale",
|
|
|
|
prompt_strength: "Prompt Strength",
|
|
|
|
use_face_correction: "Use Face Correction",
|
|
|
|
use_upscale: "Use Upscaling",
|
|
|
|
upscale_amount: "Upscale By",
|
|
|
|
sampler_name: "Sampler",
|
|
|
|
negative_prompt: "Negative Prompt",
|
|
|
|
use_stable_diffusion_model: "Stable Diffusion model",
|
|
|
|
use_hypernetwork_model: "Hypernetwork model",
|
2023-04-28 12:20:44 +02:00
|
|
|
hypernetwork_strength: "Hypernetwork Strength",
|
2023-08-18 10:46:23 +02:00
|
|
|
use_lora_model: "LoRA model",
|
|
|
|
lora_alpha: "LoRA Strength",
|
2023-08-18 14:23:16 +02:00
|
|
|
use_controlnet_model: "ControlNet model",
|
|
|
|
control_filter_to_apply: "ControlNet Filter",
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
|
|
|
function parseTaskFromText(str) {
|
|
|
|
const taskReqBody = {}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const lines = str.split("\n")
|
2022-12-27 12:36:46 +01:00
|
|
|
if (lines.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-31 04:21:39 +01:00
|
|
|
// Prompt
|
2022-12-27 12:36:46 +01:00
|
|
|
let knownKeyOnFirstLine = false
|
|
|
|
for (let key in TASK_TEXT_MAPPING) {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (lines[0].startsWith(TASK_TEXT_MAPPING[key] + ":")) {
|
2022-12-27 12:36:46 +01:00
|
|
|
knownKeyOnFirstLine = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!knownKeyOnFirstLine) {
|
|
|
|
taskReqBody.prompt = lines[0]
|
2023-04-27 19:56:56 +02:00
|
|
|
console.log("Prompt:", taskReqBody.prompt)
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-12-27 12:36:46 +01:00
|
|
|
|
2022-10-31 04:21:39 +01:00
|
|
|
for (const key in TASK_TEXT_MAPPING) {
|
2022-12-27 12:36:46 +01:00
|
|
|
if (key in taskReqBody) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const name = TASK_TEXT_MAPPING[key]
|
2022-10-31 04:21:39 +01:00
|
|
|
let val = undefined
|
2022-11-08 14:14:11 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const reName = new RegExp(`${name}\\ *:\\ *(.*)(?:\\r\\n|\\r|\\n)*`, "igm")
|
|
|
|
const match = reName.exec(str)
|
2022-11-08 14:14:11 +01:00
|
|
|
if (match) {
|
|
|
|
str = str.slice(0, match.index) + str.slice(match.index + match[0].length)
|
|
|
|
val = match[1]
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-11-08 14:14:11 +01:00
|
|
|
if (val !== undefined) {
|
2022-10-31 04:21:39 +01:00
|
|
|
taskReqBody[key] = TASK_MAPPING[key].parse(val.trim())
|
2023-04-27 19:56:56 +02:00
|
|
|
console.log(TASK_MAPPING[key].name + ":", taskReqBody[key])
|
2022-10-31 04:21:39 +01:00
|
|
|
if (!str) {
|
2022-10-31 06:01:56 +01:00
|
|
|
break
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(taskReqBody).length <= 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
const task = { reqBody: taskReqBody }
|
2023-04-27 19:56:56 +02:00
|
|
|
if ("seed" in taskReqBody) {
|
2022-10-31 04:21:39 +01:00
|
|
|
task.seed = taskReqBody.seed
|
|
|
|
}
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:44:11 +01:00
|
|
|
async function parseContent(text) {
|
2023-04-27 19:56:56 +02:00
|
|
|
text = text.trim()
|
|
|
|
if (text.startsWith("{") && text.endsWith("}")) {
|
2022-10-31 04:21:39 +01:00
|
|
|
try {
|
2022-11-17 07:44:11 +01:00
|
|
|
const task = JSON.parse(text)
|
2023-04-27 19:56:56 +02:00
|
|
|
if (!("reqBody" in task)) {
|
|
|
|
// support the format saved to the disk, by the UI
|
2022-12-14 11:13:24 +01:00
|
|
|
task.reqBody = Object.assign({}, task)
|
|
|
|
}
|
2022-10-31 04:21:39 +01:00
|
|
|
restoreTaskToUI(task)
|
2022-11-17 08:16:20 +01:00
|
|
|
return true
|
2022-10-31 04:21:39 +01:00
|
|
|
} catch (e) {
|
2022-11-17 07:44:11 +01:00
|
|
|
console.warn(`JSON text content couldn't be parsed.`, e)
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-11-17 08:16:20 +01:00
|
|
|
return false
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
|
|
|
// Normal txt file.
|
2022-11-17 07:44:11 +01:00
|
|
|
const task = parseTaskFromText(text)
|
2023-04-27 19:56:56 +02:00
|
|
|
if (text.toLowerCase().includes("seed:") && task) {
|
|
|
|
// only parse valid task content
|
2022-10-31 04:21:39 +01:00
|
|
|
restoreTaskToUI(task)
|
2022-11-17 08:16:20 +01:00
|
|
|
return true
|
2022-10-31 04:21:39 +01:00
|
|
|
} else {
|
2022-11-17 07:44:11 +01:00
|
|
|
console.warn(`Raw text content couldn't be parsed.`)
|
2023-03-19 16:21:17 +01:00
|
|
|
promptField.value = text
|
2022-11-17 08:16:20 +01:00
|
|
|
return false
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:44:11 +01:00
|
|
|
async function readFile(file, i) {
|
2022-11-22 17:09:47 +01:00
|
|
|
console.log(`Event %o reading file[${i}]:${file.name}...`)
|
2022-11-17 07:44:11 +01:00
|
|
|
const fileContent = (await file.text()).trim()
|
|
|
|
return await parseContent(fileContent)
|
|
|
|
}
|
|
|
|
|
2022-10-31 04:21:39 +01:00
|
|
|
function dropHandler(ev) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.log("Content dropped...")
|
2022-11-08 13:44:26 +01:00
|
|
|
let items = []
|
2022-10-31 04:21:39 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
if (ev?.dataTransfer?.items) {
|
|
|
|
// Use DataTransferItemList interface
|
2022-11-08 13:44:26 +01:00
|
|
|
items = Array.from(ev.dataTransfer.items)
|
2023-04-27 19:56:56 +02:00
|
|
|
items = items.filter((item) => item.kind === "file")
|
|
|
|
items = items.map((item) => item.getAsFile())
|
|
|
|
} else if (ev?.dataTransfer?.files) {
|
|
|
|
// Use DataTransfer interface
|
2022-11-08 13:44:26 +01:00
|
|
|
items = Array.from(ev.dataTransfer.files)
|
|
|
|
}
|
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
items.forEach((item) => {
|
|
|
|
item.file_ext = EXT_REGEX.exec(item.name.toLowerCase())[1]
|
|
|
|
})
|
2022-11-09 10:51:41 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
let text_items = items.filter((item) => TEXT_EXTENSIONS.includes(item.file_ext))
|
|
|
|
let image_items = items.filter((item) => IMAGE_EXTENSIONS.includes(item.file_ext))
|
2022-11-08 13:44:26 +01:00
|
|
|
|
2022-11-09 10:51:41 +01:00
|
|
|
if (image_items.length > 0 && ev.target == initImageSelector) {
|
|
|
|
return // let the event bubble up, so that the Init Image filepicker can receive this
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
2022-11-09 10:51:41 +01:00
|
|
|
|
|
|
|
ev.preventDefault() // Prevent default behavior (Prevent file/content from being opened)
|
|
|
|
text_items.forEach(readFile)
|
2022-10-31 04:21:39 +01:00
|
|
|
}
|
|
|
|
function dragOverHandler(ev) {
|
2023-04-27 19:56:56 +02:00
|
|
|
console.log("Content in drop zone")
|
2022-10-31 04:21:39 +01:00
|
|
|
|
|
|
|
// Prevent default behavior (Prevent file/content from being opened)
|
|
|
|
ev.preventDefault()
|
|
|
|
|
|
|
|
ev.dataTransfer.dropEffect = "copy"
|
|
|
|
|
|
|
|
let img = new Image()
|
2023-04-27 19:56:56 +02:00
|
|
|
img.src = "//" + location.host + "/media/images/favicon-32x32.png"
|
2022-10-31 04:21:39 +01:00
|
|
|
ev.dataTransfer.setDragImage(img, 16, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener("drop", dropHandler)
|
|
|
|
document.addEventListener("dragover", dragOverHandler)
|
2022-10-31 06:02:23 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
const TASK_REQ_NO_EXPORT = ["use_cpu", "save_to_disk_path"]
|
|
|
|
const resetSettings = document.getElementById("reset-image-settings")
|
2022-11-03 08:10:31 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
function checkReadTextClipboardPermission(result) {
|
2022-11-17 07:44:11 +01:00
|
|
|
if (result.state != "granted" && result.state != "prompt") {
|
2022-11-14 01:23:04 +01:00
|
|
|
return
|
|
|
|
}
|
2022-11-17 07:44:11 +01:00
|
|
|
// PASTE ICON
|
2023-04-27 19:56:56 +02:00
|
|
|
const pasteIcon = document.createElement("i")
|
|
|
|
pasteIcon.className = "fa-solid fa-paste section-button"
|
2022-12-11 08:29:23 +01:00
|
|
|
pasteIcon.innerHTML = `<span class="simple-tooltip top-left">Paste Image Settings</span>`
|
2023-04-27 19:56:56 +02:00
|
|
|
pasteIcon.addEventListener("click", async (event) => {
|
2022-11-17 07:44:11 +01:00
|
|
|
event.stopPropagation()
|
|
|
|
// Add css class 'active'
|
2023-04-27 19:56:56 +02:00
|
|
|
pasteIcon.classList.add("active")
|
2022-11-17 07:44:11 +01:00
|
|
|
// In 350 ms remove the 'active' class
|
2023-04-27 19:56:56 +02:00
|
|
|
asyncDelay(350).then(() => pasteIcon.classList.remove("active"))
|
2022-11-17 07:44:11 +01:00
|
|
|
|
|
|
|
// Retrieve clipboard content and try to parse it
|
2023-04-27 19:56:56 +02:00
|
|
|
const text = await navigator.clipboard.readText()
|
2022-11-17 07:44:11 +01:00
|
|
|
await parseContent(text)
|
|
|
|
})
|
|
|
|
resetSettings.parentNode.insertBefore(pasteIcon, resetSettings)
|
2022-11-14 01:23:04 +01:00
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
navigator.permissions
|
|
|
|
.query({ name: "clipboard-read" })
|
|
|
|
.then(checkReadTextClipboardPermission, (reason) => console.log("clipboard-read is not available. %o", reason))
|
2022-11-14 01:23:04 +01:00
|
|
|
|
2023-04-27 19:56:56 +02:00
|
|
|
document.addEventListener("paste", async (event) => {
|
2022-11-17 08:36:14 +01:00
|
|
|
if (event.target) {
|
|
|
|
const targetTag = event.target.tagName.toLowerCase()
|
|
|
|
// Disable when targeting input elements.
|
2023-04-27 19:56:56 +02:00
|
|
|
if (targetTag === "input" || targetTag === "textarea") {
|
2022-11-17 08:36:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 19:56:56 +02:00
|
|
|
const paste = (event.clipboardData || window.clipboardData).getData("text")
|
2022-11-17 08:16:20 +01:00
|
|
|
const selection = window.getSelection()
|
2023-04-27 19:56:56 +02:00
|
|
|
if (paste != "" && selection.toString().trim().length <= 0 && (await parseContent(paste))) {
|
2022-11-17 08:36:14 +01:00
|
|
|
event.preventDefault()
|
|
|
|
return
|
2022-11-17 08:16:20 +01:00
|
|
|
}
|
2022-11-17 08:36:14 +01:00
|
|
|
})
|
2022-11-14 01:23:04 +01:00
|
|
|
|
|
|
|
// Adds a copy and a paste icon if the browser grants permission to write to clipboard.
|
2023-04-27 19:56:56 +02:00
|
|
|
function checkWriteToClipboardPermission(result) {
|
2022-11-17 07:44:11 +01:00
|
|
|
if (result.state != "granted" && result.state != "prompt") {
|
|
|
|
return
|
2022-10-31 06:02:23 +01:00
|
|
|
}
|
2022-11-17 07:44:11 +01:00
|
|
|
// COPY ICON
|
2023-04-27 19:56:56 +02:00
|
|
|
const copyIcon = document.createElement("i")
|
|
|
|
copyIcon.className = "fa-solid fa-clipboard section-button"
|
2022-12-11 08:29:23 +01:00
|
|
|
copyIcon.innerHTML = `<span class="simple-tooltip top-left">Copy Image Settings</span>`
|
2023-04-27 19:56:56 +02:00
|
|
|
copyIcon.addEventListener("click", (event) => {
|
2022-11-17 07:44:11 +01:00
|
|
|
event.stopPropagation()
|
|
|
|
// Add css class 'active'
|
2023-04-27 19:56:56 +02:00
|
|
|
copyIcon.classList.add("active")
|
2022-11-17 07:44:11 +01:00
|
|
|
// In 350 ms remove the 'active' class
|
2023-04-27 19:56:56 +02:00
|
|
|
asyncDelay(350).then(() => copyIcon.classList.remove("active"))
|
2022-11-17 07:44:11 +01:00
|
|
|
const uiState = readUI()
|
|
|
|
TASK_REQ_NO_EXPORT.forEach((key) => delete uiState.reqBody[key])
|
|
|
|
if (uiState.reqBody.init_image && !IMAGE_REGEX.test(uiState.reqBody.init_image)) {
|
|
|
|
delete uiState.reqBody.init_image
|
|
|
|
delete uiState.reqBody.prompt_strength
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(JSON.stringify(uiState, undefined, 4))
|
|
|
|
})
|
|
|
|
resetSettings.parentNode.insertBefore(copyIcon, resetSettings)
|
2022-10-31 06:02:23 +01:00
|
|
|
}
|
2022-11-17 07:44:11 +01:00
|
|
|
// Determine which access we have to the clipboard. Clipboard access is only available on localhost or via TLS.
|
2022-10-31 06:02:23 +01:00
|
|
|
navigator.permissions.query({ name: "clipboard-write" }).then(checkWriteToClipboardPermission, (e) => {
|
2023-04-27 19:56:56 +02:00
|
|
|
if (e instanceof TypeError && typeof navigator?.clipboard?.writeText === "function") {
|
2022-10-31 06:02:23 +01:00
|
|
|
// Fix for firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1560373
|
2023-04-27 19:56:56 +02:00
|
|
|
checkWriteToClipboardPermission({ state: "granted" })
|
2022-10-31 06:02:23 +01:00
|
|
|
}
|
|
|
|
})
|