@dickpy/dsh-imagegen 1.0.7 → 1.0.9

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.
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Prompt-template library overlay: a searchable, category-filtered gallery of
3
+ * the bundled awesome-gpt-image-2 cases. The case list is served by the host
4
+ * (bundled snapshot, optionally refreshed online); reference images load
5
+ * lazily through the host's caching proxy, so browsing progressively mirrors
6
+ * the gallery onto the local disk. Picking a template hands its prompt back
7
+ * to the studio form.
8
+ */
9
+
10
+ import { useEffect, useMemo, useRef, useState } from 'react'
11
+ import { createPortal } from 'react-dom'
12
+ import { Button } from '@deepseek-ai/dsh-client-ui-primitives'
13
+ import type { ImageGenApi } from './api.ts'
14
+ import { errorMessage, tt } from './helpers.ts'
15
+ import { TEMPLATES_API, type TemplateCase, type TemplateListResult } from '../protocol.ts'
16
+ import css from './templates.module.css'
17
+
18
+ /** Concurrent image downloads while caching the whole gallery offline. */
19
+ const CACHE_ALL_CONCURRENCY = 4
20
+
21
+ /** Same-origin URL of one case's reference image (host caching proxy). */
22
+ function imageUrlOf(item: TemplateCase): string {
23
+ return `${TEMPLATES_API.image}/${encodeURIComponent(item.image)}`
24
+ }
25
+
26
+ /** A card thumbnail that falls back to a placeholder when the proxy 404s. */
27
+ function TemplateThumb(props: { item: TemplateCase }) {
28
+ const [failed, setFailed] = useState(false)
29
+ if (props.item.image === '' || failed) {
30
+ return (
31
+ <span className={css.thumbPlaceholder} aria-hidden="true">
32
+ <svg viewBox="0 0 24 24" width="26" height="26" fill="none" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="18" height="18" rx="3"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
33
+ </span>
34
+ )
35
+ }
36
+ return (
37
+ <img
38
+ className={css.thumb}
39
+ src={imageUrlOf(props.item)}
40
+ alt={props.item.title}
41
+ loading="lazy"
42
+ onError={() => { setFailed(true) }}
43
+ />
44
+ )
45
+ }
46
+
47
+ /** The template-library modal. Rendered through a portal above the studio. */
48
+ export function TemplateLibrary(props: {
49
+ api: ImageGenApi
50
+ /** Hand a picked prompt back to the studio form and close the library. */
51
+ onUse: (prompt: string) => void
52
+ onClose: () => void
53
+ }) {
54
+ const { api, onUse, onClose } = props
55
+ const [list, setList] = useState<TemplateListResult | null>(null)
56
+ const [loadError, setLoadError] = useState<string | null>(null)
57
+ const [query, setQuery] = useState('')
58
+ const [category, setCategory] = useState('')
59
+ const [selected, setSelected] = useState<TemplateCase | null>(null)
60
+ const [copied, setCopied] = useState(false)
61
+ const [refreshing, setRefreshing] = useState(false)
62
+ const [notice, setNotice] = useState<string | null>(null)
63
+ const [cacheAll, setCacheAll] = useState<{ running: boolean; done: number; total: number }>({ running: false, done: 0, total: 0 })
64
+ const searchRef = useRef<HTMLInputElement>(null)
65
+ const load = (): void => {
66
+ api.templatesList()
67
+ .then(result => { setList(result); setLoadError(null) })
68
+ .catch(caught => { setLoadError(errorMessage(caught)) })
69
+ }
70
+
71
+ // Load once on open; focus the search box for immediate typing.
72
+ useEffect(() => {
73
+ load()
74
+ searchRef.current?.focus()
75
+ // eslint-disable-next-line react-hooks/exhaustive-deps
76
+ }, [])
77
+
78
+ const categories = useMemo(() => {
79
+ if (list === null) return [] as Array<{ key: string; label: string; count: number }>
80
+ const counts = new Map<string, { label: string; count: number }>()
81
+ for (const item of list.cases) {
82
+ const entry = counts.get(item.category) ?? { label: item.categoryZh || item.category, count: 0 }
83
+ entry.count += 1
84
+ counts.set(item.category, entry)
85
+ }
86
+ return [...counts.entries()].map(([key, value]) => ({ key, label: value.label, count: value.count }))
87
+ }, [list])
88
+
89
+ const filtered = useMemo(() => {
90
+ if (list === null) return [] as TemplateCase[]
91
+ const needle = query.trim().toLowerCase()
92
+ return list.cases.filter(item => {
93
+ if (category !== '' && item.category !== category) return false
94
+ if (needle === '') return true
95
+ return item.title.toLowerCase().includes(needle)
96
+ || item.prompt.toLowerCase().includes(needle)
97
+ || item.sourceLabel.toLowerCase().includes(needle)
98
+ })
99
+ }, [list, query, category])
100
+
101
+ // Escape backs out of the detail view first, then closes the modal.
102
+ useEffect(() => {
103
+ const onKey = (event: KeyboardEvent): void => {
104
+ if (event.key !== 'Escape') return
105
+ event.stopPropagation()
106
+ if (selected !== null) setSelected(null)
107
+ else onClose()
108
+ }
109
+ window.addEventListener('keydown', onKey, true)
110
+ return () => window.removeEventListener('keydown', onKey, true)
111
+ }, [selected, onClose])
112
+
113
+ const refresh = async (): Promise<void> => {
114
+ if (refreshing) return
115
+ setRefreshing(true)
116
+ setNotice(null)
117
+ try {
118
+ const result = await api.templatesRefresh()
119
+ const reloaded = await api.templatesList()
120
+ setList(reloaded)
121
+ setLoadError(null)
122
+ setNotice(tt('templates.refreshed', { count: result.total }))
123
+ } catch (caught) {
124
+ setNotice(tt('templates.refreshFailed', { error: errorMessage(caught) }))
125
+ } finally {
126
+ setRefreshing(false)
127
+ }
128
+ }
129
+
130
+ /** Mirror every reference image through the host cache (offline browsing). */
131
+ const cacheAllImages = async (): Promise<void> => {
132
+ if (cacheAll.running || list === null) return
133
+ const files = [...new Set(list.cases.map(item => item.image).filter(name => name !== ''))]
134
+ setCacheAll({ running: true, done: 0, total: files.length })
135
+ let index = 0
136
+ const worker = async (): Promise<void> => {
137
+ while (index < files.length) {
138
+ const file = files[index]!
139
+ index += 1
140
+ try {
141
+ await fetch(`${TEMPLATES_API.image}/${encodeURIComponent(file)}`)
142
+ } catch { /* individual failures are retried on the next run */ }
143
+ setCacheAll(current => ({ ...current, done: current.done + 1 }))
144
+ }
145
+ }
146
+ await Promise.all(Array.from({ length: CACHE_ALL_CONCURRENCY }, () => worker()))
147
+ setCacheAll({ running: false, done: files.length, total: files.length })
148
+ }
149
+
150
+ const copyPrompt = async (text: string): Promise<void> => {
151
+ try {
152
+ if (navigator.clipboard?.writeText !== undefined) {
153
+ await navigator.clipboard.writeText(text)
154
+ } else {
155
+ const textarea = document.createElement('textarea')
156
+ textarea.value = text
157
+ textarea.style.position = 'fixed'
158
+ textarea.style.opacity = '0'
159
+ document.body.appendChild(textarea)
160
+ textarea.select()
161
+ const copiedOk = document.execCommand('copy')
162
+ textarea.remove()
163
+ if (!copiedOk) throw new Error('copy failed')
164
+ }
165
+ setCopied(true)
166
+ window.setTimeout(() => { setCopied(false) }, 1800)
167
+ } catch {
168
+ setCopied(false)
169
+ }
170
+ }
171
+
172
+ const originLabel = list === null ? '' : tt(list.origin === 'refreshed' ? 'templates.origin.refreshed' : 'templates.origin.bundled')
173
+
174
+ return createPortal(
175
+ <div className={css.overlay} role="dialog" aria-modal="true" aria-label={tt('templates.title')} onClick={onClose}>
176
+ <section className={css.shell} onClick={(event) => { event.stopPropagation() }}>
177
+ <header className={css.header}>
178
+ <span className={css.heading}>
179
+ <h3 className={css.title}>{tt('templates.title')}</h3>
180
+ {list !== null ? (
181
+ <span className={css.meta}>{tt('templates.meta', { count: list.total, origin: originLabel })}</span>
182
+ ) : null}
183
+ </span>
184
+ <span className={css.headerActions}>
185
+ <Button variant="outline" size="sm" disabled={refreshing || cacheAll.running} onClick={() => { void refresh() }}>
186
+ {refreshing ? tt('templates.refreshing') : tt('templates.refresh')}
187
+ </Button>
188
+ <Button
189
+ variant="outline"
190
+ size="sm"
191
+ disabled={list === null || cacheAll.running}
192
+ title={tt('templates.cacheAllHint')}
193
+ onClick={() => { void cacheAllImages() }}
194
+ >
195
+ {cacheAll.running
196
+ ? tt('templates.caching', { done: cacheAll.done, total: cacheAll.total })
197
+ : cacheAll.total > 0 && cacheAll.done === cacheAll.total
198
+ ? tt('templates.cached')
199
+ : tt('templates.cacheAll')}
200
+ </Button>
201
+ <button type="button" className={css.close} aria-label={tt('templates.close')} title={tt('templates.close')} onClick={onClose}>
202
+ <svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" aria-hidden="true"><path d="M4 4l8 8M12 4l-8 8"/></svg>
203
+ </button>
204
+ </span>
205
+ </header>
206
+
207
+ <div className={css.toolbar}>
208
+ <input
209
+ ref={searchRef}
210
+ type="search"
211
+ className={css.search}
212
+ placeholder={tt('templates.search')}
213
+ value={query}
214
+ onChange={(event) => { setQuery(event.target.value) }}
215
+ />
216
+ <div className={css.categoryRow}>
217
+ <button
218
+ type="button"
219
+ className={css.categoryPill}
220
+ data-active={category === '' ? '' : undefined}
221
+ onClick={() => { setCategory('') }}
222
+ >
223
+ {tt('templates.all')}{list !== null ? ` ${list.total}` : ''}
224
+ </button>
225
+ {categories.map(entry => (
226
+ <button
227
+ key={entry.key}
228
+ type="button"
229
+ className={css.categoryPill}
230
+ data-active={category === entry.key ? '' : undefined}
231
+ onClick={() => { setCategory(entry.key) }}
232
+ >
233
+ {entry.label} {entry.count}
234
+ </button>
235
+ ))}
236
+ </div>
237
+ </div>
238
+
239
+ {notice !== null ? <div className={css.notice} role="status">{notice}</div> : null}
240
+
241
+ <div className={css.body}>
242
+ {list === null && loadError === null ? (
243
+ <div className={css.state} role="status">
244
+ <span className={css.spinner} />
245
+ <span>{tt('templates.loading')}</span>
246
+ </div>
247
+ ) : null}
248
+
249
+ {loadError !== null ? (
250
+ <div className={css.state} role="alert">
251
+ <span>{tt('templates.loadFailed', { error: loadError })}</span>
252
+ <Button variant="outline" size="sm" onClick={() => { setLoadError(null); setList(null); load() }}>
253
+ {tt('templates.retry')}
254
+ </Button>
255
+ </div>
256
+ ) : null}
257
+
258
+ {list !== null && filtered.length === 0 ? (
259
+ <div className={css.state}>{tt('templates.empty')}</div>
260
+ ) : null}
261
+
262
+ {list !== null && filtered.length > 0 ? (
263
+ <div className={css.grid}>
264
+ {filtered.map(item => (
265
+ <button
266
+ key={item.id}
267
+ type="button"
268
+ className={css.card}
269
+ onClick={() => { setSelected(item); setCopied(false) }}
270
+ >
271
+ <span className={css.thumbWrap}>
272
+ <TemplateThumb item={item} />
273
+ {item.featured ? <span className={css.featuredBadge}>{tt('templates.featured')}</span> : null}
274
+ </span>
275
+ <span className={css.cardBody}>
276
+ <span className={css.cardTitle}>{item.title}</span>
277
+ <span className={css.cardMeta}>
278
+ <span className={css.cardCategory}>{item.categoryZh || item.category}</span>
279
+ {item.sourceLabel !== '' ? <span className={css.cardSource}>{item.sourceLabel}</span> : null}
280
+ </span>
281
+ </span>
282
+ </button>
283
+ ))}
284
+ </div>
285
+ ) : null}
286
+ </div>
287
+
288
+ <footer className={css.footer}>
289
+ <span className={css.attribution}>{tt('templates.attribution')}</span>
290
+ <a className={css.sourceLink} href="https://vibeui.top/" target="_blank" rel="noreferrer">
291
+ {tt('templates.source')}
292
+ </a>
293
+ </footer>
294
+ </section>
295
+
296
+ {selected !== null ? (
297
+ <div className={css.detailOverlay} onClick={() => { setSelected(null) }}>
298
+ <section className={css.detail} onClick={(event) => { event.stopPropagation() }}>
299
+ <div className={css.detailMedia}>
300
+ {selected.image !== '' ? (
301
+ <img className={css.detailImage} src={imageUrlOf(selected)} alt={selected.title} />
302
+ ) : (
303
+ <span className={css.thumbPlaceholder} aria-hidden="true" />
304
+ )}
305
+ </div>
306
+ <div className={css.detailInfo}>
307
+ <h4 className={css.detailTitle}>{selected.title}</h4>
308
+ <div className={css.detailMeta}>
309
+ <span className={css.cardCategory}>{selected.categoryZh || selected.category}</span>
310
+ {selected.sourceUrl !== '' ? (
311
+ <a className={css.detailLink} href={selected.sourceUrl} target="_blank" rel="noreferrer">{selected.sourceLabel || selected.sourceUrl}</a>
312
+ ) : null}
313
+ {selected.githubUrl !== '' ? (
314
+ <a className={css.detailLink} href={selected.githubUrl} target="_blank" rel="noreferrer">GitHub</a>
315
+ ) : null}
316
+ </div>
317
+ <pre className={css.detailPrompt}>{selected.prompt}</pre>
318
+ <div className={css.detailActions}>
319
+ <Button variant="primary" size="md" onClick={() => { onUse(selected.prompt) }}>
320
+ {tt('templates.use')}
321
+ </Button>
322
+ <Button variant="outline" size="md" onClick={() => { void copyPrompt(selected.prompt) }}>
323
+ {copied ? tt('templates.copied') : tt('templates.copy')}
324
+ </Button>
325
+ <Button variant="outline" size="md" onClick={() => { setSelected(null) }}>
326
+ {tt('templates.back')}
327
+ </Button>
328
+ </div>
329
+ </div>
330
+ </section>
331
+ </div>
332
+ ) : null}
333
+ </div>,
334
+ document.body,
335
+ )
336
+ }
package/src/client/api.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * data access path the panel uses — plain fetch, same origin.
4
4
  */
