2022-10-18 19:47:16 +08:00
// import { w2ui,w2toolbar,w2field,query,w2alert, w2utils,w2confirm} from "https://rawgit.com/vitmalina/w2ui/master/dist/w2ui.es6.min.js"
// import { w2ui,w2toolbar,w2field,query,w2alert, w2utils,w2confirm} from "https://cdn.jsdelivr.net/gh/vitmalina/w2ui@master/dist/w2ui.es6.min.js"
// https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript
function getBase64 ( file ) {
var reader = new FileReader ( ) ;
reader . readAsDataURL ( file ) ;
reader . onload = function ( ) {
add _image ( reader . result ) ;
// console.log(reader.result);
} ;
reader . onerror = function ( error ) {
console . log ( "Error: " , error ) ;
} ;
}
function getText ( file ) {
var reader = new FileReader ( ) ;
reader . readAsText ( file ) ;
reader . onload = function ( ) {
window . postMessage ( [ "load" , reader . result ] , "*" )
// console.log(reader.result);
} ;
reader . onerror = function ( error ) {
console . log ( "Error: " , error ) ;
} ;
}
document . querySelector ( "#upload_file" ) . addEventListener ( "change" , ( event ) => {
console . log ( event ) ;
let file = document . querySelector ( "#upload_file" ) . files [ 0 ] ;
getBase64 ( file ) ;
} )
document . querySelector ( "#upload_state" ) . addEventListener ( "change" , ( event ) => {
console . log ( event ) ;
let file = document . querySelector ( "#upload_state" ) . files [ 0 ] ;
getText ( file ) ;
} )
2022-10-21 00:19:43 +08:00
open _setting = function ( ) {
if ( ! w2ui . foo ) {
new w2form ( {
name : "foo" ,
style : "border: 0px; background-color: transparent;" ,
fields : [ {
field : "canvas_width" ,
type : "int" ,
required : true ,
html : {
label : "Canvas Width"
}
} ,
{
field : "canvas_height" ,
type : "int" ,
required : true ,
html : {
label : "Canvas Height"
}
} ,
] ,
record : {
canvas _width : 1200 ,
canvas _height : 600 ,
} ,
actions : {
Save ( ) {
this . validate ( ) ;
let record = this . getCleanRecord ( ) ;
2022-10-21 00:37:08 +08:00
window . postMessage ( [ "resize" , record . canvas _width , record . canvas _height ] , "*" ) ;
w2popup . close ( ) ;
2022-10-21 00:19:43 +08:00
} ,
custom : {
text : "Cancel" ,
style : "text-transform: uppercase" ,
onClick ( event ) {
w2popup . close ( ) ;
}
}
}
} ) ;
}
w2popup . open ( {
title : "Form in a Popup" ,
body : "<div id='form' style='width: 100%; height: 100%;''></div>" ,
style : "padding: 15px 0px 0px 0px" ,
width : 500 ,
height : 280 ,
showMax : true ,
async onToggle ( event ) {
await event . complete
w2ui . foo . resize ( ) ;
}
} )
. then ( ( event ) => {
w2ui . foo . render ( "#form" )
} ) ;
}
2022-10-18 19:47:16 +08:00
var button _lst = [ "clear" , "load" , "save" , "export" , "upload" , "selection" , "canvas" , "eraser" , "outpaint" , "accept" , "cancel" , "retry" , "prev" , "current" , "next" , "eraser_size_btn" , "eraser_size" , "resize_selection" , "scale" , "zoom_in" , "zoom_out" , "help" ] ;
2022-10-23 16:54:43 +08:00
var upload _button _lst = [ 'clear' , 'load' , 'save' , "upload" , 'export' , 'outpaint' , 'resize_selection' , 'help' , "setting" , "interrogate" ] ;
var resize _button _lst = [ 'clear' , 'load' , 'save' , "upload" , 'export' , "selection" , "canvas" , "eraser" , 'outpaint' , 'resize_selection' , "zoom_in" , "zoom_out" , 'help' , "setting" , "interrogate" ] ;
2022-10-23 19:07:25 +08:00
var outpaint _button _lst = [ 'clear' , 'load' , 'save' , "canvas" , "eraser" , "upload" , 'export' , 'resize_selection' , "zoom_in" , "zoom_out" , 'help' , "setting" , "interrogate" , "undo" , "redo" ] ;
2022-10-18 19:47:16 +08:00
var outpaint _result _lst = [ "accept" , "cancel" , "retry" , "prev" , "current" , "next" ] ;
2022-10-19 16:02:25 +08:00
var outpaint _result _func _lst = [ "accept" , "retry" , "prev" , "current" , "next" ] ;
function check _button ( id , text = "" , checked = true , tooltip = "" )
{
return { type : "check" , id : id , text : text , icon : checked ? "fa-solid fa-square-check" : "fa-regular fa-square" , checked : checked , tooltip : tooltip } ;
}
2022-10-18 19:47:16 +08:00
var toolbar = new w2toolbar ( {
box : "#toolbar" ,
name : "toolbar" ,
tooltip : "top" ,
items : [
2022-10-21 00:19:43 +08:00
{ type : "button" , id : "clear" , text : "Reset" , tooltip : "Reset Canvas" , icon : "fa-solid fa-rectangle-xmark" } ,
2022-10-18 19:47:16 +08:00
{ type : "break" } ,
{ type : "button" , id : "load" , tooltip : "Load Canvas" , icon : "fa-solid fa-file-import" } ,
{ type : "button" , id : "save" , tooltip : "Save Canvas" , icon : "fa-solid fa-file-export" } ,
{ type : "button" , id : "export" , tooltip : "Export Image" , icon : "fa-solid fa-floppy-disk" } ,
{ type : "break" } ,
{ type : "button" , id : "upload" , text : "Upload Image" , icon : "fa-solid fa-upload" } ,
{ type : "break" } ,
{ type : "radio" , id : "selection" , group : "1" , tooltip : "Selection" , icon : "fa-solid fa-arrows-up-down-left-right" , checked : true } ,
{ type : "radio" , id : "canvas" , group : "1" , tooltip : "Canvas" , icon : "fa-solid fa-image" } ,
{ type : "radio" , id : "eraser" , group : "1" , tooltip : "Eraser" , icon : "fa-solid fa-eraser" } ,
{ type : "break" } ,
2022-10-21 00:19:43 +08:00
{ type : "button" , id : "outpaint" , text : "Outpaint" , tooltip : "Run Outpainting" , icon : "fa-solid fa-brush" } ,
2022-10-23 16:54:43 +08:00
{ type : "button" , id : "interrogate" , text : "Interrogate" , tooltip : "Get a prompt with Clip Interrogator " , icon : "fa-solid fa-magnifying-glass" } ,
2022-10-18 19:47:16 +08:00
{ type : "break" } ,
2022-10-23 19:07:25 +08:00
{ type : "button" , id : "accept" , text : "Accept" , tooltip : "Accept current result" , icon : "fa-solid fa-check" , hidden : true , disabled : true , } ,
2022-10-21 00:19:43 +08:00
{ type : "button" , id : "cancel" , text : "Cancel" , tooltip : "Cancel current outpainting/error" , icon : "fa-solid fa-ban" , hidden : true } ,
2022-10-23 19:07:25 +08:00
{ type : "button" , id : "retry" , text : "Retry" , tooltip : "Retry" , icon : "fa-solid fa-rotate" , hidden : true , disabled : true , } ,
{ type : "button" , id : "prev" , tooltip : "Prev Result" , icon : "fa-solid fa-caret-left" , hidden : true , disabled : true , } ,
{ type : "html" , id : "current" , hidden : true , disabled : true ,
2022-10-18 19:47:16 +08:00
async onRefresh ( event ) {
await event . complete
let fragment = query . html ( `
< div class = "w2ui-tb-text" >
< div class = "w2ui-tb-count" >
< span > $ { this . sel _value ? ? "1/1" } < / s p a n >
< / d i v > < / d i v > ` )
query ( this . box ) . find ( "#tb_toolbar_item_current" ) . append ( fragment )
}
} ,
2022-10-23 19:07:25 +08:00
{ type : "button" , id : "next" , tooltip : "Next Result" , icon : "fa-solid fa-caret-right" , hidden : true , disabled : true , } ,
{ type : "button" , id : "add_image" , text : "Add Image" , icon : "fa-solid fa-file-circle-plus" , hidden : true , disabled : true , } ,
{ type : "button" , id : "delete_image" , text : "Delete Image" , icon : "fa-solid fa-trash-can" , hidden : true , disabled : true , } ,
{ type : "button" , id : "confirm" , text : "Confirm" , icon : "fa-solid fa-check" , hidden : true , disabled : true , } ,
{ type : "button" , id : "cancel_overlay" , text : "Cancel" , icon : "fa-solid fa-ban" , hidden : true , disabled : true , } ,
2022-10-18 19:47:16 +08:00
{ type : "break" } ,
{ type : "spacer" } ,
{ type : "break" } ,
{ type : "button" , id : "eraser_size_btn" , tooltip : "Eraser Size" , text : "Size" , icon : "fa-solid fa-eraser" , hidden : true , count : 32 } ,
{ type : "html" , id : "eraser_size" , hidden : true ,
async onRefresh ( event ) {
await event . complete
// let fragment = query.html(`
// <input type="number" size="${this.eraser_size ? this.eraser_size.length:"2"}" style="margin: 0px 3px; padding: 4px;" min="8" max="${this.eraser_max ?? "256"}" value="${this.eraser_size ?? "32"}">
// <input type="range" style="margin: 0px 3px; padding: 4px;" min="8" max="${this.eraser_max ?? "256"}" value="${this.eraser_size ?? "32"}">`)
let fragment = query . html ( `
< input type = "range" style = "margin: 0px 3px; padding: 4px;" min = "8" max = "${this.eraser_max ?? " 256 "}" value = "${this.eraser_size ?? " 32 "}" >
` )
fragment . filter ( "input" ) . on ( "change" , event => {
this . eraser _size = event . target . value ;
window . overlay . freeDrawingBrush . width = this . eraser _size ;
this . setCount ( "eraser_size_btn" , event . target . value ) ;
window . postMessage ( [ "eraser_size" , event . target . value ] , "*" )
this . refresh ( ) ;
} )
query ( this . box ) . find ( "#tb_toolbar_item_eraser_size" ) . append ( fragment )
}
} ,
// { type: "button", id: "resize_eraser", tooltip: "Resize Eraser", icon: "fa-solid fa-sliders" },
2022-10-21 00:19:43 +08:00
{ type : "button" , id : "resize_selection" , text : "Resize Selection" , tooltip : "Resize Selection" , icon : "fa-solid fa-expand" } ,
2022-10-18 19:47:16 +08:00
{ type : "break" } ,
{ type : "html" , id : "scale" ,
async onRefresh ( event ) {
await event . complete
let fragment = query . html ( `
< div class = "" >
< div style = "padding: 4px; border: 1px solid silver" >
< span > $ { this . scale _value ? ? "100%" } < / s p a n >
< / d i v > < / d i v > ` )
query ( this . box ) . find ( "#tb_toolbar_item_scale" ) . append ( fragment )
}
} ,
{ type : "button" , id : "zoom_in" , tooltip : "Zoom In" , icon : "fa-solid fa-magnifying-glass-plus" } ,
{ type : "button" , id : "zoom_out" , tooltip : "Zoom Out" , icon : "fa-solid fa-magnifying-glass-minus" } ,
{ type : "break" } ,
2022-10-19 16:02:25 +08:00
{ type : "button" , id : "help" , tooltip : "Help" , icon : "fa-solid fa-circle-info" } ,
{ type : "new-line" } ,
2022-10-21 14:18:56 +08:00
{ type : "button" , id : "setting" , text : "Canvas Setting" , tooltip : "Resize Canvas Here" , icon : "fa-solid fa-sliders" } ,
2022-10-19 16:02:25 +08:00
{ type : "break" } ,
2022-10-23 22:37:28 +08:00
check _button ( "enable_history" , "Enable History:" , false , "Enable Canvas History" ) ,
{ type : "button" , id : "undo" , tooltip : "Undo last erasing/last outpainting" , icon : "fa-solid fa-rotate-left" , disabled : true } ,
{ type : "button" , id : "redo" , tooltip : "Redo" , icon : "fa-solid fa-rotate-right" , disabled : true } ,
2022-10-23 19:07:25 +08:00
{ type : "break" } ,
2022-10-19 16:02:25 +08:00
check _button ( "enable_img2img" , "Enable Img2Img" , false ) ,
2022-10-19 18:05:02 +08:00
// check_button("use_correction","Photometric Correction",false),
2022-10-19 16:02:25 +08:00
check _button ( "resize_check" , "Resize Small Input" , true ) ,
check _button ( "enable_safety" , "Enable Safety Checker" , true ) ,
2022-10-21 13:20:26 +08:00
check _button ( "square_selection" , "Square Selection Only" , false ) ,
2022-10-19 16:26:17 +08:00
{ type : "break" } ,
check _button ( "use_seed" , "Use Seed:" , false ) ,
{ type : "html" , id : "seed_val" ,
async onRefresh ( event ) {
await event . complete
let fragment = query . html ( `
< input type = "number" style = "margin: 0px 3px; padding: 4px; width:100px;" value = "${this.config_obj.seed_val ?? " 0 "}" > ` )
fragment . filter ( "input" ) . on ( "change" , event => {
this . config _obj . seed _val = event . target . value ;
parent . config _obj = this . config _obj ;
this . refresh ( ) ;
} )
query ( this . box ) . find ( "#tb_toolbar_item_seed_val" ) . append ( fragment )
}
} ,
{ type : "button" , id : "random_seed" , tooltip : "Set a random seed" , icon : "fa-solid fa-dice" } ,
2022-10-18 19:47:16 +08:00
] ,
onClick ( event ) {
switch ( event . target ) {
2022-10-21 00:19:43 +08:00
case "setting" :
open _setting ( ) ;
break ;
2022-10-18 19:47:16 +08:00
case "upload" :
this . upload _mode = true
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "auto" ;
this . click ( "canvas" ) ;
this . click ( "selection" ) ;
this . show ( "confirm" , "cancel_overlay" , "add_image" , "delete_image" ) ;
this . enable ( "confirm" , "cancel_overlay" , "add_image" , "delete_image" ) ;
this . disable ( ... upload _button _lst ) ;
2022-10-23 22:37:28 +08:00
this . disable ( "undo" , "redo" )
2022-10-18 19:47:16 +08:00
query ( "#upload_file" ) . click ( ) ;
2022-10-21 13:20:26 +08:00
if ( this . upload _tip )
{
this . upload _tip = false ;
w2utils . notify ( "Note that only visible images will be added to canvas" , { timeout : 10000 , where : query ( "#container" ) } )
}
2022-10-18 19:47:16 +08:00
break ;
case "resize_selection" :
this . resize _mode = true ;
this . disable ( ... resize _button _lst ) ;
this . enable ( "confirm" , "cancel_overlay" ) ;
this . show ( "confirm" , "cancel_overlay" ) ;
window . postMessage ( [ "resize_selection" , "" ] , "*" ) ;
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "auto" ;
break ;
case "confirm" :
if ( this . upload _mode )
{
export _image ( ) ;
}
else
{
let sel _box = this . selection _box ;
2022-10-23 16:17:50 +08:00
if ( sel _box . width * sel _box . height > 512 * 512 )
{
w2utils . notify ( "Note that the outpainting will be much slower when the area of selection is larger than 512x512" , { timeout : 2000 , where : query ( "#container" ) } )
}
2022-10-18 19:47:16 +08:00
window . postMessage ( [ "resize_selection" , sel _box . x , sel _box . y , sel _box . width , sel _box . height ] , "*" ) ;
}
case "cancel_overlay" :
end _overlay ( ) ;
this . hide ( "confirm" , "cancel_overlay" , "add_image" , "delete_image" ) ;
if ( this . upload _mode ) {
this . enable ( ... upload _button _lst ) ;
}
else
{
this . enable ( ... resize _button _lst ) ;
window . postMessage ( [ "resize_selection" , "" , "" ] , "*" ) ;
2022-10-21 13:20:26 +08:00
if ( event . target == "cancel_overlay" )
{
this . selection _box = this . selection _box _bak ;
}
2022-10-21 00:19:43 +08:00
}
if ( this . selection _box )
{
this . setCount ( "resize_selection" , ` ${ Math . floor ( this . selection _box . width / 8 ) * 8 } x ${ Math . floor ( this . selection _box . height / 8 ) * 8 } ` ) ;
2022-10-18 19:47:16 +08:00
}
this . disable ( "confirm" , "cancel_overlay" , "add_image" , "delete_image" ) ;
this . upload _mode = false ;
this . resize _mode = false ;
this . click ( "selection" ) ;
2022-10-23 22:37:28 +08:00
window . update _undo _redo ( window . undo _redo _state . undo , window . undo _redo _state . redo ) ;
2022-10-18 19:47:16 +08:00
break ;
case "add_image" :
query ( "#upload_file" ) . click ( ) ;
break ;
case "delete_image" :
let active _obj = window . overlay . getActiveObject ( ) ;
if ( active _obj )
{
window . overlay . remove ( active _obj ) ;
window . overlay . renderAll ( ) ;
}
else
{
w2utils . notify ( "You need to select an image first" , { error : true , timeout : 2000 , where : query ( "#container" ) } )
}
break ;
case "load" :
2022-10-21 00:19:43 +08:00
query ( "#upload_state" ) . click ( ) ;
2022-10-21 00:37:08 +08:00
this . selection _box = null ;
2022-10-21 00:19:43 +08:00
this . setCount ( "resize_selection" , "" ) ;
2022-10-18 19:47:16 +08:00
break ;
case "next" :
case "prev" :
window . postMessage ( [ "outpaint" , "" , event . target ] , "*" ) ;
break ;
case "outpaint" :
2022-10-21 14:18:56 +08:00
this . click ( "selection" ) ;
2022-10-18 19:47:16 +08:00
this . disable ( ... outpaint _button _lst ) ;
this . show ( ... outpaint _result _lst ) ;
2022-10-23 22:37:28 +08:00
this . disable ( "undo" , "redo" ) ;
2022-10-18 19:47:16 +08:00
if ( this . outpaint _tip )
{
this . outpaint _tip = false ;
2022-10-23 19:07:25 +08:00
w2utils . notify ( "The canvas stays locked until you accept/cancel current outpainting. You can modify the 'sample number' to get multiple results; you can resize the canvas/selection with 'canvas setting'/'resize selection'; you can use 'photometric correction' to help preserve existing contents" , { timeout : 15000 , where : query ( "#container" ) } )
2022-10-18 19:47:16 +08:00
}
document . querySelector ( "#container" ) . style . pointerEvents = "none" ;
case "retry" :
2022-10-19 16:02:25 +08:00
this . disable ( ... outpaint _result _func _lst ) ;
2022-10-23 16:17:50 +08:00
parent . config _obj [ "interrogate_mode" ] = false ;
2022-10-18 19:47:16 +08:00
window . postMessage ( [ "transfer" , "" ] , "*" )
break ;
2022-10-23 16:17:50 +08:00
case "interrogate" :
2022-10-23 19:07:25 +08:00
if ( this . interrogate _tip )
{
this . interrogate _tip = false ;
w2utils . notify ( "ClipInterrogator v1 will be dynamically loaded when run at the first time, which may take a while" , { timeout : 10000 , where : query ( "#container" ) } )
}
2022-10-23 16:17:50 +08:00
parent . config _obj [ "interrogate_mode" ] = true ;
window . postMessage ( [ "transfer" , "" ] , "*" )
break
2022-10-18 19:47:16 +08:00
case "accept" :
case "cancel" :
this . hide ( ... outpaint _result _lst ) ;
2022-10-19 18:27:46 +08:00
this . disable ( ... outpaint _result _func _lst ) ;
2022-10-18 19:47:16 +08:00
this . enable ( ... outpaint _button _lst ) ;
document . querySelector ( "#container" ) . style . pointerEvents = "auto" ;
2022-10-23 19:07:25 +08:00
if ( this . config _obj . enable _history )
{
window . postMessage ( [ "click" , event . target , "" ] , "*" ) ;
}
else
{
window . postMessage ( [ "click" , event . target ] , "*" ) ;
}
2022-10-20 19:59:44 +08:00
let app = parent . document . querySelector ( "gradio-app" ) ;
app = app . shadowRoot ? ? app ;
app . querySelector ( "#cancel" ) . click ( ) ;
2022-10-23 22:37:28 +08:00
window . update _undo _redo ( window . undo _redo _state . undo , window . undo _redo _state . redo ) ;
2022-10-18 19:47:16 +08:00
break ;
case "eraser" :
case "selection" :
case "canvas" :
if ( event . target == "eraser" )
{
this . show ( "eraser_size" , "eraser_size_btn" ) ;
window . overlay . freeDrawingBrush . width = this . eraser _size ;
window . overlay . isDrawingMode = true ;
}
else
{
this . hide ( "eraser_size" , "eraser_size_btn" ) ;
window . overlay . isDrawingMode = false ;
}
if ( this . upload _mode )
{
if ( event . target == "canvas" )
{
window . postMessage ( [ "mode" , event . target ] , "*" )
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "none" ;
document . querySelector ( "#overlay_container" ) . style . opacity = 0.5 ;
}
else
{
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "auto" ;
document . querySelector ( "#overlay_container" ) . style . opacity = 1.0 ;
}
}
else
{
window . postMessage ( [ "mode" , event . target ] , "*" )
}
break ;
case "help" :
2022-10-21 00:19:43 +08:00
w2popup . open ( {
title : "Document" ,
2022-10-21 15:48:48 +08:00
body : "Usage: <a href='https://github.com/lkwq007/stablediffusion-infinity/blob/master/docs/usage.md' target='_blank'>https://github.com/lkwq007/stablediffusion-infinity/blob/master/docs/usage.md</a>"
2022-10-21 00:19:43 +08:00
} )
2022-10-18 19:47:16 +08:00
break ;
case "clear" :
w2confirm ( "Reset canvas?" ) . yes ( ( ) => {
window . postMessage ( [ "click" , event . target ] , "*" ) ;
} ) . no ( ( ) => { } )
break ;
2022-10-19 16:26:17 +08:00
case "random_seed" :
this . config _obj . seed _val = Math . floor ( Math . random ( ) * 3000000000 ) ;
parent . config _obj = this . config _obj ;
this . refresh ( ) ;
break ;
2022-10-23 19:07:25 +08:00
case "enable_history" :
2022-10-19 16:02:25 +08:00
case "enable_img2img" :
case "use_correction" :
case "resize_check" :
case "enable_safety" :
case "use_seed" :
2022-10-21 13:20:26 +08:00
case "square_selection" :
2022-10-23 22:52:38 +08:00
let target = this . get ( event . target ) ;
if ( event . target == "enable_history" )
{
if ( ! target . checked )
{
w2utils . notify ( "Enable canvas history might increase resource usage / slow down the canvas " , { error : true , timeout : 3000 , where : query ( "#container" ) } )
window . postMessage ( [ "click" , "history" ] , "*" ) ;
}
else
{
window . undo _redo _state . undo = false ;
window . undo _redo _state . redo = false ;
this . disable ( "undo" , "redo" ) ;
}
}
2022-10-19 16:02:25 +08:00
target . icon = target . checked ? "fa-regular fa-square" : "fa-solid fa-square-check" ;
this . config _obj [ event . target ] = ! target . checked ;
parent . config _obj = this . config _obj ;
this . refresh ( ) ;
break ;
2022-10-21 12:54:43 +08:00
case "save" :
case "export" :
ask _filename ( event . target ) ;
2022-10-21 13:20:26 +08:00
break ;
2022-10-18 19:47:16 +08:00
default :
// clear, save, export, outpaint, retry
// break, save, export, accept, retry, outpaint
window . postMessage ( [ "click" , event . target ] , "*" )
}
console . log ( "Target: " + event . target , event )
}
} )
window . w2ui = w2ui ;
2022-10-19 16:02:25 +08:00
w2ui . toolbar . config _obj = {
resize _check : true ,
enable _safety : true ,
use _correction : false ,
enable _img2img : false ,
use _seed : false ,
seed _val : 0 ,
2022-10-21 13:20:26 +08:00
square _selection : false ,
2022-10-23 19:07:25 +08:00
enable _history : false ,
2022-10-19 16:02:25 +08:00
} ;
2022-10-18 19:47:16 +08:00
w2ui . toolbar . outpaint _tip = true ;
2022-10-21 13:20:26 +08:00
w2ui . toolbar . upload _tip = true ;
2022-10-23 19:07:25 +08:00
w2ui . toolbar . interrogate _tip = true ;
2022-10-18 19:47:16 +08:00
window . update _count = function ( cur , total ) {
w2ui . toolbar . sel _value = ` ${ cur } / ${ total } ` ;
w2ui . toolbar . refresh ( ) ;
}
window . update _eraser = function ( val , max _val ) {
w2ui . toolbar . eraser _size = ` ${ val } ` ;
w2ui . toolbar . eraser _max = ` ${ max _val } ` ;
w2ui . toolbar . setCount ( "eraser_size_btn" , ` ${ val } ` ) ;
w2ui . toolbar . refresh ( ) ;
}
window . update _scale = function ( val ) {
2022-10-19 13:42:57 +08:00
w2ui . toolbar . scale _value = ` ${ val } ` ;
2022-10-18 19:47:16 +08:00
w2ui . toolbar . refresh ( ) ;
}
2022-10-18 19:50:49 +08:00
window . enable _result _lst = function ( ) {
2022-10-19 18:27:46 +08:00
w2ui . toolbar . enable ( ... outpaint _result _lst ) ;
2022-10-18 19:50:49 +08:00
}
2022-10-18 19:47:16 +08:00
function onObjectScaled ( e )
{
let object = e . target ;
if ( object . isType ( "rect" ) )
{
let width = object . getScaledWidth ( ) ;
let height = object . getScaledHeight ( ) ;
object . scale ( 1 ) ;
width = Math . max ( Math . min ( width , window . overlay . width - object . left ) , 256 ) ;
height = Math . max ( Math . min ( height , window . overlay . height - object . top ) , 256 ) ;
let l = Math . max ( Math . min ( object . left , window . overlay . width - width - object . strokeWidth ) , 0 ) ;
let t = Math . max ( Math . min ( object . top , window . overlay . height - height - object . strokeWidth ) , 0 ) ;
2022-10-21 13:20:26 +08:00
if ( window . w2ui . toolbar . config _obj . square _selection )
{
2022-10-21 14:18:56 +08:00
let max _val = Math . min ( Math . max ( width , height ) , window . overlay . width , window . overlay . height ) ;
2022-10-21 13:20:26 +08:00
width = max _val ;
height = max _val ;
}
2022-10-18 19:47:16 +08:00
object . set ( { width : width , height : height , left : l , top : t } )
window . w2ui . toolbar . selection _box = { width : width , height : height , x : object . left , y : object . top } ;
2022-10-21 00:19:43 +08:00
window . w2ui . toolbar . setCount ( "resize_selection" , ` ${ Math . floor ( width / 8 ) * 8 } x ${ Math . floor ( height / 8 ) * 8 } ` ) ;
window . w2ui . toolbar . refresh ( ) ;
2022-10-18 19:47:16 +08:00
}
}
function onObjectMoved ( e )
{
let object = e . target ;
if ( object . isType ( "rect" ) )
{
let l = Math . max ( Math . min ( object . left , window . overlay . width - object . width - object . strokeWidth ) , 0 ) ;
let t = Math . max ( Math . min ( object . top , window . overlay . height - object . height - object . strokeWidth ) , 0 ) ;
object . set ( { left : l , top : t } ) ;
window . w2ui . toolbar . selection _box = { width : object . width , height : object . height , x : object . left , y : object . top } ;
}
}
window . setup _overlay = function ( width , height )
{
if ( window . overlay )
{
window . overlay . setDimensions ( { width : width , height : height } ) ;
2022-10-19 13:42:57 +08:00
let app = parent . document . querySelector ( "gradio-app" ) ;
app = app . shadowRoot ? ? app ;
2022-10-19 16:47:58 +08:00
app . querySelector ( "#sdinfframe" ) . style . height = 80 + Number ( height ) + "px" ;
2022-10-19 16:02:25 +08:00
document . querySelector ( "#container" ) . style . height = height + "px" ;
document . querySelector ( "#container" ) . style . width = width + "px" ;
2022-10-18 19:47:16 +08:00
}
else
{
canvas = new fabric . Canvas ( "overlay_canvas" ) ;
canvas . setDimensions ( { width : width , height : height } ) ;
2022-10-19 13:42:57 +08:00
let app = parent . document . querySelector ( "gradio-app" ) ;
app = app . shadowRoot ? ? app ;
2022-10-19 16:47:58 +08:00
app . querySelector ( "#sdinfframe" ) . style . height = 80 + Number ( height ) + "px" ;
2022-10-18 19:47:16 +08:00
canvas . freeDrawingBrush = new fabric . EraserBrush ( canvas ) ;
canvas . on ( "object:scaling" , onObjectScaled ) ;
canvas . on ( "object:moving" , onObjectMoved ) ;
window . overlay = canvas ;
}
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "none" ;
}
window . update _overlay = function ( width , height )
{
window . overlay . setDimensions ( { width : width , height : height } , { backstoreOnly : true } ) ;
// document.querySelector("#overlay_container").style.pointerEvents="none";
}
window . adjust _selection = function ( x , y , width , height )
{
var rect = new fabric . Rect ( {
left : x ,
top : y ,
fill : "rgba(0,0,0,0)" ,
strokeWidth : 3 ,
stroke : "rgba(0,0,0,0.7)" ,
cornerColor : "red" ,
cornerStrokeColor : "red" ,
borderColor : "rgba(255, 0, 0, 1.0)" ,
width : width ,
height : height ,
lockRotation : true ,
} ) ;
rect . setControlsVisibility ( { mtr : false } ) ;
window . overlay . add ( rect ) ;
window . overlay . setActiveObject ( window . overlay . item ( 0 ) ) ;
2022-10-21 00:19:43 +08:00
window . w2ui . toolbar . selection _box = { width : width , height : height , x : x , y : y } ;
window . w2ui . toolbar . selection _box _bak = { width : width , height : height , x : x , y : y } ;
2022-10-18 19:47:16 +08:00
}
function add _image ( url )
{
fabric . Image . fromURL ( url , function ( img ) {
window . overlay . add ( img ) ;
window . overlay . setActiveObject ( img ) ;
} , { left : 100 , top : 100 } ) ;
}
function export _image ( )
{
data = window . overlay . toDataURL ( ) ;
document . querySelector ( "#upload_content" ) . value = data ;
2022-10-23 19:07:25 +08:00
if ( window . w2ui . toolbar . config _obj . enable _history )
{
window . postMessage ( [ "upload" , "" , "" ] , "*" ) ;
window . w2ui . toolbar . enable ( "undo" ) ;
window . w2ui . toolbar . disable ( "redo" ) ;
}
else
{
window . postMessage ( [ "upload" , "" ] , "*" ) ;
}
2022-10-18 19:47:16 +08:00
end _overlay ( ) ;
}
function end _overlay ( )
{
window . overlay . clear ( ) ;
document . querySelector ( "#overlay_container" ) . style . opacity = 1.0 ;
document . querySelector ( "#overlay_container" ) . style . pointerEvents = "none" ;
2022-10-19 16:02:25 +08:00
}
2022-10-21 12:54:43 +08:00
function ask _filename ( target )
{
w2prompt ( {
label : "Enter filename" ,
value : ` outpaint_ ${ ( ( new Date ( Date . now ( ) - ( new Date ( ) ) . getTimezoneOffset ( ) * 60000 ) ) ) . toISOString ( ) . replace ( "T" , "_" ) . replace ( /[^0-9_]/g , "" ) . substring ( 0 , 15 ) } ` ,
} )
. change ( ( event ) => {
console . log ( "change" , event . detail . originalEvent . target . value ) ;
} )
. ok ( ( event ) => {
console . log ( "value=" , event . detail . value ) ;
window . postMessage ( [ "click" , target , event . detail . value ] , "*" ) ;
} )
. cancel ( ( event ) => {
console . log ( "cancel" ) ;
} ) ;
}
2022-10-19 22:04:14 +08:00
document . querySelector ( "#container" ) . addEventListener ( "wheel" , ( e ) => { e . preventDefault ( ) } )
window . setup _shortcut = function ( json )
{
var config = JSON . parse ( json ) ;
var key _map = { } ;
Object . keys ( config . shortcut ) . forEach ( k => {
key _map [ config . shortcut [ k ] ] = k ;
} )
document . addEventListener ( "keydown" , ( e ) => {
if ( e . target . tagName != "INPUT" )
{
let key = e . key ;
if ( e . ctrlKey )
{
key = "Ctrl+" + e . key ;
2022-10-19 22:24:21 +08:00
if ( key in key _map )
{
e . preventDefault ( ) ;
}
2022-10-19 22:04:14 +08:00
}
if ( key in key _map )
{
w2ui . toolbar . click ( key _map [ key ] ) ;
}
}
} )
2022-10-23 19:07:25 +08:00
}
2022-10-23 22:37:28 +08:00
window . undo _redo _state = { undo : false , redo : false } ;
window . update _undo _redo = function ( s0 , s1 )
2022-10-23 19:07:25 +08:00
{
2022-10-23 22:37:28 +08:00
if ( s0 )
{
w2ui . toolbar . enable ( "undo" ) ;
}
else
{
w2ui . toolbar . disable ( "undo" ) ;
}
if ( s1 )
2022-10-23 19:07:25 +08:00
{
2022-10-23 22:37:28 +08:00
w2ui . toolbar . enable ( "redo" ) ;
2022-10-23 19:07:25 +08:00
}
else
{
2022-10-23 22:37:28 +08:00
w2ui . toolbar . disable ( "redo" ) ;
2022-10-23 19:07:25 +08:00
}
2022-10-23 22:37:28 +08:00
window . undo _redo _state . undo = s0 ;
window . undo _redo _state . redo = s1 ;
2022-10-19 22:04:14 +08:00
}