UNPKG

@scandit/web-datacapture-barcode

Version:

Scandit Data Capture SDK for the Web

1 lines 20.4 kB
import {a as a$1}from'./chunk-QCD3RIT4.js';import {a as a$5}from'./chunk-3OCPZ3OV.js';import {a as a$4}from'./chunk-4AF7CEVC.js';import {a as a$7}from'./chunk-S4QFMFLQ.js';import {a as a$6}from'./chunk-L6BUOAGT.js';import {a as a$3}from'./chunk-GCXWZHE6.js';import {b}from'./chunk-RU7XJPB3.js';import {a as a$2,b as b$1,c}from'./chunk-PMTVYYAY.js';import {c as c$1,b as b$2}from'./chunk-SZUPDKV5.js';import {a as a$8}from'./chunk-HDODW33D.js';import {a}from'./chunk-IIKG5XTG.js';import {Logger,DataCaptureView,Anchor,LogoStyle,TorchState,Color,NumberWithUnit,RadiusLocationSelection}from'@scandit/web-datacapture-core';import {SafeStorage}from'@scandit/web-datacapture-core/build/js/private/utils/safeStorage.js';function d(I,e,a){let t=a.value;return typeof t!="function"?(Logger.warn(`@afterPrepareScanning applied to non-function: ${e}`),a):t.constructor.name==="AsyncFunction"||t.constructor===(async()=>{}).constructor?(a.value=function(...n){return this._preparationPromise?this._preparationPromise.then(()=>t.apply(this,n)):Promise.reject(new Error(`prepare should be called before calling ${e} method`))},a):(Logger.warn(`@afterPrepareScanning applied to non-async method: ${e}`),a)}var s=class s{constructor(e,a){this._context=null;this._sparkScan=null;this._preparationPromise=null;this._stopScanningPromise=null;this._dataCaptureView=null;this.preferenceManager=new a$1(SafeStorage.getInstanceFor("localStorage"));this.defaultSuccessBrush=a$2.defaultSuccessBrush();this.defaultErrorBrush=a$2.defaultErrorBrush();this.sparkScanView=e,this.cameraManager=new a$3,this.setSparkScanViewSettings(a);}get camera(){return this.cameraManager.camera}set context(e){this._context!==e&&(this._preparationPromise=null,this._context=e,e&&(this.analyticsManager=b.forContext(e)));}get context(){return this._context}set sparkScan(e){this._sparkScan!==e&&(this._preparationPromise=null,this._sparkScan=e);}get sparkScan(){return this._sparkScan}get sparkScan$(){if(!this._sparkScan)throw new Error("SparkScan mode is required to be set before calling this method");return this._sparkScan}get context$(){if(!this._context)throw new Error("Context is required to be set before calling this method");return this._context}setSparkScanViewSettings(e){this.sparkScanViewSettings=e,this.overlayManager=new a$4(this.sparkScanViewSettings),this.stateManager=new a$5(this.sparkScanViewSettings,this.preferenceManager),this.feedbackManager=a$6.withDefaults(this.sparkScanViewSettings.soundEnabled,this.sparkScanViewSettings.hapticEnabled),this.timeoutManager=new a$7(this.sparkScanViewSettings,this);}get portraitOrientationTriggerButtonTranslateX(){var e;return (e=this.stateManager.portraitOrientationTriggerButtonTranslateX)!=null?e:this.defaultTriggerButtonTranslateX}get portraitOrientationTriggerButtonTranslateY(){var e;return (e=this.stateManager.portraitOrientationTriggerButtonTranslateY)!=null?e:this.defaultTriggerButtonTranslateY}get landscapeOrientationTriggerButtonTranslateX(){var e;return (e=this.stateManager.landscapeOrientationTriggerButtonTranslateX)!=null?e:this.defaultTriggerButtonTranslateX}get landscapeOrientationTriggerButtonTranslateY(){var e;return (e=this.stateManager.landscapeOrientationTriggerButtonTranslateY)!=null?e:this.defaultTriggerButtonTranslateY}get portraitOrientationMiniPreviewTranslateX(){var e;return (e=this.stateManager.portraitOrientationMiniPreviewTranslateX)!=null?e:this.defaultMiniPreviewTranslateX}get portraitOrientationMiniPreviewTranslateY(){var e;return (e=this.stateManager.portraitOrientationMiniPreviewTranslateY)!=null?e:this.defaultMiniPreviewTranslateY}get landscapeOrientationMiniPreviewTranslateX(){var e;return (e=this.stateManager.landscapeOrientationMiniPreviewTranslateX)!=null?e:this.defaultMiniPreviewTranslateX}get landscapeOrientationMiniPreviewTranslateY(){var e;return (e=this.stateManager.landscapeOrientationMiniPreviewTranslateY)!=null?e:this.defaultMiniPreviewTranslateY}get defaultTriggerButtonTranslateX(){return this.parentViewSizes.width-Math.min(this.parentViewSizes.width/3,110)-90/2}get defaultTriggerButtonTranslateY(){return this.parentViewSizes.height-Math.min(this.parentViewSizes.height/3,250)-90/2}get defaultMiniPreviewTranslateX(){return this.parentViewSizes.width-225-16}get parentViewSizes(){var e,a;return {width:(e=this.sparkScanView.parentElement.clientWidth)!=null?e:window.innerWidth,height:(a=this.sparkScanView.parentElement.clientHeight)!=null?a:window.innerHeight}}get defaultMiniPreviewTranslateY(){return 16}static forView(e,a){return new s(e,a)}async prepareScanning(e){if(this._preparationPromise)return this._preparationPromise;this._stopScanningPromise&&await this._stopScanningPromise,this._preparationPromise=this._internalPrepareScanning(e);try{await this._preparationPromise;}catch(a){throw this._preparationPromise=null,a}return this._preparationPromise}async _internalPrepareScanning(e){let a=this.context$,t=this.sparkScan$;this._dataCaptureView=await DataCaptureView.forContext(a),this._dataCaptureView.logoAnchor=Anchor.BottomCenter,this._dataCaptureView.logoStyle=LogoStyle.Minimal,this._dataCaptureView.setHiddenProperty("stopVideoTracksOnVisibilityChange",false),this._dataCaptureView.setHiddenProperty("freezeFrameWhenStoppingFrameSource",false),this._dataCaptureView.connectToElement(e);let i=await this.cameraManager.prepareScanning(a,this.sparkScanViewSettings);this.sparkScanView.setTorchAvailable(i.isTorchAvailable),this.sparkScanView.setZoomAvailable(i.isZoomAvailable),this.sparkScanView.cameraSwitchButtonTapped(i.isTorchAvailable,i.cameraPosition),await a.setMode(t),t.addListener(this),t.setDelegate(this),await this.overlayManager.prepareScanning(t,this._dataCaptureView,this.stateManager.scanningBehavior),this.analyticsManager.startSession({cameraPosition:this.camera.position,hapticEnabled:this.sparkScanViewSettings.hapticEnabled,scanningMode:this.stateManager.scanningMode,soundEnabled:this.sparkScanViewSettings.soundEnabled,torchState:this.camera.getDesiredTorchState()}),await this.applyPreferences(),await this.switchToIdleState();}async stopScanning(e){if(this._stopScanningPromise)return this._stopScanningPromise;if(this.stateManager.state!=="Initial"){this._preparationPromise=null,this._stopScanningPromise=this._internalStopScanning(e);try{await this._stopScanningPromise;}finally{this._stopScanningPromise=null;}}}async _internalStopScanning(e){var i;this.timeoutManager.stopCurrentTimeout();let a=this.context$,t=this.sparkScan$;await t.setEnabled(false),t.removeListener(this),t.setDelegate(void 0),await a.removeMode(t),await this.cameraManager.stopScanning(),(i=this._dataCaptureView)==null||i.detachFromElement(),this.analyticsManager.setSessionStopReason(e.sessionStopReason),this.setState("Initial");}async startScanning(){await this.switchToActiveState();}async pauseScanning(){await this.switchToInactiveState({sessionStopReason:"user"});}async onTriggerButtonPointerDown(){this.stateManager.triggerButtonPressed=true;}async onTriggerButtonTap(){switch(this.stateManager.state){case "Initial":{this.stateManager.triggerButtonPressed=false;return}case "idle":case "inactive":case "error":{await this.switchToActiveState({barcodeFeedback:this.tapBarcodeFeedback});return}case "active":{if(this.stateManager.inSmartScanSelection){this.timeoutManager.restartCurrentTimeout(),await this.sparkScan$.selectItemWithSmartScanSelection();return}await(this.stateManager.continuousModeEnabled?this.switchToInactiveState({sessionStopReason:"user",barcodeFeedback:this.silentBarcodeFeedback}):this.switchToActiveState({barcodeFeedback:this.tapBarcodeFeedback}));}}}async onTriggerButtonLongPressStart(){this.stateManager.triggerButtonPressed=true,this.sparkScanViewSettings.holdToScanEnabled&&(this.scanningBehaviorCache=this.stateManager.scanningBehavior,this.stateManager.scanningBehavior="continuous",this.stateManager.holdingToScan=true,await this.overlayManager.setShouldRemoveLocationsAfterScan(true),await this.switchToActiveState({barcodeFeedback:this.tapBarcodeFeedback}));}async onTriggerButtonLongPressEnd(){var e;this.sparkScanViewSettings.holdToScanEnabled&&this.stateManager.state!=="error"?(this.stateManager.scanningBehavior=(e=this.scanningBehaviorCache)!=null?e:"single",this.stateManager.holdingToScan=false,await this.overlayManager.setShouldRemoveLocationsAfterScan(this.stateManager.continuousModeEnabled),await this.switchToInactiveState({sessionStopReason:"user",barcodeFeedback:this.silentErrorBarcodeFeedback})):this.stateManager.triggerButtonPressed=false;}async onClickOutside(){this.stateManager.idleState||await this.switchToIdleState({emitTapFeedback:true});}async onVisibilityChange(){await this.switchToIdleState();}async onTargetModeButtonTap(e,a=this.feedbackManager.tapFeedback,t=true){var c,g;a.emit();let i=String(e),n={true:e&&this.stateManager.scanningMode instanceof b$2,false:!e&&this.stateManager.scanningMode instanceof c$1}[i];n&&(this.stateManager.scanningMode=this.stateManager.scanningMode.changeMode()),this.enableTargetModeLocationSelection(e),await this.overlayManager.setShouldDrawViewfinder(e),this.timeoutManager.restartCurrentTimeout(),this.sparkScanView.targetModeButtonTapped(e,t),n&&((g=(c=this.listener)==null?void 0:c.didChangeScanningMode)==null||g.call(c,this.stateManager.scanningMode),this.analyticsManager.setScanningMode(this.stateManager.scanningMode)),this.updateSizeControlVisibility();}async onScanningBehaviorButtonTap(e,a=this.feedbackManager.tapFeedback,t=true){a.emit(),this.stateManager.scanningMode=this.stateManager.scanningMode.copyWith(e?"continuous":"single"),await this.overlayManager.setShouldRemoveLocationsAfterScan(e),this.timeoutManager.restartCurrentTimeout(),this.sparkScanView.scanningBehaviorButtonTapped(e,t),this.analyticsManager.setScanningMode(this.stateManager.scanningMode);}async onBarcodeFindButtonTap(){var e,a;await this.switchToIdleState({sessionStopReason:"user",emitTapFeedback:true}),(a=(e=this.listener)==null?void 0:e.didTapBarcodeFindButton)==null||a.call(e,this.sparkScanView),this.timeoutManager.restartCurrentTimeout();}async onLabelCaptureButtonTap(){var e,a;await this.switchToIdleState({sessionStopReason:"user",emitTapFeedback:true}),(a=(e=this.listener)==null?void 0:e.didTapLabelCaptureButton)==null||a.call(e,this.sparkScanView),this.timeoutManager.restartCurrentTimeout();}async onCameraSwitchButtonTap(){if(this.feedbackManager.emitTapFeedback(),this.timeoutManager.restartCurrentTimeout(),!this.camera||this.stateManager.state==="error")return;let e=await this.cameraManager.onCameraSwitchButtonTap();e&&(this.sparkScanView.cameraSwitchButtonTapped(e.isTorchAvailable,e.cameraPosition),this.analyticsManager.setCameraPosition(e.cameraPosition));}async onTorchButtonTap(e,a=this.feedbackManager.tapFeedback,t=true){a.emit(),this.stateManager.torchState=e?TorchState.On:TorchState.Off,await this.cameraManager.onTorchButtonTap(this.stateManager.torchState),this.timeoutManager.restartCurrentTimeout(),this.sparkScanView.torchButtonTapped(e,t),this.analyticsManager.setTorchState(this.stateManager.torchState);}async onTriggerButtonCollapseTimeout(){this.stateManager.state!=="idle"&&this._preparationPromise&&await this.switchToIdleState({sessionStopReason:"timeout"});}async onInactiveStateTimeout(){this.stateManager.state!=="inactive"&&!this.stateManager.holdingToScan&&this._preparationPromise&&await this.switchToInactiveState({sessionStopReason:"timeout"});}async onResumeCapturingTimeout(){this.stateManager.state!=="active"&&this._preparationPromise&&await this.switchToActiveState();}async onMiniPreviewCloseButtonTap(){await this.switchToIdleState({sessionStopReason:"user",emitTapFeedback:true});}onMiniPreviewResizeButtonTap(){(this.stateManager.state==="active"||this.stateManager.state==="inactive")&&(this.feedbackManager.emitTapFeedback(),this.timeoutManager.restartCurrentTimeout());}async onMiniPreviewZoomButtonTap(e){this.feedbackManager.emitTapFeedback(),this.stateManager.zoomFactor=e,await this.cameraManager.onMiniPreviewZoomButtonTap(e),this.timeoutManager.restartCurrentTimeout();}async onMiniPreviewDrag(e,a){switch(this.stateManager.state==="active"&&await this.sparkScan$.setEnabled(false),a){case "landscape-primary":case "landscape-secondary":{this.stateManager.landscapeOrientationMiniPreviewTranslateX=e.x,this.stateManager.landscapeOrientationMiniPreviewTranslateY=e.y;break}case "portrait-primary":case "portrait-secondary":{this.stateManager.portraitOrientationMiniPreviewTranslateX=e.x,this.stateManager.portraitOrientationMiniPreviewTranslateY=e.y;break}}this.timeoutManager.restartCurrentTimeout();}onMiniPreviewPointerMove(){this.timeoutManager.restartCurrentTimeout();}async onMiniPreviewPointerUp(){await this.switchToActiveState();}async onMiniPreviewDragEnd(){this.stateManager.state==="active"&&await this.sparkScan$.setEnabled(true);}async onMiniPreviewVisibilityChange(e){e&&this.sparkScanView.repositionMiniPreviewIfOutOfBounds(),this.stateManager.inSmartScanSelection&&!e&&await this.onSwitchedAwayFromSmartScanSelection();}onTriggerButtonDrag(e,a){switch(a){case "landscape-primary":case "landscape-secondary":{this.stateManager.landscapeOrientationTriggerButtonTranslateX=e.x,this.stateManager.landscapeOrientationTriggerButtonTranslateY=e.y;break}case "portrait-primary":case "portrait-secondary":{this.stateManager.portraitOrientationTriggerButtonTranslateX=e.x,this.stateManager.portraitOrientationTriggerButtonTranslateY=e.y;break}}}async onOrientationChange(){if(this.stateManager.state==="active"&&this.stateManager.holdingToScan){this.timeoutManager.restartCurrentTimeout();return}this.stateManager.state==="error"&&await this.switchToInactiveState({sessionStopReason:"user"});}async didScan(e,a){if(!a.newlyRecognizedBarcode)return;let t=this.sparkScanView.feedbackDelegate.getFeedbackForBarcode(a.newlyRecognizedBarcode);if(t){if(t instanceof b$1){await this.switchToErrorState({barcodeFeedback:t}),this.timeoutManager.restartCurrentTimeout();return}if(t instanceof c){if(this.stateManager.continuousModeEnabled){await this.switchToActiveState({barcodeFeedback:t}),this.timeoutManager.restartCurrentTimeout();return}let i=true,n="user";await this.switchToInactiveState({barcodeFeedback:t,emitSuccessFeedback:i,sessionStopReason:n}),this.timeoutManager.restartCurrentTimeout();}}}setListener(e){this.listener=e;}async onSwitchedToSmartScanSelection(){this.overlayManager.isReady&&(this.stateManager.inSmartScanSelection=true,this.stateManager.miniPreviewSizeBeforeSmartScanSelection=this.sparkScanView.miniPreviewSize,this.stateManager.previewSizeControlVisibleBeforeSmartScanSelection=this.sparkScanView.previewSizeControlVisible,await this.overlayManager.onSwitchedToSmartScanSelection(this._dataCaptureView),this.stateManager.targetModeEnabled||(this.sparkScanView.miniPreviewSize="expanded"),this.updateSizeControlVisibility());}async onSwitchedAwayFromSmartScanSelection(){this.overlayManager.isReady&&this.stateManager.miniPreviewSizeBeforeSmartScanSelection&&(this.stateManager.inSmartScanSelection=false,await this.overlayManager.onSwitchedAwayFromSmartScanSelection(this._dataCaptureView),this.sparkScanView.miniPreviewSize=this.stateManager.miniPreviewSizeBeforeSmartScanSelection,this.stateManager.miniPreviewSizeBeforeSmartScanSelection=void 0,this.updateSizeControlVisibility());}get tapBarcodeFeedback(){return new c(Color.fromRGBA(0,0,0,0),this.defaultSuccessBrush,this.feedbackManager.tapFeedback)}get silentBarcodeFeedback(){return new c(Color.fromRGBA(0,0,0,0),this.defaultSuccessBrush,this.feedbackManager.noFeedback)}get silentErrorBarcodeFeedback(){return new b$1("",0,Color.fromRGBA(0,0,0,0),this.defaultErrorBrush,this.feedbackManager.noFeedback)}async switchToIdleState(e={}){await this.sparkScan$.setEnabled(false),await this.cameraManager.onSwitchToIdleState(),e.emitTapFeedback===true&&this.feedbackManager.emitTapFeedback(),await this.overlayManager.onSwitchToIdleState(),this.setState("idle"),this.timeoutManager.onSwitchToIdleState(),this.stateManager.triggerButtonPressed=false,this.sparkScanView.switchToIdleState(),e.sessionStopReason!=null&&this.analyticsManager.setSessionStopReason(e.sessionStopReason);}async switchToActiveState(e={}){var t;let a=this.stateManager.state;await this.cameraManager.onSwitchToActiveState(this.stateManager.torchState),await this.overlayManager.onSwitchToActiveState(a,this.stateManager.continuousModeEnabled,e.barcodeFeedback),this.feedbackManager.emitBarcodeFeedback(e.barcodeFeedback),this.timeoutManager.onSwitchToActiveState(this.stateManager.holdingToScan),await this.sparkScanView.switchToActiveState(this.sparkScanViewSettings.visualFeedbackEnabled&&e.barcodeFeedback instanceof c,this.stateManager.scanningBehavior,(t=e.barcodeFeedback)==null?void 0:t.visualFeedbackColor,this.stateManager.zoomedIn&&a!=="active"),await this.sparkScan$.setEnabled(true),this.setState("active");}async switchToInactiveState(e){var t;this.stateManager.inSmartScanSelection?await this.sparkScan$.setEnabled(false):this.stateManager.shouldEnableSuppression?await this.sparkScan$.setResultSuppressionEnabled(true):await this.sparkScan$.setEnabled(false),this.stateManager.triggerButtonPressed=false,this.stateManager.scanningMode.previewBehavior==="persistent"&&e.barcodeFeedback instanceof c&&await this.overlayManager.setShouldRemoveLocationsAfterScan(true),await this.cameraManager.onSwitchToInactiveState(this.stateManager.scanningMode.previewBehavior),(e.emitSuccessFeedback)!=null||(e.emitSuccessFeedback=true),this.setState("inactive"),e.barcodeFeedback&&await this.overlayManager.setBrushForFeedback(e.barcodeFeedback),this.feedbackManager.emitBarcodeFeedback(e.barcodeFeedback),await this.overlayManager.overlayReadyForDraw(),this.timeoutManager.onSwitchToInactiveState(),await this.sparkScanView.switchToInactiveState(this.sparkScanViewSettings.visualFeedbackEnabled&&e.barcodeFeedback instanceof c&&e.emitSuccessFeedback,this.stateManager.scanningBehavior,this.stateManager.previewBehavior,(t=e.barcodeFeedback)==null?void 0:t.visualFeedbackColor),this.analyticsManager.setSessionStopReason(e.sessionStopReason);}async switchToErrorState(e){let a=e.barcodeFeedback.resumeCapturingDelay>0;a&&(await this.sparkScan$.setEnabled(false),await this.cameraManager.onSwitchToErrorState(),this.setState("error"),this.stateManager.scanningBehavior="single"),this.timeoutManager.onSwitchToErrorState(a,e.barcodeFeedback.resumeCapturingDelay),await this.overlayManager.onSwitchToErrorState(e.barcodeFeedback),await this.sparkScan$.workerCommand("sparkScanEmitErrorFeedback",{shouldRemoveBrush:!a}),this.feedbackManager.emitBarcodeFeedback(e.barcodeFeedback),await this.sparkScanView.switchToErrorState(e.barcodeFeedback,this.sparkScanViewSettings.visualFeedbackEnabled);}enableTargetModeLocationSelection(e){let a=e?a$8.SparkScanSettings.targetModeLocationSelection.radius:a$8.SparkScanSettings.locationSelection.radius,t=e?a$8.SparkScanSettings.targetModeLocationSelection.unit:a$8.SparkScanSettings.locationSelection.unit,i=new NumberWithUnit(a,t);this.sparkScan$.settings.locationSelection=new RadiusLocationSelection(i),this.overlayManager.setRadius(i);}async applyPreferences(){await this.onTargetModeButtonTap(this.stateManager.targetModeEnabled,this.feedbackManager.noFeedback,false),await this.onScanningBehaviorButtonTap(this.stateManager.continuousModeEnabled,this.feedbackManager.noFeedback,false),await this.onTorchButtonTap(this.stateManager.torchEnabled,this.feedbackManager.noFeedback,false),this.sparkScanView.repositionElementsIfOutOfBounds();}setState(e){var a,t;e!==this.stateManager.state&&(this.stateManager.state=e,(t=(a=this.listener)==null?void 0:a.didChangeViewState)==null||t.call(a,e));}updateSizeControlVisibility(){if(!this.stateManager.inSmartScanSelection&&this.stateManager.previewSizeControlVisibleBeforeSmartScanSelection!==void 0){this.sparkScanView.previewSizeControlVisible=this.stateManager.previewSizeControlVisibleBeforeSmartScanSelection,this.stateManager.previewSizeControlVisibleBeforeSmartScanSelection=void 0;return}let e=this.stateManager.inSmartScanSelection&&!this.stateManager.targetModeEnabled,a=this.sparkScanView.previewSizeControlVisible;this.sparkScanView.previewSizeControlVisible=a&&!e;}};a([d],s.prototype,"switchToActiveState",1),a([d],s.prototype,"switchToInactiveState",1),a([d],s.prototype,"switchToErrorState",1);var V=s;export{V as a};