5
5
 
6
- import { GENERATE_API, HISTORY_API, UPDATE_API, type GenerateRequest, type GenerateResult, type HistoryEntry, type UpdateInfo } from '../protocol.ts'
6
+ import { GENERATE_API, HISTORY_API, TEMPLATES_API, UPDATE_API, type GenerateRequest, type GenerateResult, type HistoryEntry, type TemplateListResult, type TemplateRefreshResult, type UpdateInfo } from '../protocol.ts'
7
7
 
8
8
  /** Error carrying the route's JSON error message. */
9
9
  export class ImageGenApiError extends Error {
@@ -97,4 +97,24 @@ export class ImageGenApi {
97
97
  const body = await readEnvelope<{ ok: true; entries: HistoryEntry[] }>(response)
98
98
  return body.entries
99
99
  }
100
+
101
+ /** Fetch the prompt-template library (bundled snapshot or refreshed copy). */
102
+ async templatesList(): Promise<TemplateListResult> {
103
+ const response = await fetch(TEMPLATES_API.list, { method: 'POST' })
104
+ const body = await readEnvelope<TemplateListResult & { ok: true }>(response)
105
+ return {
106
+ cases: body.cases,
107
+ total: body.total,
108
+ origin: body.origin,
109
+ repository: body.repository,
110
+ fetchedAt: body.fetchedAt,
111
+ }
112
+ }
113
+
114
+ /** Re-download the template library from the upstream mirror (host-side). */
115
+ async templatesRefresh(): Promise<TemplateRefreshResult> {
116
+ const response = await fetch(TEMPLATES_API.refresh, { method: 'POST' })
117
+ const body = await readEnvelope<TemplateRefreshResult & { ok: true }>(response)
118
+ return { total: body.total, fetchedAt: body.fetchedAt }
119
+ }
100
120
  }
