File size: 5,846 Bytes
34097e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
import React, { ReactEventHandler, useState } from 'react'
import ReactDOM from 'react-dom/client'
import { action, makeAutoObservable, reaction, toJS } from 'mobx'
import { Provider, inject, observer } from 'mobx-react'
import { SpMenu } from './elements'
import * as ultimate_sd_upscale_script from './ultimate_sd_upscaler'
import { ScriptMode } from './ultimate_sd_upscaler'
export function toJsFunc(store: any) {
return toJS(store)
}
class ScriptStore {
scripts_list
disabled: boolean[]
selected_script_name
is_selected_script_available: boolean
selected_store: any
is_active: boolean
selected_args_name: string[]
mode: ScriptMode
scripts: any = {
None: { store: null, args_names: [], mode: [] },
'Ultimate SD upscale': {
store: ultimate_sd_upscale_script.ultimate_sd_upscaler_store,
args_names: ultimate_sd_upscale_script.script_args_ordered,
mode: ultimate_sd_upscale_script.script_mode,
},
}
constructor() {
this.scripts_list = ['None', ultimate_sd_upscale_script.script_name]
this.disabled = [false, true]
this.selected_script_name = 'None'
this.is_selected_script_available = true
this.selected_store = null
this.is_active = true
this.selected_args_name = []
this.mode = ScriptMode.Txt2Img
makeAutoObservable(this)
}
setSelectedScript(name: string) {
this.selected_script_name = name
this.selected_store = this.scripts[name].store
this.selected_args_name = this.scripts[name].args_names
this.is_selected_script_available = true
}
setIsActive(new_value: boolean) {
this.is_active = new_value
}
updateProperty(id: any, value: any) {}
orderedValues() {
const values: any = []
if (!this.selected_store) return values
for (const key of this.selected_args_name) {
// console.log(key, this.data[key])
values.push((this.selected_store.data as any)[key])
}
return values
}
setDisabled(newDisabled: boolean[]) {
console.log('this.disabled:', this.disabled)
console.log('newDisabled:', newDisabled)
this.disabled = newDisabled
}
setMode(newMode: ScriptMode) {
this.mode = newMode
// let index = 0
// Object.keys(this.scripts).forEach((key, index) => {
// const script = this.scripts[key]
// this.disabled[index] = script.mode.includes(newMode) ? false : true
// // console.log(key, script)
// })
const names = Object.keys(this.scripts)
let index = 0
for (let name of names) {
const script = this.scripts[name]
this.disabled[index] = script.mode.includes(newMode) ? false : true
index += 1
}
this.disabled[0] = false // None is always enabled
const selected_index = this.scripts_list.indexOf(
this.selected_script_name
)
this.is_selected_script_available = !this.disabled?.[selected_index]
this.setDisabled([...this.disabled])
}
}
export const script_store = new ScriptStore()
@observer
class ScriptComponent extends React.Component<{}> {
render(): React.ReactNode {
// const script_message = index !== -1 ? script_store.disabled[index] : undefined;
const script_message =
script_store.is_selected_script_available ? undefined : (
<span style={{ color: '#ff595e' }}>
{'the script is not available in the current Mode'}
</span>
)
return (
<>
<SpMenu
title="Scripts"
items={script_store.scripts_list}
disabled={script_store.disabled}
// style="width: 199px; margin-right: 5px"
label_item="Select A Script"
id={'script_list'}
onChange={(id: any, value: any) => {
script_store.setSelectedScript(value.item)
}}
/>
<div>
{script_message}
{/* {script_store.disabled.map((value, index) => (
<li key={index}> {value ? 'true' : 'false'}</li>
))} */}
</div>
<sp-checkbox
checked={script_store.is_active ? true : undefined}
onClick={(event: React.ChangeEvent<HTMLInputElement>) => {
script_store.setIsActive(event.target.checked)
}}
>
{'Activate'}
</sp-checkbox>
<>
{script_store.selected_script_name === 'None' && <></>}
{script_store.selected_script_name ===
ultimate_sd_upscale_script.script_name && (
<ultimate_sd_upscale_script.UltimateSDUpscalerForm
store={
// ultimate_sd_upscale_script.ultimate_sd_upscaler_store
script_store.scripts[
script_store.selected_script_name
].store
}
/>
)}
{/* ... other conditions for other components */}
</>
</>
)
}
}
const domNode = document.getElementById('scriptsContainer')!
const root = ReactDOM.createRoot(domNode)
root.render(
<React.StrictMode>
<ScriptComponent></ScriptComponent>
{/* <SliderValuesDisplay /> */}
</React.StrictMode>
)
|