|
| 1 | +import { |
| 2 | + defineComponents, |
| 3 | + DocumentReaderService, |
| 4 | + InternalScenarios, |
| 5 | + DocumentReaderWebComponent, |
| 6 | + DocumentReaderDetailType, |
| 7 | + TransactionEvent, |
| 8 | + EventActions, |
| 9 | + ResponseCode, |
| 10 | +} from '@regulaforensics/vp-frontend-document-components'; |
| 11 | +import './styles.css'; |
| 12 | + |
| 13 | +const newDocumentButton = document.querySelector('#new-document') as HTMLButtonElement; |
| 14 | +const nextPageButton = document.querySelector('#new-page') as HTMLButtonElement; |
| 15 | +const info = document.querySelector('.info') as HTMLDivElement; |
| 16 | + |
| 17 | +window.RegulaDocumentSDK = new DocumentReaderService(); |
| 18 | +window.RegulaDocumentSDK.recognizerProcessParam = { |
| 19 | + processParam: { |
| 20 | + scenario: InternalScenarios.MrzAndLocate, |
| 21 | + multipageProcessing: true, |
| 22 | + backendProcessing: { |
| 23 | + serviceURL: 'YOUR_SERVICE_URL', |
| 24 | + httpHeaders: { // you can set http headers if necessary |
| 25 | + key1: 'header1', |
| 26 | + key2: 'header2', |
| 27 | + key3: 'header3' |
| 28 | + } |
| 29 | + }, |
| 30 | + }, |
| 31 | +}; |
| 32 | +window.RegulaDocumentSDK.imageProcessParam = { |
| 33 | + processParam: { |
| 34 | + scenario: InternalScenarios.MrzAndLocate, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +defineComponents().then(() => window.RegulaDocumentSDK.initialize()); |
| 39 | +// To use the document-reader component on test environments, you have to set the base64 license |
| 40 | +// defineComponents().then(() => window.RegulaDocumentSDK.initialize({ license: 'YOUR_BASE64_LICENSE_KEY' })); |
| 41 | + |
| 42 | +function createDocumentReader() { |
| 43 | + const documentReaderElement = document.createElement('document-reader') as DocumentReaderWebComponent; |
| 44 | + const container = document.createElement('div'); |
| 45 | + documentReaderElement.setAttribute('new-layout', 'true'); |
| 46 | + container.setAttribute('class', 'reader-container'); |
| 47 | + container.append(documentReaderElement); |
| 48 | + |
| 49 | + documentReaderElement.settings = { |
| 50 | + uploadFileButton: false, |
| 51 | + manualMultipageMode: true, |
| 52 | + } |
| 53 | + |
| 54 | + return container; |
| 55 | +} |
| 56 | + |
| 57 | +function documentReaderListener(data: CustomEvent<DocumentReaderDetailType | TransactionEvent>) { |
| 58 | + const reader = document.querySelector('.reader-container'); |
| 59 | + const action = data.detail.action; |
| 60 | + |
| 61 | + if (action === EventActions.PROCESS_FINISHED) { |
| 62 | + const status = data.detail.data?.status; |
| 63 | + const isFinishStatus = status === ResponseCode.OK || status === ResponseCode.TIMEOUT; |
| 64 | + const responseData = data.detail.data; |
| 65 | + |
| 66 | + if (isFinishStatus && responseData?.response) { |
| 67 | + const isMorePagesAvailable = !!responseData.response.rawResponse.morePagesAvailable; |
| 68 | + |
| 69 | + info.textContent = 'Check your result in the console. '; |
| 70 | + |
| 71 | + if (isMorePagesAvailable) { |
| 72 | + nextPageButton.disabled = false; |
| 73 | + info.textContent += 'More pages are available, click "Start new page" ' + |
| 74 | + 'to continue scanning, or start scanning a new document by clicking "Start new document".'; |
| 75 | + } else { |
| 76 | + void window.RegulaDocumentSDK.finalizePackage(); |
| 77 | + } |
| 78 | + console.log(responseData.response); |
| 79 | + |
| 80 | + reader?.remove(); |
| 81 | + } |
| 82 | + } |
| 83 | + if (action === EventActions.CAMERA_PROCESS_CLOSED) reader?.remove(); |
| 84 | +} |
| 85 | + |
| 86 | +function newDocumentButtonListener() { |
| 87 | + if (!window.RegulaDocumentSDK) return; |
| 88 | + |
| 89 | + info.textContent = ''; |
| 90 | + nextPageButton.disabled = true; |
| 91 | + void window.RegulaDocumentSDK.createBackendTransaction(); |
| 92 | + document.body.append(createDocumentReader()); |
| 93 | +} |
| 94 | + |
| 95 | +function nextPageButtonListener() { |
| 96 | + if (!window.RegulaDocumentSDK) return; |
| 97 | + |
| 98 | + info.textContent = ''; |
| 99 | + nextPageButton.disabled = true; |
| 100 | + void window.RegulaDocumentSDK.startNewPage(); |
| 101 | + document.body.append(createDocumentReader()); |
| 102 | +} |
| 103 | + |
| 104 | +newDocumentButton?.addEventListener('click', newDocumentButtonListener); |
| 105 | +nextPageButton?.addEventListener('click', nextPageButtonListener); |
| 106 | +document.body.addEventListener('document-reader', documentReaderListener); |
0 commit comments