dsh-data-window 0.1.2 → 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/CHANGELOG.md +39 -0
- package/README.md +10 -1
- package/client.js +1095 -41
- package/docs/DETAIL.md +27 -4
- package/locale/en.json +46 -1
- package/locale/zh.json +46 -1
- package/package.json +1 -1
- package/src/client/00-css.js +86 -0
- package/src/client/05-icons.js +2 -0
- package/src/client/10-core.js +150 -0
- package/src/client/20-tree.js +33 -11
- package/src/client/27-favorites.js +331 -0
- package/src/client/30-editor.js +202 -11
- package/src/client/40-results.js +3 -0
- package/src/client/50-panel.js +189 -18
- 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/host/favorites.js +188 -0
- package/src/host/rpc.js +51 -0
|
@@ -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 }
|
package/src/client/30-editor.js
CHANGED
|
@@ -54,7 +54,7 @@ function dwInsertComposerText(text) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function DwTabs({ store, state, tr }) {
|
|
57
|
+
function DwTabs({ store, state, tr, onFavorite }) {
|
|
58
58
|
const [menu, setMenu] = useState(null)
|
|
59
59
|
if ((state.tabs ?? []).length === 0) return null
|
|
60
60
|
|
|
@@ -116,6 +116,9 @@ function DwTabs({ store, state, tr }) {
|
|
|
116
116
|
menuItem('close-left', 'arrow-left', tr('tabCloseLeft'), index <= 0, () => store.closeTabsToLeft(menu.tab.id)),
|
|
117
117
|
menuItem('close-right', 'arrow-right', tr('tabCloseRight'), index < 0 || index >= (state.tabs ?? []).length - 1, () => store.closeTabsToRight(menu.tab.id)),
|
|
118
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
|
+
}),
|
|
119
122
|
menuItem('copy-reference', 'copy', tr('copyReference'), false, () => copyReference(menu.tab)),
|
|
120
123
|
menuItem('copy-reference-chat', 'at', tr('copyReferenceToChat'), false, () => copyReferenceToChat(menu.tab)),
|
|
121
124
|
]))
|
|
@@ -137,7 +140,7 @@ function DwTabs({ store, state, tr }) {
|
|
|
137
140
|
const left = rect === null || rect === undefined ? 8 : Math.max(8, rect.left + 8)
|
|
138
141
|
const top = rect === null || rect === undefined ? 8 : Math.max(8, rect.top + 8)
|
|
139
142
|
const right = (rect === null || rect === undefined ? (globalThis.innerWidth ?? 1200) : rect.right) - 248
|
|
140
|
-
const bottom = (rect === null || rect === undefined ? (globalThis.innerHeight ?? 800) : rect.bottom) -
|
|
143
|
+
const bottom = (rect === null || rect === undefined ? (globalThis.innerHeight ?? 800) : rect.bottom) - 268
|
|
141
144
|
setMenu({
|
|
142
145
|
x: Math.max(left, Math.min(event.clientX, Math.max(left, right))),
|
|
143
146
|
y: Math.max(top, Math.min(event.clientY, Math.max(top, bottom))),
|
|
@@ -173,24 +176,36 @@ function DwTabs({ store, state, tr }) {
|
|
|
173
176
|
])
|
|
174
177
|
}
|
|
175
178
|
|
|
176
|
-
/**
|
|
177
|
-
|
|
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 }) {
|
|
178
188
|
return h('div', {
|
|
179
189
|
className: 'dw-drag',
|
|
180
190
|
role: 'separator',
|
|
181
191
|
'aria-orientation': 'horizontal',
|
|
182
|
-
title: '↕',
|
|
192
|
+
title: title ?? '↕',
|
|
193
|
+
onDoubleClick: typeof onReset === 'function' ? onReset : undefined,
|
|
183
194
|
onPointerDown: (event) => {
|
|
184
195
|
event.preventDefault()
|
|
196
|
+
if (typeof onStart === 'function') onStart()
|
|
185
197
|
const startY = event.clientY
|
|
186
198
|
const startValue = value
|
|
199
|
+
const direction = invert === true ? -1 : 1
|
|
200
|
+
let latest = value
|
|
187
201
|
const onMove = (moveEvent) => {
|
|
188
|
-
|
|
189
|
-
onChange(
|
|
202
|
+
latest = Math.min(max, Math.max(min, startValue + (moveEvent.clientY - startY) * direction))
|
|
203
|
+
onChange(latest)
|
|
190
204
|
}
|
|
191
205
|
const onUp = () => {
|
|
192
206
|
globalThis.removeEventListener('pointermove', onMove)
|
|
193
207
|
globalThis.removeEventListener('pointerup', onUp)
|
|
208
|
+
if (typeof onCommit === 'function') onCommit(latest)
|
|
194
209
|
}
|
|
195
210
|
globalThis.addEventListener('pointermove', onMove)
|
|
196
211
|
globalThis.addEventListener('pointerup', onUp)
|
|
@@ -232,17 +247,21 @@ function dwCaretPoint(area, text, caret) {
|
|
|
232
247
|
}
|
|
233
248
|
|
|
234
249
|
/** The SQL editor: highlighted text, completion and one-keystroke formatting. */
|
|
235
|
-
function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
250
|
+
function DwEditor({ store, tab, tr, onRun, height, schema, favorites }) {
|
|
236
251
|
const value = tab.sql ?? ''
|
|
237
252
|
const tokens = useMemo(() => dwSqlLex(value), [value])
|
|
238
253
|
const lines = useMemo(() => value.split('\n').length, [value])
|
|
254
|
+
const favoriteList = Array.isArray(favorites) ? favorites : []
|
|
239
255
|
const gutterRef = useRef(null)
|
|
240
256
|
const areaRef = useRef(null)
|
|
241
257
|
const highlightRef = useRef(null)
|
|
242
258
|
const completionRef = useRef(null)
|
|
259
|
+
const mentionRef = useRef(null)
|
|
243
260
|
const [completion, setCompletion] = useState(null)
|
|
261
|
+
const [mention, setMention] = useState(null)
|
|
244
262
|
const [editorMenu, setEditorMenu] = useState(null)
|
|
245
263
|
const [optimize, setOptimize] = useState(null)
|
|
264
|
+
const [favoriteDraft, setFavoriteDraft] = useState(null)
|
|
246
265
|
|
|
247
266
|
useEffect(() => {
|
|
248
267
|
const area = areaRef.current
|
|
@@ -251,8 +270,15 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
251
270
|
// lands where the user is about to type.
|
|
252
271
|
area.focus()
|
|
253
272
|
area.setSelectionRange(area.value.length, area.value.length)
|
|
273
|
+
setMention(null)
|
|
254
274
|
}, [tab.id])
|
|
255
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
|
+
|
|
256
282
|
// The completion list needs the schema of this database; one round trip, cached
|
|
257
283
|
// by the store. A failure only degrades the list to keywords.
|
|
258
284
|
useEffect(() => {
|
|
@@ -290,6 +316,7 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
290
316
|
// Re-evaluate the caret once the map arrives so the popup appears on its own.
|
|
291
317
|
useEffect(() => {
|
|
292
318
|
if (schema === undefined) return
|
|
319
|
+
if (mention !== null) return
|
|
293
320
|
const area = areaRef.current
|
|
294
321
|
if (area === null || document.activeElement !== area) return
|
|
295
322
|
refreshCompletion(value, area.selectionStart)
|
|
@@ -308,6 +335,18 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
308
335
|
else if (bottom > popup.scrollTop + popup.clientHeight) popup.scrollTop = bottom - popup.clientHeight
|
|
309
336
|
}, [completion])
|
|
310
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
|
+
|
|
311
350
|
const applyCompletion = (item) => {
|
|
312
351
|
const current = completion
|
|
313
352
|
if (current === null || item === undefined) return
|
|
@@ -323,6 +362,54 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
323
362
|
})
|
|
324
363
|
}
|
|
325
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
|
+
|
|
326
413
|
const runStatement = () => {
|
|
327
414
|
const area = areaRef.current
|
|
328
415
|
const selected = area !== null && area.selectionEnd > area.selectionStart
|
|
@@ -420,6 +507,28 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
420
507
|
setOptimize({ sql: text, database: tab.database })
|
|
421
508
|
}
|
|
422
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
|
+
|
|
423
532
|
const menuItem = (key, label, shortcut, run) => h('button', {
|
|
424
533
|
key,
|
|
425
534
|
type: 'button',
|
|
@@ -484,6 +593,7 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
484
593
|
menuItem('paste', tr('editorPaste'), '⌘V', () => void editorPaste()),
|
|
485
594
|
menuItem('select-all', tr('editorSelectAll'), '⌘A', editorSelectAll),
|
|
486
595
|
h('div', { key: 'sep2', className: 'dw-project-sep' }),
|
|
596
|
+
menuItem('favorite', tr('favoriteSave'), '', openFavorite),
|
|
487
597
|
menuItem('optimize', tr('sqlOptimize'), '', openOptimize),
|
|
488
598
|
]),
|
|
489
599
|
])
|
|
@@ -513,13 +623,29 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
513
623
|
const text = event.target.value
|
|
514
624
|
const caret = event.target.selectionStart
|
|
515
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
|
+
}
|
|
516
637
|
refreshCompletion(text, caret)
|
|
517
638
|
},
|
|
518
|
-
onClick: (event) =>
|
|
639
|
+
onClick: (event) => {
|
|
640
|
+
setMention(null)
|
|
641
|
+
refreshCompletion(event.target.value, event.target.selectionStart)
|
|
642
|
+
},
|
|
519
643
|
onContextMenu: (event) => {
|
|
520
644
|
event.preventDefault()
|
|
645
|
+
setMention(null)
|
|
646
|
+
setCompletion(null)
|
|
521
647
|
const width = 210
|
|
522
|
-
const height =
|
|
648
|
+
const height = 300
|
|
523
649
|
const maxX = (globalThis.innerWidth ?? 1200) - width
|
|
524
650
|
const maxY = (globalThis.innerHeight ?? 800) - height
|
|
525
651
|
setEditorMenu({
|
|
@@ -527,10 +653,42 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
527
653
|
y: Math.max(8, Math.min(event.clientY, maxY)),
|
|
528
654
|
})
|
|
529
655
|
},
|
|
530
|
-
onBlur: () =>
|
|
656
|
+
onBlur: () => {
|
|
657
|
+
setCompletion(null)
|
|
658
|
+
setMention(null)
|
|
659
|
+
},
|
|
531
660
|
onScroll: (event) => syncScroll(event.target),
|
|
532
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
|
|
533
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
|
+
}
|
|
534
692
|
if (completion !== null) {
|
|
535
693
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
536
694
|
event.preventDefault()
|
|
@@ -593,6 +751,32 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
593
751
|
h('span', { key: 'd', className: 'dw-complete-detail' }, item.detail ?? ''),
|
|
594
752
|
h('span', { key: 'k', className: 'dw-complete-kind' }, item.kind === 'column' ? tr('suggestColumn') : item.kind === 'table' ? tr('suggestTable') : tr('suggestKeyword')),
|
|
595
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
|
+
]))),
|
|
596
780
|
]),
|
|
597
781
|
]),
|
|
598
782
|
editorMenuLayer,
|
|
@@ -604,6 +788,13 @@ function DwEditor({ store, tab, tr, onRun, height, schema }) {
|
|
|
604
788
|
database: optimize.database,
|
|
605
789
|
onClose: () => setOptimize(null),
|
|
606
790
|
}),
|
|
791
|
+
favoriteDraft === null ? null : h(DwFavoriteDialog, {
|
|
792
|
+
key: 'favorite',
|
|
793
|
+
store,
|
|
794
|
+
tr,
|
|
795
|
+
draft: favoriteDraft,
|
|
796
|
+
onClose: () => setFavoriteDraft(null),
|
|
797
|
+
}),
|
|
607
798
|
])
|
|
608
799
|
}
|
|
609
800
|
|
package/src/client/40-results.js
CHANGED
|
@@ -91,6 +91,9 @@ const DwGrid = React.memo(function DwGrid({
|
|
|
91
91
|
props.title = tr('editingCellHint')
|
|
92
92
|
props.onBlur = (event) => onEditCommit(rowIndex, cellIndex, column.name, event.currentTarget.textContent)
|
|
93
93
|
props.onKeyDown = (event) => {
|
|
94
|
+
// Committing an IME candidate with Enter must not blur the editor: the
|
|
95
|
+
// cell is still being typed, and blur would write the pre-commit value.
|
|
96
|
+
if (dwImeComposing(event)) return
|
|
94
97
|
if (event.key === 'Enter') {
|
|
95
98
|
event.preventDefault()
|
|
96
99
|
event.currentTarget.blur()
|