dsh-data-window 0.1.1 → 0.1.3
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 +1 -1
- package/CHANGELOG.md +67 -0
- package/README.md +17 -3
- package/client.js +1760 -214
- package/docs/BRIDGE.md +6 -0
- package/docs/DETAIL.md +50 -5
- package/locale/en.json +64 -1
- package/locale/zh.json +64 -1
- package/package.json +1 -1
- package/src/client/00-css.js +204 -1
- package/src/client/05-icons.js +21 -0
- package/src/client/07-bridge.js +10 -1
- package/src/client/10-core.js +182 -64
- package/src/client/20-tree.js +51 -17
- package/src/client/27-favorites.js +331 -0
- package/src/client/30-editor.js +490 -94
- package/src/client/40-results.js +3 -0
- package/src/client/50-panel.js +203 -25
- package/src/client/63-schema.js +1 -0
- package/src/client/64-manual.js +6 -1
- package/src/client/65-ai.js +1 -0
- package/src/client/66-optimize.js +120 -1
- package/src/host/favorites.js +188 -0
- package/src/host/rpc.js +51 -0
- package/src/host/shared.js +10 -2
package/src/client/30-editor.js
CHANGED
|
@@ -7,62 +7,205 @@
|
|
|
7
7
|
* statement gets its own id from the store, never a rewrite of the first.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
/** The reference a tab copies: `schema.table`, or the schema alone. */
|
|
11
|
+
function dwTabReference(tab) {
|
|
12
|
+
const database = String(tab?.database ?? '').trim()
|
|
13
|
+
// Table consoles keep the table in their id (`sql:<dbId>:tbl:<table>`).
|
|
14
|
+
const id = String(tab?.id ?? '')
|
|
15
|
+
const table = String(tab?.table ?? '').trim()
|
|
16
|
+
|| (id.includes(':tbl:') ? id.split(':tbl:').slice(1).join(':tbl:') : '')
|
|
17
|
+
if (database !== '' && table !== '') return `${database}.${table}`
|
|
18
|
+
if (database !== '') return database
|
|
19
|
+
return String(tab?.title ?? '')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Insert text into the chat composer when it is open; false when it is not. */
|
|
23
|
+
function dwInsertComposerText(text) {
|
|
24
|
+
const doc = globalThis.document
|
|
25
|
+
if (doc === undefined || typeof doc.querySelector !== 'function') return false
|
|
26
|
+
const seat = doc.querySelector('[data-composer-seat]')
|
|
27
|
+
if (seat === null) return false
|
|
28
|
+
const editor = seat.querySelector('[contenteditable="true"], textarea')
|
|
29
|
+
if (editor === null) return false
|
|
30
|
+
try {
|
|
31
|
+
editor.focus()
|
|
32
|
+
if (editor.tagName === 'TEXTAREA') {
|
|
33
|
+
const setter = Object.getOwnPropertyDescriptor(globalThis.HTMLTextAreaElement.prototype, 'value')?.set
|
|
34
|
+
const start = editor.selectionStart ?? editor.value.length
|
|
35
|
+
const end = editor.selectionEnd ?? start
|
|
36
|
+
if (typeof setter === 'function') setter.call(editor, `${editor.value.slice(0, start)}${text}${editor.value.slice(end)}`)
|
|
37
|
+
editor.setSelectionRange(start + text.length, start + text.length)
|
|
38
|
+
editor.dispatchEvent(new Event('input', { bubbles: true }))
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
const before = editor.textContent
|
|
42
|
+
const selection = typeof globalThis.getSelection === 'function' ? globalThis.getSelection() : null
|
|
43
|
+
if (selection === null || selection === undefined) return false
|
|
44
|
+
const range = doc.createRange()
|
|
45
|
+
range.selectNodeContents(editor)
|
|
46
|
+
range.collapse(false)
|
|
47
|
+
selection.removeAllRanges()
|
|
48
|
+
selection.addRange(range)
|
|
49
|
+
doc.execCommand('insertText', false, text)
|
|
50
|
+
return editor.textContent !== before
|
|
51
|
+
} catch (error) {
|
|
52
|
+
void error
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function DwTabs({ store, state, tr, onFavorite }) {
|
|
58
|
+
const [menu, setMenu] = useState(null)
|
|
11
59
|
if ((state.tabs ?? []).length === 0) return null
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
'
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
60
|
+
|
|
61
|
+
const copyReference = (tab) => {
|
|
62
|
+
const reference = dwTabReference(tab)
|
|
63
|
+
const write = globalThis.navigator?.clipboard?.writeText?.(reference)
|
|
64
|
+
if (write === undefined) {
|
|
65
|
+
store.pushLog('warn', tr('copyUpdateFailed'))
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
write
|
|
69
|
+
.then(() => store.pushLog('info', tr('log.copiedReference', { reference })))
|
|
70
|
+
.catch(() => store.pushLog('warn', tr('copyUpdateFailed')))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const copyReferenceToChat = (tab) => {
|
|
74
|
+
const reference = dwTabReference(tab)
|
|
75
|
+
const inserted = dwInsertComposerText(`${reference} `)
|
|
76
|
+
const write = globalThis.navigator?.clipboard?.writeText?.(reference)
|
|
77
|
+
if (write !== undefined) write.catch(() => undefined)
|
|
78
|
+
store.pushLog('info', tr(inserted ? 'log.copiedReferenceToChat' : 'log.copiedReferenceOnly', { reference }))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const menuItem = (key, icon, label, disabled, run) => h('button', {
|
|
82
|
+
key,
|
|
83
|
+
type: 'button',
|
|
84
|
+
role: 'menuitem',
|
|
85
|
+
className: 'dw-tabmenu-item',
|
|
86
|
+
disabled,
|
|
87
|
+
onClick: () => {
|
|
88
|
+
setMenu(null)
|
|
89
|
+
run()
|
|
90
|
+
},
|
|
91
|
+
}, [
|
|
92
|
+
h('span', { key: 'ic', className: 'dw-tabmenu-icon' }, h(DwIcon, { name: icon, size: 13 })),
|
|
93
|
+
h('span', { key: 'lb', className: 'dw-tabmenu-label' }, label),
|
|
94
|
+
])
|
|
95
|
+
|
|
96
|
+
const index = menu === null ? -1 : (state.tabs ?? []).findIndex((tab) => tab.id === menu.tab.id)
|
|
97
|
+
const menuLayer = menu === null ? null : h('div', {
|
|
98
|
+
key: 'backdrop',
|
|
99
|
+
className: 'dw-tabmenu-backdrop',
|
|
100
|
+
onPointerDown: () => setMenu(null),
|
|
101
|
+
onContextMenu: (event) => {
|
|
102
|
+
event.preventDefault()
|
|
103
|
+
setMenu(null)
|
|
104
|
+
},
|
|
105
|
+
}, h('div', {
|
|
106
|
+
key: 'menu',
|
|
107
|
+
className: 'dw-tabmenu',
|
|
108
|
+
role: 'menu',
|
|
109
|
+
'aria-label': tr('tabMenuLabel'),
|
|
110
|
+
style: { left: menu.x, top: menu.y },
|
|
111
|
+
onPointerDown: (event) => event.stopPropagation(),
|
|
112
|
+
}, [
|
|
113
|
+
menuItem('close', 'close', tr('tabClose'), false, () => store.closeTab(menu.tab.id)),
|
|
114
|
+
menuItem('close-others', 'layers', tr('tabCloseOthers'), (state.tabs ?? []).length <= 1, () => store.closeOtherTabs(menu.tab.id)),
|
|
115
|
+
menuItem('close-all', 'stack', tr('tabCloseAll'), false, () => store.closeAllTabs()),
|
|
116
|
+
menuItem('close-left', 'arrow-left', tr('tabCloseLeft'), index <= 0, () => store.closeTabsToLeft(menu.tab.id)),
|
|
117
|
+
menuItem('close-right', 'arrow-right', tr('tabCloseRight'), index < 0 || index >= (state.tabs ?? []).length - 1, () => store.closeTabsToRight(menu.tab.id)),
|
|
118
|
+
h('div', { key: 'sep', className: 'dw-tabmenu-sep' }),
|
|
119
|
+
menuItem('favorite', 'star', tr('favoriteSave'), String(menu.tab.sql ?? '').trim() === '', () => {
|
|
120
|
+
if (typeof onFavorite === 'function') onFavorite(menu.tab)
|
|
121
|
+
}),
|
|
122
|
+
menuItem('copy-reference', 'copy', tr('copyReference'), false, () => copyReference(menu.tab)),
|
|
123
|
+
menuItem('copy-reference-chat', 'at', tr('copyReferenceToChat'), false, () => copyReferenceToChat(menu.tab)),
|
|
124
|
+
]))
|
|
125
|
+
|
|
126
|
+
return h(React.Fragment, null, [
|
|
127
|
+
h('div', { key: 'tabs', className: 'dw-tabs', role: 'tablist' }, state.tabs.map((tab) => {
|
|
128
|
+
const active = tab.id === state.activeTab
|
|
129
|
+
return h('div', {
|
|
130
|
+
key: tab.id,
|
|
131
|
+
role: 'tab',
|
|
132
|
+
'aria-selected': active,
|
|
133
|
+
className: `dw-tab${active ? ' dw-tab-active' : ''}`,
|
|
134
|
+
title: tab.subtitle ?? tab.title,
|
|
135
|
+
onClick: () => store.setActiveTab(tab.id),
|
|
136
|
+
onContextMenu: (event) => {
|
|
23
137
|
event.preventDefault()
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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)
|
|
138
|
+
// Keep the menu inside the panel, like the code workspace's menu does.
|
|
139
|
+
const rect = typeof document === 'undefined' ? null : document.querySelector('.dw-root')?.getBoundingClientRect()
|
|
140
|
+
const left = rect === null || rect === undefined ? 8 : Math.max(8, rect.left + 8)
|
|
141
|
+
const top = rect === null || rect === undefined ? 8 : Math.max(8, rect.top + 8)
|
|
142
|
+
const right = (rect === null || rect === undefined ? (globalThis.innerWidth ?? 1200) : rect.right) - 248
|
|
143
|
+
const bottom = (rect === null || rect === undefined ? (globalThis.innerHeight ?? 800) : rect.bottom) - 268
|
|
144
|
+
setMenu({
|
|
145
|
+
x: Math.max(left, Math.min(event.clientX, Math.max(left, right))),
|
|
146
|
+
y: Math.max(top, Math.min(event.clientY, Math.max(top, bottom))),
|
|
147
|
+
tab,
|
|
148
|
+
})
|
|
42
149
|
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
150
|
+
onAuxClick: (event) => {
|
|
151
|
+
if (event.button === 1) {
|
|
152
|
+
event.preventDefault()
|
|
153
|
+
store.closeTab(tab.id)
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
}, [
|
|
157
|
+
h(DwIcon, { key: 'ic', name: 'database', size: 12 }),
|
|
158
|
+
h('span', { key: 'lb', className: 'dw-tab-label' }, tab.title),
|
|
159
|
+
// Several environments can hold a schema of the same name, so the tab says
|
|
160
|
+
// which one it is; the full text stays in the tooltip.
|
|
161
|
+
tab.env !== '' ? h('span', { key: 'env', className: 'dw-badge', style: { marginLeft: 2 } }, tab.env) : null,
|
|
162
|
+
h('button', {
|
|
163
|
+
key: 'cl',
|
|
164
|
+
type: 'button',
|
|
165
|
+
className: 'dw-tab-close',
|
|
166
|
+
title: tr('closeTab'),
|
|
167
|
+
'aria-label': tr('closeTab'),
|
|
168
|
+
onClick: (event) => {
|
|
169
|
+
event.stopPropagation()
|
|
170
|
+
store.closeTab(tab.id)
|
|
171
|
+
},
|
|
172
|
+
}, h(DwIcon, { name: 'close', size: 10 })),
|
|
173
|
+
])
|
|
174
|
+
})),
|
|
175
|
+
menuLayer,
|
|
176
|
+
])
|
|
46
177
|
}
|
|
47
178
|
|
|
48
|
-
/**
|
|
49
|
-
|
|
179
|
+
/**
|
|
180
|
+
* A draggable horizontal divider.
|
|
181
|
+
*
|
|
182
|
+
* `invert` is for a handle whose panel grows upward (the run log): dragging up
|
|
183
|
+
* must make it taller. `onCommit` fires once on release, which is when a caller
|
|
184
|
+
* should persist the value; writing on every pointermove would hit storage
|
|
185
|
+
* sixty times a second.
|
|
186
|
+
*/
|
|
187
|
+
function DwDragHandle({ value, onChange, min, max, invert, title, onStart, onCommit, onReset }) {
|
|
50
188
|
return h('div', {
|
|
51
189
|
className: 'dw-drag',
|
|
52
190
|
role: 'separator',
|
|
53
191
|
'aria-orientation': 'horizontal',
|
|
54
|
-
title: '↕',
|
|
192
|
+
title: title ?? '↕',
|
|
193
|
+
onDoubleClick: typeof onReset === 'function' ? onReset : undefined,
|
|
55
194
|
onPointerDown: (event) => {
|
|
56
195
|
event.preventDefault()
|
|
196
|
+
if (typeof onStart === 'function') onStart()
|
|
57
197
|
const startY = event.clientY
|
|
58
198
|
const startValue = value
|
|
199
|
+
const direction = invert === true ? -1 : 1
|
|
200
|
+
let latest = value
|
|
59
201
|
const onMove = (moveEvent) => {
|
|
60
|
-
|
|
61
|
-
onChange(
|
|
202
|
+
latest = Math.min(max, Math.max(min, startValue + (moveEvent.clientY - startY) * direction))
|
|
203
|
+
onChange(latest)
|
|
62
204
|
}
|
|
63
205
|
const onUp = () => {
|
|
64
206
|
globalThis.removeEventListener('pointermove', onMove)
|
|
65
207
|
globalThis.removeEventListener('pointerup', onUp)
|
|
208
|
+
if (typeof onCommit === 'function') onCommit(latest)
|
|
66
209
|
}
|
|
67
210
|
globalThis.addEventListener('pointermove', onMove)
|
|
68
211
|
globalThis.addEventListener('pointerup', onUp)
|
|
@@ -104,17 +247,21 @@ function dwCaretPoint(area, text, caret) {
|
|
|
104
247
|
}
|
|
105
248
|
|
|
106
249
|
/** The SQL editor: highlighted text, completion and one-keystroke formatting. */
|
|
107
|
-
function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
250
|
+
function DwEditor({ store, tab, tr, onRun, height, schema, favorites }) {
|
|
108
251
|
const value = tab.sql ?? ''
|
|
109
252
|
const tokens = useMemo(() => dwSqlLex(value), [value])
|
|
110
253
|
const lines = useMemo(() => value.split('\n').length, [value])
|
|
254
|
+
const favoriteList = Array.isArray(favorites) ? favorites : []
|
|
111
255
|
const gutterRef = useRef(null)
|
|
112
256
|
const areaRef = useRef(null)
|
|
113
257
|
const highlightRef = useRef(null)
|
|
114
258
|
const completionRef = useRef(null)
|
|
259
|
+
const mentionRef = useRef(null)
|
|
115
260
|
const [completion, setCompletion] = useState(null)
|
|
261
|
+
const [mention, setMention] = useState(null)
|
|
116
262
|
const [editorMenu, setEditorMenu] = useState(null)
|
|
117
263
|
const [optimize, setOptimize] = useState(null)
|
|
264
|
+
const [favoriteDraft, setFavoriteDraft] = useState(null)
|
|
118
265
|
|
|
119
266
|
useEffect(() => {
|
|
120
267
|
const area = areaRef.current
|
|
@@ -123,8 +270,15 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
123
270
|
// lands where the user is about to type.
|
|
124
271
|
area.focus()
|
|
125
272
|
area.setSelectionRange(area.value.length, area.value.length)
|
|
273
|
+
setMention(null)
|
|
126
274
|
}, [tab.id])
|
|
127
275
|
|
|
276
|
+
// The `@` index reads the machine-wide saved statements; one round trip the
|
|
277
|
+
// first time a console opens, cached in the store afterwards.
|
|
278
|
+
useEffect(() => {
|
|
279
|
+
void store.loadFavorites()
|
|
280
|
+
}, [store])
|
|
281
|
+
|
|
128
282
|
// The completion list needs the schema of this database; one round trip, cached
|
|
129
283
|
// by the store. A failure only degrades the list to keywords.
|
|
130
284
|
useEffect(() => {
|
|
@@ -162,6 +316,7 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
162
316
|
// Re-evaluate the caret once the map arrives so the popup appears on its own.
|
|
163
317
|
useEffect(() => {
|
|
164
318
|
if (schema === undefined) return
|
|
319
|
+
if (mention !== null) return
|
|
165
320
|
const area = areaRef.current
|
|
166
321
|
if (area === null || document.activeElement !== area) return
|
|
167
322
|
refreshCompletion(value, area.selectionStart)
|
|
@@ -180,6 +335,18 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
180
335
|
else if (bottom > popup.scrollTop + popup.clientHeight) popup.scrollTop = bottom - popup.clientHeight
|
|
181
336
|
}, [completion])
|
|
182
337
|
|
|
338
|
+
// Same keyboard-follows-list behaviour for the `@` index.
|
|
339
|
+
useEffect(() => {
|
|
340
|
+
const popup = mentionRef.current
|
|
341
|
+
if (popup === null || mention === null) return
|
|
342
|
+
const active = popup.querySelector('.dw-complete-row.active')
|
|
343
|
+
if (active === null) return
|
|
344
|
+
const top = active.offsetTop
|
|
345
|
+
const bottom = top + active.offsetHeight
|
|
346
|
+
if (top < popup.scrollTop) popup.scrollTop = top
|
|
347
|
+
else if (bottom > popup.scrollTop + popup.clientHeight) popup.scrollTop = bottom - popup.clientHeight
|
|
348
|
+
}, [mention])
|
|
349
|
+
|
|
183
350
|
const applyCompletion = (item) => {
|
|
184
351
|
const current = completion
|
|
185
352
|
if (current === null || item === undefined) return
|
|
@@ -195,6 +362,54 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
195
362
|
})
|
|
196
363
|
}
|
|
197
364
|
|
|
365
|
+
/**
|
|
366
|
+
* The text a mention is filtering on, or `undefined` when the caret left it.
|
|
367
|
+
*
|
|
368
|
+
* A name may contain spaces (and Chinese), so the popup only ends at characters
|
|
369
|
+
* that cannot appear in a name the user would type.
|
|
370
|
+
*/
|
|
371
|
+
const mentionQueryAt = (text, from, caret) => {
|
|
372
|
+
if (from < 0 || text[from] !== '@' || caret <= from) return undefined
|
|
373
|
+
const query = text.slice(from + 1, caret)
|
|
374
|
+
if (query.length > 60 || /[\r\n,;()]/.test(query)) return undefined
|
|
375
|
+
// `@ ` is a sentence, not a mention: keep whitespace only once a name started.
|
|
376
|
+
if (query !== '' && query.trim() === '') return undefined
|
|
377
|
+
return query
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Grow the mention popup while its query is typed, or close it. */
|
|
381
|
+
const refreshMention = (area, text, caret) => {
|
|
382
|
+
const query = mentionQueryAt(text, mention.from, caret)
|
|
383
|
+
if (query === undefined) {
|
|
384
|
+
setMention(null)
|
|
385
|
+
return false
|
|
386
|
+
}
|
|
387
|
+
const point = dwCaretPoint(area, text, caret)
|
|
388
|
+
setMention({ ...mention, to: caret, query, index: 0, left: point.left, top: point.top + 20 })
|
|
389
|
+
return true
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Put one saved statement where the `@…` token is, and keep the caret after it. */
|
|
393
|
+
const insertFavorite = (favorite) => {
|
|
394
|
+
const current = mention
|
|
395
|
+
if (current === null || favorite === undefined || favorite === null) return
|
|
396
|
+
const statement = String(favorite.sql ?? '')
|
|
397
|
+
if (statement.trim() === '') return
|
|
398
|
+
const next = `${value.slice(0, current.from)}${statement}${value.slice(current.to)}`
|
|
399
|
+
const position = current.from + statement.length
|
|
400
|
+
store.setTabSql(tab.id, next)
|
|
401
|
+
setMention(null)
|
|
402
|
+
requestAnimationFrame(() => {
|
|
403
|
+
const area = areaRef.current
|
|
404
|
+
if (area === null) return
|
|
405
|
+
area.focus()
|
|
406
|
+
area.setSelectionRange(position, position)
|
|
407
|
+
})
|
|
408
|
+
store.pushLog('info', tr('log.favoriteInserted', { name: favorite.name }))
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const mentionItems = mention === null ? [] : dwFavoriteMatches(favoriteList, mention.query)
|
|
412
|
+
|
|
198
413
|
const runStatement = () => {
|
|
199
414
|
const area = areaRef.current
|
|
200
415
|
const selected = area !== null && area.selectionEnd > area.selectionStart
|
|
@@ -292,6 +507,28 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
292
507
|
setOptimize({ sql: text, database: tab.database })
|
|
293
508
|
}
|
|
294
509
|
|
|
510
|
+
// Collect the selection when there is one, the whole console otherwise: that is
|
|
511
|
+
// exactly the statement the user right-clicked. The schema comes from the console
|
|
512
|
+
// and the table from its id, falling back to the first table the statement names.
|
|
513
|
+
const openFavorite = () => {
|
|
514
|
+
const area = areaRef.current
|
|
515
|
+
const selected = area !== null && area.selectionEnd > area.selectionStart
|
|
516
|
+
? value.slice(area.selectionStart, area.selectionEnd)
|
|
517
|
+
: ''
|
|
518
|
+
const text = selected.trim() === '' ? value.trim() : selected.trim()
|
|
519
|
+
if (text === '') {
|
|
520
|
+
store.pushLog('warn', tr('log.noStatement'))
|
|
521
|
+
return
|
|
522
|
+
}
|
|
523
|
+
setFavoriteDraft({
|
|
524
|
+
sql: text,
|
|
525
|
+
name: '',
|
|
526
|
+
database: tab.database,
|
|
527
|
+
table: dwTabTableName(tab) || (dwSqlTableRefs(text)[0]?.table ?? ''),
|
|
528
|
+
env: tab.env ?? '',
|
|
529
|
+
})
|
|
530
|
+
}
|
|
531
|
+
|
|
295
532
|
const menuItem = (key, label, shortcut, run) => h('button', {
|
|
296
533
|
key,
|
|
297
534
|
type: 'button',
|
|
@@ -356,6 +593,7 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
356
593
|
menuItem('paste', tr('editorPaste'), '⌘V', () => void editorPaste()),
|
|
357
594
|
menuItem('select-all', tr('editorSelectAll'), '⌘A', editorSelectAll),
|
|
358
595
|
h('div', { key: 'sep2', className: 'dw-project-sep' }),
|
|
596
|
+
menuItem('favorite', tr('favoriteSave'), '', openFavorite),
|
|
359
597
|
menuItem('optimize', tr('sqlOptimize'), '', openOptimize),
|
|
360
598
|
]),
|
|
361
599
|
])
|
|
@@ -385,13 +623,29 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
385
623
|
const text = event.target.value
|
|
386
624
|
const caret = event.target.selectionStart
|
|
387
625
|
store.setTabSql(tab.id, text)
|
|
626
|
+
if (mention !== null) {
|
|
627
|
+
// Still inside the same `@…` token: filter it instead of letting the
|
|
628
|
+
// column list fight the favorites list for the same keystrokes.
|
|
629
|
+
if (refreshMention(event.target, text, caret)) return
|
|
630
|
+
}
|
|
631
|
+
if (caret > 0 && text[caret - 1] === '@' && dwFavoriteMentionStart(text, caret - 1)) {
|
|
632
|
+
const point = dwCaretPoint(event.target, text, caret)
|
|
633
|
+
setCompletion(null)
|
|
634
|
+
setMention({ from: caret - 1, to: caret, query: '', index: 0, left: point.left, top: point.top + 20 })
|
|
635
|
+
return
|
|
636
|
+
}
|
|
388
637
|
refreshCompletion(text, caret)
|
|
389
638
|
},
|
|
390
|
-
onClick: (event) =>
|
|
639
|
+
onClick: (event) => {
|
|
640
|
+
setMention(null)
|
|
641
|
+
refreshCompletion(event.target.value, event.target.selectionStart)
|
|
642
|
+
},
|
|
391
643
|
onContextMenu: (event) => {
|
|
392
644
|
event.preventDefault()
|
|
645
|
+
setMention(null)
|
|
646
|
+
setCompletion(null)
|
|
393
647
|
const width = 210
|
|
394
|
-
const height =
|
|
648
|
+
const height = 300
|
|
395
649
|
const maxX = (globalThis.innerWidth ?? 1200) - width
|
|
396
650
|
const maxY = (globalThis.innerHeight ?? 800) - height
|
|
397
651
|
setEditorMenu({
|
|
@@ -399,10 +653,42 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
399
653
|
y: Math.max(8, Math.min(event.clientY, maxY)),
|
|
400
654
|
})
|
|
401
655
|
},
|
|
402
|
-
onBlur: () =>
|
|
656
|
+
onBlur: () => {
|
|
657
|
+
setCompletion(null)
|
|
658
|
+
setMention(null)
|
|
659
|
+
},
|
|
403
660
|
onScroll: (event) => syncScroll(event.target),
|
|
404
661
|
onKeyDown: (event) => {
|
|
662
|
+
// An IME candidate key (Enter to commit, Escape to cancel, arrows to
|
|
663
|
+
// move through the list) belongs to the IME: never turn it into a
|
|
664
|
+
// command, a suggestion pick or a run.
|
|
665
|
+
if (dwImeComposing(event)) return
|
|
405
666
|
const modifier = event.metaKey || event.ctrlKey
|
|
667
|
+
if (mention !== null) {
|
|
668
|
+
const size = mentionItems.length
|
|
669
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
670
|
+
if (size === 0) {
|
|
671
|
+
setMention(null)
|
|
672
|
+
return
|
|
673
|
+
}
|
|
674
|
+
event.preventDefault()
|
|
675
|
+
const delta = event.key === 'ArrowDown' ? 1 : -1
|
|
676
|
+
setMention({ ...mention, index: (mention.index + delta + size) % size })
|
|
677
|
+
return
|
|
678
|
+
}
|
|
679
|
+
if ((event.key === 'Enter' && !modifier) || event.key === 'Tab') {
|
|
680
|
+
if (size > 0) {
|
|
681
|
+
event.preventDefault()
|
|
682
|
+
insertFavorite(mentionItems[Math.min(mention.index, size - 1)])
|
|
683
|
+
return
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (event.key === 'Escape') {
|
|
687
|
+
event.preventDefault()
|
|
688
|
+
setMention(null)
|
|
689
|
+
return
|
|
690
|
+
}
|
|
691
|
+
}
|
|
406
692
|
if (completion !== null) {
|
|
407
693
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
408
694
|
event.preventDefault()
|
|
@@ -465,6 +751,32 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
465
751
|
h('span', { key: 'd', className: 'dw-complete-detail' }, item.detail ?? ''),
|
|
466
752
|
h('span', { key: 'k', className: 'dw-complete-kind' }, item.kind === 'column' ? tr('suggestColumn') : item.kind === 'table' ? tr('suggestTable') : tr('suggestKeyword')),
|
|
467
753
|
]))),
|
|
754
|
+
mention === null
|
|
755
|
+
? null
|
|
756
|
+
: h('div', {
|
|
757
|
+
key: 'mention',
|
|
758
|
+
ref: mentionRef,
|
|
759
|
+
className: 'dw-complete dw-mention',
|
|
760
|
+
style: { left: mention.left, top: mention.top },
|
|
761
|
+
role: 'listbox',
|
|
762
|
+
'aria-label': tr('favoriteMentionLabel'),
|
|
763
|
+
}, mentionItems.length === 0
|
|
764
|
+
? h('div', { key: 'none', className: 'dw-complete-empty' },
|
|
765
|
+
favoriteList.length === 0 ? tr('favoriteMentionNoFavorites') : tr('favoriteMentionEmpty'))
|
|
766
|
+
: mentionItems.map((favorite, index) => h('div', {
|
|
767
|
+
key: favorite.id,
|
|
768
|
+
role: 'option',
|
|
769
|
+
'aria-selected': index === mention.index,
|
|
770
|
+
className: index === mention.index ? 'dw-complete-row active' : 'dw-complete-row',
|
|
771
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
772
|
+
onClick: () => insertFavorite(favorite),
|
|
773
|
+
}, [
|
|
774
|
+
h('span', { key: 'ic', className: 'dw-node-icon' }, h(DwIcon, { name: 'star', size: 11 })),
|
|
775
|
+
h('span', { key: 'l', className: 'dw-complete-label' }, favorite.name),
|
|
776
|
+
h('span', { key: 'd', className: 'dw-complete-detail' },
|
|
777
|
+
[favorite.database, favorite.table].filter((part) => typeof part === 'string' && part !== '').join('.')),
|
|
778
|
+
h('span', { key: 'k', className: 'dw-complete-kind' }, tr('suggestFavorite')),
|
|
779
|
+
]))),
|
|
468
780
|
]),
|
|
469
781
|
]),
|
|
470
782
|
editorMenuLayer,
|
|
@@ -476,62 +788,146 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
476
788
|
database: optimize.database,
|
|
477
789
|
onClose: () => setOptimize(null),
|
|
478
790
|
}),
|
|
791
|
+
favoriteDraft === null ? null : h(DwFavoriteDialog, {
|
|
792
|
+
key: 'favorite',
|
|
793
|
+
store,
|
|
794
|
+
tr,
|
|
795
|
+
draft: favoriteDraft,
|
|
796
|
+
onClose: () => setFavoriteDraft(null),
|
|
797
|
+
}),
|
|
479
798
|
])
|
|
480
799
|
}
|
|
481
800
|
|
|
482
|
-
/** The
|
|
483
|
-
function
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|
-
])
|
|
801
|
+
/** The create statement as a dialog: copy it and close, no work tab involved. */
|
|
802
|
+
function DwDdlDialog({ node, tr, store, onClose }) {
|
|
803
|
+
const allTables = node.kind === 'database'
|
|
804
|
+
const [phase, setPhase] = useState('loading')
|
|
805
|
+
const [ddl, setDdl] = useState('')
|
|
806
|
+
const [error, setError] = useState('')
|
|
807
|
+
const [progress, setProgress] = useState({ done: 0, total: 0 })
|
|
808
|
+
const [failureCount, setFailureCount] = useState(0)
|
|
809
|
+
const [count, setCount] = useState(0)
|
|
810
|
+
|
|
811
|
+
useEffect(() => {
|
|
812
|
+
let alive = true
|
|
813
|
+
const fail = (failure, name) => {
|
|
814
|
+
if (!alive) return
|
|
815
|
+
const message = dwExplainFailure(tr, failure)
|
|
816
|
+
setError(message)
|
|
817
|
+
setPhase('error')
|
|
818
|
+
store.pushLog('error', tr('log.loadFailed', { name, message }))
|
|
819
|
+
}
|
|
518
820
|
|
|
519
|
-
|
|
520
|
-
|
|
821
|
+
const loadTable = async () => {
|
|
822
|
+
try {
|
|
823
|
+
const value = await dwRpc('ddl', { id: node.dbId, table: node.table })
|
|
824
|
+
if (!alive) return
|
|
825
|
+
setDdl(String(value?.ddl ?? ''))
|
|
826
|
+
setCount(1)
|
|
827
|
+
setPhase('done')
|
|
828
|
+
store.pushLog('info', tr('log.ddl', { table: node.table, ms: dwDuration(value?.statementTime ?? 0) }))
|
|
829
|
+
} catch (failure) {
|
|
830
|
+
fail(failure, node.table)
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// A whole database: list the tables, then read each statement in turn so the
|
|
835
|
+
// dialog can show progress and a closed dialog stops issuing round trips.
|
|
836
|
+
const loadDatabase = async () => {
|
|
837
|
+
const startedAt = Date.now()
|
|
838
|
+
try {
|
|
839
|
+
const listed = await dwRpc('tables', { id: node.dbId })
|
|
840
|
+
if (!alive) return
|
|
841
|
+
const names = (Array.isArray(listed?.tables) ? listed.tables : [])
|
|
842
|
+
.map((table) => String(table?.name ?? ''))
|
|
843
|
+
.filter((name) => name !== '')
|
|
844
|
+
setProgress({ done: 0, total: names.length })
|
|
845
|
+
const parts = []
|
|
846
|
+
const failures = []
|
|
847
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
848
|
+
if (!alive) return
|
|
849
|
+
try {
|
|
850
|
+
const value = await dwRpc('ddl', { id: node.dbId, table: names[index] })
|
|
851
|
+
const text = String(value?.ddl ?? '').trim().replace(/;+$/, '')
|
|
852
|
+
if (text !== '') parts.push(`-- ${names[index]}\n${text};`)
|
|
853
|
+
} catch (failure) {
|
|
854
|
+
failures.push(`${names[index]}: ${dwExplainFailure(tr, failure).replace(/\s+/g, ' ')}`)
|
|
855
|
+
}
|
|
856
|
+
if (alive) setProgress({ done: index + 1, total: names.length })
|
|
857
|
+
}
|
|
858
|
+
if (!alive) return
|
|
859
|
+
const failedBlock = failures.length === 0 ? '' : failures.map((line) => `-- ${line}`).join('\n')
|
|
860
|
+
setDdl([...parts, failedBlock].filter((part) => part !== '').join('\n\n'))
|
|
861
|
+
setCount(parts.length)
|
|
862
|
+
setFailureCount(failures.length)
|
|
863
|
+
setPhase('done')
|
|
864
|
+
store.pushLog('info', tr('log.ddlAll', { database: node.database, count: parts.length, ms: Date.now() - startedAt }))
|
|
865
|
+
} catch (failure) {
|
|
866
|
+
fail(failure, node.database)
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (allTables) void loadDatabase()
|
|
871
|
+
else void loadTable()
|
|
872
|
+
return () => { alive = false }
|
|
873
|
+
}, [])
|
|
874
|
+
|
|
875
|
+
const loadingHint = allTables && progress.total > 0
|
|
876
|
+
? `${tr('ddlLoading')} ${progress.done}/${progress.total}`
|
|
877
|
+
: tr('ddlLoading')
|
|
878
|
+
const body = phase === 'loading'
|
|
879
|
+
? h('div', { key: 'loading', className: 'dw-empty', style: { minHeight: 120 } }, [
|
|
521
880
|
h(DwIcon, { key: 'i', name: 'spinner', size: 18 }),
|
|
522
|
-
h('div', { key: 't', className: 'dw-empty-hint' },
|
|
523
|
-
])
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
881
|
+
h('div', { key: 't', className: 'dw-empty-hint' }, loadingHint),
|
|
882
|
+
])
|
|
883
|
+
: phase === 'error'
|
|
884
|
+
? h('div', { key: 'error' }, [
|
|
885
|
+
h('div', { key: 't', className: 'dw-empty-title', style: { color: 'var(--dsw-alias-state-error-primary)', fontSize: 12 } }, tr('ddlFailed')),
|
|
886
|
+
h('div', { key: 'm', className: 'dw-error-text' }, error),
|
|
887
|
+
])
|
|
888
|
+
: h('div', { key: 'done', className: 'dw-ddl-result' }, [
|
|
889
|
+
failureCount > 0
|
|
890
|
+
? h('div', { key: 'warn', className: 'dw-log-warn' }, tr('ddlAllPartial', { count: failureCount }))
|
|
891
|
+
: null,
|
|
892
|
+
h('pre', { key: 'ddl', className: 'dw-cell-view dw-ddl-code' },
|
|
893
|
+
ddl === '' ? tr(allTables ? 'ddlAllEmpty' : 'ddlEmpty') : ddl),
|
|
894
|
+
])
|
|
895
|
+
|
|
896
|
+
return h('div', { className: 'dw-modal', onClick: onClose }, h('div', {
|
|
897
|
+
key: 'card',
|
|
898
|
+
className: 'dw-modal-card dw-ddl-dialog',
|
|
899
|
+
onClick: (event) => event.stopPropagation(),
|
|
900
|
+
}, [
|
|
901
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
902
|
+
h(DwIcon, { key: 'i', name: 'ddl', size: 13 }),
|
|
903
|
+
h('span', { key: 't' }, tr(allTables ? 'menuDdlAll' : 'menuDdl')),
|
|
904
|
+
h('span', { key: 'p', style: { color: 'var(--dsw-alias-label-secondary)', fontWeight: 400, fontSize: 11 } },
|
|
905
|
+
[node.env, node.database, node.table].filter((part) => typeof part === 'string' && part !== '').join(' / ')),
|
|
906
|
+
]),
|
|
907
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, body),
|
|
908
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
909
|
+
phase === 'done' && ddl !== ''
|
|
910
|
+
? h('button', {
|
|
911
|
+
key: 'copy',
|
|
912
|
+
type: 'button',
|
|
913
|
+
className: 'dw-btn',
|
|
914
|
+
onClick: () => {
|
|
915
|
+
const write = globalThis.navigator?.clipboard?.writeText?.(ddl)
|
|
916
|
+
if (write === undefined) {
|
|
917
|
+
store.pushLog('warn', tr('copyUpdateFailed'))
|
|
918
|
+
return
|
|
919
|
+
}
|
|
920
|
+
write
|
|
921
|
+
.then(() => store.pushLog('info', allTables
|
|
922
|
+
? tr('log.copiedDdlAll', { database: node.database, count })
|
|
923
|
+
: tr('log.copiedDdl', { table: node.table })))
|
|
924
|
+
.catch(() => store.pushLog('warn', tr('copyUpdateFailed')))
|
|
925
|
+
},
|
|
926
|
+
}, [h(DwIcon, { key: 'i', name: 'copy', size: 11 }), h('span', { key: 't' }, tr('copyDdl'))])
|
|
927
|
+
: null,
|
|
928
|
+
h('button', { key: 'close', type: 'button', className: 'dw-btn dw-btn-accent', onClick: onClose }, tr('close')),
|
|
929
|
+
]),
|
|
930
|
+
]))
|
|
535
931
|
}
|
|
536
932
|
|
|
537
|
-
export { DwTabs, DwEditor,
|
|
933
|
+
export { DwTabs, DwEditor, DwDdlDialog, DwDragHandle }
|