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.
Files changed (45) hide show
  1. package/AGENTS.md +111 -0
  2. package/CHANGELOG.md +239 -0
  3. package/LICENSE +21 -0
  4. package/README.md +146 -2
  5. package/client.js +7779 -0
  6. package/cordis.patch.yml +4 -0
  7. package/docs/BRIDGE.md +100 -0
  8. package/docs/DESIGN.md +417 -0
  9. package/docs/DETAIL.md +220 -0
  10. package/docs/screenshots/ddl.png +0 -0
  11. package/docs/screenshots/workbench.png +0 -0
  12. package/icon.svg +30 -0
  13. package/index.js +180 -0
  14. package/locale/en.json +286 -0
  15. package/locale/zh.json +286 -0
  16. package/package.json +68 -4
  17. package/src/client/00-css.js +993 -0
  18. package/src/client/05-icons.js +137 -0
  19. package/src/client/05-theme.js +107 -0
  20. package/src/client/06-shortcuts.js +89 -0
  21. package/src/client/07-bridge.js +193 -0
  22. package/src/client/10-core.js +1352 -0
  23. package/src/client/20-tree.js +281 -0
  24. package/src/client/25-sql.js +631 -0
  25. package/src/client/30-editor.js +537 -0
  26. package/src/client/40-results.js +556 -0
  27. package/src/client/45-export.js +219 -0
  28. package/src/client/50-panel.js +761 -0
  29. package/src/client/60-settings.js +180 -0
  30. package/src/client/63-schema.js +186 -0
  31. package/src/client/64-manual.js +350 -0
  32. package/src/client/65-ai.js +320 -0
  33. package/src/client/66-optimize.js +98 -0
  34. package/src/client/90-apply.js +183 -0
  35. package/src/host/ai.js +721 -0
  36. package/src/host/cache.js +157 -0
  37. package/src/host/catalog.js +161 -0
  38. package/src/host/consoles.js +116 -0
  39. package/src/host/discovery.js +603 -0
  40. package/src/host/manual.js +276 -0
  41. package/src/host/mysql.js +650 -0
  42. package/src/host/remembered.js +225 -0
  43. package/src/host/rpc.js +487 -0
  44. package/src/host/shared.js +172 -0
  45. package/src/host/yaml.js +335 -0
