File size: 23,655 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import { app } from '../../../scripts/app.js'
import { $el } from '../../../scripts/ui.js'
import { api } from '../../../scripts/api.js'

import { td_bg } from './td_background.js'
console.log('td_bg', td_bg)
//本机安装的插件节点全集
window._nodesAll = null

//获取当前系统的插件,节点清单
function getObjectInfo () {
  return new Promise(async (resolve, reject) => {
    let url = getUrl()

    try {
      const response = await fetch(`${url}/object_info`)
      const data = await response.json()
      resolve(data)
    } catch (error) {
      reject(error)
    }
  })
}

const base64Df =
  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAAAXNSR0IArs4c6QAAALZJREFUKFOFkLERwjAQBPdbgBkInECGaMLUQDsE0AkRVRAYWqAByxldPPOWHwnw4OBGye1p50UDSoA+W2ABLPN7i+C5dyC6R/uiAUXRQCs0bXoNIu4QPQzAxDKxHoALOrZcqtiyR/T6CXw7+3IGHhkYcy6BOR2izwT8LptG8rbMiCRAUb+CQ6WzQVb0SNOi5Z2/nX35DRyb/ENazhpWKoGwrpD6nICp5c2qogc4of+c7QcrhgF4Aa/aoAFHiL+RAAAAAElFTkSuQmCC'

const parseImageToBase64 = url => {
  return new Promise((res, rej) => {
    fetch(url)
      .then(response => response.blob())
      .then(blob => {
        const reader = new FileReader()
        reader.onloadend = () => {
          const base64data = reader.result
          res(base64data)
          // 在这里可以将base64数据用于进一步处理或显示图片
        }
        reader.readAsDataURL(blob)
      })
      .catch(error => {
        console.log('发生错误:', error)
      })
  })
}

function get_position_style (ctx, widget_width, y, node_height) {
  const MARGIN = 12 // the margin around the html element

  /* Create a transform that deals with all the scrolling and zooming */
  const elRect = ctx.canvas.getBoundingClientRect()
  const transform = new DOMMatrix()
    .scaleSelf(
      elRect.width / ctx.canvas.width,
      elRect.height / ctx.canvas.height
    )
    .multiplySelf(ctx.getTransform())
    .translateSelf(MARGIN, MARGIN + y)

  return {
    transformOrigin: '0 0',
    transform: transform,
    left: `0`,
    top: `0`,
    cursor: 'pointer',
    position: 'absolute',
    maxWidth: `${widget_width - MARGIN * 2}px`,
    // maxHeight: `${node_height - MARGIN * 2}px`, // we're assuming we have the whole height of the node
    width: `${widget_width - MARGIN * 2}px`,
    // height: `${node_height * 0.3 - MARGIN * 2}px`,
    // background: '#EEEEEE',
    display: 'flex',
    flexDirection: 'column',
    // alignItems: 'center',
    justifyContent: 'flex-start',
    zIndex: 9999999
  }
}

async function drawImageToCanvas (imageUrl, sFactor = 320) {
  var canvas = document.createElement('canvas')
  var ctx = canvas.getContext('2d')
  var img = new Image()

  await new Promise((resolve, reject) => {
    img.onload = function () {
      var scaleFactor = sFactor / img.width
      var canvasWidth = img.width * scaleFactor
      var canvasHeight = img.height * scaleFactor

      canvas.width = canvasWidth
      canvas.height = canvasHeight

      ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight)

      resolve()
    }

    img.onerror = function () {
      reject(new Error('Failed to load image'))
    }

    img.src = imageUrl
  })

  var base64 = canvas.toDataURL('image/jpeg')
  // console.log(base64); // 输出Base64数据
  return base64
  // 可以在这里执行其他操作,比如将Base64数据保存到服务器或显示在页面上
}

