@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 +1 -1
- package/lib/es/index.js +178 -163
- package/lib/types/controllers/binding-manager.d.ts +3 -0
- package/lib/types/types.d.ts +4 -0
- package/lib/umd/index.js +1 -1
- package/package.json +3 -3
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
|
2
|
-
var
|
3
|
-
var
|
4
|
-
import { CellValueType as
|
5
|
-
import { SheetInterceptorService as
|
6
|
-
import { Subject as
|
7
|
-
class
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
56
|
-
function
|
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
|
59
|
+
function A(i) {
|
60
60
|
const e = new Date(i);
|
61
|
-
if (!
|
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
|
67
|
+
class U {
|
68
68
|
constructor(e) {
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
97
|
+
class x extends U {
|
98
98
|
constructor(t, n) {
|
99
99
|
super(t);
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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,
|
116
|
-
if (
|
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[
|
118
|
+
v: this._data.fields[d]
|
119
119
|
};
|
120
120
|
let a;
|
121
|
-
|
122
|
-
|
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:
|
129
|
+
t: v.NUMBER
|
129
130
|
} : {
|
130
|
-
t: typeof a == "number" ?
|
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
|
151
|
+
class E extends U {
|
151
152
|
constructor(t) {
|
152
153
|
super(t);
|
153
|
-
|
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:
|
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:
|
169
|
+
t: v.NUMBER
|
169
170
|
} : {
|
170
171
|
v: s,
|
171
|
-
t: typeof s == "number" ?
|
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:
|
178
|
+
sourceType: g.Object
|
178
179
|
};
|
179
180
|
}
|
180
181
|
}
|
181
|
-
var
|
182
|
-
for (var o = n > 1 ? void 0 : n ?
|
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 &&
|
185
|
+
return n && o && de(e, t, o), o;
|
185
186
|
}, b = (i, e) => (t, n) => e(t, n, i);
|
186
|
-
let
|
187
|
+
let N = class extends O {
|
187
188
|
constructor(e, t, n) {
|
188
189
|
super();
|
189
|
-
|
190
|
-
|
191
|
-
|
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 =
|
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 ===
|
206
|
-
|
207
|
-
this.getBindingNode(r, d,
|
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
|
221
|
+
for (const c of d)
|
221
222
|
t.push({
|
222
223
|
unitId: o,
|
223
224
|
subunitId: r,
|
224
225
|
sourceId: e,
|
225
|
-
nodeId:
|
226
|
-
row:
|
227
|
-
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
|
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
|
-
|
255
|
-
|
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:
|
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
|
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
|
-
|
298
|
-
b(0,
|
299
|
-
b(1,
|
300
|
-
b(2,
|
301
|
-
],
|
302
|
-
class
|
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
|
-
|
306
|
-
|
307
|
-
|
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 ?
|
329
|
+
const r = s === void 0 ? J() : s;
|
322
330
|
let d;
|
323
331
|
switch (n) {
|
324
|
-
case
|
325
|
-
d = new
|
332
|
+
case g.List:
|
333
|
+
d = new x(r, o);
|
326
334
|
break;
|
327
|
-
case
|
328
|
-
d = new
|
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
|
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:
|
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:
|
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
|
359
|
-
r = new
|
366
|
+
case g.List:
|
367
|
+
r = new x(s.id);
|
360
368
|
break;
|
361
|
-
case
|
362
|
-
r = new
|
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
|
375
|
-
for (var o = n > 1 ? void 0 : n ?
|
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 &&
|
378
|
-
},
|
379
|
-
let R = class extends
|
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
|
-
|
383
|
-
|
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 =
|
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 =
|
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
|
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 ===
|
443
|
-
if (s ===
|
444
|
-
const d = this._sheetsBindingManager.getBindingModelBySourceId(t),
|
445
|
-
for (const { unitId:
|
446
|
-
const
|
447
|
-
if (
|
448
|
-
const I = { startRow: p, startColumn:
|
449
|
-
|
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 ===
|
455
|
-
const d = e.oldRecordCount,
|
456
|
-
for (const { unitId:
|
457
|
-
const
|
458
|
-
if (
|
459
|
-
const I = { startRow: p, startColumn:
|
460
|
-
|
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
|
468
|
-
for (const { unitId:
|
469
|
-
const
|
470
|
-
|
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,
|
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
|
478
|
-
if (
|
479
|
-
const
|
480
|
-
if (
|
481
|
-
|
482
|
-
else if (
|
483
|
-
|
484
|
-
else if (
|
485
|
-
const
|
486
|
-
if (
|
487
|
-
const
|
488
|
-
|
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
|
-
|
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 ===
|
511
|
+
if (d === g.List)
|
501
512
|
return {
|
502
513
|
v: `#{${r.path}}`,
|
503
514
|
s: { cl: { rgb: "blue" } }
|
504
515
|
};
|
505
|
-
if (d ===
|
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:
|
518
|
-
if (
|
519
|
-
return (
|
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 },
|
525
|
-
if (
|
526
|
-
const
|
527
|
-
if (
|
528
|
-
const { sourceId:
|
529
|
-
if (
|
530
|
-
return (
|
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(
|
537
|
-
effect:
|
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
|
-
|
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 =
|
551
|
-
|
552
|
-
|
553
|
-
|
565
|
+
R = le([
|
566
|
+
D(0, C(V)),
|
567
|
+
D(1, C(N)),
|
568
|
+
D(2, C(T))
|
554
569
|
], R);
|
555
|
-
var
|
556
|
-
for (var o = n > 1 ? void 0 : n ?
|
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 &&
|
559
|
-
},
|
560
|
-
let
|
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
|
-
[
|
567
|
-
[
|
581
|
+
[N],
|
582
|
+
[T],
|
568
583
|
[R]
|
569
584
|
].forEach((i) => this._injector.add(i));
|
570
585
|
}
|
571
586
|
onReady() {
|
572
|
-
|
573
|
-
[
|
574
|
-
[
|
587
|
+
Z(this._injector, [
|
588
|
+
[N],
|
589
|
+
[T],
|
575
590
|
[R]
|
576
591
|
]);
|
577
592
|
}
|
578
|
-
},
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
],
|
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
|
-
|
585
|
-
|
586
|
-
|
599
|
+
y as BindModeEnum,
|
600
|
+
$ as BindingModel,
|
601
|
+
g as DataBindingNodeTypeEnum,
|
587
602
|
R as SheetsSourceBindService,
|
588
|
-
|
589
|
-
|
590
|
-
|
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;
|
package/lib/types/types.d.ts
CHANGED
@@ -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.
|
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.
|
55
|
-
"@univerjs/sheets": "0.5.4-nightly.
|
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",
|