|
1 | | -// --- Syntax Highlighting Overlay e Abas --- |
2 | | -function syncEditorHighlight() { |
3 | | - const textarea = document.getElementById('queryEditor'); |
4 | | - const overlay = document.querySelector('.editor-highlight-output'); |
5 | | - if (!textarea || !overlay) return; |
6 | | - // Pega o valor do textarea e aplica highlight |
7 | | - let value = textarea.value || ''; |
8 | | - overlay.innerHTML = queryManager ? queryManager.highlightSQL(value) : value; |
9 | | - // Sincroniza scroll |
10 | | - overlay.scrollTop = textarea.scrollTop; |
11 | | - overlay.scrollLeft = textarea.scrollLeft; |
12 | | -} |
13 | | - |
14 | | -// Sincronizar overlay ao digitar e ao rolar |
15 | | -document.addEventListener('DOMContentLoaded', function() { |
16 | | - const textarea = document.getElementById('queryEditor'); |
17 | | - if (textarea) { |
18 | | - textarea.addEventListener('input', syncEditorHighlight); |
19 | | - textarea.addEventListener('scroll', syncEditorHighlight); |
20 | | - // Inicializa overlay |
21 | | - syncEditorHighlight(); |
22 | | - } |
23 | | - // Abas Bootstrap: garantir que o overlay seja atualizado ao trocar para o editor |
24 | | - const tabSql = document.getElementById('tab-sql'); |
25 | | - if (tabSql) { |
26 | | - tabSql.addEventListener('shown.bs.tab', function() { |
27 | | - setTimeout(syncEditorHighlight, 10); |
28 | | - }); |
29 | | - } |
30 | | -}); |
31 | | - |
32 | | -// Atualizar overlay ao carregar query |
33 | | -const originalLoadQueryInEditor = SQLQueryManager.prototype.loadQueryInEditor; |
34 | | -SQLQueryManager.prototype.loadQueryInEditor = function() { |
35 | | - originalLoadQueryInEditor.call(this); |
36 | | - setTimeout(syncEditorHighlight, 10); |
37 | | -}; |
38 | | - |
39 | | -// Atualizar overlay ao atualizar query |
40 | | -const originalUpdateCurrentQuery = SQLQueryManager.prototype.updateCurrentQuery; |
41 | | -SQLQueryManager.prototype.updateCurrentQuery = function() { |
42 | | - originalUpdateCurrentQuery.call(this); |
43 | | - setTimeout(syncEditorHighlight, 10); |
44 | | -}; |
45 | | - |
46 | | -// Atualizar overlay ao manipular input |
47 | | -const originalHandleQueryInput = SQLQueryManager.prototype.handleQueryInput; |
48 | | -SQLQueryManager.prototype.handleQueryInput = function() { |
49 | | - if (originalHandleQueryInput) originalHandleQueryInput.call(this); |
50 | | - setTimeout(syncEditorHighlight, 10); |
51 | | -}; |
52 | 1 | class ThemeManager { |
53 | 2 | constructor() { |
54 | 3 | this.STORAGE_KEY = 'sqlQueryManager_theme'; |
@@ -371,18 +320,20 @@ autoResizeTextarea() { |
371 | 320 | highlightSQL(sql) { |
372 | 321 | // Palavras-chave SQL |
373 | 322 | const keywords = ['SELECT', 'FROM', 'WHERE', 'JOIN', 'INNER', 'LEFT', 'RIGHT', 'ON', 'AND', 'OR', 'ORDER', 'BY', 'GROUP', 'HAVING', 'INSERT', 'UPDATE', 'DELETE', 'AS', 'DISTINCT', 'COUNT', 'SUM', 'AVG', 'MAX', 'MIN']; |
| 323 | + |
374 | 324 | let highlighted = sql; |
375 | | - // Variáveis customizadas [nome] |
376 | | - highlighted = highlighted.replace(/\[([a-zA-Z0-9_]+)\]/g, '<span class="sql-variable">[$1]</span>'); |
377 | | - // Strings |
378 | | - highlighted = highlighted.replace(/'([^']*)'/g, '<span class="sql-string">\'$1\'</span>'); |
379 | | - // Comentários |
380 | | - highlighted = highlighted.replace(/--(.*)$/gm, '<span class="sql-comment">--$1</span>'); |
381 | | - // Palavras-chave (após variáveis para não afetar dentro de variáveis) |
| 325 | + |
382 | 326 | keywords.forEach(keyword => { |
383 | 327 | const regex = new RegExp(`\\b${keyword}\\b`, 'gi'); |
384 | 328 | highlighted = highlighted.replace(regex, `<span class="sql-keyword">${keyword.toUpperCase()}</span>`); |
385 | 329 | }); |
| 330 | + |
| 331 | + // Strings |
| 332 | + highlighted = highlighted.replace(/'([^']*)'/g, '<span class="sql-string">\'$1\'</span>'); |
| 333 | + |
| 334 | + // Comentários |
| 335 | + highlighted = highlighted.replace(/--(.*)$/gm, '<span class="sql-comment">--$1</span>'); |
| 336 | + |
386 | 337 | return highlighted; |
387 | 338 | } |
388 | 339 |
|
|
0 commit comments