-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
994 lines (868 loc) · 28.2 KB
/
Copy patheditor.js
File metadata and controls
994 lines (868 loc) · 28.2 KB
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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const statusEl = document.getElementById('status');
const toastContainer = document.getElementById('toast-container');
// Modales
const confirmDialog = document.getElementById('confirm-dialog');
const textDialog = document.getElementById('text-dialog');
const textInput = document.getElementById('text-input');
const aboutDialog = document.getElementById('about-dialog');
let textResolve = null; // Para manejar el prompt de texto de manera asíncrona
const state = {
tool: 'rect',
color: '#ff2d55',
width: 3,
textSize: 24,
shapes: [],
redoStack: [],
draft: null,
drawing: false,
baseImage: null,
step: 1,
selection: null,
zoom: 80, // Escala de visualización inicial a 80%
offscreenCanvas: null,
offscreenCtx: null,
showBrowserLogo: true,
logoImg: null,
};
// Cargar imagen del logo para la marca de agua
const logoImg = new Image();
logoImg.src = 'icon128.png';
logoImg.onload = () => {
state.logoImg = logoImg;
render();
};
// --- Sistema de Notificaciones Sticky (Toasts) ---
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.className = `toast ${type}`;
const textSpan = document.createElement('span');
textSpan.textContent = message;
toast.appendChild(textSpan);
const closeBtn = document.createElement('button');
closeBtn.className = 'toast-close';
closeBtn.innerHTML = '×';
closeBtn.onclick = () => {
toast.classList.add('fade-out');
setTimeout(() => toast.remove(), 200);
};
toast.appendChild(closeBtn);
toastContainer.appendChild(toast);
// Auto-eliminar después de 4 segundos
setTimeout(() => {
if (toast.parentNode) {
toast.classList.add('fade-out');
setTimeout(() => toast.remove(), 200);
}
}, 4000);
}
function setStatus(text) {
statusEl.textContent = text;
}
function updateNumberBadge() {
const badge = document.getElementById('num-badge');
if (badge) badge.textContent = String(state.step);
}
function normRect(a) {
const x = Math.min(a.x1, a.x2);
const y = Math.min(a.y1, a.y2);
const w = Math.max(1, Math.abs(a.x2 - a.x1));
const h = Math.max(1, Math.abs(a.y2 - a.y1));
return { x, y, w, h };
}
function setTool(tool) {
state.tool = tool;
document.querySelectorAll('.icon-btn').forEach((b) => {
if (b.id.startsWith('tool-')) b.classList.remove('active');
});
const active = document.getElementById(`tool-${tool}`);
if (active) active.classList.add('active');
setStatus(`Herramienta activa: ${tool.toUpperCase()}`);
}
function getPos(e) {
const r = canvas.getBoundingClientRect();
const scaleX = canvas.width / r.width;
const scaleY = canvas.height / r.height;
return {
x: (e.clientX - r.left) * scaleX,
y: (e.clientY - r.top) * scaleY
};
}
// --- Diálogos Personalizados (Evitando Prompts/Alerts) ---
function promptTextCustom() {
return new Promise((resolve) => {
textResolve = resolve;
textInput.value = '';
textDialog.showModal();
setTimeout(() => textInput.focus(), 100);
});
}
document.getElementById('btn-text-submit').onclick = () => {
if (textResolve) {
textResolve(textInput.value.trim());
textResolve = null;
}
textDialog.close();
};
document.getElementById('btn-text-cancel').onclick = () => {
if (textResolve) {
textResolve(null);
textResolve = null;
}
textDialog.close();
};
textInput.onkeydown = (e) => {
if (e.key === 'Enter') {
document.getElementById('btn-text-submit').click();
}
};
function confirmClearCustom() {
return new Promise((resolve) => {
confirmDialog.showModal();
document.getElementById('btn-confirm-accept').onclick = () => {
confirmDialog.close();
resolve(true);
};
document.getElementById('btn-confirm-cancel').onclick = () => {
confirmDialog.close();
resolve(false);
};
});
}
// --- Funciones de Dibujo en Canvas ---
function drawArrow(shape, context = ctx) {
const head = Math.max(10, shape.w * 3);
const dx = shape.x2 - shape.x1;
const dy = shape.y2 - shape.y1;
const ang = Math.atan2(dy, dx);
context.beginPath();
context.moveTo(shape.x1, shape.y1);
context.lineTo(shape.x2, shape.y2);
context.stroke();
context.beginPath();
context.moveTo(shape.x2, shape.y2);
context.lineTo(shape.x2 - head * Math.cos(ang - Math.PI / 6), shape.y2 - head * Math.sin(ang - Math.PI / 6));
context.lineTo(shape.x2 - head * Math.cos(ang + Math.PI / 6), shape.y2 - head * Math.sin(ang + Math.PI / 6));
context.closePath();
context.fillStyle = shape.color;
context.fill();
}
function wrapText(context, text, maxWidth) {
const paragraphs = text.split('\n');
const allLines = [];
for (const para of paragraphs) {
if (para === '') {
allLines.push('');
continue;
}
const words = para.split(' ');
let currentLine = '';
for (let i = 0; i < words.length; i++) {
const testLine = currentLine ? currentLine + ' ' + words[i] : words[i];
const metrics = context.measureText(testLine);
if (metrics.width > maxWidth && i > 0) {
allLines.push(currentLine);
currentLine = words[i];
} else {
currentLine = testLine;
}
}
if (currentLine) {
allLines.push(currentLine);
}
}
return allLines;
}
function drawShape(shape, context = ctx) {
context.strokeStyle = shape.color;
context.fillStyle = shape.color;
context.lineWidth = shape.w;
context.lineCap = 'round';
context.lineJoin = 'round';
if (shape.type === 'rect') {
const r = normRect(shape);
context.strokeRect(r.x, r.y, r.w, r.h);
} else if (shape.type === 'rect-fill') {
const r = normRect(shape);
context.fillRect(r.x, r.y, r.w, r.h);
} else if (shape.type === 'highlight') {
const r = normRect(shape);
context.save();
context.fillStyle = shape.color || '#ffe600';
context.globalAlpha = 0.38;
context.fillRect(r.x, r.y, r.w, r.h);
context.restore();
} else if (shape.type === 'blur') {
const r = normRect(shape);
if (r.w > 2 && r.h > 2 && state.baseImage) {
const blurPx = 10;
const margin = blurPx * 2;
const sx = Math.max(0, r.x - margin);
const sy = Math.max(0, r.y - margin);
const sw = Math.min(state.baseImage.naturalWidth - sx, r.w + (r.x - sx) + margin);
const sh = Math.min(state.baseImage.naturalHeight - sy, r.h + (r.y - sy) + margin);
context.save();
context.beginPath();
context.rect(r.x, r.y, r.w, r.h);
context.clip();
const tempCanvas = document.createElement('canvas');
tempCanvas.width = sw;
tempCanvas.height = sh;
const tCtx = tempCanvas.getContext('2d');
tCtx.drawImage(state.baseImage, sx, sy, sw, sh, 0, 0, sw, sh);
context.filter = `blur(${blurPx}px)`;
context.drawImage(tempCanvas, sx, sy);
context.filter = 'none';
context.restore();
context.save();
context.strokeStyle = 'rgba(255, 255, 255, 0.15)';
context.lineWidth = 1;
context.strokeRect(r.x, r.y, r.w, r.h);
context.restore();
}
} else if (shape.type === 'circle') {
const r = normRect(shape);
context.beginPath();
context.ellipse(r.x + r.w / 2, r.y + r.h / 2, r.w / 2, r.h / 2, 0, 0, Math.PI * 2);
context.stroke();
} else if (shape.type === 'line') {
context.beginPath();
context.moveTo(shape.x1, shape.y1);
context.lineTo(shape.x2, shape.y2);
context.stroke();
} else if (shape.type === 'curve') {
if (shape.points && shape.points.length > 0) {
let pts = shape.points;
for (let pass = 0; pass < 2; pass++) {
if (pts.length >= 3) {
const temp = [pts[0]];
for (let i = 1; i < pts.length - 1; i++) {
temp.push({
x: (pts[i - 1].x + pts[i].x + pts[i + 1].x) / 3,
y: (pts[i - 1].y + pts[i].y + pts[i + 1].y) / 3
});
}
temp.push(pts[pts.length - 1]);
pts = temp;
}
}
context.beginPath();
context.moveTo(pts[0].x, pts[0].y);
if (pts.length === 1) {
context.lineTo(pts[0].x, pts[0].y);
} else if (pts.length === 2) {
context.lineTo(pts[1].x, pts[1].y);
} else {
for (let i = 1; i < pts.length - 1; i++) {
const xc = (pts[i].x + pts[i + 1].x) / 2;
const yc = (pts[i].y + pts[i + 1].y) / 2;
context.quadraticCurveTo(pts[i].x, pts[i].y, xc, yc);
}
context.lineTo(pts[pts.length - 1].x, pts[pts.length - 1].y);
}
context.stroke();
}
} else if (shape.type === 'arrow') {
drawArrow(shape, context);
} else if (shape.type === 'text') {
if (!shape.text) return;
const fontSize = shape.size || 24;
const lineHeight = fontSize * 1.25;
const w = shape.w || 200;
const h = shape.h || 60;
context.save();
context.beginPath();
context.rect(shape.x1, shape.y1, w, h);
context.clip();
context.fillStyle = shape.color;
context.font = `bold ${fontSize}px Inter, Arial, sans-serif`;
context.textBaseline = 'top';
const lines = wrapText(context, shape.text, w);
let currentY = shape.y1;
for (const line of lines) {
if (currentY + lineHeight > shape.y1 + h + lineHeight) break;
context.fillText(line, shape.x1, currentY);
currentY += lineHeight;
}
context.restore();
} else if (shape.type === 'number') {
const radius = shape.size || Math.max(14, shape.w * 5);
context.save();
context.beginPath();
context.arc(shape.x1, shape.y1, radius, 0, Math.PI * 2);
context.fillStyle = shape.color;
context.fill();
context.lineWidth = 2;
context.strokeStyle = '#ffffff';
context.stroke();
context.fillStyle = '#ffffff';
context.font = `bold ${Math.max(11, radius * 0.9)}px Inter, Arial, sans-serif`;
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillText(String(shape.n), shape.x1, shape.y1 + 1);
context.restore();
} else if (shape.type === 'pencil' || shape.type === 'eraser') {
if (shape.points && shape.points.length > 0) {
context.beginPath();
context.moveTo(shape.points[0].x, shape.points[0].y);
for (let i = 1; i < shape.points.length; i++) {
context.lineTo(shape.points[i].x, shape.points[i].y);
}
context.stroke();
}
}
}
function drawSelectionBox(rect) {
if (!rect || rect.w < 2 || rect.h < 2) return;
ctx.save();
ctx.setLineDash([6, 3]);
ctx.strokeStyle = '#3b82f6';
ctx.lineWidth = 2;
ctx.strokeRect(rect.x, rect.y, rect.w, rect.h);
ctx.fillStyle = 'rgba(37, 99, 235, 0.08)';
ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
ctx.restore();
}
function render(forExport = false) {
if (!state.baseImage) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(state.baseImage, 0, 0, canvas.width, canvas.height);
const oCanvas = state.offscreenCanvas;
const oCtx = state.offscreenCtx;
if (oCanvas && oCtx) {
oCtx.clearRect(0, 0, oCanvas.width, oCanvas.height);
state.shapes.forEach(shape => {
if (shape.type === 'eraser') {
oCtx.save();
oCtx.globalCompositeOperation = 'destination-out';
drawShape(shape, oCtx);
oCtx.restore();
} else {
drawShape(shape, oCtx);
}
});
if (state.draft && state.tool !== 'select') {
if (state.draft.type === 'eraser') {
oCtx.save();
oCtx.globalCompositeOperation = 'destination-out';
drawShape(state.draft, oCtx);
oCtx.restore();
} else {
drawShape(state.draft, oCtx);
}
}
ctx.drawImage(oCanvas, 0, 0);
} else {
state.shapes.forEach(shape => drawShape(shape, ctx));
if (state.draft && state.tool !== 'select' && state.draft.type !== 'text') {
drawShape(state.draft, ctx);
}
}
// Marca de agua
if (state.showBrowserLogo && state.logoImg) {
const size = 32;
const padding = 16;
let lx, ly;
if (state.selection && state.selection.w > (size + padding * 2) && state.selection.h > (size + padding * 2)) {
lx = state.selection.x + state.selection.w - size - padding;
ly = state.selection.y + state.selection.h - size - padding;
} else {
lx = canvas.width - size - padding;
ly = canvas.height - size - padding;
}
ctx.save();
ctx.globalAlpha = 0.75;
ctx.beginPath();
ctx.arc(lx + size / 2, ly + size / 2, size / 2 + 4, 0, Math.PI * 2);
ctx.fillStyle = '#ffffff';
ctx.shadowColor = 'rgba(0, 0, 0, 0.3)';
ctx.shadowBlur = 6;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 2;
ctx.fill();
ctx.drawImage(state.logoImg, lx, ly, size, size);
ctx.restore();
}
if (forExport) return;
if (state.selection) {
drawSelectionBox(state.selection);
}
if (state.draft && state.tool === 'select') {
drawSelectionBox(normRect(state.draft));
}
if (state.draft && state.tool === 'text') {
const r = normRect(state.draft);
ctx.save();
ctx.setLineDash([4, 4]);
ctx.strokeStyle = state.color || '#0076ff';
ctx.lineWidth = 1.5;
ctx.strokeRect(r.x, r.y, r.w, r.h);
ctx.restore();
}
}
// --- Editor de Texto Interactivo (Inline) ---
const inlineEditor = document.getElementById('inline-text-editor');
const canvasWrap = document.getElementById('canvas-wrap');
let activeTextRect = null;
function openInlineTextEditor(rect) {
activeTextRect = rect;
const canvasRect = canvas.getBoundingClientRect();
const wrapRect = canvasWrap.getBoundingClientRect();
const canvasOffsetLeft = canvasRect.left - wrapRect.left + canvasWrap.scrollLeft;
const canvasOffsetTop = canvasRect.top - wrapRect.top + canvasWrap.scrollTop;
const scaleX = canvasRect.width / canvas.width;
const scaleY = canvasRect.height / canvas.height;
const screenLeft = canvasOffsetLeft + rect.x * scaleX;
const screenTop = canvasOffsetTop + rect.y * scaleY;
const screenWidth = Math.max(120, rect.w * scaleX);
const screenHeight = Math.max(50, rect.h * scaleY);
const fontSize = Math.max(12, state.textSize * scaleY);
inlineEditor.value = '';
inlineEditor.style.left = `${screenLeft}px`;
inlineEditor.style.top = `${screenTop}px`;
inlineEditor.style.width = `${screenWidth}px`;
inlineEditor.style.height = `${screenHeight}px`;
inlineEditor.style.fontSize = `${fontSize}px`;
inlineEditor.style.color = state.color;
inlineEditor.style.display = 'block';
setTimeout(() => {
inlineEditor.focus();
}, 50);
}
function finalizeInlineText() {
if (!inlineEditor || inlineEditor.style.display === 'none' || !activeTextRect) return;
const text = inlineEditor.value.trim();
const canvasRect = canvas.getBoundingClientRect();
const scaleX = canvasRect.width / canvas.width;
const scaleY = canvasRect.height / canvas.height;
const finalW = Math.max(30, inlineEditor.offsetWidth / scaleX);
const finalH = Math.max(20, inlineEditor.offsetHeight / scaleY);
if (text) {
state.shapes.push({
type: 'text',
text: text,
x1: activeTextRect.x,
y1: activeTextRect.y,
w: finalW,
h: finalH,
color: state.color,
size: state.textSize
});
state.redoStack = [];
showToast('Texto agregado', 'info');
}
inlineEditor.style.display = 'none';
activeTextRect = null;
render();
}
if (inlineEditor) {
inlineEditor.addEventListener('blur', () => {
finalizeInlineText();
});
inlineEditor.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
inlineEditor.value = '';
inlineEditor.blur();
}
});
}
// Eventos de Dibujo
canvas.addEventListener('mousedown', async (e) => {
if (!state.baseImage) return;
if (inlineEditor && inlineEditor.style.display !== 'none') {
finalizeInlineText();
}
const p = getPos(e);
if (state.tool === 'number') {
state.shapes.push({
type: 'number',
n: state.step,
x1: p.x,
y1: p.y,
color: state.color,
w: state.width,
size: state.textSize
});
state.redoStack = [];
state.step++;
updateNumberBadge();
render();
showToast(`Paso #${state.step - 1} colocado`, 'info');
return;
}
state.drawing = true;
if (state.tool === 'pencil' || state.tool === 'eraser' || state.tool === 'curve') {
state.draft = {
type: state.tool,
color: state.color,
w: state.width * (state.tool === 'eraser' ? 4 : 1),
points: [p]
};
} else {
state.draft = {
type: state.tool,
color: state.tool === 'highlight' ? '#ffe600' : state.color,
w: state.width,
x1: p.x,
y1: p.y,
x2: p.x,
y2: p.y
};
}
});
canvas.addEventListener('mousemove', (e) => {
if (!state.drawing || !state.draft) return;
const p = getPos(e);
if (state.tool === 'pencil' || state.tool === 'eraser' || state.tool === 'curve') {
state.draft.points.push(p);
} else {
state.draft.x2 = p.x;
state.draft.y2 = p.y;
}
render();
});
canvas.addEventListener('mouseup', () => {
if (!state.drawing || !state.draft) return;
state.drawing = false;
if (state.tool === 'text') {
const rect = normRect(state.draft);
state.draft = null;
if (rect.w < 30 || rect.h < 20) {
rect.w = 200;
rect.h = 60;
}
render();
openInlineTextEditor(rect);
} else if (state.tool === 'select') {
state.selection = normRect(state.draft);
state.draft = null;
setStatus(`Recorte activo: ${Math.round(state.selection.w)} x ${Math.round(state.selection.h)} px`);
showToast('Área de recorte seleccionada', 'info');
render();
} else {
state.shapes.push(state.draft);
state.redoStack = [];
state.draft = null;
render();
}
});
// Aplicar Zoom
function applyZoom(pct) {
state.zoom = pct;
const scale = pct / 100;
canvas.style.width = `${canvas.width * scale}px`;
canvas.style.height = `${canvas.height * scale}px`;
document.getElementById('zoom-value').textContent = `${pct}%`;
}
// Carga de imagen base
function loadBaseImage(dataUrl) {
const img = new Image();
img.onload = () => {
state.baseImage = img;
state.shapes = [];
state.redoStack = [];
state.selection = null;
state.step = 1;
updateNumberBadge();
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
state.offscreenCanvas = document.createElement('canvas');
state.offscreenCanvas.width = canvas.width;
state.offscreenCanvas.height = canvas.height;
state.offscreenCtx = state.offscreenCanvas.getContext('2d');
applyZoom(state.zoom);
render();
setStatus(`Captura cargada: ${canvas.width} x ${canvas.height} px`);
showToast('Nueva captura cargada en el editor', 'success');
};
img.src = dataUrl;
}
// Exportar Canvas (con o sin recorte)
function getExportDataUrl() {
if (!state.baseImage) return null;
// Renderizar lienzo limpio sin overlay azul ni bordes punteados de selección
render(true);
let exportUrl;
if (state.selection && state.selection.w > 2 && state.selection.h > 2) {
const sel = state.selection;
const croppedCanvas = document.createElement('canvas');
croppedCanvas.width = sel.w;
croppedCanvas.height = sel.h;
const cCtx = croppedCanvas.getContext('2d');
cCtx.drawImage(canvas, sel.x, sel.y, sel.w, sel.h, 0, 0, sel.w, sel.h);
exportUrl = croppedCanvas.toDataURL('image/png');
} else {
exportUrl = canvas.toDataURL('image/png');
}
// Restaurar la vista interactiva UI en pantalla
render(false);
return exportUrl;
}
// Listeners de UI
document.querySelectorAll('.icon-btn').forEach((btn) => {
if (btn.id.startsWith('tool-')) {
btn.onclick = () => setTool(btn.id.replace('tool-', ''));
}
});
document.getElementById('clear-selection').onclick = () => {
state.selection = null;
render();
setStatus('Selección de recorte eliminada.');
showToast('Selección eliminada', 'info');
};
document.getElementById('color').oninput = (e) => {
state.color = e.target.value;
};
document.getElementById('width').oninput = (e) => {
state.width = Number(e.target.value);
};
document.getElementById('text-size').oninput = (e) => {
state.textSize = Number(e.target.value);
};
document.getElementById('zoom-range').oninput = (e) => {
applyZoom(Number(e.target.value));
};
document.getElementById('toggle-logo').onchange = (e) => {
state.showBrowserLogo = e.target.checked;
render();
showToast(state.showBrowserLogo ? 'Marca de agua activada' : 'Marca de agua desactivada', 'info');
};
document.getElementById('undo').onclick = () => {
if (state.shapes.length > 0) {
const popped = state.shapes.pop();
state.redoStack.push(popped);
if (popped.type === 'number' && state.step > 1) {
state.step--;
updateNumberBadge();
}
render();
setStatus('Última acción deshecha.');
showToast('Acción deshecha', 'info');
}
};
document.getElementById('redo').onclick = () => {
if (state.redoStack.length > 0) {
const restored = state.redoStack.pop();
state.shapes.push(restored);
if (restored.type === 'number') {
state.step++;
updateNumberBadge();
}
render();
setStatus('Acción rehecha.');
showToast('Acción rehecha', 'info');
}
};
document.getElementById('clear').onclick = async () => {
const confirmed = await confirmClearCustom();
if (confirmed) {
state.shapes = [];
state.redoStack = [];
state.selection = null;
state.step = 1;
updateNumberBadge();
render();
setStatus('Anotaciones limpiadas.');
showToast('Anotaciones eliminadas correctamente', 'warning');
}
};
document.getElementById('new-screenshot').onclick = () => {
if (window.electronAPI) {
window.electronAPI.takeAreaScreenshot();
}
};
function createBlankCanvas(width = 800, height = 600) {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
const tCtx = tempCanvas.getContext('2d');
tCtx.fillStyle = '#ffffff';
tCtx.fillRect(0, 0, width, height);
const dataUrl = tempCanvas.toDataURL('image/png');
loadBaseImage(dataUrl);
setStatus(`Lienzo en blanco listo: ${width} x ${height} px`);
}
function resetEditorState() {
state.shapes = [];
state.selection = null;
state.step = 1;
state.draft = null;
state.drawing = false;
updateNumberBadge();
createBlankCanvas(800, 600);
}
document.getElementById('open-file').onclick = async () => {
if (window.electronAPI) {
const res = await window.electronAPI.openImageFile();
if (res.success && res.dataUrl) {
loadBaseImage(res.dataUrl);
showToast('Imagen cargada correctamente para edición', 'success');
} else if (!res.canceled && res.error) {
showToast('Error al abrir la imagen', 'warning');
}
}
};
document.getElementById('copy').onclick = async () => {
const dataUrl = getExportDataUrl();
if (dataUrl && window.electronAPI) {
const res = await window.electronAPI.copyToClipboard(dataUrl);
if (res.success) {
showToast('Imagen copiada al portapapeles con éxito', 'success');
setStatus('Captura copiada al portapapeles.');
setTimeout(() => {
resetEditorState();
window.electronAPI.closeEditor();
}, 250);
} else {
showToast('Error al copiar al portapapeles', 'warning');
}
}
};
// Modal de Configuración
const settingsDialog = document.getElementById('settings-dialog');
const savePathInput = document.getElementById('save-path-input');
const themeDarkBtn = document.getElementById('theme-dark');
const themeLightBtn = document.getElementById('theme-light');
const appConfig = {
theme: localStorage.getItem('screenshot_theme') || 'dark',
defaultSavePath: localStorage.getItem('screenshot_save_path') || ''
};
let tempTheme = appConfig.theme;
function applyTheme(theme) {
appConfig.theme = theme;
if (theme === 'light') {
document.body.classList.add('theme-light');
if (themeLightBtn) themeLightBtn.classList.add('active-theme');
if (themeDarkBtn) themeDarkBtn.classList.remove('active-theme');
} else {
document.body.classList.remove('theme-light');
if (themeDarkBtn) themeDarkBtn.classList.add('active-theme');
if (themeLightBtn) themeLightBtn.classList.remove('active-theme');
}
}
// Aplicar tema inicial al cargar
applyTheme(appConfig.theme);
if (themeDarkBtn) {
themeDarkBtn.onclick = () => {
tempTheme = 'dark';
themeDarkBtn.classList.add('active-theme');
themeLightBtn.classList.remove('active-theme');
};
}
if (themeLightBtn) {
themeLightBtn.onclick = () => {
tempTheme = 'light';
themeLightBtn.classList.add('active-theme');
themeDarkBtn.classList.remove('active-theme');
};
}
const btnSettings = document.getElementById('btn-settings');
if (btnSettings) {
btnSettings.onclick = () => {
tempTheme = appConfig.theme;
savePathInput.value = appConfig.defaultSavePath;
applyTheme(tempTheme);
settingsDialog.showModal();
};
}
const btnBrowseFolder = document.getElementById('btn-browse-folder');
if (btnBrowseFolder) {
btnBrowseFolder.onclick = async () => {
if (window.electronAPI) {
const res = await window.electronAPI.selectDirectory();
if (res.success && res.path) {
savePathInput.value = res.path;
}
}
};
}
const btnSettingsSave = document.getElementById('btn-settings-save');
if (btnSettingsSave) {
btnSettingsSave.onclick = () => {
appConfig.theme = tempTheme;
appConfig.defaultSavePath = savePathInput.value.trim();
localStorage.setItem('screenshot_theme', appConfig.theme);
localStorage.setItem('screenshot_save_path', appConfig.defaultSavePath);
applyTheme(appConfig.theme);
settingsDialog.close();
showToast('Configuración guardada correctamente', 'success');
};
}
const btnSettingsCancel = document.getElementById('btn-settings-cancel');
if (btnSettingsCancel) {
btnSettingsCancel.onclick = () => {
settingsDialog.close();
applyTheme(appConfig.theme);
};
}
document.getElementById('save').onclick = async () => {
const dataUrl = getExportDataUrl();
if (dataUrl && window.electronAPI) {
const res = await window.electronAPI.saveImage(dataUrl, appConfig.defaultSavePath);
if (res.success) {
showToast(`Captura guardada con éxito en: ${res.filePath}`, 'success');
setStatus(`Guardado en: ${res.filePath}`);
} else if (!res.canceled) {
showToast('Error al guardar la imagen', 'warning');
}
}
};
document.getElementById('minimize-window').onclick = () => {
if (window.electronAPI) {
window.electronAPI.minimizeEditor();
}
};
document.getElementById('maximize-window').onclick = () => {
if (window.electronAPI) {
window.electronAPI.maximizeEditor();
}
};
document.getElementById('cancel').onclick = () => {
if (window.electronAPI) {
resetEditorState();
window.electronAPI.closeEditor();
}
};
document.getElementById('logo-about').onclick = () => {
aboutDialog.style.display = 'flex';
};
document.getElementById('btn-about-close').onclick = () => {
aboutDialog.style.display = 'none';
};
// Listener de Teclado (F9, PrintScreen, Ctrl+Z, Ctrl+Y, Ctrl+Shift+Z, Ctrl+C)
window.addEventListener('keydown', (e) => {
const activeTag = document.activeElement ? document.activeElement.tagName.toLowerCase() : '';
if (activeTag === 'input' || activeTag === 'textarea') {
if (e.key === 'Escape' && document.activeElement.id === 'inline-text-editor') {
document.activeElement.blur();
}
return;
}
if (e.key === 'F9' || e.key === 'PrintScreen' || e.code === 'PrintScreen') {
e.preventDefault();
if (window.electronAPI) {
window.electronAPI.takeAreaScreenshot();
}
} else if ((e.ctrlKey || e.metaKey) && e.shiftKey && (e.key === 'Z' || e.key === 'z')) {
e.preventDefault();
document.getElementById('redo').click();
} else if ((e.ctrlKey || e.metaKey) && (e.key === 'y' || e.key === 'Y')) {
e.preventDefault();
document.getElementById('redo').click();
} else if ((e.ctrlKey || e.metaKey) && (e.key === 'z' || e.key === 'Z')) {
e.preventDefault();
document.getElementById('undo').click();
} else if ((e.ctrlKey || e.metaKey) && (e.key === 'c' || e.key === 'C')) {
e.preventDefault();
document.getElementById('copy').click();
}
});
// Escuchar capturas entrantes de Electron
if (window.electronAPI) {
window.electronAPI.onLoadScreenshot((dataUrl) => {
loadBaseImage(dataUrl);
});
}
// Inicializar lienzo en blanco de forma predeterminada
createBlankCanvas(800, 600);