-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
887 lines (747 loc) · 29.2 KB
/
Copy pathscript.js
File metadata and controls
887 lines (747 loc) · 29.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
// Monaco Editor instances
let monacoEditor = null;
let monacoPreviewEditor = null;
// Inicialização do Monaco Editor
function initMonacoEditor() {
require.config({ paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.44.0/min/vs' } });
require(['vs/editor/editor.main'], function () {
const container = document.getElementById('monacoContainer');
// Configurar tema baseado no tema atual
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
const theme = isDark ? 'vs-dark' : 'vs';
monacoEditor = monaco.editor.create(container, {
value: '-- Digite sua query SQL aqui\n-- Use [variavel] para criar variáveis dinâmicas\n-- Exemplo: SELECT * FROM usuarios WHERE id = [user_id] AND status = \'[status]\'',
language: 'sql',
theme: theme,
automaticLayout: true,
fontSize: 14,
lineHeight: 1.5,
minimap: { enabled: false },
scrollBeyondLastLine: false,
renderLineHighlight: 'line',
selectionHighlight: false,
occurrencesHighlight: false,
wordWrap: 'on',
wrappingIndent: 'indent',
formatOnPaste: true,
formatOnType: true,
folding: true,
lineNumbers: 'on',
glyphMargin: false,
showFoldingControls: 'mouseover'
});
// Listener para mudanças no conteúdo
monacoEditor.onDidChangeModelContent(() => {
if (queryManager) {
queryManager.handleQueryInput();
}
});
// Criar editor de preview (somente leitura)
const previewContainer = document.getElementById('monacoPreviewContainer');
monacoPreviewEditor = monaco.editor.create(previewContainer, {
value: '-- A query processada aparecerá aqui...',
language: 'sql',
theme: theme,
automaticLayout: true,
fontSize: 14,
lineHeight: 1.5,
minimap: { enabled: false },
scrollBeyondLastLine: false,
renderLineHighlight: 'none',
selectionHighlight: false,
occurrencesHighlight: false,
wordWrap: 'on',
wrappingIndent: 'indent',
folding: true,
lineNumbers: 'on',
glyphMargin: false,
showFoldingControls: 'mouseover',
readOnly: true,
contextmenu: false,
quickSuggestions: false,
suggestOnTriggerCharacters: false,
acceptSuggestionOnEnter: 'off',
tabCompletion: 'off',
wordBasedSuggestions: false,
parameterHints: { enabled: false },
hover: { enabled: false }
});
// Redimensionar quando a janela muda de tamanho
window.addEventListener('resize', () => {
if (monacoEditor) {
monacoEditor.layout();
}
if (monacoPreviewEditor) {
monacoPreviewEditor.layout();
}
});
// Aplicar tema quando mudado
window.addEventListener('themeChanged', (e) => {
if (monacoEditor || monacoPreviewEditor) {
const newTheme = e.detail.theme === 'dark' ? 'vs-dark' : 'vs';
monaco.editor.setTheme(newTheme);
}
});
// Carregar primeira query se existir após Monaco estar pronto
if (queryManager && queryManager.queries.length > 0) {
queryManager.selectQuery(0);
}
});
}
// Alternância de abas customizadas do editor central
function showEditorTab(tab) {
const btnSql = document.getElementById('tab-sql');
const btnPreview = document.getElementById('tab-preview');
const panelSql = document.getElementById('tabPanel-sql');
const panelPreview = document.getElementById('tabPanel-preview');
if (tab === 'sql') {
btnSql.classList.add('active');
btnPreview.classList.remove('active');
panelSql.classList.add('show');
panelPreview.classList.remove('show');
panelSql.style.display = 'flex';
panelPreview.style.display = 'none';
} else {
btnSql.classList.remove('active');
btnPreview.classList.add('active');
panelSql.classList.remove('show');
panelPreview.classList.add('show');
panelSql.style.display = 'none';
panelPreview.style.display = 'flex';
}
}
// Inicialização: sempre mostrar SQL ao carregar
document.addEventListener('DOMContentLoaded', function() {
// Inicializar Monaco Editor
initMonacoEditor();
// Aguarda um pouco para garantir que todos os elementos estejam carregados
setTimeout(() => {
showEditorTab('sql');
}, 100);
});
class ThemeManager {
constructor() {
this.STORAGE_KEY = 'sqlQueryManager_theme';
this.theme = this.loadTheme();
this.init();
}
init() {
// Set initial theme
this.applyTheme(this.theme);
// Add event listeners to theme buttons
document.querySelectorAll('.theme-option').forEach(button => {
button.addEventListener('click', (e) => {
const theme = e.currentTarget.dataset.theme;
this.setTheme(theme);
});
// Keyboard accessibility
button.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
const theme = e.currentTarget.dataset.theme;
this.setTheme(theme);
}
});
});
// Listen for system theme changes
if (window.matchMedia) {
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');
darkModeQuery.addEventListener('change', (e) => {
if (this.theme === 'auto') {
this.applyTheme('auto');
}
});
}
}
loadTheme() {
try {
const stored = localStorage.getItem(this.STORAGE_KEY);
return stored || 'auto';
} catch (e) {
console.error('Error loading theme:', e);
return 'auto';
}
}
saveTheme(theme) {
try {
localStorage.setItem(this.STORAGE_KEY, theme);
} catch (e) {
console.error('Error saving theme:', e);
}
}
setTheme(theme) {
this.theme = theme;
this.saveTheme(theme);
this.applyTheme(theme);
}
applyTheme(theme) {
const htmlElement = document.documentElement;
// Update active button
document.querySelectorAll('.theme-option').forEach(button => {
if (button.dataset.theme === theme) {
button.classList.add('active');
button.setAttribute('aria-pressed', 'true');
} else {
button.classList.remove('active');
button.setAttribute('aria-pressed', 'false');
}
});
// Apply theme
let effectiveTheme = 'light';
if (theme === 'auto') {
// Use system preference
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (prefersDark) {
htmlElement.setAttribute('data-theme', 'dark');
effectiveTheme = 'dark';
} else {
htmlElement.removeAttribute('data-theme');
effectiveTheme = 'light';
}
} else if (theme === 'dark') {
htmlElement.setAttribute('data-theme', 'dark');
effectiveTheme = 'dark';
} else {
htmlElement.removeAttribute('data-theme');
effectiveTheme = 'light';
}
// Disparar evento para componentes que precisam reagir à mudança de tema
window.dispatchEvent(new CustomEvent('themeChanged', { detail: { theme: effectiveTheme } }));
}
getCurrentTheme() {
return this.theme;
}
getEffectiveTheme() {
if (this.theme === 'auto') {
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
return prefersDark ? 'dark' : 'light';
}
return this.theme;
}
}
class SQLQueryManager {
constructor() {
this.queries = [];
this.currentQueryIndex = -1;
this.loadQueries();
this.updateUI();
}
loadQueries() {
const stored = localStorage.getItem('sqlQueryManager');
if (stored) {
try {
this.queries = JSON.parse(stored);
// Migrar queries existentes para incluir o atributo order
this.queries.forEach((query, index) => {
if (query.order === undefined) {
query.order = index;
}
});
// Ordenar queries por order
this.queries.sort((a, b) => a.order - b.order);
} catch (e) {
console.error('Erro ao carregar queries do localStorage:', e);
this.loadDefaultQueries();
}
} else {
this.loadDefaultQueries();
}
}
loadDefaultQueries() {
this.queries = [
{
id: 1,
name: "Exemplo - Consulta Usuários",
sql: "SELECT u.id, u.nome, u.email, u.data_cadastro\nFROM usuarios u\nWHERE u.ativo = [ativo]\nAND u.data_cadastro >= '[data_inicio]'\nORDER BY u.nome",
variables: {
"ativo": "1",
"data_inicio": "2024-01-01"
},
order: 0
}
];
this.saveQueries();
}
saveQueries() {
try {
localStorage.setItem('sqlQueryManager', JSON.stringify(this.queries));
console.log('Queries salvas no localStorage');
} catch (e) {
console.error('Erro ao salvar queries no localStorage:', e);
this.showToast('Erro ao salvar queries no navegador', 'error');
}
}
extractVariables(sql) {
const matches = sql.match(/\[([^\]]+)\]/g);
return matches ? matches.map(match => match.slice(1, -1)) : [];
}
createNewQuery() {
const newQuery = {
id: Date.now(),
name: `Nova Query ${this.queries.length + 1}`,
sql: "",
variables: {},
order: this.queries.length
};
this.queries.push(newQuery);
this.currentQueryIndex = this.queries.length - 1;
this.updateUI();
this.loadQueryInEditor();
document.getElementById('queryName').focus();
}
selectQuery(index) {
this.currentQueryIndex = index;
this.loadQueryInEditor();
this.updateUI();
}
loadQueryInEditor() {
if (this.currentQueryIndex < 0) return;
const query = this.queries[this.currentQueryIndex];
document.getElementById('queryName').value = query.name;
// Atualizar Monaco Editor
if (monacoEditor) {
monacoEditor.setValue(query.sql || '');
}
this.updateVariablesPanel();
this.updatePreview();
}
updateCurrentQuery() {
if (this.currentQueryIndex < 0) return;
const query = this.queries[this.currentQueryIndex];
query.name = document.getElementById('queryName').value;
// Obter conteúdo do Monaco Editor
if (monacoEditor) {
query.sql = monacoEditor.getValue();
}
// Atualizar variáveis baseado na query
const variables = this.extractVariables(query.sql);
const newVariables = {};
variables.forEach(variable => {
newVariables[variable] = query.variables[variable] || '';
});
query.variables = newVariables;
this.updateVariablesPanel();
this.updateUI();
}
updateVariablesPanel() {
const panel = document.getElementById('variablesPanel');
if (this.currentQueryIndex < 0) {
panel.innerHTML = `
<div class="empty-state">
<i class="fas fa-tags"></i>
<p>Nenhuma variável detectada</p>
<small>Use [nome_variavel] na query</small>
</div>
`;
return;
}
const query = this.queries[this.currentQueryIndex];
const variables = Object.keys(query.variables);
if (variables.length === 0) {
panel.innerHTML = `
<div class="empty-state">
<i class="fas fa-tags"></i>
<p>Nenhuma variável detectada</p>
<small>Use [nome_variavel] na query</small>
</div>
`;
return;
}
let html = '';
variables.forEach(variable => {
html += `
<div class="mb-3">
<label class="form-label">
<span class="variable-tag">[${variable}]</span>
</label>
<input type="text" class="form-control variable-input"
value="${query.variables[variable]}"
onchange="queryManager.updateVariable('${variable}', this.value)"
placeholder="Valor da variável...">
</div>
`;
});
panel.innerHTML = html;
}
updateVariable(variable, value) {
if (this.currentQueryIndex < 0) return;
this.queries[this.currentQueryIndex].variables[variable] = value;
this.updatePreview();
}
updatePreview() {
if (this.currentQueryIndex < 0) {
if (monacoPreviewEditor) {
monacoPreviewEditor.setValue('-- A query processada aparecerá aqui...');
}
return;
}
const query = this.queries[this.currentQueryIndex];
let processedSQL = query.sql;
// Substituir variáveis
Object.keys(query.variables).forEach(variable => {
const value = query.variables[variable];
const regex = new RegExp(`\\[${variable}\\]`, 'g');
processedSQL = processedSQL.replace(regex, value);
});
// Atualizar Monaco Editor de preview
if (monacoPreviewEditor) {
monacoPreviewEditor.setValue(processedSQL || '-- Query vazia...');
}
}
handleQueryInput() {
this.updateCurrentQuery();
this.updatePreview();
}
highlightSQL(sql) {
// Palavras-chave SQL
const keywords = ['USE','SELECT', 'FROM', 'WHERE', 'JOIN', 'INNER', 'LEFT', 'RIGHT', 'ON', 'AND', 'OR', 'ORDER', 'BY', 'GROUP', 'HAVING', 'INSERT', 'UPDATE', 'DELETE', 'AS', 'DISTINCT', 'COUNT', 'SUM', 'AVG', 'MAX', 'MIN'];
let highlighted = sql;
keywords.forEach(keyword => {
const regex = new RegExp(`\\b${keyword}\\b`, 'gi');
highlighted = highlighted.replace(regex, `<span class="sql-keyword">${keyword.toUpperCase()}</span>`);
});
// Strings
highlighted = highlighted.replace(/'([^']*)'/g, '<span class="sql-string">\'$1\'</span>');
// Comentários
highlighted = highlighted.replace(/--(.*)$/gm, '<span class="sql-comment">--$1</span>');
return highlighted;
}
saveQuery() {
if (this.currentQueryIndex < 0) return;
this.updateCurrentQuery();
this.saveQueries();
this.showToast('Query salva com sucesso!', 'success');
}
exportQueries() {
if (this.queries.length === 0) {
this.showToast('Nenhuma query para exportar', 'warning');
return;
}
const dataToExport = {
version: "1.0",
exportDate: new Date().toISOString(),
queries: this.queries
};
const dataStr = JSON.stringify(dataToExport, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(dataBlob);
link.download = `sql-queries-${new Date().toISOString().split('T')[0]}.json`;
link.click();
URL.revokeObjectURL(link.href);
this.showToast('Queries exportadas com sucesso!', 'success');
}
importQueries() {
document.getElementById('fileInput').click();
}
handleFileImport(file) {
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const importedData = JSON.parse(e.target.result);
// Validar estrutura do arquivo
if (!importedData.queries || !Array.isArray(importedData.queries)) {
throw new Error('Formato de arquivo inválido');
}
// Validar estrutura das queries
importedData.queries.forEach((query, index) => {
if (!query.id || !query.name || typeof query.sql !== 'string' || !query.variables) {
throw new Error(`Query ${index + 1} tem formato inválido`);
}
});
// Armazenar dados para importação posterior
this.pendingImportData = importedData;
if (this.queries.length > 0) {
// Mostrar modal de opções
document.getElementById('currentQueriesCount').textContent = this.queries.length;
document.getElementById('importQueriesCount').textContent = importedData.queries.length;
const modal = new bootstrap.Modal(document.getElementById('importModal'));
modal.show();
} else {
// Importar diretamente se não há queries existentes
this.performImport('add');
}
} catch (error) {
console.error('Erro ao importar queries:', error);
this.showToast(`Erro ao importar arquivo: ${error.message}`, 'error');
}
};
reader.readAsText(file);
}
performImport(mode = 'replace') {
if (!this.pendingImportData) return;
const importedQueries = this.pendingImportData.queries;
let processedQueries = [...importedQueries];
if (mode === 'add') {
// Adicionar às existentes - ajustar IDs para evitar conflitos
const maxId = this.queries.length > 0 ? Math.max(...this.queries.map(q => q.id)) : 0;
const currentMaxOrder = this.queries.length > 0 ? Math.max(...this.queries.map(q => q.order || 0)) : -1;
processedQueries = importedQueries.map((query, index) => ({
...query,
id: maxId + index + 1,
name: this.getUniqueName(query.name),
order: currentMaxOrder + index + 1
}));
// Adicionar às queries existentes
this.queries = [...this.queries, ...processedQueries];
this.showToast(`${importedQueries.length} queries adicionadas com sucesso! Total: ${this.queries.length}`, 'success');
} else {
// Substituir todas - garantir que todas têm order
processedQueries = importedQueries.map((query, index) => ({
...query,
order: query.order !== undefined ? query.order : index
}));
this.queries = processedQueries;
// Ordenar queries por order
this.queries.sort((a, b) => a.order - b.order);
this.showToast(`${importedQueries.length} queries importadas com sucesso!`, 'success');
}
this.currentQueryIndex = this.queries.length > 0 ? 0 : -1;
this.saveQueries();
this.updateUI();
if (this.currentQueryIndex >= 0) {
this.loadQueryInEditor();
}
this.pendingImportData = null;
}
getUniqueName(baseName) {
const existingNames = this.queries.map(q => q.name);
let uniqueName = baseName;
let counter = 1;
while (existingNames.includes(uniqueName)) {
uniqueName = `${baseName} (${counter})`;
counter++;
}
return uniqueName;
}
copyQuery() {
if (this.currentQueryIndex < 0) return;
// Garantir que temos o conteúdo mais atual do Monaco Editor
this.updateCurrentQuery();
const query = this.queries[this.currentQueryIndex];
let processedSQL = query.sql;
// Substituir variáveis
Object.keys(query.variables).forEach(variable => {
const value = query.variables[variable];
const regex = new RegExp(`\\[${variable}\\]`, 'g');
processedSQL = processedSQL.replace(regex, value);
});
navigator.clipboard.writeText(processedSQL).then(() => {
this.showToast('Query copiada para a área de transferência!', 'info');
}).catch(() => {
this.showToast('Erro ao copiar query', 'error');
});
}
deleteQuery() {
if (this.currentQueryIndex < 0) return;
if (confirm('Tem certeza que deseja deletar esta query?')) {
this.queries.splice(this.currentQueryIndex, 1);
this.currentQueryIndex = this.queries.length > 0 ? 0 : -1;
this.updateUI();
if (this.currentQueryIndex >= 0) {
this.loadQueryInEditor();
} else {
this.clearEditor();
}
this.saveQueries();
this.showToast('Query deletada!', 'warning');
}
}
clearEditor() {
document.getElementById('queryName').value = '';
document.getElementById('queryEditor').value = '';
document.getElementById('queryPreview').innerHTML = '<span class="text-muted">A query processada aparecerá aqui...</span>';
document.getElementById('variablesPanel').innerHTML = `
<div class="empty-state">
<i class="fas fa-tags"></i>
<p>Nenhuma variável detectada</p>
<small>Use [nome_variavel] na query</small>
</div>
`;
// Reset textarea height
const textarea = document.getElementById('queryEditor');
textarea.style.height = '150px';
}
updateUI() {
// Atualizar lista de queries
const queryList = document.getElementById('queryList');
if (this.queries.length === 0) {
queryList.innerHTML = `
<div class="empty-state">
<i class="fas fa-database"></i>
<p>Nenhuma query salva</p>
<small>Clique em "Nova" para começar</small>
</div>
`;
} else {
let html = '';
this.queries.forEach((query, index) => {
const isActive = index === this.currentQueryIndex;
html += `
<div class="query-list-item p-3 ${isActive ? 'active' : ''}"
draggable="true"
data-index="${index}"
onclick="queryManager.selectQuery(${index})">
<div class="d-flex align-items-start">
<div class="drag-handle me-2">
<i class="fas fa-grip-vertical"></i>
</div>
<div class="flex-grow-1">
<div class="fw-bold">${query.name}</div>
<div class="text-muted small">
${Object.keys(query.variables).length} variável(is)
</div>
</div>
</div>
</div>
`;
});
queryList.innerHTML = html;
// Adicionar event listeners de drag and drop
this.attachDragListeners();
}
// Atualizar botões
const hasQuery = this.currentQueryIndex >= 0;
document.getElementById('saveBtn').disabled = !hasQuery;
document.getElementById('copyBtn').disabled = !hasQuery;
document.getElementById('deleteBtn').disabled = !hasQuery;
}
attachDragListeners() {
const items = document.querySelectorAll('.query-list-item');
items.forEach(item => {
item.addEventListener('dragstart', (e) => this.handleDragStart(e));
item.addEventListener('dragover', (e) => this.handleDragOver(e));
item.addEventListener('dragenter', (e) => this.handleDragEnter(e));
item.addEventListener('dragleave', (e) => this.handleDragLeave(e));
item.addEventListener('drop', (e) => this.handleDrop(e));
item.addEventListener('dragend', (e) => this.handleDragEnd(e));
});
}
handleDragStart(e) {
const item = e.currentTarget;
this.draggedIndex = parseInt(item.dataset.index);
item.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', item.innerHTML);
}
handleDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
return false;
}
handleDragEnter(e) {
const item = e.currentTarget;
if (item.classList.contains('query-list-item')) {
item.classList.add('drag-over');
}
}
handleDragLeave(e) {
const item = e.currentTarget;
item.classList.remove('drag-over');
}
handleDrop(e) {
e.stopPropagation();
e.preventDefault();
const item = e.currentTarget;
const dropIndex = parseInt(item.dataset.index);
if (this.draggedIndex !== dropIndex) {
this.reorderQueries(this.draggedIndex, dropIndex);
}
item.classList.remove('drag-over');
return false;
}
handleDragEnd(e) {
const items = document.querySelectorAll('.query-list-item');
items.forEach(item => {
item.classList.remove('dragging', 'drag-over');
});
}
reorderQueries(fromIndex, toIndex) {
// Salvar o ID da query atualmente selecionada
const selectedQueryId = this.currentQueryIndex >= 0 ? this.queries[this.currentQueryIndex].id : null;
// Mover o elemento no array
const movedQuery = this.queries.splice(fromIndex, 1)[0];
this.queries.splice(toIndex, 0, movedQuery);
// Atualizar os atributos order
this.queries.forEach((query, index) => {
query.order = index;
});
// Restaurar a seleção da query atual
if (selectedQueryId !== null) {
this.currentQueryIndex = this.queries.findIndex(q => q.id === selectedQueryId);
}
// Salvar e atualizar UI
this.saveQueries();
this.updateUI();
}
showToast(message, type = 'info') {
const toastContainer = document.querySelector('.toast-container');
const toastId = 'toast-' + Date.now();
const bgColor = {
'success': 'bg-success',
'error': 'bg-danger',
'warning': 'bg-warning',
'info': 'bg-info'
}[type] || 'bg-info';
const toast = document.createElement('div');
toast.className = `toast ${bgColor} text-white`;
toast.id = toastId;
toast.innerHTML = `
<div class="toast-body">
${message}
</div>
`;
toastContainer.appendChild(toast);
const bsToast = new bootstrap.Toast(toast);
bsToast.show();
toast.addEventListener('hidden.bs.toast', () => {
toast.remove();
});
}
}
// Inicializar aplicação
const themeManager = new ThemeManager();
const queryManager = new SQLQueryManager();
// Funções globais para os botões
function createNewQuery() {
queryManager.createNewQuery();
}
function saveQuery() {
queryManager.saveQuery();
}
function copyQuery() {
queryManager.copyQuery();
}
function deleteQuery() {
queryManager.deleteQuery();
}
function updateCurrentQuery() {
queryManager.updateCurrentQuery();
}
function updatePreview() {
queryManager.updatePreview();
}
function exportQueries() {
queryManager.exportQueries();
}
function importQueries() {
queryManager.importQueries();
}
function handleFileImport(event) {
const file = event.target.files[0];
if (file) {
queryManager.handleFileImport(file);
}
// Resetar o input para permitir importar o mesmo arquivo novamente
event.target.value = '';
}
function confirmImport(mode) {
queryManager.performImport(mode);
}
// Carregar primeira query será feito após Monaco Editor estar pronto
// Função global para o input
function handleQueryInput() {
queryManager.handleQueryInput();
}
// O redimensionamento do Monaco Editor é feito automaticamente com automaticLayout: true