async function extractInputAndOutputData (
  jsonData,
  inputIds = [],
  outputIds = []
) {
  // workflow
  // const workflow=jsonData.workflow;
  // const nodes=workflow.nodes;

  const data = jsonData.output
  let input = []
  let output = []
  const seed = {}
  const seedTitle = {}

  for (const id in data) {
    if (data.hasOwnProperty(id)) {
      let node = app.graph.getNodeById(id)
      if (inputIds.includes(id)) {
        // let node = app.graph.getNodeById(id)
        let options = {}
        // 模型
        try {
          if (node.type === 'CheckpointLoaderSimple') {
            options = node.widgets.filter(w => w.name === 'ckpt_name')[0]
              .options.values
          } else if (node.type === 'LoraLoader') {
            options = node.widgets.filter(w => w.name === 'lora_name')[0]
              .options.values
          }
        } catch (error) {}

        if (node.type == 'IntNumber' || node.type == 'FloatSlider') {
          // min max step
          let [v, min, max, step] = Array.from(node.widgets, w => w.value)
          options = { min, max, step }
          // node.widgets.filter(w => w.type === 'number')[0].options
        }

        if (node.type == 'PromptSlide') {
          // min max step
          options = node.widgets.filter(w => w.type === 'slider')[0].options
          // 备选的keywords清单
          try {
            let keywords = node.widgets.filter(w => w.name === 'upload')[0]
              .value
            keywords = JSON.parse(keywords)
            options.keywords = keywords
          } catch (error) {
            console.log(error)
          }
        }

        if (node.type == 'ImagesPrompt_') {
          //图库
          // console.log('ImagesPrompt_', data[id])
          let image_base64 = data[id].inputs.image_base64
          let img_index = 0
          let imgsData = JSON.parse(data[id].inputs.upload)
          for (let index = 0; index < imgsData.length; index++) {
            const imgd = imgsData[index].imgurl
            imgsData[index].index = index
            //TODO缩放大小
            imgsData[index].imgurl = await parseImageToBase64(imgd)
            if (image_base64 == imgsData[index].imgurl) {
              img_index = index
            }
          }
          options.images = imgsData
          delete data[id].inputs.upload
          delete data[id].inputs.image_base64

          data[id].inputs.imageIndex = img_index
        }

        if (node.type == 'Color') {
        }

        // 语音输入的支持
        if (node.type == 'LoadAndCombinedAudio_') {
          // if (
          //   data[id].widgets_values &&
          //   data[id].widgets_values[0] &&
          //   data[id].widgets_values[0].base64 &&
          //   data[id].widgets_values[0].base64.length > 0
          // ) {
          //   options.defaultBase64 = data[id].widgets_values[0].base64
          // }

          input[inputIds.indexOf(id)] = {
            ...data[id],
            title: node.title,
            id,
            options
          }
        }

        if (node.type === 'LoadImage') {
          // loadImage的mask支持
          let output = node.outputs.filter(ot => ot.type == 'MASK')[0]
          if (output.links) {
            // 有输出
            options.hasMask = true
          }
          // loadImage的默认图,转为base64
          let imgurl = app.graph.getNodeById(id).imgs[0].src + '&channel=rgb'

          options.defaultImage = await drawImageToCanvas(imgurl, 512)
          console.log('#loadImage的默认图', options)
        }

        input[inputIds.indexOf(id)] = {
          ...data[id],
          title: node.title,
          id,
          options
        }
        // input.push()
      }
      if (outputIds.includes(id)) {
        let options = {}
        //输出的默认图
        if (
          node.type === 'SaveImageAndMetadata_' &&
          app.graph.getNodeById(id).imgs
        ) {
          // SaveImageAndMetadata_的默认图,转为base64
          let imgurl = app.graph.getNodeById(id).imgs[0].src

          options.defaultImage = await drawImageToCanvas(imgurl, 512)
          console.log('#SaveImageAndMetadata_的默认图', options)
        }

        // let node = app.graph.getNodeById(id)
        // output.push()
        output[outputIds.indexOf(id)] = {
          ...data[id],
          title: node.title,
          id,
          options
        }
      }

      if (
        node.type === 'KSampler' ||
        node.type == 'SamplerCustom' ||
        node.type === 'ChinesePrompt_Mix' ||
        node.type === 'Seed_'
      ) {
        // seed 的类型收集
        try {
          seed[id] = node.widgets.filter(
            w => w.name === 'seed' || w.name == 'noise_seed'
          )[0].linkedWidgets[0].value
          seedTitle[id] = node.title
        } catch (error) {}
      }
    }
  }

  // 修复bug,当节点不存在时
  input = input.filter(i => i)
  output = output.filter(i => i)

  return { input, output, seed, seedTitle }
}

function getUrl () {
  let api_host = `${window.location.hostname}:${window.location.port}`
  let api_base = ''
  let url = `${window.location.protocol}//${api_host}${api_base}`
  return url
}

const getLocalData = key => {
  let data = {}
  try {
    data = JSON.parse(localStorage.getItem(key)) || {}
  } catch (error) {
    return {}
  }
  return data
}

async function save_app (json) {
  let url = getUrl()

  const res = await fetch(`${url}/mixlab/workflow`, {
    method: 'POST',
    body: JSON.stringify({
      data: json,
      task: 'save_app',
      filename: json.app.filename,
      category: json.app.category
    })
  })
  return await res.json()
}