@@ -122,6 +122,34 @@ export const zh = {
122
122
  'settings.overridden': '已覆盖',
123
123
  'settings.reset': '重置',
124
124
  'settings.invalidNumber': '请输入有效数字',
125
+ // template library
126
+ 'templates.open': '模板库',
127
+ 'templates.title': '提示词模板库',
128
+ 'templates.meta': '共 {count} 个模板 · {origin}',
129
+ 'templates.origin.bundled': '内置快照',
130
+ 'templates.origin.refreshed': '在线刷新',
131
+ 'templates.search': '搜索模板标题或提示词…',
132
+ 'templates.all': '全部',
133
+ 'templates.close': '关闭',
134
+ 'templates.back': '返回列表',
135
+ 'templates.use': '使用此提示词',
136
+ 'templates.copy': '复制提示词',
137
+ 'templates.copied': '已复制',
138
+ 'templates.refresh': '刷新模板库',
139
+ 'templates.refreshing': '刷新中…',
140
+ 'templates.refreshed': '已刷新,共 {count} 个模板',
141
+ 'templates.refreshFailed': '刷新失败:{error}',
142
+ 'templates.cacheAll': '缓存全部图片',
143
+ 'templates.cacheAllHint': '通过本机代理把全部参考图缓存到本地磁盘,之后离线也能浏览',
144
+ 'templates.caching': '缓存中 {done}/{total}…',
145
+ 'templates.cached': '图片已全部缓存',
146
+ 'templates.empty': '没有匹配的模板',
147
+ 'templates.loading': '正在加载模板库…',
148
+ 'templates.loadFailed': '模板库加载失败:{error}',
149
+ 'templates.retry': '重试',
150
+ 'templates.attribution': '模板与图片来自 awesome-gpt-image-2 项目,作者链接见各模板详情',
151
+ 'templates.source': '来源:vibeui.top',
152
+ 'templates.featured': '精选',
125
153
  } as const
