@univerjs/sheets-source-binding 0.5.4-nightly.202501160949 → 0.5.4-nightly.202501170612

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/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var J=Object.defineProperty;var V=(i,o,e)=>o in i?J(i,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[o]=e;var u=(i,o,e)=>V(i,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@univerjs/core"),v=require("@univerjs/sheets"),j=require("rxjs");class T{constructor(o){u(this,"_matrix",{});u(this,"_nodeMap",new Map);u(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var _=(i=>(i.List="list",i.Object="object",i))(_||{}),B=(i=>(i.Path="path",i.Value="value",i))(B||{}),I=(i=>(i.Add="add",i.Remove="remove",i.Update="update",i))(I||{});function L(i){return i instanceof Date&&!isNaN(i.getTime())}function $(i){const o=new Date(i);if(!L(o))return i;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class y{constructor(o){u(this,"_data");u(this,"id");u(this,"_hasData",!1);u(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class O extends y{constructor(e,t){super(e);u(this,"type",_.List);u(this,"_isListObject");u(this,"_fieldIndexMap",new Map);u(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),d=t-s;if(d===0)return{v:this._data.fields[r]};let c;return this._isListObject&&(c=this._data.records[d-1][n]),c=this._data.records[d-1][r],e.isDate===!0?{v:$(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class U extends y{constructor(e){super(e);u(this,"type",_.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:$(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:_.Object}}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(i,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},R=(i,o)=>(e,t)=>o(e,t,i);let C=class extends l.Disposable{constructor(o,e,t){super();u(this,"modelMap",new Map);u(this,"_cellBindInfoUpdate$",new j.Subject);u(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=v.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:d}=s;return(o.id===v.ClearSelectionContentCommand.id||o.id===v.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{l.Range.foreach(c,(h,a)=>{this.getBindingNode(r,d,h,a)&&this.removeBindingNode(r,d,h,a)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const d=s.getBindingNodesBySourceId(o);if(d)for(const c of d)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new T,this.addModel(o,e,n)),t.nodeId||(t.nodeId=l.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const d=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:d?I.Update:I.Add,oldSourceId:d==null?void 0:d.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new T(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};C=W([R(0,l.IUniverInstanceService),R(1,l.Inject(v.SheetInterceptorService)),R(2,l.Inject(v.SheetsSelectionsService))],C);class w extends l.Disposable{constructor(){super();u(this,"sourceMap",new Map);u(this,"_sourceDataUpdate$",new j.Subject);u(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?l.generateRandomId():s;let d;switch(t){case _.List:d=new O(r,n);break;case _.Object:d=new U(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,d),d}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof y?t.getId():t,d=s==null?void 0:s.get(r);if(d)d.setSourceData(n),this._sourceDataUpdate$.next({...d.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case _.List:r=new O(s.id);break;case _.Object:r=new U(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(i,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},D=(i,o)=>(e,t)=>o(e,t,i);exports.SheetsSourceBindService=class extends l.Disposable{constructor(e,t,n){super();u(this,"_bindingModel",B.Value);u(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=B.Path}useValueMode(){this._bindingModel=B.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new l.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===_.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),h=e.recordCount;for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of c){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+h,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m})}}return}if(r===I.Update){const c=e.oldRecordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of h){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+c,endColumn:p},b={startRow:S,startColumn:p,endRow:S+e.recordCount,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m}),M.insert({unitId:a,sheetId:f,id:g,range:b})}}return}const d=this._sheetsSourceManager.getSource(s,t);if(d&&d.hasData()){const h=d.getSourceInfo().recordCount,a=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:f,subunitId:g,nodeId:S,row:p,column:M}of a){const m=this._ensureRTreeCollection(f),b={startRow:p,startColumn:M,endRow:p+h,endColumn:M};m.insert({unitId:f,sheetId:g,id:S,range:b})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:d,column:c,changeType:h}=e,a=this._ensureRTreeCollection(t),f=this._sheetsSourceManager.getSource(t,s);if(f&&f.hasData()){const g=f.getSourceInfo();if(g.sourceType===_.List){const S=g.recordCount,p={startRow:d,startColumn:c,endRow:d+S,endColumn:c};if(h===I.Add)a.insert({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Remove)a.remove({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Update){const M=e.oldSourceId,m=this._sheetsSourceManager.getSource(t,M);if(m&&m.hasData()){const x=m.getSourceInfo().recordCount,E={startRow:d,startColumn:c,endRow:d+x,endColumn:c};a.remove({unitId:t,sheetId:n,id:r,range:E})}a.insert({unitId:t,sheetId:n,id:r,range:p})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),d=r==null?void 0:r.getBindingNode(n,s);if(d){const c=d.type;if(c===_.List)return{v:`#{${d.path}}`,s:{cl:{rgb:"blue"}}};if(c===_.Object)return{v:`[${d.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:h}=c,a=this._sheetsSourceManager.getSource(e,h);if(a&&a.hasData())return(a==null?void 0:a.getData(c,n,s))||{v:""}}}const d=this._getRTeeCollection(e);if(r&&d){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},h=Array.from(d.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(h.length>0){const a=r.getBindingNodeById(h[0]);if(a){const{sourceId:f}=a,g=this._sheetsSourceManager.getSource(e,f);if(g&&g.hasData())return(g==null?void 0:g.getData(a,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(v.INTERCEPTOR_POINT.CELL_CONTENT,{effect:l.InterceptorEffectEnum.Value|l.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:d,subUnitId:c}=t;let h=null;return this._bindingModel===B.Path?h=this._getPathModeCellValue(d,c,s,r):h=this._getValueModeCellValue(d,c,s,r),n(h!==null?{...e,...h}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}};exports.SheetsSourceBindService=z([D(0,l.Inject(v.SheetInterceptorService)),D(1,l.Inject(C)),D(2,l.Inject(w))],exports.SheetsSourceBindService);var F=Object.defineProperty,K=Object.getOwnPropertyDescriptor,Q=(i,o,e,t)=>{for(var n=t>1?void 0:t?K(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&F(o,e,n),n},P=(i,o)=>(e,t)=>o(e,t,i),N;exports.UniverSheetsBindingSourcePlugin=(N=class extends l.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[C],[w],[exports.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){l.touchDependencies(this._injector,[[C],[w],[exports.SheetsSourceBindService]])}},u(N,"type",l.UniverInstanceType.UNIVER_SHEET),u(N,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),N);exports.UniverSheetsBindingSourcePlugin=Q([P(1,l.Inject(l.Injector)),P(2,l.IConfigService)],exports.UniverSheetsBindingSourcePlugin);exports.BindModeEnum=B;exports.BindingModel=T;exports.DataBindingNodeTypeEnum=_;exports.SheetsSourceManager=w;exports.SourceModelBase=y;
1
+ "use strict";var J=Object.defineProperty;var V=(i,o,e)=>o in i?J(i,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[o]=e;var u=(i,o,e)=>V(i,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@univerjs/core"),v=require("@univerjs/sheets"),j=require("rxjs");class T{constructor(o){u(this,"_matrix",{});u(this,"_nodeMap",new Map);u(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var _=(i=>(i.List="list",i.Object="object",i))(_||{}),B=(i=>(i.Path="path",i.Value="value",i))(B||{}),I=(i=>(i.Add="add",i.Remove="remove",i.Update="update",i))(I||{});function L(i){return i instanceof Date&&!isNaN(i.getTime())}function $(i){const o=new Date(i);if(!L(o))return i;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class y{constructor(o){u(this,"_data");u(this,"id");u(this,"_hasData",!1);u(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class O extends y{constructor(e,t){super(e);u(this,"type",_.List);u(this,"_isListObject");u(this,"_fieldIndexMap",new Map);u(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),d=t-s;if(d===0)return{v:this._data.fields[r]};let c;return this._isListObject?c=this._data.records[d-1][n]:c=this._data.records[d-1][r],e.isDate===!0?{v:$(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class U extends y{constructor(e){super(e);u(this,"type",_.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:$(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:_.Object}}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(i,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},R=(i,o)=>(e,t)=>o(e,t,i);let C=class extends l.Disposable{constructor(o,e,t){super();u(this,"modelMap",new Map);u(this,"_cellBindInfoUpdate$",new j.Subject);u(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=v.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:d}=s;return(o.id===v.ClearSelectionContentCommand.id||o.id===v.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{l.Range.foreach(c,(h,a)=>{this.getBindingNode(r,d,h,a)&&this.removeBindingNode(r,d,h,a)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const d=s.getBindingNodesBySourceId(o);if(d)for(const c of d)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new T,this.addModel(o,e,n)),t.nodeId||(t.nodeId=l.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const d=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:d?I.Update:I.Add,oldSourceId:d==null?void 0:d.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new T(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};C=W([R(0,l.IUniverInstanceService),R(1,l.Inject(v.SheetInterceptorService)),R(2,l.Inject(v.SheetsSelectionsService))],C);class w extends l.Disposable{constructor(){super();u(this,"sourceMap",new Map);u(this,"_sourceDataUpdate$",new j.Subject);u(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?l.generateRandomId():s;let d;switch(t){case _.List:d=new O(r,n);break;case _.Object:d=new U(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,d),d}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof y?t.getId():t,d=s==null?void 0:s.get(r);if(d)d.setSourceData(n),this._sourceDataUpdate$.next({...d.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case _.List:r=new O(s.id);break;case _.Object:r=new U(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(i,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},D=(i,o)=>(e,t)=>o(e,t,i);exports.SheetsSourceBindService=class extends l.Disposable{constructor(e,t,n){super();u(this,"_bindingModel",B.Value);u(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=B.Path}useValueMode(){this._bindingModel=B.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new l.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===_.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),h=e.recordCount;for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of c){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+h,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m})}}return}if(r===I.Update){const c=e.oldRecordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of h){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+c,endColumn:p},b={startRow:S,startColumn:p,endRow:S+e.recordCount,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m}),M.insert({unitId:a,sheetId:f,id:g,range:b})}}return}const d=this._sheetsSourceManager.getSource(s,t);if(d&&d.hasData()){const h=d.getSourceInfo().recordCount,a=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:f,subunitId:g,nodeId:S,row:p,column:M}of a){const m=this._ensureRTreeCollection(f),b={startRow:p,startColumn:M,endRow:p+h,endColumn:M};m.insert({unitId:f,sheetId:g,id:S,range:b})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:d,column:c,changeType:h}=e,a=this._ensureRTreeCollection(t),f=this._sheetsSourceManager.getSource(t,s);if(f&&f.hasData()){const g=f.getSourceInfo();if(g.sourceType===_.List){const S=g.recordCount,p={startRow:d,startColumn:c,endRow:d+S,endColumn:c};if(h===I.Add)a.insert({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Remove)a.remove({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Update){const M=e.oldSourceId,m=this._sheetsSourceManager.getSource(t,M);if(m&&m.hasData()){const x=m.getSourceInfo().recordCount,E={startRow:d,startColumn:c,endRow:d+x,endColumn:c};a.remove({unitId:t,sheetId:n,id:r,range:E})}a.insert({unitId:t,sheetId:n,id:r,range:p})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),d=r==null?void 0:r.getBindingNode(n,s);if(d){const c=d.type;if(c===_.List)return{v:`#{${d.path}}`,s:{cl:{rgb:"blue"}}};if(c===_.Object)return{v:`[${d.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:h}=c,a=this._sheetsSourceManager.getSource(e,h);if(a&&a.hasData())return(a==null?void 0:a.getData(c,n,s))||{v:""}}}const d=this._getRTeeCollection(e);if(r&&d){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},h=Array.from(d.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(h.length>0){const a=r.getBindingNodeById(h[0]);if(a){const{sourceId:f}=a,g=this._sheetsSourceManager.getSource(e,f);if(g&&g.hasData())return(g==null?void 0:g.getData(a,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(v.INTERCEPTOR_POINT.CELL_CONTENT,{effect:l.InterceptorEffectEnum.Value|l.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:d,subUnitId:c}=t;let h=null;return this._bindingModel===B.Path?h=this._getPathModeCellValue(d,c,s,r):h=this._getValueModeCellValue(d,c,s,r),n(h!==null?{...e,...h}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}};exports.SheetsSourceBindService=z([D(0,l.Inject(v.SheetInterceptorService)),D(1,l.Inject(C)),D(2,l.Inject(w))],exports.SheetsSourceBindService);var F=Object.defineProperty,K=Object.getOwnPropertyDescriptor,Q=(i,o,e,t)=>{for(var n=t>1?void 0:t?K(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&F(o,e,n),n},P=(i,o)=>(e,t)=>o(e,t,i),N;exports.UniverSheetsBindingSourcePlugin=(N=class extends l.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[C],[w],[exports.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){l.touchDependencies(this._injector,[[C],[w],[exports.SheetsSourceBindService]])}},u(N,"type",l.UniverInstanceType.UNIVER_SHEET),u(N,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),N);exports.UniverSheetsBindingSourcePlugin=Q([P(1,l.Inject(l.Injector)),P(2,l.IConfigService)],exports.UniverSheetsBindingSourcePlugin);exports.BindModeEnum=B;exports.BindingModel=T;exports.DataBindingNodeTypeEnum=_;exports.SheetsSourceManager=w;exports.SourceModelBase=y;
package/lib/es/index.js CHANGED
@@ -118,7 +118,7 @@ class U extends D {
118
118
  v: this._data.fields[r]
119
119
  };
120
120
  let a;
121
- return this._isListObject && (a = this._data.records[d - 1][o]), a = this._data.records[d - 1][r], t.isDate === !0 ? {
121
+ return this._isListObject ? a = this._data.records[d - 1][o] : a = this._data.records[d - 1][r], t.isDate === !0 ? {
122
122
  v: L(a),
123
123
  s: {
124
124
  n: {
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","rxjs"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.UniverSheetsSourceBinding={},u.UniverCore,u.UniverSheets,u.rxjs))})(this,function(u,a,M,O){"use strict";var Q=Object.defineProperty;var X=(u,a,M)=>a in u?Q(u,a,{enumerable:!0,configurable:!0,writable:!0,value:M}):u[a]=M;var l=(u,a,M)=>X(u,typeof a!="symbol"?a+"":a,M);var w;class D{constructor(o){l(this,"_matrix",{});l(this,"_nodeMap",new Map);l(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var S=(d=>(d.List="list",d.Object="object",d))(S||{}),C=(d=>(d.Path="path",d.Value="value",d))(C||{}),I=(d=>(d.Add="add",d.Remove="remove",d.Update="update",d))(I||{});function x(d){return d instanceof Date&&!isNaN(d.getTime())}function j(d){const o=new Date(d);if(!x(o))return d;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class N{constructor(o){l(this,"_data");l(this,"id");l(this,"_hasData",!1);l(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class P extends N{constructor(e,t){super(e);l(this,"type",S.List);l(this,"_isListObject");l(this,"_fieldIndexMap",new Map);l(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),i=t-s;if(i===0)return{v:this._data.fields[r]};let c;return this._isListObject&&(c=this._data.records[i-1][n]),c=this._data.records[i-1][r],e.isDate===!0?{v:j(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class $ extends N{constructor(e){super(e);l(this,"type",S.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:j(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:S.Object}}}var J=Object.defineProperty,V=Object.getOwnPropertyDescriptor,L=(d,o,e,t)=>{for(var n=t>1?void 0:t?V(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&J(o,e,n),n},T=(d,o)=>(e,t)=>o(e,t,d);let y=class extends a.Disposable{constructor(o,e,t){super();l(this,"modelMap",new Map);l(this,"_cellBindInfoUpdate$",new O.Subject);l(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=M.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:i}=s;return(o.id===M.ClearSelectionContentCommand.id||o.id===M.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{a.Range.foreach(c,(g,h)=>{this.getBindingNode(r,i,g,h)&&this.removeBindingNode(r,i,g,h)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const i=s.getBindingNodesBySourceId(o);if(i)for(const c of i)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new D,this.addModel(o,e,n)),t.nodeId||(t.nodeId=a.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const i=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:i?I.Update:I.Add,oldSourceId:i==null?void 0:i.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new D(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};y=L([T(0,a.IUniverInstanceService),T(1,a.Inject(M.SheetInterceptorService)),T(2,a.Inject(M.SheetsSelectionsService))],y);class b extends a.Disposable{constructor(){super();l(this,"sourceMap",new Map);l(this,"_sourceDataUpdate$",new O.Subject);l(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?a.generateRandomId():s;let i;switch(t){case S.List:i=new P(r,n);break;case S.Object:i=new $(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,i),i}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof N?t.getId():t,i=s==null?void 0:s.get(r);if(i)i.setSourceData(n),this._sourceDataUpdate$.next({...i.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case S.List:r=new P(s.id);break;case S.Object:r=new $(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(d,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},U=(d,o)=>(e,t)=>o(e,t,d);u.SheetsSourceBindService=class extends a.Disposable{constructor(e,t,n){super();l(this,"_bindingModel",C.Value);l(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=C.Path}useValueMode(){this._bindingModel=C.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new a.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===S.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),g=e.recordCount;for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of c){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+g,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B})}}return}if(r===I.Update){const c=e.oldRecordCount,g=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of g){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+c,endColumn:_},R={startRow:m,startColumn:_,endRow:m+e.recordCount,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B}),v.insert({unitId:h,sheetId:p,id:f,range:R})}}return}const i=this._sheetsSourceManager.getSource(s,t);if(i&&i.hasData()){const g=i.getSourceInfo().recordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:p,subunitId:f,nodeId:m,row:_,column:v}of h){const B=this._ensureRTreeCollection(p),R={startRow:_,startColumn:v,endRow:_+g,endColumn:v};B.insert({unitId:p,sheetId:f,id:m,range:R})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:i,column:c,changeType:g}=e,h=this._ensureRTreeCollection(t),p=this._sheetsSourceManager.getSource(t,s);if(p&&p.hasData()){const f=p.getSourceInfo();if(f.sourceType===S.List){const m=f.recordCount,_={startRow:i,startColumn:c,endRow:i+m,endColumn:c};if(g===I.Add)h.insert({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Remove)h.remove({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Update){const v=e.oldSourceId,B=this._sheetsSourceManager.getSource(t,v);if(B&&B.hasData()){const F=B.getSourceInfo().recordCount,K={startRow:i,startColumn:c,endRow:i+F,endColumn:c};h.remove({unitId:t,sheetId:n,id:r,range:K})}h.insert({unitId:t,sheetId:n,id:r,range:_})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),i=r==null?void 0:r.getBindingNode(n,s);if(i){const c=i.type;if(c===S.List)return{v:`#{${i.path}}`,s:{cl:{rgb:"blue"}}};if(c===S.Object)return{v:`[${i.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:g}=c,h=this._sheetsSourceManager.getSource(e,g);if(h&&h.hasData())return(h==null?void 0:h.getData(c,n,s))||{v:""}}}const i=this._getRTeeCollection(e);if(r&&i){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},g=Array.from(i.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(g.length>0){const h=r.getBindingNodeById(g[0]);if(h){const{sourceId:p}=h,f=this._sheetsSourceManager.getSource(e,p);if(f&&f.hasData())return(f==null?void 0:f.getData(h,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(M.INTERCEPTOR_POINT.CELL_CONTENT,{effect:a.InterceptorEffectEnum.Value|a.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:i,subUnitId:c}=t;let g=null;return this._bindingModel===C.Path?g=this._getPathModeCellValue(i,c,s,r):g=this._getValueModeCellValue(i,c,s,r),n(g!==null?{...e,...g}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}},u.SheetsSourceBindService=W([U(0,a.Inject(M.SheetInterceptorService)),U(1,a.Inject(y)),U(2,a.Inject(b))],u.SheetsSourceBindService);var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(d,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},E=(d,o)=>(e,t)=>o(e,t,d);u.UniverSheetsBindingSourcePlugin=(w=class extends a.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[y],[b],[u.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){a.touchDependencies(this._injector,[[y],[b],[u.SheetsSourceBindService]])}},l(w,"type",a.UniverInstanceType.UNIVER_SHEET),l(w,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),w),u.UniverSheetsBindingSourcePlugin=z([E(1,a.Inject(a.Injector)),E(2,a.IConfigService)],u.UniverSheetsBindingSourcePlugin),u.BindModeEnum=C,u.BindingModel=D,u.DataBindingNodeTypeEnum=S,u.SheetsSourceManager=b,u.SourceModelBase=N,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","rxjs"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.UniverSheetsSourceBinding={},u.UniverCore,u.UniverSheets,u.rxjs))})(this,function(u,a,M,O){"use strict";var Q=Object.defineProperty;var X=(u,a,M)=>a in u?Q(u,a,{enumerable:!0,configurable:!0,writable:!0,value:M}):u[a]=M;var l=(u,a,M)=>X(u,typeof a!="symbol"?a+"":a,M);var w;class D{constructor(o){l(this,"_matrix",{});l(this,"_nodeMap",new Map);l(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var S=(d=>(d.List="list",d.Object="object",d))(S||{}),C=(d=>(d.Path="path",d.Value="value",d))(C||{}),I=(d=>(d.Add="add",d.Remove="remove",d.Update="update",d))(I||{});function x(d){return d instanceof Date&&!isNaN(d.getTime())}function j(d){const o=new Date(d);if(!x(o))return d;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class N{constructor(o){l(this,"_data");l(this,"id");l(this,"_hasData",!1);l(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class P extends N{constructor(e,t){super(e);l(this,"type",S.List);l(this,"_isListObject");l(this,"_fieldIndexMap",new Map);l(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),i=t-s;if(i===0)return{v:this._data.fields[r]};let c;return this._isListObject?c=this._data.records[i-1][n]:c=this._data.records[i-1][r],e.isDate===!0?{v:j(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class $ extends N{constructor(e){super(e);l(this,"type",S.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:j(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:S.Object}}}var J=Object.defineProperty,V=Object.getOwnPropertyDescriptor,L=(d,o,e,t)=>{for(var n=t>1?void 0:t?V(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&J(o,e,n),n},T=(d,o)=>(e,t)=>o(e,t,d);let y=class extends a.Disposable{constructor(o,e,t){super();l(this,"modelMap",new Map);l(this,"_cellBindInfoUpdate$",new O.Subject);l(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=M.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:i}=s;return(o.id===M.ClearSelectionContentCommand.id||o.id===M.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{a.Range.foreach(c,(g,h)=>{this.getBindingNode(r,i,g,h)&&this.removeBindingNode(r,i,g,h)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const i=s.getBindingNodesBySourceId(o);if(i)for(const c of i)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new D,this.addModel(o,e,n)),t.nodeId||(t.nodeId=a.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const i=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:i?I.Update:I.Add,oldSourceId:i==null?void 0:i.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new D(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};y=L([T(0,a.IUniverInstanceService),T(1,a.Inject(M.SheetInterceptorService)),T(2,a.Inject(M.SheetsSelectionsService))],y);class b extends a.Disposable{constructor(){super();l(this,"sourceMap",new Map);l(this,"_sourceDataUpdate$",new O.Subject);l(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?a.generateRandomId():s;let i;switch(t){case S.List:i=new P(r,n);break;case S.Object:i=new $(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,i),i}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof N?t.getId():t,i=s==null?void 0:s.get(r);if(i)i.setSourceData(n),this._sourceDataUpdate$.next({...i.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case S.List:r=new P(s.id);break;case S.Object:r=new $(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(d,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},U=(d,o)=>(e,t)=>o(e,t,d);u.SheetsSourceBindService=class extends a.Disposable{constructor(e,t,n){super();l(this,"_bindingModel",C.Value);l(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=C.Path}useValueMode(){this._bindingModel=C.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new a.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===S.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),g=e.recordCount;for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of c){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+g,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B})}}return}if(r===I.Update){const c=e.oldRecordCount,g=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of g){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+c,endColumn:_},R={startRow:m,startColumn:_,endRow:m+e.recordCount,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B}),v.insert({unitId:h,sheetId:p,id:f,range:R})}}return}const i=this._sheetsSourceManager.getSource(s,t);if(i&&i.hasData()){const g=i.getSourceInfo().recordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:p,subunitId:f,nodeId:m,row:_,column:v}of h){const B=this._ensureRTreeCollection(p),R={startRow:_,startColumn:v,endRow:_+g,endColumn:v};B.insert({unitId:p,sheetId:f,id:m,range:R})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:i,column:c,changeType:g}=e,h=this._ensureRTreeCollection(t),p=this._sheetsSourceManager.getSource(t,s);if(p&&p.hasData()){const f=p.getSourceInfo();if(f.sourceType===S.List){const m=f.recordCount,_={startRow:i,startColumn:c,endRow:i+m,endColumn:c};if(g===I.Add)h.insert({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Remove)h.remove({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Update){const v=e.oldSourceId,B=this._sheetsSourceManager.getSource(t,v);if(B&&B.hasData()){const F=B.getSourceInfo().recordCount,K={startRow:i,startColumn:c,endRow:i+F,endColumn:c};h.remove({unitId:t,sheetId:n,id:r,range:K})}h.insert({unitId:t,sheetId:n,id:r,range:_})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),i=r==null?void 0:r.getBindingNode(n,s);if(i){const c=i.type;if(c===S.List)return{v:`#{${i.path}}`,s:{cl:{rgb:"blue"}}};if(c===S.Object)return{v:`[${i.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:g}=c,h=this._sheetsSourceManager.getSource(e,g);if(h&&h.hasData())return(h==null?void 0:h.getData(c,n,s))||{v:""}}}const i=this._getRTeeCollection(e);if(r&&i){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},g=Array.from(i.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(g.length>0){const h=r.getBindingNodeById(g[0]);if(h){const{sourceId:p}=h,f=this._sheetsSourceManager.getSource(e,p);if(f&&f.hasData())return(f==null?void 0:f.getData(h,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(M.INTERCEPTOR_POINT.CELL_CONTENT,{effect:a.InterceptorEffectEnum.Value|a.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:i,subUnitId:c}=t;let g=null;return this._bindingModel===C.Path?g=this._getPathModeCellValue(i,c,s,r):g=this._getValueModeCellValue(i,c,s,r),n(g!==null?{...e,...g}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}},u.SheetsSourceBindService=W([U(0,a.Inject(M.SheetInterceptorService)),U(1,a.Inject(y)),U(2,a.Inject(b))],u.SheetsSourceBindService);var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(d,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},E=(d,o)=>(e,t)=>o(e,t,d);u.UniverSheetsBindingSourcePlugin=(w=class extends a.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[y],[b],[u.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){a.touchDependencies(this._injector,[[y],[b],[u.SheetsSourceBindService]])}},l(w,"type",a.UniverInstanceType.UNIVER_SHEET),l(w,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),w),u.UniverSheetsBindingSourcePlugin=z([E(1,a.Inject(a.Injector)),E(2,a.IConfigService)],u.UniverSheetsBindingSourcePlugin),u.BindModeEnum=C,u.BindingModel=D,u.DataBindingNodeTypeEnum=S,u.SheetsSourceManager=b,u.SourceModelBase=N,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-source-binding",
3
- "version": "0.5.4-nightly.202501160949",
3
+ "version": "0.5.4-nightly.202501170612",
4
4
  "private": false,
5
5
  "description": "A library for connect and bind data from other sources to Univer Sheets",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -51,8 +51,8 @@
51
51
  "rxjs": ">=7.0.0"
52
52
  },
53
53
  "dependencies": {
54
- "@univerjs/core": "0.5.4-nightly.202501160949",
55
- "@univerjs/sheets": "0.5.4-nightly.202501160949"
54
+ "@univerjs/core": "0.5.4-nightly.202501170612",
55
+ "@univerjs/sheets": "0.5.4-nightly.202501170612"
56
56
  },
57
57
  "devDependencies": {
58
58
  "rxjs": "^7.8.1",