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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
import { app } from '../../../scripts/app.js'
import { api } from '../../../scripts/api.js'
import { ComfyWidgets } from '../../../scripts/widgets.js'
import { $el } from '../../../scripts/ui.js'

function downloadJsonFile (jsonData, fileName = 'grid.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)
}

function createSelectWithOptions (options) {
  const select = document.createElement('select')

  options.forEach(option => {
    const optionElement = document.createElement('option')
    optionElement.text = option
    optionElement.value = option
    select.appendChild(optionElement)
  })

  select.style = `cursor: pointer;
  font-weight: 300; 
  height: 30px;
  min-width: 122px;
  position: absolute;
  top: 24px;
  left: 88px;
  z-index: 999999999999999;
       `

  return select
}

function drawCanvasWithText (w, h, tag, color = 'rgba(255,255,255,0.4)') {
  const canvas = document.createElement('canvas')
  const ctx = canvas.getContext('2d')

  // 设置画布大小
  canvas.width = w
  canvas.height = h

  // 绘制白色背景
  ctx.fillStyle = color
  ctx.fillRect(0, 0, canvas.width, canvas.height)

  // 绘制文字
  ctx.fillStyle = '#000000'
  ctx.font = '20px Arial'
  ctx.fillText(tag, 50, 50)

  // 导出为Base64
  const base64 = canvas.toDataURL()

  return base64
}

function get_position_style (ctx, widget_width, y, node_height) {
  const MARGIN = 4 // 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: 'space-around'
  }
}

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

function createImage (url) {
  let im = new Image()
  return new Promise((res, rej) => {
    im.onload = () => res(im)
    im.src = url
  })
}

const parseSvg = async svgContent => {
  // 创建一个临时的DOM元素来解析SVG
  const tempContainer = document.createElement('div')
  tempContainer.innerHTML = svgContent

  // 提取SVG元素
  const svgElement = tempContainer.querySelector('svg')
  if (!svgElement) return
  // 获取SVG中 rect元素
  var rectElements = svgElement?.querySelectorAll('rect') || []
  // console.log(rectElements,svgElement)
  // 定义一个数组来存储处理后的数据
  var data = []

  Array.from(rectElements, (rectElement, i) => {
    // 获取rect元素的属性值
    var x = ~~(rectElement.getAttribute('x') || 0)
    var y = ~~(rectElement.getAttribute('y') || 0)
    var width = ~~rectElement.getAttribute('width')
    var height = ~~rectElement.getAttribute('height')
    // console.log('rectElements',rectElement,x,y,width,height)
    if (x != undefined && y != undefined && width && height) {
      // 创建一个新的canvas元素
      var canvas = document.createElement('canvas')
      canvas.width = width
      canvas.height = height
      var context = canvas.getContext('2d')

      // 填充颜色到canvas
      var fill = rectElement.getAttribute('fill')
      context.fillStyle = fill
      context.fillRect(0, 0, width, height)

      // 将canvas转换为base64格式
      var base64 = canvas.toDataURL()

      // 将数据转化为指定的JSON格式

      var rectData = {
        x: parseInt(x),
        y: parseInt(y),
        width: parseInt(width),
        height: parseInt(height),
        z_index: i + 1,
        scale_option: 'width',
        image: base64,
        mask: base64,
        type: 'base64',
        _t: 'rect'
      }

      // 将处理后的数据添加到数组中
      data.push(rectData)
    }
  })

  var svgWidth = svgElement.getAttribute('width')
  var svgHeight = svgElement.getAttribute('height')

  if (!(svgWidth && svgHeight)) {
    // viewBox
    let viewBox = svgElement.viewBox.baseVal

    svgWidth = viewBox.width
    svgHeight = viewBox.height
  }

  // 创建一个新的canvas元素
  var canvas = document.createElement('canvas')
  canvas.width = svgWidth
  canvas.height = svgHeight
  var context = canvas.getContext('2d')
  // 绘制SVG到canvas
  var svgString = new XMLSerializer().serializeToString(svgElement)
  var DOMURL = window.URL || window.webkitURL || window

  var svgBlob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' })
  var url = DOMURL.createObjectURL(svgBlob)

  let img = await createImage(url)
  context.drawImage(img, 0, 0)

  let base64 = canvas.toDataURL()

  var rectData = {
    x: 0,
    y: 0,
    width: parseInt(svgWidth),
    height: parseInt(svgHeight),
    z_index: 0,
    scale_option: 'width',
    image: base64,
    mask: base64,
    type: 'base64',
    _t: 'canvas'
  }
  data.push(rectData)

  // 打印处理后的数据
  console.log('layers', { data, image: base64, svgElement })
  return { data, image: base64, svgElement }
}

