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
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Saved SQL: the favorites tab's rows, the dialog that names one, and the `@`
|
|
3
|
+
* index the workbench inserts from.
|
|
4
|
+
*
|
|
5
|
+
* One statement is saved with its name, its schema and its table. The favorites
|
|
6
|
+
* tab groups by schema then table, which is the shape the user recognised the
|
|
7
|
+
* statement in; the workbench only needs the name, because the caret is already in
|
|
8
|
+
* a console. Both views read the same list, so a save from one is visible in the
|
|
9
|
+
* other without a reload.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** A console's table, from its id (`sql:<dbId>:tbl:<table>`). */
|
|
13
|
+
function dwTabTableName(tab) {
|
|
14
|
+
const id = String(tab?.id ?? '')
|
|
15
|
+
const index = id.indexOf(':tbl:')
|
|
16
|
+
return index < 0 ? '' : id.slice(index + 5)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Every schema name the tree knows, for the favorite dialog's datalist. */
|
|
20
|
+
function dwDatabaseNames(environments) {
|
|
21
|
+
const names = new Set()
|
|
22
|
+
for (const environment of Array.isArray(environments) ? environments : []) {
|
|
23
|
+
for (const instance of Array.isArray(environment?.instances) ? environment.instances : []) {
|
|
24
|
+
for (const database of Array.isArray(instance?.databases) ? instance.databases : []) {
|
|
25
|
+
const name = String(database?.name ?? '')
|
|
26
|
+
if (name !== '') names.add(name)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return [...names].sort((left, right) => left.localeCompare(right))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The tree node a favorite's schema name points at.
|
|
35
|
+
*
|
|
36
|
+
* A favorite stores a name, not a database id, because the tree is shared and the
|
|
37
|
+
* same schema may be reachable through another workspace later. The environment
|
|
38
|
+
* the statement was saved under breaks ties when two environments hold a schema of
|
|
39
|
+
* the same name.
|
|
40
|
+
*/
|
|
41
|
+
function dwFindDatabaseTarget(environments, database, env) {
|
|
42
|
+
const name = String(database ?? '').trim().toLowerCase()
|
|
43
|
+
if (name === '') return undefined
|
|
44
|
+
const preferred = String(env ?? '').trim().toLowerCase()
|
|
45
|
+
let fallback
|
|
46
|
+
for (const environment of Array.isArray(environments) ? environments : []) {
|
|
47
|
+
for (const instance of Array.isArray(environment?.instances) ? environment.instances : []) {
|
|
48
|
+
for (const entry of Array.isArray(instance?.databases) ? instance.databases : []) {
|
|
49
|
+
if (String(entry?.name ?? '').toLowerCase() !== name) continue
|
|
50
|
+
if (typeof entry?.id !== 'string' || entry.id === '') continue
|
|
51
|
+
const target = {
|
|
52
|
+
env: environment.env,
|
|
53
|
+
node: {
|
|
54
|
+
key: dwNodeKey('db', entry.id),
|
|
55
|
+
kind: 'database',
|
|
56
|
+
label: entry.name,
|
|
57
|
+
detail: '',
|
|
58
|
+
dbId: entry.id,
|
|
59
|
+
database: entry.name,
|
|
60
|
+
env: environment.env,
|
|
61
|
+
instanceLabel: instance.label ?? '',
|
|
62
|
+
configured: entry.discovered !== true,
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
if (preferred !== '' && String(environment.env ?? '').toLowerCase() === preferred) return target
|
|
66
|
+
if (fallback === undefined) fallback = target
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return fallback
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One favorite as a tree leaf. */
|
|
74
|
+
function dwFavoriteLeaf(favorite) {
|
|
75
|
+
const reference = [favorite.database, favorite.table].filter((part) => typeof part === 'string' && part !== '').join('.')
|
|
76
|
+
return {
|
|
77
|
+
node: {
|
|
78
|
+
key: dwNodeKey('favorite', favorite.id),
|
|
79
|
+
kind: 'favorite',
|
|
80
|
+
label: favorite.name,
|
|
81
|
+
detail: '',
|
|
82
|
+
title: `${favorite.name} · ${reference}\n\n${favorite.sql}`,
|
|
83
|
+
favorite,
|
|
84
|
+
},
|
|
85
|
+
children: [],
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The favorites tab's rows: schema → table → statement.
|
|
91
|
+
*
|
|
92
|
+
* A statement saved without a table name still gets a table group, so the tab
|
|
93
|
+
* keeps one shape instead of mixing leaves and groups under a schema.
|
|
94
|
+
*/
|
|
95
|
+
function dwBuildFavoriteRows(favorites, tr) {
|
|
96
|
+
const list = (Array.isArray(favorites) ? favorites : []).filter((favorite) => favorite !== null
|
|
97
|
+
&& typeof favorite === 'object'
|
|
98
|
+
&& typeof favorite.id === 'string'
|
|
99
|
+
&& favorite.id !== '')
|
|
100
|
+
if (list.length === 0) return []
|
|
101
|
+
const byDatabase = new Map()
|
|
102
|
+
for (const favorite of list) {
|
|
103
|
+
const database = String(favorite.database ?? '').trim() || tr('favoriteNoDatabase')
|
|
104
|
+
const table = String(favorite.table ?? '').trim()
|
|
105
|
+
const tableKey = table === '' ? '' : table.toLowerCase()
|
|
106
|
+
if (!byDatabase.has(database)) byDatabase.set(database, new Map())
|
|
107
|
+
const tables = byDatabase.get(database)
|
|
108
|
+
if (!tables.has(tableKey)) tables.set(tableKey, { table, favorites: [] })
|
|
109
|
+
tables.get(tableKey).favorites.push(favorite)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const rows = []
|
|
113
|
+
const databaseNames = [...byDatabase.keys()].sort((left, right) => left.localeCompare(right))
|
|
114
|
+
for (const databaseName of databaseNames) {
|
|
115
|
+
const tables = byDatabase.get(databaseName)
|
|
116
|
+
const tableKeys = [...tables.keys()].sort((left, right) => {
|
|
117
|
+
if (left === '') return 1
|
|
118
|
+
if (right === '') return -1
|
|
119
|
+
return left.localeCompare(right)
|
|
120
|
+
})
|
|
121
|
+
const tableRows = []
|
|
122
|
+
let statementCount = 0
|
|
123
|
+
for (const tableKey of tableKeys) {
|
|
124
|
+
const group = tables.get(tableKey)
|
|
125
|
+
const leaves = group.favorites
|
|
126
|
+
.slice()
|
|
127
|
+
.sort((left, right) => String(left.name ?? '').localeCompare(String(right.name ?? '')))
|
|
128
|
+
.map(dwFavoriteLeaf)
|
|
129
|
+
statementCount += leaves.length
|
|
130
|
+
tableRows.push({
|
|
131
|
+
node: {
|
|
132
|
+
key: dwNodeKey('favoriteTable', databaseName, tableKey),
|
|
133
|
+
kind: 'favoriteTable',
|
|
134
|
+
label: tableKey === '' ? tr('favoriteNoTable') : group.table,
|
|
135
|
+
detail: `${leaves.length} ${tr('favoritesCount')}`,
|
|
136
|
+
title: `${databaseName}${tableKey === '' ? '' : `.${group.table}`}`,
|
|
137
|
+
database: databaseName,
|
|
138
|
+
table: group.table,
|
|
139
|
+
},
|
|
140
|
+
children: leaves,
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
rows.push({
|
|
144
|
+
node: {
|
|
145
|
+
key: dwNodeKey('favoriteDatabase', databaseName),
|
|
146
|
+
kind: 'favoriteDatabase',
|
|
147
|
+
label: databaseName,
|
|
148
|
+
detail: `${statementCount} ${tr('favoritesCount')}`,
|
|
149
|
+
title: `${tr('favoritesTitle')} · ${databaseName}`,
|
|
150
|
+
database: databaseName,
|
|
151
|
+
},
|
|
152
|
+
children: tableRows,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
return rows
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Whether an `@` typed at `index` starts a favorite mention rather than `@var`. */
|
|
159
|
+
function dwFavoriteMentionStart(text, index) {
|
|
160
|
+
if (index <= 0) return true
|
|
161
|
+
const before = text[index - 1]
|
|
162
|
+
return /\s/.test(before) || '(),;=<>+-*/'.includes(before)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The saved statements matching what was typed after `@`, name first. */
|
|
166
|
+
function dwFavoriteMatches(favorites, query) {
|
|
167
|
+
const needle = String(query ?? '').trim().toLowerCase()
|
|
168
|
+
const list = (Array.isArray(favorites) ? favorites : []).filter((favorite) => favorite !== null
|
|
169
|
+
&& typeof favorite === 'object'
|
|
170
|
+
&& typeof favorite.name === 'string')
|
|
171
|
+
const matched = needle === ''
|
|
172
|
+
? list
|
|
173
|
+
: list.filter((favorite) => [favorite.name, favorite.database, favorite.table]
|
|
174
|
+
.some((value) => String(value ?? '').toLowerCase().includes(needle)))
|
|
175
|
+
return matched
|
|
176
|
+
.slice()
|
|
177
|
+
.sort((left, right) => left.name.localeCompare(right.name))
|
|
178
|
+
.slice(0, 50)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Name one statement and keep it.
|
|
183
|
+
*
|
|
184
|
+
* The SQL is a read-only preview: what is collected is exactly the selection (or
|
|
185
|
+
* the whole editor) the user right-clicked, and a rename or a re-collection is the
|
|
186
|
+
* way to change it. The schema and table are editable because the console only
|
|
187
|
+
* knows its own database — a statement that joins another schema is the user's
|
|
188
|
+
* call, and it is what the tree groups by.
|
|
189
|
+
*/
|
|
190
|
+
function DwFavoriteDialog({ store, tr, draft, onClose, onSaved }) {
|
|
191
|
+
const editing = String(draft?.id ?? '') !== ''
|
|
192
|
+
const [name, setName] = useState(String(draft?.name ?? ''))
|
|
193
|
+
const [database, setDatabase] = useState(String(draft?.database ?? ''))
|
|
194
|
+
const [table, setTable] = useState(String(draft?.table ?? ''))
|
|
195
|
+
const [saving, setSaving] = useState(false)
|
|
196
|
+
const [error, setError] = useState('')
|
|
197
|
+
const sql = String(draft?.sql ?? '')
|
|
198
|
+
const savingRef = useRef(false)
|
|
199
|
+
savingRef.current = saving
|
|
200
|
+
// A Chinese/Japanese name is typed through the IME: its Enter belongs to the
|
|
201
|
+
// candidate list, not to the dialog. Chromium already flags the composition
|
|
202
|
+
// keystroke, and the short grace period covers a browser that ends the
|
|
203
|
+
// composition just before delivering that Enter.
|
|
204
|
+
const composingRef = useRef(false)
|
|
205
|
+
const compositionEndedAtRef = useRef(0)
|
|
206
|
+
const imeBusy = (event) => dwImeComposing(event)
|
|
207
|
+
|| composingRef.current === true
|
|
208
|
+
|| Date.now() - compositionEndedAtRef.current < 100
|
|
209
|
+
const databaseNames = dwDatabaseNames(store.getState?.()?.environments)
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
const onKey = (event) => {
|
|
213
|
+
if (event.key !== 'Escape' || savingRef.current) return
|
|
214
|
+
if (imeBusy(event)) return
|
|
215
|
+
onClose()
|
|
216
|
+
}
|
|
217
|
+
globalThis.addEventListener('keydown', onKey)
|
|
218
|
+
return () => globalThis.removeEventListener('keydown', onKey)
|
|
219
|
+
}, [onClose])
|
|
220
|
+
|
|
221
|
+
const submit = async () => {
|
|
222
|
+
if (saving === true) return
|
|
223
|
+
const nextName = name.trim()
|
|
224
|
+
const nextDatabase = database.trim()
|
|
225
|
+
if (nextName === '') {
|
|
226
|
+
setError(tr('favoriteNameRequired'))
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
if (nextDatabase === '') {
|
|
230
|
+
setError(tr('favoriteDatabaseRequired'))
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
// The Host owns the real check; this one only saves a round trip on the
|
|
234
|
+
// failure the user is most likely to hit while typing a name.
|
|
235
|
+
const taken = (store.getState?.()?.favorites ?? []).find((favorite) => String(favorite?.name ?? '').trim().toLowerCase() === nextName.toLowerCase()
|
|
236
|
+
&& favorite?.id !== draft?.id)
|
|
237
|
+
if (taken !== undefined) {
|
|
238
|
+
setError(tr('error.duplicateFavorite'))
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
setSaving(true)
|
|
242
|
+
setError('')
|
|
243
|
+
try {
|
|
244
|
+
const value = await store.saveFavorite({
|
|
245
|
+
id: draft?.id,
|
|
246
|
+
name: nextName,
|
|
247
|
+
database: nextDatabase,
|
|
248
|
+
table: table.trim(),
|
|
249
|
+
env: draft?.env ?? '',
|
|
250
|
+
sql,
|
|
251
|
+
})
|
|
252
|
+
if (typeof onSaved === 'function') onSaved(value?.favorite)
|
|
253
|
+
onClose()
|
|
254
|
+
} catch (failure) {
|
|
255
|
+
setError(dwExplainFailure(tr, failure))
|
|
256
|
+
setSaving(false)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const field = (key, label, value, onChange, hint, options) => h('label', { key, className: 'dw-form-row' }, [
|
|
261
|
+
h('span', { key: 'l', className: 'dw-form-label' }, label),
|
|
262
|
+
h('input', {
|
|
263
|
+
key: 'i',
|
|
264
|
+
className: 'dw-form-input',
|
|
265
|
+
value,
|
|
266
|
+
spellCheck: false,
|
|
267
|
+
autoComplete: 'off',
|
|
268
|
+
autoFocus: options?.focus === true,
|
|
269
|
+
list: options?.list,
|
|
270
|
+
maxLength: options?.maxLength,
|
|
271
|
+
onChange: (event) => onChange(event.target.value),
|
|
272
|
+
onCompositionStart: () => {
|
|
273
|
+
composingRef.current = true
|
|
274
|
+
},
|
|
275
|
+
onCompositionEnd: () => {
|
|
276
|
+
composingRef.current = false
|
|
277
|
+
compositionEndedAtRef.current = Date.now()
|
|
278
|
+
},
|
|
279
|
+
onKeyDown: (event) => {
|
|
280
|
+
if (event.key !== 'Enter') return
|
|
281
|
+
if (imeBusy(event)) return
|
|
282
|
+
void submit()
|
|
283
|
+
},
|
|
284
|
+
}),
|
|
285
|
+
hint === undefined ? null : h('span', { key: 'h', className: 'dw-form-hint' }, hint),
|
|
286
|
+
])
|
|
287
|
+
|
|
288
|
+
return h('div', {
|
|
289
|
+
className: 'dw-modal',
|
|
290
|
+
onClick: (event) => {
|
|
291
|
+
if (event.target === event.currentTarget && saving !== true) onClose()
|
|
292
|
+
},
|
|
293
|
+
}, h('div', {
|
|
294
|
+
key: 'card',
|
|
295
|
+
className: 'dw-modal-card dw-favorite-dialog',
|
|
296
|
+
role: 'dialog',
|
|
297
|
+
'aria-modal': 'true',
|
|
298
|
+
'aria-label': tr(editing ? 'favoriteEditTitle' : 'favoriteSaveTitle'),
|
|
299
|
+
}, [
|
|
300
|
+
h('div', { key: 'title', className: 'dw-modal-title' }, [
|
|
301
|
+
h(DwIcon, { key: 'i', name: 'star', size: 15 }),
|
|
302
|
+
h('span', { key: 't' }, tr(editing ? 'favoriteEditTitle' : 'favoriteSaveTitle')),
|
|
303
|
+
]),
|
|
304
|
+
h('div', { key: 'body', className: 'dw-modal-body' }, h('div', { key: 'form', className: 'dw-form' }, [
|
|
305
|
+
field('name', tr('favoriteName'), name, setName, tr('favoriteNameHint'), { focus: true, maxLength: 80 }),
|
|
306
|
+
h('div', { key: 'split', className: 'dw-fav-split' }, [
|
|
307
|
+
field('database', tr('favoriteDatabase'), database, setDatabase, tr('favoriteDatabaseHint'), { list: 'dw-favorite-databases', maxLength: 64 }),
|
|
308
|
+
field('table', tr('favoriteTable'), table, setTable, tr('favoriteTableHint'), { maxLength: 64 }),
|
|
309
|
+
]),
|
|
310
|
+
h('datalist', { key: 'names', id: 'dw-favorite-databases' }, databaseNames.map((value) => h('option', { key: value, value }))),
|
|
311
|
+
h('div', { key: 'sql-label', className: 'dw-form-label' }, tr('favoriteSql')),
|
|
312
|
+
h('pre', { key: 'sql', className: 'dw-fav-sql' }, sql),
|
|
313
|
+
h('div', { key: 'hint', className: 'dw-ai-note' }, tr('favoriteDialogHint')),
|
|
314
|
+
error === ''
|
|
315
|
+
? null
|
|
316
|
+
: h('div', { key: 'err', className: 'dw-ai-note dw-ai-warn' }, error),
|
|
317
|
+
])),
|
|
318
|
+
h('div', { key: 'foot', className: 'dw-modal-foot' }, [
|
|
319
|
+
h('button', { key: 'cancel', type: 'button', className: 'dw-btn', disabled: saving === true, onClick: onClose }, tr('cancel')),
|
|
320
|
+
h('button', {
|
|
321
|
+
key: 'save',
|
|
322
|
+
type: 'button',
|
|
323
|
+
className: 'dw-btn dw-btn-accent',
|
|
324
|
+
disabled: saving === true,
|
|
325
|
+
onClick: () => void submit(),
|
|
326
|
+
}, saving === true ? tr('schemaApplying') : tr(editing ? 'favoriteUpdateAction' : 'favoriteSaveAction')),
|
|
327
|
+
]),
|
|
328
|
+
]))
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export { dwTabTableName, dwDatabaseNames, dwFindDatabaseTarget, dwBuildFavoriteRows, dwFavoriteMentionStart, dwFavoriteMatches, DwFavoriteDialog }
|