function downloadJsonFile (jsonData, fileName = 'mix_app.json') {
  const dataString = JSON.stringify(jsonData)
  const blob = new Blob([dataString], { type: 'application/json' })
  const url = URL.createObjectURL(blob)

  const link = document.createElement('a')
  link.href = url
  link.download = fileName
  link.click()

  // 释放URL对象
  setTimeout(() => {
    URL.revokeObjectURL(url)
  }, 0)
}

async function save (json, download = false, showInfo = true) {
  let nodesAll = window._nodesAll || (await getObjectInfo())

  console.log('####SAVE', nodesAll, json[0])

  const name = json[0],
    version = json[5],
    share_prefix = json[6], //用于分享的功能扩展
    link = json[7], //用于创建界面上的跳转链接
    category = json[8] || '', //用于分类
    description = json[4],
    inputIds = json[2].split('\n').filter(f => f),
    outputIds = json[3].split('\n').filter(f => f)

  const iconData = json[1][0]
  let { filename, subfolder, type } = iconData
  let iconUrl = api.apiURL(
    `/view?filename=${encodeURIComponent(
      filename
    )}&type=${type}&subfolder=${subfolder}${app.getPreviewFormatParam()}${app.getRandParam()}`
  )

  try {
    let data = await app.graphToPrompt()

    //从output数据里把工作流的节点,插件数据统计出来
    data.nodesMap = {}
    for (const id in data.output) {
      data.nodesMap[data.output[id].class_type] =
        nodesAll[data.output[id].class_type]
    }

    let { input, output, seed, seedTitle } = await extractInputAndOutputData(
      data,
      inputIds,
      outputIds
    )

    let authorAvatar =
        localStorage.getItem('_mixlab_author_avatar') || base64Df,
      authorName =
        localStorage.getItem('_mixlab_author_name') ||
        localStorage.getItem('Comfy.userName'),
      authorLink = localStorage.getItem('_mixlab_author_link') || ''

    data.app = {
      name,
      description,
      version,
      input,
      output,
      seed, //控制是fixed 还是random
      seedTitle,
      share_prefix,
      link,
      category,
      filename: `${name}_${version}.json`,
      author: {
        avatar: authorAvatar,
        name: authorName,
        link: authorLink
      }
    }

    try {
      data.app.icon = await drawImageToCanvas(iconUrl)
    } catch (error) {}
    // console.log(data.app)
    // let http_workflow = app.graph.serialize()
    await save_app(data)
    if (download) {
      await downloadJsonFile(data, data.app.filename)
    }

    if (showInfo) {
      let open = window.confirm(
        `You can now access the standalone application on a new page!\n${getUrl()}/mixlab/app?filename=${encodeURIComponent(
          data.app.filename
        )}&category=${encodeURIComponent(data.app.category)}`
      )
      if (open)
        window.open(
          `${getUrl()}/mixlab/app?filename=${encodeURIComponent(
            data.app.filename
          )}&category=${encodeURIComponent(data.app.category)}`
        )
    }
  } catch (error) {
    console.log('###error', error)
  }
}

function getInputsAndOutputs () {
  const inputs =
      `LoadImage LoadImagesToBatch ImagesPrompt_ LoadAndCombinedAudio_ VHS_LoadVideo CLIPTextEncode PromptSlide TextInput_ Color FloatSlider IntNumber CheckpointLoaderSimple LoraLoader`.split(
        ' '
      ),
    outputs =
      `SaveTripoSRMesh,PreviewImage,SaveImage,TransparentImage,ShowTextForGPT,CombineAudioVideo,VHS_VideoCombine,VideoCombine_Adv,Image Save,SaveImageAndMetadata_,ClipInterrogator`.split(
        ','
      )

  let inputsId = [],
    outputsId = []

  for (let node of app.graph._nodes) {
    if (inputs.includes(node.type)) {
      inputsId.push(node.id)
    }

    if (outputs.includes(node.type)) {
      outputsId.push(node.id)
    }
  }

  return {
    input: inputsId,
    output: outputsId
  }
}

