@kc-one/smart-fill-sdk 0.0.6 → 0.0.8
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/dist/index.esm.js +1 -1
- package/dist/index.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -2475,7 +2475,7 @@ class Qr {
|
|
|
2475
2475
|
/** 挂载右下角悬浮按钮 + 弹框(floating 模式),优先挂到当前子路由页面顶层容器 */
|
|
2476
2476
|
mountFloatingButton() {
|
|
2477
2477
|
this.assertAlive();
|
|
2478
|
-
const t = fo(this.config), n = it("floating"), r = st(n, !
|
|
2478
|
+
const t = fo(this.config), n = it("floating"), r = st(n, !0);
|
|
2479
2479
|
return this.createPanel("floating", n, r).mount(t), this.bindFloatingLifecycle(t), this.schedulePreScan(), this;
|
|
2480
2480
|
}
|
|
2481
2481
|
/**
|
package/dist/index.umd.cjs
CHANGED
|
@@ -149,4 +149,4 @@
|
|
|
149
149
|
width: min(450px, calc(100vw - 32px))
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
|
-
`,Qr=.75,eo=1800,Z=new Map;let J=0;const Be="smart-fill:routechange",Dt="smart-fill:session-scan:";let Pt=!1;const Ht=new WeakMap;let to=0;function $t(){Z.clear(),Wt(),J+=1}class Nt{constructor(t,n){d(this,"events",new qe);d(this,"scanner");d(this,"ruleEngine",new Tt);d(this,"panel",null);d(this,"panels",[]);d(this,"panelStorageKeys",new Map);d(this,"registeredFields",[]);d(this,"adapters",[Oe]);d(this,"scanResult",null);d(this,"autoApplyState",null);d(this,"formConfigVersion");d(this,"localPriorityEnabled",!1);d(this,"destroyed",!1);d(this,"scanCacheVersion",J);d(this,"floatingRouteSnapshot",null);d(this,"floatingLifecycleCleanup",null);d(this,"preScanTimer",null);this.config=t,this.context=n,this.scanner=new Mt(t.root||document),this.localPriorityEnabled=Co(),this.context.manager.add(this)}on(t,n){return this.events.on(t,n)}useAdapter(t){return this.assertAlive(),this.adapters.push(t),this}registerFields(t){this.assertAlive();const n=new Set;for(const r of t){const o=r.rowKey==null?r.fieldId:`${r.fieldId}:${r.rowKey}`;if(n.has(o))throw w("UNSUPPORTED_PAGE",`字段 ${o} 重复注册。`,"scan");n.add(o)}return this.registeredFields=t,this.scanResult=null,this.schedulePreScan(),this}unregisterFields(t){this.assertAlive(),this.registeredFields=t!=null&&t.length?this.registeredFields.filter(n=>!t.includes(n.fieldId)):[],this.scanResult=null,this.schedulePreScan()}mount(t){this.assertAlive();const n=typeof t=="string"?document.querySelector(t):t;if(!n)throw w("UNSUPPORTED_PAGE","未找到智能录入挂载点。","ui");const r=Xt("inline",_o(t,n)),o=Zt(r,!0);return this.createPanel("inline",r,o).mount(n),this.schedulePreScan(),this}mountFloatingButton(){this.assertAlive();const t=go(this.config),n=Xt("floating"),r=Zt(n,!1);return this.createPanel("floating",n,r).mount(t),this.bindFloatingLifecycle(t),this.schedulePreScan(),this}createPanel(t,n,r){let o;return o=new Wr({mode:t,initialOpen:r,messages:this.config.messages,localPriorityEnabled:this.localPriorityEnabled,onOpen:()=>this.open(o),onClose:()=>this.close(o),onRecognize:i=>(this.panel=o,this.recognize(i)),onLocalPriorityChange:i=>this.handleLocalPriorityChange(i,o)}),this.panels.push(o),this.panelStorageKeys.set(o,n),this.panel=o,o}handleLocalPriorityChange(t,n){this.localPriorityEnabled=t,Ao(t);for(const r of this.panels)r!==n&&r.setLocalPriorityEnabled(t)}async open(t=this.panel){this.assertAlive(),this.syncScanCacheVersion(),t&&(this.context.manager.activate(this),this.panel=t,t.setOpen(!0),Jt(this.panelStorageKeys.get(t),!0),this.scanResult||await this.rescan())}close(t=this.panel){t&&(this.panel=t,t.setOpen(!1),Jt(this.panelStorageKeys.get(t),!1))}async rescan(){return this.assertAlive(),this.syncScanCacheVersion(),this.cancelPreScan(),this.runScan({dynamicOptions:!0,emitStatus:!0})}async preScan(){return this.assertAlive(),this.syncScanCacheVersion(),this.runScan({dynamicOptions:!1,emitStatus:!1})}async runScan(t){var n;try{this.formConfigVersion=void 0;const r=this.getCachedPageScanResult(t.dynamicOptions),o=this.registeredFields.length?this.registeredFields:void 0,i=o?this.scanner.scan({registered:o,maxFields:this.config.maxFields}):r??(t.dynamicOptions?await this.scanner.scanWithDynamicOptions({maxFields:this.config.maxFields}):this.scanner.scan({maxFields:this.config.maxFields}));if(this.scanResult=i,this.cachePageScanResult(i,t.dynamicOptions),!this.scanResult.fields.length)throw w("NO_FIELDS_FOUND","当前页面未找到可回填字段。","scan");return t.emitStatus&&(this.events.emit("scanCompleted",{scanToken:this.scanResult.scanToken,fieldCount:this.scanResult.fields.length}),(n=this.panel)==null||n.setStatus(`已扫描 ${this.scanResult.fields.length} 个字段。`)),this.scanResult}catch(r){throw t.emitStatus&&this.emitError(r,"scan"),r}}schedulePreScan(){this.destroyed||!this.panels.length||(this.preScanTimer!=null&&window.clearTimeout(this.preScanTimer),this.preScanTimer=window.setTimeout(()=>{this.preScanTimer=null,!(this.destroyed||this.scanResult)&&this.preScan().catch(()=>{})},eo))}cancelPreScan(){this.preScanTimer!=null&&(window.clearTimeout(this.preScanTimer),this.preScanTimer=null)}async recognize(t){var i,s,a,c,l,u;this.assertAlive(),this.syncScanCacheVersion(),(i=this.panel)==null||i.setBusy(!0,"扫描中..."),(s=this.panel)==null||s.setStatus("扫描中...");const n=await this.rescan(),r=he("trace"),o=performance.now();this.events.emit("recognizing",{scanToken:n.scanToken,traceId:r}),(a=this.panel)==null||a.setBusy(!0,"识别中...");try{const g=n.fields.filter(y=>so(y.localRuleMode,this.localPriorityEnabled)),R=n.fields.filter(y=>y.localRuleMode!=="only"),{text:ee,usedOcr:we}=await this.context.client.resolveInputText({text:t.text,images:t.images,onStatusChange:y=>{var Se,O,Ee;if(y==="image_uploading"){(Se=this.panel)==null||Se.setStatus("图片上传中...");return}if(y==="image_recognizing"){(O=this.panel)==null||O.setStatus("图片识别中...");return}(Ee=this.panel)==null||Ee.setStatus("识别中...")}}),ve=ee?this.ruleEngine.recognize(ee,g,n.scanToken):[];let k;if(!R.length)k=Kt(n.scanToken,r,ve,o,void 0,we);else try{const y=await this.context.client.recognize({scanToken:n.scanToken,formCode:this.config.formCode,configVersion:this.formConfigVersion,text:ee,usedOcr:we,fields:R,onStatusChange:()=>{var O;(O=this.panel)==null||O.setStatus("识别中...")}});y.trace.durationMs=y.trace.durationMs||Math.round(performance.now()-o);const Se=io(ve,y.suggestions.filter(O=>R.some(Ee=>Ee.fieldId===O.fieldId)));k={...y,suggestions:Se}}catch(y){if(!ve.length)throw y;k=Kt(n.scanToken,r,ve,o,[we?"后端识别失败,已使用 OCR 文本触发本地识别继续回填。":"后端识别失败,已启用本地识别继续回填。"],we)}if(this.autoApplyState=ro(k.scanToken,k.trace.traceId,k.suggestions,n,this.registeredFields),this.events.emit("recognized",k),(c=this.panel)==null||c.setAutoApplyState(this.autoApplyState),this.config.apiEnable){const y=no(k,this.autoApplyState,n);this.config.apiCallback&&await Promise.resolve(this.config.apiCallback(y)),(l=this.panel)==null||l.setStatus(`识别完成,已返回 ${y.fields.length} 个字段。`)}else await this.applyAutoItems(this.autoApplyState);return k}catch(g){throw this.clearScanStateOnTokenExpired(g),this.emitError(g,"recognize"),g}finally{(u=this.panel)==null||u.setBusy(!1)}}async apply(t){var o;if(this.assertAlive(),this.syncScanCacheVersion(),!this.scanResult)throw w("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");this.events.emit("applying",{scanToken:t.scanToken,count:t.values.length});const r=await new Pe(this.scanResult.fields,this.registeredFields,this.adapters).apply(t);return this.events.emit("applied",r),(o=this.panel)==null||o.setApplyResult(r),r}async applyAutoItems(t){var a,c;if(!this.scanResult)throw w("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");const n=oo(t),r=t.items.filter(l=>Vt(l)).map(l=>({fieldId:l.fieldId,value:l.value,source:l.source}));this.events.emit("applying",{scanToken:t.scanToken,count:r.length}),(a=this.panel)==null||a.setStatus("识别完成,正在自动回填...");const i=await new Pe(this.scanResult.fields,this.registeredFields,this.adapters).apply({scanToken:t.scanToken,values:r}),s={...i,skipped:[...n,...i.skipped],warnings:[...i.warnings||[],...n.length?["部分字段因置信度或风险策略被跳过。"]:[]]};return this.events.emit("applied",s),(c=this.panel)==null||c.setApplyResult(s),s}destroy(){if(!this.destroyed){this.destroyed=!0,this.cancelPreScan(),this.clearFloatingLifecycle();for(const t of this.panels)t.destroy();this.panels.length=0,this.panelStorageKeys.clear(),this.panel=null,this.events.clear(),this.context.manager.remove(this)}}assertAlive(){if(this.destroyed)throw w("INSTANCE_DESTROYED","实例已销毁。","ui")}emitError(t,n){var o;const r=kt(t,n);this.events.emit("error",r),(o=this.panel)==null||o.setError(r.message)}getCachedPageScanResult(t){const n=this.getPageScanCacheKey();if(!n)return null;const r=Z.get(n);if(r&&(!t||r.dynamicOptionsReady)){const i=co(r.scanResult);if(!i)Z.delete(n);else return i}const o=this.getPersistedPageScanResult(t);return o?(this.cachePageScanResult(o.scanResult,o.dynamicOptionsReady),o.scanResult):null}cachePageScanResult(t,n){const r=this.getPageScanCacheKey();if(!r||!t.fields.length)return;const o=Z.get(r);o!=null&&o.dynamicOptionsReady&&!n||(Z.set(r,{scanResult:t,dynamicOptionsReady:(o==null?void 0:o.dynamicOptionsReady)||n}),this.persistPageScanResult(t,(o==null?void 0:o.dynamicOptionsReady)||n))}getPageScanCacheKey(){return this.registeredFields.length?null:po(this.config)}getSessionPageScanCacheKey(){return this.registeredFields.length||!this.config.formCode?null:fo(this.config)}clearScanStateOnTokenExpired(t){kt(t,"recognize").code==="TOKEN_EXPIRED"&&($t(),this.scanCacheVersion=J,this.scanResult=null,this.autoApplyState=null)}syncScanCacheVersion(){this.scanCacheVersion!==J&&(this.scanCacheVersion=J,this.scanResult=null,this.autoApplyState=null)}getPersistedPageScanResult(t){const n=this.getSessionPageScanCacheKey();if(!n)return null;const r=xo(n);if(!r||t&&!r.dynamicOptionsReady)return null;const o=this.scanner.scan({maxFields:this.config.maxFields}),i=lo(r,o);return i?{scanResult:i,dynamicOptionsReady:r.dynamicOptionsReady}:(Wt(n),null)}persistPageScanResult(t,n){const r=this.getSessionPageScanCacheKey();!r||!t.fields.length||ko(r,{version:1,dynamicOptionsReady:n,fields:t.fields.map(({element:o,scanToken:i,...s})=>s)})}bindFloatingLifecycle(t){this.clearFloatingLifecycle(),this.floatingRouteSnapshot=Ut(),ho();const n=()=>{this.destroyed||Ut()!==this.floatingRouteSnapshot&&this.destroy()};window.addEventListener(Be,n);const r=new MutationObserver(()=>{this.destroyed||t!==document.body&&!t.isConnected&&this.destroy()});r.observe(document.body,{childList:!0,subtree:!0}),this.floatingLifecycleCleanup=()=>{window.removeEventListener(Be,n),r.disconnect(),this.floatingRouteSnapshot=null,this.floatingLifecycleCleanup=null}}clearFloatingLifecycle(){var t;(t=this.floatingLifecycleCleanup)==null||t.call(this)}}function no(e,t,n){return{scanToken:e.scanToken,trace:e.trace,warnings:e.warnings,fields:t.items.map(r=>{const o=n.fields.find(i=>i.fieldId===r.fieldId);return{fieldId:r.fieldId,label:r.label,value:r.value,displayValue:r.displayValue||String(r.value??""),confidence:r.confidence,source:r.source,type:o==null?void 0:o.type,currentValue:r.currentValue,warnings:r.warnings}})}}function ro(e,t,n,r,o){return{scanToken:e,traceId:t,items:n.map(i=>{const s=r.fields.find(l=>l.fieldId===i.fieldId),a=o.find(l=>l.fieldId===i.fieldId),c=a!=null&&a.getValue?a.getValue():ao(s==null?void 0:s.element);return{applyItemId:`${i.fieldId}_${Math.random().toString(36).slice(2,8)}`,fieldId:i.fieldId,scanToken:e,groupId:i.groupId,label:i.label,value:i.value,currentValue:c,displayValue:i.displayValue||String(i.value??""),confidence:i.confidence,source:i.source,warnings:i.warnings,previousValue:c}})}}function Vt(e){var t;return e.confidence>=Qr&&!((t=e.warnings)!=null&&t.length)}function oo(e){return e.items.filter(t=>!Vt(t)).map(t=>{var n,r;return{fieldId:t.fieldId,label:t.label,attemptedValue:t.value,reason:((n=t.warnings)==null?void 0:n.join(";"))||`置信度 ${Math.round(t.confidence*100)}% 低于自动回填阈值`,reasonCode:(r=t.warnings)!=null&&r.length?"AUTO_APPLY_WARNING":"LOW_CONFIDENCE"}})}function io(e,t){const n=new Map;for(const r of t)n.set(r.fieldId,r);for(const r of e)n.has(r.fieldId)||n.set(r.fieldId,r);return[...n.values()]}function so(e,t){return e==="only"?!0:e==="off"?!1:t}function Kt(e,t,n,r,o,i=!1){return{scanToken:e,suggestions:n,warnings:o,trace:{traceId:t,usedOcr:i,usedAi:!1,durationMs:Math.round(performance.now()-r)}}}function ao(e){if(e)return e instanceof HTMLInputElement&&e.type==="checkbox"?e.checked:e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement?e.value:e.textContent}function co(e){const t=e.fields.map(n=>{if(n.source==="registered")return n;const r=uo(n);return r?{...n,element:r}:null});return t.some(n=>!n)?null:{...e,fields:t}}function lo(e,t){var s,a;const n=new Map(t.fields.map(c=>[c.fieldId,c])),r=[],o=new Set;for(const c of e.fields){const l=n.get(c.fieldId);if(!l)continue;o.add(c.fieldId);const u={...l,label:c.label||l.label,placeholder:c.placeholder??l.placeholder,name:c.name??l.name,id:c.id??l.id,section:c.section??l.section,options:(s=c.options)!=null&&s.length?c.options:l.options,required:c.required??l.required,readonly:c.readonly??l.readonly,disabled:c.disabled??l.disabled};u.fingerprint=X({...u,tagName:(a=u.element)==null?void 0:a.tagName.toLowerCase()}),r.push(u)}if(!r.length)return null;const i=t.fields.filter(c=>!o.has(c.fieldId));return{...t,fields:[...r,...i]}}function uo(e){var t;if((t=e.element)!=null&&t.isConnected)return e.element;try{return document.querySelector(`[data-smart-fill-id="${To(e.fieldId)}"]`)}catch{return null}}function po(e){const t=typeof location<"u"?`${location.origin}${location.pathname}${location.search}`:"unknown-page",n=zt(e.root||document);return`smart-fill:${t}:${e.formCode||"default-form"}:${n}:${e.maxFields??200}:scan`}function fo(e){const t=zt(e.root||document);return`${Dt}${e.formCode||"default-form"}:${t}:${e.maxFields??200}`}function zt(e){if(e===document)return"document";const t=Ht.get(e);if(t)return t;const n=e instanceof HTMLElement&&e.id?`el:${e.id}`:`root:${++to}`;return Ht.set(e,n),n}function ho(){if(Pt||typeof window>"u")return;Pt=!0;const e=()=>{window.dispatchEvent(new Event(Be))};window.addEventListener("popstate",e),window.addEventListener("hashchange",e),Bt("pushState",e),Bt("replaceState",e)}function Bt(e,t){const n=window.history[e];window.history[e]=function(...o){const i=n.apply(this,o);return t(),i}}function Ut(){return typeof location>"u"?"unknown-route":`${location.pathname}${location.search}${location.hash}`}function go(e){const t=yo(e.floatingContainer);if(t)return t;const n=bo(e.routeContainerSelector);return n||mo()||document.body}function yo(e){return e?e instanceof HTMLElement?e:Ue(e):null}function bo(e){if(!e)return null;const t=jt(),n=t?Ue(e,t):null;return n||Ue(e)}function mo(){const e=jt();if(!e)return null;const t=wo(e);if(t)return t;const n=vo(e);if(n)return n;const r=So(e);return r!==e?r:null}function jt(){const e=["#app","#root","#__next","#nuxt","[data-reactroot]","[data-v-app]","[data-vue-app]","[data-app-root]","[data-router-root]"];for(const t of e){const n=document.querySelector(t);if(n&&me(n))return n}return null}function wo(e){const t=v(e),n=Gt(e).filter(o=>!je(o)).filter(o=>v(o)>=Math.max(t*.2,48e3)),r=n.filter(o=>Eo(o));return r.length?r.sort((o,i)=>v(i)-v(o))[0]:n.length===1?n[0]:null}function vo(e){const t=["[data-route-root]","[data-router-view]","[data-page-root]","[data-page-container]",".router-view",".route-view",".page-root",".page-container",".page",".layout-content",".app-main",".app-content","main",'[role="main"]'],n=Yt(e),r=v(e);return t.flatMap(i=>Array.from(e.querySelectorAll(i))).filter(i=>me(i)&&!je(i)).filter(i=>Ge(i,n)<=3).filter(i=>v(i)>=Math.max(r*.2,48e3)).sort((i,s)=>Ge(i,n)-Ge(s,n)||v(s)-v(i))[0]||null}function So(e){let t=e;for(;;){const n=Gt(t).filter(o=>!je(o));if(n.length!==1)return t;const[r]=n;if(v(r)<Math.max(v(t)*.25,48e3))return t;t=r}}function Gt(e){return Array.from(e.children).filter(t=>t instanceof HTMLElement).filter(t=>me(t))}function Ue(e,t=document){try{const n=t.querySelector(e);return n&&me(n)?n:null}catch{return null}}function me(e){const t=window.getComputedStyle(e);if(t.display==="none"||t.visibility==="hidden"||Number(t.opacity)===0)return!1;const n=e.getBoundingClientRect();return n.width>0&&n.height>0}function je(e){const t=window.getComputedStyle(e);return t.position==="fixed"||t.position==="sticky"?!0:v(e)<24e3}function Eo(e){if(e.tagName.toLowerCase()==="main"||e.getAttribute("role")==="main")return!0;const t=e.id||"",n=typeof e.className=="string"?e.className:"",r=`${t} ${n}`.toLowerCase();return/(page|layout|content|container|main|router|route|view)/.test(r)}function v(e){const t=e.getBoundingClientRect();return Math.max(0,t.width)*Math.max(0,t.height)}function Yt(e){let t=0,n=e;for(;n&&n!==document.body;)t+=1,n=n.parentElement;return t}function Ge(e,t){return Math.max(0,Yt(e)-t)}function xo(e){try{const t=window.sessionStorage.getItem(e);if(!t)return null;const n=JSON.parse(t);return(n==null?void 0:n.version)!==1||!Array.isArray(n.fields)?null:n}catch{return null}}function ko(e,t){try{window.sessionStorage.setItem(e,JSON.stringify(t))}catch{}}function Wt(e){try{if(e){window.sessionStorage.removeItem(e);return}const t=[];for(let n=0;n<window.sessionStorage.length;n+=1){const r=window.sessionStorage.key(n);r!=null&&r.startsWith(Dt)&&t.push(r)}for(const n of t)window.sessionStorage.removeItem(n)}catch{}}function Xt(e,t="default"){const n=typeof location<"u"?location.pathname:"unknown-page",r=t.replace(/[^\w-]/g,"_")||"default";return`smart-fill:${e}:${n}:${r}:open`}function _o(e,t){if(typeof e=="string")return e;if(t.id)return`#${t.id}`;const n=typeof t.className=="string"?t.className.trim().split(/\s+/).filter(Boolean)[0]:"";return n?`.${n}`:t.tagName.toLowerCase()}function Zt(e,t){try{const n=window.localStorage.getItem(e);return n==null?t:n==="1"}catch{return t}}function Jt(e,t){if(e)try{window.localStorage.setItem(e,t?"1":"0")}catch{}}function Qt(){return`smart-fill:${typeof location<"u"?location.pathname:"unknown-page"}:local-priority`}function Co(){try{return window.localStorage.getItem(Qt())==="1"}catch{return!1}}function Ao(e){try{window.localStorage.setItem(Qt(),e?"1":"0")}catch{}}function To(e){return typeof CSS<"u"&&CSS.escape?CSS.escape(e):e.replace(/["\\]/g,"\\$&")}const f={status:"idle"},Ye=new qe,en=new hr;class tn{static async setup(t){if(typeof window>"u")return f.status="ready",Lo();if(f.status==="ready"&&f.apiKey===t.apiKey&&f.session)return f.session;if(f.status==="loading"&&f.apiKey===t.apiKey&&f.promise)return f.promise;f.apiKey&&f.apiKey!==t.apiKey&&($t(),en.destroyAll());const n=new cr(t);return f.status="loading",f.apiKey=t.apiKey,f.client=n,f.promise=n.createSession().then(r=>(console.log("SmartFill session created:",r),n.setAccessToken(r.apiKey),f.status="ready",f.session=r,Ye.emit("ready",{apiKey:t.apiKey}),r)).catch(r=>{throw f.status="error",r}),f.promise}static create(t={}){if(typeof window>"u")return Q();if(f.status!=="ready"||!f.client)throw w("SDK_NOT_READY","请先 await SmartFill.setup({ apiKey })。","setup");return new Nt(t,{client:f.client,manager:en})}}d(tn,"on",Ye.on.bind(Ye));function Lo(){return{apiKey:"server-mock",accessToken:"server-mock",expiresIn:0,refreshBefore:0,features:{text:!1,image:!1,ai:!1,localRuleOnly:!0},rulesVersion:"server"}}function Q(e){const t=()=>{};return{on:t,useAdapter:()=>Q(),registerFields:()=>Q(),unregisterFields:t,mount:()=>Q(),mountFloatingButton:()=>Q(),open:async()=>{},close:t,rescan:async()=>({scanToken:"server",fields:[]}),recognize:async()=>({scanToken:"server",suggestions:[],trace:{traceId:"server",usedOcr:!1,usedAi:!1,durationMs:0}}),apply:async()=>({applied:[],skipped:[]}),destroy:t}}h.DEFAULT_BASE_URL=Et,h.DomFiller=Pe,h.DomScanner=Mt,h.ElementAdapter=Jn,h.EventBus=qe,h.LocalRuleEngine=Tt,h.NativeAdapter=m,h.SmartFill=tn,h.SmartFillInstance=Nt,h.UiFrameworkAdapter=Oe,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
|
152
|
+
`,Qr=.75,eo=1800,Z=new Map;let J=0;const Be="smart-fill:routechange",Dt="smart-fill:session-scan:";let Pt=!1;const Ht=new WeakMap;let to=0;function $t(){Z.clear(),Wt(),J+=1}class Nt{constructor(t,n){d(this,"events",new qe);d(this,"scanner");d(this,"ruleEngine",new Tt);d(this,"panel",null);d(this,"panels",[]);d(this,"panelStorageKeys",new Map);d(this,"registeredFields",[]);d(this,"adapters",[Oe]);d(this,"scanResult",null);d(this,"autoApplyState",null);d(this,"formConfigVersion");d(this,"localPriorityEnabled",!1);d(this,"destroyed",!1);d(this,"scanCacheVersion",J);d(this,"floatingRouteSnapshot",null);d(this,"floatingLifecycleCleanup",null);d(this,"preScanTimer",null);this.config=t,this.context=n,this.scanner=new Mt(t.root||document),this.localPriorityEnabled=Co(),this.context.manager.add(this)}on(t,n){return this.events.on(t,n)}useAdapter(t){return this.assertAlive(),this.adapters.push(t),this}registerFields(t){this.assertAlive();const n=new Set;for(const r of t){const o=r.rowKey==null?r.fieldId:`${r.fieldId}:${r.rowKey}`;if(n.has(o))throw w("UNSUPPORTED_PAGE",`字段 ${o} 重复注册。`,"scan");n.add(o)}return this.registeredFields=t,this.scanResult=null,this.schedulePreScan(),this}unregisterFields(t){this.assertAlive(),this.registeredFields=t!=null&&t.length?this.registeredFields.filter(n=>!t.includes(n.fieldId)):[],this.scanResult=null,this.schedulePreScan()}mount(t){this.assertAlive();const n=typeof t=="string"?document.querySelector(t):t;if(!n)throw w("UNSUPPORTED_PAGE","未找到智能录入挂载点。","ui");const r=Xt("inline",_o(t,n)),o=Zt(r,!0);return this.createPanel("inline",r,o).mount(n),this.schedulePreScan(),this}mountFloatingButton(){this.assertAlive();const t=go(this.config),n=Xt("floating"),r=Zt(n,!0);return this.createPanel("floating",n,r).mount(t),this.bindFloatingLifecycle(t),this.schedulePreScan(),this}createPanel(t,n,r){let o;return o=new Wr({mode:t,initialOpen:r,messages:this.config.messages,localPriorityEnabled:this.localPriorityEnabled,onOpen:()=>this.open(o),onClose:()=>this.close(o),onRecognize:i=>(this.panel=o,this.recognize(i)),onLocalPriorityChange:i=>this.handleLocalPriorityChange(i,o)}),this.panels.push(o),this.panelStorageKeys.set(o,n),this.panel=o,o}handleLocalPriorityChange(t,n){this.localPriorityEnabled=t,Ao(t);for(const r of this.panels)r!==n&&r.setLocalPriorityEnabled(t)}async open(t=this.panel){this.assertAlive(),this.syncScanCacheVersion(),t&&(this.context.manager.activate(this),this.panel=t,t.setOpen(!0),Jt(this.panelStorageKeys.get(t),!0),this.scanResult||await this.rescan())}close(t=this.panel){t&&(this.panel=t,t.setOpen(!1),Jt(this.panelStorageKeys.get(t),!1))}async rescan(){return this.assertAlive(),this.syncScanCacheVersion(),this.cancelPreScan(),this.runScan({dynamicOptions:!0,emitStatus:!0})}async preScan(){return this.assertAlive(),this.syncScanCacheVersion(),this.runScan({dynamicOptions:!1,emitStatus:!1})}async runScan(t){var n;try{this.formConfigVersion=void 0;const r=this.getCachedPageScanResult(t.dynamicOptions),o=this.registeredFields.length?this.registeredFields:void 0,i=o?this.scanner.scan({registered:o,maxFields:this.config.maxFields}):r??(t.dynamicOptions?await this.scanner.scanWithDynamicOptions({maxFields:this.config.maxFields}):this.scanner.scan({maxFields:this.config.maxFields}));if(this.scanResult=i,this.cachePageScanResult(i,t.dynamicOptions),!this.scanResult.fields.length)throw w("NO_FIELDS_FOUND","当前页面未找到可回填字段。","scan");return t.emitStatus&&(this.events.emit("scanCompleted",{scanToken:this.scanResult.scanToken,fieldCount:this.scanResult.fields.length}),(n=this.panel)==null||n.setStatus(`已扫描 ${this.scanResult.fields.length} 个字段。`)),this.scanResult}catch(r){throw t.emitStatus&&this.emitError(r,"scan"),r}}schedulePreScan(){this.destroyed||!this.panels.length||(this.preScanTimer!=null&&window.clearTimeout(this.preScanTimer),this.preScanTimer=window.setTimeout(()=>{this.preScanTimer=null,!(this.destroyed||this.scanResult)&&this.preScan().catch(()=>{})},eo))}cancelPreScan(){this.preScanTimer!=null&&(window.clearTimeout(this.preScanTimer),this.preScanTimer=null)}async recognize(t){var i,s,a,c,l,u;this.assertAlive(),this.syncScanCacheVersion(),(i=this.panel)==null||i.setBusy(!0,"扫描中..."),(s=this.panel)==null||s.setStatus("扫描中...");const n=await this.rescan(),r=he("trace"),o=performance.now();this.events.emit("recognizing",{scanToken:n.scanToken,traceId:r}),(a=this.panel)==null||a.setBusy(!0,"识别中...");try{const g=n.fields.filter(y=>so(y.localRuleMode,this.localPriorityEnabled)),R=n.fields.filter(y=>y.localRuleMode!=="only"),{text:ee,usedOcr:we}=await this.context.client.resolveInputText({text:t.text,images:t.images,onStatusChange:y=>{var Se,O,Ee;if(y==="image_uploading"){(Se=this.panel)==null||Se.setStatus("图片上传中...");return}if(y==="image_recognizing"){(O=this.panel)==null||O.setStatus("图片识别中...");return}(Ee=this.panel)==null||Ee.setStatus("识别中...")}}),ve=ee?this.ruleEngine.recognize(ee,g,n.scanToken):[];let k;if(!R.length)k=Kt(n.scanToken,r,ve,o,void 0,we);else try{const y=await this.context.client.recognize({scanToken:n.scanToken,formCode:this.config.formCode,configVersion:this.formConfigVersion,text:ee,usedOcr:we,fields:R,onStatusChange:()=>{var O;(O=this.panel)==null||O.setStatus("识别中...")}});y.trace.durationMs=y.trace.durationMs||Math.round(performance.now()-o);const Se=io(ve,y.suggestions.filter(O=>R.some(Ee=>Ee.fieldId===O.fieldId)));k={...y,suggestions:Se}}catch(y){if(!ve.length)throw y;k=Kt(n.scanToken,r,ve,o,[we?"后端识别失败,已使用 OCR 文本触发本地识别继续回填。":"后端识别失败,已启用本地识别继续回填。"],we)}if(this.autoApplyState=ro(k.scanToken,k.trace.traceId,k.suggestions,n,this.registeredFields),this.events.emit("recognized",k),(c=this.panel)==null||c.setAutoApplyState(this.autoApplyState),this.config.apiEnable){const y=no(k,this.autoApplyState,n);this.config.apiCallback&&await Promise.resolve(this.config.apiCallback(y)),(l=this.panel)==null||l.setStatus(`识别完成,已返回 ${y.fields.length} 个字段。`)}else await this.applyAutoItems(this.autoApplyState);return k}catch(g){throw this.clearScanStateOnTokenExpired(g),this.emitError(g,"recognize"),g}finally{(u=this.panel)==null||u.setBusy(!1)}}async apply(t){var o;if(this.assertAlive(),this.syncScanCacheVersion(),!this.scanResult)throw w("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");this.events.emit("applying",{scanToken:t.scanToken,count:t.values.length});const r=await new Pe(this.scanResult.fields,this.registeredFields,this.adapters).apply(t);return this.events.emit("applied",r),(o=this.panel)==null||o.setApplyResult(r),r}async applyAutoItems(t){var a,c;if(!this.scanResult)throw w("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");const n=oo(t),r=t.items.filter(l=>Vt(l)).map(l=>({fieldId:l.fieldId,value:l.value,source:l.source}));this.events.emit("applying",{scanToken:t.scanToken,count:r.length}),(a=this.panel)==null||a.setStatus("识别完成,正在自动回填...");const i=await new Pe(this.scanResult.fields,this.registeredFields,this.adapters).apply({scanToken:t.scanToken,values:r}),s={...i,skipped:[...n,...i.skipped],warnings:[...i.warnings||[],...n.length?["部分字段因置信度或风险策略被跳过。"]:[]]};return this.events.emit("applied",s),(c=this.panel)==null||c.setApplyResult(s),s}destroy(){if(!this.destroyed){this.destroyed=!0,this.cancelPreScan(),this.clearFloatingLifecycle();for(const t of this.panels)t.destroy();this.panels.length=0,this.panelStorageKeys.clear(),this.panel=null,this.events.clear(),this.context.manager.remove(this)}}assertAlive(){if(this.destroyed)throw w("INSTANCE_DESTROYED","实例已销毁。","ui")}emitError(t,n){var o;const r=kt(t,n);this.events.emit("error",r),(o=this.panel)==null||o.setError(r.message)}getCachedPageScanResult(t){const n=this.getPageScanCacheKey();if(!n)return null;const r=Z.get(n);if(r&&(!t||r.dynamicOptionsReady)){const i=co(r.scanResult);if(!i)Z.delete(n);else return i}const o=this.getPersistedPageScanResult(t);return o?(this.cachePageScanResult(o.scanResult,o.dynamicOptionsReady),o.scanResult):null}cachePageScanResult(t,n){const r=this.getPageScanCacheKey();if(!r||!t.fields.length)return;const o=Z.get(r);o!=null&&o.dynamicOptionsReady&&!n||(Z.set(r,{scanResult:t,dynamicOptionsReady:(o==null?void 0:o.dynamicOptionsReady)||n}),this.persistPageScanResult(t,(o==null?void 0:o.dynamicOptionsReady)||n))}getPageScanCacheKey(){return this.registeredFields.length?null:po(this.config)}getSessionPageScanCacheKey(){return this.registeredFields.length||!this.config.formCode?null:fo(this.config)}clearScanStateOnTokenExpired(t){kt(t,"recognize").code==="TOKEN_EXPIRED"&&($t(),this.scanCacheVersion=J,this.scanResult=null,this.autoApplyState=null)}syncScanCacheVersion(){this.scanCacheVersion!==J&&(this.scanCacheVersion=J,this.scanResult=null,this.autoApplyState=null)}getPersistedPageScanResult(t){const n=this.getSessionPageScanCacheKey();if(!n)return null;const r=xo(n);if(!r||t&&!r.dynamicOptionsReady)return null;const o=this.scanner.scan({maxFields:this.config.maxFields}),i=lo(r,o);return i?{scanResult:i,dynamicOptionsReady:r.dynamicOptionsReady}:(Wt(n),null)}persistPageScanResult(t,n){const r=this.getSessionPageScanCacheKey();!r||!t.fields.length||ko(r,{version:1,dynamicOptionsReady:n,fields:t.fields.map(({element:o,scanToken:i,...s})=>s)})}bindFloatingLifecycle(t){this.clearFloatingLifecycle(),this.floatingRouteSnapshot=Ut(),ho();const n=()=>{this.destroyed||Ut()!==this.floatingRouteSnapshot&&this.destroy()};window.addEventListener(Be,n);const r=new MutationObserver(()=>{this.destroyed||t!==document.body&&!t.isConnected&&this.destroy()});r.observe(document.body,{childList:!0,subtree:!0}),this.floatingLifecycleCleanup=()=>{window.removeEventListener(Be,n),r.disconnect(),this.floatingRouteSnapshot=null,this.floatingLifecycleCleanup=null}}clearFloatingLifecycle(){var t;(t=this.floatingLifecycleCleanup)==null||t.call(this)}}function no(e,t,n){return{scanToken:e.scanToken,trace:e.trace,warnings:e.warnings,fields:t.items.map(r=>{const o=n.fields.find(i=>i.fieldId===r.fieldId);return{fieldId:r.fieldId,label:r.label,value:r.value,displayValue:r.displayValue||String(r.value??""),confidence:r.confidence,source:r.source,type:o==null?void 0:o.type,currentValue:r.currentValue,warnings:r.warnings}})}}function ro(e,t,n,r,o){return{scanToken:e,traceId:t,items:n.map(i=>{const s=r.fields.find(l=>l.fieldId===i.fieldId),a=o.find(l=>l.fieldId===i.fieldId),c=a!=null&&a.getValue?a.getValue():ao(s==null?void 0:s.element);return{applyItemId:`${i.fieldId}_${Math.random().toString(36).slice(2,8)}`,fieldId:i.fieldId,scanToken:e,groupId:i.groupId,label:i.label,value:i.value,currentValue:c,displayValue:i.displayValue||String(i.value??""),confidence:i.confidence,source:i.source,warnings:i.warnings,previousValue:c}})}}function Vt(e){var t;return e.confidence>=Qr&&!((t=e.warnings)!=null&&t.length)}function oo(e){return e.items.filter(t=>!Vt(t)).map(t=>{var n,r;return{fieldId:t.fieldId,label:t.label,attemptedValue:t.value,reason:((n=t.warnings)==null?void 0:n.join(";"))||`置信度 ${Math.round(t.confidence*100)}% 低于自动回填阈值`,reasonCode:(r=t.warnings)!=null&&r.length?"AUTO_APPLY_WARNING":"LOW_CONFIDENCE"}})}function io(e,t){const n=new Map;for(const r of t)n.set(r.fieldId,r);for(const r of e)n.has(r.fieldId)||n.set(r.fieldId,r);return[...n.values()]}function so(e,t){return e==="only"?!0:e==="off"?!1:t}function Kt(e,t,n,r,o,i=!1){return{scanToken:e,suggestions:n,warnings:o,trace:{traceId:t,usedOcr:i,usedAi:!1,durationMs:Math.round(performance.now()-r)}}}function ao(e){if(e)return e instanceof HTMLInputElement&&e.type==="checkbox"?e.checked:e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement?e.value:e.textContent}function co(e){const t=e.fields.map(n=>{if(n.source==="registered")return n;const r=uo(n);return r?{...n,element:r}:null});return t.some(n=>!n)?null:{...e,fields:t}}function lo(e,t){var s,a;const n=new Map(t.fields.map(c=>[c.fieldId,c])),r=[],o=new Set;for(const c of e.fields){const l=n.get(c.fieldId);if(!l)continue;o.add(c.fieldId);const u={...l,label:c.label||l.label,placeholder:c.placeholder??l.placeholder,name:c.name??l.name,id:c.id??l.id,section:c.section??l.section,options:(s=c.options)!=null&&s.length?c.options:l.options,required:c.required??l.required,readonly:c.readonly??l.readonly,disabled:c.disabled??l.disabled};u.fingerprint=X({...u,tagName:(a=u.element)==null?void 0:a.tagName.toLowerCase()}),r.push(u)}if(!r.length)return null;const i=t.fields.filter(c=>!o.has(c.fieldId));return{...t,fields:[...r,...i]}}function uo(e){var t;if((t=e.element)!=null&&t.isConnected)return e.element;try{return document.querySelector(`[data-smart-fill-id="${To(e.fieldId)}"]`)}catch{return null}}function po(e){const t=typeof location<"u"?`${location.origin}${location.pathname}${location.search}`:"unknown-page",n=zt(e.root||document);return`smart-fill:${t}:${e.formCode||"default-form"}:${n}:${e.maxFields??200}:scan`}function fo(e){const t=zt(e.root||document);return`${Dt}${e.formCode||"default-form"}:${t}:${e.maxFields??200}`}function zt(e){if(e===document)return"document";const t=Ht.get(e);if(t)return t;const n=e instanceof HTMLElement&&e.id?`el:${e.id}`:`root:${++to}`;return Ht.set(e,n),n}function ho(){if(Pt||typeof window>"u")return;Pt=!0;const e=()=>{window.dispatchEvent(new Event(Be))};window.addEventListener("popstate",e),window.addEventListener("hashchange",e),Bt("pushState",e),Bt("replaceState",e)}function Bt(e,t){const n=window.history[e];window.history[e]=function(...o){const i=n.apply(this,o);return t(),i}}function Ut(){return typeof location>"u"?"unknown-route":`${location.pathname}${location.search}${location.hash}`}function go(e){const t=yo(e.floatingContainer);if(t)return t;const n=bo(e.routeContainerSelector);return n||mo()||document.body}function yo(e){return e?e instanceof HTMLElement?e:Ue(e):null}function bo(e){if(!e)return null;const t=jt(),n=t?Ue(e,t):null;return n||Ue(e)}function mo(){const e=jt();if(!e)return null;const t=wo(e);if(t)return t;const n=vo(e);if(n)return n;const r=So(e);return r!==e?r:null}function jt(){const e=["#app","#root","#__next","#nuxt","[data-reactroot]","[data-v-app]","[data-vue-app]","[data-app-root]","[data-router-root]"];for(const t of e){const n=document.querySelector(t);if(n&&me(n))return n}return null}function wo(e){const t=v(e),n=Gt(e).filter(o=>!je(o)).filter(o=>v(o)>=Math.max(t*.2,48e3)),r=n.filter(o=>Eo(o));return r.length?r.sort((o,i)=>v(i)-v(o))[0]:n.length===1?n[0]:null}function vo(e){const t=["[data-route-root]","[data-router-view]","[data-page-root]","[data-page-container]",".router-view",".route-view",".page-root",".page-container",".page",".layout-content",".app-main",".app-content","main",'[role="main"]'],n=Yt(e),r=v(e);return t.flatMap(i=>Array.from(e.querySelectorAll(i))).filter(i=>me(i)&&!je(i)).filter(i=>Ge(i,n)<=3).filter(i=>v(i)>=Math.max(r*.2,48e3)).sort((i,s)=>Ge(i,n)-Ge(s,n)||v(s)-v(i))[0]||null}function So(e){let t=e;for(;;){const n=Gt(t).filter(o=>!je(o));if(n.length!==1)return t;const[r]=n;if(v(r)<Math.max(v(t)*.25,48e3))return t;t=r}}function Gt(e){return Array.from(e.children).filter(t=>t instanceof HTMLElement).filter(t=>me(t))}function Ue(e,t=document){try{const n=t.querySelector(e);return n&&me(n)?n:null}catch{return null}}function me(e){const t=window.getComputedStyle(e);if(t.display==="none"||t.visibility==="hidden"||Number(t.opacity)===0)return!1;const n=e.getBoundingClientRect();return n.width>0&&n.height>0}function je(e){const t=window.getComputedStyle(e);return t.position==="fixed"||t.position==="sticky"?!0:v(e)<24e3}function Eo(e){if(e.tagName.toLowerCase()==="main"||e.getAttribute("role")==="main")return!0;const t=e.id||"",n=typeof e.className=="string"?e.className:"",r=`${t} ${n}`.toLowerCase();return/(page|layout|content|container|main|router|route|view)/.test(r)}function v(e){const t=e.getBoundingClientRect();return Math.max(0,t.width)*Math.max(0,t.height)}function Yt(e){let t=0,n=e;for(;n&&n!==document.body;)t+=1,n=n.parentElement;return t}function Ge(e,t){return Math.max(0,Yt(e)-t)}function xo(e){try{const t=window.sessionStorage.getItem(e);if(!t)return null;const n=JSON.parse(t);return(n==null?void 0:n.version)!==1||!Array.isArray(n.fields)?null:n}catch{return null}}function ko(e,t){try{window.sessionStorage.setItem(e,JSON.stringify(t))}catch{}}function Wt(e){try{if(e){window.sessionStorage.removeItem(e);return}const t=[];for(let n=0;n<window.sessionStorage.length;n+=1){const r=window.sessionStorage.key(n);r!=null&&r.startsWith(Dt)&&t.push(r)}for(const n of t)window.sessionStorage.removeItem(n)}catch{}}function Xt(e,t="default"){const n=typeof location<"u"?location.pathname:"unknown-page",r=t.replace(/[^\w-]/g,"_")||"default";return`smart-fill:${e}:${n}:${r}:open`}function _o(e,t){if(typeof e=="string")return e;if(t.id)return`#${t.id}`;const n=typeof t.className=="string"?t.className.trim().split(/\s+/).filter(Boolean)[0]:"";return n?`.${n}`:t.tagName.toLowerCase()}function Zt(e,t){try{const n=window.localStorage.getItem(e);return n==null?t:n==="1"}catch{return t}}function Jt(e,t){if(e)try{window.localStorage.setItem(e,t?"1":"0")}catch{}}function Qt(){return`smart-fill:${typeof location<"u"?location.pathname:"unknown-page"}:local-priority`}function Co(){try{return window.localStorage.getItem(Qt())==="1"}catch{return!1}}function Ao(e){try{window.localStorage.setItem(Qt(),e?"1":"0")}catch{}}function To(e){return typeof CSS<"u"&&CSS.escape?CSS.escape(e):e.replace(/["\\]/g,"\\$&")}const f={status:"idle"},Ye=new qe,en=new hr;class tn{static async setup(t){if(typeof window>"u")return f.status="ready",Lo();if(f.status==="ready"&&f.apiKey===t.apiKey&&f.session)return f.session;if(f.status==="loading"&&f.apiKey===t.apiKey&&f.promise)return f.promise;f.apiKey&&f.apiKey!==t.apiKey&&($t(),en.destroyAll());const n=new cr(t);return f.status="loading",f.apiKey=t.apiKey,f.client=n,f.promise=n.createSession().then(r=>(console.log("SmartFill session created:",r),n.setAccessToken(r.apiKey),f.status="ready",f.session=r,Ye.emit("ready",{apiKey:t.apiKey}),r)).catch(r=>{throw f.status="error",r}),f.promise}static create(t={}){if(typeof window>"u")return Q();if(f.status!=="ready"||!f.client)throw w("SDK_NOT_READY","请先 await SmartFill.setup({ apiKey })。","setup");return new Nt(t,{client:f.client,manager:en})}}d(tn,"on",Ye.on.bind(Ye));function Lo(){return{apiKey:"server-mock",accessToken:"server-mock",expiresIn:0,refreshBefore:0,features:{text:!1,image:!1,ai:!1,localRuleOnly:!0},rulesVersion:"server"}}function Q(e){const t=()=>{};return{on:t,useAdapter:()=>Q(),registerFields:()=>Q(),unregisterFields:t,mount:()=>Q(),mountFloatingButton:()=>Q(),open:async()=>{},close:t,rescan:async()=>({scanToken:"server",fields:[]}),recognize:async()=>({scanToken:"server",suggestions:[],trace:{traceId:"server",usedOcr:!1,usedAi:!1,durationMs:0}}),apply:async()=>({applied:[],skipped:[]}),destroy:t}}h.DEFAULT_BASE_URL=Et,h.DomFiller=Pe,h.DomScanner=Mt,h.ElementAdapter=Jn,h.EventBus=qe,h.LocalRuleEngine=Tt,h.NativeAdapter=m,h.SmartFill=tn,h.SmartFillInstance=Nt,h.UiFrameworkAdapter=Oe,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|