@hmg-report-editor/rules 1.0.0 → 1.0.1

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.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function m(o){return JSON.stringify([o.editorId,o.controlId])}const Ie=/【\s*([^】]+?)\s*】/g,be="CONTROL_RULE_FAILED",Y=100;function ve(o={}){const l=new Map,i=new Map,a=new Map,c=new Map,f=new Map,g=new Map,h=[];let w=new Set,q=new Map,R=null,T=null,y=0,P=0;const S=new Set,re={name:"hmg-widget-rules",getCurrentBatchId:()=>h[h.length-1],runBatch(e){h.push(Ce());try{const t=e();if(ne(t))throw new Error("runBatch requires a synchronous operation.");return t}finally{h.pop(),h.length===0&&ie()}},runWithoutEvaluation(e){if(h.length>0)throw new Error("runWithoutEvaluation cannot run inside an active rules batch.");y+=1;try{const t=e();if(ne(t))throw new Error("runWithoutEvaluation requires a synchronous operation.");return t}finally{y-=1,y===0&&de()}},onEditorReady(e){l.set(e.editorId,e)},onEditorDestroyed(e){l.get(e.editorId)===e&&l.delete(e.editorId);for(const t of Array.from(i.values()))t.editorId===e.editorId&&A(t)},onControlAdded(e,t){$(e,t);const n=m(e);if(y>0){S.add(n);return}E(new Set([n]),t),_(e,t)},onControlRemoved(e,t){const n=t.getControl(e.controlId);if(n){A(e),$(n,t),y>0&&S.add(m(n));return}const r=y>0?new Set:D(e);if(A(e),y>0){S.delete(m(e));return}E(r,t)},onControlValueChanged(e,t){const n=m(e.current);if(i.set(n,e.current),oe(e.previous,e.current),y>0){g.delete(n),S.add(n);return}_(e.current,t)},onControlMetadataChanged(e,t){const n=y>0?new Set:D(e.previous);if(A(e.previous),$(e.current,t),y>0){S.add(m(e.current));return}E(new Set([m(e.current)]),t),_(e.current,t),E(n,t)}};function $(e,t){const n=m(e);i.set(n,e);const r=b(e);if(r&&U(r,n),C(e.attrs.rules).length===0)return;if(!e.attrs.templateId){I(t,"Rule control is missing templateId and will be skipped.",{control:e});return}if(!e.attrs.batchId){I(t,"Rule control is missing batchId and will be skipped.",{control:e});return}const s=we(e);f.set(n,s);for(const d of s){let p=c.get(d);p||(p=new Set,c.set(d,p)),p.add(n)}}function A(e){const t=m(e);i.delete(t),g.delete(t);const n=b(e);n&&L(n,t);const r=f.get(t);if(r)for(const u of r){const s=c.get(u);s==null||s.delete(t),(s==null?void 0:s.size)===0&&c.delete(u)}f.delete(t)}function D(e){const t=b(e);return t?new Set(c.get(t)??[]):new Set}function oe(e,t){const n=b(e),r=b(t);n&&n!==r&&L(n,m(e)),r&&U(r,m(t))}function U(e,t){let n=a.get(e);n||(n=new Set,a.set(e,n)),n.add(t)}function L(e,t){const n=a.get(e);n==null||n.delete(t),(n==null?void 0:n.size)===0&&a.delete(e)}function ue(e,t){const n=a.get(e);let r;for(const s of n??[]){const d=i.get(s),p=d?l.get(d.editorId)??((t==null?void 0:t.editorId)===d.editorId?t:void 0):void 0,N=d?p==null?void 0:p.getControl(d.controlId):void 0;N&&b(N)===e?(i.set(s,N),r=s):(i.delete(s),n==null||n.delete(s))}if((n==null?void 0:n.size)===0&&a.delete(e),r)return r;const u=new Set(l.values());t&&u.add(t);for(const s of u)for(const d of s.getControls())if(b(d)===e){const p=m(d);i.set(p,d),U(e,p),r=p}return r}function _(e,t){const n=b(e);if(!n)return;const r=c.get(n);r&&E(new Set(r),t,m(e))}function E(e,t,n){if(!(y>0||e.size===0)){if(h.length>0&&!T){for(const r of e)w.add(r);B(q,n,e),R??(R=t);return}j(e,t,n)}}function ie(){if(w.size===0||!R){w.clear(),q.clear(),R=null;return}const e=w,t=q,n=R;w=new Set,q=new Map,R=null,j(e,n,void 0,t)}function j(e,t,n,r){if(e.size===0)return;if(T){B(T.propagationEdges,n,e),J(T,e,t,n);return}const u={evaluatingTargetKeys:new Set,queuedTargetKeys:new Set,processedTargetKeys:new Set,pendingTargetKeys:[],propagationEdges:ae(r),processedCount:0};B(u.propagationEdges,n,e),T=u,J(u,e,t,n);try{se(u,t)}finally{T=null}}function J(e,t,n,r){for(const u of t){if(e.evaluatingTargetKeys.has(u)){r&&k(e.propagationEdges,u,r)&&G(n,u);continue}if(!e.queuedTargetKeys.has(u)){if(e.processedTargetKeys.has(u)&&r&&k(e.propagationEdges,u,r)){G(n,u);continue}e.queuedTargetKeys.add(u),e.pendingTargetKeys.push(u)}}}function se(e,t){for(;e.pendingTargetKeys.length>0;){if(e.processedCount>=Y){I(t,"Rule evaluation limit exceeded and propagation was stopped.",{maxRuleEvaluations:Y,pendingTargetKeys:[...e.pendingTargetKeys]}),e.pendingTargetKeys=[],e.queuedTargetKeys.clear();return}const n=e.pendingTargetKeys.shift();if(n){e.queuedTargetKeys.delete(n),e.evaluatingTargetKeys.add(n),e.processedTargetKeys.add(n),e.processedCount+=1;try{le(n,t)}finally{e.evaluatingTargetKeys.delete(n)}}}}function B(e,t,n){if(!t)return;let r=e.get(t);r||(r=new Set,e.set(t,r));for(const u of n)r.add(u)}function ae(e){const t=new Map;for(const[n,r]of e??[])t.set(n,new Set(r));return t}function k(e,t,n){if(t===n)return!0;const r=[t],u=new Set;for(;r.length>0;){const s=r.pop();if(!(!s||u.has(s))){u.add(s);for(const d of e.get(s)??[]){if(d===n)return!0;u.has(d)||r.push(d)}}}return!1}function G(e,t){const n=i.get(t);I(e,"Rule cycle detected and skipped.",{editorId:n==null?void 0:n.editorId,controlId:n==null?void 0:n.controlId})}function le(e,t){const n=i.get(e);if(!n)return;const r=l.get(n.editorId)??(t.editorId===n.editorId?t:void 0),u=r==null?void 0:r.getControl(n.controlId);if(!r||!u)return;i.set(e,u);const s=C(u.attrs.rules);if(s.length===0)return;if(!u.attrs.templateId){I(r,"Rule target is missing templateId and will be skipped.",{target:u});return}if(!u.attrs.batchId){I(r,"Rule target is missing batchId and will be skipped.",{target:u});return}const d=Q(u,s,r);if(d){const p=d.value;r.updateControlAttrs(u.controlId,{value:p,ruleStyleColor:d.ruleStyleColor})&&g.set(e,p);return}if(u.attrs.noMatchDefault&&me(u)){const p=u.attrs.defaultValue??"";r.updateControlAttrs(u.controlId,{value:p,ruleStyleColor:null})&&g.set(e,p)}}function Q(e,t,n){let r=null;for(const u of t)for(const s of O(u)){if(!ce(e,s,n))continue;const d=fe(e,s,n);if(d!==null){r=d;break}}return r}function de(){P+=1;try{for(const e of S){const t=i.get(e);if(!t)continue;const n=l.get(t.editorId),r=n==null?void 0:n.getControl(t.controlId);if(!n||!r||!r.attrs.templateId||!r.attrs.batchId)continue;const u=C(r.attrs.rules);if(u.length===0)continue;const s=Q(r,u,n);s&&M(s.value)===M(r.attrs.value)&&g.set(e,s.value)}}finally{P-=1,S.clear()}}function ce(e,t,n){for(const r of Z(t))try{const u=pe(e,r,n);if(!he(u,r))return!1}catch(u){return I(n,"Rule premise failed and was skipped.",{target:e,premise:r,error:u}),!1}return!0}function fe(e,t,n){try{const r=t.result,u=r.type==="math"?ge(e,r,n):r.value,s=!!(r.style??r.bStyle);return{value:u,ruleStyleColor:s&&r.styleValue?r.styleValue:null}}catch(r){return I(n,"Rule result failed and was skipped.",{target:e,content:t,error:r}),null}}function pe(e,t,n){if(t.entryType==="math")return String(ee(X(e,t.inputSerialMath,n)));const r=K(t.inputSerialMath)[0];if(!r)throw new Error(`Rule premise has no widget reference: ${t.inputSerialMath}.`);return M(H(e,r,n))}function ge(e,t,n){const r=ee(X(e,t.value,n)),u=Re(t.exactType),s=t.exact?Math.max(0,Number(t.exactNum??0)):Math.max(0,15-Math.trunc(Math.abs(r)).toString().length);return String(Ee(r,u,s))}function X(e,t,n){const r=K(t);let u=t;for(const s of r){const d=H(e,s,n),p=v(d);if(p===null)throw new Error(`Referenced widget ${s} is not a number.`);u=u.replace(new RegExp(`【\\s*${Me(s)}\\s*】`,"g"),String(p))}return u}function H(e,t,n){const r=z(e.attrs.templateId,e.attrs.batchId,t),u=r?ue(r,n):void 0,s=u?i.get(u):void 0;if(!s)throw I(n,"Referenced rule control was not found.",{target:e,widgetNumber:t,templateId:e.attrs.templateId,batchId:e.attrs.batchId}),new Error(`Referenced widget not found: ${t}.`);return s.attrs.value}function he(e,t){const n=Te(t.condition),r=t.conditionDes??"",u=t.conditionRange??{};switch(n){case"inOption":return V(r,e);case"notInOption":return!V(r,e);case"inRange":return x(e,u);case"notInRange":return!x(e,u);case"empty":return e==="";case"notEmpty":return e!=="";case"include":return e.includes(r);case"notInclude":return!e.includes(r);case"=":return e===String(r);case"≠":return e!==String(r);case"greater":return F(e,r,(s,d)=>s>d);case"greaterEqual":return F(e,r,(s,d)=>s>=d);case"less":return F(e,r,(s,d)=>s<d);case"lessEqual":return F(e,r,(s,d)=>s<=d);case"isNumber":return v(e)!==null;case"isNotNumber":return v(e)===null;default:return!1}}function me(e){const t=M(e.attrs.value),n=m(e);return t===""||g.has(n)&&t===M(g.get(n))?!0:ye(e).has(t)}function ye(e){const t=new Set;for(const n of C(e.attrs.rules))for(const r of O(n))r.result.type!=="math"&&t.add(String(r.result.value??""));return t}function we(e){const t=new Set;for(const n of C(e.attrs.rules))for(const r of O(n)){for(const u of Z(r))for(const s of K(u.inputSerialMath)){const d=z(e.attrs.templateId,e.attrs.batchId,s);d&&t.add(d)}if(r.result.type==="math")for(const u of K(r.result.value)){const s=z(e.attrs.templateId,e.attrs.batchId,u);s&&t.add(s)}}return t}function I(e,t,n){var r;P>0||(e.warn(be,t,n),(r=o.onWarning)==null||r.call(o,{message:t,detail:n}))}return re}function C(o){return Array.isArray(o)?o:[]}function O(o){return o.contents??o.list??[]}function Z(o){return o.premises??o.premiss??[]}function K(o){return Array.from(o.matchAll(Ie),l=>l[1].trim()).filter(Boolean)}function b(o){return z(o.attrs.templateId,o.attrs.batchId,o.attrs.widgetNumber)}function z(o,l,i){const a=W(o),c=W(l),f=Se(i);if(!(!a||!c||!f))return`${a}_${c}_${f}`}function W(o){return o==null?void 0:String(o).trim()||void 0}function Se(o){const l=W(o);if(!l)return;const i=l.match(/^【\s*([^】]+?)\s*】$/);return((i==null?void 0:i[1])??l).trim()||void 0}function Te(o){switch(o){case">":return"greater";case">=":return"greaterEqual";case"<":return"less";case"<=":return"lessEqual";default:return o}}function M(o){return o==null?"":String(o)}function V(o,l){if(Array.isArray(o))return o.map(i=>String(i)).includes(l);if(typeof o=="string"){const i=o.trim();if(i.startsWith("["))try{const a=JSON.parse(i);return Array.isArray(a)&&a.map(c=>String(c)).includes(l)}catch{return o.includes(l)}return o.includes(l)}return!1}function x(o,l){const i=v(o),a=v(l.number1),c=v(l.number2);if(i===null||a===null||c===null)return!1;const f=l.symbol1??"greaterEqual",g=l.symbol2??"lessEqual",h=f==="greater"?i>a:i>=a,w=g==="less"?i<c:i<=c;return h&&w}function F(o,l,i){const a=v(o),c=v(l);return a!==null&&c!==null&&i(a,c)}function v(o){if(o==null||typeof o=="string"&&o.trim()==="")return null;const l=Number(o);return Number.isFinite(l)?l:null}function Re(o){return o==="2"||o==="3"?o:"1"}function Ee(o,l,i){const a=10**i;switch(l){case"2":return Math.ceil(o*a)/a;case"3":return Math.floor(o*a)/a;default:return Math.round(o*a)/a}}function ee(o){const l=Ne(o),i=[],a=[],c=()=>{const f=a.pop();if(!f)throw new Error("Formula operator stack is empty.");if(f==="u-"){const w=i.pop();if(w===void 0)throw new Error("Formula unary operand is missing.");i.push(-w);return}const g=i.pop(),h=i.pop();if(h===void 0||g===void 0)throw new Error("Formula operand is missing.");switch(f){case"+":i.push(h+g);return;case"-":i.push(h-g);return;case"*":i.push(h*g);return;case"/":i.push(h/g);return;default:throw new Error(`Unsupported formula operator: ${f}.`)}};for(const f of l){if(typeof f=="number"){i.push(f);continue}if(f==="("){a.push(f);continue}if(f===")"){for(;a.length>0&&a[a.length-1]!=="(";)c();if(a.pop()!=="(")throw new Error("Formula parentheses are not balanced.");continue}for(;a.length>0&&a[a.length-1]!=="("&&te(a[a.length-1])>=te(f);)c();a.push(f)}for(;a.length>0;){if(a[a.length-1]==="(")throw new Error("Formula parentheses are not balanced.");c()}if(i.length!==1||!Number.isFinite(i[0]))throw new Error("Formula result is invalid.");return i[0]}function Ne(o){const l=[];let i=0,a;for(;i<o.length;){const c=o[i];if(/\s/.test(c)){i+=1;continue}if(c==="("||c===")"){l.push(c),a=c,i+=1;continue}if("+-*/".includes(c)){const h=c==="-"&&(a===void 0||typeof a=="string"&&a!==")");l.push(h?"u-":c),a=h?"u-":c,i+=1;continue}const f=o.slice(i).match(/^\d+(?:\.\d+)?|^\.\d+/);if(!f)throw new Error(`Formula contains unsupported token near: ${o.slice(i)}.`);const g=Number(f[0]);if(!Number.isFinite(g))throw new Error(`Formula number is invalid: ${f[0]}.`);l.push(g),a=g,i+=f[0].length}return l}function te(o){return o==="u-"?3:o==="*"||o==="/"?2:o==="+"||o==="-"?1:0}function Ce(){const o=globalThis.crypto;return o!=null&&o.randomUUID?`batch_${o.randomUUID()}`:`batch_${Date.now().toString(36)}_${Math.random().toString(36).slice(2)}`}function ne(o){return(typeof o=="object"||typeof o=="function")&&o!==null&&"then"in o&&typeof o.then=="function"}function Me(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}exports.createWidgetRulesPlugin=ve;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function m(o){return JSON.stringify([o.editorId,o.controlId])}const Ie=/【\s*([^】]+?)\s*】/g,be="CONTROL_RULE_FAILED",Y=100;function ve(o={}){const l=new Map,i=new Map,a=new Map,c=new Map,f=new Map,g=new Map,h=[];let w=new Set,q=new Map,R=null,T=null,y=0,P=0;const S=new Set,re={name:"hmg-widget-rules",getCurrentBatchId:()=>h[h.length-1],runBatch(e){h.push(Ce());try{const t=e();if(ne(t))throw new Error("runBatch requires a synchronous operation.");return t}finally{h.pop(),h.length===0&&ie()}},runWithoutEvaluation(e){if(h.length>0)throw new Error("runWithoutEvaluation cannot run inside an active rules batch.");y+=1;try{const t=e();if(ne(t))throw new Error("runWithoutEvaluation requires a synchronous operation.");return t}finally{y-=1,y===0&&de()}},onEditorReady(e){l.set(e.editorId,e)},onEditorDestroyed(e){l.get(e.editorId)===e&&l.delete(e.editorId);for(const t of Array.from(i.values()))t.editorId===e.editorId&&K(t)},onControlAdded(e,t){$(e,t);const n=m(e);if(y>0){S.add(n);return}E(new Set([n]),t),_(e,t)},onControlRemoved(e,t){const n=t.getControl(e.controlId);if(n){K(e),$(n,t),y>0&&S.add(m(n));return}const r=y>0?new Set:D(e);if(K(e),y>0){S.delete(m(e));return}E(r,t)},onControlValueChanged(e,t){const n=m(e.current);if(i.set(n,e.current),oe(e.previous,e.current),y>0){g.delete(n),S.add(n);return}_(e.current,t)},onControlMetadataChanged(e,t){const n=y>0?new Set:D(e.previous);if(K(e.previous),$(e.current,t),y>0){S.add(m(e.current));return}E(new Set([m(e.current)]),t),_(e.current,t),E(n,t)}};function $(e,t){const n=m(e);i.set(n,e);const r=b(e);if(r&&U(r,n),C(e.attrs.rules).length===0)return;if(!e.attrs.templateId){I(t,"Rule control is missing templateId and will be skipped.",{control:e});return}if(!e.attrs.batchId){I(t,"Rule control is missing batchId and will be skipped.",{control:e});return}const s=we(e);f.set(n,s);for(const d of s){let p=c.get(d);p||(p=new Set,c.set(d,p)),p.add(n)}}function K(e){const t=m(e);i.delete(t),g.delete(t);const n=b(e);n&&L(n,t);const r=f.get(t);if(r)for(const u of r){const s=c.get(u);s==null||s.delete(t),(s==null?void 0:s.size)===0&&c.delete(u)}f.delete(t)}function D(e){const t=b(e);return t?new Set(c.get(t)??[]):new Set}function oe(e,t){const n=b(e),r=b(t);n&&n!==r&&L(n,m(e)),r&&U(r,m(t))}function U(e,t){let n=a.get(e);n||(n=new Set,a.set(e,n)),n.add(t)}function L(e,t){const n=a.get(e);n==null||n.delete(t),(n==null?void 0:n.size)===0&&a.delete(e)}function ue(e,t){const n=a.get(e);let r;for(const s of n??[]){const d=i.get(s),p=d?l.get(d.editorId)??((t==null?void 0:t.editorId)===d.editorId?t:void 0):void 0,N=d?p==null?void 0:p.getControl(d.controlId):void 0;N&&b(N)===e?(i.set(s,N),r=s):(i.delete(s),n==null||n.delete(s))}if((n==null?void 0:n.size)===0&&a.delete(e),r)return r;const u=new Set(l.values());t&&u.add(t);for(const s of u)for(const d of s.getControls())if(b(d)===e){const p=m(d);i.set(p,d),U(e,p),r=p}return r}function _(e,t){const n=b(e);if(!n)return;const r=c.get(n);r&&E(new Set(r),t,m(e))}function E(e,t,n){if(!(y>0||e.size===0)){if(h.length>0&&!T){for(const r of e)w.add(r);B(q,n,e),R??(R=t);return}j(e,t,n)}}function ie(){if(w.size===0||!R){w.clear(),q.clear(),R=null;return}const e=w,t=q,n=R;w=new Set,q=new Map,R=null,j(e,n,void 0,t)}function j(e,t,n,r){if(e.size===0)return;if(T){B(T.propagationEdges,n,e),J(T,e,t,n);return}const u={evaluatingTargetKeys:new Set,queuedTargetKeys:new Set,processedTargetKeys:new Set,pendingTargetKeys:[],propagationEdges:ae(r),processedCount:0};B(u.propagationEdges,n,e),T=u,J(u,e,t,n);try{se(u,t)}finally{T=null}}function J(e,t,n,r){for(const u of t){if(e.evaluatingTargetKeys.has(u)){r&&k(e.propagationEdges,u,r)&&G(n,u);continue}if(!e.queuedTargetKeys.has(u)){if(e.processedTargetKeys.has(u)&&r&&k(e.propagationEdges,u,r)){G(n,u);continue}e.queuedTargetKeys.add(u),e.pendingTargetKeys.push(u)}}}function se(e,t){for(;e.pendingTargetKeys.length>0;){if(e.processedCount>=Y){I(t,"Rule evaluation limit exceeded and propagation was stopped.",{maxRuleEvaluations:Y,pendingTargetKeys:[...e.pendingTargetKeys]}),e.pendingTargetKeys=[],e.queuedTargetKeys.clear();return}const n=e.pendingTargetKeys.shift();if(n){e.queuedTargetKeys.delete(n),e.evaluatingTargetKeys.add(n),e.processedTargetKeys.add(n),e.processedCount+=1;try{le(n,t)}finally{e.evaluatingTargetKeys.delete(n)}}}}function B(e,t,n){if(!t)return;let r=e.get(t);r||(r=new Set,e.set(t,r));for(const u of n)r.add(u)}function ae(e){const t=new Map;for(const[n,r]of e??[])t.set(n,new Set(r));return t}function k(e,t,n){if(t===n)return!0;const r=[t],u=new Set;for(;r.length>0;){const s=r.pop();if(!(!s||u.has(s))){u.add(s);for(const d of e.get(s)??[]){if(d===n)return!0;u.has(d)||r.push(d)}}}return!1}function G(e,t){const n=i.get(t);I(e,"Rule cycle detected and skipped.",{editorId:n==null?void 0:n.editorId,controlId:n==null?void 0:n.controlId})}function le(e,t){const n=i.get(e);if(!n)return;const r=l.get(n.editorId)??(t.editorId===n.editorId?t:void 0),u=r==null?void 0:r.getControl(n.controlId);if(!r||!u)return;i.set(e,u);const s=C(u.attrs.rules);if(s.length===0)return;if(!u.attrs.templateId){I(r,"Rule target is missing templateId and will be skipped.",{target:u});return}if(!u.attrs.batchId){I(r,"Rule target is missing batchId and will be skipped.",{target:u});return}const d=Q(u,s,r);if(d){const p=d.value;r.updateControlAttrs(u.controlId,{value:p,ruleStyleColor:d.ruleStyleColor})&&g.set(e,p);return}if(u.attrs.noMatchDefault&&me(u)){const p=u.attrs.defaultValue??"";r.updateControlAttrs(u.controlId,{value:p,ruleStyleColor:null})&&g.set(e,p)}}function Q(e,t,n){let r=null;for(const u of t)for(const s of O(u)){if(!ce(e,s,n))continue;const d=fe(e,s,n);if(d!==null){r=d;break}}return r}function de(){P+=1;try{for(const e of S){const t=i.get(e);if(!t)continue;const n=l.get(t.editorId),r=n==null?void 0:n.getControl(t.controlId);if(!n||!r||!r.attrs.templateId||!r.attrs.batchId)continue;const u=C(r.attrs.rules);if(u.length===0)continue;const s=Q(r,u,n);s&&M(s.value)===M(r.attrs.value)&&g.set(e,s.value)}}finally{P-=1,S.clear()}}function ce(e,t,n){for(const r of Z(t))try{const u=pe(e,r,n);if(!he(u,r))return!1}catch(u){return I(n,"Rule premise failed and was skipped.",{target:e,premise:r,error:u}),!1}return!0}function fe(e,t,n){try{const r=t.result,u=r.type==="math"?ge(e,r,n):r.value,s=!!(r.style??r.bStyle);return{value:u,ruleStyleColor:s&&r.styleValue?r.styleValue:null}}catch(r){return I(n,"Rule result failed and was skipped.",{target:e,content:t,error:r}),null}}function pe(e,t,n){if(t.entryType==="math")return String(ee(X(e,t.inputSerialMath,n)));const r=A(t.inputSerialMath)[0];if(!r)throw new Error(`Rule premise has no widget reference: ${t.inputSerialMath}.`);return M(H(e,r,n))}function ge(e,t,n){const r=ee(X(e,t.value,n)),u=Re(t.exactType),s=t.exact?Math.max(0,Number(t.exactNum??0)):Math.max(0,15-Math.trunc(Math.abs(r)).toString().length);return String(Ee(r,u,s))}function X(e,t,n){const r=A(t);let u=t;for(const s of r){const d=H(e,s,n),p=v(d);if(p===null)throw new Error(`Referenced widget ${s} is not a number.`);u=u.replace(new RegExp(`【\\s*${Me(s)}\\s*】`,"g"),String(p))}return u}function H(e,t,n){const r=z(e.attrs.templateId,e.attrs.batchId,t),u=r?ue(r,n):void 0,s=u?i.get(u):void 0;if(!s)throw I(n,"Referenced rule control was not found.",{target:e,widgetNumber:t,templateId:e.attrs.templateId,batchId:e.attrs.batchId}),new Error(`Referenced widget not found: ${t}.`);return s.attrs.value}function he(e,t){const n=Te(t.condition),r=t.conditionDes??"",u=t.conditionRange??{};switch(n){case"inOption":return V(r,e);case"notInOption":return!V(r,e);case"inRange":return x(e,u);case"notInRange":return!x(e,u);case"empty":return e==="";case"notEmpty":return e!=="";case"include":return e.includes(r);case"notInclude":return!e.includes(r);case"=":return e===String(r);case"≠":return e!==String(r);case"greater":return F(e,r,(s,d)=>s>d);case"greaterEqual":return F(e,r,(s,d)=>s>=d);case"less":return F(e,r,(s,d)=>s<d);case"lessEqual":return F(e,r,(s,d)=>s<=d);case"isNumber":return v(e)!==null;case"isNotNumber":return v(e)===null;default:return!1}}function me(e){const t=M(e.attrs.value),n=m(e);return t===""||g.has(n)&&t===M(g.get(n))?!0:ye(e).has(t)}function ye(e){const t=new Set;for(const n of C(e.attrs.rules))for(const r of O(n))r.result.type!=="math"&&t.add(String(r.result.value??""));return t}function we(e){const t=new Set;for(const n of C(e.attrs.rules))for(const r of O(n)){for(const u of Z(r))for(const s of A(u.inputSerialMath)){const d=z(e.attrs.templateId,e.attrs.batchId,s);d&&t.add(d)}if(r.result.type==="math")for(const u of A(r.result.value)){const s=z(e.attrs.templateId,e.attrs.batchId,u);s&&t.add(s)}}return t}function I(e,t,n){var r;P>0||(e.warn(be,t,n),(r=o.onWarning)==null||r.call(o,{message:t,detail:n}))}return re}function C(o){return Array.isArray(o)?o:[]}function O(o){return o.contents??o.list??[]}function Z(o){return o.premises??o.premiss??[]}function A(o){return(o.match(Ie)??[]).map(l=>l.slice(1,-1).trim()).filter(Boolean)}function b(o){return z(o.attrs.templateId,o.attrs.batchId,o.attrs.widgetNumber)}function z(o,l,i){const a=W(o),c=W(l),f=Se(i);if(!(!a||!c||!f))return`${a}_${c}_${f}`}function W(o){return o==null?void 0:String(o).trim()||void 0}function Se(o){const l=W(o);if(!l)return;const i=l.match(/^【\s*([^】]+?)\s*】$/);return((i==null?void 0:i[1])??l).trim()||void 0}function Te(o){switch(o){case">":return"greater";case">=":return"greaterEqual";case"<":return"less";case"<=":return"lessEqual";default:return o}}function M(o){return o==null?"":String(o)}function V(o,l){if(Array.isArray(o))return o.map(i=>String(i)).includes(l);if(typeof o=="string"){const i=o.trim();if(i.startsWith("["))try{const a=JSON.parse(i);return Array.isArray(a)&&a.map(c=>String(c)).includes(l)}catch{return o.includes(l)}return o.includes(l)}return!1}function x(o,l){const i=v(o),a=v(l.number1),c=v(l.number2);if(i===null||a===null||c===null)return!1;const f=l.symbol1??"greaterEqual",g=l.symbol2??"lessEqual",h=f==="greater"?i>a:i>=a,w=g==="less"?i<c:i<=c;return h&&w}function F(o,l,i){const a=v(o),c=v(l);return a!==null&&c!==null&&i(a,c)}function v(o){if(o==null||typeof o=="string"&&o.trim()==="")return null;const l=Number(o);return Number.isFinite(l)?l:null}function Re(o){return o==="2"||o==="3"?o:"1"}function Ee(o,l,i){const a=10**i;switch(l){case"2":return Math.ceil(o*a)/a;case"3":return Math.floor(o*a)/a;default:return Math.round(o*a)/a}}function ee(o){const l=Ne(o),i=[],a=[],c=()=>{const f=a.pop();if(!f)throw new Error("Formula operator stack is empty.");if(f==="u-"){const w=i.pop();if(w===void 0)throw new Error("Formula unary operand is missing.");i.push(-w);return}const g=i.pop(),h=i.pop();if(h===void 0||g===void 0)throw new Error("Formula operand is missing.");switch(f){case"+":i.push(h+g);return;case"-":i.push(h-g);return;case"*":i.push(h*g);return;case"/":i.push(h/g);return;default:throw new Error(`Unsupported formula operator: ${f}.`)}};for(const f of l){if(typeof f=="number"){i.push(f);continue}if(f==="("){a.push(f);continue}if(f===")"){for(;a.length>0&&a[a.length-1]!=="(";)c();if(a.pop()!=="(")throw new Error("Formula parentheses are not balanced.");continue}for(;a.length>0&&a[a.length-1]!=="("&&te(a[a.length-1])>=te(f);)c();a.push(f)}for(;a.length>0;){if(a[a.length-1]==="(")throw new Error("Formula parentheses are not balanced.");c()}if(i.length!==1||!Number.isFinite(i[0]))throw new Error("Formula result is invalid.");return i[0]}function Ne(o){const l=[];let i=0,a;for(;i<o.length;){const c=o[i];if(/\s/.test(c)){i+=1;continue}if(c==="("||c===")"){l.push(c),a=c,i+=1;continue}if("+-*/".includes(c)){const h=c==="-"&&(a===void 0||typeof a=="string"&&a!==")");l.push(h?"u-":c),a=h?"u-":c,i+=1;continue}const f=o.slice(i).match(/^\d+(?:\.\d+)?|^\.\d+/);if(!f)throw new Error(`Formula contains unsupported token near: ${o.slice(i)}.`);const g=Number(f[0]);if(!Number.isFinite(g))throw new Error(`Formula number is invalid: ${f[0]}.`);l.push(g),a=g,i+=f[0].length}return l}function te(o){return o==="u-"?3:o==="*"||o==="/"?2:o==="+"||o==="-"?1:0}function Ce(){const o=globalThis.crypto;return o!=null&&o.randomUUID?`batch_${o.randomUUID()}`:`batch_${Date.now().toString(36)}_${Math.random().toString(36).slice(2)}`}function ne(o){return(typeof o=="object"||typeof o=="function")&&o!==null&&"then"in o&&typeof o.then=="function"}function Me(o){return o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}exports.createWidgetRulesPlugin=ve;
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ function m(o) {
4
4
  const Ie = /【\s*([^】]+?)\s*】/g, be = "CONTROL_RULE_FAILED", Y = 100;
5
5
  function Me(o = {}) {
6
6
  const l = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), h = [];
7
- let w = /* @__PURE__ */ new Set(), q = /* @__PURE__ */ new Map(), E = null, T = null, y = 0, P = 0;
7
+ let y = /* @__PURE__ */ new Set(), q = /* @__PURE__ */ new Map(), E = null, T = null, w = 0, P = 0;
8
8
  const S = /* @__PURE__ */ new Set(), re = {
9
9
  name: "hmg-widget-rules",
10
10
  getCurrentBatchId: () => h[h.length - 1],
@@ -22,14 +22,14 @@ function Me(o = {}) {
22
22
  runWithoutEvaluation(e) {
23
23
  if (h.length > 0)
24
24
  throw new Error("runWithoutEvaluation cannot run inside an active rules batch.");
25
- y += 1;
25
+ w += 1;
26
26
  try {
27
27
  const t = e();
28
28
  if (ne(t))
29
29
  throw new Error("runWithoutEvaluation requires a synchronous operation.");
30
30
  return t;
31
31
  } finally {
32
- y -= 1, y === 0 && de();
32
+ w -= 1, w === 0 && de();
33
33
  }
34
34
  },
35
35
  onEditorReady(e) {
@@ -38,12 +38,12 @@ function Me(o = {}) {
38
38
  onEditorDestroyed(e) {
39
39
  l.get(e.editorId) === e && l.delete(e.editorId);
40
40
  for (const t of Array.from(s.values()))
41
- t.editorId === e.editorId && A(t);
41
+ t.editorId === e.editorId && K(t);
42
42
  },
43
43
  onControlAdded(e, t) {
44
44
  $(e, t);
45
45
  const n = m(e);
46
- if (y > 0) {
46
+ if (w > 0) {
47
47
  S.add(n);
48
48
  return;
49
49
  }
@@ -52,11 +52,11 @@ function Me(o = {}) {
52
52
  onControlRemoved(e, t) {
53
53
  const n = t.getControl(e.controlId);
54
54
  if (n) {
55
- A(e), $(n, t), y > 0 && S.add(m(n));
55
+ K(e), $(n, t), w > 0 && S.add(m(n));
56
56
  return;
57
57
  }
58
- const r = y > 0 ? /* @__PURE__ */ new Set() : W(e);
59
- if (A(e), y > 0) {
58
+ const r = w > 0 ? /* @__PURE__ */ new Set() : W(e);
59
+ if (K(e), w > 0) {
60
60
  S.delete(m(e));
61
61
  return;
62
62
  }
@@ -64,15 +64,15 @@ function Me(o = {}) {
64
64
  },
65
65
  onControlValueChanged(e, t) {
66
66
  const n = m(e.current);
67
- if (s.set(n, e.current), oe(e.previous, e.current), y > 0) {
67
+ if (s.set(n, e.current), oe(e.previous, e.current), w > 0) {
68
68
  g.delete(n), S.add(n);
69
69
  return;
70
70
  }
71
71
  _(e.current, t);
72
72
  },
73
73
  onControlMetadataChanged(e, t) {
74
- const n = y > 0 ? /* @__PURE__ */ new Set() : W(e.previous);
75
- if (A(e.previous), $(e.current, t), y > 0) {
74
+ const n = w > 0 ? /* @__PURE__ */ new Set() : W(e.previous);
75
+ if (K(e.previous), $(e.current, t), w > 0) {
76
76
  S.add(m(e.current));
77
77
  return;
78
78
  }
@@ -93,14 +93,14 @@ function Me(o = {}) {
93
93
  I(t, "Rule control is missing batchId and will be skipped.", { control: e });
94
94
  return;
95
95
  }
96
- const i = we(e);
96
+ const i = ye(e);
97
97
  f.set(n, i);
98
98
  for (const d of i) {
99
99
  let p = c.get(d);
100
100
  p || (p = /* @__PURE__ */ new Set(), c.set(d, p)), p.add(n);
101
101
  }
102
102
  }
103
- function A(e) {
103
+ function K(e) {
104
104
  const t = m(e);
105
105
  s.delete(t), g.delete(t);
106
106
  const n = b(e);
@@ -156,10 +156,10 @@ function Me(o = {}) {
156
156
  r && R(new Set(r), t, m(e));
157
157
  }
158
158
  function R(e, t, n) {
159
- if (!(y > 0 || e.size === 0)) {
159
+ if (!(w > 0 || e.size === 0)) {
160
160
  if (h.length > 0 && !T) {
161
161
  for (const r of e)
162
- w.add(r);
162
+ y.add(r);
163
163
  B(q, n, e), E ?? (E = t);
164
164
  return;
165
165
  }
@@ -167,12 +167,12 @@ function Me(o = {}) {
167
167
  }
168
168
  }
169
169
  function se() {
170
- if (w.size === 0 || !E) {
171
- w.clear(), q.clear(), E = null;
170
+ if (y.size === 0 || !E) {
171
+ y.clear(), q.clear(), E = null;
172
172
  return;
173
173
  }
174
- const e = w, t = q, n = E;
175
- w = /* @__PURE__ */ new Set(), q = /* @__PURE__ */ new Map(), E = null, j(e, n, void 0, t);
174
+ const e = y, t = q, n = E;
175
+ y = /* @__PURE__ */ new Set(), q = /* @__PURE__ */ new Map(), E = null, j(e, n, void 0, t);
176
176
  }
177
177
  function j(e, t, n, r) {
178
178
  if (e.size === 0)
@@ -364,7 +364,7 @@ function Me(o = {}) {
364
364
  function pe(e, t, n) {
365
365
  if (t.entryType === "math")
366
366
  return String(ee(X(e, t.inputSerialMath, n)));
367
- const r = K(t.inputSerialMath)[0];
367
+ const r = A(t.inputSerialMath)[0];
368
368
  if (!r)
369
369
  throw new Error(`Rule premise has no widget reference: ${t.inputSerialMath}.`);
370
370
  return M(H(e, r, n));
@@ -374,7 +374,7 @@ function Me(o = {}) {
374
374
  return String(Ee(r, u, i));
375
375
  }
376
376
  function X(e, t, n) {
377
- const r = K(t);
377
+ const r = A(t);
378
378
  let u = t;
379
379
  for (const i of r) {
380
380
  const d = H(e, i, n), p = v(d);
@@ -436,26 +436,26 @@ function Me(o = {}) {
436
436
  }
437
437
  function me(e) {
438
438
  const t = M(e.attrs.value), n = m(e);
439
- return t === "" || g.has(n) && t === M(g.get(n)) ? !0 : ye(e).has(t);
439
+ return t === "" || g.has(n) && t === M(g.get(n)) ? !0 : we(e).has(t);
440
440
  }
441
- function ye(e) {
441
+ function we(e) {
442
442
  const t = /* @__PURE__ */ new Set();
443
443
  for (const n of C(e.attrs.rules))
444
444
  for (const r of D(n))
445
445
  r.result.type !== "math" && t.add(String(r.result.value ?? ""));
446
446
  return t;
447
447
  }
448
- function we(e) {
448
+ function ye(e) {
449
449
  const t = /* @__PURE__ */ new Set();
450
450
  for (const n of C(e.attrs.rules))
451
451
  for (const r of D(n)) {
452
452
  for (const u of Z(r))
453
- for (const i of K(u.inputSerialMath)) {
453
+ for (const i of A(u.inputSerialMath)) {
454
454
  const d = z(e.attrs.templateId, e.attrs.batchId, i);
455
455
  d && t.add(d);
456
456
  }
457
457
  if (r.result.type === "math")
458
- for (const u of K(r.result.value)) {
458
+ for (const u of A(r.result.value)) {
459
459
  const i = z(e.attrs.templateId, e.attrs.batchId, u);
460
460
  i && t.add(i);
461
461
  }
@@ -477,8 +477,8 @@ function D(o) {
477
477
  function Z(o) {
478
478
  return o.premises ?? o.premiss ?? [];
479
479
  }
480
- function K(o) {
481
- return Array.from(o.matchAll(Ie), (l) => l[1].trim()).filter(Boolean);
480
+ function A(o) {
481
+ return (o.match(Ie) ?? []).map((l) => l.slice(1, -1).trim()).filter(Boolean);
482
482
  }
483
483
  function b(o) {
484
484
  return z(o.attrs.templateId, o.attrs.batchId, o.attrs.widgetNumber);
@@ -535,8 +535,8 @@ function x(o, l) {
535
535
  const s = v(o), a = v(l.number1), c = v(l.number2);
536
536
  if (s === null || a === null || c === null)
537
537
  return !1;
538
- const f = l.symbol1 ?? "greaterEqual", g = l.symbol2 ?? "lessEqual", h = f === "greater" ? s > a : s >= a, w = g === "less" ? s < c : s <= c;
539
- return h && w;
538
+ const f = l.symbol1 ?? "greaterEqual", g = l.symbol2 ?? "lessEqual", h = f === "greater" ? s > a : s >= a, y = g === "less" ? s < c : s <= c;
539
+ return h && y;
540
540
  }
541
541
  function F(o, l, s) {
542
542
  const a = v(o), c = v(l);
@@ -568,10 +568,10 @@ function ee(o) {
568
568
  if (!f)
569
569
  throw new Error("Formula operator stack is empty.");
570
570
  if (f === "u-") {
571
- const w = s.pop();
572
- if (w === void 0)
571
+ const y = s.pop();
572
+ if (y === void 0)
573
573
  throw new Error("Formula unary operand is missing.");
574
- s.push(-w);
574
+ s.push(-y);
575
575
  return;
576
576
  }
577
577
  const g = s.pop(), h = s.pop();
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
- {
1
+ {
2
2
  "name": "@hmg-report-editor/rules",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "registry": "https://registry.npmjs.org/"
8
8
  },
9
- "files": [
10
- "dist"
11
- ],
12
- "main": "./dist/index.cjs",
13
- "module": "./dist/index.js",
14
- "types": "./dist/index.d.ts",
15
- "exports": {
16
- ".": {
17
- "types": "./dist/index.d.ts",
18
- "import": "./dist/index.js",
19
- "require": "./dist/index.cjs"
20
- }
21
- },
22
- "scripts": {
23
- "build": "vite build && tsc -p tsconfig.build.json && node ../../scripts/finalize-package-types.mjs .",
24
- "typecheck": "tsc -p tsconfig.json --noEmit"
25
- },
26
- "peerDependencies": {
27
- "@hmg-report-editor/core": "^1.0.0"
28
- }
29
- }
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "scripts": {
23
+ "build": "vite build && tsc -p tsconfig.build.json && node ../../scripts/finalize-package-types.mjs .",
24
+ "typecheck": "tsc -p tsconfig.json --noEmit"
25
+ },
26
+ "peerDependencies": {
27
+ "@hmg-report-editor/core": "^1.0.1"
28
+ }
29
+ }