app.registerExtension({
  name: 'Mixlab.utils.AppInfo',
  init () {
    if (!window._nodesAll) {
      getObjectInfo().then(r => (window._nodesAll = r))
    }
  },
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'AppInfo') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = function () {
        orig_nodeCreated?.apply(this, arguments)
        // console.log('#orig_nodeCreated', this)

        // 自动计算workflow里哪些节点支持
        let input_ids = this.widgets.filter(w => w.name == 'input_ids')[0],
          output_ids = this.widgets.filter(w => w.name == 'output_ids')[0]

        const { input, output } = getInputsAndOutputs()
        input_ids.value = input.join('\n')
        output_ids.value = output.join('\n')

        const widget = {
          type: 'div',
          name: 'AppInfoRun',
          draw (ctx, node, widget_width, y, widget_height) {
            Object.assign(
              this.div.style,
              get_position_style(
                ctx,
                widget_width,
                node.size[1] - widget_height,
                node.size[1]
              )
            )
          }
        }

        const style = `
        flex-direction: row;
        background-color: var(--comfy-input-bg);
        border-radius: 8px;
        border-color: var(--border-color);
        border-style: solid;
        color: var(--descrip-text);`

        widget.div = $el('div', {})

        const btn = document.createElement('button')
        btn.innerText = 'Save & Open'
        btn.style = style

        btn.addEventListener('click', () => {
          //   console.log('hahhah')
          if (window._mixlab_app_json) {
            save(window._mixlab_app_json)
          } else {
            alert('Please run the workflow before saving')
            // app.queuePrompt(0, 1)
            this.widgets.filter(w => w.name === 'version')[0].value += 1
          }
        })

        const download = document.createElement('button')
        download.innerText = 'Download For App'
        download.style = style
        download.style.marginLeft = '12px'

        download.addEventListener('click', () => {
          //   console.log('hahhah')
          if (window._mixlab_app_json) {
            save(window._mixlab_app_json, true)
          } else {
            alert('Please run the workflow before saving')
            // app.queuePrompt(0, 1)
            this.widgets.filter(w => w.name === 'version')[0].value += 1
          }
        })

        //td bg
        const tdBG = document.createElement('button')
        tdBG.innerText = 'Canvas Mode'
        tdBG.style = style
        tdBG.style.marginLeft = '12px'

        tdBG.addEventListener('click', () => {
          td_bg.toggle()
          if (td_bg.running) {
            tdBG.style.background = 'yellow'
          } else {
            tdBG.style.background = 'transparent'
          }
        })

        // author
        let author = document.createElement('div')
        // author.style=`display: flex`

        let authorAvatar = document.createElement('img')
        authorAvatar.className = `${'comfy-multiline-input'}`
        authorAvatar.style = `outline: none;
          border: none;
          padding: 4px;
          width: 32px;
          cursor: pointer;
          height: 32px;`

        if (localStorage.getItem('_mixlab_author_avatar')) {
          authorAvatar.src =
            localStorage.getItem('_mixlab_author_avatar') || base64Df
        }

        let authorAvatarUpload = document.createElement('input')
        authorAvatarUpload.type = 'file'
        authorAvatarUpload.style = `display:none`

        let authorAvatarInput = document.createElement('div')
        authorAvatarInput.style = `display: flex;justify-content: flex-start;
        align-items: center;`
        let authorAvatarInputLabel = document.createElement('p')
        authorAvatarInputLabel.innerText = 'Author Avatar'
        authorAvatarInputLabel.className = `${'comfy-multiline-input'}`
        authorAvatarInputLabel.style = `font-size:12px`

        authorAvatar.addEventListener('click', e => {
          authorAvatarUpload.click()
        })

        authorAvatarInputLabel.addEventListener('click', e => {
          authorAvatarUpload.click()
        })

        authorAvatarUpload.addEventListener('change', event => {
          const file = event.target.files[0]
          const reader = new FileReader()

          reader.onload = async e => {
            let im = new Image()
            im.src = e.target.result
            authorAvatar.src = e.target.result
            im.onload = () => {
              let c = document.createElement('canvas')
              let ctx = c.getContext('2d')
              c.width = 72
              c.height = 72
              ctx.drawImage(
                im,
                0,
                0,
                im.naturalWidth,
                im.naturalHeight,
                0,
                0,
                c.width,
                c.height
              )
              window._mixlab_author_avatar = c.toDataURL()
              localStorage.setItem(
                '_mixlab_author_avatar',
                window._mixlab_author_avatar
              )
            }
          }

          // 以文本形式读取文件
          reader.readAsDataURL(file)
        })

        author.appendChild(authorAvatarInput)
        authorAvatarInput.appendChild(authorAvatarInputLabel)
        authorAvatarInput.appendChild(authorAvatar)
        authorAvatarInput.appendChild(authorAvatarUpload)

        let authorName = document.createElement('input')
        authorName.type = 'text'
        authorName.value =
          localStorage.getItem('_mixlab_author_name') ||
          localStorage.getItem('Comfy.userName')
        authorName.placeholder = 'author name'
        authorName.className = `${'comfy-multiline-input'}`
        authorName.style = `
          outline: none;
          border: none;
          padding: 4px;
          width: 100%;
          cursor: pointer;
          height: 32px;`

        let authorNameInput = document.createElement('div')
        authorNameInput.style = `display: flex;justify-content: flex-start;
          align-items: center;`
        let authorNameInputLabel = document.createElement('p')
        authorNameInputLabel.innerText = 'Author Name'
        authorNameInputLabel.className = `${'comfy-multiline-input'}`
        authorNameInputLabel.style = `font-size:12px;width: 110px`

        authorName.addEventListener('change', e => {
          window._mixlab_author_name = authorName.value.trim()
          localStorage.setItem(
            '_mixlab_author_name',
            window._mixlab_author_name
          )
        })

        author.appendChild(authorNameInput)
        authorNameInput.appendChild(authorNameInputLabel)
        authorNameInput.appendChild(authorName)

        // 社交链接
        let authorLink = document.createElement('input')
        authorLink.type = 'text'
        authorLink.value = localStorage.getItem('_mixlab_author_link') || ''
        authorLink.placeholder = 'author link'
        authorLink.className = `${'comfy-multiline-input'}`
        authorLink.style = `
          outline: none;
          border: none;
          padding: 4px;
          width: 100%;
          cursor: pointer;
          height: 32px;`

        let authorLinkInput = document.createElement('div')
        authorLinkInput.style = `display: flex;justify-content: flex-start;
          align-items: center;`
        let authorLinkInputLabel = document.createElement('p')
        authorLinkInputLabel.innerText = 'Author Link'
        authorLinkInputLabel.className = `${'comfy-multiline-input'}`
        authorLinkInputLabel.style = `font-size:12px;width: 110px`

        authorLink.addEventListener('change', e => {
          window._mixlab_author_link = authorLink.value.trim()
          localStorage.setItem(
            '_mixlab_author_link',
            window._mixlab_author_link
          )
        })

        author.appendChild(authorLinkInput)
        authorLinkInput.appendChild(authorLinkInputLabel)
        authorLinkInput.appendChild(authorLink)

        widget.div.appendChild(author)

        let btns = document.createElement('div')

        widget.div.appendChild(btns)

        btns.appendChild(btn)
        btns.appendChild(download)
        btns.appendChild(tdBG)

        document.body.appendChild(widget.div)
        this.addCustomWidget(widget)

        const onRemoved = this.onRemoved
        this.onRemoved = () => {
          widget.div.remove()
          return onRemoved?.()
        }

        this.serialize_widgets = true //需要保存参数

        window._mixlab_app_json = null
 
      }

      const onExecuted = nodeType.prototype.onExecuted
      nodeType.prototype.onExecuted = function (message) {
        onExecuted?.apply(this, arguments)
        console.log(message.json)
        window._mixlab_app_json = message.json
        try {
          let a = this.widgets.filter(w => w.name === 'AppInfoRun')[0]
          if (a) {
            if (!a.value) a.value = 0
            a.value += 1
          }

          const div = this.widgets.filter(w => w.div)[0].div
          Array.from(div.querySelectorAll('button'), b =>
            b.innerText != 'Canvas Mode' ? (b.style.background = 'yellow') : ''
          )
        } catch (error) {}
      }
    }
  },
  async loadedGraphNode (node, app) {
    // console.log('#loadedGraphNode1111')
    window._mixlab_app_json = null //切换workflow需要清空
    if (node.type === 'AppInfo') {
      let auto_save = node.widgets.filter(w => w.name == 'auto_save')[0]
      if (auto_save) {
        if (!['enable', 'disable'].includes(auto_save.value)) {
          auto_save.value = 'enable'
        }
      }

      // app.canvas.centerOnNode(node)
      // app.canvas.setZoom(0.45)
    }
  }
})

api.addEventListener('execution_start', async ({ detail }) => {
  console.log('#execution_start', detail)
  window._mixlab_app_json = null
})

api.addEventListener('executed', async ({ detail }) => {
  console.log('#executed', detail)
  // window._mixlab_app_json=null;
  const { output } = getInputsAndOutputs()
  if (output.includes(parseInt(detail.node))) {
    let appinfo = app.graph.findNodesByType('AppInfo')[0]
    if (appinfo) {
      let auto_save = appinfo.widgets.filter(w => w.name == 'auto_save')[0]
      if (auto_save?.value === 'enable') {
        // 自动保存
        console.log('auto_save')
        if (window._mixlab_app_json) save(window._mixlab_app_json, false, false)
      }
    }
  }
})