85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import type { FieldResult } from '../src/types'
|
|
import { getOverlayRect, getScrollTarget, getTargetZoom } from '../src/components/pdfPreviewMath'
|
|
|
|
const field: FieldResult = {
|
|
id: 'field-1',
|
|
page: 1,
|
|
text: '食品名称:天问礼品粽',
|
|
font_name: 'Mock Font',
|
|
font_size_pt: 12,
|
|
font_height_mm: 4,
|
|
x0_pt: 100,
|
|
top_pt: 80,
|
|
x1_pt: 180,
|
|
bottom_pt: 116,
|
|
normalized_text: '食品名称天问礼品粽',
|
|
validation_status: 'matched',
|
|
validation_reason: 'matched',
|
|
matched_excerpt: '食品名称:天问礼品粽',
|
|
}
|
|
|
|
test('getOverlayRect keeps tiny fields visible without drifting away from center', () => {
|
|
const rect = getOverlayRect(
|
|
{
|
|
...field,
|
|
x1_pt: 101,
|
|
bottom_pt: 81,
|
|
},
|
|
0.4,
|
|
0.4,
|
|
)
|
|
|
|
assert.equal(rect.width, 4)
|
|
assert.equal(rect.height, 4)
|
|
assert.equal(rect.strokeWidth, 1)
|
|
assert.equal(rect.left, 38.2)
|
|
assert.equal(rect.top, 30.2)
|
|
})
|
|
|
|
test('getTargetZoom caps focus zoom and keeps normal fit as the floor', () => {
|
|
assert.equal(
|
|
getTargetZoom(field, { width: 1200, height: 900 }, 0.5),
|
|
2.8,
|
|
)
|
|
|
|
assert.equal(
|
|
getTargetZoom(field, { width: 0, height: 900 }, 0.5),
|
|
1,
|
|
)
|
|
})
|
|
|
|
test('getScrollTarget centers the active field and clamps to scroll bounds', () => {
|
|
const rect = getOverlayRect(field, 2, 2)
|
|
const centered = getScrollTarget({
|
|
containerWidth: 800,
|
|
containerHeight: 600,
|
|
scrollWidth: 2200,
|
|
scrollHeight: 1800,
|
|
pageOffsetLeft: 260,
|
|
pageOffsetTop: 420,
|
|
rect,
|
|
})
|
|
|
|
assert.deepEqual(centered, {
|
|
left: 140,
|
|
top: 316,
|
|
})
|
|
|
|
const clamped = getScrollTarget({
|
|
containerWidth: 800,
|
|
containerHeight: 600,
|
|
scrollWidth: 1200,
|
|
scrollHeight: 900,
|
|
pageOffsetLeft: 20,
|
|
pageOffsetTop: 30,
|
|
rect,
|
|
})
|
|
|
|
assert.deepEqual(clamped, {
|
|
left: 0,
|
|
top: 0,
|
|
})
|
|
})
|