diff --git "a/Auto-Photoshop-StableDiffusion-Plugin/index.js" "b/Auto-Photoshop-StableDiffusion-Plugin/index.js" new file mode 100644--- /dev/null +++ "b/Auto-Photoshop-StableDiffusion-Plugin/index.js" @@ -0,0 +1,4777 @@ +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') + } +} + +// import {helloHelper} from 'helper.js' +// helloHelper2 = require('./helper.js') +// for organizational proposes +// let g_sdapi_path = 'sdapi' +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') +// let g_sdapi_path = 'sdapi_py_re' +// const sdapi = require(`./${g_sdapi_path}`) +const sdapi = require('./sdapi_py_re') + +// const exportHelper = require('./export_png') +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 {entrypoints} = require('uxp') +const html_manip = require('./utility/html_manip') +// const export_png = require('./export_png') +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') +//load tabs +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 = require('./ultimate_sd_upscaler/dist/ultimate_sd_upscaler') +const ultimate_sd_upscaler_script = require('./ultimate_sd_upscaler/dist/ultimate_sd_upscaler.bundle') +const scripts = require('./ultimate_sd_upscaler/dist/scripts.bundle') + +// const ultimate_sd_upscaler_script_test = require('./ultimate_sd_upscaler/dist/main') + +// const { +// script_args, +// script_name, +// } = require('./ultimate_sd_upscaler/dist/ultimate_sd_upscaler') + +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 +//REFACTOR: move to session.js +async function hasSessionSelectionChanged() { + try { + const isSelectionActive = await psapi.checkIfSelectionAreaIsActive() + if (isSelectionActive) { + const current_selection = isSelectionActive // Note: don't use checkIfSelectionAreaIsActive to return the selection object, change this. + + if ( + await hasSelectionChanged( + current_selection, + g_generation_session.selectionInfo + ) + ) { + return true + } else { + //selection has not changed + return false + } + } + } catch (e) { + console.warn(e) + return false + } +} +//REFACTOR: move to selection.js, add selection mode as attribute (linked to rbSelectionMode event) +async function calcWidthHeightFromSelection() { + //set the width and height, hrWidth, and hrHeight using selection info and selection mode + const selection_mode = html_manip.getSelectionMode() + if (selection_mode === 'ratio') { + //change (width and height) and (hrWidth, hrHeight) to match the ratio of selection + 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) + } +} +//REFACTOR: rename to newSelectionEventHandler and move to session.js +const eventHandler = async (event, descriptor) => { + try { + console.log(event, descriptor) + + const isSelectionActive = await psapi.checkIfSelectionAreaIsActive() + if (isSelectionActive) { + const current_selection = isSelectionActive // Note: don't use checkIfSelectionAreaIsActive to return the selection object, change this. + + await calcWidthHeightFromSelection() + + // console.log(` (${final_width}* ${final_height})/(${current_selection.width} * ${current_selection.height})`) + // console.log("detail density: ",(final_width* final_height)/(current_selection.width * current_selection.height)) + + // const new_selection = await psapi.getSelectionInfoExe() + + if ( + await hasSelectionChanged( + current_selection, + g_generation_session.selectionInfo + ) //new selection + ) { + // endSessionUI //red color + // if selection has changed : change the color and text generate btn "Generate" color "red" + // g_ui.endSessionUI() + // const selected_mode = html_manip.getMode() + const selected_mode = getCurrentGenerationModeByValue(g_sd_mode) + g_ui.generateModeUI(selected_mode) + } else { + // it's the same selection and the session is active + + //indicate that the session will continue. only if the session we are in the same mode as the session's mode + // startSessionUI// green color + const current_mode = html_manip.getMode() + if ( + g_generation_session.isActive() && // the session is active + g_generation_session.isSameMode(current_mode) //same mode + ) { + // g_ui.startSessionUI() + g_ui.generateMoreUI() + } + } + } + } catch (e) { + console.warn(e) + } +} +//REFACTOR: move to generation_settings.js +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 +) +//REFACTOR: move to document.js +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 + ) + + // Regular expression to check if string is a valid UUID + 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 + + // String with valid UUID separated by dash + // const str = 'a24a6ea4-ce75-4665-a070-57453082c256' + + const isValidId = regexExp.test(uniqueDocumentId) // true + console.log('isValidId: ', isValidId) + if (isValidId == false) { + let uuid = self.crypto.randomUUID() + console.log(uuid) // for example "36b8f84d-df4e-4d49-b662-bcde71a8764f" + await psapi.saveUniqueDocumentIdExe(uuid) + uniqueDocumentId = uuid + } + return uniqueDocumentId + } catch (e) { + console.warn('warning Document Id may not be valid', e) + } +} + +// document +// .getElementById('btnLinkCurrentDocument') +// .addEventListener('click', async () => { +// await getUniqueDocumentId() +// }) + +// attach event listeners for tabs +//REFACTOR: move to html_manip.js (?) - if there is no business logic here and it's only for UI. +Array.from(document.querySelectorAll('.sp-tab')).forEach((theTab) => { + theTab.onclick = () => { + try { + // localStorage.setItem("currentTab", theTab.getAttribute("id")); + 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) + } + } +}) +//REFACTOR: move to events.js +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() + } +}) +//REFACTOR: move to events.js +document.getElementById('sp-viewer-tab').addEventListener('click', async () => { + moveElementToAnotherTab('batchNumberUi', 'batchNumberViewerTabContainer') + await displayUpdate() +}) +//REFACTOR: move to events.js +document + .getElementById('sp-stable-diffusion-ui-tab') + .addEventListener('click', () => { + moveElementToAnotherTab('batchNumberUi', 'batchNumber-steps-container') + }) +// entrypoints.setup({ + +// panels:{ +// vanilla: ()=>{ +// console.log("you are in the vanilla panel") +// }, +// experimental_1: ()=>{ +// console.log("you are in the experimental_1 panel") + +// } +// } +// } +// ) +// just a number that shouldn't unique enough that we will use when save files. +// each session will get a number from 1 to 1000000 +//REFACTOR: move to session.js +const random_session_id = Math.floor(Math.random() * 1000000 + 1) +//REFACTOR: move to helpers.js (or other utility file) +function getSelectedText() { + // JavaScript + // // Obtain the object reference for the