126
154
 
127
155
  export const en: Record<keyof typeof zh, string> = {
@@ -233,6 +261,34 @@ export const en: Record<keyof typeof zh, string> = {
233
261
  'settings.overridden': 'Overridden',
234
262
  'settings.reset': 'Reset',
235
263
  'settings.invalidNumber': 'Enter a valid number',
264
+ // template library
265
+ 'templates.open': 'Templates',
266
+ 'templates.title': 'Prompt Template Library',
267
+ 'templates.meta': '{count} templates · {origin}',
268
+ 'templates.origin.bundled': 'bundled snapshot',
269
+ 'templates.origin.refreshed': 'refreshed online',
270
+ 'templates.search': 'Search template titles or prompts…',
271
+ 'templates.all': 'All',
272
+ 'templates.close': 'Close',
273
+ 'templates.back': 'Back to list',
274
+ 'templates.use': 'Use this prompt',
275
+ 'templates.copy': 'Copy prompt',
276
+ 'templates.copied': 'Copied',
277
+ 'templates.refresh': 'Refresh library',
278
+ 'templates.refreshing': 'Refreshing…',
279
+ 'templates.refreshed': 'Refreshed — {count} templates',
280
+ 'templates.refreshFailed': 'Refresh failed: {error}',
281
+ 'templates.cacheAll': 'Cache all images',
282
+ 'templates.cacheAllHint': 'Mirror every reference image to local disk through the host proxy, for offline browsing',
283
+ 'templates.caching': 'Caching {done}/{total}…',
284
+ 'templates.cached': 'All images cached',
285
+ 'templates.empty': 'No matching templates',
286
+ 'templates.loading': 'Loading the template library…',
287
+ 'templates.loadFailed': 'Failed to load the library: {error}',
288
+ 'templates.retry': 'Retry',
289
+ 'templates.attribution': 'Templates and images come from the awesome-gpt-image-2 project; author links are on each template',
290
+ 'templates.source': 'Source: vibeui.top',
291
+ 'templates.featured': 'Featured',
236
292
  }
237
293
 
238
294
  /** Locale key union. */
@@ -606,10 +606,45 @@ html[data-dsh-imagegen-active]:not([data-dsh-taskboard-active]):not([data-dsh-ss
606
606
 
607
607
  .promptFooter {
608
608
  display: flex;
609
- justify-content: flex-end;
609
+ align-items: center;
610
+ justify-content: space-between;
611
+ gap: 8px;
610
612
  margin-top: -6px;
611
613
  }
612
614
 
615
+ .templatesButton {
616
+ display: inline-flex;
617
+ align-items: center;
618
+ gap: 6px;
619
+ height: 26px;
620
+ padding: 0 12px;
621
+ border: 1px solid var(--dsw-alias-brand-primary);
622
+ border-radius: 999px;
623
+ background: linear-gradient(135deg, color-mix(in srgb, var(--dsw-alias-brand-primary) 14%, transparent), color-mix(in srgb, var(--dsw-alias-brand-primary) 5%, transparent));
624
+ color: var(--dsw-alias-brand-primary);
625
+ font-size: 12px;
626
+ font-weight: 600;
627
+ font-family: inherit;
628
+ cursor: pointer;
629
+ box-shadow: 0 1px 0 color-mix(in srgb, var(--dsw-alias-brand-primary) 22%, transparent);
630
+ transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
631
+ }
632
+
633
+ .templatesButton svg {
634
+ flex: none;
635
+ }
636
+
637
+ .templatesButton:hover {
638
+ background: linear-gradient(135deg, color-mix(in srgb, var(--dsw-alias-brand-primary) 24%, transparent), color-mix(in srgb, var(--dsw-alias-brand-primary) 8%, transparent));
639
+ color: var(--dsw-alias-brand-primary);
640
+ transform: translateY(-1px);
641
+ box-shadow: 0 2px 6px color-mix(in srgb, var(--dsw-alias-brand-primary) 30%, transparent);
642
+ }
643
+
644
+ .templatesButton:active {
645
+ transform: translateY(0);
646
+ }
647
+
613
648
  .promptCount {
614
649
  font-size: 11px;
615
650
  color: var(--dsw-alias-label-tertiary);