|
const io = require('./utility/io') |
|
const log = console.log |
|
const warn = console.warn |
|
const error = console.error |
|
const should_log = false |
|
if (should_log) { |
|
window.addEventListener('error', (event) => { |
|
const [a, b, c, d, e] = [1, 2, 3, 4, 5] |
|
console.log(`message: ${a}`) |
|
console.log(`source: ${b}`) |
|
console.log(`lineno: ${c}`) |
|
console.log(`colno: ${d}`) |
|
console.log(`error: ${e}`) |
|
}) |
|
|
|
console.log = (data, ...optional_param) => { |
|
log(data, ...optional_param) |
|
io.IOLog.saveLogToFile({ data, ...optional_param }, 'log.txt') |
|
} |
|
console.warn = (data, ...optional_param) => { |
|
try { |
|
warn(data, ...optional_param) |
|
io.IOLog.saveLogToFile({ data, ...optional_param }, 'warn.txt') |
|
} catch (e) { |
|
warn('error while logging: ') |
|
warn(e) |
|
} |
|
} |
|
console.error = (data, ...optional_param) => { |
|
error(data, ...optional_param) |
|
io.IOLog.saveLogToFile({ data, ...optional_param }, 'error.txt') |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
let g_version = 'v1.2.5' |
|
let g_sd_url = 'http://127.0.0.1:7860' |
|
let g_online_data_url = |
|
'https://raw.githubusercontent.com/AbdullahAlfaraj/Auto-Photoshop-StableDiffusion-Plugin/master/utility/online_data.json' |
|
const Enum = require('./enum') |
|
const helper = require('./helper') |
|
const sd_tab = require('./utility/tab/sd') |
|
|
|
|
|
const sdapi = require('./sdapi_py_re') |
|
|
|
|
|
const outpaint = require('./outpaint') |
|
const psapi = require('./psapi') |
|
const app = window.require('photoshop').app |
|
|
|
const { batchPlay } = require('photoshop').action |
|
const { executeAsModal } = require('photoshop').core |
|
const dialog_box = require('./dialog_box') |
|
|
|
const html_manip = require('./utility/html_manip') |
|
|
|
const viewer = require('./viewer') |
|
const selection = require('./selection') |
|
const layer_util = require('./utility/layer') |
|
const sd_options = require('./utility/sdapi/options') |
|
const sd_config = require('./utility/sdapi/config') |
|
const session = require('./utility/session') |
|
const ui = require('./utility/ui') |
|
const preset_util = require('./utility/presets/preset') |
|
const script_horde = require('./utility/sd_scripts/horde') |
|
const prompt_shortcut = require('./utility/sdapi/prompt_shortcut') |
|
const formats = require('uxp').storage.formats |
|
const storage = require('uxp').storage |
|
const shell = require('uxp').shell |
|
const fs = storage.localFileSystem |
|
const horde_native = require('./utility/sdapi/horde_native') |
|
|
|
const dummy = require('./utility/dummy') |
|
const general = require('./utility/general') |
|
const thumbnail = require('./thumbnail') |
|
const note = require('./utility/notification') |
|
const sampler_data = require('./utility/sampler') |
|
const settings_tab = require('./utility/tab/settings') |
|
const control_net = require('./utility/tab/control_net') |
|
|
|
const history_tab = require('./utility/tab/history_tab') |
|
const image_search_tab = require('./utility/tab/image_search_tab') |
|
const lexica_tab = require('./utility/tab/lexica_tab') |
|
const share_tab = require('./utility/tab/share_tab') |
|
|
|
const ultimate_sd_upscaler_script = require('./ultimate_sd_upscaler/dist/ultimate_sd_upscaler.bundle') |
|
const scripts = require('./ultimate_sd_upscaler/dist/scripts.bundle') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let g_horde_generator = new horde_native.hordeGenerator() |
|
let g_automatic_status = Enum.AutomaticStatusEnum['Offline'] |
|
let g_models_status = false |
|
let g_current_batch_index = 0 |
|
|
|
async function hasSessionSelectionChanged() { |
|
try { |
|
const isSelectionActive = await psapi.checkIfSelectionAreaIsActive() |
|
if (isSelectionActive) { |
|
const current_selection = isSelectionActive |
|
|
|
if ( |
|
await hasSelectionChanged( |
|
current_selection, |
|
g_generation_session.selectionInfo |
|
) |
|
) { |
|
return true |
|
} else { |
|
|
|
return false |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
return false |
|
} |
|
} |
|
|
|
async function calcWidthHeightFromSelection() { |
|
|
|
const selection_mode = html_manip.getSelectionMode() |
|
if (selection_mode === 'ratio') { |
|
|
|
const [width, height, hr_width, hr_height] = |
|
await selection.selectionToFinalWidthHeight() |
|
|
|
html_manip.autoFillInWidth(width) |
|
html_manip.autoFillInHeight(height) |
|
html_manip.autoFillInHRWidth(hr_width) |
|
html_manip.autoFillInHRHeight(hr_height) |
|
} else if (selection_mode === 'precise') { |
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
const [width, height, hr_width, hr_height] = [ |
|
selectionInfo.width, |
|
selectionInfo.height, |
|
0, |
|
0, |
|
] |
|
html_manip.autoFillInWidth(width) |
|
html_manip.autoFillInHeight(height) |
|
} |
|
} |
|
|
|
const eventHandler = async (event, descriptor) => { |
|
try { |
|
console.log(event, descriptor) |
|
|
|
const isSelectionActive = await psapi.checkIfSelectionAreaIsActive() |
|
if (isSelectionActive) { |
|
const current_selection = isSelectionActive |
|
|
|
await calcWidthHeightFromSelection() |
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
await hasSelectionChanged( |
|
current_selection, |
|
g_generation_session.selectionInfo |
|
) |
|
) { |
|
|
|
|
|
|
|
|
|
const selected_mode = getCurrentGenerationModeByValue(g_sd_mode) |
|
g_ui.generateModeUI(selected_mode) |
|
} else { |
|
|
|
|
|
|
|
|
|
const current_mode = html_manip.getMode() |
|
if ( |
|
g_generation_session.isActive() && |
|
g_generation_session.isSameMode(current_mode) |
|
) { |
|
|
|
g_ui.generateMoreUI() |
|
} |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
function getCurrentGenerationModeByValue(value) { |
|
for (let key in generationMode) { |
|
if ( |
|
generationMode.hasOwnProperty(key) && |
|
generationMode[key] === value |
|
) { |
|
return key |
|
} |
|
} |
|
return undefined |
|
} |
|
|
|
require('photoshop').action.addNotificationListener( |
|
['set', 'move'], |
|
eventHandler |
|
) |
|
|
|
async function getUniqueDocumentId() { |
|
console.warn( |
|
'getUniqueDocumentId is deprecated, instead use the methods in IOFolder' |
|
) |
|
try { |
|
let uniqueDocumentId = await psapi.readUniqueDocumentIdExe() |
|
|
|
console.log( |
|
'getUniqueDocumentId(): uniqueDocumentId: ', |
|
uniqueDocumentId |
|
) |
|
|
|
|
|
const regexExp = |
|
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi |
|
|
|
|
|
|
|
|
|
const isValidId = regexExp.test(uniqueDocumentId) |
|
console.log('isValidId: ', isValidId) |
|
if (isValidId == false) { |
|
let uuid = self.crypto.randomUUID() |
|
console.log(uuid) |
|
await psapi.saveUniqueDocumentIdExe(uuid) |
|
uniqueDocumentId = uuid |
|
} |
|
return uniqueDocumentId |
|
} catch (e) { |
|
console.warn('warning Document Id may not be valid', e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Array.from(document.querySelectorAll('.sp-tab')).forEach((theTab) => { |
|
theTab.onclick = () => { |
|
try { |
|
|
|
Array.from(document.querySelectorAll('.sp-tab')).forEach((aTab) => { |
|
if (aTab.getAttribute('id') === theTab.getAttribute('id')) { |
|
aTab.classList.add('selected') |
|
} else { |
|
aTab.classList.remove('selected') |
|
} |
|
}) |
|
Array.from(document.querySelectorAll('.sp-tab-page')).forEach( |
|
(tabPage) => { |
|
if ( |
|
tabPage |
|
.getAttribute('id') |
|
.startsWith(theTab.getAttribute('id')) |
|
) { |
|
tabPage.classList.add('visible-hack') |
|
} else { |
|
tabPage.classList.remove('visible-hack') |
|
} |
|
} |
|
) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
}) |
|
|
|
document.getElementById('sp-viewer-tab').addEventListener('click', async () => { |
|
if ( |
|
g_generation_session.isActive() && |
|
g_generation_session.mode === 'upscale' |
|
) { |
|
g_sd_mode = 'upscale' |
|
} else { |
|
g_sd_mode = html_manip.getMode() |
|
} |
|
}) |
|
|
|
document.getElementById('sp-viewer-tab').addEventListener('click', async () => { |
|
moveElementToAnotherTab('batchNumberUi', 'batchNumberViewerTabContainer') |
|
await displayUpdate() |
|
}) |
|
|
|
document |
|
.getElementById('sp-stable-diffusion-ui-tab') |
|
.addEventListener('click', () => { |
|
moveElementToAnotherTab('batchNumberUi', 'batchNumber-steps-container') |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const random_session_id = Math.floor(Math.random() * 1000000 + 1) |
|
|
|
function getSelectedText() { |
|
|
|
|
|
|
|
const promptTextarea = document.querySelector('#taPrompt') |
|
console.log('promptTextarea: ', promptTextarea.value) |
|
|
|
var start = promptTextarea.selectionStart |
|
console.log('start: ', start) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
function getCommentedString() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let text = `Visit /*W3Schools |
|
cute, girl, painterly |
|
*/ any text |
|
and prompt |
|
|
|
|
|
|
|
|
|
cute cat /*by greg |
|
|
|
and artgerm |
|
|
|
*/ and famous artist` |
|
console.log('getCommentedString: text: ', text) |
|
|
|
|
|
let pattern = /(\/)(\*)(\s|\S)*?(\*\/)/g |
|
|
|
let result = text.match(pattern) |
|
console.log('getCommentedString: ', result) |
|
} |
|
|
|
|
|
function tempDisableElement(element, time) { |
|
element.classList.add('disableBtn') |
|
element.disabled = true |
|
|
|
|
|
setTimeout(function () { |
|
element.disabled = false |
|
element.classList.remove('disableBtn') |
|
|
|
|
|
}, time) |
|
} |
|
|
|
|
|
async function displayNotification(automatic_status) { |
|
if (automatic_status === Enum.AutomaticStatusEnum['RunningWithApi']) { |
|
|
|
} else if ( |
|
g_automatic_status === Enum.AutomaticStatusEnum['RunningNoApi'] |
|
) { |
|
await note.Notification.webuiAPIMissing() |
|
} else if (g_automatic_status === Enum.AutomaticStatusEnum['Offline']) { |
|
await note.Notification.webuiIsOffline() |
|
} |
|
} |
|
|
|
async function checkAutoStatus() { |
|
try { |
|
const options = await g_sd_options_obj.getOptions() |
|
if (options) { |
|
|
|
html_manip.setAutomaticStatus('connected', 'disconnected') |
|
g_automatic_status = Enum.AutomaticStatusEnum['RunningWithApi'] |
|
|
|
} else { |
|
html_manip.setAutomaticStatus('disconnected', 'connected') |
|
if (await sdapi.isWebuiRunning()) { |
|
|
|
g_automatic_status = Enum.AutomaticStatusEnum['RunningNoApi'] |
|
|
|
} else { |
|
|
|
g_automatic_status = Enum.AutomaticStatusEnum['Offline'] |
|
|
|
} |
|
|
|
return g_automatic_status |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
return g_automatic_status |
|
} |
|
|
|
|
|
async function refreshUI() { |
|
try { |
|
const b_proxy_server_status = await updateVersionUI() |
|
if (b_proxy_server_status) { |
|
html_manip.setProxyServerStatus('connected', 'disconnected') |
|
|
|
} else { |
|
html_manip.setProxyServerStatus('disconnected', 'connected') |
|
} |
|
|
|
g_automatic_status = await checkAutoStatus() |
|
await displayNotification(g_automatic_status) |
|
|
|
const bSamplersStatus = await initSamplers() |
|
|
|
g_models_status = await refreshModels() |
|
await refreshExtraUpscalers() |
|
|
|
await sdapi.setInpaintMaskWeight(1.0) |
|
|
|
|
|
|
|
await g_sd_options_obj.getOptions() |
|
|
|
const current_model_title = g_sd_options_obj.getCurrentModel() |
|
|
|
const current_model_hash = |
|
html_manip.getModelHashByTitle(current_model_title) |
|
html_manip.autoFillInModel(current_model_hash) |
|
|
|
|
|
const inpainting_mask_weight = |
|
await g_sd_options_obj.getInpaintingMaskWeight() |
|
console.log('inpainting_mask_weight: ', inpainting_mask_weight) |
|
html_manip.autoFillInInpaintMaskWeight(inpainting_mask_weight) |
|
|
|
await g_sd_config_obj.getConfig() |
|
|
|
|
|
g_controlnet_max_models = g_sd_config_obj.getControlNetMaxModelsNum() |
|
await control_net.initializeControlNetTab(g_controlnet_max_models) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function refreshModels() { |
|
let b_result = false |
|
try { |
|
g_models = await sdapi.requestGetModels() |
|
if (g_models.length > 0) { |
|
b_result = true |
|
} |
|
|
|
|
|
|
|
document.getElementById('mModelsMenu').innerHTML = '' |
|
|
|
for (let model of g_models) { |
|
|
|
const menu_item_element = document.createElement('sp-menu-item') |
|
menu_item_element.className = 'mModelMenuItem' |
|
menu_item_element.innerHTML = model.title |
|
menu_item_element.dataset.model_hash = model.hash |
|
menu_item_element.dataset.model_title = model.title |
|
document |
|
.getElementById('mModelsMenu') |
|
.appendChild(menu_item_element) |
|
} |
|
} catch (e) { |
|
b_result = false |
|
console.warn(e) |
|
} |
|
return b_result |
|
} |
|
|
|
async function refreshExtraUpscalers() { |
|
try { |
|
|
|
var hrModelsMenuClass = document.getElementsByClassName( |
|
'hrExtrasUpscaleModelsMenuClass' |
|
) |
|
for (let i = 0; i < hrModelsMenuClass.length; i++) { |
|
hrModelsMenuClass[i].innerHTML = '' |
|
} |
|
g_extra_upscalers = await sdapi.requestGetUpscalers() |
|
|
|
for (let model of g_extra_upscalers) { |
|
var hrModelsMenuClass = document.getElementsByClassName( |
|
'hrExtrasUpscaleModelsMenuClass' |
|
) |
|
for (let i = 0; i < hrModelsMenuClass.length; i++) { |
|
const menu_item_element = document.createElement('sp-menu-item') |
|
menu_item_element.className = 'hrExtrasUpscaleModelsMenuItem' |
|
menu_item_element.innerHTML = model.name |
|
hrModelsMenuClass[i].appendChild(menu_item_element) |
|
|
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
async function updateVersionUI() { |
|
let bStatus = false |
|
try { |
|
version = await sdapi.getVersionRequest() |
|
document.getElementById('lVersionNumber').textContent = version |
|
if (version !== 'v0.0.0') { |
|
bStatus = true |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
document.getElementById('lVersionNumber').textContent = 'v0.0.0' |
|
bStatus = false |
|
} |
|
return bStatus |
|
} |
|
|
|
async function initSamplers() { |
|
let bStatus = false |
|
try { |
|
let sampler_group = document.getElementById('sampler_group') |
|
sampler_group.innerHTML = '' |
|
|
|
let samplers = await sdapi.requestGetSamplers() |
|
if (!samplers) { |
|
|
|
|
|
samplers = sampler_data.samplers |
|
} |
|
|
|
for (let sampler of samplers) { |
|
|
|
|
|
|
|
const rbSampler = document.createElement('sp-radio') |
|
|
|
rbSampler.innerHTML = sampler.name |
|
rbSampler.setAttribute('class', 'rbSampler') |
|
rbSampler.setAttribute('value', sampler.name) |
|
|
|
sampler_group.appendChild(rbSampler) |
|
|
|
|
|
|
|
rbSampler.addEventListener('click', (evt) => { |
|
g_sd_sampler = evt.target.value |
|
console.log(`You clicked: ${g_sd_sampler}`) |
|
}) |
|
} |
|
document |
|
.getElementsByClassName('rbSampler')[0] |
|
.setAttribute('checked', '') |
|
if (samplers.length > 0) { |
|
bStatus = true |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
return bStatus |
|
} |
|
|
|
function promptShortcutExample() { |
|
let prompt_shortcut_example = { |
|
game_like: |
|
'Unreal Engine, Octane Render, arcane card game ui, hearthstone art style, epic fantasy style art', |
|
large_building_1: 'castle, huge building, large building', |
|
painterly_style_1: |
|
'A full portrait of a beautiful post apocalyptic offworld arctic explorer, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration', |
|
ugly: ' ((((ugly)))), (((duplicate))), ((morbid)), ((mutilated)), out of frame, extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), ((ugly)), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck)))', |
|
} |
|
var JSONInPrettyFormat = JSON.stringify( |
|
prompt_shortcut_example, |
|
undefined, |
|
7 |
|
) |
|
document.getElementById('taPromptShortcut').value = JSONInPrettyFormat |
|
return prompt_shortcut_example |
|
} |
|
|
|
|
|
let prompt_dir_name = '' |
|
let gImage_paths = [] |
|
let g_image_path_to_layer = {} |
|
let g_init_images_dir = './server/python_server/init_images' |
|
|
|
gCurrentImagePath = '' |
|
|
|
let g_init_image_name = '' |
|
|
|
|
|
let g_init_image_mask_name = '' |
|
|
|
|
|
|
|
|
|
|
|
let g_sd_mode = 'txt2img' |
|
|
|
|
|
let g_sd_mode_last = g_sd_mode |
|
|
|
let g_sd_sampler = 'Euler a' |
|
|
|
let g_denoising_strength = 0.7 |
|
|
|
let g_use_mask_image = false |
|
let g_models = [] |
|
|
|
let g_model_title = '' |
|
|
|
|
|
|
|
let hWidth = 512 |
|
|
|
let hHeight = 512 |
|
|
|
let h_denoising_strength = 0.7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
let g_metadatas = [] |
|
let g_last_seed = '-1' |
|
|
|
let g_can_request_progress = true |
|
let g_saved_active_layers = [] |
|
let g_saved_active_selection = {} |
|
let g_is_active_layers_stored = false |
|
|
|
let g_number_generation_per_session = 0 |
|
let g_isViewerMenuDisabled = false |
|
let g_b_mask_layer_exist = false |
|
let g_inpaint_mask_layer |
|
let g_inpaint_mask_layer_history_id |
|
|
|
|
|
|
|
let g_selection = {} |
|
let g_b_use_smart_object = true |
|
let g_sd_options_obj = new sd_options.SdOptions() |
|
|
|
g_sd_options_obj.getOptions() |
|
|
|
|
|
|
|
|
|
|
|
|
|
let g_old_slider_width = 512 |
|
let g_old_slider_height = 512 |
|
let g_sd_config_obj |
|
let g_hi_res_upscaler_models |
|
let g_controlnet_max_models |
|
;(async function () { |
|
let temp_config = new sd_config.SdConfig() |
|
g_sd_config_obj = temp_config |
|
await g_sd_config_obj.getConfig() |
|
g_hi_res_upscaler_models = g_sd_config_obj.getUpscalerModels() |
|
g_controlnet_max_models = g_sd_config_obj.getControlNetMaxModelsNum() |
|
|
|
for (let model of g_hi_res_upscaler_models) { |
|
|
|
let hrModelsMenuClass = |
|
document.getElementsByClassName('hrModelsMenuClass') |
|
for (let i = 0; i < hrModelsMenuClass.length; i++) { |
|
const menu_item_element = document.createElement('sp-menu-item') |
|
menu_item_element.className = 'hrModelsMenuItem' |
|
menu_item_element.innerHTML = model |
|
hrModelsMenuClass[i].appendChild(menu_item_element) |
|
|
|
} |
|
} |
|
})() |
|
|
|
let g_generation_session = new session.GenerationSession(0) |
|
g_generation_session.deactivate() |
|
let g_ui = new ui.UI() |
|
|
|
let g_ui_settings_object = ui.getUISettingsObject() |
|
let g_batch_count_interrupt_status = false |
|
const requestState = { |
|
Generate: 'generate', |
|
Interrupt: 'interrupt', |
|
} |
|
|
|
let g_request_status = '' |
|
|
|
|
|
const generationMode = { |
|
Txt2Img: 'txt2img', |
|
Img2Img: 'img2img', |
|
Inpaint: 'inpaint', |
|
Outpaint: 'outpaint', |
|
Upscale: 'upscale', |
|
} |
|
const backendTypeEnum = { |
|
Auto1111: 'auto1111', |
|
HordeNative: 'horde_native', |
|
Auto1111HordeExtension: 'auto1111_horde_extension', |
|
} |
|
|
|
g_generation_session.mode = generationMode['Txt2Img'] |
|
let g_viewer_manager = new viewer.ViewerManager() |
|
|
|
|
|
|
|
|
|
|
|
async function initPlugin() { |
|
|
|
|
|
|
|
|
|
|
|
await settings_tab.loadSettings() |
|
await horde_native.HordeSettings.loadSettings() |
|
const bSamplersStatus = await initSamplers() |
|
await sdapi.setInpaintMaskWeight(1.0) |
|
await refreshUI() |
|
await displayUpdate() |
|
|
|
await loadPromptShortcut() |
|
await refreshPromptMenue() |
|
|
|
await g_sd_config_obj.getConfig() |
|
|
|
|
|
g_controlnet_max_models = g_sd_config_obj.getControlNetMaxModelsNum() |
|
await control_net.initializeControlNetTab(g_controlnet_max_models) |
|
} |
|
initPlugin() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rbModeElements = document.getElementsByClassName('rbMode') |
|
for (let rbModeElement of rbModeElements) { |
|
rbModeElement.addEventListener('click', async (evt) => { |
|
try { |
|
g_sd_mode = evt.target.value |
|
scripts.script_store.setMode(g_sd_mode) |
|
|
|
await displayUpdate() |
|
await postModeSelection() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
} |
|
|
|
|
|
|
|
document |
|
.getElementById('sp-extras-tab') |
|
.addEventListener('click', async (evt) => { |
|
try { |
|
|
|
g_sd_mode = 'upscale' |
|
console.log(`You clicked: ${g_sd_mode}`) |
|
await displayUpdate() |
|
await postModeSelection() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
document |
|
.getElementById('sp-stable-diffusion-ui-tab') |
|
.addEventListener('click', async (evt) => { |
|
try { |
|
|
|
g_sd_mode = html_manip.getMode() |
|
console.log(`mode restored to: ${g_sd_mode}`) |
|
await displayUpdate() |
|
await postModeSelection() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
async function createTempInpaintMaskLayer() { |
|
if (!g_b_mask_layer_exist) { |
|
|
|
|
|
const name = 'Mask -- Paint White to Mask -- temporary' |
|
await psapi.unselectActiveLayersExe() |
|
const top_layer_doc = await app.activeDocument.layers[0] |
|
g_inpaint_mask_layer = await layer_util.createNewLayerExe(name, 60) |
|
await executeAsModal(async () => { |
|
await g_inpaint_mask_layer.moveAbove(top_layer_doc) |
|
}) |
|
|
|
g_b_mask_layer_exist = true |
|
const index = app.activeDocument.historyStates.length - 1 |
|
g_inpaint_mask_layer_history_id = |
|
app.activeDocument.historyStates[index].id |
|
console.log( |
|
'g_inpaint_mask_layer_history_id: ', |
|
g_inpaint_mask_layer_history_id |
|
) |
|
} |
|
} |
|
|
|
async function deleteTempInpaintMaskLayer() { |
|
console.log( |
|
'g_inpaint_mask_layer_history_id: ', |
|
g_inpaint_mask_layer_history_id |
|
) |
|
const historyBrushTools = app.activeDocument.historyStates.filter( |
|
(h) => h.id > g_inpaint_mask_layer_history_id && h.name === 'Brush Tool' |
|
) |
|
console.log(historyBrushTools) |
|
if (historyBrushTools.length === 0 && g_b_mask_layer_exist) { |
|
await layer_util.deleteLayers([g_inpaint_mask_layer]) |
|
|
|
g_b_mask_layer_exist = false |
|
} |
|
} |
|
|
|
async function postModeSelection() { |
|
|
|
try { |
|
if (g_sd_mode === generationMode['Inpaint']) { |
|
|
|
await createTempInpaintMaskLayer() |
|
} else { |
|
|
|
|
|
await deleteTempInpaintMaskLayer() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
rbMaskContentElements = document.getElementsByClassName('rbMaskContent') |
|
|
|
for (let rbMaskContentElement of rbMaskContentElements) { |
|
rbMaskContentElement.addEventListener('click', async (evt) => { |
|
|
|
|
|
}) |
|
} |
|
|
|
btnSquareClass = document.getElementsByClassName('btnSquare') |
|
|
|
for (let btnSquareButton of btnSquareClass) { |
|
btnSquareButton.addEventListener('click', async (evt) => { |
|
|
|
setTimeout(() => { |
|
try { |
|
evt.target.blur() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}, 500) |
|
}) |
|
} |
|
|
|
btnRefreshModelsClass = document.getElementsByClassName('btnRefreshModels') |
|
|
|
for (let btnRefreshModel of btnRefreshModelsClass) { |
|
btnRefreshModel.addEventListener('click', async (evt) => { |
|
|
|
setTimeout(() => { |
|
try { |
|
evt.target.blur() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}, 500) |
|
}) |
|
} |
|
|
|
document.addEventListener('mouseenter', async (event) => { |
|
try { |
|
|
|
|
|
if ( |
|
g_generation_session.isActive() && |
|
g_generation_session.mode === g_sd_mode |
|
) { |
|
|
|
|
|
console.log('hover on window') |
|
|
|
const new_selection = await psapi.getSelectionInfoExe() |
|
|
|
if ( |
|
new_selection && |
|
(await hasSelectionChanged( |
|
new_selection, |
|
g_generation_session.selectionInfo |
|
)) |
|
) { |
|
|
|
|
|
await calcWidthHeightFromSelection() |
|
|
|
if ( |
|
g_generation_session.state === |
|
session.SessionState['Active'] |
|
) { |
|
|
|
|
|
|
|
const selected_mode = html_manip.getMode() |
|
g_ui.generateModeUI(selected_mode) |
|
} else { |
|
|
|
} |
|
} else { |
|
|
|
|
|
|
|
g_ui.generateMoreUI() |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function displayUpdate() { |
|
try { |
|
sd_tab.displayImageCfgScaleSlider(g_sd_mode) |
|
if (g_sd_mode == 'txt2img') { |
|
document.getElementById('slDenoisingStrength').style.display = |
|
'none' |
|
|
|
document.getElementById('init_image_container').style.display = |
|
'none' |
|
document.getElementById('init_image_mask_container').style.display = |
|
'none' |
|
document.getElementById('slInpainting_fill').style.display = 'none' |
|
document.getElementById('chInpaintFullRes').style.display = 'none' |
|
|
|
document.getElementById('slInpaintingMaskWeight').style.display = |
|
'none' |
|
|
|
document.getElementById('chHiResFixs').style.display = 'flex' |
|
if (html_manip.getHiResFixs()) { |
|
document.getElementById('HiResDiv').style.display = 'block' |
|
} |
|
|
|
document.getElementById('slMaskExpansion').style.display = 'none' |
|
|
|
document.getElementById('slInpaintPadding').style.display = 'none' |
|
document.getElementById('slMaskBlur').style.display = 'none' |
|
|
|
|
|
} |
|
|
|
if (g_sd_mode == 'img2img') { |
|
document.getElementById('slDenoisingStrength').style.display = |
|
'block' |
|
document.getElementById('slMaskExpansion').style.display = 'none' |
|
|
|
document.getElementById('init_image_container').style.display = |
|
'block' |
|
|
|
document.getElementById('init_image_mask_container').style.display = |
|
'none' |
|
document.getElementById('slInpainting_fill').style.display = 'none' |
|
|
|
document.getElementById('HiResDiv').style.display = 'none' |
|
document.getElementById('chInpaintFullRes').style.display = 'none' |
|
document.getElementById('slInpaintPadding').style.display = 'none' |
|
document.getElementById('slMaskBlur').style.display = 'none' |
|
document.getElementById('chHiResFixs').style.display = 'none' |
|
document.getElementById('slInpaintingMaskWeight').style.display = |
|
'block' |
|
|
|
|
|
} |
|
|
|
if (g_sd_mode == 'inpaint' || g_sd_mode == 'outpaint') { |
|
|
|
|
|
document.getElementById('tableInitImageContainer').style.display = |
|
'none' |
|
document.getElementById('slMaskExpansion').style.display = 'block' |
|
setTimeout(() => { |
|
try { |
|
document.getElementById( |
|
'tableInitImageContainer' |
|
).style.display = 'table' |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}, 100) |
|
|
|
document.getElementById('slDenoisingStrength').style.display = |
|
'block' |
|
document.getElementById('init_image_mask_container').style.display = |
|
'block' |
|
document.getElementById('slInpainting_fill').style.display = 'block' |
|
document.getElementById('init_image_container').style.display = |
|
'block' |
|
document.getElementById('slInpaintingMaskWeight').style.display = |
|
'block' |
|
|
|
document.getElementById('HiResDiv').style.display = 'none' |
|
document.getElementById('chInpaintFullRes').style.display = |
|
'inline-flex' |
|
if (document.getElementById('chInpaintFullRes').checked) { |
|
document.getElementById('slInpaintPadding').style.display = |
|
'block' |
|
} else { |
|
document.getElementById('slInpaintPadding').style.display = |
|
'none' |
|
} |
|
document.getElementById('slMaskBlur').style.display = 'block' |
|
document.getElementById('chHiResFixs').style.display = 'none' |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
} |
|
|
|
if (g_generation_session.isActive()) { |
|
|
|
|
|
|
|
if (g_generation_session.mode !== g_sd_mode) { |
|
|
|
|
|
const generate_btns = Array.from( |
|
document.getElementsByClassName('btnGenerateClass') |
|
) |
|
generate_btns.forEach((element) => { |
|
const selected_mode = |
|
getCurrentGenerationModeByValue(g_sd_mode) |
|
element.textContent = `Generate ${selected_mode}` |
|
}) |
|
|
|
html_manip.setGenerateButtonsColor('generate', 'generate-more') |
|
} else { |
|
|
|
|
|
|
|
if (!(await hasSessionSelectionChanged())) { |
|
|
|
|
|
const generate_btns = Array.from( |
|
document.getElementsByClassName('btnGenerateClass') |
|
) |
|
|
|
|
|
|
|
const generation_mode = g_generation_session.mode |
|
const generation_name = |
|
getCurrentGenerationModeByValue(generation_mode) |
|
generate_btns.forEach((element) => { |
|
element.textContent = `Generate More ${generation_name}` |
|
}) |
|
|
|
html_manip.setGenerateButtonsColor( |
|
'generate-more', |
|
'generate' |
|
) |
|
} else { |
|
|
|
} |
|
} |
|
} else { |
|
|
|
const selected_mode = getCurrentGenerationModeByValue(g_sd_mode) |
|
g_ui.setGenerateBtnText(`Generate ${selected_mode}`) |
|
html_manip.setGenerateButtonsColor('generate', 'generate-more') |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function selectTool() { |
|
var doc = app.activeDocument |
|
var activeTool = app.currentTool |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.dir(app, { depth: null }) |
|
console.log('hello this is Abdullah') |
|
document.getElementById('layers').innerHTML = `<span> |
|
selectTool was called, ${activeTool} |
|
</span>` |
|
|
|
|
|
|
|
} |
|
|
|
async function testServerPath() { |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
const serverPath = 'http://127.0.0.1:8000/txt2img/random%20prompt' |
|
|
|
console.log('testServerPath function was called') |
|
|
|
let response = await fetch(serverPath) |
|
console.log('testServerPath finished fetch') |
|
|
|
if (!response.ok) { |
|
throw new Error( |
|
`HTTP error fetching weather station; status: ${response.status}` |
|
) |
|
} |
|
let stationJson = await response.json() |
|
console.dir(stationJson) |
|
} catch (err) { |
|
console.error('testServerPath error: ' + err.message) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
async function fillImage() { |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
let imageFile = await fs.getFileForOpening({ |
|
types: storage.fileTypes.images, |
|
}) |
|
|
|
|
|
const ImageFill = require('scenegraph').ImageFill |
|
let fill = new ImageFill(imageFile) |
|
|
|
|
|
selection.items[0].fill = fill |
|
} |
|
|
|
|
|
function pastImage2Layer() { |
|
const { batchPlay } = require('photoshop').action |
|
const { executeAsModal } = require('photoshop').core |
|
|
|
executeAsModal( |
|
() => { |
|
|
|
const result = batchPlay( |
|
[ |
|
{ |
|
_obj: 'paste', |
|
antiAlias: { |
|
_enum: 'antiAliasType', |
|
_value: 'antiAliasNone', |
|
}, |
|
as: { |
|
_class: 'pixel', |
|
}, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{ |
|
synchronousExecution: true, |
|
modalBehavior: 'fail', |
|
} |
|
) |
|
}, |
|
{ |
|
commandName: 'Create Label', |
|
} |
|
) |
|
} |
|
|
|
function sliderToResolution(sliderValue) { |
|
return sliderValue * 64 |
|
} |
|
|
|
|
|
document.querySelector('#hrHeight').addEventListener('input', (evt) => { |
|
hHeight = sliderToResolution(evt.target.value) |
|
document.querySelector('#hHeight').textContent = hHeight |
|
}) |
|
|
|
document.querySelector('#hrWidth').addEventListener('input', (evt) => { |
|
hWidth = sliderToResolution(evt.target.value) |
|
document.querySelector('#hWidth').textContent = hWidth |
|
}) |
|
|
|
|
|
document.querySelector('#slInpaintPadding').addEventListener('input', (evt) => { |
|
padding = evt.target.value * 4 |
|
document.querySelector('#lInpaintPadding').textContent = padding |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function snapAndFillHandler() { |
|
try { |
|
const isSelectionAreaValid = await psapi.checkIfSelectionAreaIsActive() |
|
if (isSelectionAreaValid) { |
|
if (g_generation_session.isFirstGeneration) { |
|
|
|
|
|
|
|
|
|
|
|
await executeAsModal( |
|
async () => { |
|
|
|
await outpaint.snapAndFillExe(random_session_id) |
|
}, |
|
{ commandName: 'Snap And Fill' } |
|
) |
|
|
|
|
|
|
|
|
|
} |
|
} else { |
|
psapi.promptForMarqueeTool() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function easyModeOutpaint() { |
|
try { |
|
if (g_generation_session.isFirstGeneration) { |
|
|
|
|
|
|
|
|
|
|
|
await outpaint.outpaintExe(random_session_id) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function btnInitInpaintHandler() { |
|
try { |
|
if (g_generation_session.isFirstGeneration) { |
|
|
|
|
|
|
|
|
|
await outpaint.inpaintFasterExe(random_session_id) |
|
|
|
|
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function toggleTwoButtonsByClass(isVisible, first_class, second_class) { |
|
const first_class_btns = Array.from( |
|
document.getElementsByClassName(first_class) |
|
) |
|
const second_class_btns = Array.from( |
|
document.getElementsByClassName(second_class) |
|
) |
|
|
|
if (isVisible) { |
|
|
|
first_class_btns.forEach((element) => (element.style.display = 'none')) |
|
second_class_btns.forEach( |
|
(element) => (element.style.display = 'inline-block') |
|
) |
|
|
|
} else { |
|
|
|
first_class_btns.forEach( |
|
(element) => (element.style.display = 'inline-block') |
|
) |
|
if (g_generation_session.isActive()) { |
|
|
|
|
|
const generation_mode = g_generation_session.mode |
|
const generation_name = |
|
getCurrentGenerationModeByValue(generation_mode) |
|
first_class_btns.forEach( |
|
(element) => |
|
(element.textContent = `Generate More ${generation_name}`) |
|
) |
|
} else { |
|
|
|
first_class_btns.forEach( |
|
(element) => |
|
(element.textContent = `Generate ${getCurrentGenerationModeByValue( |
|
g_sd_mode |
|
)}`) |
|
) |
|
} |
|
second_class_btns.forEach((element) => (element.style.display = 'none')) |
|
} |
|
return isVisible |
|
} |
|
|
|
async function discardAll() { |
|
|
|
|
|
try { |
|
for (const [path, viewer_image_obj] of Object.entries( |
|
g_viewer_manager.pathToViewerImage |
|
)) { |
|
try { |
|
viewer_image_obj.active(false) |
|
viewer_image_obj.setHighlight(false) |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
} |
|
await discard() |
|
await layer_util.deleteLayers([g_generation_session.outputGroup]) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function acceptAll() { |
|
|
|
|
|
try { |
|
for (const [path, viewer_image_obj] of Object.entries( |
|
g_viewer_manager.pathToViewerImage |
|
)) { |
|
try { |
|
if ( |
|
viewer_image_obj.isActive() && |
|
viewer_image_obj instanceof viewer.OutputImage |
|
) { |
|
|
|
|
|
if ( |
|
layer_util.Layer.doesLayerExist(viewer_image_obj.layer) |
|
) { |
|
await g_generation_session.moveToTopOfOutputGroup( |
|
viewer_image_obj.layer |
|
) |
|
} |
|
} |
|
viewer_image_obj.setHighlight(true) |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
} |
|
await discard() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function discardSelected() { |
|
|
|
|
|
try { |
|
for (const [path, viewer_image_obj] of Object.entries( |
|
g_viewer_manager.pathToViewerImage |
|
)) { |
|
try { |
|
if (viewer_image_obj.is_active) { |
|
viewer_image_obj.active(false) |
|
viewer_image_obj.setHighlight(true) |
|
} |
|
|
|
viewer_image_obj.toggleHighlight() |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
} |
|
await discard() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
const discard_selected_class_btns = Array.from( |
|
document.getElementsByClassName('discardSelectedClass') |
|
) |
|
|
|
discard_selected_class_btns.forEach((element) => |
|
element.addEventListener('click', async () => { |
|
try { |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['DiscardSelected'] |
|
) |
|
g_ui.onEndSessionUI() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
) |
|
|
|
|
|
const accept_selected_class_btns = Array.from( |
|
document.getElementsByClassName('acceptSelectedClass') |
|
) |
|
|
|
accept_selected_class_btns.forEach((element) => |
|
element.addEventListener('click', async () => { |
|
try { |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['AcceptSelected'] |
|
) |
|
g_ui.onEndSessionUI() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
) |
|
|
|
const accept_class_btns = Array.from( |
|
document.getElementsByClassName('acceptClass') |
|
) |
|
|
|
accept_class_btns.forEach((element) => |
|
element.addEventListener('click', async () => { |
|
try { |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Accept'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
|
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
) |
|
|
|
function toggleTwoButtons(defaultVal, first_btn_id, second_btn_id) { |
|
if (defaultVal) { |
|
document.getElementById(first_btn_id).style.display = 'none' |
|
document.getElementById(second_btn_id).style.display = 'inline-block' |
|
} else { |
|
document.getElementById(first_btn_id).style.display = 'inline-block' |
|
document.getElementById(second_btn_id).style.display = 'none' |
|
} |
|
return defaultVal |
|
} |
|
|
|
document.getElementById('btnRandomSeed').addEventListener('click', async () => { |
|
document.querySelector('#tiSeed').value = '-1' |
|
}) |
|
|
|
document.getElementById('btnLastSeed').addEventListener('click', async () => { |
|
try { |
|
console.log('click on Last seed') |
|
let seed = '-1' |
|
|
|
if (g_last_seed >= 0) { |
|
seed = g_last_seed.toString() |
|
} |
|
|
|
console.log('seed:', seed) |
|
document.querySelector('#tiSeed').value = seed |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
async function discard() { |
|
try { |
|
await executeAsModal(async () => { |
|
await psapi.selectLayersExe([g_generation_session.outputGroup]) |
|
g_generation_session.outputGroup.allLocked = false |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const random_img_src = 'https://source.unsplash.com/random' |
|
html_manip.setInitImageSrc(random_img_src) |
|
html_manip.setInitImageMaskSrc(random_img_src) |
|
|
|
|
|
await deleteNoneSelected(g_viewer_manager.pathToViewerImage) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
Array.from(document.getElementsByClassName('discardClass')).forEach( |
|
(element) => { |
|
element.addEventListener('click', async () => { |
|
|
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Discard'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
|
|
}) |
|
} |
|
) |
|
|
|
Array.from(document.getElementsByClassName('btnInterruptClass')).forEach( |
|
(element) => { |
|
element.addEventListener('click', async () => { |
|
try { |
|
if ( |
|
g_generation_session.request_status === |
|
Enum.RequestStateEnum['Finished'] |
|
) { |
|
toggleTwoButtonsByClass( |
|
false, |
|
'btnGenerateClass', |
|
'btnInterruptClass' |
|
) |
|
g_can_request_progress = false |
|
|
|
return null |
|
} |
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Interrupted'] |
|
|
|
g_batch_count_interrupt_status = true |
|
const backend_type = html_manip.getBackendType() |
|
|
|
if (backend_type === backendTypeEnum['HordeNative']) { |
|
|
|
|
|
await g_horde_generator.interrupt() |
|
} else { |
|
|
|
|
|
json = await sdapi.requestInterrupt() |
|
} |
|
|
|
toggleTwoButtonsByClass( |
|
false, |
|
'btnGenerateClass', |
|
'btnInterruptClass' |
|
) |
|
g_can_request_progress = false |
|
|
|
|
|
} catch (e) { |
|
|
|
toggleTwoButtonsByClass( |
|
false, |
|
'btnGenerateClass', |
|
'btnInterruptClass' |
|
) |
|
g_can_request_progress = false |
|
console.warn(e) |
|
} |
|
}) |
|
} |
|
) |
|
|
|
|
|
async function storeActiveLayers() { |
|
setTimeout(async () => { |
|
const layers = await app.activeDocument.activeLayers |
|
console.log('storeActiveLayers: ', layers.length) |
|
|
|
if (layers.length > 0) { |
|
g_saved_active_layers = layers |
|
await psapi.unselectActiveLayersExe() |
|
} |
|
}, 200) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
async function restoreActiveLayers() { |
|
const layers = await app.activeDocument.activeLayers |
|
console.log('restoreActiveLayers: ', layers.length) |
|
if (layers.length == 0) { |
|
await psapi.selectLayersExe(g_saved_active_layers) |
|
g_saved_active_layers = [] |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
async function storeActiveSelection() { |
|
try { |
|
setTimeout(async () => { |
|
const layers = await app.activeDocument.activeLayers |
|
const current_selection = await psapi.checkIfSelectionAreaIsActive() |
|
console.log('storeActiveSelection: ', current_selection) |
|
|
|
if (current_selection) { |
|
g_saved_active_selection = current_selection |
|
await psapi.unSelectMarqueeExe() |
|
} |
|
}, 200) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function restoreActiveSelection() { |
|
try { |
|
const current_selection = await psapi.checkIfSelectionAreaIsActive() |
|
|
|
console.log('restoreActiveSelection: ', current_selection) |
|
if ( |
|
!current_selection && |
|
psapi.isSelectionValid(g_saved_active_selection) |
|
) { |
|
await psapi.reSelectMarqueeExe(g_saved_active_selection) |
|
g_saved_active_selection = {} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document.querySelector('#taPrompt').addEventListener('focus', async () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
document.querySelector('#taPrompt').addEventListener('blur', async () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
document |
|
.querySelector('#taNegativePrompt') |
|
.addEventListener('focus', async () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
document |
|
.querySelector('#taNegativePrompt') |
|
.addEventListener('blur', async () => { |
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
function updateMetadata(new_metadata) { |
|
const metadatas = [] |
|
try { |
|
for (metadata of new_metadata) { |
|
metadata_json = JSON.parse(metadata) |
|
console.log('metadata_json:', metadata_json) |
|
metadatas.push(metadata_json) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
return metadatas |
|
} |
|
|
|
async function getSettings() { |
|
let payload = {} |
|
|
|
try { |
|
const extension_type = settings_tab.getExtensionType() |
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
payload['selection_info'] = selectionInfo |
|
const numberOfBatchSize = parseInt( |
|
document.querySelector('#tiNumberOfBatchSize').value |
|
) |
|
const numberOfSteps = document.querySelector('#tiNumberOfSteps').value |
|
const prompt = html_manip.getPrompt() |
|
const negative_prompt = html_manip.getNegativePrompt() |
|
const hi_res_fix = html_manip.getHiResFixs() |
|
|
|
|
|
const model_index = document.querySelector('#mModelsMenu').selectedIndex |
|
const upscaler = document.querySelector('#hrModelsMenu').value |
|
const cfg_scale = document.querySelector('#slCfgScale').value |
|
|
|
|
|
function calculateSeed(init_seed, batch_index, batch_size) { |
|
if (init_seed === -1) return -1 |
|
const seed = init_seed + batch_index * batch_size |
|
return seed |
|
} |
|
|
|
const init_seed = parseInt(document.querySelector('#tiSeed').value) |
|
const seed = calculateSeed( |
|
init_seed, |
|
g_current_batch_index, |
|
numberOfBatchSize |
|
) |
|
|
|
|
|
const use_sharp_mask = settings_tab.getUseSharpMask() |
|
const mask_blur = html_manip.getMaskBlur() |
|
const mask_expansion = document.getElementById('slMaskExpansion').value |
|
|
|
const inpaint_full_res_padding = |
|
document.querySelector('#slInpaintPadding').value |
|
|
|
|
|
const bUsePromptShortcut = document.getElementById( |
|
'chUsePromptShortcut' |
|
).checked |
|
let prompt_shortcut_ui_dict = {} |
|
try { |
|
let prompt_shortcut_string = |
|
document.getElementById('taPromptShortcut').value |
|
prompt_shortcut_ui_dict = JSON.parse(prompt_shortcut_string) |
|
} catch (e) { |
|
console.warn( |
|
`warning prompt_shortcut_ui_dict is not valid Json obj: ${e}` |
|
) |
|
prompt_shortcut_ui_dict = {} |
|
} |
|
|
|
|
|
|
|
const original_width = html_manip.getWidth() |
|
const original_height = html_manip.getHeight() |
|
|
|
const width = general.nearestMultiple(original_width, 64) |
|
const height = general.nearestMultiple(original_height, 64) |
|
|
|
const hWidth = html_manip.getSliderSdValue_Old('hrWidth', 64) |
|
const hHeight = html_manip.getSliderSdValue_Old('hrHeight', 64) |
|
const hSteps = html_manip.getSliderSdValue_Old('hrNumberOfSteps', 1) |
|
|
|
console.log('Check') |
|
|
|
const uniqueDocumentId = await getUniqueDocumentId() |
|
const h_denoising_strength = html_manip.getSliderSdValue_Old( |
|
'hrDenoisingStrength', |
|
0.01 |
|
) |
|
console.log('Check2') |
|
|
|
|
|
|
|
const sampler_name = html_manip.getCheckedSamplerName() |
|
|
|
const mode = html_manip.getMode() |
|
const b_restore_faces = |
|
document.getElementById('chRestoreFaces').checked |
|
|
|
let denoising_strength = h_denoising_strength |
|
if (mode == 'inpaint' || mode == 'outpaint') { |
|
var g_use_mask_image = true |
|
payload['inpaint_full_res'] = |
|
document.getElementById('chInpaintFullRes').checked |
|
payload['inpaint_full_res_padding'] = inpaint_full_res_padding * 4 |
|
|
|
console.log('g_use_mask_image is ', g_use_mask_image) |
|
console.log('g_init_image_mask_name is ', g_init_image_mask_name) |
|
payload['init_image_mask_name'] = g_init_image_mask_name |
|
payload['inpainting_fill'] = html_manip.getMaskContent() |
|
payload['mask_expansion'] = mask_expansion |
|
payload['mask'] = g_generation_session.activeBase64MaskImage |
|
|
|
if (use_sharp_mask === false && payload['mask']) { |
|
|
|
|
|
const iterations = payload['mask_expansion'] |
|
const mask = await py_re.maskExpansionRequest( |
|
payload['mask'], |
|
iterations |
|
) |
|
if (mask) { |
|
g_generation_session.base64maskExpansionImage = mask |
|
payload['mask'] = mask |
|
} |
|
} |
|
} else if (mode == 'img2img') { |
|
var g_use_mask_image = false |
|
delete payload['inpaint_full_res'] |
|
delete payload['inpaint_full_res_padding'] |
|
delete payload['init_image_mask_name'] |
|
delete payload['inpainting_fill'] |
|
} |
|
|
|
if ( |
|
g_sd_mode == 'img2img' || |
|
g_sd_mode == 'inpaint' || |
|
g_sd_mode == 'outpaint' |
|
) { |
|
console.log(`g_use_mask_image:? ${g_use_mask_image}`) |
|
|
|
denoising_strength = html_manip.getDenoisingStrength() |
|
payload['denoising_strength'] = denoising_strength |
|
payload['init_image_name'] = g_init_image_name |
|
|
|
payload['init_images'] = [ |
|
g_generation_session.activeBase64InitImage, |
|
] |
|
payload['image_cfg_scale'] = sd_tab.getImageCfgScaleSDValue() |
|
|
|
if ( |
|
scripts.script_store.is_active && |
|
scripts.script_store.selected_script_name !== 'None' && |
|
scripts.script_store.is_selected_script_available |
|
) { |
|
payload['script_args'] = scripts.script_store.orderedValues() |
|
|
|
payload['script_name'] = |
|
scripts.script_store.selected_script_name |
|
} |
|
} else { |
|
delete payload['script_args'] |
|
delete payload['script_name'] |
|
} |
|
|
|
if (hi_res_fix && width >= 512 && height >= 512) { |
|
const hr_scale = sd_tab.getHrScaleSliderSDValue() |
|
|
|
payload['enable_hr'] = hi_res_fix |
|
|
|
|
|
|
|
|
|
payload['hr_scale'] = hr_scale |
|
payload['hr_upscaler'] = upscaler |
|
payload['hr_second_pass_steps'] = hSteps |
|
} else { |
|
|
|
|
|
delete payload['enable_hr'] |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const backend_type = html_manip.getBackendType() |
|
if (backend_type === backendTypeEnum['Auto1111HordeExtension']) { |
|
payload['script_name'] = script_horde.script_name |
|
payload['script_args'] = script_horde.getScriptArgs() |
|
} else if ( |
|
payload['script_name'] === script_horde.script_name && |
|
backend_type !== backendTypeEnum['Auto1111HordeExtension'] |
|
) { |
|
delete payload['script_name'] |
|
delete payload['script_args'] |
|
} |
|
|
|
if (bUsePromptShortcut) { |
|
|
|
const [new_prompt, new_negative_prompt] = |
|
py_re.replacePromptsWithShortcuts( |
|
prompt, |
|
negative_prompt, |
|
prompt_shortcut_ui_dict |
|
) |
|
|
|
|
|
payload['prompt'] = new_prompt |
|
payload['negative_prompt'] = new_negative_prompt |
|
|
|
|
|
payload['original_prompt'] = prompt |
|
payload['original_negative_prompt'] = negative_prompt |
|
} else { |
|
|
|
payload['prompt'] = prompt |
|
payload['negative_prompt'] = negative_prompt |
|
|
|
payload['original_prompt'] = prompt |
|
payload['original_negative_prompt'] = negative_prompt |
|
} |
|
|
|
payload = { |
|
...payload, |
|
|
|
|
|
steps: numberOfSteps, |
|
|
|
sampler_index: sampler_name, |
|
width: width, |
|
height: height, |
|
denoising_strength: denoising_strength, |
|
batch_size: numberOfBatchSize, |
|
cfg_scale: cfg_scale, |
|
seed: seed, |
|
mask_blur: mask_blur, |
|
use_sharp_mask: use_sharp_mask, |
|
use_prompt_shortcut: bUsePromptShortcut, |
|
prompt_shortcut_ui_dict: prompt_shortcut_ui_dict, |
|
uniqueDocumentId: uniqueDocumentId, |
|
mode: mode, |
|
restore_faces: b_restore_faces, |
|
|
|
|
|
} |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
return payload |
|
} |
|
|
|
async function getExtraSettings() { |
|
let payload = {} |
|
try { |
|
const html_manip = require('./utility/html_manip') |
|
const upscaling_resize = html_manip.getUpscaleSize() |
|
const gfpgan_visibility = html_manip.getGFPGANVisibility() |
|
const codeformer_visibility = html_manip.getCodeFormerVisibility() |
|
const codeformer_weight = html_manip.getCodeFormerWeight() |
|
const selection_info = await psapi.getSelectionInfoExe() |
|
const width = selection_info.width * upscaling_resize |
|
const height = selection_info.height * upscaling_resize |
|
|
|
|
|
payload['resize_mode'] = 0 |
|
payload['show_extras_results'] = 0 |
|
payload['gfpgan_visibility'] = gfpgan_visibility |
|
payload['codeformer_visibility'] = codeformer_visibility |
|
payload['codeformer_weight'] = codeformer_weight |
|
payload['upscaling_resize'] = upscaling_resize |
|
payload['upscaling_resize_w'] = width |
|
payload['upscaling_resize_h'] = height |
|
payload['upscaling_crop'] = true |
|
const upscaler1 = document.querySelector('#hrModelsMenuUpscale1').value |
|
payload['upscaler_1'] = upscaler1 === undefined ? 'None' : upscaler1 |
|
const upscaler2 = document.querySelector('#hrModelsMenuUpscale2').value |
|
payload['upscaler_2'] = upscaler2 === undefined ? 'None' : upscaler2 |
|
const extras_upscaler_2_visibility = html_manip.getUpscaler2Visibility() |
|
payload['extras_upscaler_2_visibility'] = extras_upscaler_2_visibility |
|
payload['upscale_first'] = false |
|
const uniqueDocumentId = await getUniqueDocumentId() |
|
payload['uniqueDocumentId'] = uniqueDocumentId |
|
|
|
|
|
const layer = await app.activeDocument.activeLayers[0] |
|
const old_name = layer.name |
|
|
|
|
|
|
|
|
|
let image_name = psapi.layerNameToFileName( |
|
old_name, |
|
layer.id, |
|
random_session_id |
|
) |
|
image_name = `${image_name}.png` |
|
|
|
const base64_image = g_generation_session.activeBase64InitImage |
|
|
|
payload['image'] = base64_image |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
return payload |
|
} |
|
|
|
async function getExtraSettings() { |
|
let payload = {} |
|
try { |
|
const html_manip = require('./utility/html_manip') |
|
const upscaling_resize = html_manip.getUpscaleSize() |
|
const gfpgan_visibility = html_manip.getGFPGANVisibility() |
|
const codeformer_visibility = html_manip.getCodeFormerVisibility() |
|
const codeformer_weight = html_manip.getCodeFormerWeight() |
|
const selection_info = await psapi.getSelectionInfoExe() |
|
const width = selection_info.width * upscaling_resize |
|
const height = selection_info.height * upscaling_resize |
|
|
|
|
|
payload['resize_mode'] = 0 |
|
payload['show_extras_results'] = 0 |
|
payload['gfpgan_visibility'] = gfpgan_visibility |
|
payload['codeformer_visibility'] = codeformer_visibility |
|
payload['codeformer_weight'] = codeformer_weight |
|
payload['upscaling_resize'] = upscaling_resize |
|
payload['upscaling_resize_w'] = width |
|
payload['upscaling_resize_h'] = height |
|
payload['upscaling_crop'] = true |
|
const upscaler1 = document.querySelector('#hrModelsMenuUpscale1').value |
|
payload['upscaler_1'] = upscaler1 === undefined ? 'None' : upscaler1 |
|
const upscaler2 = document.querySelector('#hrModelsMenuUpscale2').value |
|
payload['upscaler_2'] = upscaler2 === undefined ? 'None' : upscaler2 |
|
const extras_upscaler_2_visibility = html_manip.getUpscaler2Visibility() |
|
payload['extras_upscaler_2_visibility'] = extras_upscaler_2_visibility |
|
payload['upscale_first'] = false |
|
const uniqueDocumentId = await getUniqueDocumentId() |
|
payload['uniqueDocumentId'] = uniqueDocumentId |
|
|
|
|
|
const layer = await app.activeDocument.activeLayers[0] |
|
const old_name = layer.name |
|
|
|
|
|
|
|
|
|
let image_name = psapi.layerNameToFileName( |
|
old_name, |
|
layer.id, |
|
random_session_id |
|
) |
|
image_name = `${image_name}.png` |
|
|
|
const base64_image = g_generation_session.activeBase64InitImage |
|
|
|
payload['image'] = base64_image |
|
} catch (e) { |
|
console.error(e) |
|
} |
|
return payload |
|
} |
|
|
|
async function generateImg2Img(settings) { |
|
let json = {} |
|
try { |
|
const backend_type = html_manip.getBackendType() |
|
if (backend_type === backendTypeEnum['HordeNative']) { |
|
json = await g_horde_generator.generate() |
|
|
|
|
|
} else if ( |
|
backend_type === backendTypeEnum['Auto1111'] || |
|
backend_type === backendTypeEnum['Auto1111HordeExtension'] |
|
) { |
|
|
|
const b_enable_control_net = control_net.isControlNetModeEnable() |
|
|
|
if (b_enable_control_net) { |
|
|
|
json = await sdapi.requestControlNetImg2Img(settings) |
|
} else { |
|
json = await sdapi.requestImg2Img(settings) |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
json = {} |
|
} |
|
|
|
return json |
|
} |
|
|
|
async function generateTxt2Img(settings) { |
|
let json = {} |
|
try { |
|
const backend_type = html_manip.getBackendType() |
|
if (backend_type === backendTypeEnum['HordeNative']) { |
|
json = await g_horde_generator.generate() |
|
|
|
|
|
} else if ( |
|
backend_type === backendTypeEnum['Auto1111'] || |
|
backend_type === backendTypeEnum['Auto1111HordeExtension'] |
|
) { |
|
|
|
const b_enable_control_net = control_net.isControlNetModeEnable() |
|
|
|
if (b_enable_control_net) { |
|
|
|
|
|
json = await sdapi.requestControlNetTxt2Img(settings) |
|
} else { |
|
json = await sdapi.requestTxt2Img(settings) |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
json = {} |
|
} |
|
|
|
return json |
|
} |
|
|
|
async function hasSelectionChanged(new_selection, old_selection) { |
|
if ( |
|
new_selection.left === old_selection.left && |
|
new_selection.bottom === old_selection.bottom && |
|
new_selection.right === old_selection.right && |
|
new_selection.top === old_selection.top |
|
) { |
|
return false |
|
} else { |
|
return true |
|
} |
|
} |
|
|
|
async function easyModeGenerate(mode) { |
|
try { |
|
if ( |
|
g_generation_session.request_status !== |
|
Enum.RequestStateEnum['Finished'] |
|
) { |
|
app.showAlert( |
|
'A generation is still active in the background. \nPlease check your Automatic1111 command line.' |
|
) |
|
return null |
|
} |
|
|
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Generating'] |
|
await executeAsModal(async (context) => { |
|
const document_type = await findDocumentType() |
|
|
|
const history_id = await context.hostControl.suspendHistory({ |
|
documentID: app.activeDocument.id, |
|
name: 'Correct Background', |
|
}) |
|
|
|
|
|
const selectionInfo = await psapi.getSelectionInfoExe() |
|
await psapi.unSelectMarqueeExe() |
|
const active_layers = app.activeDocument.activeLayers |
|
|
|
|
|
|
|
await correctDocumentType(document_type) |
|
|
|
|
|
|
|
await psapi.reSelectMarqueeExe(selectionInfo) |
|
await psapi.selectLayersExe(active_layers) |
|
await context.hostControl.resumeHistory(history_id) |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const backend_type = html_manip.getBackendType() |
|
if ( |
|
backend_type === backendTypeEnum['Auto1111'] || |
|
backend_type === backendTypeEnum['Auto1111HordeExtension'] |
|
) { |
|
g_automatic_status = await checkAutoStatus() |
|
await displayNotification(g_automatic_status) |
|
if ( |
|
g_automatic_status === Enum.AutomaticStatusEnum['Offline'] || |
|
g_automatic_status === Enum.AutomaticStatusEnum['RunningNoApi'] |
|
) { |
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Finished'] |
|
return false |
|
} |
|
} |
|
|
|
let active_layer = await app.activeDocument.activeLayers[0] |
|
|
|
const isSelectionAreaValid = await psapi.checkIfSelectionAreaIsActive() |
|
if ( |
|
!isSelectionAreaValid && |
|
(await note.Notification.inactiveSelectionArea( |
|
g_generation_session.isActive() |
|
)) === false |
|
) { |
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Finished'] |
|
return null |
|
} |
|
|
|
console.log('easyModeGenerate mdoe: ', mode) |
|
if (psapi.isSelectionValid(g_generation_session.selectionInfo)) { |
|
|
|
const new_selection = await psapi.getSelectionInfoExe() |
|
if ( |
|
await hasSelectionChanged( |
|
new_selection, |
|
g_generation_session.selectionInfo |
|
) |
|
) { |
|
|
|
|
|
g_generation_session.selectionInfo = new_selection |
|
try { |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Accept'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
|
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
} else { |
|
|
|
g_generation_session.selectionInfo = |
|
await psapi.getSelectionInfoExe() |
|
} |
|
|
|
if (g_generation_session.isActive()) { |
|
|
|
|
|
if (g_generation_session.mode !== mode) { |
|
|
|
|
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Accept'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
await g_generation_session.startSession() |
|
|
|
g_generation_session.mode = mode |
|
} |
|
} else { |
|
|
|
g_generation_session.mode = mode |
|
await g_generation_session.startSession() |
|
} |
|
|
|
await psapi.selectLayersExe([active_layer]) |
|
if (mode === 'txt2img') { |
|
|
|
} else if (mode === 'img2img' || mode === 'upscale') { |
|
await snapAndFillHandler() |
|
} else if (mode === 'inpaint') { |
|
await btnInitInpaintHandler() |
|
} else if (mode === 'outpaint') { |
|
await easyModeOutpaint() |
|
} |
|
|
|
|
|
|
|
|
|
await g_generation_session.closePreviousOutputGroup() |
|
|
|
const settings = |
|
mode === 'upscale' ? await getExtraSettings() : await getSettings() |
|
|
|
g_generation_session.last_settings = settings |
|
|
|
g_generation_session.is_control_net = |
|
control_net.isControlNetModeEnable() |
|
|
|
await generate(settings, mode) |
|
|
|
|
|
await g_generation_session.deleteProgressImage() |
|
} catch (e) { |
|
await g_generation_session.deleteProgressImage() |
|
console.warn(e) |
|
g_generation_session.request_status = Enum.RequestStateEnum['Finished'] |
|
} |
|
toggleTwoButtonsByClass(false, 'btnGenerateClass', 'btnInterruptClass') |
|
g_can_request_progress = false |
|
|
|
g_generation_session.request_status = Enum.RequestStateEnum['Finished'] |
|
|
|
if (g_generation_session.sudo_timer_id) { |
|
|
|
g_generation_session.sudo_timer_id = clearInterval( |
|
g_generation_session.sudo_timer_id |
|
) |
|
} |
|
} |
|
|
|
|
|
async function generate(settings, mode) { |
|
try { |
|
|
|
|
|
|
|
|
|
|
|
const isFirstGeneration = g_generation_session.isFirstGeneration |
|
|
|
|
|
g_generation_session.activate() |
|
|
|
g_ui.onStartSessionUI() |
|
|
|
toggleTwoButtonsByClass(true, 'btnGenerateClass', 'btnInterruptClass') |
|
g_can_request_progress = true |
|
|
|
|
|
if ( |
|
html_manip.getBackendType() !== backendTypeEnum['HordeNative'] |
|
) { |
|
setTimeout(async function () { |
|
|
|
await progressRecursive() |
|
}, 2000) |
|
} |
|
|
|
if ( |
|
html_manip.getBackendType() === backendTypeEnum['Auto1111'] && |
|
g_generation_session.is_control_net |
|
) { |
|
g_generation_session.sudo_timer_id = general.sudoTimer() |
|
} |
|
|
|
console.log(settings) |
|
|
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Generating'] |
|
let json = {} |
|
if (mode == 'txt2img') { |
|
json = await generateTxt2Img(settings) |
|
} else if ( |
|
mode == 'img2img' || |
|
mode == 'inpaint' || |
|
mode == 'outpaint' |
|
) { |
|
|
|
|
|
json = await generateImg2Img(settings) |
|
} else if (mode == 'upscale') { |
|
json = await sdapi.requestExtraSingleImage(settings) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
g_generation_session.request_status === |
|
Enum.RequestStateEnum['Interrupted'] |
|
) { |
|
|
|
html_manip.updateProgressBarsHtml(0) |
|
console.log( |
|
'before delete g_generation_session.progress_layer: ', |
|
g_generation_session.progress_layer |
|
) |
|
await g_generation_session.deleteProgressImage() |
|
console.log( |
|
'after delete g_generation_session.progress_layer: ', |
|
g_generation_session.progress_layer |
|
) |
|
|
|
|
|
if (isFirstGeneration) { |
|
await loadViewerImages() |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Discard'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
|
|
} |
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Finished'] |
|
return null |
|
} |
|
|
|
|
|
if (Object.keys(json).length === 0) { |
|
if (isFirstGeneration) { |
|
await g_generation_session.endSession( |
|
session.GarbageCollectionState['Discard'] |
|
) |
|
g_ui.onEndSessionUI() |
|
|
|
|
|
} |
|
g_generation_session.request_status = |
|
Enum.RequestStateEnum['Finished'] |
|
return null |
|
} |
|
|
|
|
|
|
|
|
|
|
|
g_last_seed = json.images_info[0]?.auto_metadata?.Seed |
|
|
|
|
|
|
|
toggleTwoButtonsByClass(false, 'btnGenerateClass', 'btnInterruptClass') |
|
g_can_request_progress = false |
|
html_manip.updateProgressBarsHtml(0) |
|
|
|
const images_info = json?.images_info |
|
|
|
|
|
|
|
|
|
|
|
if (isFirstGeneration) { |
|
|
|
|
|
g_generation_session.image_paths_to_layers = |
|
await silentImagesToLayersExe(images_info) |
|
|
|
g_generation_session.base64OutputImages = {} |
|
for (const image_info of images_info) { |
|
const path = image_info['path'] |
|
const base64_image = image_info['base64'] |
|
g_generation_session.base64OutputImages[path] = base64_image |
|
const [document_name, image_name] = path.split('/') |
|
await saveFileInSubFolder( |
|
base64_image, |
|
document_name, |
|
image_name |
|
) |
|
const json_file_name = `${image_name.split('.')[0]}.json` |
|
settings['auto_metadata'] = image_info?.auto_metadata |
|
await saveJsonFileInSubFolder( |
|
settings, |
|
document_name, |
|
json_file_name |
|
) |
|
} |
|
|
|
g_number_generation_per_session = 1 |
|
g_generation_session.isFirstGeneration = false |
|
} else { |
|
|
|
|
|
let last_images_paths = await silentImagesToLayersExe(images_info) |
|
|
|
for (const image_info of images_info) { |
|
const path = image_info['path'] |
|
const base64_image = image_info['base64'] |
|
g_generation_session.base64OutputImages[path] = base64_image |
|
const [document_name, image_name] = path.split('/') |
|
await saveFileInSubFolder( |
|
base64_image, |
|
document_name, |
|
image_name |
|
) |
|
const json_file_name = `${image_name.split('.')[0]}.json` |
|
settings['auto_metadata'] = image_info?.auto_metadata |
|
await saveJsonFileInSubFolder( |
|
settings, |
|
document_name, |
|
json_file_name |
|
) |
|
} |
|
|
|
g_generation_session.image_paths_to_layers = { |
|
...g_generation_session.image_paths_to_layers, |
|
...last_images_paths, |
|
} |
|
g_number_generation_per_session++ |
|
} |
|
await psapi.reSelectMarqueeExe(g_generation_session.selectionInfo) |
|
|
|
await loadViewerImages() |
|
|
|
|
|
updateProgressBarsHtml(0) |
|
} catch (e) { |
|
console.error(`btnGenerate.click(): `, e) |
|
g_generation_session.request_status = Enum.RequestStateEnum['Finished'] |
|
} |
|
g_generation_session.request_status = Enum.RequestStateEnum['Finished'] |
|
} |
|
|
|
Array.from(document.getElementsByClassName('btnGenerateClass')).forEach( |
|
(btn) => { |
|
btn.addEventListener('click', async (evt) => { |
|
tempDisableElement(evt.target, 5000) |
|
const numberOfBatchCount = parseInt( |
|
document.querySelector('#tiNumberOfBatchCount').value |
|
) |
|
for (let i = 0; i < numberOfBatchCount; i++) { |
|
if (g_batch_count_interrupt_status === true) { |
|
break |
|
} |
|
g_current_batch_index = i |
|
await easyModeGenerate(g_sd_mode) |
|
} |
|
g_batch_count_interrupt_status = false |
|
g_current_batch_index = 0 |
|
}) |
|
} |
|
) |
|
|
|
document |
|
.getElementById('btnRefreshModels') |
|
.addEventListener('click', async (e) => { |
|
await refreshUI() |
|
await sd_tab.refreshSDTab() |
|
tempDisableElement(e.target, 3000) |
|
}) |
|
|
|
document.querySelector('#mModelsMenu').addEventListener('change', (evt) => { |
|
const model_index = evt.target.selectedIndex |
|
console.log(`Selected item: ${evt.target.selectedIndex}`) |
|
let model = g_models[0] |
|
if (model_index < g_models.length) { |
|
model = g_models[model_index] |
|
} |
|
|
|
g_model_title = model.title |
|
console.log('g_model_title: ', g_model_title) |
|
sdapi.requestSwapModel(g_model_title) |
|
}) |
|
|
|
document |
|
.getElementById('btnLayerToSelection') |
|
.addEventListener('click', async () => { |
|
try { |
|
const isSelectionAreaValid = |
|
await psapi.checkIfSelectionAreaIsActive() |
|
if (isSelectionAreaValid) { |
|
const validSelection = isSelectionAreaValid |
|
await psapi.layerToSelection(validSelection) |
|
} else { |
|
await psapi.promptForMarqueeTool() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
document |
|
.getElementById('btnSetInitImageViewer') |
|
.addEventListener('click', async () => { |
|
|
|
const layer = await app.activeDocument.activeLayers[0] |
|
const image_info = await psapi.silentSetInitImage( |
|
layer, |
|
random_session_id |
|
) |
|
const image_name = image_info['name'] |
|
const path = `./server/python_server/init_images/${image_name}` |
|
g_viewer_manager.addInitImageLayers(layer, path, false) |
|
await g_viewer_manager.loadInitImageViewerObject(path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
async function setMaskViewer() { |
|
try { |
|
await executeAsModal(async () => { |
|
if (g_viewer_manager.mask_solid_background) { |
|
g_viewer_manager.mask_solid_background.visible = true |
|
} |
|
}) |
|
const layer = g_viewer_manager.maskGroup |
|
|
|
const mask_info = await psapi.silentSetInitImageMask( |
|
layer, |
|
random_session_id |
|
) |
|
const image_name = mask_info['name'] |
|
const path = `./server/python_server/init_images/${image_name}` |
|
g_viewer_manager.addMaskLayers(layer, path, false, mask_info['base64']) |
|
await psapi.unselectActiveLayersExe() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document |
|
.getElementById('btnSetMaskViewer') |
|
.addEventListener('click', async () => { |
|
await setMaskViewer() |
|
}) |
|
|
|
|
|
function moveElementToAnotherTab(elementId, newParentId) { |
|
const element = document.getElementById(elementId) |
|
document.getElementById(newParentId).appendChild(element) |
|
} |
|
|
|
|
|
|
|
function updateProgressBarsHtml(new_value) { |
|
document.querySelectorAll('.pProgressBars').forEach((el) => { |
|
|
|
|
|
el.setAttribute('value', new_value) |
|
}) |
|
document.querySelectorAll('.lProgressLabel').forEach((el) => { |
|
console.log('updateProgressBarsHtml: ', new_value) |
|
if (new_value > 0) el.innerHTML = 'In progress...' |
|
else el.innerHTML = 'No work in progress' |
|
}) |
|
|
|
} |
|
|
|
async function updateProgressImage(progress_base64) { |
|
try { |
|
await executeAsModal(async (context) => { |
|
const history_id = await context.hostControl.suspendHistory({ |
|
documentID: app.activeDocument.id, |
|
name: 'Progress Image', |
|
}) |
|
await g_generation_session.deleteProgressLayer() |
|
|
|
|
|
const selection_info = await g_generation_session.selectionInfo |
|
const b_exsit = layer_util.Layer.doesLayerExist( |
|
g_generation_session.progress_layer |
|
) |
|
if (!b_exsit) { |
|
const layer = await io.IO.base64ToLayer( |
|
progress_base64, |
|
'temp_progress_image.png', |
|
selection_info.left, |
|
selection_info.top, |
|
selection_info.width, |
|
selection_info.height |
|
) |
|
g_generation_session.progress_layer = layer |
|
} else { |
|
|
|
await layer_util.deleteLayers([ |
|
g_generation_session.progress_layer, |
|
]) |
|
} |
|
await context.hostControl.resumeHistory(history_id) |
|
}) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function progressRecursive() { |
|
try { |
|
let json = await sdapi.requestProgress() |
|
|
|
const progress_value = json.progress * 100 |
|
if (g_generation_session.sudo_timer_id) { |
|
|
|
|
|
|
|
if (progress_value > 1) { |
|
|
|
g_generation_session.sudo_timer_id = clearInterval( |
|
g_generation_session.sudo_timer_id |
|
) |
|
} |
|
} else { |
|
|
|
|
|
html_manip.updateProgressBarsHtml(progress_value) |
|
} |
|
|
|
if ( |
|
json?.current_image && |
|
g_generation_session.request_status === |
|
Enum.RequestStateEnum['Generating'] |
|
) { |
|
const base64_url = general.base64ToBase64Url(json.current_image) |
|
|
|
const progress_image_html = document.getElementById('progressImage') |
|
const container_width = document.querySelector( |
|
'#divProgressImageViewerContainer' |
|
).offsetWidth |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
html_manip.setProgressImageSrc(base64_url) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
g_generation_session.last_settings.batch_size === 1 && |
|
settings_tab.getUseLiveProgressImage() |
|
) { |
|
|
|
|
|
await updateProgressImage(json.current_image) |
|
} |
|
} |
|
if (g_generation_session.isActive() && g_can_request_progress == true) { |
|
|
|
setTimeout(async () => { |
|
await progressRecursive() |
|
}, 1000) |
|
} |
|
} catch (e) { |
|
if ( |
|
g_generation_session.isActive() && |
|
g_can_request_progress === true |
|
) { |
|
setTimeout(async () => { |
|
await progressRecursive() |
|
}, 1000) |
|
} |
|
} |
|
} |
|
|
|
function changeImage() { |
|
let img = document.getElementById('img1') |
|
img.src = 'https://source.unsplash.com/random' |
|
} |
|
|
|
|
|
|
|
async function imageToSmartObject() { |
|
const { batchPlay } = require('photoshop').action |
|
const { executeAsModal } = require('photoshop').core |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
await executeAsModal( |
|
async () => { |
|
console.log('imageToSmartObject():') |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
let pluginFolder = await fs.getPluginFolder() |
|
let img = await pluginFolder.getEntry( |
|
'output- 1672730735.1670313.png' |
|
) |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'placeEvent', |
|
ID: 95, |
|
null: { |
|
_path: img, |
|
_kind: 'local', |
|
}, |
|
freeTransformCenterState: { |
|
_enum: 'quadCenterState', |
|
_value: 'QCSAverage', |
|
}, |
|
offset: { |
|
_obj: 'offset', |
|
horizontal: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
vertical: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
}, |
|
replaceLayer: { |
|
_obj: 'placeEvent', |
|
from: { |
|
_ref: 'layer', |
|
_id: 56, |
|
}, |
|
to: { |
|
_ref: 'layer', |
|
_id: 70, |
|
}, |
|
}, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{ |
|
synchronousExecution: true, |
|
modalBehavior: 'execute', |
|
} |
|
) |
|
}, |
|
{ |
|
commandName: 'Create Label', |
|
} |
|
) |
|
} catch (e) { |
|
console.log('imageToSmartObject() => error: ') |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
async function placeEmbedded(image_name, dir_entery) { |
|
|
|
|
|
try { |
|
|
|
|
|
const formats = require('uxp').storage.formats |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
|
|
|
|
|
|
|
|
let image_dir = dir_entery |
|
|
|
|
|
|
|
|
|
|
|
|
|
const file = await image_dir.createFile(image_name, { overwrite: true }) |
|
|
|
const img = await file.read({ format: formats.binary }) |
|
const token = await storage.localFileSystem.createSessionToken(file) |
|
let place_event_result |
|
await executeAsModal(async () => { |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'placeEvent', |
|
ID: 6, |
|
null: { |
|
_path: token, |
|
_kind: 'local', |
|
}, |
|
freeTransformCenterState: { |
|
_enum: 'quadCenterState', |
|
_value: 'QCSAverage', |
|
}, |
|
offset: { |
|
_obj: 'offset', |
|
horizontal: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
vertical: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
}, |
|
_isCommand: true, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{ |
|
synchronousExecution: true, |
|
modalBehavior: 'execute', |
|
} |
|
) |
|
console.log('placeEmbedd batchPlay result: ', result) |
|
|
|
place_event_result = result[0] |
|
}) |
|
|
|
return place_event_result |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
function _base64ToArrayBuffer(base64) { |
|
var binary_string = window.atob(base64) |
|
var len = binary_string.length |
|
var bytes = new Uint8Array(len) |
|
for (var i = 0; i < len; i++) { |
|
bytes[i] = binary_string.charCodeAt(i) |
|
} |
|
return bytes.buffer |
|
} |
|
|
|
function _arrayBufferToBase64(buffer) { |
|
var binary = '' |
|
var bytes = new Uint8Array(buffer) |
|
var len = bytes.byteLength |
|
for (var i = 0; i < len; i++) { |
|
binary += String.fromCharCode(bytes[i]) |
|
} |
|
return window.btoa(binary) |
|
} |
|
|
|
|
|
async function getDocFolder(doc_uuid) { |
|
try { |
|
|
|
const data_folder = await storage.localFileSystem.getDataFolder() |
|
|
|
let doc_folder |
|
try { |
|
doc_folder = await data_folder.getEntry(doc_uuid) |
|
} catch (e) { |
|
console.warn(e) |
|
|
|
doc_folder = await data_folder.createFolder(doc_uuid) |
|
} |
|
|
|
return doc_folder |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function getCurrentDocFolder() { |
|
|
|
const uuid = await getUniqueDocumentId() |
|
|
|
let doc_folder = await getDocFolder(uuid) |
|
return doc_folder |
|
} |
|
|
|
async function getInitImagesDir() { |
|
const uuid = await getUniqueDocumentId() |
|
|
|
let doc_folder = await getDocFolder(uuid) |
|
let init_folder |
|
try { |
|
init_folder = await doc_folder.getEntry('init_images') |
|
} catch (e) { |
|
console.warn(e) |
|
|
|
init_folder = await doc_folder.createFolder('init_images') |
|
} |
|
return init_folder |
|
} |
|
|
|
async function saveFileInSubFolder(b64Image, sub_folder_name, file_name) { |
|
|
|
|
|
|
|
const img = _base64ToArrayBuffer(b64Image) |
|
|
|
|
|
const img_name = file_name |
|
const folder = await storage.localFileSystem.getDataFolder() |
|
const documentFolderName = sub_folder_name |
|
let documentFolder |
|
try { |
|
documentFolder = await folder.getEntry(documentFolderName) |
|
} catch (e) { |
|
console.warn(e) |
|
|
|
documentFolder = await folder.createFolder(documentFolderName) |
|
} |
|
|
|
console.log('documentFolder.nativePath: ', documentFolder.nativePath) |
|
const file = await documentFolder.createFile(img_name, { overwrite: true }) |
|
|
|
await file.write(img, { format: storage.formats.binary }) |
|
|
|
const token = await storage.localFileSystem.createSessionToken(file) |
|
} |
|
|
|
async function saveJsonFileInSubFolder(json, sub_folder_name, file_name) { |
|
|
|
|
|
|
|
|
|
|
|
const json_file_name = file_name |
|
|
|
const folder = await storage.localFileSystem.getDataFolder() |
|
const documentFolderName = sub_folder_name |
|
let documentFolder |
|
try { |
|
documentFolder = await folder.getEntry(documentFolderName) |
|
} catch (e) { |
|
console.warn(e) |
|
|
|
documentFolder = await folder.createFolder(documentFolderName) |
|
} |
|
|
|
console.log('documentFolder.nativePath: ', documentFolder.nativePath) |
|
const file = await documentFolder.createFile(json_file_name, { |
|
type: storage.types.file, |
|
overwrite: true, |
|
}) |
|
|
|
const JSONInPrettyFormat = JSON.stringify(json, undefined, 4) |
|
await file.write(JSONInPrettyFormat, { |
|
format: storage.formats.utf8, |
|
append: false, |
|
}) |
|
|
|
const token = await storage.localFileSystem.createSessionToken(file) |
|
} |
|
|
|
async function base64ToFile(b64Image, image_name = 'output_image.png') { |
|
|
|
|
|
try { |
|
const img = _base64ToArrayBuffer(b64Image) |
|
|
|
const img_name = image_name |
|
|
|
const folder = await storage.localFileSystem.getTemporaryFolder() |
|
const file = await folder.createFile(img_name, { overwrite: true }) |
|
|
|
await file.write(img, { format: storage.formats.binary }) |
|
|
|
const token = await storage.localFileSystem.createSessionToken(file) |
|
|
|
let place_event_result |
|
let imported_layer |
|
await executeAsModal(async () => { |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'placeEvent', |
|
|
|
null: { |
|
_path: token, |
|
_kind: 'local', |
|
}, |
|
freeTransformCenterState: { |
|
_enum: 'quadCenterState', |
|
_value: 'QCSAverage', |
|
}, |
|
offset: { |
|
_obj: 'offset', |
|
horizontal: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
vertical: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
}, |
|
_isCommand: true, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{ |
|
synchronousExecution: true, |
|
modalBehavior: 'execute', |
|
} |
|
) |
|
console.log('placeEmbedd batchPlay result: ', result) |
|
|
|
place_event_result = result[0] |
|
imported_layer = await app.activeDocument.activeLayers[0] |
|
}) |
|
return imported_layer |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
|
|
|
|
} |
|
|
|
async function placeImageB64ToLayer(image_path, entery) { |
|
|
|
|
|
try { |
|
console.log('placeEmbedded(): image_path: ', image_path) |
|
|
|
const formats = require('uxp').storage.formats |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
|
|
const names = image_path.split('/') |
|
const length = names.length |
|
const image_name = names[length - 1] |
|
const project_name = names[length - 2] |
|
let pluginFolder = await fs.getPluginFolder() |
|
|
|
const image_dir = `./server/python_server/output/${project_name}` |
|
|
|
|
|
let img_dir = await pluginFolder.getEntry(image_dir) |
|
|
|
|
|
const file = await img_dir.createFile(image_name, { overwrite: true }) |
|
|
|
const img = await file.read({ format: formats.binary }) |
|
const token = await storage.localFileSystem.createSessionToken(file) |
|
let place_event_result |
|
await executeAsModal(async () => { |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'placeEvent', |
|
ID: 6, |
|
null: { |
|
_path: token, |
|
_kind: 'local', |
|
}, |
|
freeTransformCenterState: { |
|
_enum: 'quadCenterState', |
|
_value: 'QCSAverage', |
|
}, |
|
offset: { |
|
_obj: 'offset', |
|
horizontal: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
vertical: { |
|
_unit: 'pixelsUnit', |
|
_value: 0, |
|
}, |
|
}, |
|
_isCommand: true, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{ |
|
synchronousExecution: true, |
|
modalBehavior: 'execute', |
|
} |
|
) |
|
console.log('placeEmbedd batchPlay result: ', result) |
|
|
|
place_event_result = result[0] |
|
}) |
|
|
|
return place_event_result |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function openImageAction() { |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
try { |
|
let pluginFolder = await fs.getPluginFolder() |
|
|
|
|
|
const relative_dir_path = `./server/python_server/` |
|
|
|
const image_path = `${relative_dir_path}/${gCurrentImagePath}` |
|
|
|
let theTemplate = await pluginFolder.getEntry(image_path) |
|
|
|
await app.open(theTemplate) |
|
} catch (e) { |
|
console.warn("couldn't open image ", e) |
|
} |
|
} |
|
|
|
async function openImageExe() { |
|
await require('photoshop').core.executeAsModal(openImageAction) |
|
} |
|
|
|
|
|
|
|
async function convertToSmartObjectAction() { |
|
const batchPlay = require('photoshop').action.batchPlay |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'newPlacedLayer', |
|
_isCommand: true, |
|
_options: { |
|
dialogOptions: 'dontDisplay', |
|
}, |
|
}, |
|
], |
|
{} |
|
) |
|
} |
|
|
|
async function convertToSmartObjectExe() { |
|
await require('photoshop').core.executeAsModal(convertToSmartObjectAction) |
|
} |
|
|
|
async function ImagesToLayersExe(images_paths) { |
|
g_generation_session.isLoadingActive = true |
|
|
|
await psapi.reSelectMarqueeExe(g_generation_session.selectionInfo) |
|
image_path_to_layer = {} |
|
console.log('ImagesToLayersExe: images_paths: ', images_paths) |
|
for (image_path of images_paths) { |
|
gCurrentImagePath = image_path |
|
console.log(gCurrentImagePath) |
|
await openImageExe() |
|
await convertToSmartObjectExe() |
|
if (g_b_use_smart_object === false) { |
|
await executeAsModal(async () => { |
|
await app.activeDocument.activeLayers[0].rasterize() |
|
}) |
|
} |
|
await stackLayers() |
|
await psapi.layerToSelection(g_generation_session.selectionInfo) |
|
layer = await app.activeDocument.activeLayers[0] |
|
image_path_to_layer[image_path] = layer |
|
|
|
} |
|
return image_path_to_layer |
|
} |
|
|
|
async function silentImagesToLayersExe_old(images_info) { |
|
try { |
|
g_generation_session.isLoadingActive = true |
|
|
|
await psapi.reSelectMarqueeExe(g_generation_session.selectionInfo) |
|
image_path_to_layer = {} |
|
console.log( |
|
'silentImagesToLayersExe: images_info.images_paths: ', |
|
images_info.images_paths |
|
) |
|
|
|
const timer = (ms) => new Promise((res) => setTimeout(res, ms)) |
|
|
|
for (image_info of images_info) { |
|
console.log(gCurrentImagePath) |
|
|
|
await psapi.unselectActiveLayersExe() |
|
|
|
let placeEventResult |
|
|
|
|
|
|
|
|
|
|
|
placeEventResult = await base64ToFile(image_info.base64) |
|
|
|
let layer = await app.activeDocument.layers.filter( |
|
(l) => l.id === placeEventResult?.ID |
|
)[0] |
|
|
|
|
|
let timer_count = 0 |
|
|
|
|
|
console.log('loaded layer: ', layer) |
|
console.log('placeEventResult?.ID: ', placeEventResult?.ID) |
|
|
|
while (!layer && timer_count <= 10000) { |
|
await timer(100) |
|
timer_count += 100 |
|
|
|
layer = await app.activeDocument.layers.filter( |
|
(l) => l.id === placeEventResult?.ID |
|
)[0] |
|
const active_layer = await app.activeDocument.activeLayers[0] |
|
console.log('active_layer.id: ', active_layer.id) |
|
if (active_layer.id === placeEventResult?.ID) { |
|
layer = active_layer |
|
} |
|
|
|
console.log('timer_count: ', timer_count) |
|
console.log('loaded layer: ', layer) |
|
console.log('placeEventResult?.ID: ', placeEventResult?.ID) |
|
} |
|
|
|
if (g_b_use_smart_object === false) { |
|
await executeAsModal(async () => { |
|
await layer.rasterize() |
|
}) |
|
} |
|
|
|
await psapi.selectLayersExe([layer]) |
|
await psapi.layerToSelection(g_generation_session.selectionInfo) |
|
|
|
|
|
|
|
|
|
await g_generation_session.moveToTopOfOutputGroup(layer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image_path_to_layer[image_info.path] = layer |
|
|
|
} |
|
return image_path_to_layer |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
g_generation_session.isLoadingActive = false |
|
} |
|
|
|
async function silentImagesToLayersExe(images_info) { |
|
|
|
try { |
|
g_generation_session.isLoadingActive = true |
|
|
|
await psapi.reSelectMarqueeExe(g_generation_session.selectionInfo) |
|
image_path_to_layer = {} |
|
console.log( |
|
'silentImagesToLayersExe: images_info.images_paths: ', |
|
images_info.images_paths |
|
) |
|
|
|
|
|
|
|
for (image_info of images_info) { |
|
console.log(gCurrentImagePath) |
|
|
|
await psapi.unselectActiveLayersExe() |
|
|
|
let imported_layer |
|
|
|
|
|
|
|
|
|
|
|
|
|
const selection_info = await g_generation_session.selectionInfo |
|
|
|
imported_layer = await io.IO.base64ToLayer( |
|
image_info.base64, |
|
'output_image.png', |
|
selection_info.left, |
|
selection_info.top, |
|
selection_info.width, |
|
selection_info.height |
|
) |
|
if (!layer_util.Layer.doesLayerExist(imported_layer)) { |
|
continue |
|
} |
|
|
|
|
|
|
|
|
|
let timer_count = 0 |
|
|
|
|
|
console.log('imported_layer: ', imported_layer) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (g_b_use_smart_object === false) { |
|
await executeAsModal(async () => { |
|
await imported_layer.rasterize() |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
await g_generation_session.moveToTopOfOutputGroup(imported_layer) |
|
await psapi.setVisibleExe(imported_layer, false) |
|
image_path_to_layer[image_info.path] = imported_layer |
|
|
|
} |
|
return image_path_to_layer |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
g_generation_session.isLoadingActive = false |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function stackLayers() { |
|
|
|
const workingDoc = app.documents[0] |
|
|
|
const docsToStack = app.documents.filter( |
|
(doc) => doc._id !== workingDoc._id |
|
) |
|
let docCounter = 0 |
|
|
|
|
|
|
|
|
|
await require('photoshop').core.executeAsModal(async () => { |
|
|
|
docCounter++ |
|
|
|
|
|
for (const doc of docsToStack) { |
|
|
|
|
|
|
|
|
|
doc.layers[0].name = `Layer ${docCounter}` |
|
|
|
|
|
docCounter++ |
|
|
|
|
|
doc.layers[0].duplicate(workingDoc) |
|
|
|
|
|
await doc.closeWithoutSaving() |
|
} |
|
}) |
|
} |
|
|
|
document.getElementById('collapsible').addEventListener('click', function () { |
|
this.classList.toggle('active') |
|
var content = this.nextElementSibling |
|
console.log('content:', content) |
|
if (content.style.display === 'block') { |
|
content.style.display = 'none' |
|
this.textContent = 'Show Samplers' |
|
} else { |
|
content.style.display = 'block' |
|
this.textContent = 'Hide Samplers' |
|
} |
|
}) |
|
|
|
function removeInitImageFromViewer() {} |
|
function removeMaskFromViewer() {} |
|
|
|
async function viewerThumbnailclickHandler(e, viewer_obj_owner) { |
|
if (g_isViewerMenuDisabled) { |
|
return g_isViewerMenuDisabled |
|
} |
|
|
|
let click_type = Enum.clickTypeEnum['Click'] |
|
|
|
if (e.shiftKey) { |
|
click_type = Enum.clickTypeEnum['ShiftClick'] |
|
} else if (e.altKey) { |
|
click_type = Enum.clickTypeEnum['AltClick'] |
|
} |
|
|
|
if ( |
|
viewer_obj_owner.isActive() && |
|
click_type === Enum.clickTypeEnum['Click'] |
|
) { |
|
|
|
click_type = Enum.clickTypeEnum['SecondClick'] |
|
console.log('converted click_type: ', click_type) |
|
} |
|
|
|
await executeAsModal(async () => { |
|
|
|
|
|
await viewer_obj_owner.click(click_type) |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createViewerImgHtml(output_dir_relative, image_path, base64_image) { |
|
const img = document.createElement('img') |
|
|
|
img.src = base64ToSrc(base64_image) |
|
img.className = 'viewer-image' |
|
console.log('image_path: ', image_path) |
|
|
|
|
|
return img |
|
} |
|
|
|
|
|
function toggleLayerVisibility(layer, b_on) { |
|
try { |
|
layer.visible = b_on |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function turnMaskVisible( |
|
b_mask_group_on, |
|
b_white_mask_on, |
|
b_solid_black_mask_on |
|
) { |
|
|
|
try { |
|
await executeAsModal(() => { |
|
toggleLayerVisibility(g_mask_group, b_mask_group_on) |
|
toggleLayerVisibility(g_white_mask, b_white_mask_on) |
|
toggleLayerVisibility(g_solid_black_mask, b_solid_black_mask_on) |
|
}) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function loadInitImageViewerObject( |
|
group, |
|
snapshot, |
|
solid_background, |
|
path, |
|
auto_delete, |
|
base64_image |
|
) { |
|
const initImage = g_viewer_manager.addInitImage( |
|
group, |
|
snapshot, |
|
solid_background, |
|
path, |
|
auto_delete |
|
) |
|
|
|
const init_img_html = createViewerImgHtml( |
|
'./server/python_server/init_images/', |
|
path, |
|
base64_image |
|
) |
|
initImage.createThumbnailNew(init_img_html) |
|
g_viewer_manager.init_image_container.appendChild( |
|
initImage.thumbnail_container |
|
) |
|
initImage.setImgHtml(init_img_html) |
|
|
|
init_img_html.addEventListener('click', async (e) => { |
|
await viewerThumbnailclickHandler(e, initImage) |
|
}) |
|
} |
|
|
|
async function loadViewerImages() { |
|
try { |
|
|
|
console.log( |
|
'g_generation_session.image_paths_to_layers:', |
|
g_generation_session.image_paths_to_layers |
|
) |
|
|
|
const output_dir_relative = './server/python_server/' |
|
|
|
|
|
|
|
const mask_container = document.getElementById( |
|
'divInitMaskViewerContainer' |
|
) |
|
const output_image_container = document.getElementById( |
|
'divViewerImagesContainer' |
|
) |
|
|
|
|
|
|
|
|
|
image_paths = Object.keys(g_generation_session.image_paths_to_layers) |
|
console.log('image_paths: ', image_paths) |
|
let i = 0 |
|
|
|
|
|
|
|
|
|
if (g_viewer_manager.initGroup) { |
|
|
|
|
|
const paths = Object.keys(g_viewer_manager.initImageLayersJson) |
|
for (const path of paths) { |
|
if (!g_viewer_manager.hasViewerImage(path)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await g_viewer_manager.loadInitImageViewerObject(path) |
|
|
|
|
|
} |
|
} |
|
} |
|
|
|
|
|
if (g_viewer_manager.maskGroup) { |
|
const path = `./server/python_server/init_images/${g_init_image_mask_name}` |
|
if (!g_viewer_manager.hasViewerImage(path)) { |
|
|
|
|
|
|
|
|
|
const group = g_viewer_manager.maskLayersJson[path].group |
|
const white_mark = |
|
g_viewer_manager.maskLayersJson[path].white_mark |
|
const solid_background = |
|
g_viewer_manager.maskLayersJson[path].solid_background |
|
const mask_obj = g_viewer_manager.addMask( |
|
group, |
|
white_mark, |
|
solid_background, |
|
path |
|
) |
|
|
|
const mask_img_html = createViewerImgHtml( |
|
'./server/python_server/init_images/', |
|
g_init_image_mask_name, |
|
g_generation_session.base64maskImage[path] |
|
) |
|
|
|
mask_obj.createThumbnailNew(mask_img_html) |
|
mask_container.appendChild(mask_obj.thumbnail_container) |
|
mask_obj.setImgHtml(mask_img_html) |
|
|
|
|
|
|
|
mask_img_html.addEventListener('click', async (e) => { |
|
await viewerThumbnailclickHandler(e, mask_obj) |
|
}) |
|
|
|
|
|
} |
|
} |
|
|
|
console.log('image_paths: ', image_paths) |
|
let lastOutputImage |
|
for (const path of image_paths) { |
|
|
|
|
|
if (!g_viewer_manager.hasViewerImage(path)) { |
|
|
|
|
|
|
|
|
|
const layer = g_generation_session.image_paths_to_layers[path] |
|
const img = createViewerImgHtml( |
|
output_dir_relative, |
|
path, |
|
g_generation_session.base64OutputImages[path] |
|
) |
|
const output_image_obj = g_viewer_manager.addOutputImage( |
|
layer, |
|
path |
|
) |
|
lastOutputImage = output_image_obj |
|
const b_button_visible = |
|
g_generation_session.mode !== generationMode['Txt2Img'] |
|
? true |
|
: false |
|
|
|
output_image_obj.createThumbnailNew(img, b_button_visible) |
|
|
|
|
|
|
|
|
|
|
|
output_image_container.appendChild( |
|
output_image_obj.thumbnail_container |
|
) |
|
|
|
|
|
img.addEventListener('click', async (e) => { |
|
await viewerThumbnailclickHandler(e, output_image_obj) |
|
}) |
|
} |
|
|
|
|
|
} |
|
|
|
const thumbnail_size_slider = document.getElementById('slThumbnailSize') |
|
scaleThumbnailsEvenHandler( |
|
thumbnail_size_slider.value, |
|
thumbnail_size_slider.max, |
|
thumbnail_size_slider.min |
|
) |
|
if (lastOutputImage) { |
|
|
|
|
|
await executeAsModal(async () => { |
|
await lastOutputImage.click(Enum.clickTypeEnum['Click']) |
|
}) |
|
} |
|
} catch (e) { |
|
console.error(`loadViewer images: `, e) |
|
} |
|
} |
|
|
|
async function deleteNoneSelected(viewer_objects) { |
|
try { |
|
|
|
|
|
|
|
await executeAsModal(async () => { |
|
for (const [path, viewer_object] of Object.entries( |
|
viewer_objects |
|
)) { |
|
try { |
|
|
|
|
|
|
|
viewer_object.visible(true) |
|
|
|
await viewer_object.delete() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete g_generation_session.image_paths_to_layers[path] |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
g_viewer_manager.pathToViewerImage = {} |
|
g_viewer_manager.initImageLayersJson = {} |
|
g_viewer_manager.outputImages = [] |
|
|
|
|
|
g_generation_session.image_paths_to_layers = {} |
|
}) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
async function moveHistoryImageToLayer(img) { |
|
let image_path = img.dataset.path |
|
const image_path_escape = image_path.replace(/\o/g, '/o') |
|
|
|
|
|
const base64_image = img.src.replace('data:image/png;base64,', '') |
|
|
|
const metadata_json = JSON.parse(img.dataset.metadata_json_string) |
|
const to_x = metadata_json['selection_info']?.left |
|
const to_y = metadata_json['selection_info']?.top |
|
const width = metadata_json['selection_info']?.width |
|
const height = metadata_json['selection_info']?.height |
|
await io.IO.base64ToLayer( |
|
base64_image, |
|
'History Image', |
|
to_x, |
|
to_y, |
|
width, |
|
height |
|
) |
|
} |
|
|
|
|
|
async function loadPromptShortcut() { |
|
try { |
|
let prompt_shortcut = await sdapi.loadPromptShortcut() |
|
if (!prompt_shortcut || prompt_shortcut === {}) { |
|
prompt_shortcut = promptShortcutExample() |
|
} |
|
|
|
|
|
|
|
html_manip.setPromptShortcut(prompt_shortcut) |
|
await refreshPromptMenue() |
|
} catch (e) { |
|
console.warn(`loadPromptShortcut warning: ${e}`) |
|
} |
|
} |
|
|
|
document |
|
.getElementById('btnLoadPromptShortcut') |
|
.addEventListener('click', async function () { |
|
await loadPromptShortcut() |
|
}) |
|
|
|
document |
|
.getElementById('btnUpdatePromptShortcut') |
|
.addEventListener('click', async function () { |
|
try { |
|
|
|
const prompt_shortcut_string = |
|
document.getElementById('taPromptShortcut').value |
|
const prompt_shortcut = JSON.parse(prompt_shortcut_string) |
|
var newKey = document.getElementById('KeyPromptShortcut').value |
|
var newValue = document.getElementById('ValuePromptShortcut').value |
|
console.log(newKey) |
|
console.log(newValue) |
|
prompt_shortcut[newKey] = newValue |
|
var JSONInPrettyFormat = JSON.stringify( |
|
prompt_shortcut, |
|
undefined, |
|
4 |
|
) |
|
console.log(JSONInPrettyFormat) |
|
document.getElementById('taPromptShortcut').value = |
|
JSONInPrettyFormat |
|
await refreshPromptMenue() |
|
} catch (e) { |
|
console.warn(`loadPromptShortcut warning: ${e}`) |
|
} |
|
}) |
|
|
|
document |
|
.getElementById('btnSavePromptShortcut') |
|
.addEventListener('click', async function () { |
|
try { |
|
const r1 = await dialog_box.prompt( |
|
'Are you sure you want to save prompt shortcut?', |
|
"This will override your old prompt shortcut file, you can't undo this operation", |
|
['Cancel', 'Save'] |
|
) |
|
if ((r1 || 'Save') !== 'Save') { |
|
|
|
console.log('cancel') |
|
} else { |
|
|
|
console.log('Save') |
|
|
|
prompt_shortcut_string = |
|
document.getElementById('taPromptShortcut').value |
|
let prompt_shortcut = JSON.parse(prompt_shortcut_string) |
|
|
|
prompt_shortcut = await sdapi.savePromptShortcut( |
|
prompt_shortcut |
|
) |
|
|
|
console.log('prompt_shortcut was saved: ', prompt_shortcut) |
|
} |
|
} catch (e) { |
|
console.warn(`savePromptShortcut warning: ${e}`) |
|
} |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var chHiResFixs = document.getElementById('chHiResFixs') |
|
var div = document.getElementById('HiResDiv') |
|
|
|
chHiResFixs.addEventListener('change', function () { |
|
if (chHiResFixs.checked) { |
|
div.style.display = 'block' |
|
} else { |
|
div.style.display = 'none' |
|
} |
|
}) |
|
|
|
async function refreshPromptMenue() { |
|
try { |
|
|
|
|
|
const prompt_shortcut = html_manip.getPromptShortcut() |
|
const prompt_shortcut_menu = document.getElementById( |
|
'mPromptShortcutMenu' |
|
) |
|
prompt_shortcut_menu.innerHTML = '' |
|
|
|
for (const [key, value] of Object.entries(prompt_shortcut)) { |
|
if (value.trim() === '') { |
|
|
|
continue |
|
} |
|
const menu_item_element = document.createElement('sp-menu-item') |
|
|
|
menu_item_element.innerHTML = key |
|
prompt_shortcut_menu.appendChild(menu_item_element) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document |
|
.getElementById('mPromptShortcutMenu') |
|
.addEventListener('change', (evt) => { |
|
const prompt_shortcut = html_manip.getPromptShortcut() |
|
const key = evt.target.value |
|
console.log('key:', key) |
|
changePromptShortcutKey(key) |
|
changePromptShortcutValue(prompt_shortcut[key]) |
|
}) |
|
|
|
document |
|
.getElementById('btnRefreshPromptShortcutMenu') |
|
.addEventListener('click', async () => { |
|
await refreshPromptMenue() |
|
}) |
|
|
|
function changePromptShortcutKey(new_key) { |
|
document.getElementById('KeyPromptShortcut').value = new_key |
|
} |
|
|
|
function changePromptShortcutValue(new_value) { |
|
document.getElementById('ValuePromptShortcut').value = new_value |
|
} |
|
|
|
|
|
|
|
|
|
document |
|
.querySelector('#slInpaintingMaskWeight') |
|
.addEventListener('input', async (evt) => { |
|
const label_value = evt.target.value / 100 |
|
document.getElementById( |
|
'lInpaintingMaskWeight' |
|
).innerHTML = `${label_value}` |
|
|
|
}) |
|
|
|
document |
|
.querySelector('#slInpaintingMaskWeight') |
|
.addEventListener('change', async (evt) => { |
|
try { |
|
const label_value = evt.target.value / 100 |
|
document.getElementById( |
|
'lInpaintingMaskWeight' |
|
).innerHTML = `${label_value}` |
|
await sdapi.setInpaintMaskWeight(label_value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
|
|
async function downloadIt(link, writeable_entry, image_file_name) { |
|
const image = await fetch(link) |
|
console.log(link) |
|
const storage = require('uxp').storage |
|
const fs = storage.localFileSystem |
|
|
|
try { |
|
const img = await image.arrayBuffer() |
|
|
|
|
|
const file = await writeable_entry.createFile(image_file_name, { |
|
overwrite: true, |
|
}) |
|
|
|
|
|
await file.write(img) |
|
const currentDocument = app.activeDocument |
|
let newDocument |
|
let new_layer |
|
try { |
|
newDocument = await app.open(file) |
|
if (currentDocument) { |
|
new_layer = await newDocument.activeLayers[0].duplicate( |
|
currentDocument |
|
) |
|
await newDocument.closeWithoutSaving() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
|
|
if (!file) { |
|
return |
|
} |
|
return new_layer |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function downloadItExe(link, writeable_entry, image_file_name) { |
|
let new_layer |
|
await executeAsModal(async () => { |
|
try { |
|
new_layer = await downloadIt(link, writeable_entry, image_file_name) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
}) |
|
return new_layer |
|
} |
|
|
|
|
|
async function activateSessionSelectionArea() { |
|
try { |
|
if (psapi.isSelectionValid(g_generation_session.selectionInfo)) { |
|
await psapi.reSelectMarqueeExe(g_generation_session.selectionInfo) |
|
await eventHandler() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document |
|
.getElementById('btnSelectionArea') |
|
.addEventListener('click', async () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await activateSessionSelectionArea() |
|
}) |
|
|
|
|
|
function base64ToSrc(base64_image) { |
|
const image_src = `data:image/png;base64, ${base64_image}` |
|
return image_src |
|
} |
|
|
|
const py_re = require('./utility/sdapi/python_replacement') |
|
|
|
function getDimensions(image) { |
|
return new Promise((resolve, reject) => { |
|
var img = new Image() |
|
img.src = image |
|
|
|
img.addEventListener('load', function () { |
|
|
|
console.log('image loaded:', img.width, img.height) |
|
resolve({ width: img.width, height: img.height }) |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
}) |
|
} |
|
|
|
function scaleThumbnailsEvenHandler(scale_index, max_index, min_index) { |
|
const slider_max = max_index |
|
const slider_min = min_index |
|
const scaler_value = general.mapRange(scale_index, 0, slider_max, 0, 2) |
|
g_viewer_manager.thumbnail_scaler = scaler_value |
|
|
|
try { |
|
g_viewer_manager.scaleThumbnails( |
|
0, |
|
0, |
|
0, |
|
0, |
|
g_viewer_manager.thumbnail_scaler |
|
) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document.getElementById('slThumbnailSize').addEventListener('input', (evt) => { |
|
scaleThumbnailsEvenHandler(evt.target.value, evt.target.max, evt.target.min) |
|
}) |
|
|
|
document.getElementById('linkWidthHeight').addEventListener('click', (evt) => { |
|
evt.target.classList.toggle('blackChain') |
|
const b_state = !evt.target.classList.contains('blackChain') |
|
html_manip.setLinkWidthHeightState(b_state) |
|
}) |
|
|
|
document |
|
.getElementById('chSquareThumbnail') |
|
.addEventListener('click', (evt) => { |
|
if (evt.target.checked) { |
|
g_viewer_manager.isSquareThumbnail = true |
|
} else { |
|
g_viewer_manager.isSquareThumbnail = false |
|
} |
|
const thumbnail_size_slider = document.getElementById('slThumbnailSize') |
|
scaleThumbnailsEvenHandler( |
|
thumbnail_size_slider.value, |
|
thumbnail_size_slider.max, |
|
thumbnail_size_slider.min |
|
) |
|
}) |
|
|
|
|
|
function switchMenu(rb) { |
|
const tab_button_name = rb.dataset['tab-name'] |
|
const tab_page_name = `${tab_button_name}-page` |
|
|
|
try { |
|
const contianer_class = rb.parentElement.dataset['container-class'] |
|
const radio_group = rb.parentElement |
|
document |
|
.getElementById(tab_button_name) |
|
.addEventListener('click', () => { |
|
document.getElementById(tab_button_name) |
|
const option_container = document |
|
.getElementById(tab_page_name) |
|
.querySelector(`.${contianer_class}`) |
|
|
|
|
|
rb.checked = true |
|
option_container.appendChild(radio_group) |
|
}) |
|
|
|
rb.onclick = () => { |
|
document.getElementById(tab_button_name).click() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
async function updateResDifferenceLabel() { |
|
const ratio = await selection.Selection.getImageToSelectionDifference() |
|
const arrow = ratio >= 1 ? '↑' : '↓' |
|
let final_ratio = ratio |
|
if (ratio >= 1) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
document |
|
.getElementById('res-difference') |
|
.classList.remove('res-decrease') |
|
} else { |
|
final_ratio = 1 / ratio |
|
document.getElementById('res-difference').classList.add('res-decrease') |
|
} |
|
const ratio_str = `${arrow}x${final_ratio.toFixed(2)}` |
|
document.getElementById('res-difference').innerText = ratio_str |
|
} |
|
|
|
document |
|
.getElementById('btnSaveHordeSettings') |
|
.addEventListener('click', async () => { |
|
await horde_native.HordeSettings.saveSettings() |
|
}) |
|
|
|
async function getColor(X, Y) { |
|
|
|
|
|
const batchPlay = require('photoshop').action.batchPlay |
|
try { |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'colorSampler', |
|
_target: { |
|
_ref: 'document', |
|
_enum: 'ordinal', |
|
_value: 'targetEnum', |
|
}, |
|
samplePoint: { |
|
horizontal: X, |
|
vertical: Y, |
|
}, |
|
}, |
|
], |
|
{} |
|
) |
|
|
|
const red = result[0].colorSampler.red |
|
const green = result[0].colorSampler.grain |
|
const blue = result[0].colorSampler.blue |
|
|
|
return [red, green, blue] |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async function findDocumentType() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let document_type |
|
const background_layer = await app.activeDocument.backgroundLayer |
|
const artboards = Array.from(await app.activeDocument.artboards) |
|
if (artboards.length > 0) { |
|
document_type = Enum.DocumentTypeEnum['ArtBoard'] |
|
} else if (layer_util.Layer.doesLayerExist(background_layer)) { |
|
|
|
const b_correct_background = await isCorrectBackground() |
|
if (b_correct_background) { |
|
document_type = Enum.DocumentTypeEnum['SolidBackground'] |
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
let width = app.activeDocument.width |
|
let height = app.activeDocument.height |
|
let old_rgb |
|
let same_color = true |
|
|
|
await executeAsModal(async () => { |
|
await layer_util.toggleBackgroundLayerExe() |
|
for (let i = 0; i < 10; ++i) { |
|
let x = Math.floor(Math.random() * width) |
|
let y = Math.floor(Math.random() * height) |
|
|
|
const rgb = await getColor(x, y) |
|
if (old_rgb) { |
|
if ( |
|
Math.round(old_rgb[0]) === Math.round(rgb[0]) && |
|
Math.round(old_rgb[1]) === Math.round(rgb[1]) && |
|
Math.round(old_rgb[2]) === Math.round(rgb[2]) |
|
) { |
|
} else { |
|
same_color = false |
|
break |
|
} |
|
} |
|
old_rgb = rgb |
|
} |
|
await layer_util.toggleBackgroundLayerExe() |
|
}) |
|
|
|
document_type = same_color |
|
? Enum.DocumentTypeEnum['SolidBackground'] |
|
: Enum.DocumentTypeEnum['ImageBackground'] |
|
} |
|
} else { |
|
|
|
document_type = Enum.DocumentTypeEnum['NoBackground'] |
|
} |
|
|
|
return document_type |
|
} |
|
|
|
async function correctDocumentType(documentType) { |
|
if (documentType === Enum.DocumentTypeEnum['SolidBackground']) { |
|
|
|
} else if (documentType === Enum.DocumentTypeEnum['ImageBackground']) { |
|
|
|
await executeAsModal(async () => { |
|
const image_layer = |
|
await app.activeDocument.backgroundLayer.duplicate() |
|
image_layer.name = 'Image' |
|
await app.activeDocument.backgroundLayer.delete() |
|
await layer_util.createBackgroundLayer(255, 255, 255) |
|
}) |
|
} else if (documentType === Enum.DocumentTypeEnum['ArtBoard']) { |
|
|
|
await app.showAlert( |
|
"the plugin doesn't work with artboards, create normal document with no artboard to use the plugin" |
|
) |
|
throw "the plugin doesn't work with artboards, create normal document with no artboard to use the plugin" |
|
} else if (documentType === Enum.DocumentTypeEnum['NoBackground']) { |
|
await layer_util.createBackgroundLayer(255, 255, 255) |
|
} |
|
} |
|
|
|
async function isCorrectBackground() { |
|
const historylist = app.activeDocument.historyStates.filter( |
|
(h) => h.name === 'Correct Background' |
|
) |
|
console.log('historylist:', historylist) |
|
const is_correct_background = historylist.length > 0 ? true : false |
|
return is_correct_background |
|
} |
|
|
|
document |
|
.getElementById('btnSaveHordeSettings') |
|
.addEventListener('click', async () => { |
|
await horde_native.HordeSettings.saveSettings() |
|
}) |
|
|
|
async function getColor(X, Y) { |
|
|
|
|
|
const batchPlay = require('photoshop').action.batchPlay |
|
try { |
|
const result = await batchPlay( |
|
[ |
|
{ |
|
_obj: 'colorSampler', |
|
_target: { |
|
_ref: 'document', |
|
_enum: 'ordinal', |
|
_value: 'targetEnum', |
|
}, |
|
samplePoint: { |
|
horizontal: X, |
|
vertical: Y, |
|
}, |
|
}, |
|
], |
|
{} |
|
) |
|
|
|
const red = result[0].colorSampler.red |
|
const green = result[0].colorSampler.grain |
|
const blue = result[0].colorSampler.blue |
|
|
|
return [red, green, blue] |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function correctDocumentType(documentType) { |
|
if (documentType === Enum.DocumentTypeEnum['SolidBackground']) { |
|
|
|
} else if (documentType === Enum.DocumentTypeEnum['ImageBackground']) { |
|
|
|
await executeAsModal(async () => { |
|
const image_layer = |
|
await app.activeDocument.backgroundLayer.duplicate() |
|
image_layer.name = 'Image' |
|
await app.activeDocument.backgroundLayer.delete() |
|
await layer_util.createBackgroundLayer(255, 255, 255) |
|
}) |
|
} else if (documentType === Enum.DocumentTypeEnum['ArtBoard']) { |
|
|
|
await app.showAlert( |
|
"the plugin doesn't work with artboards, create normal document with no artboard to use the plugin" |
|
) |
|
throw "the plugin doesn't work with artboards, create normal document with no artboard to use the plugin" |
|
} else if (documentType === Enum.DocumentTypeEnum['NoBackground']) { |
|
await layer_util.createBackgroundLayer(255, 255, 255) |
|
} |
|
} |
|
|
|
async function isCorrectBackground() { |
|
const historylist = app.activeDocument.historyStates.filter( |
|
(h) => h.name === 'Correct Background' |
|
) |
|
console.log('historylist:', historylist) |
|
const is_correct_background = historylist.length > 0 ? true : false |
|
return is_correct_background |
|
} |
|
|
|
let g_viewer_sub_menu_list = [] |
|
|
|
const submenu = { |
|
viewer: { |
|
value: 'viewer', |
|
Label: 'Viewer', |
|
'data-tab-name': 'sp-viewer-tab', |
|
}, |
|
prompts_library: { |
|
value: 'prompts-library', |
|
Label: 'Prompts Library', |
|
'data-tab-name': 'sp-prompts-library-tab', |
|
}, |
|
history: { |
|
value: 'history', |
|
Label: 'History', |
|
'data-tab-name': 'sp-history-tab', |
|
}, |
|
lexica: { |
|
value: 'lexica', |
|
Label: 'Lexica', |
|
'data-tab-name': 'sp-lexica-tab', |
|
}, |
|
image_search: { |
|
value: 'image_search', |
|
Label: 'Image Search', |
|
'data-tab-name': 'sp-image_search-tab', |
|
}, |
|
} |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
const viewer_sub_menu_html = document.getElementById('viewer-sub-menu') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const [option_id, option_data] of Object.entries(submenu)) { |
|
const li = document.createElement('li') |
|
|
|
li.innerHTML = ` |
|
<input |
|
type="radio" |
|
id="${option_id}" |
|
name="options" |
|
checked |
|
class="sub-menu-tab-class" |
|
data-tab-name='${option_data['data-tab-name']}' |
|
|
|
/> |
|
<label for="${option_id}">${option_data.Label}</label> |
|
` |
|
|
|
viewer_sub_menu_html.appendChild(li) |
|
} |
|
|
|
function switchMenu_new(rb) { |
|
const tab_button_name = rb.dataset['tab-name'] |
|
const tab_page_name = `${tab_button_name}-page` |
|
|
|
try { |
|
const contianer_class = |
|
rb.parentElement.parentElement.parentElement.dataset[ |
|
'container-class' |
|
] |
|
const radio_group = rb.parentElement.parentElement.parentElement |
|
document |
|
.getElementById(tab_button_name) |
|
.addEventListener('click', () => { |
|
document.getElementById(tab_button_name) |
|
const option_container = document |
|
.getElementById(tab_page_name) |
|
.querySelector(`.${contianer_class}`) |
|
|
|
|
|
rb.checked = true |
|
option_container.appendChild(radio_group) |
|
}) |
|
|
|
rb.parentElement.onclick = () => { |
|
document.getElementById(tab_button_name).click() |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
document.getElementsByClassName('sub-menu-tab-class').forEach((element) => { |
|
switchMenu_new(element) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
Array.from(document.querySelectorAll('.rbSubTab')).forEach((rb) => { |
|
switchMenu(rb) |
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|