function findImages (nodeId) {
  // 检查当前节点是否有 imgs 字段
  const n = app.graph.getNodeById(nodeId)
  if (n.imgs) {
    return n.imgs
  }

  // 检查当前节点的 inputs 是否有 image 字段
  if (n.inputs) {
    for (let i = 0; i < n.inputs.length; i++) {
      if (n.inputs[i].name === 'image' || n.inputs[i].name === 'images') {
        // 获取新的 nodeId,并递归调用 findImages 函数
        var linkId = n.inputs[i]?.link
        var origin_id = app.graph.links[linkId].origin_id
        return findImages(origin_id)
      }
    }
  }

  // 如果没有找到 imgs 字段或者 image 字段,则返回 null
  return null
}

async function setArea (cw, ch, topBase64, base64, data, fn) {
  let displayHeight = Math.round(window.screen.availHeight * 0.8)
  let div = document.createElement('div')
  div.innerHTML = `
    <div id='ml_overlay' style='position: absolute;top:0;background: #251f1fc4;
    height: 100vh;
    z-index:999999;
    width: 100%;'>
      <img id='ml_video' style='position: absolute; 
      height: ${displayHeight}px;user-select: none; 
      -webkit-user-drag: none;
      outline: 2px solid #eaeaea;
      box-shadow: 8px 9px 17px #575757;' />
      <div id='ml_selection' style='position: absolute; 
      border: 2px dashed red;
      pointer-events: none;
      background-image: url("${topBase64}");
            background-repeat: no-repeat;
            background-size: cover;
            '></div>
      <div class="mx_close"> X </div>
    </div>`
  // document.body.querySelector('#ml_overlay')
  document.body.appendChild(div)

  // let canvas = document.createElement('canvas')
  // canvas.width = cw
  // canvas.height = ch

  let img = div.querySelector('#ml_video')
  // let overlay = div.querySelector('#ml_overlay')
  let selection = div.querySelector('#ml_selection')
  let close = div.querySelector('.mx_close')
  let startX, startY, endX, endY
  let start = false
  let setDone = false
  // Set video source
  img.src = base64
  // canvas.toDataURL();
  close.style = `cursor: pointer;
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 99999999;
  background: black;
  width: 44px;
  height: 44px;
  text-align: center;
  line-height: 44px;`

  // init area
  // const data = getSetAreaData()
  let x = 0,
    y = 0,
    width = (cw * displayHeight) / ch,
    height = displayHeight

  let imgWidth = cw
  let imgHeight = ch

  if (data && data.width > 0 && data.height > 0) {
    // 相同尺寸窗口,恢复选区
    x = (width * data.x) / imgWidth
    y = (height * data.y) / imgHeight
    width = (width * data.width) / imgWidth
    height = (height * data.height) / imgHeight
  }

  selection.style.left = x + 'px'
  selection.style.top = y + 'px'
  selection.style.width = width + 'px'
  selection.style.height = height + 'px'

  // Add mouse events
  img.addEventListener('mousedown', startSelection)
  img.addEventListener('mousemove', updateSelection)
  img.addEventListener('mouseup', endSelection)

  const removeDiv = () => {
    div.remove()
    close.removeEventListener('click', removeDiv)
    img.removeEventListener('mousedown', startSelection)
    img.removeEventListener('mousemove', updateSelection)
    img.removeEventListener('mouseup', endSelection)
    img.removeEventListener('mousedown', setDoneCheck)
  }
  close.addEventListener('click', removeDiv)

  const setDoneCheck = event => {
    console.log(setDone)
    if (setDone) {
      img.addEventListener('mousedown', startSelection)
      img.addEventListener('mousemove', updateSelection)
      img.addEventListener('mouseup', endSelection)
      setDone = false
      start = false
      startX = event.clientX
      startY = event.clientY
    }
  }
  img.addEventListener('mousedown', setDoneCheck)

  function remove () {
    img.removeEventListener('mousedown', startSelection)
    img.removeEventListener('mousemove', updateSelection)
    img.removeEventListener('mouseup', endSelection)
    setDone = true
    // div.remove()
  }

  function startSelection (event) {
    if (start == false) {
      startX = event.clientX
      startY = event.clientY
      updateSelection(event)
      start = true
    } else {
    }
  }

  function updateSelection (event) {
    endX = event.clientX
    endY = event.clientY

    // Calculate width, height, and coordinates
    let width = Math.abs(endX - startX)
    let height = Math.abs(endY - startY)
    let left = Math.min(startX, endX)
    let top = Math.min(startY, endY)

    // Set selection style
    selection.style.left = left + 'px'
    selection.style.top = top + 'px'
    selection.style.width = width + 'px'
    selection.style.height = height + 'px'
  }

  function endSelection (event) {
    endX = event.clientX
    endY = event.clientY

    // 获取img元素的真实宽度和高度
    let imgWidth = img.naturalWidth
    let imgHeight = img.naturalHeight

    // 换算起始坐标
    let realStartX = (startX / img.offsetWidth) * imgWidth
    let realStartY = (startY / img.offsetHeight) * imgHeight

    // 换算起始坐标
    let realEndX = (endX / img.offsetWidth) * imgWidth
    let realEndY = (endY / img.offsetHeight) * imgHeight

    startX = realStartX
    startY = realStartY
    endX = realEndX
    endY = realEndY
    // Calculate width, height, and coordinates
    let width = Math.round(Math.abs(endX - startX))
    let height = Math.round(Math.abs(endY - startY))
    let left = Math.round(Math.min(startX, endX))
    let top = Math.round(Math.min(startY, endY))

    if (width <= 0 && height <= 0) return remove()

    if (fn) fn(left, top, width, height)

    remove()
  }
}

