@univerjs/sheets-source-binding 0.5.4-nightly.202501191605 → 0.5.4-nightly.202501200849

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 V=Object.defineProperty;var J=(i,o,e)=>o in i?V(i,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[o]=e;var l=(i,o,e)=>J(i,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@univerjs/core"),v=require("@univerjs/sheets"),j=require("rxjs");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 _=(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 E(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 R{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 O extends R{constructor(e,t){super(e);l(this,"type",_.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),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:E(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{t:typeof c=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING,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 R{constructor(e){super(e);l(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:E(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:s,t:typeof s=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING}}getSourceInfo(){return{sourceId:this.id,sourceType:_.Object}}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,G=(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},w=(i,o)=>(e,t)=>o(e,t,i);let C=class extends a.Disposable{constructor(o,e,t){super();l(this,"modelMap",new Map);l(this,"_cellBindInfoUpdate$",new j.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=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})=>{a.Range.foreach(c,(h,u)=>{this.getBindingNode(r,d,h,u)&&this.removeBindingNode(r,d,h,u)})}),{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 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 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 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()}};C=G([w(0,a.IUniverInstanceService),w(1,a.Inject(v.SheetInterceptorService)),w(2,a.Inject(v.SheetsSelectionsService))],C);class b extends a.Disposable{constructor(){super();l(this,"sourceMap",new Map);l(this,"_sourceDataUpdate$",new j.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 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 R?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 W=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&&W(o,e,n),n},T=(i,o)=>(e,t)=>o(e,t,i);exports.SheetsSourceBindService=class extends a.Disposable{constructor(e,t,n){super();l(this,"_bindingModel",B.Value);l(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 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===_.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),h=e.recordCount;for(const{unitId:u,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:u,sheetId:f,id:g,range:m})}}return}if(r===I.Update){const c=e.oldRecordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:u,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},y={startRow:S,startColumn:p,endRow:S+e.recordCount,endColumn:p};M.remove({unitId:u,sheetId:f,id:g,range:m}),M.insert({unitId:u,sheetId:f,id:g,range:y})}}return}const d=this._sheetsSourceManager.getSource(s,t);if(d&&d.hasData()){const h=d.getSourceInfo().recordCount,u=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:f,subunitId:g,nodeId:S,row:p,column:M}of u){const m=this._ensureRTreeCollection(f),y={startRow:p,startColumn:M,endRow:p+h,endColumn:M};m.insert({unitId:f,sheetId:g,id:S,range:y})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:d,column:c,changeType:h}=e,u=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)u.insert({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Remove)u.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 $=m.getSourceInfo().recordCount,x={startRow:d,startColumn:c,endRow:d+$,endColumn:c};u.remove({unitId:t,sheetId:n,id:r,range:x})}u.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,u=this._sheetsSourceManager.getSource(e,h);if(u&&u.hasData())return(u==null?void 0:u.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 u=r.getBindingNodeById(h[0]);if(u){const{sourceId:f}=u,g=this._sheetsSourceManager.getSource(e,f);if(g&&g.hasData())return(g==null?void 0:g.getData(u,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(v.INTERCEPTOR_POINT.CELL_CONTENT,{effect:a.InterceptorEffectEnum.Value|a.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([T(0,a.Inject(v.SheetInterceptorService)),T(1,a.Inject(C)),T(2,a.Inject(b))],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 a.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[C],[b],[exports.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){a.touchDependencies(this._injector,[[C],[b],[exports.SheetsSourceBindService]])}},l(N,"type",a.UniverInstanceType.UNIVER_SHEET),l(N,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),N);exports.UniverSheetsBindingSourcePlugin=Q([P(1,a.Inject(a.Injector)),P(2,a.IConfigService)],exports.UniverSheetsBindingSourcePlugin);exports.BindModeEnum=B;exports.BindingModel=D;exports.DataBindingNodeTypeEnum=_;exports.SheetsSourceManager=b;exports.SourceModelBase=R;
1
+ "use strict";var J=Object.defineProperty;var H=(i,s,e)=>s in i?J(i,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[s]=e;var l=(i,s,e)=>H(i,typeof s!="symbol"?s+"":s,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@univerjs/core"),y=require("@univerjs/sheets"),$=require("rxjs");class U{constructor(s){l(this,"_matrix",{});l(this,"_nodeMap",new Map);l(this,"_sourceIdMap",new Map);s&&this._init(s)}_init(s){this.fromJSON(s)}getBindingNodesBySourceId(s){const e=this._sourceIdMap.get(s);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(s,e,t){this._matrix[s]||(this._matrix[s]={}),this._matrix[s][e]||(this._matrix[s][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(s,e){var t;return(t=this._matrix[s])==null?void 0:t[e]}removeBindingNode(s,e){var n;const t=(n=this._matrix[s])==null?void 0:n[e];if(t){this._matrix[s][e]=void 0,this._nodeMap.delete(t.nodeId);const o=this._sourceIdMap.get(t.sourceId);if(o){const r=o.indexOf(t.nodeId);r>=0&&o.splice(r,1),o.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(s){return this._nodeMap.get(s)}fromJSON(s){s.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var p=(i=>(i.List="list",i.Object="object",i))(p||{}),N=(i=>(i.Path="path",i.Value="value",i))(N||{}),B=(i=>(i.Add="add",i.Remove="remove",i.Update="update",i))(B||{});function A(i){return i instanceof Date&&!isNaN(i.getTime())}function x(i){const s=new Date(i);if(!A(s))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 o=(s.getTime()-e.getTime())/(1e3*3600*24);return s>t&&(o+=1),o+1}class T{constructor(s){l(this,"_data");l(this,"id");l(this,"_hasData",!1);l(this,"type");this.id=s}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(s){this._data=s,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(s){this.id=s.id,this.type=s.type}}class P extends T{constructor(e,t){super(e);l(this,"type",p.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:o,containHeader:r}=e,d=this._fieldIndexMap.get(n),a=t-o;if(r&&a===0)return{v:this._data.fields[d]};let h;const c=r?1:0;return this._isListObject?h=this._data.records[a-c][n]:h=this._data.records[a-c][d],e.isDate===!0?{v:x(h),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:u.CellValueType.NUMBER}:{t:typeof h=="number"?u.CellValueType.NUMBER:u.CellValueType.STRING,v:h}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,o)=>{this._fieldIndexMap.set(n,o)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class j extends T{constructor(e){super(e);l(this,"type",p.Object)}getData(e){const n=e.path.split(".");let o=this._data;for(const r of n)if(o=o[r],o===void 0)return null;return e.isDate===!0?{v:x(o),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:u.CellValueType.NUMBER}:{v:o,t:typeof o=="number"?u.CellValueType.NUMBER:u.CellValueType.STRING}}getSourceInfo(){return{sourceId:this.id,sourceType:p.Object}}}var q=Object.defineProperty,G=Object.getOwnPropertyDescriptor,W=(i,s,e,t)=>{for(var n=t>1?void 0:t?G(s,e):s,o=i.length-1,r;o>=0;o--)(r=i[o])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&q(s,e,n),n},D=(i,s)=>(e,t)=>s(e,t,i);let b=class extends u.Disposable{constructor(s,e,t){super();l(this,"modelMap",new Map);l(this,"_cellBindInfoUpdate$",new $.Subject);l(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=s,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:s=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),o=y.getSheetCommandTarget(this._univerInstanceService);if(!o||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:d}=o;return(s.id===y.ClearSelectionContentCommand.id||s.id===y.ClearSelectionAllCommand.id)&&n.forEach(({range:a})=>{u.Range.foreach(a,(h,c)=>{this.getBindingNode(r,d,h,c)&&this.removeBindingNode(r,d,h,c)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(s){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((o,r)=>{const d=o.getBindingNodesBySourceId(s);if(d)for(const a of d)e.push({unitId:n,subunitId:r,sourceId:s,nodeId:a.nodeId,row:a.row,column:a.column})})}),e}addModel(s,e,t){var n;this.modelMap.has(s)||this.modelMap.set(s,new Map),(n=this.modelMap.get(s))==null||n.set(e,t)}getModel(s,e){var t;return(t=this.modelMap.get(s))==null?void 0:t.get(e)}setBindingNode(s,e,t){let n=this.getModel(s,e);n||(n=new U,this.addModel(s,e,n)),t.nodeId||(t.nodeId=u.generateRandomId());const{row:o,column:r}=t;if(o===void 0||r===void 0)throw new Error("row and column is required");const d=n.getBindingNode(o,r),a=t.type===p.List?!!t.containHeader:!1;n.setBindingNode(o,r,{...t,row:o,column:r}),this._cellBindInfoUpdate$.next({unitId:s,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:o,column:r,containHeader:a,changeType:d?B.Update:B.Add,oldSourceId:d==null?void 0:d.sourceId,oldNodeContainHeader:(d==null?void 0:d.type)===p.List?!!d.containHeader:!1})}removeBindingNode(s,e,t,n){const o=this.getModel(s,e);if(o){const r=o.getBindingNode(t,n);r&&(o.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:s,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:B.Remove}))}}getBindingNode(s,e,t,n){const o=this.getModel(s,e);if(o)return o.getBindingNode(t,n)}getBindingNodeById(s,e,t){const n=this.getModel(s,e);if(n)return n.getBindingNodeById(t)}createModel(s,e,t){const n=new U(t);return this.addModel(s,e,n),n}toJSON(s){const e={},t=this.modelMap.get(s);return t&&t.forEach((n,o)=>{e[o]=n.toJSON()}),e}fromJSON(s,e){Object.entries(e).forEach(([t,n])=>{this.createModel(s,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};b=W([D(0,u.IUniverInstanceService),D(1,u.Inject(y.SheetInterceptorService)),D(2,u.Inject(y.SheetsSelectionsService))],b);class w extends u.Disposable{constructor(){super();l(this,"sourceMap",new Map);l(this,"_sourceDataUpdate$",new $.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,o){const r=o===void 0?u.generateRandomId():o;let d;switch(t){case p.List:d=new P(r,n);break;case p.Object:d=new j(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,d),d}updateSourceData(e,t,n){const o=this._getUnitMap(e),r=t instanceof T?t.getId():t,d=o==null?void 0:o.get(r);if(d)d.setSourceData(n),this._sourceDataUpdate$.next({...d.getSourceInfo(),unitId:e,changeType:B.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),o=n==null?void 0:n.get(t);o&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...o.getSourceInfo(),unitId:e,changeType:B.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const o of n.values())t.push(o.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const o of t){let r;switch(o.type){case p.List:r=new P(o.id);break;case p.Object:r=new j(o.id);break;default:throw new Error(`Invalid source type: ${o.type}`)}r.fromJSON(o),n.set(o.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var z=Object.defineProperty,F=Object.getOwnPropertyDescriptor,K=(i,s,e,t)=>{for(var n=t>1?void 0:t?F(s,e):s,o=i.length-1,r;o>=0;o--)(r=i[o])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&z(s,e,n),n},O=(i,s)=>(e,t)=>s(e,t,i);exports.SheetsSourceBindService=class extends u.Disposable{constructor(e,t,n){super();l(this,"_bindingModel",N.Value);l(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=N.Path}useValueMode(){this._bindingModel=N.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,o){this._sheetsBindingManager.removeBindingNode(e,t,n,o)}getBindingNode(e,t,n,o){return this._sheetsBindingManager.getBindingNode(e,t,n,o)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,o){return this._sheetsSourceManager.createSource(e,t,n,o)}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 u.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:o,changeType:r}=e;if(n===p.List){if(r===B.Remove){const a=this._sheetsBindingManager.getBindingModelBySourceId(t),h=e.recordCount;for(const{unitId:c,subunitId:f,nodeId:g,row:S,column:M}of a){const I=this._getRTeeCollection(o),_=this._sheetsBindingManager.getBindingNodeById(c,f,g);if(I&&(_==null?void 0:_.type)===p.List){const m=_.containHeader?0:1,v={startRow:S,startColumn:M,endRow:S+h-m,endColumn:M};I.remove({unitId:c,sheetId:f,id:g,range:v})}}return}if(r===B.Update){const a=e.oldRecordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:c,subunitId:f,nodeId:g,row:S,column:M}of h){const I=this._getRTeeCollection(o),_=this._sheetsBindingManager.getBindingNodeById(c,f,g);if(I&&(_==null?void 0:_.type)===p.List){const m=_.containHeader?0:1,v={startRow:S,startColumn:M,endRow:S+a-m,endColumn:M},C={startRow:S,startColumn:M,endRow:S+e.recordCount-m,endColumn:M};I.remove({unitId:c,sheetId:f,id:g,range:v}),I.insert({unitId:c,sheetId:f,id:g,range:C})}}return}const d=this._sheetsSourceManager.getSource(o,t);if(d&&d.hasData()){const h=d.getSourceInfo().recordCount,c=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:f,subunitId:g,nodeId:S,row:M,column:I}of c){const _=this._ensureRTreeCollection(f),m=this._sheetsBindingManager.getBindingNodeById(f,g,S);if(_&&(m==null?void 0:m.type)===p.List){const v=m.containHeader?0:1,C={startRow:M,startColumn:I,endRow:M+h-v,endColumn:I};_.insert({unitId:f,sheetId:g,id:S,range:C})}}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:o,nodeId:r,row:d,column:a,changeType:h,containHeader:c}=e,f=this._ensureRTreeCollection(t),g=this._sheetsSourceManager.getSource(t,o);if(g&&g.hasData()){const S=g.getSourceInfo();if(S.sourceType===p.List){const M=S.recordCount,I=c?0:1,_={startRow:d,startColumn:a,endRow:d+M-I,endColumn:a};if(h===B.Add)f.insert({unitId:t,sheetId:n,id:r,range:_});else if(h===B.Remove)f.remove({unitId:t,sheetId:n,id:r,range:_});else if(h===B.Update){const m=e.oldSourceId,v=e.oldNodeContainHeader?0:1,C=this._sheetsSourceManager.getSource(t,m);if(C&&C.hasData()){const V=C.getSourceInfo().recordCount,L={startRow:d,startColumn:a,endRow:d+V-v,endColumn:a};f.remove({unitId:t,sheetId:n,id:r,range:L})}f.insert({unitId:t,sheetId:n,id:r,range:_})}}}}))}_getPathModeCellValue(e,t,n,o){const r=this._sheetsBindingManager.getModel(e,t),d=r==null?void 0:r.getBindingNode(n,o);if(d){const a=d.type;if(a===p.List)return{v:`#{${d.path}}`,s:{cl:{rgb:"blue"}}};if(a===p.Object)return{v:`[${d.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,o){const r=this._sheetsBindingManager.getModel(e,t);if(r){const a=r.getBindingNode(n,o);if(a){const{sourceId:h}=a,c=this._sheetsSourceManager.getSource(e,h);if(c&&c.hasData())return(c==null?void 0:c.getData(a,n,o))||{v:""}}}const d=this._getRTeeCollection(e);if(r&&d){const a={startRow:n,startColumn:o,endRow:n,endColumn:o},h=Array.from(d.bulkSearch([{unitId:e,sheetId:t,range:a}]));if(h.length>0){const c=r.getBindingNodeById(h[0]);if(c){const{sourceId:f}=c,g=this._sheetsSourceManager.getSource(e,f);if(g&&g.hasData())return(g==null?void 0:g.getData(c,n,o))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(y.INTERCEPTOR_POINT.CELL_CONTENT,{effect:u.InterceptorEffectEnum.Value|u.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:o,col:r,unitId:d,subUnitId:a,workbook:h}=t;let c=null;if(this._bindingModel===N.Path?c=this._getPathModeCellValue(d,a,o,r):c=this._getValueModeCellValue(d,a,o,r),c!==null){const g={...(typeof(e==null?void 0:e.s)=="string"?h.getStyles().get(e==null?void 0:e.s):e==null?void 0:e.s)||{}};return n({...e,...c,s:g})}return n(e)}}))}dispose(){this._bindModelRTreeCollection.clear()}};exports.SheetsSourceBindService=K([O(0,u.Inject(y.SheetInterceptorService)),O(1,u.Inject(b)),O(2,u.Inject(w))],exports.SheetsSourceBindService);var Q=Object.defineProperty,X=Object.getOwnPropertyDescriptor,Y=(i,s,e,t)=>{for(var n=t>1?void 0:t?X(s,e):s,o=i.length-1,r;o>=0;o--)(r=i[o])&&(n=(t?r(s,e,n):r(n))||n);return t&&n&&Q(s,e,n),n},E=(i,s)=>(e,t)=>s(e,t,i),R;exports.UniverSheetsBindingSourcePlugin=(R=class extends u.Plugin{constructor(s={},e,t){super(),this._config=s,this._injector=e,this._configService=t}onStarting(){[[b],[w],[exports.SheetsSourceBindService]].forEach(s=>this._injector.add(s))}onReady(){u.touchDependencies(this._injector,[[b],[w],[exports.SheetsSourceBindService]])}},l(R,"type",u.UniverInstanceType.UNIVER_SHEET),l(R,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),R);exports.UniverSheetsBindingSourcePlugin=Y([E(1,u.Inject(u.Injector)),E(2,u.IConfigService)],exports.UniverSheetsBindingSourcePlugin);exports.BindModeEnum=N;exports.BindingModel=U;exports.DataBindingNodeTypeEnum=p;exports.SheetsSourceManager=w;exports.SourceModelBase=T;
package/lib/es/index.js CHANGED
@@ -1,14 +1,14 @@
1
- var W = Object.defineProperty;
2
- var H = (i, e, t) => e in i ? W(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
- var u = (i, e, t) => H(i, typeof e != "symbol" ? e + "" : e, t);
4
- import { CellValueType as S, Inject as m, Disposable as D, Range as q, generateRandomId as j, IUniverInstanceService as z, RTree as F, InterceptorEffectEnum as O, Plugin as K, UniverInstanceType as Q, touchDependencies as X, Injector as Y, IConfigService as Z } from "@univerjs/core";
5
- import { SheetInterceptorService as J, SheetsSelectionsService as k, getSheetCommandTarget as ee, ClearSelectionContentCommand as te, ClearSelectionAllCommand as ne, INTERCEPTOR_POINT as oe } from "@univerjs/sheets";
6
- import { Subject as V } from "rxjs";
7
- class U {
1
+ var q = Object.defineProperty;
2
+ var z = (i, e, t) => e in i ? q(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
+ var h = (i, e, t) => z(i, typeof e != "symbol" ? e + "" : e, t);
4
+ import { CellValueType as v, Inject as C, Disposable as O, Range as F, generateRandomId as J, IUniverInstanceService as K, RTree as Q, InterceptorEffectEnum as P, Plugin as X, UniverInstanceType as Y, touchDependencies as Z, Injector as k, IConfigService as ee } from "@univerjs/core";
5
+ import { SheetInterceptorService as V, SheetsSelectionsService as te, getSheetCommandTarget as ne, ClearSelectionContentCommand as oe, ClearSelectionAllCommand as se, INTERCEPTOR_POINT as re } from "@univerjs/sheets";
6
+ import { Subject as H } from "rxjs";
7
+ class $ {
8
8
  constructor(e) {
9
- u(this, "_matrix", {});
10
- u(this, "_nodeMap", /* @__PURE__ */ new Map());
11
- u(this, "_sourceIdMap", /* @__PURE__ */ new Map());
9
+ h(this, "_matrix", {});
10
+ h(this, "_nodeMap", /* @__PURE__ */ new Map());
11
+ h(this, "_sourceIdMap", /* @__PURE__ */ new Map());
12
12
  e && this._init(e);
13
13
  }
14
14
  _init(e) {
@@ -52,24 +52,24 @@ class U {
52
52
  return Array.from(this._nodeMap.values());
53
53
  }
54
54
  }
55
- var f = /* @__PURE__ */ ((i) => (i.List = "list", i.Object = "object", i))(f || {}), v = /* @__PURE__ */ ((i) => (i.Path = "path", i.Value = "value", i))(v || {}), M = /* @__PURE__ */ ((i) => (i.Add = "add", i.Remove = "remove", i.Update = "update", i))(M || {});
56
- function se(i) {
55
+ var g = /* @__PURE__ */ ((i) => (i.List = "list", i.Object = "object", i))(g || {}), y = /* @__PURE__ */ ((i) => (i.Path = "path", i.Value = "value", i))(y || {}), S = /* @__PURE__ */ ((i) => (i.Add = "add", i.Remove = "remove", i.Update = "update", i))(S || {});
56
+ function ie(i) {
57
57
  return i instanceof Date && !isNaN(i.getTime());
58
58
  }
59
- function L(i) {
59
+ function A(i) {
60
60
  const e = new Date(i);
61
- if (!se(e))
61
+ if (!ie(e))
62
62
  return i;
63
63
  const t = new Date(Date.UTC(1900, 0, 1, 0, 0, 0)), n = new Date(Date.UTC(1900, 1, 28, 0, 0, 0));
64
64
  let s = (e.getTime() - t.getTime()) / (1e3 * 3600 * 24);
65
65
  return e > n && (s += 1), s + 1;
66
66
  }
67
- class T {
67
+ class U {
68
68
  constructor(e) {
69
- u(this, "_data");
70
- u(this, "id");
71
- u(this, "_hasData", !1);
72
- u(this, "type");
69
+ h(this, "_data");
70
+ h(this, "id");
71
+ h(this, "_hasData", !1);
72
+ h(this, "type");
73
73
  this.id = e;
74
74
  }
75
75
  getId() {
@@ -94,13 +94,13 @@ class T {
94
94
  this.id = e.id, this.type = e.type;
95
95
  }
96
96
  }
97
- class P extends T {
97
+ class x extends U {
98
98
  constructor(t, n) {
99
99
  super(t);
100
- u(this, "type", f.List);
101
- u(this, "_isListObject");
102
- u(this, "_fieldIndexMap", /* @__PURE__ */ new Map());
103
- u(this, "_data", { fields: [], records: [] });
100
+ h(this, "type", g.List);
101
+ h(this, "_isListObject");
102
+ h(this, "_fieldIndexMap", /* @__PURE__ */ new Map());
103
+ h(this, "_data", { fields: [], records: [] });
104
104
  this._isListObject = n != null ? n : !0;
105
105
  }
106
106
  /**
@@ -112,22 +112,23 @@ class P extends T {
112
112
  this._isListObject = t;
113
113
  }
114
114
  getData(t, n) {
115
- const { path: o, row: s } = t, r = this._fieldIndexMap.get(o), d = n - s;
116
- if (d === 0)
115
+ const { path: o, row: s, containHeader: r } = t, d = this._fieldIndexMap.get(o), c = n - s;
116
+ if (r && c === 0)
117
117
  return {
118
- v: this._data.fields[r]
118
+ v: this._data.fields[d]
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 ? {
122
- v: L(a),
121
+ const u = r ? 1 : 0;
122
+ return this._isListObject ? a = this._data.records[c - u][o] : a = this._data.records[c - u][d], t.isDate === !0 ? {
123
+ v: A(a),
123
124
  s: {
124
125
  n: {
125
126
  pattern: "yyyy-m-d am/pm h:mm"
126
127
  }
127
128
  },
128
- t: S.NUMBER
129
+ t: v.NUMBER
129
130
  } : {
130
- t: typeof a == "number" ? S.NUMBER : S.STRING,
131
+ t: typeof a == "number" ? v.NUMBER : v.STRING,
131
132
  v: a
132
133
  };
133
134
  }
@@ -147,10 +148,10 @@ class P extends T {
147
148
  };
148
149
  }
149
150
  }
150
- class $ extends T {
151
+ class E extends U {
151
152
  constructor(t) {
152
153
  super(t);
153
- u(this, "type", f.Object);
154
+ h(this, "type", g.Object);
154
155
  }
155
156
  getData(t) {
156
157
  const o = t.path.split(".");
@@ -159,52 +160,52 @@ class $ extends T {
159
160
  if (s = s[r], s === void 0)
160
161
  return null;
161
162
  return t.isDate === !0 ? {
162
- v: L(s),
163
+ v: A(s),
163
164
  s: {
164
165
  n: {
165
166
  pattern: "yyyy-m-d am/pm h:mm"
166
167
  }
167
168
  },
168
- t: S.NUMBER
169
+ t: v.NUMBER
169
170
  } : {
170
171
  v: s,
171
- t: typeof s == "number" ? S.NUMBER : S.STRING
172
+ t: typeof s == "number" ? v.NUMBER : v.STRING
172
173
  };
173
174
  }
174
175
  getSourceInfo() {
175
176
  return {
176
177
  sourceId: this.id,
177
- sourceType: f.Object
178
+ sourceType: g.Object
178
179
  };
179
180
  }
180
181
  }
181
- var re = Object.defineProperty, ie = Object.getOwnPropertyDescriptor, de = (i, e, t, n) => {
182
- for (var o = n > 1 ? void 0 : n ? ie(e, t) : e, s = i.length - 1, r; s >= 0; s--)
182
+ var de = Object.defineProperty, ae = Object.getOwnPropertyDescriptor, ce = (i, e, t, n) => {
183
+ for (var o = n > 1 ? void 0 : n ? ae(e, t) : e, s = i.length - 1, r; s >= 0; s--)
183
184
  (r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
184
- return n && o && re(e, t, o), o;
185
+ return n && o && de(e, t, o), o;
185
186
  }, b = (i, e) => (t, n) => e(t, n, i);
186
- let B = class extends D {
187
+ let N = class extends O {
187
188
  constructor(e, t, n) {
188
189
  super();
189
- u(this, "modelMap", /* @__PURE__ */ new Map());
190
- u(this, "_cellBindInfoUpdate$", new V());
191
- u(this, "cellBindInfoUpdate$", this._cellBindInfoUpdate$.asObservable());
190
+ h(this, "modelMap", /* @__PURE__ */ new Map());
191
+ h(this, "_cellBindInfoUpdate$", new H());
192
+ h(this, "cellBindInfoUpdate$", this._cellBindInfoUpdate$.asObservable());
192
193
  this._univerInstanceService = e, this._sheetInterceptorService = t, this._sheetsSelectionsService = n, this._initRemoveCommand();
193
194
  }
194
195
  _initRemoveCommand() {
195
196
  this.disposeWithMe(
196
197
  this._sheetInterceptorService.interceptCommand({
197
198
  getMutations: (e) => {
198
- const t = [], n = [], o = this._sheetsSelectionsService.getCurrentSelections(), s = ee(this._univerInstanceService);
199
+ const t = [], n = [], o = this._sheetsSelectionsService.getCurrentSelections(), s = ne(this._univerInstanceService);
199
200
  if (!s || !o || o.length === 0)
200
201
  return {
201
202
  redos: [],
202
203
  undos: []
203
204
  };
204
205
  const { unitId: r, subUnitId: d } = s;
205
- return (e.id === te.id || e.id === ne.id) && o.forEach(({ range: a }) => {
206
- q.foreach(a, (c, l) => {
207
- this.getBindingNode(r, d, c, l) && this.removeBindingNode(r, d, c, l);
206
+ return (e.id === oe.id || e.id === se.id) && o.forEach(({ range: c }) => {
207
+ F.foreach(c, (a, u) => {
208
+ this.getBindingNode(r, d, a, u) && this.removeBindingNode(r, d, a, u);
208
209
  });
209
210
  }), { redos: t, undos: n };
210
211
  }
@@ -217,14 +218,14 @@ let B = class extends D {
217
218
  n.forEach((s, r) => {
218
219
  const d = s.getBindingNodesBySourceId(e);
219
220
  if (d)
220
- for (const a of d)
221
+ for (const c of d)
221
222
  t.push({
222
223
  unitId: o,
223
224
  subunitId: r,
224
225
  sourceId: e,
225
- nodeId: a.nodeId,
226
- row: a.row,
227
- column: a.column
226
+ nodeId: c.nodeId,
227
+ row: c.row,
228
+ column: c.column
228
229
  });
229
230
  });
230
231
  }), t;
@@ -239,11 +240,11 @@ let B = class extends D {
239
240
  }
240
241
  setBindingNode(e, t, n) {
241
242
  let o = this.getModel(e, t);
242
- o || (o = new U(), this.addModel(e, t, o)), n.nodeId || (n.nodeId = j());
243
+ o || (o = new $(), this.addModel(e, t, o)), n.nodeId || (n.nodeId = J());
243
244
  const { row: s, column: r } = n;
244
245
  if (s === void 0 || r === void 0)
245
246
  throw new Error("row and column is required");
246
- const d = o.getBindingNode(s, r);
247
+ const d = o.getBindingNode(s, r), c = n.type === g.List ? !!n.containHeader : !1;
247
248
  o.setBindingNode(s, r, { ...n, row: s, column: r }), this._cellBindInfoUpdate$.next({
248
249
  unitId: e,
249
250
  subunitId: t,
@@ -251,8 +252,10 @@ let B = class extends D {
251
252
  nodeId: n.nodeId,
252
253
  row: s,
253
254
  column: r,
254
- changeType: d ? M.Update : M.Add,
255
- oldSourceId: d == null ? void 0 : d.sourceId
255
+ containHeader: c,
256
+ changeType: d ? S.Update : S.Add,
257
+ oldSourceId: d == null ? void 0 : d.sourceId,
258
+ oldNodeContainHeader: (d == null ? void 0 : d.type) === g.List ? !!d.containHeader : !1
256
259
  });
257
260
  }
258
261
  removeBindingNode(e, t, n, o) {
@@ -266,7 +269,7 @@ let B = class extends D {
266
269
  nodeId: r.nodeId,
267
270
  row: n,
268
271
  column: o,
269
- changeType: M.Remove
272
+ changeType: S.Remove
270
273
  }));
271
274
  }
272
275
  }
@@ -275,8 +278,13 @@ let B = class extends D {
275
278
  if (s)
276
279
  return s.getBindingNode(n, o);
277
280
  }
281
+ getBindingNodeById(e, t, n) {
282
+ const o = this.getModel(e, t);
283
+ if (o)
284
+ return o.getBindingNodeById(n);
285
+ }
278
286
  createModel(e, t, n) {
279
- const o = new U(n);
287
+ const o = new $(n);
280
288
  return this.addModel(e, t, o), o;
281
289
  }
282
290
  toJSON(e) {
@@ -294,17 +302,17 @@ let B = class extends D {
294
302
  this.modelMap.clear(), this._cellBindInfoUpdate$.complete();
295
303
  }
296
304
  };
297
- B = de([
298
- b(0, z),
299
- b(1, m(J)),
300
- b(2, m(k))
301
- ], B);
302
- class y extends D {
305
+ N = ce([
306
+ b(0, K),
307
+ b(1, C(V)),
308
+ b(2, C(te))
309
+ ], N);
310
+ class T extends O {
303
311
  constructor() {
304
312
  super();
305
- u(this, "sourceMap", /* @__PURE__ */ new Map());
306
- u(this, "_sourceDataUpdate$", new V());
307
- u(this, "sourceDataUpdate$", this._sourceDataUpdate$.asObservable());
313
+ h(this, "sourceMap", /* @__PURE__ */ new Map());
314
+ h(this, "_sourceDataUpdate$", new H());
315
+ h(this, "sourceDataUpdate$", this._sourceDataUpdate$.asObservable());
308
316
  }
309
317
  _ensureUnitMap(t) {
310
318
  let n = this.sourceMap.get(t);
@@ -318,14 +326,14 @@ class y extends D {
318
326
  return o == null ? void 0 : o.get(n);
319
327
  }
320
328
  createSource(t, n, o, s) {
321
- const r = s === void 0 ? j() : s;
329
+ const r = s === void 0 ? J() : s;
322
330
  let d;
323
331
  switch (n) {
324
- case f.List:
325
- d = new P(r, o);
332
+ case g.List:
333
+ d = new x(r, o);
326
334
  break;
327
- case f.Object:
328
- d = new $(r);
335
+ case g.Object:
336
+ d = new E(r);
329
337
  break;
330
338
  default:
331
339
  throw new Error(`Invalid source type: ${n}`);
@@ -333,15 +341,15 @@ class y extends D {
333
341
  return this._ensureUnitMap(t).set(r, d), d;
334
342
  }
335
343
  updateSourceData(t, n, o) {
336
- const s = this._getUnitMap(t), r = n instanceof T ? n.getId() : n, d = s == null ? void 0 : s.get(r);
344
+ const s = this._getUnitMap(t), r = n instanceof U ? n.getId() : n, d = s == null ? void 0 : s.get(r);
337
345
  if (d)
338
- d.setSourceData(o), this._sourceDataUpdate$.next({ ...d.getSourceInfo(), unitId: t, changeType: M.Add });
346
+ d.setSourceData(o), this._sourceDataUpdate$.next({ ...d.getSourceInfo(), unitId: t, changeType: S.Add });
339
347
  else
340
348
  throw new Error(`Source not found: ${r}`);
341
349
  }
342
350
  removeSource(t, n) {
343
351
  const o = this._getUnitMap(t), s = o == null ? void 0 : o.get(n);
344
- s && (o == null || o.delete(n), this._sourceDataUpdate$.next({ ...s.getSourceInfo(), unitId: t, changeType: M.Remove }));
352
+ s && (o == null || o.delete(n), this._sourceDataUpdate$.next({ ...s.getSourceInfo(), unitId: t, changeType: S.Remove }));
345
353
  }
346
354
  toJSON(t) {
347
355
  const n = [], o = this._getUnitMap(t);
@@ -355,11 +363,11 @@ class y extends D {
355
363
  for (const s of n) {
356
364
  let r;
357
365
  switch (s.type) {
358
- case f.List:
359
- r = new P(s.id);
366
+ case g.List:
367
+ r = new x(s.id);
360
368
  break;
361
- case f.Object:
362
- r = new $(s.id);
369
+ case g.Object:
370
+ r = new E(s.id);
363
371
  break;
364
372
  default:
365
373
  throw new Error(`Invalid source type: ${s.type}`);
@@ -371,29 +379,29 @@ class y extends D {
371
379
  this._sourceDataUpdate$.complete(), this.sourceMap.clear();
372
380
  }
373
381
  }
374
- var ae = Object.defineProperty, ce = Object.getOwnPropertyDescriptor, ue = (i, e, t, n) => {
375
- for (var o = n > 1 ? void 0 : n ? ce(e, t) : e, s = i.length - 1, r; s >= 0; s--)
382
+ var ue = Object.defineProperty, he = Object.getOwnPropertyDescriptor, le = (i, e, t, n) => {
383
+ for (var o = n > 1 ? void 0 : n ? he(e, t) : e, s = i.length - 1, r; s >= 0; s--)
376
384
  (r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
377
- return n && o && ae(e, t, o), o;
378
- }, w = (i, e) => (t, n) => e(t, n, i);
379
- let R = class extends D {
385
+ return n && o && ue(e, t, o), o;
386
+ }, D = (i, e) => (t, n) => e(t, n, i);
387
+ let R = class extends O {
380
388
  constructor(e, t, n) {
381
389
  super();
382
- u(this, "_bindingModel", v.Value);
383
- u(this, "_bindModelRTreeCollection", /* @__PURE__ */ new Map());
390
+ h(this, "_bindingModel", y.Value);
391
+ h(this, "_bindModelRTreeCollection", /* @__PURE__ */ new Map());
384
392
  this._sheetInterceptorService = e, this._sheetsBindingManager = t, this._sheetsSourceManager = n, this._registerInterceptor(), this._registerSourceChange();
385
393
  }
386
394
  /**
387
395
  * Set the binding model to path mode, in this mode, the binding path will show in the cell.
388
396
  */
389
397
  usePathMode() {
390
- this._bindingModel = v.Path;
398
+ this._bindingModel = y.Path;
391
399
  }
392
400
  /**
393
401
  * Set the binding model to value mode, in this mode, the value of source will show in the cell.
394
402
  */
395
403
  useValueMode() {
396
- this._bindingModel = v.Value;
404
+ this._bindingModel = y.Value;
397
405
  }
398
406
  /**
399
407
  * Get the current binding model.
@@ -430,7 +438,7 @@ let R = class extends D {
430
438
  this._sheetsSourceManager.fromJSON(e, t.source), this._sheetsBindingManager.fromJSON(e, t.cellBinding);
431
439
  }
432
440
  _ensureRTreeCollection(e) {
433
- return this._bindModelRTreeCollection.has(e) || this._bindModelRTreeCollection.set(e, new F()), this._bindModelRTreeCollection.get(e);
441
+ return this._bindModelRTreeCollection.has(e) || this._bindModelRTreeCollection.set(e, new Q()), this._bindModelRTreeCollection.get(e);
434
442
  }
435
443
  _getRTeeCollection(e) {
436
444
  return this._bindModelRTreeCollection.get(e);
@@ -439,55 +447,58 @@ let R = class extends D {
439
447
  _registerSourceChange() {
440
448
  this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe((e) => {
441
449
  const { sourceId: t, sourceType: n, unitId: o, changeType: s } = e;
442
- if (n === f.List) {
443
- if (s === M.Remove) {
444
- const d = this._sheetsBindingManager.getBindingModelBySourceId(t), a = e.recordCount;
445
- for (const { unitId: c, subunitId: l, nodeId: h, row: p, column: g } of d) {
446
- const _ = this._getRTeeCollection(o);
447
- if (_) {
448
- const I = { startRow: p, startColumn: g, endRow: p + a, endColumn: g };
449
- _.remove({ unitId: c, sheetId: l, id: h, range: I });
450
+ if (n === g.List) {
451
+ if (s === S.Remove) {
452
+ const d = this._sheetsBindingManager.getBindingModelBySourceId(t), c = e.recordCount;
453
+ for (const { unitId: a, subunitId: u, nodeId: l, row: p, column: _ } of d) {
454
+ const M = this._getRTeeCollection(o), f = this._sheetsBindingManager.getBindingNodeById(a, u, l);
455
+ if (M && (f == null ? void 0 : f.type) === g.List) {
456
+ const I = f.containHeader ? 0 : 1, m = { startRow: p, startColumn: _, endRow: p + c - I, endColumn: _ };
457
+ M.remove({ unitId: a, sheetId: u, id: l, range: m });
450
458
  }
451
459
  }
452
460
  return;
453
461
  }
454
- if (s === M.Update) {
455
- const d = e.oldRecordCount, a = this._sheetsBindingManager.getBindingModelBySourceId(t);
456
- for (const { unitId: c, subunitId: l, nodeId: h, row: p, column: g } of a) {
457
- const _ = this._getRTeeCollection(o);
458
- if (_) {
459
- const I = { startRow: p, startColumn: g, endRow: p + d, endColumn: g }, C = { startRow: p, startColumn: g, endRow: p + e.recordCount, endColumn: g };
460
- _.remove({ unitId: c, sheetId: l, id: h, range: I }), _.insert({ unitId: c, sheetId: l, id: h, range: C });
462
+ if (s === S.Update) {
463
+ const d = e.oldRecordCount, c = this._sheetsBindingManager.getBindingModelBySourceId(t);
464
+ for (const { unitId: a, subunitId: u, nodeId: l, row: p, column: _ } of c) {
465
+ const M = this._getRTeeCollection(o), f = this._sheetsBindingManager.getBindingNodeById(a, u, l);
466
+ if (M && (f == null ? void 0 : f.type) === g.List) {
467
+ const I = f.containHeader ? 0 : 1, m = { startRow: p, startColumn: _, endRow: p + d - I, endColumn: _ }, B = { startRow: p, startColumn: _, endRow: p + e.recordCount - I, endColumn: _ };
468
+ M.remove({ unitId: a, sheetId: u, id: l, range: m }), M.insert({ unitId: a, sheetId: u, id: l, range: B });
461
469
  }
462
470
  }
463
471
  return;
464
472
  }
465
473
  const r = this._sheetsSourceManager.getSource(o, t);
466
474
  if (r && r.hasData()) {
467
- const a = r.getSourceInfo().recordCount, c = this._sheetsBindingManager.getBindingModelBySourceId(t);
468
- for (const { unitId: l, subunitId: h, nodeId: p, row: g, column: _ } of c) {
469
- const I = this._ensureRTreeCollection(l), C = { startRow: g, startColumn: _, endRow: g + a, endColumn: _ };
470
- I.insert({ unitId: l, sheetId: h, id: p, range: C });
475
+ const c = r.getSourceInfo().recordCount, a = this._sheetsBindingManager.getBindingModelBySourceId(t);
476
+ for (const { unitId: u, subunitId: l, nodeId: p, row: _, column: M } of a) {
477
+ const f = this._ensureRTreeCollection(u), I = this._sheetsBindingManager.getBindingNodeById(u, l, p);
478
+ if (f && (I == null ? void 0 : I.type) === g.List) {
479
+ const m = I.containHeader ? 0 : 1, B = { startRow: _, startColumn: M, endRow: _ + c - m, endColumn: M };
480
+ f.insert({ unitId: u, sheetId: l, id: p, range: B });
481
+ }
471
482
  }
472
483
  }
473
484
  }
474
485
  })), this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe((e) => {
475
- const { unitId: t, subunitId: n, sourceId: o, nodeId: s, row: r, column: d, changeType: a } = e, c = this._ensureRTreeCollection(t), l = this._sheetsSourceManager.getSource(t, o);
486
+ const { unitId: t, subunitId: n, sourceId: o, nodeId: s, row: r, column: d, changeType: c, containHeader: a } = e, u = this._ensureRTreeCollection(t), l = this._sheetsSourceManager.getSource(t, o);
476
487
  if (l && l.hasData()) {
477
- const h = l.getSourceInfo();
478
- if (h.sourceType === f.List) {
479
- const p = h.recordCount, g = { startRow: r, startColumn: d, endRow: r + p, endColumn: d };
480
- if (a === M.Add)
481
- c.insert({ unitId: t, sheetId: n, id: s, range: g });
482
- else if (a === M.Remove)
483
- c.remove({ unitId: t, sheetId: n, id: s, range: g });
484
- else if (a === M.Update) {
485
- const _ = e.oldSourceId, I = this._sheetsSourceManager.getSource(t, _);
486
- if (I && I.hasData()) {
487
- const A = I.getSourceInfo().recordCount, G = { startRow: r, startColumn: d, endRow: r + A, endColumn: d };
488
- c.remove({ unitId: t, sheetId: n, id: s, range: G });
488
+ const p = l.getSourceInfo();
489
+ if (p.sourceType === g.List) {
490
+ const _ = p.recordCount, M = a ? 0 : 1, f = { startRow: r, startColumn: d, endRow: r + _ - M, endColumn: d };
491
+ if (c === S.Add)
492
+ u.insert({ unitId: t, sheetId: n, id: s, range: f });
493
+ else if (c === S.Remove)
494
+ u.remove({ unitId: t, sheetId: n, id: s, range: f });
495
+ else if (c === S.Update) {
496
+ const I = e.oldSourceId, m = e.oldNodeContainHeader ? 0 : 1, B = this._sheetsSourceManager.getSource(t, I);
497
+ if (B && B.hasData()) {
498
+ const G = B.getSourceInfo().recordCount, W = { startRow: r, startColumn: d, endRow: r + G - m, endColumn: d };
499
+ u.remove({ unitId: t, sheetId: n, id: s, range: W });
489
500
  }
490
- c.insert({ unitId: t, sheetId: n, id: s, range: g });
501
+ u.insert({ unitId: t, sheetId: n, id: s, range: f });
491
502
  }
492
503
  }
493
504
  }
@@ -497,12 +508,12 @@ let R = class extends D {
497
508
  const s = this._sheetsBindingManager.getModel(e, t), r = s == null ? void 0 : s.getBindingNode(n, o);
498
509
  if (r) {
499
510
  const d = r.type;
500
- if (d === f.List)
511
+ if (d === g.List)
501
512
  return {
502
513
  v: `#{${r.path}}`,
503
514
  s: { cl: { rgb: "blue" } }
504
515
  };
505
- if (d === f.Object)
516
+ if (d === g.Object)
506
517
  return {
507
518
  v: `[${r.path}]`,
508
519
  s: { cl: { rgb: "blue" } }
@@ -514,32 +525,36 @@ let R = class extends D {
514
525
  if (s) {
515
526
  const d = s.getBindingNode(n, o);
516
527
  if (d) {
517
- const { sourceId: a } = d, c = this._sheetsSourceManager.getSource(e, a);
518
- if (c && c.hasData())
519
- return (c == null ? void 0 : c.getData(d, n, o)) || { v: "" };
528
+ const { sourceId: c } = d, a = this._sheetsSourceManager.getSource(e, c);
529
+ if (a && a.hasData())
530
+ return (a == null ? void 0 : a.getData(d, n, o)) || { v: "" };
520
531
  }
521
532
  }
522
533
  const r = this._getRTeeCollection(e);
523
534
  if (s && r) {
524
- const d = { startRow: n, startColumn: o, endRow: n, endColumn: o }, a = Array.from(r.bulkSearch([{ unitId: e, sheetId: t, range: d }]));
525
- if (a.length > 0) {
526
- const c = s.getBindingNodeById(a[0]);
527
- if (c) {
528
- const { sourceId: l } = c, h = this._sheetsSourceManager.getSource(e, l);
529
- if (h && h.hasData())
530
- return (h == null ? void 0 : h.getData(c, n, o)) || { v: "" };
535
+ const d = { startRow: n, startColumn: o, endRow: n, endColumn: o }, c = Array.from(r.bulkSearch([{ unitId: e, sheetId: t, range: d }]));
536
+ if (c.length > 0) {
537
+ const a = s.getBindingNodeById(c[0]);
538
+ if (a) {
539
+ const { sourceId: u } = a, l = this._sheetsSourceManager.getSource(e, u);
540
+ if (l && l.hasData())
541
+ return (l == null ? void 0 : l.getData(a, n, o)) || { v: "" };
531
542
  }
532
543
  }
533
544
  }
534
545
  }
535
546
  _registerInterceptor() {
536
- this.disposeWithMe(this._sheetInterceptorService.intercept(oe.CELL_CONTENT, {
537
- effect: O.Value | O.Style,
547
+ this.disposeWithMe(this._sheetInterceptorService.intercept(re.CELL_CONTENT, {
548
+ effect: P.Value | P.Style,
538
549
  priority: 102,
539
550
  handler: (e, t, n) => {
540
- const { row: o, col: s, unitId: r, subUnitId: d } = t;
551
+ const { row: o, col: s, unitId: r, subUnitId: d, workbook: c } = t;
541
552
  let a = null;
542
- return this._bindingModel === v.Path ? a = this._getPathModeCellValue(r, d, o, s) : a = this._getValueModeCellValue(r, d, o, s), n(a !== null ? { ...e, ...a } : e);
553
+ if (this._bindingModel === y.Path ? a = this._getPathModeCellValue(r, d, o, s) : a = this._getValueModeCellValue(r, d, o, s), a !== null) {
554
+ const l = { ...(typeof (e == null ? void 0 : e.s) == "string" ? c.getStyles().get(e == null ? void 0 : e.s) : e == null ? void 0 : e.s) || {} };
555
+ return n({ ...e, ...a, s: l });
556
+ }
557
+ return n(e);
543
558
  }
544
559
  }));
545
560
  }
@@ -547,45 +562,45 @@ let R = class extends D {
547
562
  this._bindModelRTreeCollection.clear();
548
563
  }
549
564
  };
550
- R = ue([
551
- w(0, m(J)),
552
- w(1, m(B)),
553
- w(2, m(y))
565
+ R = le([
566
+ D(0, C(V)),
567
+ D(1, C(N)),
568
+ D(2, C(T))
554
569
  ], R);
555
- var le = Object.defineProperty, he = Object.getOwnPropertyDescriptor, ge = (i, e, t, n) => {
556
- for (var o = n > 1 ? void 0 : n ? he(e, t) : e, s = i.length - 1, r; s >= 0; s--)
570
+ var ge = Object.defineProperty, fe = Object.getOwnPropertyDescriptor, pe = (i, e, t, n) => {
571
+ for (var o = n > 1 ? void 0 : n ? fe(e, t) : e, s = i.length - 1, r; s >= 0; s--)
557
572
  (r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
558
- return n && o && le(e, t, o), o;
559
- }, x = (i, e) => (t, n) => e(t, n, i), N;
560
- let E = (N = class extends K {
573
+ return n && o && ge(e, t, o), o;
574
+ }, j = (i, e) => (t, n) => e(t, n, i), w;
575
+ let L = (w = class extends X {
561
576
  constructor(i = {}, e, t) {
562
577
  super(), this._config = i, this._injector = e, this._configService = t;
563
578
  }
564
579
  onStarting() {
565
580
  [
566
- [B],
567
- [y],
581
+ [N],
582
+ [T],
568
583
  [R]
569
584
  ].forEach((i) => this._injector.add(i));
570
585
  }
571
586
  onReady() {
572
- X(this._injector, [
573
- [B],
574
- [y],
587
+ Z(this._injector, [
588
+ [N],
589
+ [T],
575
590
  [R]
576
591
  ]);
577
592
  }
578
- }, u(N, "type", Q.UNIVER_SHEET), u(N, "pluginName", "SHEET_BINDING_SOURCE_PLUGIN"), N);
579
- E = ge([
580
- x(1, m(Y)),
581
- x(2, Z)
582
- ], E);
593
+ }, h(w, "type", Y.UNIVER_SHEET), h(w, "pluginName", "SHEET_BINDING_SOURCE_PLUGIN"), w);
594
+ L = pe([
595
+ j(1, C(k)),
596
+ j(2, ee)
597
+ ], L);
583
598
  export {
584
- v as BindModeEnum,
585
- U as BindingModel,
586
- f as DataBindingNodeTypeEnum,
599
+ y as BindModeEnum,
600
+ $ as BindingModel,
601
+ g as DataBindingNodeTypeEnum,
587
602
  R as SheetsSourceBindService,
588
- y as SheetsSourceManager,
589
- T as SourceModelBase,
590
- E as UniverSheetsBindingSourcePlugin
603
+ T as SheetsSourceManager,
604
+ U as SourceModelBase,
605
+ L as UniverSheetsBindingSourcePlugin
591
606
  };
@@ -19,6 +19,8 @@ export declare class SheetsBindingManager extends Disposable {
19
19
  cellBindInfoUpdate$: import('rxjs').Observable<IBindingNodeInfo & {
20
20
  changeType: BindingSourceChangeTypeEnum;
21
21
  oldSourceId?: string;
22
+ containHeader?: boolean;
23
+ oldNodeContainHeader?: boolean;
22
24
  }>;
23
25
  constructor(_univerInstanceService: IUniverInstanceService, _sheetInterceptorService: SheetInterceptorService, _sheetsSelectionsService: SheetsSelectionsService);
24
26
  private _initRemoveCommand;
@@ -28,6 +30,7 @@ export declare class SheetsBindingManager extends Disposable {
28
30
  setBindingNode(unitId: string, subunitId: string, node: ICellBindingNodeParam): void;
29
31
  removeBindingNode(unitId: string, subunitId: string, row: number, column: number): void;
30
32
  getBindingNode(unitId: string, subunitId: string, row: number, column: number): ICellBindingNode | undefined;
33
+ getBindingNodeById(unitId: string, subunitId: string, nodeId: string): ICellBindingNode | undefined;
31
34
  createModel(unitId: string, subunitId: string, json?: ICellBindingNode[]): SheetBindingModel;
32
35
  toJSON(unitId: string): ICellBindingJSON;
33
36
  fromJSON(unitId: string, json: ICellBindingJSON): void;
@@ -112,6 +112,10 @@ export interface IListDataBindingNode extends ICellBindingNode {
112
112
  endColumn: number;
113
113
  };
114
114
  displayName?: string;
115
+ /**
116
+ * Whether contain header in the list data binding node.
117
+ */
118
+ containHeader?: boolean;
115
119
  }
116
120
  export interface IDataBindingService {
117
121
  /**
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 h=(u,a,M)=>X(u,typeof a!="symbol"?a+"":a,M);var R;class w{constructor(o){h(this,"_matrix",{});h(this,"_nodeMap",new Map);h(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 V(d){return d instanceof Date&&!isNaN(d.getTime())}function j(d){const o=new Date(d);if(!V(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){h(this,"_data");h(this,"id");h(this,"_hasData",!1);h(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);h(this,"type",S.List);h(this,"_isListObject");h(this,"_fieldIndexMap",new Map);h(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}:{t:typeof c=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING,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 E extends N{constructor(e){super(e);h(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,t:typeof s=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING}}getSourceInfo(){return{sourceId:this.id,sourceType:S.Object}}}var x=Object.defineProperty,J=Object.getOwnPropertyDescriptor,L=(d,o,e,t)=>{for(var n=t>1?void 0:t?J(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&&x(o,e,n),n},D=(d,o)=>(e,t)=>o(e,t,d);let y=class extends a.Disposable{constructor(o,e,t){super();h(this,"modelMap",new Map);h(this,"_cellBindInfoUpdate$",new O.Subject);h(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,l)=>{this.getBindingNode(r,i,g,l)&&this.removeBindingNode(r,i,g,l)})}),{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 w,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 w(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([D(0,a.IUniverInstanceService),D(1,a.Inject(M.SheetInterceptorService)),D(2,a.Inject(M.SheetsSelectionsService))],y);class b extends a.Disposable{constructor(){super();h(this,"sourceMap",new Map);h(this,"_sourceDataUpdate$",new O.Subject);h(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 E(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 E(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,G=(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();h(this,"_bindingModel",C.Value);h(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:l,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:l,sheetId:p,id:f,range:B})}}return}if(r===I.Update){const c=e.oldRecordCount,g=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:l,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:_},T={startRow:m,startColumn:_,endRow:m+e.recordCount,endColumn:_};v.remove({unitId:l,sheetId:p,id:f,range:B}),v.insert({unitId:l,sheetId:p,id:f,range:T})}}return}const i=this._sheetsSourceManager.getSource(s,t);if(i&&i.hasData()){const g=i.getSourceInfo().recordCount,l=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:p,subunitId:f,nodeId:m,row:_,column:v}of l){const B=this._ensureRTreeCollection(p),T={startRow:_,startColumn:v,endRow:_+g,endColumn:v};B.insert({unitId:p,sheetId:f,id:m,range:T})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:i,column:c,changeType:g}=e,l=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)l.insert({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Remove)l.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};l.remove({unitId:t,sheetId:n,id:r,range:K})}l.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,l=this._sheetsSourceManager.getSource(e,g);if(l&&l.hasData())return(l==null?void 0:l.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 l=r.getBindingNodeById(g[0]);if(l){const{sourceId:p}=l,f=this._sheetsSourceManager.getSource(e,p);if(f&&f.hasData())return(f==null?void 0:f.getData(l,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=G([U(0,a.Inject(M.SheetInterceptorService)),U(1,a.Inject(y)),U(2,a.Inject(b))],u.SheetsSourceBindService);var W=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&&W(o,e,n),n},$=(d,o)=>(e,t)=>o(e,t,d);u.UniverSheetsBindingSourcePlugin=(R=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]])}},h(R,"type",a.UniverInstanceType.UNIVER_SHEET),h(R,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),R),u.UniverSheetsBindingSourcePlugin=z([$(1,a.Inject(a.Injector)),$(2,a.IConfigService)],u.UniverSheetsBindingSourcePlugin),u.BindModeEnum=C,u.BindingModel=w,u.DataBindingNodeTypeEnum=S,u.SheetsSourceManager=b,u.SourceModelBase=N,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,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):(h=typeof globalThis<"u"?globalThis:h||self,a(h.UniverSheetsSourceBinding={},h.UniverCore,h.UniverSheets,h.rxjs))})(this,function(h,a,I,P){"use strict";var Y=Object.defineProperty;var Z=(h,a,I)=>a in h?Y(h,a,{enumerable:!0,configurable:!0,writable:!0,value:I}):h[a]=I;var g=(h,a,I)=>Z(h,typeof a!="symbol"?a+"":a,I);var D;class U{constructor(o){g(this,"_matrix",{});g(this,"_nodeMap",new Map);g(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 i=s.indexOf(t.nodeId);i>=0&&s.splice(i,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 _=(d=>(d.List="list",d.Object="object",d))(_||{}),b=(d=>(d.Path="path",d.Value="value",d))(b||{}),B=(d=>(d.Add="add",d.Remove="remove",d.Update="update",d))(B||{});function x(d){return d instanceof Date&&!isNaN(d.getTime())}function E(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 w{constructor(o){g(this,"_data");g(this,"id");g(this,"_hasData",!1);g(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 $ extends w{constructor(e,t){super(e);g(this,"type",_.List);g(this,"_isListObject");g(this,"_fieldIndexMap",new Map);g(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s,containHeader:i}=e,r=this._fieldIndexMap.get(n),c=t-s;if(i&&c===0)return{v:this._data.fields[r]};let l;const u=i?1:0;return this._isListObject?l=this._data.records[c-u][n]:l=this._data.records[c-u][r],e.isDate===!0?{v:E(l),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{t:typeof l=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING,v:l}}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 V extends w{constructor(e){super(e);g(this,"type",_.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const i of n)if(s=s[i],s===void 0)return null;return e.isDate===!0?{v:E(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:s,t:typeof s=="number"?a.CellValueType.NUMBER:a.CellValueType.STRING}}getSourceInfo(){return{sourceId:this.id,sourceType:_.Object}}}var J=Object.defineProperty,H=Object.getOwnPropertyDescriptor,A=(d,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=d.length-1,i;s>=0;s--)(i=d[s])&&(n=(t?i(o,e,n):i(n))||n);return t&&n&&J(o,e,n),n},O=(d,o)=>(e,t)=>o(e,t,d);let R=class extends a.Disposable{constructor(o,e,t){super();g(this,"modelMap",new Map);g(this,"_cellBindInfoUpdate$",new P.Subject);g(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=I.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:i,subUnitId:r}=s;return(o.id===I.ClearSelectionContentCommand.id||o.id===I.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{a.Range.foreach(c,(l,u)=>{this.getBindingNode(i,r,l,u)&&this.removeBindingNode(i,r,l,u)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,i)=>{const r=s.getBindingNodesBySourceId(o);if(r)for(const c of r)e.push({unitId:n,subunitId:i,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 U,this.addModel(o,e,n)),t.nodeId||(t.nodeId=a.generateRandomId());const{row:s,column:i}=t;if(s===void 0||i===void 0)throw new Error("row and column is required");const r=n.getBindingNode(s,i),c=t.type===_.List?!!t.containHeader:!1;n.setBindingNode(s,i,{...t,row:s,column:i}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:i,containHeader:c,changeType:r?B.Update:B.Add,oldSourceId:r==null?void 0:r.sourceId,oldNodeContainHeader:(r==null?void 0:r.type)===_.List?!!r.containHeader:!1})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const i=s.getBindingNode(t,n);i&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:i.sourceId,nodeId:i.nodeId,row:t,column:n,changeType:B.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}getBindingNodeById(o,e,t){const n=this.getModel(o,e);if(n)return n.getBindingNodeById(t)}createModel(o,e,t){const n=new U(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()}};R=A([O(0,a.IUniverInstanceService),O(1,a.Inject(I.SheetInterceptorService)),O(2,a.Inject(I.SheetsSelectionsService))],R);class T extends a.Disposable{constructor(){super();g(this,"sourceMap",new Map);g(this,"_sourceDataUpdate$",new P.Subject);g(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 i=s===void 0?a.generateRandomId():s;let r;switch(t){case _.List:r=new $(i,n);break;case _.Object:r=new V(i);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(i,r),r}updateSourceData(e,t,n){const s=this._getUnitMap(e),i=t instanceof w?t.getId():t,r=s==null?void 0:s.get(i);if(r)r.setSourceData(n),this._sourceDataUpdate$.next({...r.getSourceInfo(),unitId:e,changeType:B.Add});else throw new Error(`Source not found: ${i}`)}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:B.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 i;switch(s.type){case _.List:i=new $(s.id);break;case _.Object:i=new V(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}i.fromJSON(s),n.set(s.id,i)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var q=Object.defineProperty,G=Object.getOwnPropertyDescriptor,W=(d,o,e,t)=>{for(var n=t>1?void 0:t?G(o,e):o,s=d.length-1,i;s>=0;s--)(i=d[s])&&(n=(t?i(o,e,n):i(n))||n);return t&&n&&q(o,e,n),n},j=(d,o)=>(e,t)=>o(e,t,d);h.SheetsSourceBindService=class extends a.Disposable{constructor(e,t,n){super();g(this,"_bindingModel",b.Value);g(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 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:i}=e;if(n===_.List){if(i===B.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),l=e.recordCount;for(const{unitId:u,subunitId:p,nodeId:f,row:M,column:m}of c){const v=this._getRTeeCollection(s),S=this._sheetsBindingManager.getBindingNodeById(u,p,f);if(v&&(S==null?void 0:S.type)===_.List){const C=S.containHeader?0:1,y={startRow:M,startColumn:m,endRow:M+l-C,endColumn:m};v.remove({unitId:u,sheetId:p,id:f,range:y})}}return}if(i===B.Update){const c=e.oldRecordCount,l=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:u,subunitId:p,nodeId:f,row:M,column:m}of l){const v=this._getRTeeCollection(s),S=this._sheetsBindingManager.getBindingNodeById(u,p,f);if(v&&(S==null?void 0:S.type)===_.List){const C=S.containHeader?0:1,y={startRow:M,startColumn:m,endRow:M+c-C,endColumn:m},N={startRow:M,startColumn:m,endRow:M+e.recordCount-C,endColumn:m};v.remove({unitId:u,sheetId:p,id:f,range:y}),v.insert({unitId:u,sheetId:p,id:f,range:N})}}return}const r=this._sheetsSourceManager.getSource(s,t);if(r&&r.hasData()){const l=r.getSourceInfo().recordCount,u=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:p,subunitId:f,nodeId:M,row:m,column:v}of u){const S=this._ensureRTreeCollection(p),C=this._sheetsBindingManager.getBindingNodeById(p,f,M);if(S&&(C==null?void 0:C.type)===_.List){const y=C.containHeader?0:1,N={startRow:m,startColumn:v,endRow:m+l-y,endColumn:v};S.insert({unitId:p,sheetId:f,id:M,range:N})}}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:i,row:r,column:c,changeType:l,containHeader:u}=e,p=this._ensureRTreeCollection(t),f=this._sheetsSourceManager.getSource(t,s);if(f&&f.hasData()){const M=f.getSourceInfo();if(M.sourceType===_.List){const m=M.recordCount,v=u?0:1,S={startRow:r,startColumn:c,endRow:r+m-v,endColumn:c};if(l===B.Add)p.insert({unitId:t,sheetId:n,id:i,range:S});else if(l===B.Remove)p.remove({unitId:t,sheetId:n,id:i,range:S});else if(l===B.Update){const C=e.oldSourceId,y=e.oldNodeContainHeader?0:1,N=this._sheetsSourceManager.getSource(t,C);if(N&&N.hasData()){const Q=N.getSourceInfo().recordCount,X={startRow:r,startColumn:c,endRow:r+Q-y,endColumn:c};p.remove({unitId:t,sheetId:n,id:i,range:X})}p.insert({unitId:t,sheetId:n,id:i,range:S})}}}}))}_getPathModeCellValue(e,t,n,s){const i=this._sheetsBindingManager.getModel(e,t),r=i==null?void 0:i.getBindingNode(n,s);if(r){const c=r.type;if(c===_.List)return{v:`#{${r.path}}`,s:{cl:{rgb:"blue"}}};if(c===_.Object)return{v:`[${r.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const i=this._sheetsBindingManager.getModel(e,t);if(i){const c=i.getBindingNode(n,s);if(c){const{sourceId:l}=c,u=this._sheetsSourceManager.getSource(e,l);if(u&&u.hasData())return(u==null?void 0:u.getData(c,n,s))||{v:""}}}const r=this._getRTeeCollection(e);if(i&&r){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},l=Array.from(r.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(l.length>0){const u=i.getBindingNodeById(l[0]);if(u){const{sourceId:p}=u,f=this._sheetsSourceManager.getSource(e,p);if(f&&f.hasData())return(f==null?void 0:f.getData(u,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(I.INTERCEPTOR_POINT.CELL_CONTENT,{effect:a.InterceptorEffectEnum.Value|a.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:i,unitId:r,subUnitId:c,workbook:l}=t;let u=null;if(this._bindingModel===b.Path?u=this._getPathModeCellValue(r,c,s,i):u=this._getValueModeCellValue(r,c,s,i),u!==null){const f={...(typeof(e==null?void 0:e.s)=="string"?l.getStyles().get(e==null?void 0:e.s):e==null?void 0:e.s)||{}};return n({...e,...u,s:f})}return n(e)}}))}dispose(){this._bindModelRTreeCollection.clear()}},h.SheetsSourceBindService=W([j(0,a.Inject(I.SheetInterceptorService)),j(1,a.Inject(R)),j(2,a.Inject(T))],h.SheetsSourceBindService);var z=Object.defineProperty,F=Object.getOwnPropertyDescriptor,K=(d,o,e,t)=>{for(var n=t>1?void 0:t?F(o,e):o,s=d.length-1,i;s>=0;s--)(i=d[s])&&(n=(t?i(o,e,n):i(n))||n);return t&&n&&z(o,e,n),n},L=(d,o)=>(e,t)=>o(e,t,d);h.UniverSheetsBindingSourcePlugin=(D=class extends a.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[R],[T],[h.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){a.touchDependencies(this._injector,[[R],[T],[h.SheetsSourceBindService]])}},g(D,"type",a.UniverInstanceType.UNIVER_SHEET),g(D,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),D),h.UniverSheetsBindingSourcePlugin=K([L(1,a.Inject(a.Injector)),L(2,a.IConfigService)],h.UniverSheetsBindingSourcePlugin),h.BindModeEnum=b,h.BindingModel=U,h.DataBindingNodeTypeEnum=_,h.SheetsSourceManager=T,h.SourceModelBase=w,Object.defineProperty(h,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.202501191605",
3
+ "version": "0.5.4-nightly.202501200849",
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.202501191605",
55
- "@univerjs/sheets": "0.5.4-nightly.202501191605"
54
+ "@univerjs/core": "0.5.4-nightly.202501200849",
55
+ "@univerjs/sheets": "0.5.4-nightly.202501200849"
56
56
  },
57
57
  "devDependencies": {
58
58
  "rxjs": "^7.8.1",