@smileid/web-components 11.0.0 → 11.0.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.
Files changed (141) hide show
  1. package/README.md +15 -15
  2. package/dist/README.md +15 -0
  3. package/dist/components/README.md +14 -0
  4. package/dist/components/document/src/README.md +111 -0
  5. package/dist/components/document/src/document-capture/README.md +90 -0
  6. package/dist/components/document/src/document-capture-instructions/README.md +56 -0
  7. package/dist/components/document/src/document-capture-review/README.md +79 -0
  8. package/dist/components/selfie/README.md +225 -0
  9. package/dist/components/smart-camera-web/src/README.md +207 -0
  10. package/dist/domain/camera/src/README.md +38 -0
  11. package/dist/domain/file-upload/README.md +35 -0
  12. package/dist/esm/{DocumentCaptureScreens-RECPb0wH.js → DocumentCaptureScreens-CRx5ymqY.js} +2 -2
  13. package/dist/esm/DocumentCaptureScreens-CRx5ymqY.js.map +1 -0
  14. package/dist/esm/EndUserConsent-D4fd1ovG.js.map +1 -1
  15. package/dist/esm/Navigation-CTjK6tLU.js.map +1 -1
  16. package/dist/esm/PoweredBySmileId-CxbaihMu.js.map +1 -1
  17. package/dist/esm/{SelfieCaptureScreens-CqBVGEJk.js → SelfieCaptureScreens-DuQjKt_K.js} +274 -257
  18. package/dist/esm/SelfieCaptureScreens-DuQjKt_K.js.map +1 -0
  19. package/dist/esm/SignaturePad-C7MtmT8m.js.map +1 -1
  20. package/dist/esm/TotpConsent-CQU5jQi4.js.map +1 -1
  21. package/dist/esm/combobox.js.map +1 -1
  22. package/dist/esm/document.js +1 -1
  23. package/dist/esm/main.js +2 -2
  24. package/dist/esm/{package-BDJnoIAU.js → package-C_cKNrUu.js} +2 -2
  25. package/dist/esm/package-C_cKNrUu.js.map +1 -0
  26. package/dist/esm/selfie.js +1 -1
  27. package/dist/esm/smart-camera-web.js +3 -3
  28. package/dist/esm/smart-camera-web.js.map +1 -1
  29. package/dist/esm/styles-BOEZtbuc.js.map +1 -1
  30. package/dist/package-lock.json +4948 -0
  31. package/dist/package.json +59 -0
  32. package/dist/smart-camera-web.js +23 -23
  33. package/dist/smart-camera-web.js.gz +0 -0
  34. package/dist/smart-camera-web.js.map +1 -1
  35. package/dist/src/components/combobox/src/index.js +2 -0
  36. package/dist/src/components/combobox/src/index.js.map +7 -0
  37. package/dist/src/components/document/src/index.js +2 -0
  38. package/dist/src/components/document/src/index.js.map +7 -0
  39. package/dist/src/components/end-user-consent/src/index.js +14 -0
  40. package/dist/src/components/end-user-consent/src/index.js.map +7 -0
  41. package/dist/src/components/selfie/src/index.js +2 -0
  42. package/dist/src/components/selfie/src/index.js.map +7 -0
  43. package/dist/src/components/signature-pad/src/index.js +10 -0
  44. package/dist/src/components/signature-pad/src/index.js.map +7 -0
  45. package/dist/src/components/smart-camera-web/src/SmartCameraWeb.js +2 -0
  46. package/dist/src/components/smart-camera-web/src/SmartCameraWeb.js.map +7 -0
  47. package/dist/src/components/totp-consent/src/index.js +14 -0
  48. package/dist/src/components/totp-consent/src/index.js.map +7 -0
  49. package/dist/src/index.js.map +7 -0
  50. package/dist/styles/README.md +3 -0
  51. package/dist/types/combobox.d.ts +19 -19
  52. package/dist/types/document.d.ts +19 -19
  53. package/dist/types/end-user-consent.d.ts +19 -19
  54. package/dist/types/main.d.ts +24 -20
  55. package/dist/types/navigation.d.ts +19 -19
  56. package/dist/types/selfie.d.ts +19 -19
  57. package/dist/types/signature-pad.d.ts +19 -19
  58. package/dist/types/smart-camera-web.d.ts +19 -19
  59. package/dist/types/totp-consent.d.ts +19 -19
  60. package/lib/components/README.md +14 -14
  61. package/lib/components/attribution/PoweredBySmileId.js +42 -42
  62. package/lib/components/camera-permission/CameraPermission.js +139 -139
  63. package/lib/components/camera-permission/CameraPermission.stories.js +27 -27
  64. package/lib/components/combobox/src/Combobox.js +589 -589
  65. package/lib/components/combobox/src/index.js +1 -1
  66. package/lib/components/document/src/DocumentCaptureScreens.js +410 -410
  67. package/lib/components/document/src/DocumentCaptureScreens.stories.js +57 -57
  68. package/lib/components/document/src/README.md +111 -111
  69. package/lib/components/document/src/document-capture/DocumentCapture.js +760 -760
  70. package/lib/components/document/src/document-capture/DocumentCapture.stories.js +78 -78
  71. package/lib/components/document/src/document-capture/README.md +90 -90
  72. package/lib/components/document/src/document-capture/index.js +3 -3
  73. package/lib/components/document/src/document-capture-instructions/DocumentCaptureInstructions.js +545 -545
  74. package/lib/components/document/src/document-capture-instructions/DocumentCaptureInstructions.stories.js +24 -24
  75. package/lib/components/document/src/document-capture-instructions/README.md +56 -56
  76. package/lib/components/document/src/document-capture-instructions/index.js +3 -3
  77. package/lib/components/document/src/document-capture-review/DocumentCaptureReview.js +360 -360
  78. package/lib/components/document/src/document-capture-review/DocumentCaptureReview.stories.js +24 -24
  79. package/lib/components/document/src/document-capture-review/README.md +79 -79
  80. package/lib/components/document/src/document-capture-review/index.js +3 -3
  81. package/lib/components/document/src/index.js +3 -3
  82. package/lib/components/end-user-consent/src/EndUserConsent.js +795 -795
  83. package/lib/components/end-user-consent/src/EndUserConsent.stories.js +29 -29
  84. package/lib/components/end-user-consent/src/index.js +4 -4
  85. package/lib/components/navigation/src/Navigation.js +171 -171
  86. package/lib/components/navigation/src/Navigation.stories.js +24 -24
  87. package/lib/components/navigation/src/index.js +3 -3
  88. package/lib/components/selfie/README.md +225 -225
  89. package/lib/components/selfie/src/SelfieCaptureScreens.js +420 -420
  90. package/lib/components/selfie/src/SelfieCaptureScreens.stories.js +29 -29
  91. package/lib/components/selfie/src/index.js +3 -3
  92. package/lib/components/selfie/src/selfie-capture/SelfieCapture.js +1099 -1099
  93. package/lib/components/selfie/src/selfie-capture/SelfieCapture.stories.js +36 -36
  94. package/lib/components/selfie/src/selfie-capture/index.js +3 -3
  95. package/lib/components/selfie/src/selfie-capture-instructions/SelfieCaptureInstructions.js +689 -689
  96. package/lib/components/selfie/src/selfie-capture-instructions/SelfieCaptureInstructions.stories.js +23 -23
  97. package/lib/components/selfie/src/selfie-capture-instructions/index.js +3 -3
  98. package/lib/components/selfie/src/selfie-capture-review/SelfieCaptureReview.js +209 -209
  99. package/lib/components/selfie/src/selfie-capture-review/SelfieCaptureReview.stories.js +24 -24
  100. package/lib/components/selfie/src/selfie-capture-review/index.js +3 -3
  101. package/lib/components/selfie/src/selfie-capture-wrapper/SelfieCaptureWrapper.tsx +281 -256
  102. package/lib/components/selfie/src/selfie-capture-wrapper/index.ts +1 -1
  103. package/lib/components/selfie/src/smartselfie-capture/OvalProgress.tsx +81 -81
  104. package/lib/components/selfie/src/smartselfie-capture/SmartSelfieCapture.tsx +265 -265
  105. package/lib/components/selfie/src/smartselfie-capture/components/AlertDisplay.tsx +34 -34
  106. package/lib/components/selfie/src/smartselfie-capture/components/CameraPreview.tsx +97 -97
  107. package/lib/components/selfie/src/smartselfie-capture/components/CaptureControls.tsx +78 -78
  108. package/lib/components/selfie/src/smartselfie-capture/components/index.ts +3 -3
  109. package/lib/components/selfie/src/smartselfie-capture/constants.ts +23 -23
  110. package/lib/components/selfie/src/smartselfie-capture/hooks/index.ts +2 -2
  111. package/lib/components/selfie/src/smartselfie-capture/hooks/useCamera.ts +238 -238
  112. package/lib/components/selfie/src/smartselfie-capture/hooks/useFaceCapture.ts +618 -618
  113. package/lib/components/selfie/src/smartselfie-capture/index.ts +1 -1
  114. package/lib/components/selfie/src/smartselfie-capture/utils/alertMessages.ts +13 -13
  115. package/lib/components/selfie/src/smartselfie-capture/utils/canvas.ts +105 -105
  116. package/lib/components/selfie/src/smartselfie-capture/utils/faceDetection.ts +129 -129
  117. package/lib/components/selfie/src/smartselfie-capture/utils/imageCapture.ts +64 -64
  118. package/lib/components/selfie/src/smartselfie-capture/utils/index.ts +4 -4
  119. package/lib/components/selfie/src/smartselfie-capture/utils/mediapipeManager.ts +118 -77
  120. package/lib/components/signature-pad/package-lock.json +3009 -3009
  121. package/lib/components/signature-pad/package.json +30 -30
  122. package/lib/components/signature-pad/src/SignaturePad.js +484 -484
  123. package/lib/components/signature-pad/src/SignaturePad.stories.js +32 -32
  124. package/lib/components/signature-pad/src/index.js +3 -3
  125. package/lib/components/smart-camera-web/src/README.md +206 -206
  126. package/lib/components/smart-camera-web/src/SmartCameraWeb.js +305 -305
  127. package/lib/components/smart-camera-web/src/SmartCameraWeb.stories.js +57 -57
  128. package/lib/components/totp-consent/src/TotpConsent.js +949 -949
  129. package/lib/components/totp-consent/src/index.js +4 -4
  130. package/lib/domain/camera/src/README.md +38 -38
  131. package/lib/domain/camera/src/SmartCamera.js +109 -109
  132. package/lib/domain/constants/src/Constants.js +27 -27
  133. package/lib/domain/file-upload/README.md +35 -35
  134. package/lib/domain/file-upload/src/SmartFileUpload.js +65 -65
  135. package/lib/styles/README.md +3 -3
  136. package/lib/styles/src/styles.js +372 -372
  137. package/lib/styles/src/typography.js +52 -52
  138. package/package.json +111 -112
  139. package/dist/esm/DocumentCaptureScreens-RECPb0wH.js.map +0 -1
  140. package/dist/esm/SelfieCaptureScreens-CqBVGEJk.js.map +0 -1
  141. package/dist/esm/package-BDJnoIAU.js.map +0 -1
