dsh-cait 1.1.6 → 1.1.7

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.
@@ -187,6 +187,20 @@ export const persistenceMethods = {
187
187
  name: cleanName(sourcePptx.name, 'source.pptx'),
188
188
  }
189
189
  }
190
+ const sourceXlsx = rawDoc.sourceXlsx
191
+ if (
192
+ sourceXlsx &&
193
+ typeof sourceXlsx === 'object' &&
194
+ sourceXlsx.kind === 'file' &&
195
+ typeof sourceXlsx.asset === 'string' &&
196
+ /^sources\/[A-Za-z0-9_.-]+\.xlsx$/.test(sourceXlsx.asset)
197
+ ) {
198
+ doc.sourceXlsx = {
199
+ kind: 'file',
200
+ asset: sourceXlsx.asset,
201
+ name: cleanName(sourceXlsx.name, 'source.xlsx'),
202
+ }
203
+ }
190
204
  const sourceEpub = rawDoc.sourceEpub
191
205
  if (
192
206
  sourceEpub &&
package/lib/pptx.js CHANGED
@@ -15,6 +15,13 @@ function slideNames(entries) {
15
15
  .sort((left, right) => Number(left.match(/(\d+)\.xml$/)[1]) - Number(right.match(/(\d+)\.xml$/)[1]))
16
16
  }
17
17
 
18
+ function noteNames(entries) {
19
+ return entries
20
+ .map((entry) => entry.name)
21
+ .filter((name) => /^ppt\/notesSlides\/notesSlide\d+\.xml$/i.test(name))
22
+ .sort((left, right) => Number(left.match(/(\d+)\.xml$/)[1]) - Number(right.match(/(\d+)\.xml$/)[1]))
23
+ }
24
+
18
25
  function paragraphText(paragraph) {
19
26
  return [...String(paragraph).matchAll(/<a:t\b[^>]*>([\s\S]*?)<\/a:t>/gi)]
20
27
  .map((match) => decodeXml(match[1]))
@@ -47,8 +54,33 @@ export function parsePptxDetailed(buffer) {
47
54
  })
48
55
  }
49
56
  }
57
+ for (const name of noteNames(entries)) {
58
+ const xml = entries.find((entry) => entry.name === name)?.data.toString('utf8') ?? ''
59
+ let index = 0
60
+ for (const match of xml.matchAll(/<a:p\b[^>]*>([\s\S]*?)<\/a:p>/gi)) {
61
+ const source = paragraphText(match[1]).trim()
62
+ const noteParagraph = index
63
+ index += 1
64
+ if (!source) continue
65
+ segments.push({
66
+ source,
67
+ target: '',
68
+ paragraph: segments.length,
69
+ meta: {
70
+ format: 'pptx',
71
+ pptxSlide: name,
72
+ pptxParagraph: noteParagraph,
73
+ context: 'notes',
74
+ },
75
+ })
76
+ }
77
+ }
50
78
  if (!segments.length) throw fail('PPTX_NO_UNITS', 'PPTX 中没有可用的正文文本。')
51
- return { format: 'pptx', segments, warnings: [] }
79
+ return {
80
+ format: 'pptx',
81
+ segments,
82
+ warnings: noteNames(entries).length ? ['已导入演讲者备注(notesSlides)。'] : [],
83
+ }
52
84
  }
53
85
 
