printInputOffset.ts
29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/**
* 与 Web 管理端 labelFormDatePreview 一致:templateProductDefaults 中日期/时间/时长存 JSON
* `{"unit":"Days","value":"2"}`,预览与打印时按 BaseTime 解析为 config.text。
*/
import type { SystemLabelTemplate, SystemTemplateElementBase } from '../print/types/printer'
const OFFSET_UNITS = new Set([
'Minutes',
'Hours',
'Days',
'Weeks',
'Months (30 Day)',
'Years',
])
/** 与 Web PropertiesPanel DATE_FORMAT_OPTIONS + 常用 datetime 预设一致 */
export const DATE_DISPLAY_FORMAT_PRESETS = new Set([
'DD/MM/YYYY',
'MM/DD/YYYY',
'DD/MM/YY',
'MM/DD/YY',
'MM/YY',
'MM/DD',
'MM',
'DD',
'YY',
'FULLY DAY(WEDNESDAY)',
'DAY (WED)',
'MONTH (DECEMBER)',
'YEAR (2025)',
'DD MONTH YEAR (25 DECEMBER 2025)',
'YYYY-MM-DD',
'YYYY-MM-DD HH:mm',
'HH:mm',
'12 hr',
'24 hr',
])
export function isKnownDateDisplayFormat (raw: string | null | undefined): boolean {
return DATE_DISPLAY_FORMAT_PRESETS.has(String(raw ?? '').trim())
}
export function isStoredPrintInputOffsetPayload (raw: string | null | undefined): boolean {
return parseStoredPrintInputOffset(String(raw ?? '').trim()) != null
}
/** 自定义 datetime 模板(非预设名),不能作为展示 format */
function looksLikeCustomDateTimeFormatTemplate (raw: string | null | undefined): boolean {
const t = String(raw ?? '').trim()
if (!t || isKnownDateDisplayFormat(t)) return false
if (/GMT/i.test(t)) return true
if (/\bCST\b|\bMT\+|\(CST\)/i.test(t)) return true
if (/HH:mm:ss/i.test(t)) return true
if (/:\d{2}:\d{2}/.test(t)) return true
if (/^\d{1,2}\s+\d{4}\s+\d{1,2}:\d{2}/.test(t)) return true
return false
}
/** 已展开的日期/时间乱串(GMT 等);**不含**合法的偏移 JSON */
export function isCorruptedDateDisplayFormat (raw: string | null | undefined): boolean {
const t = String(raw ?? '').trim()
if (!t) return false
if (isStoredPrintInputOffsetPayload(t)) return false
if (OFFSET_UNITS.has(t)) return true
if (looksLikeCustomDateTimeFormatTemplate(t)) return true
/** 如 06 2026 13:45:57 GMT+0 */
if (/^\d{1,2}\s+\d{4}\s+\d{1,2}:\d{2}(:\d{2})?(\s+GMT)?/i.test(t)) return true
/** App 端 toLocaleString 失败时的 Date.toString():MON JUL 06 2026 14:29:07 GMT+0800 (CST) */
if (/^(SUN|MON|TUE|WED|THU|FRI|SAT)\s+[A-Z]{3}\s+\d{1,2}\s+\d{4}/i.test(t)) return true
return false
}
/** 与 normalizeLabelPanelTypeAdd 一致:优先元素 typeAdd,其次 config.typeAdd */
export function resolveElementTypeAdd (el: SystemTemplateElementBase): string {
const cfg = (el.config || {}) as Record<string, unknown>
return String(
(el as { typeAdd?: string }).typeAdd ??
cfg.typeAdd ??
cfg.TypeAdd ??
'',
).trim()
}
/** 元素命名/id hint(用于区分 durationdate / durationdate2) */
export function elementDateNameHint (el: SystemTemplateElementBase): string {
return `${el.elementName ?? ''} ${el.inputKey ?? ''} ${el.id ?? ''}`.toLowerCase()
}
/** 底部黑条星期:仅 durationdate2(**不含** Use By 的 durationdate) */
export function isWeekdayBarNameHint (hint: string): boolean {
return /durationdate2|duration_date_2|durationdate_2|duration\s*date\s*2/.test(hint)
}
/** Use By / 到期日:durationdate(无 2)、use by 等 */
export function isUseByDateNameHint (hint: string): boolean {
if (isWeekdayBarNameHint(hint)) return false
return (
/(?:^|\s|_)durationdate(?:\s|_|$)/.test(hint) ||
/duration\s*date(?!\s*2)/.test(hint) ||
/duration_date(?!_2)/.test(hint) ||
/use\s*by|must\s*use|expir/.test(hint)
)
}
function frozenKnownDateFormat (cfg: Record<string, unknown>): string {
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
if (preserved && isKnownDateDisplayFormat(preserved)) return preserved
for (const c of [cfg.format, cfg.Format, cfg.displayFormat, cfg.DisplayFormat]) {
const raw = String(c ?? '').trim()
if (raw && isKnownDateDisplayFormat(raw) && !parseStoredPrintInputOffset(raw)) return raw
}
return ''
}
/** 打印时输入的字面量字段(Number/Text/Weight),不得按日期偏移重算 */
export function isPrintInputLiteralField (el: SystemTemplateElementBase): boolean {
const vst = String(el.valueSourceType || '').toUpperCase()
if (vst !== 'PRINT_INPUT') return false
const rawType = String(el.type || '').trim()
const typeUpper = rawType.toUpperCase()
if (typeUpper === 'WEIGHT' || typeUpper.includes('|WEIGHT')) return true
const cfg = (el.config || {}) as Record<string, unknown>
const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
if (it === 'number' || it === 'text') return true
const ta = resolveElementTypeAdd(el).toLowerCase()
if (/\|\s*number\b/.test(ta) || ta.endsWith('number')) return true
if (/\|\s*weight\b/.test(ta) || ta.endsWith('weight')) return true
if (/\|\s*text\b/.test(ta) || ta.endsWith('|text')) return true
if (/print[\s|]*number/i.test(rawType)) return true
if (/print[\s|]*text/i.test(rawType)) return true
if (/print[\s|]*weight/i.test(rawType)) return true
return false
}
/** 打印时输入 Number/Text/Weight 的展示文案(与 Web 画布 print|number 一致) */
export function resolvePrintInputLiteralDisplay (el: SystemTemplateElementBase): string {
const cfg = (el.config || {}) as Record<string, unknown>
const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
const type = String(el.type || '').toUpperCase()
let body = ''
if (type === 'WEIGHT') {
body = String(cfg.value ?? cfg.Value ?? '').trim()
if (!body) body = String(cfg.text ?? cfg.Text ?? '').trim()
} else {
body = String(cfg.text ?? cfg.Text ?? '').trim()
}
if (!body && it === 'number') body = '0'
/** 字面量 Number/Text 的纯数字不能走偏移 JSON 解析(否则会误当成 Days 偏移而清空) */
if (isCorruptedDateDisplayFormat(body)) return ''
const unit = String(cfg.unit ?? cfg.Unit ?? '').trim()
if (unit && body && !body.endsWith(unit)) body = `${body}${unit}`
return applyElementPrefix(cfg, body)
}
/** Text (For Template) 等固定文案:invertColors 仅表示黑底白字,不是星期条 */
export function isFixedStaticTextElement (el: SystemTemplateElementBase): boolean {
if (isPrintInputLiteralField(el)) return true
const type = String(el.type || '').toUpperCase()
if (type !== 'TEXT_STATIC' && type !== 'TEXT' && type !== 'TEXT_PRODUCT' && type !== 'TEXT_PRICE') {
return false
}
const vst = String(el.valueSourceType || '').toUpperCase()
if (vst && vst !== 'FIXED' && vst !== 'STATIC') return false
const cfg = (el.config || {}) as Record<string, unknown>
const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
if (
inputType === 'date' ||
inputType === 'datetime' ||
inputType === 'number' ||
inputType === 'options'
) {
return false
}
if (isUniAppDateTimeOffsetField(el)) return false
if (isWeekdayBarNameHint(elementDateNameHint(el))) return false
return true
}
/**
* 底部黑条星期元素(durationdate2 / FULLY DAY 格式)。
* invertColors 只负责黑底白字样式,不能单独判定为星期条。
*/
export function isWeekdayBarElement (el: SystemTemplateElementBase): boolean {
const type = String(el.type || '').toUpperCase()
/** 重打已焙成 TEXT_STATIC 的星期条不再走 live 偏移,直接画 config.text */
if (type === 'TEXT_STATIC' || type === 'TEXT') return false
if (isFixedStaticTextElement(el)) return false
const cfg = (el.config || {}) as Record<string, unknown>
const hint = elementDateNameHint(el)
if (isWeekdayBarNameHint(hint)) return true
const fmt = frozenKnownDateFormat(cfg)
if (fmt === 'FULLY DAY(WEDNESDAY)' || fmt === 'DAY (WED)') return true
return false
}
/** @deprecated 使用 isWeekdayBarElement */
export function isInvertedWeekdayElement (el: SystemTemplateElementBase): boolean {
return isWeekdayBarElement(el)
}
/** 首次解析模板时冻结展示用 format,避免后续 merge 把 offset JSON / 展开串写进 format 后丢失 FULLY DAY 等 */
export function captureDateDisplayFormatInConfig (cfg: Record<string, unknown>): void {
if (String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()) return
for (const c of [cfg.format, cfg.Format, cfg.displayFormat, cfg.DisplayFormat]) {
const raw = String(c ?? '').trim()
if (!raw || parseStoredPrintInputOffset(raw)) continue
if (!isKnownDateDisplayFormat(raw) || isCorruptedDateDisplayFormat(raw)) continue
cfg.__dateDisplayFormat = raw
return
}
}
/** 清理被污染的 format/text,恢复 FULLY DAY 等展示格式;保留合法偏移 JSON */
export function sanitizeDateElementConfig (
el: SystemTemplateElementBase,
cfg: Record<string, unknown>,
): void {
if (isFixedStaticTextElement(el)) return
captureDateDisplayFormatInConfig(cfg)
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
let fmt = String(cfg.format ?? cfg.Format ?? '').trim()
let txt = String(cfg.text ?? cfg.Text ?? '').trim()
const fmtOffset = isStoredPrintInputOffsetPayload(fmt)
const txtOffset = isStoredPrintInputOffsetPayload(txt)
/** 后端 PRINT_INPUT 可能把偏移 JSON 写进 format;统一迁到 text 并恢复展示 format */
if (fmtOffset && !txtOffset) {
cfg.text = fmt
cfg.Text = fmt
txt = fmt
}
if (fmtOffset || txtOffset) {
const hint = elementDateNameHint(el)
const next =
(preserved && isKnownDateDisplayFormat(preserved) ? preserved : '') ||
(isWeekdayBarNameHint(hint) ? 'FULLY DAY(WEDNESDAY)' : '') ||
(isUseByDateNameHint(hint) ? 'MM/DD/YYYY' : '') ||
'DD/MM/YYYY'
if (isKnownDateDisplayFormat(next)) {
cfg.format = next
cfg.Format = next
if (!cfg.__dateDisplayFormat) cfg.__dateDisplayFormat = next
}
fmt = String(cfg.format ?? cfg.Format ?? '').trim()
} else if (fmt && (isCorruptedDateDisplayFormat(fmt) || looksLikeCustomDateTimeFormatTemplate(fmt))) {
if (isFixedStaticTextElement(el)) return
const hint = elementDateNameHint(el)
const next =
preserved ||
(isWeekdayBarNameHint(hint) ? 'FULLY DAY(WEDNESDAY)' : '') ||
(isUseByDateNameHint(hint) ? 'MM/DD/YYYY' : '')
if (next) {
cfg.format = next
cfg.Format = next
if (!cfg.__dateDisplayFormat) cfg.__dateDisplayFormat = next
} else {
delete cfg.format
delete cfg.Format
}
}
const txtAfter = String(cfg.text ?? cfg.Text ?? '').trim()
if (txtAfter && isCorruptedDateDisplayFormat(txtAfter)) {
const hint = elementDateNameHint(el)
if (isUniAppDateTimeOffsetField(el) || isWeekdayBarNameHint(hint) || isUseByDateNameHint(hint)) {
delete cfg.text
delete cfg.Text
}
}
const pf = String(cfg.__previewFormatted ?? cfg.__PreviewFormatted ?? '').trim()
if (pf && isCorruptedDateDisplayFormat(pf)) {
delete cfg.__previewFormatted
delete cfg.__PreviewFormatted
}
}
export function applyOffsetToDate (base: Date, amount: number, unit: string): Date {
const d = new Date(base.getTime())
const u = String(unit ?? '').trim()
switch (u) {
case 'Minutes':
d.setMinutes(d.getMinutes() + amount)
break
case 'Hours':
d.setHours(d.getHours() + amount)
break
case 'Days':
d.setDate(d.getDate() + amount)
break
case 'Weeks':
d.setDate(d.getDate() + amount * 7)
break
case 'Months (30 Day)':
d.setDate(d.getDate() + amount * 30)
break
case 'Years':
d.setFullYear(d.getFullYear() + amount)
break
default:
d.setDate(d.getDate() + amount)
}
return d
}
/** App 原生端 toLocaleString(weekday) 常返回整段 Date.toString(),须用手动表 */
const WEEKDAY_LONG_EN = [
'SUNDAY',
'MONDAY',
'TUESDAY',
'WEDNESDAY',
'THURSDAY',
'FRIDAY',
'SATURDAY',
] as const
const WEEKDAY_SHORT_EN = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'] as const
/** 已是展开后的星期展示文案(重打快照 / 落库 __previewFormatted) */
export function isResolvedWeekdayDisplayLiteral (raw: string | null | undefined): boolean {
const t = String(raw ?? '').trim().toUpperCase()
if (!t) return false
if ((WEEKDAY_LONG_EN as readonly string[]).includes(t)) return true
if ((WEEKDAY_SHORT_EN as readonly string[]).includes(t)) return true
if (/^DAY\s*\([A-Z]{3}\)$/i.test(String(raw ?? '').trim())) return true
return false
}
/** 重打/画布:优先读快照里已冻结的星期文案,禁止再按 today 重算 */
export function readFrozenWeekdayDisplayFromConfig (cfg: Record<string, unknown>): string {
for (const key of ['__previewFormatted', '__PreviewFormatted', 'text', 'Text'] as const) {
const raw = String(cfg[key] ?? '').trim()
if (!raw) continue
if (parseStoredPrintInputOffset(raw)) continue
if (isCorruptedDateDisplayFormat(raw)) continue
if (isResolvedWeekdayDisplayLiteral(raw)) {
return raw.toUpperCase().startsWith('DAY (') ? raw : raw.toUpperCase()
}
}
return ''
}
const MONTH_LONG_EN = [
'JANUARY',
'FEBRUARY',
'MARCH',
'APRIL',
'MAY',
'JUNE',
'JULY',
'AUGUST',
'SEPTEMBER',
'OCTOBER',
'NOVEMBER',
'DECEMBER',
] as const
function weekdayLongEn (date: Date): string {
return WEEKDAY_LONG_EN[date.getDay()] ?? 'SUNDAY'
}
function weekdayShortEn (date: Date): string {
return WEEKDAY_SHORT_EN[date.getDay()] ?? 'SUN'
}
function monthLongEn (date: Date): string {
return MONTH_LONG_EN[date.getMonth()] ?? 'JANUARY'
}
function formatDateByPreset (format: string, date: Date): string {
const yyyy = String(date.getFullYear())
const yy = yyyy.slice(-2)
const mm = String(date.getMonth() + 1).padStart(2, '0')
const dd = String(date.getDate()).padStart(2, '0')
const hh = String(date.getHours()).padStart(2, '0')
const min = String(date.getMinutes()).padStart(2, '0')
const hour12 = date.getHours() % 12 || 12
const ampm = date.getHours() >= 12 ? 'pm' : 'am'
const monthLong = monthLongEn(date)
const dayLong = weekdayLongEn(date)
const dayShort = weekdayShortEn(date)
switch (format) {
case 'DD/MM/YYYY':
return `${dd}/${mm}/${yyyy}`
case 'MM/DD/YYYY':
return `${mm}/${dd}/${yyyy}`
case 'DD/MM/YY':
return `${dd}/${mm}/${yy}`
case 'MM/DD/YY':
return `${mm}/${dd}/${yy}`
case 'MM/YY':
return `${mm}/${yy}`
case 'MM/DD':
return `${mm}/${dd}`
case 'MM':
return mm
case 'DD':
return dd
case 'YY':
return yy
case 'FULLY DAY(WEDNESDAY)':
return dayLong
case 'DAY (WED)':
return dayShort
case 'MONTH (DECEMBER)':
return monthLong
case 'YEAR (2025)':
return yyyy
case 'DD MONTH YEAR (25 DECEMBER 2025)':
return `${dd} ${monthLong} ${yyyy}`
case 'YYYY-MM-DD':
return `${yyyy}-${mm}-${dd}`
case 'YYYY-MM-DD HH:mm':
return `${yyyy}-${mm}-${dd} ${hh}:${min}`
case '12 hr':
return `${hour12}:${min}${ampm}`
case '24 hr':
case 'HH:mm':
return `${hh}:${min}`
default:
if (isCorruptedDateDisplayFormat(format) || looksLikeCustomDateTimeFormatTemplate(format)) {
return `${mm}/${dd}/${yyyy}`
}
return String(format || '')
.replace(/YYYY/g, yyyy)
.replace(/YY/g, yy)
.replace(/MM/g, mm)
.replace(/DD/g, dd)
.replace(/HH/g, hh)
.replace(/mm/g, min)
}
}
/** 已算好的日期/时间展示串(非 format 预设名、非 JSON 偏移) */
export function isLikelyResolvedDateTimeLiteral (raw: string | null | undefined): boolean {
const t = String(raw ?? '').trim()
if (!t || t.startsWith('{')) return false
if (OFFSET_UNITS.has(t)) return false
if (/^\d{4}-\d{2}-\d{2}/.test(t)) return true
if (/^\d{4}-\d{2}-\d{2}\s+\d{1,2}:\d{2}/.test(t)) return true
if (/^\d{1,2}\/\d{1,2}\/\d{2,4}/.test(t)) return true
if (/^\d{1,2}:\d{2}(:\d{2})?(\s?[ap]m)?$/i.test(t)) return true
return false
}
function applyElementPrefix (cfg: Record<string, unknown>, body: string): string {
const prefix = String(cfg.prefix ?? cfg.Prefix ?? '')
if (!prefix) return body
if (body.startsWith(prefix)) return body
return `${prefix}${body}`
}
function normalizeOffsetAmount (valueRaw: string): number | null {
const t = String(valueRaw ?? '').trim()
if (t === '') return 0
const n = Number(t)
return Number.isFinite(n) ? n : null
}
function inferDurationWeekdayDisplayFormat (el: SystemTemplateElementBase, cfg: Record<string, unknown>): string | null {
if (!isWeekdayBarElement({ ...el, config: cfg })) return null
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
if (preserved === 'FULLY DAY(WEDNESDAY)' || preserved === 'DAY (WED)') return preserved
return 'FULLY DAY(WEDNESDAY)'
}
function dateFormatPatternForElement (
el: SystemTemplateElementBase,
cfg: Record<string, unknown>,
): string {
const type = String(el.type || '').toUpperCase()
if (type === 'TIME') {
const raw = String(cfg.format ?? cfg.Format ?? '24 hr').trim()
return raw === '12 hr' ? '12 hr' : '24 hr'
}
/** 仅底部黑条星期走 FULLY DAY;Use By 仍用 MM/DD/YYYY */
if (isWeekdayBarElement({ ...el, config: cfg })) {
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
if (preserved === 'FULLY DAY(WEDNESDAY)' || preserved === 'DAY (WED)') return preserved
return 'FULLY DAY(WEDNESDAY)'
}
const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
if (preserved && isKnownDateDisplayFormat(preserved)) return preserved
const hint = elementDateNameHint(el)
if (isUseByDateNameHint(hint)) return 'MM/DD/YYYY'
const candidates = [
cfg.format,
cfg.Format,
cfg.displayFormat,
cfg.DisplayFormat,
cfg.dateFormat,
cfg.DateFormat,
]
.map((v) => (typeof v === 'string' ? v.trim() : ''))
.filter(Boolean)
for (const raw of candidates) {
if (isStoredPrintInputOffsetPayload(raw)) continue
if (isCorruptedDateDisplayFormat(raw) || looksLikeCustomDateTimeFormatTemplate(raw)) continue
if (isLikelyResolvedDateTimeLiteral(raw) || OFFSET_UNITS.has(raw)) continue
if (isKnownDateDisplayFormat(raw)) return raw
}
const inferred = inferDurationWeekdayDisplayFormat(el, cfg)
if (inferred) return inferred
return it === 'datetime' ? 'YYYY-MM-DD HH:mm' : 'DD/MM/YYYY'
}
function readOffsetFromElementConfig (
el: SystemTemplateElementBase,
): { amount: number; unit: string } | null {
const type = String(el.type || '').toUpperCase()
const cfg = (el.config || {}) as Record<string, unknown>
const fromStoredText = parseStoredPrintInputOffset(String(cfg.text ?? cfg.Text ?? ''))
if (fromStoredText) {
const amount = normalizeOffsetAmount(fromStoredText.value)
if (amount !== null) {
return { amount, unit: fromStoredText.unit || 'Days' }
}
}
const fromStoredFormat = parseStoredPrintInputOffset(String(cfg.format ?? cfg.Format ?? ''))
if (fromStoredFormat) {
const amount = normalizeOffsetAmount(fromStoredFormat.value)
if (amount !== null) {
return { amount, unit: fromStoredFormat.unit || 'Days' }
}
}
if (type === 'DURATION') {
const unit = String(cfg.format ?? cfg.Format ?? 'Days').trim() || 'Days'
const u = OFFSET_UNITS.has(unit) ? unit : 'Days'
const rawV =
cfg.durationValue ??
cfg.value ??
cfg.offsetDays ??
cfg.DurationValue ??
cfg.Value ??
cfg.OffsetDays
const val = Number(rawV)
return { amount: Number.isFinite(val) ? val : 0, unit: u }
}
const od = cfg.offsetDays ?? cfg.OffsetDays
if (od != null && String(od).trim() !== '') {
const n = Number(od)
if (Number.isFinite(n)) return { amount: n, unit: 'Days' }
}
const fmt = String(cfg.format ?? cfg.Format ?? '').trim()
if (fmt && OFFSET_UNITS.has(fmt)) {
const rawV = cfg.durationValue ?? cfg.value ?? cfg.DurationValue ?? cfg.Value ?? 0
const val = Number(rawV)
return { amount: Number.isFinite(val) ? val : 0, unit: fmt }
}
return null
}
function resolveFromOffsetAmount (
el: SystemTemplateElementBase,
amount: number,
unit: string,
base: Date,
): string {
const type = String(el.type || '').toUpperCase()
const cfg = (el.config || {}) as Record<string, unknown>
const d = applyOffsetToDate(base, amount, unit)
if (type === 'DATE') {
return applyElementPrefix(cfg, formatDateByPreset(dateFormatPatternForElement(el, cfg), d))
}
if (type === 'TIME') {
return applyElementPrefix(cfg, formatDateByPreset(dateFormatPatternForElement(el, cfg), d))
}
return applyElementPrefix(cfg, `${amount} ${unit}`.trim())
}
/**
* 画布预览 / 原生打印:按基准时刻解析 DATE/TIME/DURATION,忽略 AUTO_DB 上已过期的 text/format。
*/
export function resolveElementDateTimeDisplay (
el: SystemTemplateElementBase,
base: Date = new Date(),
): string | null {
const type = String(el.type || '').toUpperCase()
if (type !== 'DATE' && type !== 'TIME' && type !== 'DURATION') return null
const cfg = { ...(el.config || {}) } as Record<string, unknown>
sanitizeDateElementConfig(el, cfg)
if (isPrintInputLiteralField({ ...el, config: cfg })) {
const line = resolvePrintInputLiteralDisplay({ ...el, config: cfg })
return line || null
}
const vst = String(el.valueSourceType ?? '').toUpperCase()
const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
const rawText = String(cfg.text ?? cfg.Text ?? '').trim()
const fromOffsetJson = (raw: string): string | null => {
const p = parseStoredPrintInputOffset(raw)
if (!p) return null
const amount = normalizeOffsetAmount(p.value)
if (amount === null) return null
return resolveFromOffsetAmount(el, amount, p.unit || 'Days', base)
}
const jsonResolved = (rawText ? fromOffsetJson(rawText) : null)
?? fromOffsetJson(String(cfg.format ?? cfg.Format ?? ''))
if (jsonResolved) {
if (isCorruptedDateDisplayFormat(jsonResolved)) {
if (isWeekdayBarElement({ ...el, config: cfg })) {
return resolveWeekdayDisplayForElement({ ...el, config: cfg }, base)
}
return null
}
return jsonResolved
}
const isOffsetField = isUniAppDateTimeOffsetField({ ...el, config: cfg })
if (vst === 'PRINT_INPUT') {
/** 非相对偏移的 PRINT_INPUT 才使用固定文案;勿把已展开的 GMT/时间串当最终展示 */
if (
rawText &&
!parseStoredPrintInputOffset(rawText) &&
!isOffsetField &&
isLikelyResolvedDateTimeLiteral(rawText) &&
!isCorruptedDateDisplayFormat(rawText)
) {
return applyElementPrefix(cfg, rawText)
}
if (
isOffsetField ||
inputType === 'date' ||
inputType === 'datetime' ||
type === 'TIME'
) {
const off = readOffsetFromElementConfig({ ...el, config: cfg })
if (off) return resolveFromOffsetAmount({ ...el, config: cfg }, off.amount, off.unit, base)
return resolveFromOffsetAmount({ ...el, config: cfg }, 0, 'Days', base)
}
return null
}
/** FIXED 的 durationdate/durationtime 等:config.text 可能已是平台录入的偏移 JSON / 纯数字 */
if (vst === 'FIXED' && isOffsetField) {
const off = readOffsetFromElementConfig({ ...el, config: cfg })
if (off) return resolveFromOffsetAmount({ ...el, config: cfg }, off.amount, off.unit, base)
return resolveFromOffsetAmount({ ...el, config: cfg }, 0, 'Days', base)
}
if (vst === 'AUTO_DB' && (type === 'DATE' || type === 'TIME' || type === 'DURATION' || isOffsetField)) {
const off = readOffsetFromElementConfig({ ...el, config: cfg })
if (off) return resolveFromOffsetAmount({ ...el, config: cfg }, off.amount, off.unit, base)
return resolveFromOffsetAmount({ ...el, config: cfg }, 0, 'Days', base)
}
const off = readOffsetFromElementConfig({ ...el, config: cfg })
if (off) return resolveFromOffsetAmount({ ...el, config: cfg }, off.amount, off.unit, base)
if (isOffsetField) {
return resolveFromOffsetAmount({ ...el, config: cfg }, 0, 'Days', base)
}
if (
rawText &&
!parseStoredPrintInputOffset(rawText) &&
isLikelyResolvedDateTimeLiteral(rawText)
) {
return applyElementPrefix(cfg, rawText)
}
return resolveFromOffsetAmount({ ...el, config: cfg }, 0, 'Days', base)
}
/** 底部黑条星期(durationdate2);Use By 的 durationdate 不在此列 */
export function resolveWeekdayDisplayForElement (
el: SystemTemplateElementBase,
base: Date = new Date(),
): string {
const cfg = { ...(el.config || {}) } as Record<string, unknown>
sanitizeDateElementConfig(el, cfg)
const frozen = readFrozenWeekdayDisplayFromConfig(cfg)
if (frozen) return applyElementPrefix(cfg, frozen)
const off = readOffsetFromElementConfig({ ...el, config: cfg })
const amount = off?.amount ?? 0
const unit = off?.unit ?? 'Days'
const d = applyOffsetToDate(base, amount, unit)
const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim()
const pattern =
preserved === 'DAY (WED)' ? 'DAY (WED)' : 'FULLY DAY(WEDNESDAY)'
return applyElementPrefix(cfg, formatDateByPreset(pattern, d))
}
export function tryParsePrintInputOffsetStored (
raw: string | null | undefined,
): { unit: string; value: string } | null {
const t = String(raw ?? '').trim()
if (!t) return null
if (t.startsWith('{')) {
try {
const o = JSON.parse(t) as unknown
if (o == null || typeof o !== 'object' || Array.isArray(o)) return null
const rec = o as Record<string, unknown>
const unit = String(rec.unit ?? rec.Unit ?? '').trim()
const value = String(rec.value ?? rec.Value ?? '')
if (!unit || !OFFSET_UNITS.has(unit)) return null
return { unit, value }
} catch {
return null
}
}
/** 兼容旧版 templateProductDefaults 仅存纯数字(表示 Days) */
if (/^\d+$/.test(t)) return { unit: 'Days', value: t }
return null
}
/** 与 Web offsetFieldUiStateFromStored 对齐 */
export function parseStoredPrintInputOffset (
raw: string | null | undefined,
): { unit: string; value: string } | null {
return tryParsePrintInputOffsetStored(raw)
}
/** 与 Web isDateTimeLiveResolveField 对齐:含 auto current date/time 等相对基准解析字段 */
export function isUniAppDateTimeOffsetField (el: SystemTemplateElementBase): boolean {
if (isPrintInputLiteralField(el)) return false
const type = String(el.type || '').toUpperCase()
const cfg = (el.config || {}) as Record<string, unknown>
if (type === 'TIME' || type === 'DURATION') return true
if (type !== 'DATE') return false
const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase()
if (it === 'datetime' || it === 'date') return true
const ta = resolveElementTypeAdd(el).toLowerCase().replace(/\s+/g, ' ')
if (ta === 'label_duration date' || ta.includes('duration date')) return true
if (ta === 'auto_current date' || ta.includes('current date')) return true
if (ta === 'auto_current time' || ta.includes('current time')) return true
const nameHint = `${el.elementName ?? ''} ${el.inputKey ?? ''}`.toLowerCase()
if (/(durationdate|duration_date|duration\s*date)/.test(nameHint)) return true
return false
}
/**
* 将 templateProductDefaults 里单格字符串转为画布用的展示文案(相对 now)。
* 非 JSON 或无法解析时返回原串(兼容旧版已展开的日期文案)。
*/
/**
* 预览/出纸前:按同一基准时刻重算所有 DATE/TIME/DURATION 及偏移类字段,保证 Prep 与 Use By 一致。
*/
export function applyLiveDateTimeFieldsToTemplate (
template: SystemLabelTemplate,
base: Date = new Date(),
): SystemLabelTemplate {
const elements = (template.elements || []).map((el) => {
const type = String(el.type || '').toUpperCase()
const cfg = { ...(el.config || {}) } as Record<string, unknown>
const weekdayEl = isWeekdayBarElement(el)
if (isPrintInputLiteralField(el)) return el
const needsLive =
type === 'DATE' ||
type === 'TIME' ||
type === 'DURATION' ||
isUniAppDateTimeOffsetField(el) ||
weekdayEl
if (!needsLive) return el
sanitizeDateElementConfig(el, cfg)
/** 仅 durationdate2 黑条:偏移 + FULLY DAY */
if (weekdayEl) {
const live = resolveWeekdayDisplayForElement({ ...el, config: cfg }, base)
if (!live || !String(live).trim() || isCorruptedDateDisplayFormat(live)) return el
const raw = String(cfg.text ?? cfg.Text ?? '').trim()
const keepsOffsetStorage =
isUniAppDateTimeOffsetField(el) &&
(parseStoredPrintInputOffset(raw) != null || /^\d+$/.test(raw))
cfg.__previewFormatted = live
if (!keepsOffsetStorage) {
cfg.text = live
cfg.Text = live
}
return { ...el, config: cfg }
}
let live: string | null = null
try {
live = resolveElementDateTimeDisplay({ ...el, config: cfg }, base)
} catch (err) {
console.warn('[printInputOffset] applyLiveDateTimeFields resolve failed', el.id, err)
}
if (live != null && isCorruptedDateDisplayFormat(live)) {
live = null
}
if (live == null || !String(live).trim()) return el
const raw = String(cfg.text ?? cfg.Text ?? '').trim()
const keepsOffsetStorage =
isUniAppDateTimeOffsetField(el) &&
(parseStoredPrintInputOffset(raw) != null || /^\d+$/.test(raw))
cfg.__previewFormatted = live
if (!keepsOffsetStorage) {
cfg.text = live
cfg.Text = live
}
return { ...el, config: cfg }
})
return { ...template, elements }
}
export function resolveTemplateDefaultValueForElement (
el: SystemTemplateElementBase,
stored: string,
now: Date,
): string {
const s = String(stored ?? '').trim()
if (!s) return ''
if (!isUniAppDateTimeOffsetField(el)) return s
const parsed = parseStoredPrintInputOffset(s)
if (parsed) {
const amount = normalizeOffsetAmount(parsed.value)
if (amount === null) return ''
return resolveFromOffsetAmount(el, amount, parsed.unit || 'Days', now)
}
if (isLikelyResolvedDateTimeLiteral(s)) {
const off = readOffsetFromElementConfig(el)
if (off) return resolveFromOffsetAmount(el, off.amount, off.unit, now)
}
return s
}