@@ -1,420 +1,420 @@
1
- import './selfie-capture-instructions';
2
- import './selfie-capture-review';
3
- import './selfie-capture-wrapper/index.ts';
4
- import SmartCamera from '../../../domain/camera/src/SmartCamera';
5
- import styles from '../../../styles/src/styles';
6
- import packageJson from '../../../../package.json';
7
-
8
- const COMPONENTS_VERSION = packageJson.version;
9
-
10
- const smartCameraWeb = document.querySelector('smart-camera-web');
11
-
12
- const cropImageFromDataUri = (
13
- dataUri,
14
- cropPercentX = 0,
15
- cropPercentY = 0,
16
- quality = 0.9,
17
- ) =>
18
- new Promise((resolve, reject) => {
19
- if (!dataUri || typeof dataUri !== 'string') {
20
- reject(new Error('Invalid data URI provided'));
21
- return;
22
- }
23
-
24
- if (
25
- cropPercentX < 0 ||
26
- cropPercentX >= 99 ||
27
- cropPercentY < 0 ||
28
- cropPercentY >= 99
29
- ) {
30
- reject(new Error('Crop percentages must be between 0 and 99'));
31
- return;
32
- }
33
-
34
- const img = new Image();
35
-
36
- img.onload = () => {
37
- try {
38
- const canvas = document.createElement('canvas');
39
- const ctx = canvas.getContext('2d');
40
-
41
- const originalWidth = img.width;
42
- const originalHeight = img.height;
43
- const cropAmountX = (originalWidth * cropPercentX) / 100;
44
- const cropAmountY = (originalHeight * cropPercentY) / 100;
45
-
46
- const newWidth = originalWidth - cropAmountX * 2;
47
- const newHeight = originalHeight - cropAmountY * 2;
48
- canvas.width = newWidth;
49
- canvas.height = newHeight;
50
-
51
- ctx.drawImage(
52
- img,
53
- cropAmountX,
54
- cropAmountY,
55
- newWidth,
56
- newHeight,
57
- 0,
58
- 0,
59
- newWidth,
60
- newHeight,
61
- );
62
-
63
- const croppedDataUri = canvas.toDataURL('image/jpeg', quality);
64
- resolve(croppedDataUri);
65
- } catch (error) {
66
- reject(new Error(`Failed to process image: ${error.message}`));
67
- }
68
- };
69
-
70
- img.onerror = () => {
71
- reject(new Error('Failed to load image from data URI'));
72
- };
73
-
74
- img.src = dataUri;
75
- });
76
-
77
- class SelfieCaptureScreens extends HTMLElement {
78
- constructor() {
79
- super();
80
- this.activeScreen = null;
81
- this._remountKey = 0; // Counter for forcing wrapper remounts
82
- smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.initialize'));
83
- }
84
-
85
- connectedCallback() {
86
- this.innerHTML = `
87
- ${styles(this.themeColor)}
88
- <div style="height: 100%;">
89
- <selfie-capture-instructions theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} ${this.hideBack} hidden></selfie-capture-instructions>
90
- <selfie-capture-wrapper theme-color='${this.themeColor}' ${this.showNavigation} ${this.allowAgentMode} ${this.allowAgentModeTests} ${this.hideAttribution} ${this.disableImageTests} key="${this._remountKey}" start-countdown="false" hidden></selfie-capture-wrapper>
91
- <selfie-capture-review theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} hidden></selfie-capture-review>
92
- </div>
93
- `;
94
-
95
- this._data = {
96
- images: [],
97
- meta: {
98
- libraryVersion: COMPONENTS_VERSION,
99
- },
100
- };
101
- this.selfieInstruction = this.querySelector('selfie-capture-instructions');
102
- this.selfieCapture = this.querySelector('selfie-capture-wrapper');
103
- this.selfieReview = this.querySelector('selfie-capture-review');
104
-
105
- if (
106
- this.getAttribute('initial-screen') === 'selfie-capture' ||
107
- this.hideInstructions
108
- ) {
109
- this.setActiveScreen(this.selfieCapture);
110
- } else {
111
- this.setActiveScreen(this.selfieInstruction);
112
- }
113
-
114
- this.setUpEventListeners();
115
- }
116
-
117
- getAgentMode() {
118
- return this.inAgentMode ? 'environment' : 'user';
119
- }
120
-
121
- disconnectedCallback() {
122
- SmartCamera.stopMedia();
123
- smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.cleanup'));
124
-
125
- if (this._selfieWrapperListeners) {
126
- this._selfieWrapperListeners.forEach(({ event, handler }) => {
127
- window.removeEventListener(event, handler);
128
- });
129
- this._selfieWrapperListeners = null;
130
- }
131
-
132
- if (this.activeScreen) {
133
- this.activeScreen.removeAttribute('hidden');
134
- }
135
- this.activeScreen = null;
136
- this.innerHTML = '';
137
- }
138
-
139
- setUpEventListeners() {
140
- this.selfieInstruction.addEventListener(
141
- 'selfie-capture-instructions.capture',
142
- async () => {
143
- this.setActiveScreen(this.selfieCapture);
144
- },
145
- );
146
- this.selfieInstruction.addEventListener(
147
- 'selfie-capture-instructions.cancelled',
148
- () => {
149
- this.handleBackEvents();
150
- },
151
- );
152
- this.setupSelfieWrapperEventListeners();
153
-
154
- this.selfieReview.addEventListener(
155
- 'selfie-capture-review.rejected',
156
- async () => {
157
- smartCameraWeb?.dispatchEvent(
158
- new CustomEvent('metadata.selfie-capture-retry'),
159
- );
160
- this.selfieReview.removeAttribute('data-image');
161
- this.selfieReview.removeAttribute('mirror-image');
162
- this._data.images = [];
163
-
164
- await this.forceWrapperRemount();
165
-
166
- this.setActiveScreen(this.selfieCapture);
167
- },
168
- );
169
-
170
- this.selfieReview.addEventListener(
171
- 'selfie-capture-review.accepted',
172
- async () => {
173
- this._publishSelectedImages();
174
- },
175
- );
176
-
177
- [this.selfieInstruction, this.selfieCapture, this.selfieReview].forEach(
178
- (screen) => {
179
- screen.addEventListener(
180
- `${screen.nodeName.toLowerCase()}.close`,
181
- () => {
182
- this.handleCloseEvent();
183
- },
184
- );
185
- },
186
- );
187
- }
188
-
189
- // Force remount of selfie-capture-wrapper component for clean state
190
- async forceWrapperRemount() {
191
- SmartCamera.stopMedia();
192
-
193
- this._remountKey++;
194
-
195
- const container = this.querySelector('div');
196
- const oldWrapper = this.selfieCapture;
197
-
198
- if (oldWrapper && container) {
199
- // recreate wrapper element
200
- const newWrapper = document.createElement('selfie-capture-wrapper');
201
-
202
- // copy attributes from old wrapper, but skip key and start-countdown
203
- const attributesToCopy = Array.from(oldWrapper.attributes);
204
-
205
- attributesToCopy.forEach((attr) => {
206
- newWrapper.setAttribute(attr.name, attr.value);
207
- });
208
- oldWrapper.remove();
209
- await new Promise((resolve) => {
210
- setTimeout(resolve, 50);
211
- });
212
-
213
- newWrapper.setAttribute('key', this._remountKey.toString());
214
- newWrapper.setAttribute('start-countdown', 'false');
215
- newWrapper.setAttribute('hidden', '');
216
-
217
- const reviewElement = container.querySelector('selfie-capture-review');
218
- if (reviewElement) {
219
- container.insertBefore(newWrapper, reviewElement);
220
- } else {
221
- container.appendChild(newWrapper);
222
- }
223
-
224
- this.selfieCapture = newWrapper;
225
-
226
- this.setupSelfieWrapperEventListeners();
227
- }
228
-
229
- // give time for the new component to initialize
230
- return new Promise((resolve) => {
231
- setTimeout(() => {
232
- resolve();
233
- }, 200);
234
- });
235
- }
236
-
237
- // Override setActiveScreen to enable countdown when selfie-capture is active
238
- setActiveScreen(screen) {
239
- if (this.activeScreen === screen) {
240
- return;
241
- }
242
-
243
- this.activeScreen?.setAttribute('hidden', '');
244
- screen.removeAttribute('hidden');
245
- this.activeScreen = screen;
246
-
247
- // If activating selfie-capture-wrapper, enable the countdown
248
- if (screen === this.selfieCapture) {
249
- screen.setAttribute('start-countdown', 'true');
250
- } else if (this.selfieCapture) {
251
- // Disable countdown when not on capture screen
252
- this.selfieCapture.setAttribute('start-countdown', 'false');
253
- }
254
- }
255
-
256
- setupSelfieWrapperEventListeners() {
257
- // Remove existing event listeners if they exist
258
- if (this._selfieWrapperListeners) {
259
- this._selfieWrapperListeners.forEach(({ event, handler }) => {
260
- window.removeEventListener(event, handler);
261
- });
262
- }
263
-
264
- // Create new event handlers
265
- const cancelledHandler = async () => {
266
- SmartCamera.stopMedia();
267
-
268
- // Force remount of selfie-capture-wrapper for clean state on next visit
269
- await this.forceWrapperRemount();
270
-
271
- if (this.hideInstructions) {
272
- this.handleBackEvents();
273
- return;
274
- }
275
-
276
- this.setActiveScreen(this.selfieInstruction);
277
- };
278
-
279
- const closeHandler = async () => {
280
- SmartCamera.stopMedia();
281
-
282
- // Force remount of selfie-capture-wrapper for clean state on next visit
283
- await this.forceWrapperRemount();
284
-
285
- this.handleCloseEvent();
286
- };
287
-
288
- const publishHandler = async (event) => {
289
- smartCameraWeb?.dispatchEvent(
290
- new CustomEvent('metadata.selfie-capture-end'),
291
- );
292
- this.selfieReview.setAttribute(
293
- 'data-image',
294
- await cropImageFromDataUri(event.detail.referenceImage, 20, 20),
295
- );
296
- const shouldMirror = event.detail.facingMode === 'user';
297
- this.selfieReview.setAttribute(
298
- 'mirror-image',
299
- shouldMirror ? 'true' : 'false',
300
- );
301
- this._data.images = event.detail.images;
302
- SmartCamera.stopMedia();
303
- this.setActiveScreen(this.selfieReview);
304
- };
305
-
306
- // Store references to remove them later
307
- this._selfieWrapperListeners = [
308
- { event: 'selfie-capture.cancelled', handler: cancelledHandler },
309
- { event: 'selfie-capture.close', handler: closeHandler },
310
- { event: 'selfie-capture.publish', handler: publishHandler },
311
- ];
312
-
313
- // Add event listeners
314
- this._selfieWrapperListeners.forEach(({ event, handler }) => {
315
- window.addEventListener(event, handler);
316
- });
317
-
318
- // Also listen for the publish event on the parent SelfieCaptureScreens element
319
- // in case smartselfie-capture dispatches it there
320
- this.addEventListener('selfie-capture.publish', publishHandler);
321
- }
322
-
323
- _publishSelectedImages() {
324
- this.dispatchEvent(
325
- new CustomEvent('selfie-capture-screens.publish', { detail: this._data }),
326
- );
327
- }
328
-
329
- get hideInstructions() {
330
- return this.hasAttribute('hide-instructions');
331
- }
332
-
333
- get hideAttribution() {
334
- return this.hasAttribute('hide-attribution') ? 'hide-attribution=""' : '';
335
- }
336
-
337
- get hideBackOfId() {
338
- return this.hasAttribute('hide-back-of-id');
339
- }
340
-
341
- get showNavigation() {
342
- return this.hasAttribute('show-navigation') ? 'show-navigation=""' : '';
343
- }
344
-
345
- get inAgentMode() {
346
- return this.getAttribute('allow-agent-mode') === 'true';
347
- }
348
-
349
- get allowAgentMode() {
350
- return this.inAgentMode ? 'allow-agent-mode="true"' : '';
351
- }
352
-
353
- get allowAgentModeTests() {
354
- return this.hasAttribute('show-agent-mode-for-tests')
355
- ? 'show-agent-mode-for-tests=""'
356
- : '';
357
- }
358
-
359
- get hideBack() {
360
- return this.hasAttribute('hide-back-to-host') ||
361
- this.hasAttribute('hide-back')
362
- ? 'hide-back=""'
363
- : '';
364
- }
365
-
366
- get disableImageTests() {
367
- return this.hasAttribute('disable-image-tests')
368
- ? 'disable-image-tests=""'
369
- : '';
370
- }
371
-
372
- get themeColor() {
373
- return this.getAttribute('theme-color') || '#001096';
374
- }
375
-
376
- handleBackEvents() {
377
- this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
378
- }
379
-
380
- handleCloseEvent() {
381
- this.dispatchEvent(new CustomEvent('selfie-capture-screens.close'));
382
- }
383
-
384
- static get observedAttributes() {
385
- return [
386
- 'title',
387
- 'hidden',
388
- 'show-navigation',
389
- 'hide-back-to-host',
390
- 'initial-screen',
391
- 'allow-agent-mode',
392
- 'show-agent-mode-for-tests',
393
- 'disable-image-tests',
394
- ];
395
- }
396
-
397
- attributeChangedCallback(name) {
398
- switch (name) {
399
- case 'title':
400
- case 'hidden':
401
- case 'initial-screen':
402
- case 'allow-agent-mode':
403
- case 'show-agent-mode-for-tests':
404
- case 'disable-image-tests':
405
- this.connectedCallback();
406
- break;
407
- default:
408
- break;
409
- }
410
- }
411
- }
412
-
413
- if (
414
- 'customElements' in window &&
415
- !customElements.get('selfie-capture-screens')
416
- ) {
417
- customElements.define('selfie-capture-screens', SelfieCaptureScreens);
418
- }
419
-
420
- export default SelfieCaptureScreens;
1
+ import './selfie-capture-instructions';
2
+ import './selfie-capture-review';
3
+ import './selfie-capture-wrapper/index.ts';
4
+ import SmartCamera from '../../../domain/camera/src/SmartCamera';
5
+ import styles from '../../../styles/src/styles';
6
+ import packageJson from '../../../../package.json';
7
+
8
+ const COMPONENTS_VERSION = packageJson.version;
9
+
10
+ const smartCameraWeb = document.querySelector('smart-camera-web');
11
+
12
+ const cropImageFromDataUri = (
13
+ dataUri,
14
+ cropPercentX = 0,
15
+ cropPercentY = 0,
16
+ quality = 0.9,
17
+ ) =>
18
+ new Promise((resolve, reject) => {
19
+ if (!dataUri || typeof dataUri !== 'string') {
20
+ reject(new Error('Invalid data URI provided'));
21
+ return;
22
+ }
23
+
24
+ if (
25
+ cropPercentX < 0 ||
26
+ cropPercentX >= 99 ||
27
+ cropPercentY < 0 ||
28
+ cropPercentY >= 99
29
+ ) {
30
+ reject(new Error('Crop percentages must be between 0 and 99'));
31
+ return;
32
+ }
33
+
34
+ const img = new Image();
35
+
36
+ img.onload = () => {
37
+ try {
38
+ const canvas = document.createElement('canvas');
39
+ const ctx = canvas.getContext('2d');
40
+
41
+ const originalWidth = img.width;
42
+ const originalHeight = img.height;
43
+ const cropAmountX = (originalWidth * cropPercentX) / 100;
44
+ const cropAmountY = (originalHeight * cropPercentY) / 100;
45
+
46
+ const newWidth = originalWidth - cropAmountX * 2;
47
+ const newHeight = originalHeight - cropAmountY * 2;
48
+ canvas.width = newWidth;
49
+ canvas.height = newHeight;
50
+
51
+ ctx.drawImage(
52
+ img,
53
+ cropAmountX,
54
+ cropAmountY,
55
+ newWidth,
56
+ newHeight,
57
+ 0,
58
+ 0,
59
+ newWidth,
60
+ newHeight,
61
+ );
62
+
63
+ const croppedDataUri = canvas.toDataURL('image/jpeg', quality);
64
+ resolve(croppedDataUri);
65
+ } catch (error) {
66
+ reject(new Error(`Failed to process image: ${error.message}`));
67
+ }
68
+ };
69
+
70
+ img.onerror = () => {
71
+ reject(new Error('Failed to load image from data URI'));
72
+ };
73
+
74
+ img.src = dataUri;
75
+ });
76
+
77
+ class SelfieCaptureScreens extends HTMLElement {
78
+ constructor() {
79
+ super();
80
+ this.activeScreen = null;
81
+ this._remountKey = 0; // Counter for forcing wrapper remounts
82
+ smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.initialize'));
83
+ }
84
+
85
+ connectedCallback() {
86
+ this.innerHTML = `
87
+ ${styles(this.themeColor)}
88
+ <div style="height: 100%;">
89
+ <selfie-capture-instructions theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} ${this.hideBack} hidden></selfie-capture-instructions>
90
+ <selfie-capture-wrapper theme-color='${this.themeColor}' ${this.showNavigation} ${this.allowAgentMode} ${this.allowAgentModeTests} ${this.hideAttribution} ${this.disableImageTests} key="${this._remountKey}" start-countdown="false" hidden></selfie-capture-wrapper>
91
+ <selfie-capture-review theme-color='${this.themeColor}' ${this.showNavigation} ${this.hideAttribution} hidden></selfie-capture-review>
92
+ </div>
93
+ `;
94
+
95
+ this._data = {
96
+ images: [],
97
+ meta: {
98
+ libraryVersion: COMPONENTS_VERSION,
99
+ },
100
+ };
101
+ this.selfieInstruction = this.querySelector('selfie-capture-instructions');
102
+ this.selfieCapture = this.querySelector('selfie-capture-wrapper');
103
+ this.selfieReview = this.querySelector('selfie-capture-review');
104
+
105
+ if (
106
+ this.getAttribute('initial-screen') === 'selfie-capture' ||
107
+ this.hideInstructions
108
+ ) {
109
+ this.setActiveScreen(this.selfieCapture);
110
+ } else {
111
+ this.setActiveScreen(this.selfieInstruction);
112
+ }
113
+
114
+ this.setUpEventListeners();
115
+ }
116
+
117
+ getAgentMode() {
118
+ return this.inAgentMode ? 'environment' : 'user';
119
+ }
120
+
121
+ disconnectedCallback() {
122
+ SmartCamera.stopMedia();
123
+ smartCameraWeb?.dispatchEvent(new CustomEvent('metadata.cleanup'));
124
+
125
+ if (this._selfieWrapperListeners) {
126
+ this._selfieWrapperListeners.forEach(({ event, handler }) => {
127
+ window.removeEventListener(event, handler);
128
+ });
129
+ this._selfieWrapperListeners = null;
130
+ }
131
+
132
+ if (this.activeScreen) {
133
+ this.activeScreen.removeAttribute('hidden');
134
+ }
135
+ this.activeScreen = null;
136
+ this.innerHTML = '';
137
+ }
138
+
139
+ setUpEventListeners() {
140
+ this.selfieInstruction.addEventListener(
141
+ 'selfie-capture-instructions.capture',
142
+ async () => {
143
+ this.setActiveScreen(this.selfieCapture);
144
+ },
145
+ );
146
+ this.selfieInstruction.addEventListener(
147
+ 'selfie-capture-instructions.cancelled',
148
+ () => {
149
+ this.handleBackEvents();
150
+ },
151
+ );
152
+ this.setupSelfieWrapperEventListeners();
153
+
154
+ this.selfieReview.addEventListener(
155
+ 'selfie-capture-review.rejected',
156
+ async () => {
157
+ smartCameraWeb?.dispatchEvent(
158
+ new CustomEvent('metadata.selfie-capture-retry'),
159
+ );
160
+ this.selfieReview.removeAttribute('data-image');
161
+ this.selfieReview.removeAttribute('mirror-image');
162
+ this._data.images = [];
163
+
164
+ await this.forceWrapperRemount();
165
+
166
+ this.setActiveScreen(this.selfieCapture);
167
+ },
168
+ );
169
+
170
+ this.selfieReview.addEventListener(
171
+ 'selfie-capture-review.accepted',
172
+ async () => {
173
+ this._publishSelectedImages();
174
+ },
175
+ );
176
+
177
+ [this.selfieInstruction, this.selfieCapture, this.selfieReview].forEach(
178
+ (screen) => {
179
+ screen.addEventListener(
180
+ `${screen.nodeName.toLowerCase()}.close`,
181
+ () => {
182
+ this.handleCloseEvent();
183
+ },
184
+ );
185
+ },
186
+ );
187
+ }
188
+
189
+ // Force remount of selfie-capture-wrapper component for clean state
190
+ async forceWrapperRemount() {
191
+ SmartCamera.stopMedia();
192
+
193
+ this._remountKey++;
194
+
195
+ const container = this.querySelector('div');
196
+ const oldWrapper = this.selfieCapture;
197
+
198
+ if (oldWrapper && container) {
199
+ // recreate wrapper element
200
+ const newWrapper = document.createElement('selfie-capture-wrapper');
201
+
202
+ // copy attributes from old wrapper, but skip key and start-countdown
203
+ const attributesToCopy = Array.from(oldWrapper.attributes);
204
+
205
+ attributesToCopy.forEach((attr) => {
206
+ newWrapper.setAttribute(attr.name, attr.value);
207
+ });
208
+ oldWrapper.remove();
209
+ await new Promise((resolve) => {
210
+ setTimeout(resolve, 50);
211
+ });
212
+
213
+ newWrapper.setAttribute('key', this._remountKey.toString());
214
+ newWrapper.setAttribute('start-countdown', 'false');
215
+ newWrapper.setAttribute('hidden', '');
216
+
217
+ const reviewElement = container.querySelector('selfie-capture-review');
218
+ if (reviewElement) {
219
+ container.insertBefore(newWrapper, reviewElement);
220
+ } else {
221
+ container.appendChild(newWrapper);
222
+ }
223
+
224
+ this.selfieCapture = newWrapper;
225
+
226
+ this.setupSelfieWrapperEventListeners();
227
+ }
228
+
229
+ // give time for the new component to initialize
230
+ return new Promise((resolve) => {
231
+ setTimeout(() => {
232
+ resolve();
233
+ }, 200);
234
+ });
235
+ }
236
+
237
+ // Override setActiveScreen to enable countdown when selfie-capture is active
238
+ setActiveScreen(screen) {
239
+ if (this.activeScreen === screen) {
240
+ return;
241
+ }
242
+
243
+ this.activeScreen?.setAttribute('hidden', '');
244
+ screen.removeAttribute('hidden');
245
+ this.activeScreen = screen;
246
+
247
+ // If activating selfie-capture-wrapper, enable the countdown
248
+ if (screen === this.selfieCapture) {
249
+ screen.setAttribute('start-countdown', 'true');
250
+ } else if (this.selfieCapture) {
251
+ // Disable countdown when not on capture screen
252
+ this.selfieCapture.setAttribute('start-countdown', 'false');
253
+ }
254
+ }
255
+
256
+ setupSelfieWrapperEventListeners() {
257
+ // Remove existing event listeners if they exist
258
+ if (this._selfieWrapperListeners) {
259
+ this._selfieWrapperListeners.forEach(({ event, handler }) => {
260
+ window.removeEventListener(event, handler);
261
+ });
262
+ }
263
+
264
+ // Create new event handlers
265
+ const cancelledHandler = async () => {
266
+ SmartCamera.stopMedia();
267
+
268
+ // Force remount of selfie-capture-wrapper for clean state on next visit
269
+ await this.forceWrapperRemount();
270
+
271
+ if (this.hideInstructions) {
272
+ this.handleBackEvents();
273
+ return;
274
+ }
275
+
276
+ this.setActiveScreen(this.selfieInstruction);
277
+ };
278
+
279
+ const closeHandler = async () => {
280
+ SmartCamera.stopMedia();
281
+
282
+ // Force remount of selfie-capture-wrapper for clean state on next visit
283
+ await this.forceWrapperRemount();
284
+
285
+ this.handleCloseEvent();
286
+ };
287
+
288
+ const publishHandler = async (event) => {
289
+ smartCameraWeb?.dispatchEvent(
290
+ new CustomEvent('metadata.selfie-capture-end'),
291
+ );
292
+ this.selfieReview.setAttribute(
293
+ 'data-image',
294
+ await cropImageFromDataUri(event.detail.referenceImage, 20, 20),
295
+ );
296
+ const shouldMirror = event.detail.facingMode === 'user';
297
+ this.selfieReview.setAttribute(
298
+ 'mirror-image',
299
+ shouldMirror ? 'true' : 'false',
300
+ );
301
+ this._data.images = event.detail.images;
302
+ SmartCamera.stopMedia();
303
+ this.setActiveScreen(this.selfieReview);
304
+ };
305
+
306
+ // Store references to remove them later
307
+ this._selfieWrapperListeners = [
308
+ { event: 'selfie-capture.cancelled', handler: cancelledHandler },
309
+ { event: 'selfie-capture.close', handler: closeHandler },
310
+ { event: 'selfie-capture.publish', handler: publishHandler },
311
+ ];
312
+
313
+ // Add event listeners
314
+ this._selfieWrapperListeners.forEach(({ event, handler }) => {
315
+ window.addEventListener(event, handler);
316
+ });
317
+
318
+ // Also listen for the publish event on the parent SelfieCaptureScreens element
319
+ // in case smartselfie-capture dispatches it there
320
+ this.addEventListener('selfie-capture.publish', publishHandler);
321
+ }
322
+
323
+ _publishSelectedImages() {
324
+ this.dispatchEvent(
325
+ new CustomEvent('selfie-capture-screens.publish', { detail: this._data }),
326
+ );
327
+ }
328
+
329
+ get hideInstructions() {
330
+ return this.hasAttribute('hide-instructions');
331
+ }
332
+
333
+ get hideAttribution() {
334
+ return this.hasAttribute('hide-attribution') ? 'hide-attribution=""' : '';
335
+ }
336
+
337
+ get hideBackOfId() {
338
+ return this.hasAttribute('hide-back-of-id');
339
+ }
340
+
341
+ get showNavigation() {
342
+ return this.hasAttribute('show-navigation') ? 'show-navigation=""' : '';
343
+ }
344
+
345
+ get inAgentMode() {
346
+ return this.getAttribute('allow-agent-mode') === 'true';
347
+ }
348
+
349
+ get allowAgentMode() {
350
+ return this.inAgentMode ? 'allow-agent-mode="true"' : '';
351
+ }
352
+
353
+ get allowAgentModeTests() {
354
+ return this.hasAttribute('show-agent-mode-for-tests')
355
+ ? 'show-agent-mode-for-tests=""'
356
+ : '';
357
+ }
358
+
359
+ get hideBack() {
360
+ return this.hasAttribute('hide-back-to-host') ||
361
+ this.hasAttribute('hide-back')
362
+ ? 'hide-back=""'
363
+ : '';
364
+ }
365
+
366
+ get disableImageTests() {
367
+ return this.hasAttribute('disable-image-tests')
368
+ ? 'disable-image-tests=""'
369
+ : '';
370
+ }
371
+
372
+ get themeColor() {
373
+ return this.getAttribute('theme-color') || '#001096';
374
+ }
375
+
376
+ handleBackEvents() {
377
+ this.dispatchEvent(new CustomEvent('selfie-capture-screens.cancelled'));
378
+ }
379
+
380
+ handleCloseEvent() {
381
+ this.dispatchEvent(new CustomEvent('selfie-capture-screens.close'));
382
+ }
383
+
384
+ static get observedAttributes() {
385
+ return [
386
+ 'title',
387
+ 'hidden',
388
+ 'show-navigation',
389
+ 'hide-back-to-host',
390
+ 'initial-screen',
391
+ 'allow-agent-mode',
392
+ 'show-agent-mode-for-tests',
393
+ 'disable-image-tests',
394
+ ];
395
+ }
396
+
397
+ attributeChangedCallback(name) {
398
+ switch (name) {
399
+ case 'title':
400
+ case 'hidden':
401
+ case 'initial-screen':
402
+ case 'allow-agent-mode':
403
+ case 'show-agent-mode-for-tests':
404
+ case 'disable-image-tests':
405
+ this.connectedCallback();
406
+ break;
407
+ default:
408
+ break;
409
+ }
410
+ }
411
+ }
412
+
413
+ if (
414
+ 'customElements' in window &&
415
+ !customElements.get('selfie-capture-screens')
416
+ ) {
417
+ customElements.define('selfie-capture-screens', SelfieCaptureScreens);
418
+ }
419
+
420
+ export default SelfieCaptureScreens;