@olenbetong/synergi-react 4.1.0 → 4.1.2
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/es/components/ImageEditor/plugins/cropPlugin/useCropTool.js +3 -1
- package/es/components/ImageEditor/useEdgeDetection.d.ts +17 -2
- package/es/components/ImageEditor/useEdgeDetection.js +238 -61
- package/es/components/PdfViewer/PdfViewer.js +22 -3
- package/es/components/Toolbar/index.js +20 -1
- package/package.json +7 -7
- package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +3 -1
- package/src/components/ImageEditor/useEdgeDetection.ts +255 -66
- package/src/components/PdfViewer/PdfViewer.tsx +22 -3
- package/src/components/Toolbar/index.tsx +21 -1
|
@@ -52,7 +52,9 @@ export function useCropTool(imageUrl, enabled) {
|
|
|
52
52
|
let activeCorners = draftQuad ?? corners ?? fullImageQuad(canvasPixelSize.width, canvasPixelSize.height);
|
|
53
53
|
let scale = displaySize.width > 0 ? displaySize.width / canvasPixelSize.width : 1;
|
|
54
54
|
let handleCanvasReady = useCallback((canvas, w, h) => {
|
|
55
|
-
|
|
55
|
+
// Detect on a bare copy: the displayed canvas carries the selection border, which
|
|
56
|
+
// starts out along the image edges and would be found as the document outline.
|
|
57
|
+
setCanvasEl(canvasRef.current?.getCleanCanvas() ?? null);
|
|
56
58
|
setCanvasPixelSize({ width: w, height: h });
|
|
57
59
|
setDisplaySize({ width: canvas.clientWidth, height: canvas.clientHeight });
|
|
58
60
|
}, []);
|
|
@@ -5,12 +5,27 @@ export interface Point {
|
|
|
5
5
|
/** TL, TR, BR, BL */
|
|
6
6
|
export type Quad = [Point, Point, Point, Point];
|
|
7
7
|
export declare function fullImageQuad(width: number, height: number): Quad;
|
|
8
|
+
/**
|
|
9
|
+
* Finds the most likely document outline in an RGBA image.
|
|
10
|
+
*
|
|
11
|
+
* Print is first removed with a morphological close, so text and table rules do not
|
|
12
|
+
* compete with the paper's outline. Straight segments from a Hough transform are merged
|
|
13
|
+
* into candidate lines, and every combination of two near-vertical and two near-horizontal
|
|
14
|
+
* lines (the image borders included, for receipts that run off the photo) is scored by how
|
|
15
|
+
* much of each side both lies on an edge and separates two differently coloured surfaces.
|
|
16
|
+
* Scoring by supported length, rather than by the share of each side, lets a long receipt
|
|
17
|
+
* whose edges bend at a fold beat a small, perfectly straight section of it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function findDocumentQuad(cv: any, src: any): Quad | null;
|
|
8
20
|
interface UseEdgeDetectionResult {
|
|
9
21
|
corners: Quad | null;
|
|
10
22
|
}
|
|
11
23
|
/**
|
|
12
|
-
*
|
|
13
|
-
* Falls back to full-image corners if no
|
|
24
|
+
* Suggests document corners for the image on `canvas`.
|
|
25
|
+
* Falls back to full-image corners if no document outline is found.
|
|
26
|
+
*
|
|
27
|
+
* `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
|
|
28
|
+
* selection border, is detected as an edge like any other.
|
|
14
29
|
*/
|
|
15
30
|
export declare function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult;
|
|
16
31
|
export {};
|
|
@@ -7,80 +7,257 @@ export function fullImageQuad(width, height) {
|
|
|
7
7
|
{ x: 0, y: height },
|
|
8
8
|
];
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
/** Longest side of the downscaled working image. Detection is scale-dependent, so it is fixed. */
|
|
11
|
+
const WORK_SIZE = 512;
|
|
12
|
+
/** Closing kernel that erases dark print (text, thin rules) so only surface boundaries remain. */
|
|
13
|
+
const CLOSE_KERNEL = 5;
|
|
14
|
+
/** Candidate lines kept per orientation family. The search is O(n⁴), so keep this small. */
|
|
15
|
+
const MAX_LINES_PER_FAMILY = 14;
|
|
16
|
+
/** Distances (working pixels) sampled on each side of a candidate edge. */
|
|
17
|
+
const SAMPLE_OFFSET_MIN = 3;
|
|
18
|
+
const SAMPLE_OFFSET_MAX = 9;
|
|
19
|
+
/** Minimum Lab colour difference between the two sides for an edge sample to count. */
|
|
20
|
+
const MIN_CONTRAST = 8;
|
|
21
|
+
/** Cost of an unsupported stretch of side, relative to the gain of a supported one. */
|
|
22
|
+
const UNSUPPORTED_COST = 0.5;
|
|
23
|
+
/** Cost per pixel of using the image border as a side (document running off the frame). */
|
|
24
|
+
const BORDER_COST = 0.1;
|
|
25
|
+
/** Minimum normalised score for a suggestion; below it the full image is used. */
|
|
26
|
+
const MIN_SCORE = 0.3;
|
|
27
|
+
const MIN_AREA_FRACTION = 0.1;
|
|
28
|
+
/**
|
|
29
|
+
* Finds the most likely document outline in an RGBA image.
|
|
30
|
+
*
|
|
31
|
+
* Print is first removed with a morphological close, so text and table rules do not
|
|
32
|
+
* compete with the paper's outline. Straight segments from a Hough transform are merged
|
|
33
|
+
* into candidate lines, and every combination of two near-vertical and two near-horizontal
|
|
34
|
+
* lines (the image borders included, for receipts that run off the photo) is scored by how
|
|
35
|
+
* much of each side both lies on an edge and separates two differently coloured surfaces.
|
|
36
|
+
* Scoring by supported length, rather than by the share of each side, lets a long receipt
|
|
37
|
+
* whose edges bend at a fold beat a small, perfectly straight section of it.
|
|
38
|
+
*/
|
|
39
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
40
|
+
export function findDocumentQuad(cv, src) {
|
|
41
|
+
let scale = Math.min(1, WORK_SIZE / Math.max(src.cols, src.rows));
|
|
42
|
+
let small = new cv.Mat();
|
|
43
|
+
let rgb = new cv.Mat();
|
|
44
|
+
let colour = new cv.Mat();
|
|
45
|
+
let lab = new cv.Mat();
|
|
11
46
|
let gray = new cv.Mat();
|
|
12
|
-
let blurred = new cv.Mat();
|
|
13
47
|
let edges = new cv.Mat();
|
|
14
|
-
let
|
|
15
|
-
let
|
|
16
|
-
let hierarchy = new cv.Mat();
|
|
48
|
+
let support = new cv.Mat();
|
|
49
|
+
let segments = new cv.Mat();
|
|
17
50
|
try {
|
|
18
|
-
cv.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let
|
|
24
|
-
cv.
|
|
25
|
-
|
|
26
|
-
cv.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
cv.resize(src, small, new cv.Size(Math.round(src.cols * scale), Math.round(src.rows * scale)), 0, 0, cv.INTER_AREA);
|
|
52
|
+
let w = small.cols;
|
|
53
|
+
let h = small.rows;
|
|
54
|
+
let minDim = Math.min(w, h);
|
|
55
|
+
cv.cvtColor(small, rgb, cv.COLOR_RGBA2RGB);
|
|
56
|
+
let closeKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(CLOSE_KERNEL, CLOSE_KERNEL));
|
|
57
|
+
cv.morphologyEx(rgb, rgb, cv.MORPH_CLOSE, closeKernel);
|
|
58
|
+
closeKernel.delete();
|
|
59
|
+
cv.GaussianBlur(rgb, colour, new cv.Size(5, 5), 0);
|
|
60
|
+
cv.cvtColor(colour, lab, cv.COLOR_RGB2Lab);
|
|
61
|
+
// Blurred after the grey conversion, not taken from `colour`: the rounding differs,
|
|
62
|
+
// and the weaker receipt edges sit close enough to the Hough threshold to notice.
|
|
63
|
+
cv.cvtColor(rgb, gray, cv.COLOR_RGB2GRAY);
|
|
64
|
+
cv.GaussianBlur(gray, gray, new cv.Size(5, 5), 0);
|
|
65
|
+
// Low thresholds: a receipt on a white table has very little contrast.
|
|
66
|
+
cv.Canny(gray, edges, 20, 60);
|
|
67
|
+
// A tolerant copy for scoring, so a side a pixel or two off its edge still counts.
|
|
68
|
+
let supportKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(5, 5));
|
|
69
|
+
cv.dilate(edges, support, supportKernel);
|
|
70
|
+
supportKernel.delete();
|
|
71
|
+
cv.HoughLinesP(edges, segments, 1, Math.PI / 180, 30, minDim * 0.12, 8);
|
|
72
|
+
let { vertical, horizontal } = groupLines(segments, minDim);
|
|
73
|
+
vertical.push({ theta: 0, rho: 0, weight: 0, border: true }, { theta: 0, rho: w - 1, weight: 0, border: true });
|
|
74
|
+
horizontal.push({ theta: Math.PI / 2, rho: 0, weight: 0, border: true }, { theta: Math.PI / 2, rho: h - 1, weight: 0, border: true });
|
|
75
|
+
let labData = lab.data;
|
|
76
|
+
let supportData = support.data;
|
|
77
|
+
let labAt = (x, y, channel) => {
|
|
78
|
+
let xi = Math.min(w - 1, Math.max(0, Math.round(x)));
|
|
79
|
+
let yi = Math.min(h - 1, Math.max(0, Math.round(y)));
|
|
80
|
+
return labData[(yi * w + xi) * 3 + channel];
|
|
81
|
+
};
|
|
82
|
+
/** Share of a side that lies on an edge with different surfaces on either side of it. */
|
|
83
|
+
let coverage = (a, b) => {
|
|
84
|
+
let len = distance(a, b);
|
|
85
|
+
let n = Math.max(8, Math.round(len / 3));
|
|
86
|
+
let nx = -(b.y - a.y) / len;
|
|
87
|
+
let ny = (b.x - a.x) / len;
|
|
88
|
+
let hits = 0;
|
|
89
|
+
for (let i = 1; i < n; i++) {
|
|
90
|
+
let px = a.x + ((b.x - a.x) * i) / n;
|
|
91
|
+
let py = a.y + ((b.y - a.y) * i) / n;
|
|
92
|
+
let x = Math.round(px);
|
|
93
|
+
let y = Math.round(py);
|
|
94
|
+
if (x < 0 || y < 0 || x >= w || y >= h || !supportData[y * w + x])
|
|
95
|
+
continue;
|
|
96
|
+
let diff = [0, 0, 0];
|
|
97
|
+
let count = 0;
|
|
98
|
+
for (let d = SAMPLE_OFFSET_MIN; d <= SAMPLE_OFFSET_MAX; d += 2) {
|
|
99
|
+
for (let c = 0; c < 3; c++) {
|
|
100
|
+
diff[c] += labAt(px + nx * d, py + ny * d, c) - labAt(px - nx * d, py - ny * d, c);
|
|
101
|
+
}
|
|
102
|
+
count++;
|
|
45
103
|
}
|
|
104
|
+
// Chroma weighs double: a cream receipt on a grey-white table differs mostly in tint.
|
|
105
|
+
if (Math.hypot(diff[0] / count, (2 * diff[1]) / count, (2 * diff[2]) / count) > MIN_CONTRAST)
|
|
106
|
+
hits++;
|
|
46
107
|
}
|
|
47
|
-
|
|
48
|
-
|
|
108
|
+
return hits / (n - 1);
|
|
109
|
+
};
|
|
110
|
+
let margin = minDim * 0.02;
|
|
111
|
+
let inBounds = (p) => p !== null && p.x >= -margin && p.y >= -margin && p.x <= w + margin && p.y <= h + margin;
|
|
112
|
+
let minGap = minDim * 0.1;
|
|
113
|
+
let best = null;
|
|
114
|
+
let bestScore = -Infinity;
|
|
115
|
+
for (let left of vertical) {
|
|
116
|
+
for (let right of vertical) {
|
|
117
|
+
if (xAt(left, h / 2) >= xAt(right, h / 2) - minGap)
|
|
118
|
+
continue;
|
|
119
|
+
for (let top of horizontal) {
|
|
120
|
+
for (let bottom of horizontal) {
|
|
121
|
+
if (yAt(top, w / 2) >= yAt(bottom, w / 2) - minGap)
|
|
122
|
+
continue;
|
|
123
|
+
let sides = [top, right, bottom, left];
|
|
124
|
+
if (sides.filter((l) => l.border).length > 2)
|
|
125
|
+
continue;
|
|
126
|
+
let corners = [
|
|
127
|
+
intersect(left, top),
|
|
128
|
+
intersect(right, top),
|
|
129
|
+
intersect(right, bottom),
|
|
130
|
+
intersect(left, bottom),
|
|
131
|
+
];
|
|
132
|
+
if (!corners.every(inBounds))
|
|
133
|
+
continue;
|
|
134
|
+
let quad = corners;
|
|
135
|
+
if (polygonArea(quad) < w * h * MIN_AREA_FRACTION || !isConvex(quad))
|
|
136
|
+
continue;
|
|
137
|
+
let score = 0;
|
|
138
|
+
for (let s = 0; s < 4; s++) {
|
|
139
|
+
let a = quad[s];
|
|
140
|
+
let b = quad[(s + 1) % 4];
|
|
141
|
+
let len = distance(a, b);
|
|
142
|
+
if (sides[s].border) {
|
|
143
|
+
score -= BORDER_COST * len;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
let cov = coverage(a, b);
|
|
147
|
+
score += len * (cov - UNSUPPORTED_COST * (1 - cov));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
score /= w + h;
|
|
151
|
+
if (score > bestScore) {
|
|
152
|
+
bestScore = score;
|
|
153
|
+
best = quad;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
49
157
|
}
|
|
50
|
-
contour.delete();
|
|
51
158
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
159
|
+
if (!best || bestScore < MIN_SCORE)
|
|
160
|
+
return null;
|
|
161
|
+
return best.map((p) => ({
|
|
162
|
+
x: Math.min(src.cols, Math.max(0, p.x / scale)),
|
|
163
|
+
y: Math.min(src.rows, Math.max(0, p.y / scale)),
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
for (let mat of [small, rgb, colour, lab, gray, edges, support, segments])
|
|
59
168
|
mat.delete();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Merges Hough segments that lie on the same line and splits the result into near-vertical
|
|
173
|
+
* and near-horizontal families, each ordered by total segment length.
|
|
174
|
+
*/
|
|
175
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
176
|
+
function groupLines(segments, minDim) {
|
|
177
|
+
let lines = [];
|
|
178
|
+
let maxAngle = (3 * Math.PI) / 180;
|
|
179
|
+
let maxOffset = minDim * 0.015;
|
|
180
|
+
for (let i = 0; i < segments.rows; i++) {
|
|
181
|
+
let [x1, y1, x2, y2] = segments.data32S.slice(i * 4, i * 4 + 4);
|
|
182
|
+
let len = Math.hypot(x2 - x1, y2 - y1);
|
|
183
|
+
// Normal of the segment, folded into [0, π)
|
|
184
|
+
let theta = Math.atan2(x1 - x2, y2 - y1);
|
|
185
|
+
if (theta < 0)
|
|
186
|
+
theta += Math.PI;
|
|
187
|
+
if (theta >= Math.PI)
|
|
188
|
+
theta -= Math.PI;
|
|
189
|
+
let rho = x1 * Math.cos(theta) + y1 * Math.sin(theta);
|
|
190
|
+
let match = lines.find((l) => Math.abs(l.theta - theta) < maxAngle && Math.abs(l.rho - rho) < maxOffset);
|
|
191
|
+
if (match) {
|
|
192
|
+
let total = match.weight + len;
|
|
193
|
+
match.theta = (match.theta * match.weight + theta * len) / total;
|
|
194
|
+
match.rho = (match.rho * match.weight + rho * len) / total;
|
|
195
|
+
match.weight = total;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
lines.push({ theta, rho, weight: len });
|
|
68
199
|
}
|
|
69
|
-
return quad;
|
|
70
200
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
201
|
+
lines.sort((a, b) => b.weight - a.weight);
|
|
202
|
+
let isVertical = (l) => l.theta < Math.PI / 4 || l.theta > (3 * Math.PI) / 4;
|
|
203
|
+
return {
|
|
204
|
+
vertical: lines.filter(isVertical).slice(0, MAX_LINES_PER_FAMILY),
|
|
205
|
+
horizontal: lines.filter((l) => !isVertical(l)).slice(0, MAX_LINES_PER_FAMILY),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/** x where a near-vertical line crosses the given y. */
|
|
209
|
+
function xAt(line, y) {
|
|
210
|
+
return (line.rho - y * Math.sin(line.theta)) / Math.cos(line.theta);
|
|
211
|
+
}
|
|
212
|
+
/** y where a near-horizontal line crosses the given x. */
|
|
213
|
+
function yAt(line, x) {
|
|
214
|
+
return (line.rho - x * Math.cos(line.theta)) / Math.sin(line.theta);
|
|
215
|
+
}
|
|
216
|
+
function intersect(a, b) {
|
|
217
|
+
let ca = Math.cos(a.theta);
|
|
218
|
+
let sa = Math.sin(a.theta);
|
|
219
|
+
let cb = Math.cos(b.theta);
|
|
220
|
+
let sb = Math.sin(b.theta);
|
|
221
|
+
let det = ca * sb - sa * cb;
|
|
222
|
+
if (Math.abs(det) < 1e-6)
|
|
223
|
+
return null;
|
|
224
|
+
return { x: (a.rho * sb - b.rho * sa) / det, y: (ca * b.rho - cb * a.rho) / det };
|
|
225
|
+
}
|
|
226
|
+
function distance(a, b) {
|
|
227
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
228
|
+
}
|
|
229
|
+
function polygonArea(points) {
|
|
230
|
+
let area = 0;
|
|
231
|
+
for (let i = 0; i < points.length; i++) {
|
|
232
|
+
let p = points[i];
|
|
233
|
+
let q = points[(i + 1) % points.length];
|
|
234
|
+
area += p.x * q.y - q.x * p.y;
|
|
235
|
+
}
|
|
236
|
+
return Math.abs(area) / 2;
|
|
237
|
+
}
|
|
238
|
+
function isConvex(points) {
|
|
239
|
+
let sign = 0;
|
|
240
|
+
for (let i = 0; i < points.length; i++) {
|
|
241
|
+
let a = points[i];
|
|
242
|
+
let b = points[(i + 1) % points.length];
|
|
243
|
+
let c = points[(i + 2) % points.length];
|
|
244
|
+
let s = Math.sign((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x));
|
|
245
|
+
if (s === 0)
|
|
246
|
+
continue;
|
|
247
|
+
if (sign && s !== sign)
|
|
248
|
+
return false;
|
|
249
|
+
sign = s;
|
|
78
250
|
}
|
|
251
|
+
return true;
|
|
79
252
|
}
|
|
80
253
|
/**
|
|
81
|
-
*
|
|
82
|
-
* Falls back to full-image corners if no
|
|
254
|
+
* Suggests document corners for the image on `canvas`.
|
|
255
|
+
* Falls back to full-image corners if no document outline is found.
|
|
256
|
+
*
|
|
257
|
+
* `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
|
|
258
|
+
* selection border, is detected as an edge like any other.
|
|
83
259
|
*/
|
|
260
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
84
261
|
export function useEdgeDetection(cv, canvas, enabled) {
|
|
85
262
|
let [corners, setCorners] = useState(null);
|
|
86
263
|
useEffect(() => {
|
|
@@ -90,7 +267,7 @@ export function useEdgeDetection(cv, canvas, enabled) {
|
|
|
90
267
|
let src = cv.imread(canvas);
|
|
91
268
|
let quad;
|
|
92
269
|
try {
|
|
93
|
-
quad =
|
|
270
|
+
quad = findDocumentQuad(cv, src);
|
|
94
271
|
}
|
|
95
272
|
finally {
|
|
96
273
|
src.delete();
|
|
@@ -2,9 +2,28 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { Document, Page, pdfjs } from "react-pdf";
|
|
4
4
|
import "./PdfViewer.css";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/*
|
|
6
|
+
* pdf.js parses in a Web Worker that must be loaded from a URL on the page's origin. On the
|
|
7
|
+
* 2026 templates pdfjs-dist comes from the platform bundle through the import map, and the
|
|
8
|
+
* platform serves the worker next to it - so resolve the specifier the way the browser does
|
|
9
|
+
* and step up one level. This makes the viewer platform-only: off the platform (a legacy
|
|
10
|
+
* template bundling this package) the specifier does not resolve, pdf.js logs a warning and
|
|
11
|
+
* parses on the main thread. A `new URL(..., import.meta.url)` fallback would make Vite emit
|
|
12
|
+
* the 1.2 MB worker into every consumer's build output without anything ever serving it.
|
|
13
|
+
*/
|
|
14
|
+
function platformWorkerUrl() {
|
|
15
|
+
try {
|
|
16
|
+
return new URL("../pdf.worker.min.js", import.meta.resolve("pdfjs-dist")).toString();
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
console.warn("PdfViewer: pdfjs-dist is not on the page's import map; pdf.js will parse on the main thread.");
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let workerUrl = platformWorkerUrl();
|
|
24
|
+
if (workerUrl) {
|
|
25
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
26
|
+
}
|
|
8
27
|
export function PdfViewer({ url, file }) {
|
|
9
28
|
let [numPages, setNumPages] = useState(0);
|
|
10
29
|
let [error, setError] = useState(null);
|
|
@@ -30,6 +30,25 @@ function findTarget(ids) {
|
|
|
30
30
|
}
|
|
31
31
|
return null;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The 2026 template reserves the drawer slot only for articles whose deploy wrote the
|
|
35
|
+
* `ToolbarDrawer` block (the build detects `ToolbarDrawerToggle` in the app). An app with a
|
|
36
|
+
* drawer that has not been deployed since renders on a toolbar without the slot: create it in
|
|
37
|
+
* front of the app region and drop the no-drawer layout class, so the toggle still mounts. The
|
|
38
|
+
* toolbar shifts once when that happens, which the block avoids on the next deploy.
|
|
39
|
+
*/
|
|
40
|
+
function createDrawerSlot() {
|
|
41
|
+
let toolbar = document.querySelector(".ob-toolbar");
|
|
42
|
+
let lead = toolbar?.querySelector(".ob-toolbar__lead");
|
|
43
|
+
if (!toolbar || !lead)
|
|
44
|
+
return null;
|
|
45
|
+
let slot = document.createElement("div");
|
|
46
|
+
slot.id = DRAWER_SLOT_IDS[0];
|
|
47
|
+
slot.className = "ob-toolbar__drawer";
|
|
48
|
+
lead.prepend(slot);
|
|
49
|
+
toolbar.classList.remove("ob-toolbar--no-drawer");
|
|
50
|
+
return slot;
|
|
51
|
+
}
|
|
33
52
|
/**
|
|
34
53
|
* Renders its children into the toolbar's app region. Render `ToolbarBreadcrumbs` before any
|
|
35
54
|
* other `ToolbarPortal`, since the region lays its children out in mount order with the first
|
|
@@ -47,7 +66,7 @@ export function ToolbarPortal({ children }) {
|
|
|
47
66
|
* contrast colour on every brand. Renders nothing on a template without a toolbar.
|
|
48
67
|
*/
|
|
49
68
|
export function ToolbarDrawerToggle({ open, onToggle, label }) {
|
|
50
|
-
let target = findTarget(DRAWER_SLOT_IDS);
|
|
69
|
+
let target = findTarget(DRAWER_SLOT_IDS) ?? createDrawerSlot();
|
|
51
70
|
if (!target)
|
|
52
71
|
return null;
|
|
53
72
|
return createPortal(_jsx("button", { type: "button", className: "ob-toolbar__icon-button", "aria-label": label ?? getLocalizedString("Toggle menu"), "aria-expanded": open, onClick: onToggle, children: _jsx(MenuHamburgerIcon, { "aria-hidden": true, fontSize: "1.5rem" }) }), target);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -48,17 +48,17 @@
|
|
|
48
48
|
"filepond-plugin-image-transform": "^3.8.8",
|
|
49
49
|
"localforage": "^1.10.0",
|
|
50
50
|
"mitt": "^3.0.1",
|
|
51
|
-
"pdf-lib": "
|
|
51
|
+
"pdf-lib": "1.17.1",
|
|
52
52
|
"pdfjs-dist": "5.7.284",
|
|
53
53
|
"react": "19.3.0",
|
|
54
54
|
"react-dom": "19.3.0",
|
|
55
|
-
"react-pdf": "
|
|
55
|
+
"react-pdf": "10.5.0",
|
|
56
56
|
"zxing-wasm": "3.1.4",
|
|
57
|
+
"@olenbetong/appframe-ds": "1.3.0",
|
|
57
58
|
"@olenbetong/appframe-core": "2.14.0",
|
|
58
59
|
"@olenbetong/appframe-data": "1.7.2",
|
|
59
60
|
"@olenbetong/appframe-mui": "7.0.3",
|
|
60
|
-
"@olenbetong/appframe-react": "2.2.1"
|
|
61
|
-
"@olenbetong/appframe-ds": "1.1.0"
|
|
61
|
+
"@olenbetong/appframe-react": "2.2.1"
|
|
62
62
|
},
|
|
63
63
|
"repository": {
|
|
64
64
|
"type": "git",
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
]
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@digdir/designsystemet-react": "1.
|
|
75
|
+
"@digdir/designsystemet-react": "1.22.0",
|
|
76
76
|
"@navikt/aksel-icons": "8.16.2",
|
|
77
|
-
"@testing-library/dom": "^10.4.
|
|
77
|
+
"@testing-library/dom": "^10.4.2",
|
|
78
78
|
"@testing-library/jest-dom": "^7.0.1",
|
|
79
79
|
"@testing-library/react": "16.3.3",
|
|
80
80
|
"@testing-library/user-event": "^14.6.7",
|
|
@@ -106,7 +106,9 @@ export function useCropTool(imageUrl: string, enabled: boolean): CropToolState {
|
|
|
106
106
|
let scale = displaySize.width > 0 ? displaySize.width / canvasPixelSize.width : 1;
|
|
107
107
|
|
|
108
108
|
let handleCanvasReady = useCallback((canvas: HTMLCanvasElement, w: number, h: number) => {
|
|
109
|
-
|
|
109
|
+
// Detect on a bare copy: the displayed canvas carries the selection border, which
|
|
110
|
+
// starts out along the image edges and would be found as the document outline.
|
|
111
|
+
setCanvasEl(canvasRef.current?.getCleanCanvas() ?? null);
|
|
110
112
|
setCanvasPixelSize({ width: w, height: h });
|
|
111
113
|
setDisplaySize({ width: canvas.clientWidth, height: canvas.clientHeight });
|
|
112
114
|
}, []);
|
|
@@ -17,81 +17,266 @@ export function fullImageQuad(width: number, height: number): Quad {
|
|
|
17
17
|
];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
/** Longest side of the downscaled working image. Detection is scale-dependent, so it is fixed. */
|
|
21
|
+
const WORK_SIZE = 512;
|
|
22
|
+
/** Closing kernel that erases dark print (text, thin rules) so only surface boundaries remain. */
|
|
23
|
+
const CLOSE_KERNEL = 5;
|
|
24
|
+
/** Candidate lines kept per orientation family. The search is O(n⁴), so keep this small. */
|
|
25
|
+
const MAX_LINES_PER_FAMILY = 14;
|
|
26
|
+
/** Distances (working pixels) sampled on each side of a candidate edge. */
|
|
27
|
+
const SAMPLE_OFFSET_MIN = 3;
|
|
28
|
+
const SAMPLE_OFFSET_MAX = 9;
|
|
29
|
+
/** Minimum Lab colour difference between the two sides for an edge sample to count. */
|
|
30
|
+
const MIN_CONTRAST = 8;
|
|
31
|
+
/** Cost of an unsupported stretch of side, relative to the gain of a supported one. */
|
|
32
|
+
const UNSUPPORTED_COST = 0.5;
|
|
33
|
+
/** Cost per pixel of using the image border as a side (document running off the frame). */
|
|
34
|
+
const BORDER_COST = 0.1;
|
|
35
|
+
/** Minimum normalised score for a suggestion; below it the full image is used. */
|
|
36
|
+
const MIN_SCORE = 0.3;
|
|
37
|
+
const MIN_AREA_FRACTION = 0.1;
|
|
38
|
+
|
|
39
|
+
/** A line in normal form: x·cosθ + y·sinθ = ρ. */
|
|
40
|
+
interface Line {
|
|
41
|
+
theta: number;
|
|
42
|
+
rho: number;
|
|
43
|
+
weight: number;
|
|
44
|
+
border?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Finds the most likely document outline in an RGBA image.
|
|
49
|
+
*
|
|
50
|
+
* Print is first removed with a morphological close, so text and table rules do not
|
|
51
|
+
* compete with the paper's outline. Straight segments from a Hough transform are merged
|
|
52
|
+
* into candidate lines, and every combination of two near-vertical and two near-horizontal
|
|
53
|
+
* lines (the image borders included, for receipts that run off the photo) is scored by how
|
|
54
|
+
* much of each side both lies on an edge and separates two differently coloured surfaces.
|
|
55
|
+
* Scoring by supported length, rather than by the share of each side, lets a long receipt
|
|
56
|
+
* whose edges bend at a fold beat a small, perfectly straight section of it.
|
|
57
|
+
*/
|
|
58
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
59
|
+
export function findDocumentQuad(cv: any, src: any): Quad | null {
|
|
60
|
+
let scale = Math.min(1, WORK_SIZE / Math.max(src.cols, src.rows));
|
|
61
|
+
let small = new cv.Mat();
|
|
62
|
+
let rgb = new cv.Mat();
|
|
63
|
+
let colour = new cv.Mat();
|
|
64
|
+
let lab = new cv.Mat();
|
|
21
65
|
let gray = new cv.Mat();
|
|
22
|
-
let blurred = new cv.Mat();
|
|
23
66
|
let edges = new cv.Mat();
|
|
24
|
-
let
|
|
25
|
-
let
|
|
26
|
-
let hierarchy = new cv.Mat();
|
|
67
|
+
let support = new cv.Mat();
|
|
68
|
+
let segments = new cv.Mat();
|
|
27
69
|
|
|
28
70
|
try {
|
|
29
|
-
cv.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
let
|
|
36
|
-
cv.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
cv.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
71
|
+
cv.resize(src, small, new cv.Size(Math.round(src.cols * scale), Math.round(src.rows * scale)), 0, 0, cv.INTER_AREA);
|
|
72
|
+
let w = small.cols;
|
|
73
|
+
let h = small.rows;
|
|
74
|
+
let minDim = Math.min(w, h);
|
|
75
|
+
|
|
76
|
+
cv.cvtColor(small, rgb, cv.COLOR_RGBA2RGB);
|
|
77
|
+
let closeKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(CLOSE_KERNEL, CLOSE_KERNEL));
|
|
78
|
+
cv.morphologyEx(rgb, rgb, cv.MORPH_CLOSE, closeKernel);
|
|
79
|
+
closeKernel.delete();
|
|
80
|
+
cv.GaussianBlur(rgb, colour, new cv.Size(5, 5), 0);
|
|
81
|
+
cv.cvtColor(colour, lab, cv.COLOR_RGB2Lab);
|
|
82
|
+
// Blurred after the grey conversion, not taken from `colour`: the rounding differs,
|
|
83
|
+
// and the weaker receipt edges sit close enough to the Hough threshold to notice.
|
|
84
|
+
cv.cvtColor(rgb, gray, cv.COLOR_RGB2GRAY);
|
|
85
|
+
cv.GaussianBlur(gray, gray, new cv.Size(5, 5), 0);
|
|
86
|
+
|
|
87
|
+
// Low thresholds: a receipt on a white table has very little contrast.
|
|
88
|
+
cv.Canny(gray, edges, 20, 60);
|
|
89
|
+
// A tolerant copy for scoring, so a side a pixel or two off its edge still counts.
|
|
90
|
+
let supportKernel = cv.getStructuringElement(cv.MORPH_RECT, new cv.Size(5, 5));
|
|
91
|
+
cv.dilate(edges, support, supportKernel);
|
|
92
|
+
supportKernel.delete();
|
|
93
|
+
cv.HoughLinesP(edges, segments, 1, Math.PI / 180, 30, minDim * 0.12, 8);
|
|
94
|
+
|
|
95
|
+
let { vertical, horizontal } = groupLines(segments, minDim);
|
|
96
|
+
vertical.push({ theta: 0, rho: 0, weight: 0, border: true }, { theta: 0, rho: w - 1, weight: 0, border: true });
|
|
97
|
+
horizontal.push(
|
|
98
|
+
{ theta: Math.PI / 2, rho: 0, weight: 0, border: true },
|
|
99
|
+
{ theta: Math.PI / 2, rho: h - 1, weight: 0, border: true },
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
let labData: Uint8Array = lab.data;
|
|
103
|
+
let supportData: Uint8Array = support.data;
|
|
104
|
+
let labAt = (x: number, y: number, channel: number) => {
|
|
105
|
+
let xi = Math.min(w - 1, Math.max(0, Math.round(x)));
|
|
106
|
+
let yi = Math.min(h - 1, Math.max(0, Math.round(y)));
|
|
107
|
+
return labData[(yi * w + xi) * 3 + channel];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/** Share of a side that lies on an edge with different surfaces on either side of it. */
|
|
111
|
+
let coverage = (a: Point, b: Point) => {
|
|
112
|
+
let len = distance(a, b);
|
|
113
|
+
let n = Math.max(8, Math.round(len / 3));
|
|
114
|
+
let nx = -(b.y - a.y) / len;
|
|
115
|
+
let ny = (b.x - a.x) / len;
|
|
116
|
+
let hits = 0;
|
|
117
|
+
for (let i = 1; i < n; i++) {
|
|
118
|
+
let px = a.x + ((b.x - a.x) * i) / n;
|
|
119
|
+
let py = a.y + ((b.y - a.y) * i) / n;
|
|
120
|
+
let x = Math.round(px);
|
|
121
|
+
let y = Math.round(py);
|
|
122
|
+
if (x < 0 || y < 0 || x >= w || y >= h || !supportData[y * w + x]) continue;
|
|
123
|
+
|
|
124
|
+
let diff = [0, 0, 0];
|
|
125
|
+
let count = 0;
|
|
126
|
+
for (let d = SAMPLE_OFFSET_MIN; d <= SAMPLE_OFFSET_MAX; d += 2) {
|
|
127
|
+
for (let c = 0; c < 3; c++) {
|
|
128
|
+
diff[c] += labAt(px + nx * d, py + ny * d, c) - labAt(px - nx * d, py - ny * d, c);
|
|
129
|
+
}
|
|
130
|
+
count++;
|
|
60
131
|
}
|
|
61
|
-
|
|
62
|
-
|
|
132
|
+
// Chroma weighs double: a cream receipt on a grey-white table differs mostly in tint.
|
|
133
|
+
if (Math.hypot(diff[0] / count, (2 * diff[1]) / count, (2 * diff[2]) / count) > MIN_CONTRAST) hits++;
|
|
63
134
|
}
|
|
64
|
-
|
|
65
|
-
}
|
|
135
|
+
return hits / (n - 1);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
let margin = minDim * 0.02;
|
|
139
|
+
let inBounds = (p: Point | null): p is Point =>
|
|
140
|
+
p !== null && p.x >= -margin && p.y >= -margin && p.x <= w + margin && p.y <= h + margin;
|
|
141
|
+
let minGap = minDim * 0.1;
|
|
142
|
+
|
|
143
|
+
let best: Quad | null = null;
|
|
144
|
+
let bestScore = -Infinity;
|
|
145
|
+
for (let left of vertical) {
|
|
146
|
+
for (let right of vertical) {
|
|
147
|
+
if (xAt(left, h / 2) >= xAt(right, h / 2) - minGap) continue;
|
|
148
|
+
for (let top of horizontal) {
|
|
149
|
+
for (let bottom of horizontal) {
|
|
150
|
+
if (yAt(top, w / 2) >= yAt(bottom, w / 2) - minGap) continue;
|
|
151
|
+
let sides = [top, right, bottom, left];
|
|
152
|
+
if (sides.filter((l) => l.border).length > 2) continue;
|
|
153
|
+
|
|
154
|
+
let corners = [
|
|
155
|
+
intersect(left, top),
|
|
156
|
+
intersect(right, top),
|
|
157
|
+
intersect(right, bottom),
|
|
158
|
+
intersect(left, bottom),
|
|
159
|
+
];
|
|
160
|
+
if (!corners.every(inBounds)) continue;
|
|
161
|
+
let quad = corners as Quad;
|
|
162
|
+
if (polygonArea(quad) < w * h * MIN_AREA_FRACTION || !isConvex(quad)) continue;
|
|
66
163
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
164
|
+
let score = 0;
|
|
165
|
+
for (let s = 0; s < 4; s++) {
|
|
166
|
+
let a = quad[s];
|
|
167
|
+
let b = quad[(s + 1) % 4];
|
|
168
|
+
let len = distance(a, b);
|
|
169
|
+
if (sides[s].border) {
|
|
170
|
+
score -= BORDER_COST * len;
|
|
171
|
+
} else {
|
|
172
|
+
let cov = coverage(a, b);
|
|
173
|
+
score += len * (cov - UNSUPPORTED_COST * (1 - cov));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
score /= w + h;
|
|
177
|
+
if (score > bestScore) {
|
|
178
|
+
bestScore = score;
|
|
179
|
+
best = quad;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
73
183
|
}
|
|
74
|
-
mat.delete();
|
|
75
|
-
|
|
76
|
-
// Sort into TL, TR, BR, BL
|
|
77
|
-
pts.sort((a, b) => a.x + a.y - (b.x + b.y));
|
|
78
|
-
let tl = pts[0];
|
|
79
|
-
let br = pts[3];
|
|
80
|
-
let remaining = [pts[1], pts[2]];
|
|
81
|
-
let tr = remaining[0].x > remaining[1].x ? remaining[0] : remaining[1];
|
|
82
|
-
let bl = remaining[0].x < remaining[1].x ? remaining[0] : remaining[1];
|
|
83
|
-
quad = [tl, tr, br, bl];
|
|
84
184
|
}
|
|
85
185
|
|
|
86
|
-
return
|
|
186
|
+
if (!best || bestScore < MIN_SCORE) return null;
|
|
187
|
+
return best.map((p) => ({
|
|
188
|
+
x: Math.min(src.cols, Math.max(0, p.x / scale)),
|
|
189
|
+
y: Math.min(src.rows, Math.max(0, p.y / scale)),
|
|
190
|
+
})) as Quad;
|
|
87
191
|
} finally {
|
|
88
|
-
gray.delete();
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
192
|
+
for (let mat of [small, rgb, colour, lab, gray, edges, support, segments]) mat.delete();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Merges Hough segments that lie on the same line and splits the result into near-vertical
|
|
198
|
+
* and near-horizontal families, each ordered by total segment length.
|
|
199
|
+
*/
|
|
200
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
201
|
+
function groupLines(segments: any, minDim: number): { vertical: Line[]; horizontal: Line[] } {
|
|
202
|
+
let lines: Line[] = [];
|
|
203
|
+
let maxAngle = (3 * Math.PI) / 180;
|
|
204
|
+
let maxOffset = minDim * 0.015;
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < segments.rows; i++) {
|
|
207
|
+
let [x1, y1, x2, y2] = segments.data32S.slice(i * 4, i * 4 + 4);
|
|
208
|
+
let len = Math.hypot(x2 - x1, y2 - y1);
|
|
209
|
+
// Normal of the segment, folded into [0, π)
|
|
210
|
+
let theta = Math.atan2(x1 - x2, y2 - y1);
|
|
211
|
+
if (theta < 0) theta += Math.PI;
|
|
212
|
+
if (theta >= Math.PI) theta -= Math.PI;
|
|
213
|
+
let rho = x1 * Math.cos(theta) + y1 * Math.sin(theta);
|
|
214
|
+
|
|
215
|
+
let match = lines.find((l) => Math.abs(l.theta - theta) < maxAngle && Math.abs(l.rho - rho) < maxOffset);
|
|
216
|
+
if (match) {
|
|
217
|
+
let total = match.weight + len;
|
|
218
|
+
match.theta = (match.theta * match.weight + theta * len) / total;
|
|
219
|
+
match.rho = (match.rho * match.weight + rho * len) / total;
|
|
220
|
+
match.weight = total;
|
|
221
|
+
} else {
|
|
222
|
+
lines.push({ theta, rho, weight: len });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
lines.sort((a, b) => b.weight - a.weight);
|
|
227
|
+
let isVertical = (l: Line) => l.theta < Math.PI / 4 || l.theta > (3 * Math.PI) / 4;
|
|
228
|
+
return {
|
|
229
|
+
vertical: lines.filter(isVertical).slice(0, MAX_LINES_PER_FAMILY),
|
|
230
|
+
horizontal: lines.filter((l) => !isVertical(l)).slice(0, MAX_LINES_PER_FAMILY),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** x where a near-vertical line crosses the given y. */
|
|
235
|
+
function xAt(line: Line, y: number): number {
|
|
236
|
+
return (line.rho - y * Math.sin(line.theta)) / Math.cos(line.theta);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** y where a near-horizontal line crosses the given x. */
|
|
240
|
+
function yAt(line: Line, x: number): number {
|
|
241
|
+
return (line.rho - x * Math.cos(line.theta)) / Math.sin(line.theta);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function intersect(a: Line, b: Line): Point | null {
|
|
245
|
+
let ca = Math.cos(a.theta);
|
|
246
|
+
let sa = Math.sin(a.theta);
|
|
247
|
+
let cb = Math.cos(b.theta);
|
|
248
|
+
let sb = Math.sin(b.theta);
|
|
249
|
+
let det = ca * sb - sa * cb;
|
|
250
|
+
if (Math.abs(det) < 1e-6) return null;
|
|
251
|
+
return { x: (a.rho * sb - b.rho * sa) / det, y: (ca * b.rho - cb * a.rho) / det };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function distance(a: Point, b: Point): number {
|
|
255
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function polygonArea(points: Point[]): number {
|
|
259
|
+
let area = 0;
|
|
260
|
+
for (let i = 0; i < points.length; i++) {
|
|
261
|
+
let p = points[i];
|
|
262
|
+
let q = points[(i + 1) % points.length];
|
|
263
|
+
area += p.x * q.y - q.x * p.y;
|
|
264
|
+
}
|
|
265
|
+
return Math.abs(area) / 2;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function isConvex(points: Point[]): boolean {
|
|
269
|
+
let sign = 0;
|
|
270
|
+
for (let i = 0; i < points.length; i++) {
|
|
271
|
+
let a = points[i];
|
|
272
|
+
let b = points[(i + 1) % points.length];
|
|
273
|
+
let c = points[(i + 2) % points.length];
|
|
274
|
+
let s = Math.sign((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x));
|
|
275
|
+
if (s === 0) continue;
|
|
276
|
+
if (sign && s !== sign) return false;
|
|
277
|
+
sign = s;
|
|
94
278
|
}
|
|
279
|
+
return true;
|
|
95
280
|
}
|
|
96
281
|
|
|
97
282
|
interface UseEdgeDetectionResult {
|
|
@@ -99,9 +284,13 @@ interface UseEdgeDetectionResult {
|
|
|
99
284
|
}
|
|
100
285
|
|
|
101
286
|
/**
|
|
102
|
-
*
|
|
103
|
-
* Falls back to full-image corners if no
|
|
287
|
+
* Suggests document corners for the image on `canvas`.
|
|
288
|
+
* Falls back to full-image corners if no document outline is found.
|
|
289
|
+
*
|
|
290
|
+
* `canvas` must hold the bare image: anything drawn over it, such as the crop overlay's
|
|
291
|
+
* selection border, is detected as an edge like any other.
|
|
104
292
|
*/
|
|
293
|
+
// oxlint-disable-next-line typescript/no-explicit-any -- OpenCV.js has no bundled types
|
|
105
294
|
export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enabled: boolean): UseEdgeDetectionResult {
|
|
106
295
|
let [corners, setCorners] = useState<Quad | null>(null);
|
|
107
296
|
|
|
@@ -112,7 +301,7 @@ export function useEdgeDetection(cv: any, canvas: HTMLCanvasElement | null, enab
|
|
|
112
301
|
let src = cv.imread(canvas);
|
|
113
302
|
let quad: Quad | null;
|
|
114
303
|
try {
|
|
115
|
-
quad =
|
|
304
|
+
quad = findDocumentQuad(cv, src);
|
|
116
305
|
} finally {
|
|
117
306
|
src.delete();
|
|
118
307
|
}
|
|
@@ -2,9 +2,28 @@ import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
2
2
|
import { Document, Page, pdfjs } from "react-pdf";
|
|
3
3
|
import "./PdfViewer.css";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/*
|
|
6
|
+
* pdf.js parses in a Web Worker that must be loaded from a URL on the page's origin. On the
|
|
7
|
+
* 2026 templates pdfjs-dist comes from the platform bundle through the import map, and the
|
|
8
|
+
* platform serves the worker next to it - so resolve the specifier the way the browser does
|
|
9
|
+
* and step up one level. This makes the viewer platform-only: off the platform (a legacy
|
|
10
|
+
* template bundling this package) the specifier does not resolve, pdf.js logs a warning and
|
|
11
|
+
* parses on the main thread. A `new URL(..., import.meta.url)` fallback would make Vite emit
|
|
12
|
+
* the 1.2 MB worker into every consumer's build output without anything ever serving it.
|
|
13
|
+
*/
|
|
14
|
+
function platformWorkerUrl(): string | undefined {
|
|
15
|
+
try {
|
|
16
|
+
return new URL("../pdf.worker.min.js", import.meta.resolve("pdfjs-dist")).toString();
|
|
17
|
+
} catch {
|
|
18
|
+
console.warn("PdfViewer: pdfjs-dist is not on the page's import map; pdf.js will parse on the main thread.");
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let workerUrl = platformWorkerUrl();
|
|
24
|
+
if (workerUrl) {
|
|
25
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
|
26
|
+
}
|
|
8
27
|
|
|
9
28
|
type PdfViewerProps = { url?: string; file?: string | Blob | ArrayBuffer };
|
|
10
29
|
|
|
@@ -32,6 +32,26 @@ function findTarget(ids: string[]) {
|
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The 2026 template reserves the drawer slot only for articles whose deploy wrote the
|
|
37
|
+
* `ToolbarDrawer` block (the build detects `ToolbarDrawerToggle` in the app). An app with a
|
|
38
|
+
* drawer that has not been deployed since renders on a toolbar without the slot: create it in
|
|
39
|
+
* front of the app region and drop the no-drawer layout class, so the toggle still mounts. The
|
|
40
|
+
* toolbar shifts once when that happens, which the block avoids on the next deploy.
|
|
41
|
+
*/
|
|
42
|
+
function createDrawerSlot() {
|
|
43
|
+
let toolbar = document.querySelector<HTMLElement>(".ob-toolbar");
|
|
44
|
+
let lead = toolbar?.querySelector<HTMLElement>(".ob-toolbar__lead");
|
|
45
|
+
if (!toolbar || !lead) return null;
|
|
46
|
+
|
|
47
|
+
let slot = document.createElement("div");
|
|
48
|
+
slot.id = DRAWER_SLOT_IDS[0];
|
|
49
|
+
slot.className = "ob-toolbar__drawer";
|
|
50
|
+
lead.prepend(slot);
|
|
51
|
+
toolbar.classList.remove("ob-toolbar--no-drawer");
|
|
52
|
+
return slot;
|
|
53
|
+
}
|
|
54
|
+
|
|
35
55
|
export type ToolbarPortalProps = {
|
|
36
56
|
children: React.ReactNode;
|
|
37
57
|
};
|
|
@@ -61,7 +81,7 @@ export type ToolbarDrawerToggleProps = {
|
|
|
61
81
|
* contrast colour on every brand. Renders nothing on a template without a toolbar.
|
|
62
82
|
*/
|
|
63
83
|
export function ToolbarDrawerToggle({ open, onToggle, label }: ToolbarDrawerToggleProps) {
|
|
64
|
-
let target = findTarget(DRAWER_SLOT_IDS);
|
|
84
|
+
let target = findTarget(DRAWER_SLOT_IDS) ?? createDrawerSlot();
|
|
65
85
|
if (!target) return null;
|
|
66
86
|
|
|
67
87
|
return createPortal(
|