async function setAreaTags (cw, ch, grids, fn) {
  let base64 = drawCanvasWithText(cw, ch, '', 'white')
  let displayHeight = Math.round(window.screen.availHeight * 0.8)
  let div = document.createElement('div')
  div.innerHTML = `
    <div id='ml_overlay' style='position: absolute;top:0;background: #251f1fc4;
    height: 100vh;
    z-index:999999;
    width: 100%;'>
      <img id='ml_video' style='position: absolute; 
      height: ${displayHeight}px;user-select: none; 
      -webkit-user-drag: none;
      outline: 2px solid #eaeaea;
      box-shadow: 8px 9px 17px #575757;' />
      ${Array.from(grids, g => {
        const { label: tag, grid } = g
        const [dx, dy, dw, dh] = grid
        const base64Data = drawCanvasWithText(dw, dh, tag)

        let x = 0,
          y = 0,
          width = (cw * displayHeight) / ch,
          height = displayHeight

        let imgWidth = cw
        let imgHeight = ch

        if (dw > 0 && dh > 0) {
          // 相同尺寸窗口,恢复选区
          x = (width * dx) / imgWidth
          y = (height * dy) / imgHeight
          width = (width * dw) / imgWidth
          height = (height * dh) / imgHeight
        }

        return `<div class='ml_selection' 
          data-tag="${tag}"
          style='position:absolute; 
          border: 2px dashed red;
          pointer-events: none;
          background-image: url("${base64Data}");
                background-repeat: no-repeat;
                background-size: cover;
                left:${x}px;
                top:${y}px;
                width:${width}px;
                height:${height}px;
                '></div>`
      })}
      <div class="mx_close"> X </div>
    </div>`
  // document.body.querySelector('#ml_overlay')
  document.body.appendChild(div)

  const tags = Array.from(grids, g => g.label)
  let select = createSelectWithOptions(tags)
  document.body.appendChild(select)

  let img = div.querySelector('#ml_video')
  // let overlay = div.querySelector('#ml_overlay')
  let selections = [...div.querySelectorAll('.ml_selection')]

  let selection = selections.filter(
    s => s.getAttribute('data-tag') === select.value
  )[0]

  select.addEventListener('change', e => {
    selection = selections.filter(
      s => s.getAttribute('data-tag') === select.value
    )[0]
  })

  // console.log(select.value,selection)
  let close = div.querySelector('.mx_close')
  let startX, startY, endX, endY
  let start = false
  let setDone = false
  // Set video source
  img.src = base64
  // canvas.toDataURL();
  close.style = `cursor: pointer;
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 99999999;
  background: black;
  width: 44px;
  height: 44px;
  text-align: center;
  line-height: 44px;`

  // Add mouse events
  img.addEventListener('mousedown', startSelection)
  img.addEventListener('mousemove', updateSelection)
  img.addEventListener('mouseup', endSelection)

  const removeDiv = () => {
    div.remove()
    select?.remove()
    close.removeEventListener('click', removeDiv)
    img.removeEventListener('mousedown', startSelection)
    img.removeEventListener('mousemove', updateSelection)
    img.removeEventListener('mouseup', endSelection)
    img.removeEventListener('mousedown', setDoneCheck)
  }
  close.addEventListener('click', removeDiv)

  const setDoneCheck = event => {
    console.log(setDone)
    if (setDone) {
      img.addEventListener('mousedown', startSelection)
      img.addEventListener('mousemove', updateSelection)
      img.addEventListener('mouseup', endSelection)
      setDone = false
      start = false
      startX = event.clientX
      startY = event.clientY
    }
  }
  img.addEventListener('mousedown', setDoneCheck)

  function remove () {
    img.removeEventListener('mousedown', startSelection)
    img.removeEventListener('mousemove', updateSelection)
    img.removeEventListener('mouseup', endSelection)
    setDone = true
    // select?.remove()
  }

  function startSelection (event) {
    if (start == false) {
      startX = event.clientX
      startY = event.clientY
      updateSelection(event)
      start = true
    } else {
    }
  }

  function updateSelection (event) {
    endX = event.clientX
    endY = event.clientY

    // Calculate width, height, and coordinates
    let width = Math.abs(endX - startX)
    let height = Math.abs(endY - startY)
    let left = Math.min(startX, endX)
    let top = Math.min(startY, endY)

    // Set selection style
    selection.style.left = left + 'px'
    selection.style.top = top + 'px'
    selection.style.width = width + 'px'
    selection.style.height = height + 'px'
  }

  function endSelection (event) {
    endX = event.clientX
    endY = event.clientY

    // 获取img元素的真实宽度和高度
    let imgWidth = img.naturalWidth
    let imgHeight = img.naturalHeight

    // 换算起始坐标
    let realStartX = (startX / img.offsetWidth) * imgWidth
    let realStartY = (startY / img.offsetHeight) * imgHeight

    // 换算起始坐标
    let realEndX = (endX / img.offsetWidth) * imgWidth
    let realEndY = (endY / img.offsetHeight) * imgHeight

    startX = realStartX
    startY = realStartY
    endX = realEndX
    endY = realEndY
    // Calculate width, height, and coordinates
    let width = Math.round(Math.abs(endX - startX))
    let height = Math.round(Math.abs(endY - startY))
    let left = Math.round(Math.min(startX, endX))
    let top = Math.round(Math.min(startY, endY))

    if (width <= 0 && height <= 0) return remove()

    if (!!fn) fn(select.value, left, top, width, height)

    remove()
  }
}

app.registerExtension({
  name: 'Mixlab.layer.ShowLayer',
  async getCustomWidgets (app) {
    return {
      EDIT (node, inputName, inputData, app) {
        //   console.log('EditLayer##node', node,inputName, inputData)
        const widget = {
          type: inputData[0], // the type, CHEESE
          name: inputName, // the name, slice
          size: [128, 44], // a default size
          draw (ctx, node, widget_width, y, widget_height) {
            // console.log('EditLayer', this)
            if (this.input)
              Object.assign(
                this.input.style,
                get_position_style(ctx, widget_width, 32, node.size[1])
              )
          },
          computeSize (...args) {
            return [128, 44] // a method to compute the current size of the widget
          },
          async serializeValue (nodeId, widgetIndex) {
            let d = getLocalData('_mixlab_edit_layer')
            // console.log('EditLayer',d[node.id])
            return d[node.id]
          }
        }
        //  widget.something = something;          // maybe adds stuff to it
        node.addCustomWidget(widget) // adds it to the node
        return widget // and returns it.
      }
    }
  },

  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'ShowLayer') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = async function () {
        orig_nodeCreated?.apply(this, arguments)

        const findNode = nodeId => {
          let node = app.graph._nodes_by_id[nodeId]
          if (node?.type == 'Reroute') {
            let linkId = node.inputs.filter(i => i.type == '*')[0].link
            nodeId = app.graph.links.filter(link => link.id == linkId)[0]
              ?.origin_id
            return findNode(nodeId)
          } else {
            return nodeId
          }
        }

        // 获取layers数据
        const getLayers = async () => {
          console.log(
            'getLayers1',
            this.inputs.filter(ip => ip.name === 'layers')
          )
          let linkId = this.inputs.filter(ip => ip.name === 'layers')[0].link
          let nodeId = app.graph.links?.filter(link => link.id == linkId)[0]
            ?.origin_id

          if (nodeId) {
            nodeId = findNode(nodeId)
          }

          //   let node = app.graph._nodes_by_id[nodeId]
          //   if (node?.type == 'Reroute') {
          //     linkId = node.inputs[0].link
          //     nodeId = app.graph.links.filter(link => link.id == linkId)[0]
          //       ?.origin_id
          //   }

          let d = getLocalData('_mixlab_svg_image')
          console.log('test', d[nodeId])

          if (d[nodeId]) {
            let url = d[nodeId]
            let dt = await fetch(url)

            let svgStr = await dt.text()

            const { data } = (await parseSvg(svgStr)) || {}
            console.log('fetch', data)
            return data
          } else {
            return []
          }
        }

        // 修改layers数据
        const setLayer = async (editIndex, layers = null) => {
          //   let editIndex = 0
          let lys = layers || (await getLayers())
          let layer = lys[editIndex]
          //   console.log(layer)

          const updateValue = name => {
            const x = this.widgets.filter(w => w.name == name)[0]
            x.value = layer[name]
          }
          if (layer) {
            Array.from(['x', 'y', 'width', 'height', 'z_index'], n =>
              updateValue(n)
            )
          }
        }

        let that = this
        const save_edit_layer_index = i => {
          let data = getLocalData('_mixlab_edit_layer')
          data[that.id] = i
          localStorage.setItem('_mixlab_edit_layer', JSON.stringify(data))
        }

        await setLayer(0)
        save_edit_layer_index(0)

        const edit = this.widgets.filter(w => w.name == 'edit')[0]

        edit.input = $el('div', {})
        edit.input.style = `
        display: flex;
        flex-direction:row;
        align-items: center;
        margin-top: 0;`

        const ip = $el('input', {})
        ip.className = 'comfy-multiline-input'
        ip.type = 'number'
        ip.min = 0
        ip.step = 1
        ip.max = Math.max(0, (await getLayers()).length - 1)
        // ip.className = `${'comfy-multiline-input'} `

        ip.value = 0

        ip.style = `
        background-color: var(--comfy-input-bg);
        color: var(--input-text);
        outline: none;
          border: none;
          padding: 4px;
          width: 60%;
          cursor: pointer;
          height: 24px;`
        const label = document.createElement('label')
        label.style = 'font-size: 10px;min-width:32px'
        label.innerText = 'Layer Index'
        edit.input.appendChild(label)
        edit.input.appendChild(ip)

        document.body.appendChild(edit.input)

        ip.addEventListener('click', async event => {
          console.log(await getLayers())
          ip.max = Math.max(0, (await getLayers()).length - 1)
        })

        ip.addEventListener('change', async event => {
          let index = ~~ip.value
          let lys = await getLayers()
          await setLayer(index, lys)
          app.graph.setDirtyCanvas(true, true)
          save_edit_layer_index(index)
        })

        // console.log('EditLayer nodeData', edit)

        const onRemoved = this.onRemoved
        this.onRemoved = () => {
          edit.input.remove()
          return onRemoved?.()
        }

        if (this.onResize) {
          this.onResize(this.size)
        }

        this.serialize_widgets = false //需要保存参数
      }
    }
  },
  async loadedGraphNode (node, app) {
    // Fires every time a node is constructed
    // You can modify widgets/add handlers/etc here
    if (node.type === 'SvgImage') {
      let widget = node.widgets.filter(w => w.div)[0]
      let data = getLocalData('_mixlab_svg_image')
      let id = node.id

      // widget.div.querySelector('.Svg').value = data[id] || '#000000'
    }
  }
})

app.registerExtension({
  name: 'Mixlab.layer.NewLayer',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeData.name === 'NewLayer') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = async function () {
        orig_nodeCreated?.apply(this, arguments)

        let b = this.widgets.filter(w => w.type === 'button')[0]
        // const [w, h, base64] = canvas

        if (!b) {
          const updateValue = (x1, y1, w1, h1) => {
            if (this.widgets) {
              for (const widget of this.widgets) {
                if (widget.name === 'x') {
                  widget.value = x1
                }
                if (widget.name === 'y') {
                  widget.value = y1
                }
                if (widget.name === 'width') {
                  widget.value = w1
                }
                if (widget.name === 'height') {
                  widget.value = h1
                }
              }
            }
          }

          this.addWidget('button', 'Set Area', '', () => {
            let data = {}
            for (const widget of this.widgets) {
              if (widget.name === 'x') {
                data.x = widget.value
              }
              if (widget.name === 'y') {
                data.y = widget.value
              }
              if (widget.name === 'width') {
                data.width = widget.value
              }
              if (widget.name === 'height') {
                data.height = widget.value
              }
            }
            try {
              console.log('this.inputs', this.id)
              let imgs = findImages(this.id)

              // let topLinkId = this.inputs[0].link
              // let topNodeId = app.graph.links[topLinkId].origin_id
              let topIm = imgs[0]

              let linkId = this.inputs[3].link
              let nodeId = app.graph.links[linkId].origin_id
              // console.log(linkId,this.inputs)
              let imgs2 = findImages(nodeId)
              let im = imgs2[0]
              console.log(topIm, im)
              // let src = im.src
              setArea(
                im.naturalWidth,
                im.naturalHeight,
                topIm.src,
                im.src,
                data,
                updateValue
              )
            } catch (error) {
              console.log(error)
            }
          })
        }
      }

      const onRemoved = this.onRemoved
      this.onRemoved = () => {
        // let b = this.widgets.filter(w => w.type === 'button')[0];

        return onRemoved?.()
      }

      if (this.onResize) {
        this.onResize(this.size)
      }

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

app.registerExtension({
  name: 'Mixlab.layer.GridInput',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'GridInput') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = async function () {
        orig_nodeCreated?.apply(this, arguments)

        const grids_widget = this.widgets.filter(w => w.name == 'grids')[0]

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

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

        const addBtn = document.createElement('button')
        addBtn.innerText = 'Add Box'
        addBtn.style = `cursor: pointer;
        font-weight: 300;
        margin: 2px; 
        color: var(--descrip-text);
        background-color: var(--comfy-input-bg);
        border-radius: 8px;
        border-color: var(--border-color);
        border-style: solid;height: 30px;min-width: 122px;
       `

        const vbtn = document.createElement('button')
        vbtn.innerText = 'Set Box'
        vbtn.style = `cursor: pointer;
        font-weight: 300;
        margin: 2px; 
        color: var(--descrip-text);
        background-color: var(--comfy-input-bg);
        border-radius: 8px;
        border-color: var(--border-color);
        border-style: solid;height: 30px;min-width: 122px;
       `

        const btn = document.createElement('button')
        btn.innerText = 'Upload JSON'

        btn.style = `cursor: pointer;
        font-weight: 300;
        margin: 2px; 
        color: var(--descrip-text);
        background-color: var(--comfy-input-bg);
        border-radius: 8px;
        border-color: var(--border-color);
        border-style: solid;height: 30px;min-width: 122px;
       `

        addBtn.addEventListener('click', () => {
          const { width, height, grids } = JSON.parse(grids_widget.value)
          grids.push({
            label: 'background',
            grid: [12, 12, width - 24, height - 24]
          })
          grids_widget.value = JSON.stringify(
            {
              width,
              height,
              grids
            },
            null,
            2
          )
        })

        vbtn.addEventListener('click', () => {
          const { width, height, grids } = JSON.parse(grids_widget.value)

          setAreaTags(width, height, grids, (tag, x, y, w, h) => {
            grids_widget.value = JSON.stringify(
              {
                width,
                height,
                grids: Array.from(grids, g => {
                  if (g.label === tag) {
                    g.grid = [x, y, w, h]
                  }
                  return g
                })
              },
              null,
              2
            )
          })
        })

        btn.addEventListener('click', () => {
          let inp = document.createElement('input')
          inp.type = 'file'
          inp.accept = '.json'
          inp.click()
          inp.addEventListener('change', event => {
            // 获取选择的文件
            const file = event.target.files[0]
            this.title = file.name.split('.')[0]

            // console.log(file.name.split('.')[0])
            // 创建文件读取器
            const reader = new FileReader()

            // 定义读取完成事件的回调函数
            reader.onload = event => {
              // 读取完成后的文本内容
              const fileContent = JSON.parse(event.target.result)
              const grids = fileContent
              grids_widget.value = JSON.stringify(grids, null, 2)
              // widget.value = grids

              inp.remove()
            }

            // 以文本方式读取文件
            reader.readAsText(file)
          })
        })

        widget.div.appendChild(addBtn)
        widget.div.appendChild(vbtn)
        widget.div.appendChild(btn)
        document.body.appendChild(widget.div)
        this.addCustomWidget(widget)

        const onExecuted = nodeType.prototype.onExecuted
        nodeType.prototype.onExecuted = function (message) {
          const r = onExecuted?.apply?.(this, arguments)

          let json = message.json
          if (json) {
            json = {
              width: json[0],
              height: json[1],
              grids: json[2]
            }
            grids_widget.value = JSON.stringify(json, null, 2)
            // widget.value = json
          }

          return r
        }

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

        if (this.onResize) {
          this.onResize(this.size)
        }

        this.serialize_widgets = true //需要保存参数
      }
    }
  },
  async loadedGraphNode (node, app) {
    if (node.type === 'GridInput') {
      try {
        const grids_widget = node.widgets.filter(w => w.name == 'grids')[0]
        const { width, height, grids } = JSON.parse(grids_widget.value)
        console.log('#GridInput', node, grids)

        const div = node.widgets.filter(w => w.name == 'upload')[0]
        div.div.querySelector('select').innerHTML = Array.from(
          grids,
          g => `<option value="${g.label}">${g.label}</option>`
        ).join('')
      } catch (error) {}
    }
  }
})

app.registerExtension({
  name: 'Mixlab.layer.GridDisplayAndSave',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'GridDisplayAndSave') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = async function () {
        orig_nodeCreated?.apply(this, arguments)

        const grids_widget = this.widgets.filter(w => w.name == 'grids')[0]
        console.log('GridDisplayAndSave', grids_widget)
        const widget = {
          type: 'div',
          name: 'save_json',
          draw (ctx, node, widget_width, y, widget_height) {
            Object.assign(
              this.div.style,
              get_position_style(ctx, widget_width, y, node.size[1]),
              {
                justifyContent: 'flex-start',
                flexDirection: 'column'
              }
            )
          }
        }

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

        const btn = document.createElement('button')
        btn.innerText = 'Save JSON'

        btn.style = `cursor: pointer;
        font-weight: 300;
        margin: 2px; 
        color: var(--descrip-text);
        background-color: var(--comfy-input-bg);
        border-radius: 8px;
        border-color: var(--border-color);
        border-style: solid;height: 30px;min-width: 122px;
        max-width: 122px;
       `

        btn.addEventListener('click', () => {
          if (window._mixlab_grid)
            downloadJsonFile(
              window._mixlab_grid,
              this.widgets.filter(w => w.name == 'filename_prefix')[0]?.value +
                '_grid.json'
            )
        })

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

        const onExecuted = nodeType.prototype.onExecuted
        nodeType.prototype.onExecuted = function (message) {
          const r = onExecuted?.apply?.(this, arguments)
          let save_json = this.widgets.filter(d => d.name == 'save_json')[0]
          let div = save_json?.div
          // console.log('Test',message)

          let image = message.image[0]
          let json = message.json
          if (image) {
            const { filename, subfolder, type } = image

            if (!div.querySelector('img')) {
              let im = new Image()
              div.appendChild(im)
              im.style.width = '100%'
            }
            div.querySelector('img').src = api.apiURL(
              `/view?filename=${encodeURIComponent(
                filename
              )}&type=${type}&subfolder=${subfolder}${app.getPreviewFormatParam()}${app.getRandParam()}`
            )

            window._mixlab_grid = {
              width: json[0],
              height: json[1],
              grids: json[2]
            }
            // console.log(src)
          }

          this.onResize?.(this.size)

          return r
        }

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

        if (this.onResize) {
          this.onResize(this.size)
        }

        this.serialize_widgets = true //需要保存参数
      }
    }
  },
  async loadedGraphNode (node, app) {
    if (node.type === 'GridDisplayAndSave') {
      try {
        let grids_widget = node.widgets.filter(w => w.name === 'grids')[0]
        // let ks = getLocalData(`_mixlab_PromptSlide`)
        let uploadWidget = node.widgets.filter(w => w.name == 'upload')[0]
        // console.log('##widget', uploadWidget.value)
        let grids = JSON.parse(uploadWidget.value)
      } catch (error) {}
    }
  }
})