File size: 7,303 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const thumbnail = require('../../thumbnail')
const api = require('../api')
const io = require('../io')
const html_manip = require('../html_manip')

class Lexica {
    constructor(items = []) {
        //items is json object return from lexica request
        this.items = items
    }

    delete() {
        this.items = []
    }
}

async function requestHostedUrl(base64) {
    try {
        const payload = {
            key: '6d207e02198a847aa98d0a2a901485a5',
            source: base64,
        }
        const url = 'https://freeimage.host/api/1/upload'

        const result_json = await api.requestFormDataPost(url, payload)

        const image_url = result_json.image.url
        return image_url
    } catch (e) {
        console.warn(e)
    }
}

async function requestLexica(search_query) {
    const lexica_url = `https://lexica.art/api/v1/search?q=${search_query}`
    const url_encoded = encodeURI(lexica_url)
    result = await api.requestGet(url_encoded)
    console.log('result:', result)
    return result
}
function displayAllLexicaImages(lexica_items) {
    const lexicaMasterImageContainer = document.getElementById(
        'divLexicaImagesContainer'
    )
    lexicaMasterImageContainer.innerHTML = ''
    for (item of lexica_items) {
        displayLexicaImage(item)
    }
}
function displayLexicaImage(lexica_item) {
    // let lexica_item = {
    //     id: '0482ee68-0368-4eca-8846-5930db866b33',
    //     gallery: 'https://lexica.art?q=0482ee68-0368-4eca-8846-5930db866b33',
    //     src: 'https://lexica-serve-encoded-images.sharif.workers.dev/md/0482ee68-0368-4eca-8846-5930db866b33',
    //     srcSmall:
    //         'https://lexica-serve-encoded-images.sharif.workers.dev/sm/0482ee68-0368-4eca-8846-5930db866b33',
    //     prompt: 'cute chubby blue fruits icons for mobile game ui ',
    //     width: 512,
    //     height: 512,
    //     seed: '1413536227',
    //     grid: false,
    //     model: 'stable-diffusion',
    //     guidance: 7,
    //     promptid: 'd9868972-dad8-477d-8e5a-4a0ae1e9b72b',
    //     nsfw: false,
    // }

    const lexicaMasterImageContainer = document.getElementById(
        'divLexicaImagesContainer'
    )

    img_html = document.createElement('img')
    img_html.classList.add('viewer-image')
    img_html.src = lexica_item.srcSmall
    img_html.style.width = '100px'
    const thumbnail_container = thumbnail.Thumbnail.wrapImgInContainer(
        img_html,
        'viewer-image-container'
    )

    async function loadOnCanvas(lexica_item) {
        // lexica_item.
        try {
            const link = lexica_item.src
            const image_file_name = 'lexica_image.png'
            await io.IO.urlToLayer(link, image_file_name)
        } catch (e) {
            console.warn('loadOnCanvas(): ', lexica_item, e)
        }
    }
    async function loadSettingsToUI(lexica_item) {
        try {
            const settings = {
                prompt: lexica_item.prompt,
                width: lexica_item.width,
                height: lexica_item.height,
                seed: lexica_item.seed,
                cfg_scale: lexica_item.guidance,
            }
            g_ui_settings_object.autoFillInSettings(settings)
        } catch (e) {
            console.warn(e)
        }
    }
    async function searchForSimilarImage(lexica_item) {
        try {
            document.getElementById('LexicaSearchField').value = lexica_item.src
            const result_json = await requestLexica(lexica_item.src)

            const lexica_items = result_json.images

            displayAllLexicaImages(lexica_items)
        } catch (e) {
            console.warn(e)
        }
    }
    thumbnail.Thumbnail.addSPButtonToContainer(
        thumbnail_container,
        'svg_sp_btn_canvas',
        'Load on Canvas',
        loadOnCanvas,
        lexica_item
    )
    thumbnail.Thumbnail.addSPButtonToContainer(
        thumbnail_container,
        'svg_sp_btn',
        'load settings',
        loadSettingsToUI,
        lexica_item
    )
    thumbnail.Thumbnail.addSPButtonToContainer(
        thumbnail_container,
        'svg_sp_btn_search',
        'Search for Similar Image',
        searchForSimilarImage,
        lexica_item
    )
    thumbnail_container.addEventListener('click', () => {
        setLexicaPromptValue(lexica_item.prompt)
        // taLexicaPrompt.style.position = 'fixed'
        const originalPosition = taLexicaPrompt.offsetTop
        const currentPosition = document.querySelector('body > div').scrollTop

        const isScrolledPast = currentPosition > originalPosition

        if (isScrolledPast) {
            taLexicaPrompt.style.position = 'fixed'
        } else {
            taLexicaPrompt.style.position = 'static'
        }
    })

    lexicaMasterImageContainer.appendChild(thumbnail_container)
}

const taLexicaPrompt = document.querySelector('#lexicaPrompt')
function setLexicaPromptValue(input) {
    taLexicaPrompt.value = input
}
function getLexicaPromptValue() {
    return taLexicaPrompt.value
}

document
    .getElementById('btnSearchLexica')
    .addEventListener('click', async () => {
        const search_query = document.getElementById('LexicaSearchField').value
        const result_json = await requestLexica(search_query)

        const lexica_items = result_json.images
        const lexica_obj = getLexicaObject()
        lexica_obj.items = lexica_items
        displayAllLexicaImages(lexica_items)
    })

document
    .getElementById('btnReverseSearchLexica')
    .addEventListener('click', async () => {
        //*) check if selection is valid
        //*) get base64 from selection
        //*) request global url from base64
        //*) request Lexica search

        try {
            const width = html_manip.getWidth()
            const height = html_manip.getHeight()
            const selectionInfo = await psapi.getSelectionInfoExe()

            const base64 =
                await io.IO.getSelectionFromCanvasAsBase64Interface_New(
                    width,
                    height,
                    selectionInfo,
                    true
                )
            const hosted_url = await requestHostedUrl(base64)

            const result_json = await requestLexica(hosted_url)

            const lexica_items = result_json.images

            displayAllLexicaImages(lexica_items)
        } catch (e) {
            console.warn(e)
        }
    })

const windowEventListener = document
    .querySelector('body > div')
    .addEventListener('scroll', () => {
        const originalPosition = taLexicaPrompt.offsetTop
        const currentPosition = document.querySelector('body > div').scrollTop
        taLexicaPrompt.style.position = 'static'
        // const isScrolledPast = currentPosition > originalPosition

        // if (isScrolledPast) {
        //     taLexicaPrompt.style.position = 'fixed'
        // } else {
        //     taLexicaPrompt.style.position = 'static'
        // }
    })

const g_lexica_obj = new Lexica()

function getLexicaObject() {
    return g_lexica_obj
}
function setLexicaObject(lexica_obj) {
    g_lexica_obj = lexica_obj
}
module.exports = {
    requestLexica,
    displayLexicaImage,
    displayAllLexicaImages,
    requestHostedUrl,
    Lexica,
    getLexicaObject,
    setLexicaObject,
}