@@ -0,0 +1,556 @@
1
+ /**
2
+ * The result grid.
3
+ *
4
+ * Rows arrive as arrays of already-normalized cells, so the grid only decides how a
5
+ * value looks: `NULL` is the one value a SQL client must render differently from the
6
+ * text `null`, and a cell wider than the column is clipped with the full value in
7
+ * its tooltip.
8
+ *
9
+ * The grid itself is a memo component keyed on the result rows: typing in the
10
+ * editor patches the store on every keystroke, and rebuilding a few thousand cells
11
+ * for a keystroke that cannot change them is the difference between a usable
12
+ * console and a laggy one.
13
+ */
14
+
15
+ /** MySQL protocol type codes that carry a number, for right alignment. */
16
+ const DW_NUMERIC_TYPES = new Set([0, 1, 2, 3, 4, 5, 8, 9, 13, 246])
17
+
18
+ /** MySQL column-type codes the driver reports as numbers. */
19
+ const DW_COLUMN_TYPES = {
20
+ 0: 'DECIMAL', 1: 'TINYINT', 2: 'SMALLINT', 3: 'INT', 4: 'FLOAT', 5: 'DOUBLE', 6: 'NULL', 7: 'TIMESTAMP',
21
+ 8: 'BIGINT', 9: 'MEDIUMINT', 10: 'DATE', 11: 'TIME', 12: 'DATETIME', 13: 'YEAR', 15: 'VARCHAR',
22
+ 16: 'BIT', 245: 'JSON', 246: 'DECIMAL', 247: 'ENUM', 248: 'SET', 249: 'TINYBLOB', 250: 'MEDIUMBLOB',
23
+ 251: 'LONGBLOB', 252: 'BLOB', 253: 'VAR_STRING', 254: 'CHAR', 255: 'GEOMETRY',
24
+ }
25
+
26
+ function dwColumnTypeName(type, charset) {
27
+ const code = Number(type)
28
+ if (!Number.isFinite(code)) return String(type)
29
+ // 249-252 covers TINY/MEDIUM/LONG/BLOB; a non-binary character set means TEXT.
30
+ if (code >= 249 && code <= 252 && Number(charset) !== 63) {
31
+ return { 249: 'TINYTEXT', 250: 'MEDIUMTEXT', 251: 'LONGTEXT', 252: 'TEXT' }[code]
32
+ }
33
+ return DW_COLUMN_TYPES[code] ?? String(type)
34
+ }
35
+
36
+ function dwIsNumericColumn(column) {
37
+ const code = Number(column?.type)
38
+ return Number.isFinite(code) && DW_NUMERIC_TYPES.has(code)
39
+ }
40
+
41
+ /** Render one cell. */
42
+ function dwCell(value) {
43
+ if (value === null || value === undefined) {
44
+ return h('span', { className: 'dw-cell-null' }, 'NULL')
45
+ }
46
+ if (typeof value === 'string') {
47
+ if (value === '') return h('span', { style: { opacity: .45 } }, "''")
48
+ return value
49
+ }
50
+ if (typeof value === 'boolean') return value ? '1' : '0'
51
+ if (Array.isArray(value)) return value.map((item) => (item === null ? 'NULL' : String(item))).join(', ')
52
+ return String(value)
53
+ }
54
+
55
+
56
+ /** The grid, memoized on the rows it is given. */
57
+ const DwGrid = React.memo(function DwGrid({
58
+ columns, rows, tr, edits, editing, selected, skipBlur, onSelect, onCellDoubleClick, onEditCommit, onEditCancel, onCellMenu,
59
+ }) {
60
+ const head = h('thead', { key: 'head' }, h('tr', { key: 'row' }, [
61
+ h('th', { key: '#', className: 'dw-rownum' }, '#'),
62
+ ...columns.map((column, index) => h('th', { key: `${column.name}-${index}`, title: `${column.name}${column.type !== '' ? ` · ${dwColumnTypeName(column.type, column.charset)}` : ''}` }, [
63
+ h('span', { key: 'n' }, column.name),
64
+ column.type !== '' ? h('span', { key: 't', className: 'dw-th-type' }, dwColumnTypeName(column.type, column.charset)) : null,
65
+ ])),
66
+ ]))
67
+ const body = h('tbody', { key: 'body' }, rows.map((row, rowIndex) => h('tr', { key: rowIndex }, [
68
+ h('td', { key: '#', className: 'dw-rownum' }, String(rowIndex + 1)),
69
+ ...row.map((cell, cellIndex) => {
70
+ const column = columns[cellIndex] ?? { name: String(cellIndex) }
71
+ const rowEdits = edits === undefined ? undefined : edits[rowIndex]
72
+ const edited = rowEdits !== undefined && Object.prototype.hasOwnProperty.call(rowEdits, column.name)
73
+ const value = edited ? rowEdits[column.name] : cell
74
+ const isEditing = editing !== null && editing.row === rowIndex && editing.index === cellIndex
75
+ const isSelected = selected !== null && selected.row === rowIndex && selected.index === cellIndex
76
+ const props = {
77
+ key: cellIndex,
78
+ className: [edited ? 'dw-cell-edited' : '', isSelected ? 'dw-cell-selected' : ''].filter(Boolean).join(' ') || undefined,
79
+ style: dwIsNumericColumn(column) ? { textAlign: 'right', fontVariantNumeric: 'tabular-nums' } : undefined,
80
+ onClick: () => onSelect(rowIndex, cellIndex),
81
+ onDoubleClick: () => onCellDoubleClick(rowIndex, cellIndex),
82
+ onContextMenu: (event) => {
83
+ event.preventDefault()
84
+ onCellMenu(event, rowIndex, cellIndex)
85
+ },
86
+ }
87
+ if (isEditing) {
88
+ props.contentEditable = true
89
+ props.suppressContentEditableWarning = true
90
+ props['data-dw-edit'] = '1'
91
+ props.title = tr('editingCellHint')
92
+ props.onBlur = (event) => onEditCommit(rowIndex, cellIndex, column.name, event.currentTarget.textContent)
93
+ props.onKeyDown = (event) => {
94
+ if (event.key === 'Enter') {
95
+ event.preventDefault()
96
+ event.currentTarget.blur()
97
+ return
98
+ }
99
+ if (event.key !== 'Escape') return
100
+ event.preventDefault()
101
+ skipBlur.current = true
102
+ event.currentTarget.blur()
103
+ }
104
+ return h('td', props, value === null || value === undefined ? 'NULL' : String(value))
105
+ }
106
+ props.title = value === null || value === undefined ? 'NULL' : String(value).slice(0, 400)
107
+ return h('td', props, h('span', { key: 'v', className: 'dw-cell-text' }, dwCell(value)))
108
+ }),
109
+ ])))
110
+ void tr
111
+ return h('div', { className: 'dw-result-body' }, h('table', { className: 'dw-grid' }, [head, body]))
112
+ })
113
+
114
+ function DwResults({ store, state, tab, tr }) {
115
+ const result = tab.result
116
+ const running = tab.running === true
117
+ const [editing, setEditing] = useState(null)
118
+ const [cellMenu, setCellMenu] = useState(null)
119
+ const [exportMenu, setExportMenu] = useState(null)
120
+ const [selected, setSelected] = useState(null)
121
+ const [detail, setDetail] = useState(null)
122
+ const [notice, setNotice] = useState(null)
123
+ const skipBlur = useRef(false)
124
+ const edits = tab.edits ?? {}
125
+ const editCount = Object.values(edits).reduce((total, row) => total + Object.keys(row).length, 0)
126
+ // Export what is on screen, edits included — it is the view the user is acting on.
127
+ const exportRows = (result?.rows ?? []).map((row, rowIndex) => {
128
+ const rowEdits = edits[rowIndex]
129
+ if (rowEdits === undefined) return row
130
+ return row.map((cell, cellIndex) => {
131
+ const name = (result.columns[cellIndex] ?? {}).name
132
+ return Object.prototype.hasOwnProperty.call(rowEdits, name) ? rowEdits[name] : cell
133
+ })
134
+ })
135
+ const exportNumeric = (result?.columns ?? []).map((column) => dwIsNumericColumn(column))
136
+
137
+ // A new result (or a new page) invalidates both the edit cursor and the menu.
138
+ useEffect(() => {
139
+ setEditing(null)
140
+ setCellMenu(null)
141
+ setSelected(null)
142
+ setDetail(null)
143
+ setNotice(null)
144
+ }, [tab.id, result, tab.page?.offset])
145
+
146
+ // Focus the cell that just entered edit mode and select its text, so typing
147
+ // replaces the value instead of appending to it.
148
+ useEffect(() => {
149
+ if (editing === null) return
150
+ const doc = globalThis.document
151
+ if (doc === undefined || typeof doc.querySelector !== 'function') return
152
+ const node = doc.querySelector('[data-dw-edit="1"]')
153
+ if (node === null || node === undefined) return
154
+ node.focus()
155
+ const selection = typeof globalThis.getSelection === 'function' ? globalThis.getSelection() : null
156
+ if (selection === null || selection === undefined) return
157
+ const range = doc.createRange()
158
+ range.selectNodeContents(node)
159
+ selection.removeAllRanges()
160
+ selection.addRange(range)
161
+ }, [editing])
162
+
163
+ const columnIndexOf = (name) => (result?.columns ?? []).findIndex((column) => column.name === name)
164
+
165
+ // Full value of one cell, edits included: what the detail dialog shows.
166
+ const openDetail = (rowIndex, cellIndex) => {
167
+ const column = (result?.columns ?? [])[cellIndex] ?? {}
168
+ const rowEdits = edits[rowIndex] ?? {}
169
+ const raw = Object.prototype.hasOwnProperty.call(rowEdits, column.name)
170
+ ? rowEdits[column.name]
171
+ : (result?.rows ?? [])[rowIndex]?.[cellIndex]
172
+ setDetail({
173
+ row: rowIndex,
174
+ index: cellIndex,
175
+ name: column.name ?? '',
176
+ text: raw === null || raw === undefined ? 'NULL' : String(raw),
177
+ })
178
+ }
179
+
180
+ // One UPDATE for one edited row: edited columns in SET, the original primary
181
+ // key in WHERE. `reason` is what the UI shows when no statement can be built.
182
+ const rowUpdate = (rowIndex) => {
183
+ if (result === undefined) return { reason: tr('updateNeedsEdits') }
184
+ const target = dwSqlTargetTable(tab.lastStatement ?? tab.sql ?? '')
185
+ if (target === undefined) return { reason: tr('updateNeedsSingleTable') }
186
+ const primaryKeys = (result.columns ?? []).filter((column) => dwIsPrimaryKey(column))
187
+ if (primaryKeys.length === 0) return { reason: tr('updateNeedsPrimaryKey') }
188
+ const rowEdits = edits[rowIndex] ?? {}
189
+ const assignments = Object.keys(rowEdits).map((name) => ({
190
+ column: name,
191
+ value: rowEdits[name],
192
+ numeric: dwIsNumericColumn((result.columns ?? [])[columnIndexOf(name)]),
193
+ }))
194
+ if (assignments.length === 0) return { reason: tr('updateNeedsEdits') }
195
+ const source = (result.rows ?? [])[rowIndex] ?? []
196
+ const conditions = primaryKeys.map((column) => ({
197
+ column: column.name,
198
+ value: source[columnIndexOf(column.name)],
199
+ numeric: dwIsNumericColumn(column),
200
+ }))
201
+ return { sql: dwSqlUpdate(target, assignments, conditions), table: target.table, reason: '' }
202
+ }
203
+
204
+ const copyText = (text, onDone, onFail) => {
205
+ const write = globalThis.navigator?.clipboard?.writeText?.(text)
206
+ if (write === undefined) {
207
+ store.pushLog('warn', tr('copyUpdateFailed'))
208
+ return
209
+ }
210
+ write.then(onDone).catch(() => store.pushLog('warn', tr('copyUpdateFailed')))
211
+ }
212
+
213
+ const copyUpdate = () => {
214
+ if (cellMenu === null) return
215
+ const built = rowUpdate(cellMenu.row)
216
+ if (built.reason !== '') {
217
+ store.pushLog('warn', built.reason)
218
+ setCellMenu(null)
219
+ return
220
+ }
221
+ copyText(built.sql, () => store.pushLog('info', tr('log.copiedUpdate', { table: built.table })))
222
+ setCellMenu(null)
223
+ }
224
+
225
+ // Every edited row at once: one statement per row, in result order.
226
+ const copyAllUpdates = () => {
227
+ const rows = Object.keys(edits).map(Number).sort((left, right) => left - right)
228
+ if (rows.length === 0) return
229
+ const statements = []
230
+ for (const row of rows) {
231
+ const built = rowUpdate(row)
232
+ if (built.reason !== '') {
233
+ store.pushLog('warn', built.reason)
234
+ return
235
+ }
236
+ statements.push(built.sql)
237
+ }
238
+ copyText(statements.join('\n\n'), () => store.pushLog('info', tr('log.copiedUpdates', { count: statements.length })))
239
+ }
240
+
241
+ // Export the displayed rows: CSV, XLSX or an INSERT script. The save step asks
242
+ // for a path through the File System Access API when the host supports it.
243
+ const exportResult = (kind) => {
244
+ setExportMenu(null)
245
+ if (result === undefined) return
246
+ const columns = result.columns ?? []
247
+ const target = dwSqlTargetTable(tab.lastStatement ?? tab.sql ?? '')
248
+ const now = new Date()
249
+ const pad = (value) => String(value).padStart(2, '0')
250
+ const stamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
251
+ const base = `${target?.table ?? tab.database ?? 'result'}-${stamp}`
252
+ let payload
253
+ let name
254
+ let mime
255
+ let extension
256
+ let description
257
+ if (kind === 'sql') {
258
+ if (target === undefined) {
259
+ // Not a silent log line: the user asked for a file, so say why there is none.
260
+ store.pushLog('warn', tr('insertNeedsTable'))
261
+ setNotice({ title: tr('insertNeedsTitle'), message: tr('insertNeedsTable') })
262
+ return
263
+ }
264
+ payload = dwInsertText(target, columns, exportRows, exportNumeric)
265
+ name = `${base}.sql`
266
+ mime = 'text/plain'
267
+ extension = '.sql'
268
+ description = tr('exportInsert')
269
+ } else if (kind === 'csv') {
270
+ payload = dwCsvText(columns, exportRows)
271
+ name = `${base}.csv`
272
+ mime = 'text/csv'
273
+ extension = '.csv'
274
+ description = tr('exportCsv')
275
+ } else {
276
+ payload = dwXlsxBytes(columns, exportRows, exportNumeric)
277
+ name = `${base}.xlsx`
278
+ mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
279
+ extension = '.xlsx'
280
+ description = tr('exportXlsx')
281
+ }
282
+ dwSaveFile({ name, data: payload, mime, extension, description })
283
+ .then((outcome) => {
284
+ if (outcome.cancelled === true) {
285
+ store.pushLog('info', tr('log.exportCancelled'))
286
+ return
287
+ }
288
+ store.pushLog('info', tr('log.exported', { name: outcome.name, rows: exportRows.length }))
289
+ if (outcome.via === 'download') store.pushLog('warn', tr('log.exportDownloaded', { name: outcome.name }))
290
+ })
291
+ .catch((error) => store.pushLog('warn', tr('exportFailed', { message: error?.message ?? String(error) })))
292
+ }
293
+
294
+ const bar = h('div', { key: 'bar', className: 'dw-result-bar' }, [
295
+ result === undefined
296
+ ? h('span', { key: 'idle' }, tr('resultIdle'))
297
+ : h('span', { key: 'meta' }, [
298
+ h('b', { key: 'r', style: { color: 'var(--dsw-alias-state-success-primary)' } }, `${result.rowCount} ${tr('rows')}`),
299
+ h('span', { key: 'd' }, ` · ${dwDuration(result.elapsedMs)}`),
300
+ result.truncated === true ? h('span', { key: 't', className: 'dw-log-warn' }, ` · ${tr('truncated')} ${result.limit}`) : null,
301
+ ]),
302
+ editCount > 0
303
+ ? h('span', { key: 'edits', className: 'dw-edit-badge' }, [
304
+ h('span', { key: 'n' }, tr('editsCount', { count: editCount })),
305
+ h('button', {
306
+ key: 'copy',
307
+ type: 'button',
308
+ className: 'dw-edit-clear',
309
+ title: tr('copyEditsHint'),
310
+ onClick: copyAllUpdates,
311
+ }, tr('copyEdits')),
312
+ h('button', {
313
+ key: 'c',
314
+ type: 'button',
315
+ className: 'dw-edit-clear',
316
+ onClick: () => store.clearCellEdits(tab.id),
317
+ }, tr('clearEdits')),
318
+ ])
319
+ : null,
320
+ tab.page === undefined
321
+ ? null
322
+ : h('span', { key: 'pager', className: 'dw-pager' }, [
323
+ h('button', {
324
+ key: 'prev',
325
+ type: 'button',
326
+ className: 'dw-pager-btn',
327
+ disabled: running || tab.page.offset <= 0,
328
+ title: tr('pagePrev'),
329
+ onClick: () => void store.prevPage(),
330
+ }, `‹ ${tr('pagePrev')}`),
331
+ h('span', { key: 'label', className: 'dw-pager-label' }, tr('pageLabel', {
332
+ page: Math.floor(tab.page.offset / tab.page.size) + 1,
333
+ size: tab.page.size,
334
+ })),
335
+ h('button', {
336
+ key: 'next',
337
+ type: 'button',
338
+ className: 'dw-pager-btn',
339
+ disabled: running || tab.page.hasMore !== true,
340
+ title: tr('pageNext'),
341
+ onClick: () => void store.nextPage(),
342
+ }, `${tr('pageNext')} ›`),
343
+ ]),
344
+ result === undefined
345
+ ? null
346
+ : h('span', { key: 'count', className: 'dw-count' }, [
347
+ h('button', {
348
+ key: 'r',
349
+ type: 'button',
350
+ className: 'dw-count-btn',
351
+ title: tab.countError !== undefined && tab.countError !== '' ? tab.countError : tr('countRowsHint'),
352
+ 'aria-label': tr('countRows'),
353
+ disabled: tab.counting === true
354
+ || tab.lastStatement === undefined
355
+ || !/^(select|with)\b/i.test(String(tab.lastStatement ?? '').trim()),
356
+ onClick: () => void store.refreshTotal(tab.id),
357
+ }, h(DwIcon, { key: 'i', name: tab.counting === true ? 'spinner' : 'refresh', size: 11 })),
358
+ h('span', {
359
+ key: 'v',
360
+ className: 'dw-count-value',
361
+ style: tab.countError !== undefined && tab.countError !== '' ? { color: 'var(--dsw-alias-state-error-primary)' } : undefined,
362
+ }, tab.counting === true
363
+ ? tr('countRowsRunning')
364
+ : tab.countError !== undefined && tab.countError !== ''
365
+ ? tr('countRowsFailed')
366
+ : tab.totalCount === undefined
367
+ ? tr('countRowsUnknown')
368
+ : tr('countRowsValue', { rows: Number(tab.totalCount).toLocaleString() })),
369
+ ]),
370
+ h('span', { key: 'sp', className: 'dw-spacer' }),
371
+ result !== undefined && (result.rows ?? []).length > 0
372
+ ? h('button', {
373
+ key: 'export',
374
+ type: 'button',
375
+ className: 'dw-btn',
376
+ title: tr('exportHint', { rows: exportRows.length }),
377
+ onClick: (event) => {
378
+ const rect = event.currentTarget.getBoundingClientRect()
379
+ setExportMenu({ x: Math.max(8, rect.right - 200), y: rect.bottom + 4 })
380
+ },
381
+ }, [h(DwIcon, { key: 'i', name: 'download', size: 11 }), h('span', { key: 't' }, tr('export'))])
382
+ : null,
383
+ h('label', { key: 'limit', style: { display: 'flex', alignItems: 'center', gap: 4 } }, [
384
+ h('span', { key: 'l' }, tr('limit')),
385
+ h('select', {
386
+ key: 's',
387
+ className: 'dw-select',
388
+ value: String(state.limit),
389
+ onChange: (event) => store.setLimit(Number(event.target.value)),
390
+ style: { height: 22, fontSize: 11 },
391
+ }, [50, 100, 200, 500, 1000, 2000].map((option) => h('option', { key: option, value: String(option) }, String(option)))),
392
+ ]),
393
+ ])
394
+
395
+ let body = null
396
+ if (tab.error !== '' && tab.error !== undefined) {
397
+ body = h('div', { key: 'body', className: 'dw-result-body', style: { padding: '10px 12px' } }, [
398
+ h('div', { key: 't', className: 'dw-empty-title', style: { color: 'var(--dsw-alias-state-error-primary)', fontSize: 12 } }, tr('queryFailed')),
399
+ h('div', { key: 'm', className: 'dw-error-text' }, tab.error),
400
+ ])
401
+ } else if (running === true) {
402
+ body = h('div', { key: 'body', className: 'dw-empty' }, [
403
+ h(DwIcon, { key: 'i', name: 'spinner', size: 18 }),
404
+ h('div', { key: 't', className: 'dw-empty-hint' }, tr('running')),
405
+ ])
406
+ } else if (result === undefined) {
407
+ body = h('div', { key: 'body', className: 'dw-empty' }, [
408
+ h('div', { key: 't', className: 'dw-empty-hint' }, tr('resultHint')),
409
+ ])
410
+ } else if ((result.rows ?? []).length === 0) {
411
+ body = h('div', { key: 'body', className: 'dw-empty' }, [
412
+ h('div', { key: 't', className: 'dw-empty-hint' }, tr('noRows')),
413
+ h('div', { key: 'q', className: 'dw-code-block' }, result.sql ?? ''),
414
+ ])
415
+ } else {
416
+ body = h(DwGrid, {
417
+ key: 'grid',
418
+ columns: result.columns,
419
+ rows: result.rows,
420
+ tr,
421
+ edits: tab.edits,
422
+ editing,
423
+ selected,
424
+ skipBlur,
425
+ onSelect: (row, index) => setSelected({ row, index }),
426
+ onCellDoubleClick: (row, index) => openDetail(row, index),
427
+ onEditCommit: (row, index, name, text) => {
428
+ if (skipBlur.current) {
429
+ skipBlur.current = false
430
+ setEditing(null)
431
+ return
432
+ }
433
+ store.setCellEdit(tab.id, row, index, name, text === 'NULL' ? null : text)
434
+ setEditing(null)
435
+ },
436
+ onEditCancel: () => setEditing(null),
437
+ onCellMenu: (event, row, index) => setCellMenu({ x: event.clientX, y: event.clientY, row, index }),
438
+ })
439
+ }
440
+
441
+ const cellMenuLayer = cellMenu === null ? null : h(React.Fragment, null, [
442
+ h('div', {
443
+ key: 'backdrop',
444
+ className: 'dw-backdrop',
445
+ onClick: () => setCellMenu(null),
446
+ onContextMenu: (event) => {
447
+ event.preventDefault()
448
+ setCellMenu(null)
449
+ },
450
+ }),
451
+ h('div', { key: 'menu', className: 'dw-menu', style: { left: cellMenu.x, top: cellMenu.y } }, [
452
+ h('button', {
453
+ key: 'edit',
454
+ type: 'button',
455
+ className: 'dw-menu-item',
456
+ onClick: () => {
457
+ setSelected({ row: cellMenu.row, index: cellMenu.index })
458
+ setEditing({ row: cellMenu.row, index: cellMenu.index })
459
+ setCellMenu(null)
460
+ },
461
+ }, tr('editCell')),
462
+ h('button', {
463
+ key: 'update',
464
+ type: 'button',
465
+ className: 'dw-menu-item',
466
+ disabled: rowUpdate(cellMenu.row).reason !== '',
467
+ title: rowUpdate(cellMenu.row).reason === '' ? tr('menuCopyUpdateHint') : rowUpdate(cellMenu.row).reason,
468
+ onClick: copyUpdate,
469
+ }, tr('menuCopyUpdate')),
470
+ h('button', {
471
+ key: 'value',
472
+ type: 'button',
473
+ className: 'dw-menu-item',
474
+ onClick: () => {
475
+ const row = (result?.rows ?? [])[cellMenu.row] ?? []
476
+ const edited = edits[cellMenu.row] ?? {}
477
+ const name = (result?.columns ?? [])[cellMenu.index]?.name ?? ''
478
+ const value = Object.prototype.hasOwnProperty.call(edited, name) ? edited[name] : row[cellMenu.index]
479
+ const write = globalThis.navigator?.clipboard?.writeText?.(value === null || value === undefined ? 'NULL' : String(value))
480
+ if (write !== undefined) write.then(() => store.pushLog('info', tr('log.copiedCell'))).catch(() => undefined)
481
+ setCellMenu(null)
482
+ },
483
+ }, tr('menuCopyCell')),
484
+ ]),
485
+ ])
486
+
487
+ const exportLayer = exportMenu === null ? null : h(React.Fragment, null, [
488
+ h('div', {
489
+ key: 'backdrop',
490
+ className: 'dw-backdrop',
491
+ onClick: () => setExportMenu(null),
492
+ onContextMenu: (event) => {
493
+ event.preventDefault()
494
+ setExportMenu(null)
495
+ },
496
+ }),
497
+ h('div', { key: 'menu', className: 'dw-menu', style: { left: exportMenu.x, top: exportMenu.y } }, [
498
+ h('div', { key: 'note', className: 'dw-menu-note' }, tr('exportHint', { rows: exportRows.length })),
499
+ h('button', { key: 'xlsx', type: 'button', className: 'dw-menu-item', onClick: () => exportResult('xlsx') }, tr('exportXlsx')),
500
+ h('button', { key: 'csv', type: 'button', className: 'dw-menu-item', onClick: () => exportResult('csv') }, tr('exportCsv')),
501
+ h('button', { key: 'sql', type: 'button', className: 'dw-menu-item', onClick: () => exportResult('sql') }, tr('exportInsert')),
502
+ ]),
503
+ ])
504
+
505
+ const detailLayer = detail === null ? null : h('div', {
506
+ key: 'detail',
507
+ className: 'dw-modal',
508
+ onClick: () => setDetail(null),
509
+ }, h('div', {
510
+ key: 'card',
511
+ className: 'dw-modal-card dw-cell-dialog',
512
+ onClick: (event) => event.stopPropagation(),
513
+ }, [
514
+ h('div', { key: 'title', className: 'dw-modal-title' }, tr('cellDetailTitle', { column: detail.name, row: detail.row + 1 })),
515
+ h('div', { key: 'body', className: 'dw-modal-body' }, h('pre', { key: 'v', className: 'dw-cell-view' }, detail.text)),
516
+ h('div', { key: 'foot', className: 'dw-modal-foot' }, [
517
+ h('button', {
518
+ key: 'copy',
519
+ type: 'button',
520
+ className: 'dw-btn',
521
+ onClick: () => copyText(detail.text, () => store.pushLog('info', tr('log.copiedCell'))),
522
+ }, tr('menuCopyCell')),
523
+ h('button', {
524
+ key: 'edit',
525
+ type: 'button',
526
+ className: 'dw-btn',
527
+ onClick: () => {
528
+ setSelected({ row: detail.row, index: detail.index })
529
+ setEditing({ row: detail.row, index: detail.index })
530
+ setDetail(null)
531
+ },
532
+ }, tr('editCell')),
533
+ h('button', { key: 'close', type: 'button', className: 'dw-btn dw-btn-accent', onClick: () => setDetail(null) }, tr('close')),
534
+ ]),
535
+ ]))
536
+
537
+ const noticeLayer = notice === null ? null : h('div', {
538
+ key: 'notice',
539
+ className: 'dw-modal',
540
+ onClick: () => setNotice(null),
541
+ }, h('div', {
542
+ key: 'card',
543
+ className: 'dw-modal-card',
544
+ onClick: (event) => event.stopPropagation(),
545
+ }, [
546
+ h('div', { key: 'title', className: 'dw-modal-title' }, notice.title),
547
+ h('div', { key: 'body', className: 'dw-modal-body' }, h('div', { key: 'm', className: 'dw-error-text' }, notice.message)),
548
+ h('div', { key: 'foot', className: 'dw-modal-foot' }, [
549
+ h('button', { key: 'close', type: 'button', className: 'dw-btn dw-btn-accent', onClick: () => setNotice(null) }, tr('close')),
550
+ ]),
551
+ ]))
552
+
553
+ return h('div', { className: 'dw-result' }, [bar, body, cellMenuLayer, exportLayer, detailLayer, noticeLayer])
554
+ }
555
+
556
+ export { DwResults, DwGrid, dwCell, dwColumnTypeName, dwIsNumericColumn }