dsh-bilingual-reader 0.1.1 → 0.2.0
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/README.en.md +27 -5
- package/README.md +28 -5
- package/docs/architecture.md +1 -0
- package/docs/demo.jpg +0 -0
- package/docs/release-v0.2.0.md +41 -0
- package/package.json +1 -1
- package/src/glossary.js +33 -0
- package/src/quality.js +30 -0
- package/src/routes.js +96 -14
- package/src/store.js +19 -0
- package/src/translator.js +24 -11
- package/web/app.js +339 -13
- package/web/index.html +23 -5
- package/web/style.css +112 -4
package/web/app.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
const API = '/dsh-bilingual-reader/api'
|
|
2
2
|
const VIEW_KEY = 'dsh-bilingual-reader:view'
|
|
3
|
+
const FONT_KEY = 'dsh-bilingual-reader:font'
|
|
4
|
+
const POSITION_PREFIX = 'dsh-bilingual-reader:pos:'
|
|
5
|
+
const LAST_DOC_KEY = 'dsh-bilingual-reader:last-doc'
|
|
3
6
|
const VIEWS = new Set(['inline', 'bilingual', 'original', 'translation'])
|
|
7
|
+
const FONT_SCALES = [90, 100, 110, 120, 130]
|
|
4
8
|
|
|
5
9
|
const $ = (selector, root = document) => root.querySelector(selector)
|
|
6
10
|
const $$ = (selector, root = document) => [...root.querySelectorAll(selector)]
|
|
7
11
|
|
|
12
|
+
function storedFontScale() {
|
|
13
|
+
try {
|
|
14
|
+
const value = Number(localStorage.getItem(FONT_KEY))
|
|
15
|
+
if (FONT_SCALES.includes(value)) return value
|
|
16
|
+
} catch {
|
|
17
|
+
// localStorage can be unavailable in hardened webviews; default is fine.
|
|
18
|
+
}
|
|
19
|
+
return 100
|
|
20
|
+
}
|
|
21
|
+
|
|
8
22
|
function storedView() {
|
|
9
23
|
try {
|
|
10
24
|
const value = localStorage.getItem(VIEW_KEY)
|
|
@@ -20,9 +34,35 @@ const state = {
|
|
|
20
34
|
docs: [],
|
|
21
35
|
doc: null,
|
|
22
36
|
view: storedView(),
|
|
37
|
+
fontScale: storedFontScale(),
|
|
38
|
+
composerMode: 'url',
|
|
23
39
|
poll: null,
|
|
24
40
|
busy: false,
|
|
25
41
|
glossary: [],
|
|
42
|
+
inboxAvailable: false,
|
|
43
|
+
restoreUntil: 0,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function applyFontScale(scale, persist = true) {
|
|
47
|
+
const index = FONT_SCALES.indexOf(Number(scale))
|
|
48
|
+
const safe = index === -1 ? FONT_SCALES.indexOf(100) : index
|
|
49
|
+
state.fontScale = FONT_SCALES[safe]
|
|
50
|
+
els.blocks.style.fontSize = `${state.fontScale}%`
|
|
51
|
+
els.fontSmaller.disabled = safe === 0
|
|
52
|
+
els.fontLarger.disabled = safe === FONT_SCALES.length - 1
|
|
53
|
+
if (persist) {
|
|
54
|
+
try {
|
|
55
|
+
localStorage.setItem(FONT_KEY, String(state.fontScale))
|
|
56
|
+
} catch {
|
|
57
|
+
// Preference persistence is optional.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function stepFont(delta) {
|
|
63
|
+
const index = FONT_SCALES.indexOf(state.fontScale)
|
|
64
|
+
const next = Math.min(FONT_SCALES.length - 1, Math.max(0, index + delta))
|
|
65
|
+
applyFontScale(FONT_SCALES[next])
|
|
26
66
|
}
|
|
27
67
|
|
|
28
68
|
const rendered = {
|
|
@@ -33,20 +73,30 @@ const rendered = {
|
|
|
33
73
|
const els = {
|
|
34
74
|
form: $('#read-form'),
|
|
35
75
|
url: $('#url-input'),
|
|
76
|
+
text: $('#text-input'),
|
|
77
|
+
composerModes: $$('.composer-modes button'),
|
|
36
78
|
lang: $('#lang-select'),
|
|
37
79
|
readBtn: $('#read-btn'),
|
|
38
80
|
aiHint: $('#ai-hint'),
|
|
39
81
|
notice: $('#notice'),
|
|
40
82
|
docs: $('#docs'),
|
|
41
83
|
refreshDocs: $('#refresh-docs'),
|
|
84
|
+
reader: $('.reader'),
|
|
42
85
|
empty: $('#empty'),
|
|
43
86
|
docView: $('#doc-view'),
|
|
44
87
|
title: $('#doc-title'),
|
|
45
88
|
meta: $('#doc-meta'),
|
|
46
89
|
status: $('#doc-status'),
|
|
47
90
|
progress: $('#progress-bar'),
|
|
91
|
+
progressWrap: $('#progress-wrap'),
|
|
48
92
|
blocks: $('#blocks'),
|
|
93
|
+
fontSmaller: $('#font-smaller'),
|
|
94
|
+
fontReset: $('#font-reset'),
|
|
95
|
+
fontLarger: $('#font-larger'),
|
|
49
96
|
exportLink: $('#export-link'),
|
|
97
|
+
inboxLink: $('#inbox-link'),
|
|
98
|
+
printBtn: $('#print-btn'),
|
|
99
|
+
copyBtn: $('#copy-btn'),
|
|
50
100
|
retry: $('#retry-btn'),
|
|
51
101
|
delete: $('#delete-btn'),
|
|
52
102
|
glossaryBtn: $('#glossary-btn'),
|
|
@@ -54,6 +104,9 @@ const els = {
|
|
|
54
104
|
glossaryDialog: $('#glossary-dialog'),
|
|
55
105
|
glossaryClose: $('#glossary-close'),
|
|
56
106
|
glossaryList: $('#glossary-list'),
|
|
107
|
+
glossaryExport: $('#glossary-export'),
|
|
108
|
+
glossaryImport: $('#glossary-import'),
|
|
109
|
+
glossaryFile: $('#glossary-file'),
|
|
57
110
|
glossaryForm: $('#glossary-form'),
|
|
58
111
|
termSource: $('#term-source'),
|
|
59
112
|
termTarget: $('#term-target'),
|
|
@@ -121,7 +174,28 @@ function showGlossaryNotice(message, isError = false) {
|
|
|
121
174
|
function setBusy(busy) {
|
|
122
175
|
state.busy = busy
|
|
123
176
|
els.readBtn.disabled = busy
|
|
124
|
-
els.readBtn.textContent = busy
|
|
177
|
+
els.readBtn.textContent = busy
|
|
178
|
+
? '处理中…'
|
|
179
|
+
: state.composerMode === 'text' ? '翻译文本' : '读取并翻译'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function setComposerMode(mode) {
|
|
183
|
+
if (mode !== 'url' && mode !== 'text') return
|
|
184
|
+
state.composerMode = mode
|
|
185
|
+
const isUrl = mode === 'url'
|
|
186
|
+
els.url.hidden = !isUrl
|
|
187
|
+
els.url.required = isUrl
|
|
188
|
+
els.text.hidden = isUrl
|
|
189
|
+
els.text.required = !isUrl
|
|
190
|
+
for (const button of els.composerModes) {
|
|
191
|
+
const active = button.dataset.composeMode === mode
|
|
192
|
+
button.classList.toggle('active', active)
|
|
193
|
+
button.setAttribute('aria-pressed', String(active))
|
|
194
|
+
}
|
|
195
|
+
els.readBtn.textContent = isUrl ? '读取并翻译' : '翻译文本'
|
|
196
|
+
showNotice('')
|
|
197
|
+
const field = isUrl ? els.url : els.text
|
|
198
|
+
field.focus()
|
|
125
199
|
}
|
|
126
200
|
|
|
127
201
|
function renderDocs() {
|
|
@@ -145,6 +219,9 @@ async function loadDocs() {
|
|
|
145
219
|
const data = await api('/docs?limit=30')
|
|
146
220
|
state.docs = data.docs
|
|
147
221
|
renderDocs()
|
|
222
|
+
// The list is a file snapshot; the open document is the source of truth for
|
|
223
|
+
// its own status, which avoids a stale "翻译中" after the final poll.
|
|
224
|
+
if (state.doc !== null) updateActiveDocItem(state.doc)
|
|
148
225
|
}
|
|
149
226
|
|
|
150
227
|
function startPolling() {
|
|
@@ -170,21 +247,114 @@ function startPolling() {
|
|
|
170
247
|
}, 900)
|
|
171
248
|
}
|
|
172
249
|
|
|
250
|
+
function positionKey(id) {
|
|
251
|
+
return `${POSITION_PREFIX}${id}`
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function storedLastDoc() {
|
|
255
|
+
try {
|
|
256
|
+
return localStorage.getItem(LAST_DOC_KEY)
|
|
257
|
+
} catch {
|
|
258
|
+
return null
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function rememberDoc(id) {
|
|
263
|
+
try {
|
|
264
|
+
localStorage.setItem(LAST_DOC_KEY, id)
|
|
265
|
+
} catch {
|
|
266
|
+
// Resume is optional.
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function forgetDoc(id) {
|
|
271
|
+
try {
|
|
272
|
+
if (localStorage.getItem(LAST_DOC_KEY) === id) localStorage.removeItem(LAST_DOC_KEY)
|
|
273
|
+
} catch {
|
|
274
|
+
// Resume is optional.
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function readerScrolls() {
|
|
279
|
+
return els.reader.scrollHeight > els.reader.clientHeight + 1
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function currentScrollTop() {
|
|
283
|
+
if (readerScrolls()) return els.reader.scrollTop
|
|
284
|
+
return window.scrollY || document.documentElement.scrollTop || 0
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function setScrollTop(value) {
|
|
288
|
+
// Set both: the page scrolls inside DSH's iframe, while the reader container
|
|
289
|
+
// becomes the scroller in some embeddings. The inactive one ignores the value.
|
|
290
|
+
els.reader.scrollTop = value
|
|
291
|
+
window.scrollTo(0, value)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function saveScroll() {
|
|
295
|
+
if (state.doc === null || Date.now() < state.restoreUntil) return
|
|
296
|
+
try {
|
|
297
|
+
localStorage.setItem(positionKey(state.doc.id), String(currentScrollTop()))
|
|
298
|
+
} catch {
|
|
299
|
+
// Position memory is optional.
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
let restoreToken = 0
|
|
304
|
+
|
|
305
|
+
function restoreScroll(id) {
|
|
306
|
+
let value = 0
|
|
307
|
+
try {
|
|
308
|
+
value = Number(localStorage.getItem(positionKey(id)))
|
|
309
|
+
} catch {
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
if (!Number.isFinite(value) || value <= 0) return
|
|
313
|
+
const token = ++restoreToken
|
|
314
|
+
state.restoreUntil = Date.now() + 500
|
|
315
|
+
const apply = () => {
|
|
316
|
+
if (token !== restoreToken || state.doc?.id !== id) return
|
|
317
|
+
setScrollTop(value)
|
|
318
|
+
}
|
|
319
|
+
// Layout is synchronous after innerHTML; timers also work in background
|
|
320
|
+
// webviews where requestAnimationFrame may be throttled.
|
|
321
|
+
setTimeout(apply, 0)
|
|
322
|
+
setTimeout(apply, 150)
|
|
323
|
+
}
|
|
324
|
+
|
|
173
325
|
async function openDoc(id) {
|
|
326
|
+
saveScroll()
|
|
327
|
+
// While the new document renders, the browser can emit a scroll event at 0.
|
|
328
|
+
// Hold position writes until the saved position has been restored.
|
|
329
|
+
state.restoreUntil = Date.now() + 1000
|
|
174
330
|
const data = await api(`/docs/${encodeURIComponent(id)}`)
|
|
175
331
|
state.doc = data.doc
|
|
176
|
-
|
|
332
|
+
rememberDoc(id)
|
|
177
333
|
renderDocs()
|
|
334
|
+
renderDoc()
|
|
335
|
+
restoreScroll(id)
|
|
178
336
|
startPolling()
|
|
179
337
|
}
|
|
180
338
|
|
|
339
|
+
function retryButton(block) {
|
|
340
|
+
return `<button type="button" class="badge-action" data-retry-block="${escapeAttr(block.id)}">重试此段</button>`
|
|
341
|
+
}
|
|
342
|
+
|
|
181
343
|
function blockPlaceholder(block) {
|
|
182
344
|
if (block.status === 'failed') {
|
|
183
|
-
return `<p class="placeholder error">翻译失败${block.error ? `:${escapeHtml(block.error)}` : ''}</p>`
|
|
345
|
+
return `<p class="placeholder error">翻译失败${block.error ? `:${escapeHtml(block.error)}` : ''} ${retryButton(block)}</p>`
|
|
184
346
|
}
|
|
185
347
|
return '<p class="placeholder">待翻译</p>'
|
|
186
348
|
}
|
|
187
349
|
|
|
350
|
+
function warningLabel(value) {
|
|
351
|
+
const text = String(value ?? '')
|
|
352
|
+
if (text.includes('保护片段')) return '保护片段丢失'
|
|
353
|
+
if (text.includes('术语未使用')) return '术语未命中'
|
|
354
|
+
if (text.includes('译文与原文')) return '可能未翻译'
|
|
355
|
+
return '需复核'
|
|
356
|
+
}
|
|
357
|
+
|
|
188
358
|
function renderBlock(block) {
|
|
189
359
|
const id = ` data-block-id="${escapeAttr(block.id)}"`
|
|
190
360
|
if (block.type === 'code' || block.type === 'hr') {
|
|
@@ -194,7 +364,7 @@ function renderBlock(block) {
|
|
|
194
364
|
return `<section class="block block-plain"${id}>${block.originalHtml}</section>`
|
|
195
365
|
}
|
|
196
366
|
const warning = block.warning
|
|
197
|
-
? `<span class="badge warn" title="${escapeAttr(block.warning)}"
|
|
367
|
+
? `<span class="badge warn" title="${escapeAttr(block.warning)}">${escapeHtml(warningLabel(block.warning))}</span>${retryButton(block)}`
|
|
198
368
|
: ''
|
|
199
369
|
const translation = block.translationHtml
|
|
200
370
|
? block.translationHtml
|
|
@@ -251,6 +421,8 @@ function renderDocChrome() {
|
|
|
251
421
|
if (doc === null) {
|
|
252
422
|
els.empty.hidden = false
|
|
253
423
|
els.docView.hidden = true
|
|
424
|
+
els.progressWrap.setAttribute('aria-valuenow', '0')
|
|
425
|
+
els.progressWrap.removeAttribute('aria-valuetext')
|
|
254
426
|
return false
|
|
255
427
|
}
|
|
256
428
|
els.empty.hidden = true
|
|
@@ -265,7 +437,10 @@ function renderDocChrome() {
|
|
|
265
437
|
`请求 ${stats.requests ?? 0}`,
|
|
266
438
|
].filter(Boolean)
|
|
267
439
|
els.meta.textContent = meta.join(' · ')
|
|
268
|
-
|
|
440
|
+
const percent = Math.round((doc.progress ?? 0) * 100)
|
|
441
|
+
els.progress.style.width = `${percent}%`
|
|
442
|
+
els.progressWrap.setAttribute('aria-valuenow', String(percent))
|
|
443
|
+
els.progressWrap.setAttribute('aria-valuetext', `${statusLabel(doc.status)} ${percent}%`)
|
|
269
444
|
const failed = stats.failed ?? 0
|
|
270
445
|
els.status.textContent = doc.error
|
|
271
446
|
? `${statusLabel(doc.status)} · ${doc.error}`
|
|
@@ -274,11 +449,27 @@ function renderDocChrome() {
|
|
|
274
449
|
els.retry.hidden = !(failed > 0 || ['failed', 'partial', 'interrupted'].includes(doc.status))
|
|
275
450
|
els.exportLink.href = `${API}/docs/${encodeURIComponent(doc.id)}/export`
|
|
276
451
|
els.exportLink.setAttribute('download', `${doc.id}.bilingual.md`)
|
|
452
|
+
const canSaveToInbox = state.inboxAvailable && typeof doc.url === 'string' && doc.url.trim() !== ''
|
|
453
|
+
els.inboxLink.hidden = !canSaveToInbox
|
|
454
|
+
if (canSaveToInbox) {
|
|
455
|
+
els.inboxLink.href = `/dsh-reading-inbox/?clip=${encodeURIComponent(doc.url)}`
|
|
456
|
+
els.inboxLink.title = `在阅读箱中归档 ${doc.url}`
|
|
457
|
+
}
|
|
277
458
|
els.blocks.className = `blocks view-${state.view}`
|
|
278
459
|
updateActiveDocItem(doc)
|
|
279
460
|
return true
|
|
280
461
|
}
|
|
281
462
|
|
|
463
|
+
async function detectReadingInbox() {
|
|
464
|
+
try {
|
|
465
|
+
const response = await fetch('/dsh-reading-inbox/api/health', { cache: 'no-store' })
|
|
466
|
+
state.inboxAvailable = response.ok
|
|
467
|
+
} catch {
|
|
468
|
+
state.inboxAvailable = false
|
|
469
|
+
}
|
|
470
|
+
if (state.doc !== null) renderDocChrome()
|
|
471
|
+
}
|
|
472
|
+
|
|
282
473
|
function renderDoc() {
|
|
283
474
|
if (!renderDocChrome()) return
|
|
284
475
|
renderBlocksFull(state.doc)
|
|
@@ -305,24 +496,32 @@ function setView(view) {
|
|
|
305
496
|
}
|
|
306
497
|
}
|
|
307
498
|
|
|
308
|
-
async function
|
|
499
|
+
async function startDoc(path, body, progressNotice) {
|
|
309
500
|
setBusy(true)
|
|
310
|
-
showNotice(
|
|
501
|
+
showNotice(progressNotice)
|
|
311
502
|
try {
|
|
312
|
-
const data = await api('
|
|
313
|
-
method: 'POST',
|
|
314
|
-
body: { url, targetLang: els.lang.value },
|
|
315
|
-
})
|
|
503
|
+
const data = await api(path, { method: 'POST', body })
|
|
316
504
|
showNotice('已开始翻译,完成后会自动刷新。')
|
|
317
505
|
await loadDocs()
|
|
318
506
|
await openDoc(data.doc.id)
|
|
507
|
+
return true
|
|
319
508
|
} catch (error) {
|
|
320
509
|
showNotice(error instanceof Error ? error.message : String(error), true)
|
|
510
|
+
return false
|
|
321
511
|
} finally {
|
|
322
512
|
setBusy(false)
|
|
323
513
|
}
|
|
324
514
|
}
|
|
325
515
|
|
|
516
|
+
function startRead(url) {
|
|
517
|
+
return startDoc('/read', { url, targetLang: els.lang.value }, '正在抓取正文…')
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async function startText(text) {
|
|
521
|
+
const started = await startDoc('/read-text', { text, targetLang: els.lang.value }, '正在准备文本…')
|
|
522
|
+
if (started) els.text.value = ''
|
|
523
|
+
}
|
|
524
|
+
|
|
326
525
|
async function retryDoc() {
|
|
327
526
|
if (state.doc === null) return
|
|
328
527
|
try {
|
|
@@ -334,11 +533,30 @@ async function retryDoc() {
|
|
|
334
533
|
}
|
|
335
534
|
}
|
|
336
535
|
|
|
536
|
+
async function retryBlock(blockId) {
|
|
537
|
+
if (state.doc === null) return
|
|
538
|
+
const id = state.doc.id
|
|
539
|
+
try {
|
|
540
|
+
await api(`/docs/${encodeURIComponent(id)}/blocks/${encodeURIComponent(blockId)}/retry`, { method: 'POST' })
|
|
541
|
+
await openDoc(id)
|
|
542
|
+
showNotice('已重试该段。')
|
|
543
|
+
} catch (error) {
|
|
544
|
+
showNotice(error instanceof Error ? error.message : String(error), true)
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
337
548
|
async function deleteDoc() {
|
|
338
549
|
if (state.doc === null) return
|
|
339
550
|
if (!window.confirm(`删除《${state.doc.title}》?`)) return
|
|
340
551
|
try {
|
|
341
|
-
|
|
552
|
+
const id = state.doc.id
|
|
553
|
+
await api(`/docs/${encodeURIComponent(id)}`, { method: 'DELETE' })
|
|
554
|
+
try {
|
|
555
|
+
localStorage.removeItem(positionKey(id))
|
|
556
|
+
} catch {
|
|
557
|
+
// Position memory is optional.
|
|
558
|
+
}
|
|
559
|
+
forgetDoc(id)
|
|
342
560
|
state.doc = null
|
|
343
561
|
renderDoc()
|
|
344
562
|
await loadDocs()
|
|
@@ -399,15 +617,61 @@ async function init() {
|
|
|
399
617
|
} catch {
|
|
400
618
|
els.docs.innerHTML = '<p class="sidebar-empty">读取最近文档失败。</p>'
|
|
401
619
|
}
|
|
620
|
+
const params = new URLSearchParams(location.search)
|
|
621
|
+
const requestedDoc = params.get('doc')
|
|
622
|
+
if (requestedDoc !== null && requestedDoc.trim() !== '') {
|
|
623
|
+
try {
|
|
624
|
+
await openDoc(requestedDoc.trim())
|
|
625
|
+
} catch (error) {
|
|
626
|
+
showNotice(error instanceof Error ? error.message : String(error), true)
|
|
627
|
+
}
|
|
628
|
+
} else {
|
|
629
|
+
const lastDoc = storedLastDoc()
|
|
630
|
+
if (lastDoc !== null && state.docs.some(doc => doc.id === lastDoc)) {
|
|
631
|
+
await openDoc(lastDoc).catch(() => forgetDoc(lastDoc))
|
|
632
|
+
} else if (lastDoc !== null) {
|
|
633
|
+
forgetDoc(lastDoc)
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const requestedUrl = params.get('url')
|
|
637
|
+
if (requestedDoc === null && requestedUrl !== null && /^https?:\/\//i.test(requestedUrl.trim())) {
|
|
638
|
+
const url = requestedUrl.trim()
|
|
639
|
+
setComposerMode('url')
|
|
640
|
+
els.url.value = url
|
|
641
|
+
showNotice('已填入链接,点击“读取并翻译”。')
|
|
642
|
+
if (params.get('start') === '1') void startRead(url)
|
|
643
|
+
}
|
|
644
|
+
void detectReadingInbox()
|
|
402
645
|
}
|
|
403
646
|
|
|
404
647
|
els.form.addEventListener('submit', event => {
|
|
405
648
|
event.preventDefault()
|
|
649
|
+
if (state.busy) return
|
|
650
|
+
if (state.composerMode === 'text') {
|
|
651
|
+
const text = els.text.value.trim()
|
|
652
|
+
if (text === '') {
|
|
653
|
+
showNotice('请先粘贴要翻译的文本。', true)
|
|
654
|
+
return
|
|
655
|
+
}
|
|
656
|
+
void startText(text)
|
|
657
|
+
return
|
|
658
|
+
}
|
|
406
659
|
const url = els.url.value.trim()
|
|
407
|
-
if (url === ''
|
|
660
|
+
if (url === '') return
|
|
408
661
|
void startRead(url)
|
|
409
662
|
})
|
|
410
663
|
|
|
664
|
+
for (const button of els.composerModes) {
|
|
665
|
+
button.addEventListener('click', () => setComposerMode(button.dataset.composeMode))
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
els.text.addEventListener('keydown', event => {
|
|
669
|
+
if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
|
|
670
|
+
event.preventDefault()
|
|
671
|
+
els.form.requestSubmit()
|
|
672
|
+
}
|
|
673
|
+
})
|
|
674
|
+
|
|
411
675
|
els.refreshDocs.addEventListener('click', () => {
|
|
412
676
|
void loadDocs().catch(error => showNotice(error.message, true))
|
|
413
677
|
})
|
|
@@ -418,12 +682,39 @@ els.docs.addEventListener('click', event => {
|
|
|
418
682
|
void openDoc(button.dataset.docId).catch(error => showNotice(error.message, true))
|
|
419
683
|
})
|
|
420
684
|
|
|
685
|
+
els.blocks.addEventListener('click', event => {
|
|
686
|
+
const button = event.target.closest('[data-retry-block]')
|
|
687
|
+
if (button === null) return
|
|
688
|
+
void retryBlock(button.dataset.retryBlock)
|
|
689
|
+
})
|
|
690
|
+
|
|
421
691
|
for (const button of $$('.view-switch button')) {
|
|
422
692
|
button.addEventListener('click', () => setView(button.dataset.view))
|
|
423
693
|
}
|
|
424
694
|
|
|
425
695
|
els.retry.addEventListener('click', () => void retryDoc())
|
|
426
696
|
els.delete.addEventListener('click', () => void deleteDoc())
|
|
697
|
+
els.printBtn.addEventListener('click', () => window.print())
|
|
698
|
+
els.copyBtn.addEventListener('click', async () => {
|
|
699
|
+
if (state.doc === null) return
|
|
700
|
+
const text = state.doc.blocks
|
|
701
|
+
.filter(block => block.translatable && block.translation)
|
|
702
|
+
.map(block => block.translation)
|
|
703
|
+
.join('\n\n')
|
|
704
|
+
if (text === '') {
|
|
705
|
+
showNotice('还没有可复制的译文。', true)
|
|
706
|
+
return
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
await navigator.clipboard.writeText(text)
|
|
710
|
+
showNotice('已复制全部译文。')
|
|
711
|
+
} catch {
|
|
712
|
+
showNotice('复制失败,请手动选择文本。', true)
|
|
713
|
+
}
|
|
714
|
+
})
|
|
715
|
+
els.fontSmaller.addEventListener('click', () => stepFont(-1))
|
|
716
|
+
els.fontReset.addEventListener('click', () => applyFontScale(100))
|
|
717
|
+
els.fontLarger.addEventListener('click', () => stepFont(1))
|
|
427
718
|
|
|
428
719
|
els.glossaryBtn.addEventListener('click', () => void openGlossary())
|
|
429
720
|
els.glossaryClose.addEventListener('click', () => els.glossaryDialog.close())
|
|
@@ -431,6 +722,26 @@ els.glossaryDialog.addEventListener('click', event => {
|
|
|
431
722
|
if (event.target === els.glossaryDialog) els.glossaryDialog.close()
|
|
432
723
|
})
|
|
433
724
|
|
|
725
|
+
els.glossaryExport.href = `${API}/glossary/export`
|
|
726
|
+
els.glossaryImport.addEventListener('click', () => els.glossaryFile.click())
|
|
727
|
+
els.glossaryFile.addEventListener('change', async () => {
|
|
728
|
+
const file = els.glossaryFile.files?.[0]
|
|
729
|
+
if (file === undefined) return
|
|
730
|
+
try {
|
|
731
|
+
const parsed = JSON.parse(await file.text())
|
|
732
|
+
const terms = Array.isArray(parsed) ? parsed : parsed.terms
|
|
733
|
+
if (!Array.isArray(terms)) throw new Error('JSON 里没有 terms 数组')
|
|
734
|
+
const data = await api('/glossary/import', { method: 'POST', body: { terms } })
|
|
735
|
+
state.glossary = data.terms
|
|
736
|
+
renderGlossary()
|
|
737
|
+
showGlossaryNotice(`导入完成:新增 ${data.added},更新 ${data.updated},跳过 ${data.skipped}。`)
|
|
738
|
+
} catch (error) {
|
|
739
|
+
showGlossaryNotice(error instanceof Error ? error.message : String(error), true)
|
|
740
|
+
} finally {
|
|
741
|
+
els.glossaryFile.value = ''
|
|
742
|
+
}
|
|
743
|
+
})
|
|
744
|
+
|
|
434
745
|
els.glossaryForm.addEventListener('submit', async event => {
|
|
435
746
|
event.preventDefault()
|
|
436
747
|
const source = els.termSource.value.trim()
|
|
@@ -469,9 +780,24 @@ els.glossaryList.addEventListener('click', async event => {
|
|
|
469
780
|
}
|
|
470
781
|
})
|
|
471
782
|
|
|
783
|
+
let scrollTimer = null
|
|
784
|
+
function scheduleScrollSave() {
|
|
785
|
+
if (state.doc === null || scrollTimer !== null) return
|
|
786
|
+
scrollTimer = setTimeout(() => {
|
|
787
|
+
scrollTimer = null
|
|
788
|
+
saveScroll()
|
|
789
|
+
}, 300)
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
window.addEventListener('scroll', scheduleScrollSave, { passive: true })
|
|
793
|
+
els.reader.addEventListener('scroll', scheduleScrollSave, { passive: true })
|
|
794
|
+
|
|
472
795
|
window.addEventListener('beforeunload', () => {
|
|
473
796
|
if (state.poll !== null) clearInterval(state.poll)
|
|
797
|
+
saveScroll()
|
|
474
798
|
})
|
|
475
799
|
|
|
476
800
|
setView(state.view)
|
|
801
|
+
setComposerMode('url')
|
|
802
|
+
applyFontScale(state.fontScale, false)
|
|
477
803
|
void init()
|
package/web/index.html
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
6
|
<title>双语阅读 · DSH</title>
|
|
7
7
|
<link rel="icon" href="./favicon.svg" type="image/svg+xml">
|
|
8
|
-
<link rel="stylesheet" href="./style.css">
|
|
8
|
+
<link rel="stylesheet" href="./style.css?v=0.2.0">
|
|
9
9
|
</head>
|
|
10
10
|
<body>
|
|
11
11
|
<div id="app">
|
|
@@ -18,13 +18,23 @@
|
|
|
18
18
|
</div>
|
|
19
19
|
</div>
|
|
20
20
|
<div class="top-actions">
|
|
21
|
+
<div class="font-control" role="group" aria-label="阅读字号">
|
|
22
|
+
<button type="button" id="font-smaller" class="icon-button" aria-label="减小字号" title="减小字号">A−</button>
|
|
23
|
+
<button type="button" id="font-reset" class="icon-button" aria-label="恢复默认字号" title="恢复默认字号">A</button>
|
|
24
|
+
<button type="button" id="font-larger" class="icon-button" aria-label="增大字号" title="增大字号">A+</button>
|
|
25
|
+
</div>
|
|
21
26
|
<button type="button" id="glossary-btn" class="button ghost">术语表 <span id="glossary-count">0</span></button>
|
|
22
27
|
</div>
|
|
23
28
|
</header>
|
|
24
29
|
|
|
25
30
|
<section class="composer">
|
|
31
|
+
<div class="composer-modes" role="group" aria-label="输入方式">
|
|
32
|
+
<button type="button" data-compose-mode="url" class="active" aria-pressed="true">网页链接</button>
|
|
33
|
+
<button type="button" data-compose-mode="text" aria-pressed="false">粘贴文本</button>
|
|
34
|
+
</div>
|
|
26
35
|
<form id="read-form" autocomplete="off">
|
|
27
36
|
<input id="url-input" type="url" placeholder="粘贴网页链接,例如 https://example.com/article" required>
|
|
37
|
+
<textarea id="text-input" rows="4" placeholder="粘贴 Markdown 或纯文本,最多 20000 字符" required hidden></textarea>
|
|
28
38
|
<select id="lang-select" aria-label="目标语言"></select>
|
|
29
39
|
<button type="submit" id="read-btn" class="button primary">读取并翻译</button>
|
|
30
40
|
</form>
|
|
@@ -36,7 +46,7 @@
|
|
|
36
46
|
<aside class="sidebar" aria-label="最近文档">
|
|
37
47
|
<div class="sidebar-head">
|
|
38
48
|
<h2>最近</h2>
|
|
39
|
-
<button type="button" id="refresh-docs" class="icon-button" title="刷新">↻</button>
|
|
49
|
+
<button type="button" id="refresh-docs" class="icon-button" title="刷新" aria-label="刷新最近文档">↻</button>
|
|
40
50
|
</div>
|
|
41
51
|
<div id="docs" class="docs"></div>
|
|
42
52
|
</aside>
|
|
@@ -45,7 +55,7 @@
|
|
|
45
55
|
<div id="empty" class="empty-state">
|
|
46
56
|
<div class="empty-mark" aria-hidden="true">译</div>
|
|
47
57
|
<h2>把网页读成双语</h2>
|
|
48
|
-
<p
|
|
58
|
+
<p>粘贴一个公开文章链接或一段正文,也可以让 Agent 调用 <code>bilingual_read_url</code>。代码、公式、链接和表格保持原样,只翻译正文。</p>
|
|
49
59
|
<ul>
|
|
50
60
|
<li>上下 / 对照 / 原文 / 译文四种视图</li>
|
|
51
61
|
<li>术语表 + 内容哈希缓存,重复段落不再调用模型</li>
|
|
@@ -66,12 +76,15 @@
|
|
|
66
76
|
<button type="button" data-view="original">原文</button>
|
|
67
77
|
<button type="button" data-view="translation">译文</button>
|
|
68
78
|
</div>
|
|
79
|
+
<button type="button" id="print-btn" class="button">打印</button>
|
|
80
|
+
<button type="button" id="copy-btn" class="button" title="复制全部译文(Markdown)">复制译文</button>
|
|
69
81
|
<a id="export-link" class="button" href="#" download>导出双语 Markdown</a>
|
|
82
|
+
<a id="inbox-link" class="button" href="#" target="_blank" rel="noopener" hidden>存入阅读箱</a>
|
|
70
83
|
<button type="button" id="retry-btn" class="button" hidden>重试失败段</button>
|
|
71
84
|
<button type="button" id="delete-btn" class="button danger">删除</button>
|
|
72
85
|
</div>
|
|
73
86
|
</div>
|
|
74
|
-
<div class="progress" aria-
|
|
87
|
+
<div class="progress" id="progress-wrap" role="progressbar" aria-label="翻译进度" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div id="progress-bar"></div></div>
|
|
75
88
|
<p class="doc-status" id="doc-status"></p>
|
|
76
89
|
<article id="blocks" class="blocks view-inline"></article>
|
|
77
90
|
</div>
|
|
@@ -84,6 +97,11 @@
|
|
|
84
97
|
<button type="button" id="glossary-close" class="icon-button" aria-label="关闭">×</button>
|
|
85
98
|
</div>
|
|
86
99
|
<p class="dialog-hint">翻译时会强制使用这些译法。示例:token → 令牌。</p>
|
|
100
|
+
<div class="glossary-tools">
|
|
101
|
+
<a id="glossary-export" class="button small" href="#" download="dsh-bilingual-glossary.json">导出 JSON</a>
|
|
102
|
+
<button type="button" id="glossary-import" class="button small">导入 JSON</button>
|
|
103
|
+
<input type="file" id="glossary-file" accept="application/json,.json" hidden>
|
|
104
|
+
</div>
|
|
87
105
|
<div id="glossary-list" class="glossary-list"></div>
|
|
88
106
|
<form id="glossary-form" class="glossary-form" autocomplete="off">
|
|
89
107
|
<input id="term-source" type="text" placeholder="原文术语" required maxlength="80">
|
|
@@ -94,6 +112,6 @@
|
|
|
94
112
|
<p class="notice" id="glossary-notice" role="status" hidden></p>
|
|
95
113
|
</dialog>
|
|
96
114
|
</div>
|
|
97
|
-
<script src="./app.js"></script>
|
|
115
|
+
<script src="./app.js?v=0.2.0"></script>
|
|
98
116
|
</body>
|
|
99
117
|
</html>
|