54
86
  function patchParagraph(paragraph, text) {
package/lib/qa.js CHANGED
@@ -12,6 +12,7 @@ const RISK_BY_CODE = {
12
12
  NUMBER_MISMATCH: 'high',
13
13
  TERM_FORBIDDEN: 'high',
14
14
  UNTRANSLATED_RESIDUE: 'high',
15
+ SUBTITLE_OVERLAP: 'high',
15
16
  TERM_NOT_USED: 'medium',
16
17
  INCONSISTENT_TARGET: 'medium',
17
18
  LENGTH_DRIFT: 'low',
@@ -190,6 +191,22 @@ export const qaMethods = {
190
191
  }
191
192
  }
192
193
 
194
+ // Subtitle cues are allowed to be adjacent, not overlapping. Do not touch
195
+ // the timing here: an overlap is a product decision for the translator.
196
+ if (doc.format === 'srt' || doc.format === 'vtt') {
197
+ let previous = null
198
+ for (let index = 0; index < doc.segments.length; index += 1) {
199
+ const segment = doc.segments[index]
200
+ const start = Number(segment.meta?.start)
201
+ const end = Number(segment.meta?.end)
202
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) continue
203
+ if (previous && start < previous.end) {
204
+ push(index, 'SUBTITLE_OVERLAP', `第 ${index + 1} 条字幕与上一条时间轴重叠。`)
205
+ }
206
+ previous = { end }
207
+ }
208
+ }
209
+
193
210
  const counts = issues.reduce(
194
211
  (acc, issue) => ({ ...acc, [issue.code]: (acc[issue.code] || 0) + 1 }),
195
212
  {
@@ -201,6 +218,7 @@ export const qaMethods = {
201
218
  UNTRANSLATED_RESIDUE: 0,
202
219
  INCONSISTENT_TARGET: 0,
203
220
  LENGTH_DRIFT: 0,
221
+ SUBTITLE_OVERLAP: 0,
204
222
  },
205
223
  )
206
224
 
@@ -6,7 +6,7 @@ import { DEFAULT_PREPROCESS, normalizePreprocessOptions } from './preprocess.js'
6
6
  export { clamp, clampInt }
7
7
 
8
8
  export const MAX_SEGMENTS = 20000
9
- export const CAIT_VERSION = '1.1.6'
9
+ export const CAIT_VERSION = '1.1.7'
10
10
  export const MAX_SEGMENT_CHARS = 20000
11
11
  export const MAX_IMPORT_CHARS = 8_000_000
12
12
  export const MAX_DOCX_BASE64 = 28_000_000
@@ -209,11 +209,11 @@ function cleanInlineTokens(value) {
209
209
  export function cleanSegmentMeta(value) {
210
210
  if (!value || typeof value !== 'object' || Array.isArray(value)) return null
211
211
  const meta = {}
212
- for (const key of ['start', 'end', 'xlsxRow', 'pptxParagraph', 'epubBlock']) {
212
+ for (const key of ['start', 'end', 'xlsxRow', 'pptxParagraph', 'epubBlock', 'docxParagraph', 'table', 'row', 'cell', 'sheet']) {
213
213
  const number = Number(value[key])
214
214
  if (Number.isFinite(number) && number >= 0) meta[key] = Math.round(number)
215
215
  }
216
- for (const key of ['settings', 'identifier', 'context', 'msgidPlural', 'xliffId', 'format', 'jsonPath', 'pptxSlide', 'epubHref']) {
216
+ for (const key of ['settings', 'identifier', 'context', 'msgidPlural', 'xliffId', 'format', 'jsonPath', 'pptxSlide', 'epubHref', 'docxPart', 'keyPath', 'icuType', 'sheetName', 'cell', 'targetCell']) {
217
217
  if (typeof value[key] === 'string' && value[key].trim()) {
218
218
  meta[key] = value[key].slice(0, 500)
219
219
  }
@@ -238,9 +238,14 @@ export function cleanFormatMeta(value) {
238
238
  const meta = {}
239
239
  const obsolete = cleanStringList(value.poObsolete, 200, 5000)
240
240
  if (obsolete) meta.poObsolete = obsolete
241
+ const docxParts = cleanStringList(value.docxParts, 50, 200)
242
+ if (docxParts) meta.docxParts = docxParts
241
243
  if (typeof value.jsonTemplate === 'string' && value.jsonTemplate.length <= 8 * 1024 * 1024) {
242
244
  meta.jsonTemplate = value.jsonTemplate
243
245
  }
246
+ if (typeof value.localizationTemplate === 'string' && value.localizationTemplate.length <= 8 * 1024 * 1024) {
247
+ meta.localizationTemplate = value.localizationTemplate
248
+ }
244
249
  return Object.keys(meta).length ? meta : null
245
250
  }
246
251
 
package/lib/store.js CHANGED
@@ -21,6 +21,7 @@ import { replaceMethods } from './replace.js'
21
21
  import { noteMethods } from './notes.js'
22
22
  import { lockMethods } from './lock.js'
23
23
  import { segmentEditMethods } from './segment-edit.js'
24
+ import { subtitleMethods } from './subtitles.js'
24
25
  import { reviewMethods } from './review.js'
25
26
  import { classifyMatchType, normalizeSource, normalizeText, similarity } from './similarity.js'
26
27
  import { contextOf } from './context.js'
@@ -254,6 +255,7 @@ export class CatStore {
254
255
  paragraph: segment.paragraph,
255
256
  notes: (segment.notes || []).map((note) => ({ ...note })),
256
257
  locked: Boolean(segment.locked),
258
+ meta: segment.meta ? { ...segment.meta } : null,
257
259
  confirmedAt: segment.confirmedAt,
258
260
  }
259
261
  }
@@ -373,6 +375,9 @@ export class CatStore {
373
375
 
374
376
  const createdAt = now()
375
377
  const paragraphMap = Array.isArray(input.paragraphMap) ? input.paragraphMap : null
378
+ const paragraphMetaMap = input.paragraphMeta && typeof input.paragraphMeta === 'object'
379
+ ? input.paragraphMeta
380
+ : null
376
381
  const segments = rawSegments.map((raw, index) => {
377
382
  const source = cleanText(raw.source, '原文')
378
383
  const target = cleanText(raw.target, '译文')
@@ -382,7 +387,11 @@ export class CatStore {
382
387
  : Number.isInteger(raw.paragraph)
383
388
  ? raw.paragraph
384
389
  : index
385
- const meta = cleanSegmentMeta(raw.meta)
390
+ const inheritedMeta = paragraphMetaMap?.[paragraph]
391
+ const meta = cleanSegmentMeta({
392
+ ...(raw.meta && typeof raw.meta === 'object' ? raw.meta : {}),
393
+ ...(inheritedMeta && typeof inheritedMeta === 'object' ? inheritedMeta : {}),
394
+ })
386
395
  const segment = {
387
396
  id: `s${index + 1}`,
388
397
  uid: makeId('u'),
@@ -408,6 +417,7 @@ export class CatStore {
408
417
  if (existing) {
409
418
  if (input.docxSource) this.attachDocxSource(existing, input.docxSource, input.docxName)
410
419
  if (input.pptxSource) this.attachPptxSource(existing, input.pptxSource, input.pptxName)
420
+ if (input.xlsxSource) this.attachXlsxSource(existing, input.xlsxSource, input.xlsxName)
411
421
  if (input.epubSource) this.attachEpubSource(existing, input.epubSource, input.epubName)
412
422
  if (input.format) existing.format = String(input.format).slice(0, 24)
413
423
  if (formatMeta) existing.formatMeta = cleanFormatMeta({ ...(existing.formatMeta || {}), ...formatMeta })
@@ -455,6 +465,7 @@ export class CatStore {
455
465
  if (doc.cursor < 0) doc.cursor = 0
456
466
  if (input.docxSource) this.attachDocxSource(doc, input.docxSource, input.docxName)
457
467
  if (input.pptxSource) this.attachPptxSource(doc, input.pptxSource, input.pptxName)
468
+ if (input.xlsxSource) this.attachXlsxSource(doc, input.xlsxSource, input.xlsxName)
458
469
  if (input.epubSource) this.attachEpubSource(doc, input.epubSource, input.epubName)
459
470
 
460
471
  this.docs.set(doc.id, doc)
@@ -480,6 +491,7 @@ export class CatStore {
480
491
  const tabIndex = this.openDocIds.indexOf(doc.id)
481
492
  this.removeDocxSource(doc)
482
493
  this.removePptxSource(doc)
494
+ this.removeXlsxSource(doc)
483
495
  this.removeEpubSource(doc)
484
496
  this.docs.delete(doc.id)
485
497
  this.openDocIds = this.openDocIds.filter((id) => id !== doc.id)
@@ -1310,4 +1322,4 @@ export class CatStore {
1310
1322
  }
1311
1323
  }
1312
1324
 
1313
- Object.assign(CatStore.prototype, libraryMethods, tmMethods, glossaryMethods, persistenceMethods, importExportMethods, preTranslateMethods, qaMethods, libraryViewMethods, propagateMethods, alignMethods, termMethods, briefMethods, analysisMethods, tmMaintenanceMethods, replaceMethods, noteMethods, lockMethods, segmentEditMethods, reviewMethods)
1325
+ Object.assign(CatStore.prototype, libraryMethods, tmMethods, glossaryMethods, persistenceMethods, importExportMethods, preTranslateMethods, qaMethods, libraryViewMethods, propagateMethods, alignMethods, termMethods, briefMethods, analysisMethods, tmMaintenanceMethods, replaceMethods, noteMethods, lockMethods, segmentEditMethods, subtitleMethods, reviewMethods)
@@ -0,0 +1,213 @@
1
+ import { CatError } from './errors.js'
2
+ import { makeId, normalizeSegmentState } from './store-utils.js'
3
+
4
+ const SUBTITLE_FORMATS = new Set(['srt', 'vtt'])
5
+
6
+ function fail(code, message) {
7
+ return new CatError(code, message)
8
+ }
9
+
10
+ function subtitleDocument(store, documentId) {
11
+ const doc = store.docs?.get(documentId)
12
+ if (!doc) throw fail('DOCUMENT_NOT_FOUND', '文档不存在。')
13
+ if (!SUBTITLE_FORMATS.has(doc.format)) {
14
+ throw fail('SUBTITLE_FORMAT_UNSUPPORTED', '时间轴编辑只支持 SRT/VTT 字幕。')
15
+ }
16
+ return doc
17
+ }
18
+
19
+ function requireUnlocked(segment) {
20
+ if (segment.locked) throw fail('SEGMENT_LOCKED', '句段已锁定,先解锁再编辑。')
21
+ }
22
+
23
+ function timingOf(segment) {
24
+ const start = Number(segment.meta?.start)
25
+ const end = Number(segment.meta?.end)
26
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) {
27
+ throw fail('SUBTITLE_TIMING_INVALID', `第 ${Number(segment.meta?.index ?? 0) + 1} 条字幕时间码无效。`)
28
+ }
29
+ return { start, end }
30
+ }
31
+
32
+ function splitTextAtRatio(text, ratio) {
33
+ const value = String(text ?? '')
34
+ if (!value.trim()) return ['', '']
35
+ const target = Math.max(0, Math.min(value.length, Math.round(value.length * ratio)))
36
+ let index = value.indexOf(' ', target)
37
+ if (index < 0) index = value.lastIndexOf(' ', target)
38
+ if (index <= 0 || index >= value.length - 1) index = target
39
+ const left = value.slice(0, index).trim()
40
+ const right = value.slice(index).trim()
41
+ if (!left || !right) return null
42
+ return [left, right]
43
+ }
44
+
45
+ function snapshot(doc) {
46
+ return {
47
+ segments: doc.segments.map((segment) => ({
48
+ ...segment,
49
+ meta: segment.meta ? { ...segment.meta } : segment.meta,
50
+ notes: Array.isArray(segment.notes) ? segment.notes.map((note) => ({ ...note })) : segment.notes,
51
+ })),
52
+ cursor: doc.cursor,
53
+ }
54
+ }
55
+
56
+ function restore(doc, state) {
57
+ doc.segments = state.segments
58
+ doc.cursor = state.cursor
59
+ }
60
+
61
+ export const subtitleMethods = {
62
+ mergeCues({ documentId, index, count = 2 } = {}) {
63
+ const doc = subtitleDocument(this, documentId)
64
+ const startIndex = Number(index)
65
+ const size = Number(count)
66
+ if (!Number.isInteger(startIndex) || startIndex < 0 || startIndex >= doc.segments.length) {
67
+ throw fail('MERGE_RANGE_INVALID', '合并起始句段无效。')
68
+ }
69
+ if (!Number.isInteger(size) || size < 2 || startIndex + size > doc.segments.length) {
70
+ throw fail('MERGE_RANGE_INVALID', '合并范围无效。')
71
+ }
72
+ const segments = doc.segments.slice(startIndex, startIndex + size)
73
+ for (const segment of segments) requireUnlocked(segment)
74
+ const first = segments[0]
75
+ const last = segments[segments.length - 1]
76
+ const before = timingOf(first)
77
+ const after = timingOf(last)
78
+ const merged = {
79
+ ...first,
80
+ uid: makeId('u'),
81
+ source: segments.map((segment) => segment.source).join('\n'),
82
+ target: segments.some((segment) => segment.target)
83
+ ? segments.map((segment) => segment.target || '').join('\n').trim()
84
+ : '',
85
+ status: segments.some((segment) => segment.target) ? 'translated' : 'pending',
86
+ origin: segments.some((segment) => segment.origin === 'manual') ? 'manual' : (first.origin || null),
87
+ suggestion: null,
88
+ suggestionReason: null,
89
+ confirmedAt: null,
90
+ updatedAt: new Date().toISOString(),
91
+ meta: {
92
+ ...first.meta,
93
+ start: before.start,
94
+ end: after.end,
95
+ identifier: first.meta?.identifier || '',
96
+ mergedFrom: segments.map((segment) => segment.uid),
97
+ },
98
+ }
99
+ normalizeSegmentState(merged)
100
+ const state = snapshot(doc)
101
+ try {
102
+ doc.segments.splice(startIndex, size, merged)
103
+ doc.segments.forEach((segment, at) => {
104
+ segment.id = `s${at + 1}`
105
+ })
106
+ doc.cursor = startIndex
107
+ this.touch()
108
+ this.persist()
109
+ } catch (error) {
110
+ restore(doc, state)
111
+ throw error
112
+ }
113
+ return { documentId, index: startIndex, count: size, segment: this.segmentView(merged, startIndex) }
114
+ },
115
+
116
+ splitCue({ documentId, index, at } = {}) {
117
+ const doc = subtitleDocument(this, documentId)
118
+ const position = Number(index)
119
+ const splitAt = Number(at)
120
+ if (!Number.isInteger(position) || position < 0 || position >= doc.segments.length) {
121
+ throw fail('SPLIT_RANGE_INVALID', '拆分句段无效。')
122
+ }
123
+ const segment = doc.segments[position]
124
+ requireUnlocked(segment)
125
+ const { start, end } = timingOf(segment)
126
+ if (!Number.isFinite(splitAt) || splitAt <= start || splitAt >= end) {
127
+ throw fail('SPLIT_RANGE_INVALID', '拆分钟点必须落在该条字幕的时间范围内。')
128
+ }
129
+ const ratio = (splitAt - start) / (end - start)
130
+ const source = splitTextAtRatio(segment.source, ratio)
131
+ if (!source) throw fail('SPLIT_RANGE_INVALID', '源文字幕太短,无法在此时点拆分。')
132
+ const target = segment.target ? splitTextAtRatio(segment.target, ratio) : null
133
+ if (segment.target && !target) throw fail('SPLIT_RANGE_INVALID', '译文字幕太短,无法在此时点拆分。')
134
+ const state = snapshot(doc)
135
+ const left = {
136
+ ...segment,
137
+ uid: makeId('u'),
138
+ source: source[0],
139
+ target: target?.[0] || '',
140
+ status: target?.[0] ? 'translated' : 'pending',
141
+ origin: target?.[0] ? 'manual' : null,
142
+ suggestion: null,
143
+ suggestionReason: null,
144
+ confirmedAt: null,
145
+ updatedAt: new Date().toISOString(),
146
+ meta: { ...segment.meta, end: splitAt, splitFrom: segment.uid },
147
+ }
148
+ const right = {
149
+ ...segment,
150
+ uid: makeId('u'),
151
+ source: source[1],
152
+ target: target?.[1] || '',
153
+ status: target?.[1] ? 'translated' : 'pending',
154
+ origin: target?.[1] ? 'manual' : null,
155
+ suggestion: null,
156
+ suggestionReason: null,
157
+ confirmedAt: null,
158
+ updatedAt: new Date().toISOString(),
159
+ meta: {
160
+ ...segment.meta,
161
+ start: splitAt,
162
+ identifier: segment.meta?.identifier ? `${segment.meta.identifier}-2` : '',
163
+ splitFrom: segment.uid,
164
+ },
165
+ }
166
+ normalizeSegmentState(left)
167
+ normalizeSegmentState(right)
168
+ try {
169
+ doc.segments.splice(position, 1, left, right)
170
+ doc.segments.forEach((item, atIndex) => {
171
+ item.id = `s${atIndex + 1}`
172
+ })
173
+ doc.cursor = position
174
+ this.touch()
175
+ this.persist()
176
+ } catch (error) {
177
+ restore(doc, state)
178
+ throw error
179
+ }
180
+ return {
181
+ documentId,
182
+ index: position,
183
+ segments: [this.segmentView(left, position), this.segmentView(right, position + 1)],
184
+ }
185
+ },
186
+
187
+ shiftSubtitles({ documentId, offsetMs } = {}) {
188
+ const doc = subtitleDocument(this, documentId)
189
+ const offset = Number(offsetMs)
190
+ if (!Number.isFinite(offset) || !Number.isInteger(offset)) {
191
+ throw fail('SUBTITLE_SHIFT_INVALID', '平移毫秒数必须是整数。')
192
+ }
193
+ if (!offset) return { documentId, offsetMs: 0, shifted: 0 }
194
+ const state = snapshot(doc)
195
+ try {
196
+ for (const segment of doc.segments) {
197
+ requireUnlocked(segment)
198
+ const { start, end } = timingOf(segment)
199
+ if (start + offset < 0) {
200
+ throw fail('SUBTITLE_SHIFT_INVALID', '平移后出现负时间码,请减小平移量。')
201
+ }
202
+ segment.meta = { ...segment.meta, start: start + offset, end: end + offset }
203
+ segment.updatedAt = new Date().toISOString()
204
+ }
205
+ this.touch()
206
+ this.persist()
207
+ } catch (error) {
208
+ restore(doc, state)
209
+ throw error
210
+ }
211
+ return { documentId, offsetMs: offset, shifted: doc.segments.length }
212
+ },
213
+ }