dsh-data-window 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +111 -0
- package/CHANGELOG.md +239 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/client.js +7779 -0
- package/cordis.patch.yml +4 -0
- package/docs/BRIDGE.md +100 -0
- package/docs/DESIGN.md +417 -0
- package/docs/DETAIL.md +220 -0
- package/docs/screenshots/ddl.png +0 -0
- package/docs/screenshots/workbench.png +0 -0
- package/icon.svg +30 -0
- package/index.js +180 -0
- package/locale/en.json +286 -0
- package/locale/zh.json +286 -0
- package/package.json +68 -4
- package/src/client/00-css.js +993 -0
- package/src/client/05-icons.js +137 -0
- package/src/client/05-theme.js +107 -0
- package/src/client/06-shortcuts.js +89 -0
- package/src/client/07-bridge.js +193 -0
- package/src/client/10-core.js +1352 -0
- package/src/client/20-tree.js +281 -0
- package/src/client/25-sql.js +631 -0
- package/src/client/30-editor.js +537 -0
- package/src/client/40-results.js +556 -0
- package/src/client/45-export.js +219 -0
- package/src/client/50-panel.js +761 -0
- package/src/client/60-settings.js +180 -0
- package/src/client/63-schema.js +186 -0
- package/src/client/64-manual.js +350 -0
- package/src/client/65-ai.js +320 -0
- package/src/client/66-optimize.js +98 -0
- package/src/client/90-apply.js +183 -0
- package/src/host/ai.js +721 -0
- package/src/host/cache.js +157 -0
- package/src/host/catalog.js +161 -0
- package/src/host/consoles.js +116 -0
- package/src/host/discovery.js +603 -0
- package/src/host/manual.js +276 -0
- package/src/host/mysql.js +650 -0
- package/src/host/remembered.js +225 -0
- package/src/host/rpc.js +487 -0
- package/src/host/shared.js +172 -0
- package/src/host/yaml.js +335 -0
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workbench tabs, the SQL editor and the DDL document.
|
|
3
|
+
*
|
|
4
|
+
* One tab per database is what requirement 4 asks for, so the tab id is the
|
|
5
|
+
* database target id: asking for the same database twice focuses the console that
|
|
6
|
+
* already exists instead of opening a second one. A tab that needs a second
|
|
7
|
+
* statement gets its own id from the store, never a rewrite of the first.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function DwTabs({ store, state, tr }) {
|
|
11
|
+
if ((state.tabs ?? []).length === 0) return null
|
|
12
|
+
return h('div', { className: 'dw-tabs', role: 'tablist' }, state.tabs.map((tab) => {
|
|
13
|
+
const active = tab.id === state.activeTab
|
|
14
|
+
return h('div', {
|
|
15
|
+
key: tab.id,
|
|
16
|
+
role: 'tab',
|
|
17
|
+
'aria-selected': active,
|
|
18
|
+
className: `dw-tab${active ? ' dw-tab-active' : ''}`,
|
|
19
|
+
title: tab.subtitle ?? tab.title,
|
|
20
|
+
onClick: () => store.setActiveTab(tab.id),
|
|
21
|
+
onAuxClick: (event) => {
|
|
22
|
+
if (event.button === 1) {
|
|
23
|
+
event.preventDefault()
|
|
24
|
+
store.closeTab(tab.id)
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
}, [
|
|
28
|
+
h(DwIcon, { key: 'ic', name: tab.kind === 'ddl' ? 'ddl' : 'database', size: 12 }),
|
|
29
|
+
h('span', { key: 'lb', className: 'dw-tab-label' }, tab.title),
|
|
30
|
+
// Several environments can hold a schema of the same name, so the tab says
|
|
31
|
+
// which one it is; the full text stays in the tooltip.
|
|
32
|
+
tab.env !== '' ? h('span', { key: 'env', className: 'dw-badge', style: { marginLeft: 2 } }, tab.env) : null,
|
|
33
|
+
h('button', {
|
|
34
|
+
key: 'cl',
|
|
35
|
+
type: 'button',
|
|
36
|
+
className: 'dw-tab-close',
|
|
37
|
+
title: tr('closeTab'),
|
|
38
|
+
'aria-label': tr('closeTab'),
|
|
39
|
+
onClick: (event) => {
|
|
40
|
+
event.stopPropagation()
|
|
41
|
+
store.closeTab(tab.id)
|
|
42
|
+
},
|
|
43
|
+
}, h(DwIcon, { name: 'close', size: 10 })),
|
|
44
|
+
])
|
|
45
|
+
}))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A draggable horizontal divider that resizes the editor. */
|
|
49
|
+
function DwDragHandle({ value, onChange, min, max }) {
|
|
50
|
+
return h('div', {
|
|
51
|
+
className: 'dw-drag',
|
|
52
|
+
role: 'separator',
|
|
53
|
+
'aria-orientation': 'horizontal',
|
|
54
|
+
title: '↕',
|
|
55
|
+
onPointerDown: (event) => {
|
|
56
|
+
event.preventDefault()
|
|
57
|
+
const startY = event.clientY
|
|
58
|
+
const startValue = value
|
|
59
|
+
const onMove = (moveEvent) => {
|
|
60
|
+
const next = Math.min(max, Math.max(min, startValue + (moveEvent.clientY - startY)))
|
|
61
|
+
onChange(next)
|
|
62
|
+
}
|
|
63
|
+
const onUp = () => {
|
|
64
|
+
globalThis.removeEventListener('pointermove', onMove)
|
|
65
|
+
globalThis.removeEventListener('pointerup', onUp)
|
|
66
|
+
}
|
|
67
|
+
globalThis.addEventListener('pointermove', onMove)
|
|
68
|
+
globalThis.addEventListener('pointerup', onUp)
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Caret position inside the textarea, measured with an in-place mirror. */
|
|
74
|
+
function dwCaretPoint(area, text, caret) {
|
|
75
|
+
const wrap = area?.parentElement
|
|
76
|
+
if (area === null || area === undefined || wrap === null || wrap === undefined) return { left: 0, top: 0 }
|
|
77
|
+
const style = globalThis.getComputedStyle(area)
|
|
78
|
+
const mirror = document.createElement('div')
|
|
79
|
+
mirror.style.position = 'absolute'
|
|
80
|
+
mirror.style.visibility = 'hidden'
|
|
81
|
+
mirror.style.pointerEvents = 'none'
|
|
82
|
+
mirror.style.top = '0'
|
|
83
|
+
mirror.style.left = '0'
|
|
84
|
+
mirror.style.width = `${area.offsetWidth}px`
|
|
85
|
+
mirror.style.padding = style.padding
|
|
86
|
+
mirror.style.border = style.border
|
|
87
|
+
mirror.style.fontFamily = style.fontFamily
|
|
88
|
+
mirror.style.fontSize = style.fontSize
|
|
89
|
+
mirror.style.fontWeight = style.fontWeight
|
|
90
|
+
mirror.style.letterSpacing = style.letterSpacing
|
|
91
|
+
mirror.style.lineHeight = style.lineHeight
|
|
92
|
+
mirror.style.tabSize = style.tabSize
|
|
93
|
+
mirror.style.whiteSpace = 'pre'
|
|
94
|
+
mirror.style.overflow = 'hidden'
|
|
95
|
+
mirror.textContent = text.slice(0, caret)
|
|
96
|
+
const marker = document.createElement('span')
|
|
97
|
+
marker.textContent = '|'
|
|
98
|
+
mirror.append(marker)
|
|
99
|
+
wrap.append(mirror)
|
|
100
|
+
const left = marker.offsetLeft - area.scrollLeft
|
|
101
|
+
const top = marker.offsetTop - area.scrollTop
|
|
102
|
+
mirror.remove()
|
|
103
|
+
return { left, top }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The SQL editor: highlighted text, completion and one-keystroke formatting. */
|
|
107
|
+
function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
108
|
+
const value = tab.sql ?? ''
|
|
109
|
+
const tokens = useMemo(() => dwSqlLex(value), [value])
|
|
110
|
+
const lines = useMemo(() => value.split('\n').length, [value])
|
|
111
|
+
const gutterRef = useRef(null)
|
|
112
|
+
const areaRef = useRef(null)
|
|
113
|
+
const highlightRef = useRef(null)
|
|
114
|
+
const completionRef = useRef(null)
|
|
115
|
+
const [completion, setCompletion] = useState(null)
|
|
116
|
+
const [editorMenu, setEditorMenu] = useState(null)
|
|
117
|
+
const [optimize, setOptimize] = useState(null)
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const area = areaRef.current
|
|
121
|
+
if (area === null) return
|
|
122
|
+
// Focusing on tab activation is the behaviour of every SQL console: the caret
|
|
123
|
+
// lands where the user is about to type.
|
|
124
|
+
area.focus()
|
|
125
|
+
area.setSelectionRange(area.value.length, area.value.length)
|
|
126
|
+
}, [tab.id])
|
|
127
|
+
|
|
128
|
+
// The completion list needs the schema of this database; one round trip, cached
|
|
129
|
+
// by the store. A failure only degrades the list to keywords.
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
void store.loadSchema(tab.dbId)
|
|
132
|
+
}, [tab.dbId, store])
|
|
133
|
+
|
|
134
|
+
const syncScroll = (area) => {
|
|
135
|
+
const gutter = gutterRef.current
|
|
136
|
+
if (gutter !== null) {
|
|
137
|
+
gutter.scrollTop = area.scrollTop
|
|
138
|
+
gutter.scrollLeft = area.scrollLeft
|
|
139
|
+
}
|
|
140
|
+
const highlight = highlightRef.current
|
|
141
|
+
if (highlight !== null) {
|
|
142
|
+
highlight.scrollTop = area.scrollTop
|
|
143
|
+
highlight.scrollLeft = area.scrollLeft
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const refreshCompletion = (text, caret) => {
|
|
148
|
+
const area = areaRef.current
|
|
149
|
+
if (area === null) return
|
|
150
|
+
const result = dwSqlCompletion(text, caret, schema)
|
|
151
|
+
const afterDot = /[A-Za-z0-9_$`]\.$/.test(text.slice(0, caret))
|
|
152
|
+
if (result.items.length === 0 || (result.prefix === '' && !afterDot)) {
|
|
153
|
+
setCompletion(null)
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
const point = dwCaretPoint(area, text, caret)
|
|
157
|
+
setCompletion({ ...result, index: 0, left: point.left, top: point.top + 20 })
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// The schema map starts loading when the tab opens; a fast typist can type a whole
|
|
161
|
+
// word before it lands, which used to leave the list closed until the next key.
|
|
162
|
+
// Re-evaluate the caret once the map arrives so the popup appears on its own.
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (schema === undefined) return
|
|
165
|
+
const area = areaRef.current
|
|
166
|
+
if (area === null || document.activeElement !== area) return
|
|
167
|
+
refreshCompletion(value, area.selectionStart)
|
|
168
|
+
}, [schema])
|
|
169
|
+
|
|
170
|
+
// Arrow keys move the highlight through a list that can be taller than its box;
|
|
171
|
+
// scroll just the popup so the selected row never disappears below the fold.
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
const popup = completionRef.current
|
|
174
|
+
if (popup === null || completion === null) return
|
|
175
|
+
const active = popup.querySelector('.dw-complete-row.active')
|
|
176
|
+
if (active === null) return
|
|
177
|
+
const top = active.offsetTop
|
|
178
|
+
const bottom = top + active.offsetHeight
|
|
179
|
+
if (top < popup.scrollTop) popup.scrollTop = top
|
|
180
|
+
else if (bottom > popup.scrollTop + popup.clientHeight) popup.scrollTop = bottom - popup.clientHeight
|
|
181
|
+
}, [completion])
|
|
182
|
+
|
|
183
|
+
const applyCompletion = (item) => {
|
|
184
|
+
const current = completion
|
|
185
|
+
if (current === null || item === undefined) return
|
|
186
|
+
const next = `${value.slice(0, current.from)}${item.label}${value.slice(current.to)}`
|
|
187
|
+
const position = current.from + item.label.length
|
|
188
|
+
store.setTabSql(tab.id, next)
|
|
189
|
+
setCompletion(null)
|
|
190
|
+
requestAnimationFrame(() => {
|
|
191
|
+
const area = areaRef.current
|
|
192
|
+
if (area === null) return
|
|
193
|
+
area.focus()
|
|
194
|
+
area.setSelectionRange(position, position)
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const runStatement = () => {
|
|
199
|
+
const area = areaRef.current
|
|
200
|
+
const selected = area !== null && area.selectionEnd > area.selectionStart
|
|
201
|
+
? value.slice(area.selectionStart, area.selectionEnd)
|
|
202
|
+
: ''
|
|
203
|
+
onRun(selected.trim() === '' ? undefined : selected)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The right-click menu replaces the browser's own so it can offer the same
|
|
207
|
+
// editing commands plus the model. Clipboard writes/reads go through the async
|
|
208
|
+
// API; undo/redo keep using the textarea's native history.
|
|
209
|
+
const editorMenuAction = (run) => () => {
|
|
210
|
+
setEditorMenu(null)
|
|
211
|
+
run()
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const editorCommand = (command) => {
|
|
215
|
+
const area = areaRef.current
|
|
216
|
+
if (area === null) return
|
|
217
|
+
area.focus()
|
|
218
|
+
try {
|
|
219
|
+
document.execCommand(command)
|
|
220
|
+
} catch (error) {
|
|
221
|
+
void error
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const editorCopy = () => {
|
|
226
|
+
const area = areaRef.current
|
|
227
|
+
if (area === null) return
|
|
228
|
+
const selected = area.selectionEnd > area.selectionStart ? value.slice(area.selectionStart, area.selectionEnd) : value
|
|
229
|
+
const write = globalThis.navigator?.clipboard?.writeText?.(selected)
|
|
230
|
+
if (write !== undefined) write.catch(() => undefined)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const editorCut = () => {
|
|
234
|
+
const area = areaRef.current
|
|
235
|
+
if (area === null) return
|
|
236
|
+
const start = area.selectionStart
|
|
237
|
+
const end = area.selectionEnd
|
|
238
|
+
if (end <= start) return
|
|
239
|
+
editorCopy()
|
|
240
|
+
store.setTabSql(tab.id, `${value.slice(0, start)}${value.slice(end)}`)
|
|
241
|
+
requestAnimationFrame(() => {
|
|
242
|
+
const next = areaRef.current
|
|
243
|
+
if (next === null) return
|
|
244
|
+
next.focus()
|
|
245
|
+
next.setSelectionRange(start, start)
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const editorPaste = async () => {
|
|
250
|
+
const area = areaRef.current
|
|
251
|
+
if (area === null) return
|
|
252
|
+
const read = globalThis.navigator?.clipboard?.readText
|
|
253
|
+
if (typeof read !== 'function') {
|
|
254
|
+
store.pushLog('warn', tr('editorPasteBlocked'))
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const text = await read.call(globalThis.navigator.clipboard)
|
|
259
|
+
if (typeof text !== 'string' || text === '') return
|
|
260
|
+
const start = area.selectionStart
|
|
261
|
+
const end = area.selectionEnd
|
|
262
|
+
store.setTabSql(tab.id, `${value.slice(0, start)}${text}${value.slice(end)}`)
|
|
263
|
+
requestAnimationFrame(() => {
|
|
264
|
+
const next = areaRef.current
|
|
265
|
+
if (next === null) return
|
|
266
|
+
next.focus()
|
|
267
|
+
next.setSelectionRange(start + text.length, start + text.length)
|
|
268
|
+
})
|
|
269
|
+
} catch (error) {
|
|
270
|
+
void error
|
|
271
|
+
store.pushLog('warn', tr('editorPasteBlocked'))
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const editorSelectAll = () => {
|
|
276
|
+
const area = areaRef.current
|
|
277
|
+
if (area === null) return
|
|
278
|
+
area.focus()
|
|
279
|
+
area.select()
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const openOptimize = () => {
|
|
283
|
+
const area = areaRef.current
|
|
284
|
+
const selected = area !== null && area.selectionEnd > area.selectionStart
|
|
285
|
+
? value.slice(area.selectionStart, area.selectionEnd)
|
|
286
|
+
: ''
|
|
287
|
+
const text = selected.trim() === '' ? value.trim() : selected.trim()
|
|
288
|
+
if (text === '') {
|
|
289
|
+
store.pushLog('warn', tr('log.noStatement'))
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
setOptimize({ sql: text, database: tab.database })
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const menuItem = (key, label, shortcut, run) => h('button', {
|
|
296
|
+
key,
|
|
297
|
+
type: 'button',
|
|
298
|
+
className: 'dw-menu-item',
|
|
299
|
+
onClick: editorMenuAction(run),
|
|
300
|
+
}, [
|
|
301
|
+
h('span', { key: 'l' }, label),
|
|
302
|
+
shortcut === '' ? null : h('span', { key: 's', className: 'dw-menu-shortcut' }, shortcut),
|
|
303
|
+
])
|
|
304
|
+
|
|
305
|
+
const format = () => {
|
|
306
|
+
const formatted = dwFormatSql(value)
|
|
307
|
+
if (formatted === value) return
|
|
308
|
+
store.setTabSql(tab.id, formatted)
|
|
309
|
+
requestAnimationFrame(() => areaRef.current?.focus())
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const gutter = []
|
|
313
|
+
for (let index = 1; index <= Math.max(lines, 1); index += 1) gutter.push(`${index}\n`)
|
|
314
|
+
|
|
315
|
+
const bar = h('div', { key: 'bar', className: 'dw-editor-bar' }, [
|
|
316
|
+
h('span', { key: 'db', style: { color: 'var(--dsw-alias-label-primary)', fontWeight: 600 } }, tab.database),
|
|
317
|
+
h('span', { key: 'env' }, tab.subtitle ?? ''),
|
|
318
|
+
h('span', { key: 'sp', className: 'dw-spacer' }),
|
|
319
|
+
h('button', {
|
|
320
|
+
key: 'format',
|
|
321
|
+
type: 'button',
|
|
322
|
+
className: 'dw-btn',
|
|
323
|
+
title: tr('formatSqlHint'),
|
|
324
|
+
onClick: format,
|
|
325
|
+
}, [h(DwIcon, { key: 'i', name: 'format', size: 11 }), h('span', { key: 't' }, tr('formatSql'))]),
|
|
326
|
+
h('span', { key: 'hint', style: { fontSize: 11 } }, tr('runHint')),
|
|
327
|
+
h('button', {
|
|
328
|
+
key: 'run',
|
|
329
|
+
type: 'button',
|
|
330
|
+
className: 'dw-btn dw-btn-accent',
|
|
331
|
+
disabled: tab.running === true,
|
|
332
|
+
title: tr('runHint'),
|
|
333
|
+
onClick: runStatement,
|
|
334
|
+
}, [
|
|
335
|
+
h(DwIcon, { key: 'i', name: tab.running === true ? 'spinner' : 'play', size: 11 }),
|
|
336
|
+
h('span', { key: 't' }, tab.running === true ? tr('running') : tr('run')),
|
|
337
|
+
]),
|
|
338
|
+
])
|
|
339
|
+
|
|
340
|
+
const editorMenuLayer = editorMenu === null ? null : h(React.Fragment, null, [
|
|
341
|
+
h('div', {
|
|
342
|
+
key: 'backdrop',
|
|
343
|
+
className: 'dw-backdrop',
|
|
344
|
+
onClick: () => setEditorMenu(null),
|
|
345
|
+
onContextMenu: (event) => {
|
|
346
|
+
event.preventDefault()
|
|
347
|
+
setEditorMenu(null)
|
|
348
|
+
},
|
|
349
|
+
}),
|
|
350
|
+
h('div', { key: 'menu', className: 'dw-menu', style: { left: editorMenu.x, top: editorMenu.y } }, [
|
|
351
|
+
menuItem('undo', tr('editorUndo'), '⌘Z', () => editorCommand('undo')),
|
|
352
|
+
menuItem('redo', tr('editorRedo'), '⇧⌘Z', () => editorCommand('redo')),
|
|
353
|
+
h('div', { key: 'sep1', className: 'dw-project-sep' }),
|
|
354
|
+
menuItem('cut', tr('editorCut'), '⌘X', editorCut),
|
|
355
|
+
menuItem('copy', tr('editorCopy'), '⌘C', editorCopy),
|
|
356
|
+
menuItem('paste', tr('editorPaste'), '⌘V', () => void editorPaste()),
|
|
357
|
+
menuItem('select-all', tr('editorSelectAll'), '⌘A', editorSelectAll),
|
|
358
|
+
h('div', { key: 'sep2', className: 'dw-project-sep' }),
|
|
359
|
+
menuItem('optimize', tr('sqlOptimize'), '', openOptimize),
|
|
360
|
+
]),
|
|
361
|
+
])
|
|
362
|
+
|
|
363
|
+
return h('div', { className: 'dw-editor-wrap', style: { height } }, [
|
|
364
|
+
bar,
|
|
365
|
+
h('div', { key: 'editor', className: 'dw-editor' }, [
|
|
366
|
+
h('div', {
|
|
367
|
+
key: 'gutter',
|
|
368
|
+
className: 'dw-gutter',
|
|
369
|
+
ref: gutterRef,
|
|
370
|
+
'aria-hidden': true,
|
|
371
|
+
}, gutter.join('')),
|
|
372
|
+
h('div', { key: 'wrap', className: 'dw-code-wrap' }, [
|
|
373
|
+
h('pre', { key: 'hl', className: 'dw-code-highlight', ref: highlightRef, 'aria-hidden': true },
|
|
374
|
+
tokens.map((token, index) => (token.type === 'space'
|
|
375
|
+
? token.value
|
|
376
|
+
: h('span', { key: index, className: `dw-tok-${token.type}` }, token.value)))),
|
|
377
|
+
h('textarea', {
|
|
378
|
+
key: 'area',
|
|
379
|
+
ref: areaRef,
|
|
380
|
+
className: 'dw-code',
|
|
381
|
+
value,
|
|
382
|
+
spellCheck: false,
|
|
383
|
+
placeholder: tr('sqlPlaceholder'),
|
|
384
|
+
onChange: (event) => {
|
|
385
|
+
const text = event.target.value
|
|
386
|
+
const caret = event.target.selectionStart
|
|
387
|
+
store.setTabSql(tab.id, text)
|
|
388
|
+
refreshCompletion(text, caret)
|
|
389
|
+
},
|
|
390
|
+
onClick: (event) => refreshCompletion(event.target.value, event.target.selectionStart),
|
|
391
|
+
onContextMenu: (event) => {
|
|
392
|
+
event.preventDefault()
|
|
393
|
+
const width = 210
|
|
394
|
+
const height = 268
|
|
395
|
+
const maxX = (globalThis.innerWidth ?? 1200) - width
|
|
396
|
+
const maxY = (globalThis.innerHeight ?? 800) - height
|
|
397
|
+
setEditorMenu({
|
|
398
|
+
x: Math.max(8, Math.min(event.clientX, maxX)),
|
|
399
|
+
y: Math.max(8, Math.min(event.clientY, maxY)),
|
|
400
|
+
})
|
|
401
|
+
},
|
|
402
|
+
onBlur: () => setCompletion(null),
|
|
403
|
+
onScroll: (event) => syncScroll(event.target),
|
|
404
|
+
onKeyDown: (event) => {
|
|
405
|
+
const modifier = event.metaKey || event.ctrlKey
|
|
406
|
+
if (completion !== null) {
|
|
407
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
408
|
+
event.preventDefault()
|
|
409
|
+
const size = completion.items.length
|
|
410
|
+
const delta = event.key === 'ArrowDown' ? 1 : -1
|
|
411
|
+
setCompletion({ ...completion, index: (completion.index + delta + size) % size })
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
if ((event.key === 'Enter' && !modifier) || event.key === 'Tab') {
|
|
415
|
+
event.preventDefault()
|
|
416
|
+
applyCompletion(completion.items[completion.index])
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
if (event.key === 'Escape') {
|
|
420
|
+
event.preventDefault()
|
|
421
|
+
setCompletion(null)
|
|
422
|
+
return
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
if (modifier && event.key === 'Enter') {
|
|
426
|
+
event.preventDefault()
|
|
427
|
+
runStatement()
|
|
428
|
+
return
|
|
429
|
+
}
|
|
430
|
+
if (modifier && event.key === ' ') {
|
|
431
|
+
event.preventDefault()
|
|
432
|
+
refreshCompletion(value, event.target.selectionStart)
|
|
433
|
+
return
|
|
434
|
+
}
|
|
435
|
+
if (event.key === 'Tab' && !event.shiftKey && !modifier) {
|
|
436
|
+
event.preventDefault()
|
|
437
|
+
const area = event.target
|
|
438
|
+
const start = area.selectionStart
|
|
439
|
+
const end = area.selectionEnd
|
|
440
|
+
const next = `${value.slice(0, start)} ${value.slice(end)}`
|
|
441
|
+
store.setTabSql(tab.id, next)
|
|
442
|
+
requestAnimationFrame(() => {
|
|
443
|
+
area.setSelectionRange(start + 2, start + 2)
|
|
444
|
+
})
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
}),
|
|
448
|
+
completion === null
|
|
449
|
+
? null
|
|
450
|
+
: h('div', {
|
|
451
|
+
key: 'ac',
|
|
452
|
+
ref: completionRef,
|
|
453
|
+
className: 'dw-complete',
|
|
454
|
+
style: { left: completion.left, top: completion.top },
|
|
455
|
+
role: 'listbox',
|
|
456
|
+
}, completion.items.map((item, index) => h('div', {
|
|
457
|
+
key: `${item.kind}:${item.label}`,
|
|
458
|
+
role: 'option',
|
|
459
|
+
'aria-selected': index === completion.index,
|
|
460
|
+
className: index === completion.index ? 'dw-complete-row active' : 'dw-complete-row',
|
|
461
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
462
|
+
onClick: () => applyCompletion(item),
|
|
463
|
+
}, [
|
|
464
|
+
h('span', { key: 'l', className: 'dw-complete-label' }, item.label),
|
|
465
|
+
h('span', { key: 'd', className: 'dw-complete-detail' }, item.detail ?? ''),
|
|
466
|
+
h('span', { key: 'k', className: 'dw-complete-kind' }, item.kind === 'column' ? tr('suggestColumn') : item.kind === 'table' ? tr('suggestTable') : tr('suggestKeyword')),
|
|
467
|
+
]))),
|
|
468
|
+
]),
|
|
469
|
+
]),
|
|
470
|
+
editorMenuLayer,
|
|
471
|
+
optimize === null ? null : h(DwSqlOptimizeDialog, {
|
|
472
|
+
key: 'optimize',
|
|
473
|
+
store,
|
|
474
|
+
tr,
|
|
475
|
+
sql: optimize.sql,
|
|
476
|
+
database: optimize.database,
|
|
477
|
+
onClose: () => setOptimize(null),
|
|
478
|
+
}),
|
|
479
|
+
])
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** The DDL document: the create statement plus the column summary above it. */
|
|
483
|
+
function DwDdlView({ tab, tr, store }) {
|
|
484
|
+
const bar = h('div', { key: 'bar', className: 'dw-result-bar' }, [
|
|
485
|
+
h('span', { key: 'n' }, [tab.env, tab.database, tab.table].filter((part) => typeof part === 'string' && part !== '').join(' / ')),
|
|
486
|
+
h('span', { key: 'c' }, `${tab.columns.length} ${tr('columns')}`),
|
|
487
|
+
h('span', { key: 'sp', className: 'dw-spacer' }),
|
|
488
|
+
h('button', {
|
|
489
|
+
key: 'data',
|
|
490
|
+
type: 'button',
|
|
491
|
+
className: 'dw-btn',
|
|
492
|
+
onClick: () => {
|
|
493
|
+
store.seedStatement(
|
|
494
|
+
{ dbId: tab.dbId, database: tab.database, table: tab.table, env: tab.env, instanceLabel: tab.instanceLabel },
|
|
495
|
+
`SELECT *\nFROM \`${tab.database}\`.\`${tab.table}\`\nLIMIT 100`,
|
|
496
|
+
)
|
|
497
|
+
void store.runActiveTab()
|
|
498
|
+
},
|
|
499
|
+
}, [h(DwIcon, { key: 'i', name: 'table', size: 11 }), h('span', { key: 't' }, tr('openData'))]),
|
|
500
|
+
h('button', {
|
|
501
|
+
key: 'copy',
|
|
502
|
+
type: 'button',
|
|
503
|
+
className: 'dw-btn',
|
|
504
|
+
onClick: () => {
|
|
505
|
+
globalThis.navigator?.clipboard?.writeText?.(tab.ddl).catch(() => undefined)
|
|
506
|
+
store.pushLog('info', tr('log.copiedDdl', { table: tab.table }))
|
|
507
|
+
},
|
|
508
|
+
}, [h(DwIcon, { key: 'i', name: 'copy', size: 11 }), h('span', { key: 't' }, tr('copyDdl'))]),
|
|
509
|
+
h('button', {
|
|
510
|
+
key: 'refresh',
|
|
511
|
+
type: 'button',
|
|
512
|
+
className: 'dw-btn dw-btn-icon',
|
|
513
|
+
title: tr('refreshDdl'),
|
|
514
|
+
disabled: tab.loading === true,
|
|
515
|
+
onClick: () => void store.refreshDdl(tab.id),
|
|
516
|
+
}, h(DwIcon, { name: tab.loading === true ? 'spinner' : 'refresh', size: 12 })),
|
|
517
|
+
])
|
|
518
|
+
|
|
519
|
+
if (tab.loading === true) {
|
|
520
|
+
return h('div', { className: 'dw-ddl' }, [bar, h('div', { key: 'body', className: 'dw-empty' }, [
|
|
521
|
+
h(DwIcon, { key: 'i', name: 'spinner', size: 18 }),
|
|
522
|
+
h('div', { key: 't', className: 'dw-empty-hint' }, tr('ddlLoading')),
|
|
523
|
+
])])
|
|
524
|
+
}
|
|
525
|
+
if (tab.error !== '') {
|
|
526
|
+
return h('div', { className: 'dw-ddl' }, [bar, h('div', { key: 'body', className: 'dw-empty' }, [
|
|
527
|
+
h('div', { key: 't', className: 'dw-empty-title', style: { color: 'var(--dsw-alias-state-error-primary)' } }, tr('ddlFailed')),
|
|
528
|
+
h('div', { key: 'm', className: 'dw-error-text' }, tab.error),
|
|
529
|
+
])])
|
|
530
|
+
}
|
|
531
|
+
return h('div', { className: 'dw-ddl' }, [
|
|
532
|
+
bar,
|
|
533
|
+
h('pre', { key: 'body', className: 'dw-ddl-body' }, h('code', null, tab.ddl === '' ? tr('ddlEmpty') : tab.ddl)),
|
|
534
|
+
])
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
export { DwTabs, DwEditor, DwDdlView, DwDragHandle }
|