@univerjs/sheets-source-binding 0.5.4-nightly.202501170612 → 0.5.4-nightly.202501171606
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/README.md +131 -2
- package/lib/cjs/index.js +1 -1
- package/lib/es/index.js +65 -63
- package/lib/umd/index.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
@@ -5,12 +5,141 @@
|
|
5
5
|
|
6
6
|
## Introduction
|
7
7
|
|
8
|
-
|
8
|
+
The `sheets-source-binding` plugin is used to read data from third parties. You can set the path of the data format to be bound and the corresponding `source` on the sheet cell to achieve simple data writing to the sheet cell.
|
9
9
|
|
10
|
-
|
10
|
+
> This plugin is currently in draft status and does not support collaboration.
|
11
|
+
|
12
|
+
## Usage `facade`
|
13
|
+
|
14
|
+
#### Example Code
|
15
|
+
|
16
|
+
```ts
|
17
|
+
const fWorkbook = univerAPI.getActiveWorkbook();
|
18
|
+
const fSheet = fWorkbook.getActiveSheet();
|
19
|
+
|
20
|
+
const source = fWorkbook.createSource(univerAPI.Enum.DataBindingNodeTypeEnum.Object);
|
21
|
+
source.setSourceData({
|
22
|
+
user: {
|
23
|
+
name: 'Jack'
|
24
|
+
}
|
25
|
+
})
|
26
|
+
|
27
|
+
fSheet.setBindingNode({
|
28
|
+
row: 1,
|
29
|
+
column: 2,
|
30
|
+
path: 'user.name',
|
31
|
+
type: univerAPI.Enum.DataBindingNodeTypeEnum.Object,
|
32
|
+
sourceId: source.getId()
|
33
|
+
})
|
34
|
+
|
35
|
+
const listSource = fWorkbook.createSource(univerAPI.Enum.DataBindingNodeTypeEnum.List, false);
|
36
|
+
listSource.setSourceData({
|
37
|
+
fields: ['Product', 'Category', 'Date'],
|
38
|
+
records: [['Apple', 1, 1736942545041], ['Banana', 'Fruit', 1736942545041], ['Pen', 'Stationery', 1736942545041]]
|
39
|
+
})
|
40
|
+
fSheet.setBindingNode({
|
41
|
+
row: 1,
|
42
|
+
column: 4,
|
43
|
+
path: 'Product',
|
44
|
+
type: univerAPI.Enum.DataBindingNodeTypeEnum.List,
|
45
|
+
sourceId: listSource.getId()
|
46
|
+
})
|
47
|
+
|
48
|
+
fSheet.setBindingNode({
|
49
|
+
row: 1,
|
50
|
+
column: 5,
|
51
|
+
path: 'Category',
|
52
|
+
type: univerAPI.Enum.DataBindingNodeTypeEnum.List,
|
53
|
+
sourceId: listSource.getId()
|
54
|
+
})
|
55
|
+
fSheet.setBindingNode({
|
56
|
+
row: 1,
|
57
|
+
column: 6,
|
58
|
+
path: 'Date',
|
59
|
+
type: univerAPI.Enum.DataBindingNodeTypeEnum.List,
|
60
|
+
sourceId: listSource.getId(),
|
61
|
+
isDate: true,
|
62
|
+
})
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
#### `facade api` Introduction
|
67
|
+
|
68
|
+
`sheets-source-binding` is divided into two parts, `source` and `bind`:
|
69
|
+
|
70
|
+
The `source` related APIs are encapsulated in `FWorkbook`:
|
71
|
+
|
72
|
+
```ts
|
73
|
+
// Create
|
74
|
+
createSource(type, isListObject?, id?): SourceModelBase;
|
75
|
+
// Get
|
76
|
+
getSource(sourceId): SourceModelBase;
|
77
|
+
// Update data
|
78
|
+
setSourceData(sourceId, data): void;
|
79
|
+
```
|
80
|
+
The `bind` related APIs can be found in `FWorksheet`:
|
81
|
+
|
82
|
+
```ts
|
83
|
+
/**
|
84
|
+
* The binding node of cell, which configures the source id, path, row, column.
|
85
|
+
*/
|
86
|
+
export interface ICellBindingNodeParam {
|
87
|
+
/**
|
88
|
+
* The binding node type, the node type should be the same as the provided source type.
|
89
|
+
*/
|
90
|
+
type: DataBindingNodeTypeEnum;
|
91
|
+
/**
|
92
|
+
* The path of the binding node, the path should be the same as the provided source path.
|
93
|
+
* @example
|
94
|
+
* for object type: the source is: {user: {name: 'Tom'}}, we can set path 'user.name' to represent Tom.
|
95
|
+
* for list type: the source is: {fields: ['name', 'age'], records: [['Tom', 18], ['Jerry', 20]]}, we can set path 'name' to represent all names in data.
|
96
|
+
*/
|
97
|
+
path: string;
|
98
|
+
/**
|
99
|
+
* The source id of the binding node, the source id should be the same as the provided source id.
|
100
|
+
*/
|
101
|
+
sourceId: string;
|
102
|
+
/**
|
103
|
+
* The target row of the binding node.
|
104
|
+
*/
|
105
|
+
row: number;
|
106
|
+
/**
|
107
|
+
* The target column of the binding node.
|
108
|
+
*/
|
109
|
+
column: number;
|
110
|
+
/**
|
111
|
+
* Whether to treat the data as a date.
|
112
|
+
*/
|
113
|
+
isDate?: boolean;
|
114
|
+
}
|
115
|
+
// Set
|
116
|
+
setBindingNode(bindingNode): void;
|
117
|
+
// Remove
|
118
|
+
removeBindingNode(row, column): void;
|
119
|
+
// Get
|
120
|
+
getBindingNode(row, column): ICellBindingNode | undefined;
|
121
|
+
|
122
|
+
```
|
123
|
+
Additionally, we provide data mode and binding mode, which can be switched in `FWorkbook` through the `facade api`:
|
124
|
+
|
125
|
+
```ts
|
126
|
+
// Binding mode
|
127
|
+
fWorkbook.usePathMode();
|
128
|
+
// Value mode
|
129
|
+
fWorkbook.useValueMode()
|
130
|
+
```
|
131
|
+
In value mode, `univer` will directly read the value set in the source. In binding mode, the bound path will be displayed on the cell.
|
11
132
|
|
12
133
|
### Installation
|
13
134
|
|
14
135
|
```shell
|
15
136
|
npm i @univerjs/sheets-source-binding
|
137
|
+
```
|
138
|
+
|
139
|
+
### Import
|
140
|
+
|
141
|
+
```ts
|
142
|
+
import { UniverSheetsBindingSourcePlugin } from '@univerjs/sheets-source-binding';
|
143
|
+
|
144
|
+
univer.registerPlugin(UniverSheetsBindingSourcePlugin);
|
16
145
|
```
|
package/lib/cjs/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
"use strict";var J=Object.defineProperty;var V=(i,o,e)=>o in i?J(i,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[o]=e;var u=(i,o,e)=>V(i,typeof o!="symbol"?o+"":o,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@univerjs/core"),v=require("@univerjs/sheets"),j=require("rxjs");class T{constructor(o){u(this,"_matrix",{});u(this,"_nodeMap",new Map);u(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var _=(i=>(i.List="list",i.Object="object",i))(_||{}),B=(i=>(i.Path="path",i.Value="value",i))(B||{}),I=(i=>(i.Add="add",i.Remove="remove",i.Update="update",i))(I||{});function L(i){return i instanceof Date&&!isNaN(i.getTime())}function $(i){const o=new Date(i);if(!L(o))return i;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class y{constructor(o){u(this,"_data");u(this,"id");u(this,"_hasData",!1);u(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class O extends y{constructor(e,t){super(e);u(this,"type",_.List);u(this,"_isListObject");u(this,"_fieldIndexMap",new Map);u(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),d=t-s;if(d===0)return{v:this._data.fields[r]};let c;return this._isListObject?c=this._data.records[d-1][n]:c=this._data.records[d-1][r],e.isDate===!0?{v:$(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class U extends y{constructor(e){super(e);u(this,"type",_.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:$(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:l.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:_.Object}}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(i,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},R=(i,o)=>(e,t)=>o(e,t,i);let C=class extends l.Disposable{constructor(o,e,t){super();u(this,"modelMap",new Map);u(this,"_cellBindInfoUpdate$",new j.Subject);u(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=v.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:d}=s;return(o.id===v.ClearSelectionContentCommand.id||o.id===v.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{l.Range.foreach(c,(h,a)=>{this.getBindingNode(r,d,h,a)&&this.removeBindingNode(r,d,h,a)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const d=s.getBindingNodesBySourceId(o);if(d)for(const c of d)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new T,this.addModel(o,e,n)),t.nodeId||(t.nodeId=l.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const d=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:d?I.Update:I.Add,oldSourceId:d==null?void 0:d.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new T(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};C=W([R(0,l.IUniverInstanceService),R(1,l.Inject(v.SheetInterceptorService)),R(2,l.Inject(v.SheetsSelectionsService))],C);class w extends l.Disposable{constructor(){super();u(this,"sourceMap",new Map);u(this,"_sourceDataUpdate$",new j.Subject);u(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?l.generateRandomId():s;let d;switch(t){case _.List:d=new O(r,n);break;case _.Object:d=new U(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,d),d}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof y?t.getId():t,d=s==null?void 0:s.get(r);if(d)d.setSourceData(n),this._sourceDataUpdate$.next({...d.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case _.List:r=new O(s.id);break;case _.Object:r=new U(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(i,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},D=(i,o)=>(e,t)=>o(e,t,i);exports.SheetsSourceBindService=class extends l.Disposable{constructor(e,t,n){super();u(this,"_bindingModel",B.Value);u(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=B.Path}useValueMode(){this._bindingModel=B.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new l.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===_.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),h=e.recordCount;for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of c){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+h,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m})}}return}if(r===I.Update){const c=e.oldRecordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:a,subunitId:f,nodeId:g,row:S,column:p}of h){const M=this._getRTeeCollection(s);if(M){const m={startRow:S,startColumn:p,endRow:S+c,endColumn:p},b={startRow:S,startColumn:p,endRow:S+e.recordCount,endColumn:p};M.remove({unitId:a,sheetId:f,id:g,range:m}),M.insert({unitId:a,sheetId:f,id:g,range:b})}}return}const d=this._sheetsSourceManager.getSource(s,t);if(d&&d.hasData()){const h=d.getSourceInfo().recordCount,a=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:f,subunitId:g,nodeId:S,row:p,column:M}of a){const m=this._ensureRTreeCollection(f),b={startRow:p,startColumn:M,endRow:p+h,endColumn:M};m.insert({unitId:f,sheetId:g,id:S,range:b})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:d,column:c,changeType:h}=e,a=this._ensureRTreeCollection(t),f=this._sheetsSourceManager.getSource(t,s);if(f&&f.hasData()){const g=f.getSourceInfo();if(g.sourceType===_.List){const S=g.recordCount,p={startRow:d,startColumn:c,endRow:d+S,endColumn:c};if(h===I.Add)a.insert({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Remove)a.remove({unitId:t,sheetId:n,id:r,range:p});else if(h===I.Update){const M=e.oldSourceId,m=this._sheetsSourceManager.getSource(t,M);if(m&&m.hasData()){const x=m.getSourceInfo().recordCount,E={startRow:d,startColumn:c,endRow:d+x,endColumn:c};a.remove({unitId:t,sheetId:n,id:r,range:E})}a.insert({unitId:t,sheetId:n,id:r,range:p})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),d=r==null?void 0:r.getBindingNode(n,s);if(d){const c=d.type;if(c===_.List)return{v:`#{${d.path}}`,s:{cl:{rgb:"blue"}}};if(c===_.Object)return{v:`[${d.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:h}=c,a=this._sheetsSourceManager.getSource(e,h);if(a&&a.hasData())return(a==null?void 0:a.getData(c,n,s))||{v:""}}}const d=this._getRTeeCollection(e);if(r&&d){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},h=Array.from(d.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(h.length>0){const a=r.getBindingNodeById(h[0]);if(a){const{sourceId:f}=a,g=this._sheetsSourceManager.getSource(e,f);if(g&&g.hasData())return(g==null?void 0:g.getData(a,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(v.INTERCEPTOR_POINT.CELL_CONTENT,{effect:l.InterceptorEffectEnum.Value|l.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:d,subUnitId:c}=t;let h=null;return this._bindingModel===B.Path?h=this._getPathModeCellValue(d,c,s,r):h=this._getValueModeCellValue(d,c,s,r),n(h!==null?{...e,...h}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}};exports.SheetsSourceBindService=z([D(0,l.Inject(v.SheetInterceptorService)),D(1,l.Inject(C)),D(2,l.Inject(w))],exports.SheetsSourceBindService);var F=Object.defineProperty,K=Object.getOwnPropertyDescriptor,Q=(i,o,e,t)=>{for(var n=t>1?void 0:t?K(o,e):o,s=i.length-1,r;s>=0;s--)(r=i[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&F(o,e,n),n},P=(i,o)=>(e,t)=>o(e,t,i),N;exports.UniverSheetsBindingSourcePlugin=(N=class extends l.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[C],[w],[exports.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){l.touchDependencies(this._injector,[[C],[w],[exports.SheetsSourceBindService]])}},u(N,"type",l.UniverInstanceType.UNIVER_SHEET),u(N,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),N);exports.UniverSheetsBindingSourcePlugin=Q([P(1,l.Inject(l.Injector)),P(2,l.IConfigService)],exports.UniverSheetsBindingSourcePlugin);exports.BindModeEnum=B;exports.BindingModel=T;exports.DataBindingNodeTypeEnum=_;exports.SheetsSourceManager=w;exports.SourceModelBase=y;
|
1
|
+
"use strict";var 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;
|
package/lib/es/index.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
var
|
2
|
-
var H = (i, e, t) => e in i ?
|
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
3
|
var u = (i, e, t) => H(i, typeof e != "symbol" ? e + "" : e, t);
|
4
|
-
import { CellValueType as
|
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
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
6
|
import { Subject as V } from "rxjs";
|
7
|
-
class
|
7
|
+
class U {
|
8
8
|
constructor(e) {
|
9
9
|
u(this, "_matrix", {});
|
10
10
|
u(this, "_nodeMap", /* @__PURE__ */ new Map());
|
@@ -52,7 +52,7 @@ class O {
|
|
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 || {}),
|
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
56
|
function se(i) {
|
57
57
|
return i instanceof Date && !isNaN(i.getTime());
|
58
58
|
}
|
@@ -64,7 +64,7 @@ function L(i) {
|
|
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 T {
|
68
68
|
constructor(e) {
|
69
69
|
u(this, "_data");
|
70
70
|
u(this, "id");
|
@@ -94,7 +94,7 @@ class D {
|
|
94
94
|
this.id = e.id, this.type = e.type;
|
95
95
|
}
|
96
96
|
}
|
97
|
-
class
|
97
|
+
class P extends T {
|
98
98
|
constructor(t, n) {
|
99
99
|
super(t);
|
100
100
|
u(this, "type", f.List);
|
@@ -125,8 +125,9 @@ class U extends D {
|
|
125
125
|
pattern: "yyyy-m-d am/pm h:mm"
|
126
126
|
}
|
127
127
|
},
|
128
|
-
t:
|
128
|
+
t: S.NUMBER
|
129
129
|
} : {
|
130
|
+
t: typeof a == "number" ? S.NUMBER : S.STRING,
|
130
131
|
v: a
|
131
132
|
};
|
132
133
|
}
|
@@ -146,7 +147,7 @@ class U extends D {
|
|
146
147
|
};
|
147
148
|
}
|
148
149
|
}
|
149
|
-
class
|
150
|
+
class $ extends T {
|
150
151
|
constructor(t) {
|
151
152
|
super(t);
|
152
153
|
u(this, "type", f.Object);
|
@@ -164,9 +165,10 @@ class P extends D {
|
|
164
165
|
pattern: "yyyy-m-d am/pm h:mm"
|
165
166
|
}
|
166
167
|
},
|
167
|
-
t:
|
168
|
+
t: S.NUMBER
|
168
169
|
} : {
|
169
|
-
v: s
|
170
|
+
v: s,
|
171
|
+
t: typeof s == "number" ? S.NUMBER : S.STRING
|
170
172
|
};
|
171
173
|
}
|
172
174
|
getSourceInfo() {
|
@@ -180,8 +182,8 @@ var re = Object.defineProperty, ie = Object.getOwnPropertyDescriptor, de = (i, e
|
|
180
182
|
for (var o = n > 1 ? void 0 : n ? ie(e, t) : e, s = i.length - 1, r; s >= 0; s--)
|
181
183
|
(r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
|
182
184
|
return n && o && re(e, t, o), o;
|
183
|
-
},
|
184
|
-
let
|
185
|
+
}, b = (i, e) => (t, n) => e(t, n, i);
|
186
|
+
let B = class extends D {
|
185
187
|
constructor(e, t, n) {
|
186
188
|
super();
|
187
189
|
u(this, "modelMap", /* @__PURE__ */ new Map());
|
@@ -237,7 +239,7 @@ let v = class extends y {
|
|
237
239
|
}
|
238
240
|
setBindingNode(e, t, n) {
|
239
241
|
let o = this.getModel(e, t);
|
240
|
-
o || (o = new
|
242
|
+
o || (o = new U(), this.addModel(e, t, o)), n.nodeId || (n.nodeId = j());
|
241
243
|
const { row: s, column: r } = n;
|
242
244
|
if (s === void 0 || r === void 0)
|
243
245
|
throw new Error("row and column is required");
|
@@ -274,7 +276,7 @@ let v = class extends y {
|
|
274
276
|
return s.getBindingNode(n, o);
|
275
277
|
}
|
276
278
|
createModel(e, t, n) {
|
277
|
-
const o = new
|
279
|
+
const o = new U(n);
|
278
280
|
return this.addModel(e, t, o), o;
|
279
281
|
}
|
280
282
|
toJSON(e) {
|
@@ -292,12 +294,12 @@ let v = class extends y {
|
|
292
294
|
this.modelMap.clear(), this._cellBindInfoUpdate$.complete();
|
293
295
|
}
|
294
296
|
};
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
],
|
300
|
-
class
|
297
|
+
B = de([
|
298
|
+
b(0, z),
|
299
|
+
b(1, m(J)),
|
300
|
+
b(2, m(k))
|
301
|
+
], B);
|
302
|
+
class y extends D {
|
301
303
|
constructor() {
|
302
304
|
super();
|
303
305
|
u(this, "sourceMap", /* @__PURE__ */ new Map());
|
@@ -320,10 +322,10 @@ class R extends y {
|
|
320
322
|
let d;
|
321
323
|
switch (n) {
|
322
324
|
case f.List:
|
323
|
-
d = new
|
325
|
+
d = new P(r, o);
|
324
326
|
break;
|
325
327
|
case f.Object:
|
326
|
-
d = new
|
328
|
+
d = new $(r);
|
327
329
|
break;
|
328
330
|
default:
|
329
331
|
throw new Error(`Invalid source type: ${n}`);
|
@@ -331,7 +333,7 @@ class R extends y {
|
|
331
333
|
return this._ensureUnitMap(t).set(r, d), d;
|
332
334
|
}
|
333
335
|
updateSourceData(t, n, o) {
|
334
|
-
const s = this._getUnitMap(t), r = n instanceof
|
336
|
+
const s = this._getUnitMap(t), r = n instanceof T ? n.getId() : n, d = s == null ? void 0 : s.get(r);
|
335
337
|
if (d)
|
336
338
|
d.setSourceData(o), this._sourceDataUpdate$.next({ ...d.getSourceInfo(), unitId: t, changeType: M.Add });
|
337
339
|
else
|
@@ -354,10 +356,10 @@ class R extends y {
|
|
354
356
|
let r;
|
355
357
|
switch (s.type) {
|
356
358
|
case f.List:
|
357
|
-
r = new
|
359
|
+
r = new P(s.id);
|
358
360
|
break;
|
359
361
|
case f.Object:
|
360
|
-
r = new
|
362
|
+
r = new $(s.id);
|
361
363
|
break;
|
362
364
|
default:
|
363
365
|
throw new Error(`Invalid source type: ${s.type}`);
|
@@ -373,11 +375,11 @@ var ae = Object.defineProperty, ce = Object.getOwnPropertyDescriptor, ue = (i, e
|
|
373
375
|
for (var o = n > 1 ? void 0 : n ? ce(e, t) : e, s = i.length - 1, r; s >= 0; s--)
|
374
376
|
(r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
|
375
377
|
return n && o && ae(e, t, o), o;
|
376
|
-
},
|
377
|
-
let
|
378
|
+
}, w = (i, e) => (t, n) => e(t, n, i);
|
379
|
+
let R = class extends D {
|
378
380
|
constructor(e, t, n) {
|
379
381
|
super();
|
380
|
-
u(this, "_bindingModel",
|
382
|
+
u(this, "_bindingModel", v.Value);
|
381
383
|
u(this, "_bindModelRTreeCollection", /* @__PURE__ */ new Map());
|
382
384
|
this._sheetInterceptorService = e, this._sheetsBindingManager = t, this._sheetsSourceManager = n, this._registerInterceptor(), this._registerSourceChange();
|
383
385
|
}
|
@@ -385,13 +387,13 @@ let N = class extends y {
|
|
385
387
|
* Set the binding model to path mode, in this mode, the binding path will show in the cell.
|
386
388
|
*/
|
387
389
|
usePathMode() {
|
388
|
-
this._bindingModel =
|
390
|
+
this._bindingModel = v.Path;
|
389
391
|
}
|
390
392
|
/**
|
391
393
|
* Set the binding model to value mode, in this mode, the value of source will show in the cell.
|
392
394
|
*/
|
393
395
|
useValueMode() {
|
394
|
-
this._bindingModel =
|
396
|
+
this._bindingModel = v.Value;
|
395
397
|
}
|
396
398
|
/**
|
397
399
|
* Get the current binding model.
|
@@ -454,8 +456,8 @@ let N = class extends y {
|
|
454
456
|
for (const { unitId: c, subunitId: l, nodeId: h, row: p, column: g } of a) {
|
455
457
|
const _ = this._getRTeeCollection(o);
|
456
458
|
if (_) {
|
457
|
-
const I = { startRow: p, startColumn: g, endRow: p + d, endColumn: g },
|
458
|
-
_.remove({ unitId: c, sheetId: l, id: h, range: I }), _.insert({ unitId: c, sheetId: l, id: h, range:
|
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 });
|
459
461
|
}
|
460
462
|
}
|
461
463
|
return;
|
@@ -464,8 +466,8 @@ let N = class extends y {
|
|
464
466
|
if (r && r.hasData()) {
|
465
467
|
const a = r.getSourceInfo().recordCount, c = this._sheetsBindingManager.getBindingModelBySourceId(t);
|
466
468
|
for (const { unitId: l, subunitId: h, nodeId: p, row: g, column: _ } of c) {
|
467
|
-
const I = this._ensureRTreeCollection(l),
|
468
|
-
I.insert({ unitId: l, sheetId: h, id: p, range:
|
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 });
|
469
471
|
}
|
470
472
|
}
|
471
473
|
}
|
@@ -482,8 +484,8 @@ let N = class extends y {
|
|
482
484
|
else if (a === M.Update) {
|
483
485
|
const _ = e.oldSourceId, I = this._sheetsSourceManager.getSource(t, _);
|
484
486
|
if (I && I.hasData()) {
|
485
|
-
const A = I.getSourceInfo().recordCount,
|
486
|
-
c.remove({ unitId: t, sheetId: n, id: s, range:
|
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 });
|
487
489
|
}
|
488
490
|
c.insert({ unitId: t, sheetId: n, id: s, range: g });
|
489
491
|
}
|
@@ -532,12 +534,12 @@ let N = class extends y {
|
|
532
534
|
}
|
533
535
|
_registerInterceptor() {
|
534
536
|
this.disposeWithMe(this._sheetInterceptorService.intercept(oe.CELL_CONTENT, {
|
535
|
-
effect:
|
537
|
+
effect: O.Value | O.Style,
|
536
538
|
priority: 102,
|
537
539
|
handler: (e, t, n) => {
|
538
540
|
const { row: o, col: s, unitId: r, subUnitId: d } = t;
|
539
541
|
let a = null;
|
540
|
-
return this._bindingModel ===
|
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);
|
541
543
|
}
|
542
544
|
}));
|
543
545
|
}
|
@@ -545,45 +547,45 @@ let N = class extends y {
|
|
545
547
|
this._bindModelRTreeCollection.clear();
|
546
548
|
}
|
547
549
|
};
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
],
|
550
|
+
R = ue([
|
551
|
+
w(0, m(J)),
|
552
|
+
w(1, m(B)),
|
553
|
+
w(2, m(y))
|
554
|
+
], R);
|
553
555
|
var le = Object.defineProperty, he = Object.getOwnPropertyDescriptor, ge = (i, e, t, n) => {
|
554
556
|
for (var o = n > 1 ? void 0 : n ? he(e, t) : e, s = i.length - 1, r; s >= 0; s--)
|
555
557
|
(r = i[s]) && (o = (n ? r(e, t, o) : r(o)) || o);
|
556
558
|
return n && o && le(e, t, o), o;
|
557
|
-
},
|
558
|
-
let
|
559
|
+
}, x = (i, e) => (t, n) => e(t, n, i), N;
|
560
|
+
let E = (N = class extends K {
|
559
561
|
constructor(i = {}, e, t) {
|
560
562
|
super(), this._config = i, this._injector = e, this._configService = t;
|
561
563
|
}
|
562
564
|
onStarting() {
|
563
565
|
[
|
564
|
-
[
|
565
|
-
[
|
566
|
-
[
|
566
|
+
[B],
|
567
|
+
[y],
|
568
|
+
[R]
|
567
569
|
].forEach((i) => this._injector.add(i));
|
568
570
|
}
|
569
571
|
onReady() {
|
570
572
|
X(this._injector, [
|
571
|
-
[
|
572
|
-
[
|
573
|
-
[
|
573
|
+
[B],
|
574
|
+
[y],
|
575
|
+
[R]
|
574
576
|
]);
|
575
577
|
}
|
576
|
-
}, u(
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
],
|
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);
|
581
583
|
export {
|
582
|
-
|
583
|
-
|
584
|
+
v as BindModeEnum,
|
585
|
+
U as BindingModel,
|
584
586
|
f as DataBindingNodeTypeEnum,
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
587
|
+
R as SheetsSourceBindService,
|
588
|
+
y as SheetsSourceManager,
|
589
|
+
T as SourceModelBase,
|
590
|
+
E as UniverSheetsBindingSourcePlugin
|
589
591
|
};
|
package/lib/umd/index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
(function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","rxjs"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.UniverSheetsSourceBinding={},u.UniverCore,u.UniverSheets,u.rxjs))})(this,function(u,a,M,O){"use strict";var Q=Object.defineProperty;var X=(u,a,M)=>a in u?Q(u,a,{enumerable:!0,configurable:!0,writable:!0,value:M}):u[a]=M;var l=(u,a,M)=>X(u,typeof a!="symbol"?a+"":a,M);var w;class D{constructor(o){l(this,"_matrix",{});l(this,"_nodeMap",new Map);l(this,"_sourceIdMap",new Map);o&&this._init(o)}_init(o){this.fromJSON(o)}getBindingNodesBySourceId(o){const e=this._sourceIdMap.get(o);if(e)return e.map(t=>this._nodeMap.get(t))}setBindingNode(o,e,t){this._matrix[o]||(this._matrix[o]={}),this._matrix[o][e]||(this._matrix[o][e]=t),this._nodeMap.set(t.nodeId,t);const n=this._sourceIdMap.get(t.sourceId);n?n.push(t.nodeId):this._sourceIdMap.set(t.sourceId,[t.nodeId])}getBindingNode(o,e){var t;return(t=this._matrix[o])==null?void 0:t[e]}removeBindingNode(o,e){var n;const t=(n=this._matrix[o])==null?void 0:n[e];if(t){this._matrix[o][e]=void 0,this._nodeMap.delete(t.nodeId);const s=this._sourceIdMap.get(t.sourceId);if(s){const r=s.indexOf(t.nodeId);r>=0&&s.splice(r,1),s.length===0&&this._sourceIdMap.delete(t.sourceId)}}}getBindingNodeById(o){return this._nodeMap.get(o)}fromJSON(o){o.forEach(e=>{this.setBindingNode(e.row,e.column,e)})}toJSON(){return Array.from(this._nodeMap.values())}}var S=(d=>(d.List="list",d.Object="object",d))(S||{}),C=(d=>(d.Path="path",d.Value="value",d))(C||{}),I=(d=>(d.Add="add",d.Remove="remove",d.Update="update",d))(I||{});function x(d){return d instanceof Date&&!isNaN(d.getTime())}function j(d){const o=new Date(d);if(!x(o))return d;const e=new Date(Date.UTC(1900,0,1,0,0,0)),t=new Date(Date.UTC(1900,1,28,0,0,0));let s=(o.getTime()-e.getTime())/(1e3*3600*24);return o>t&&(s+=1),s+1}class N{constructor(o){l(this,"_data");l(this,"id");l(this,"_hasData",!1);l(this,"type");this.id=o}getId(){return this.id}getType(){return this.type}hasData(){return this._hasData}setSourceData(o){this._data=o,this._hasData=!0}toJSON(){return{id:this.id,type:this.type}}fromJSON(o){this.id=o.id,this.type=o.type}}class P extends N{constructor(e,t){super(e);l(this,"type",S.List);l(this,"_isListObject");l(this,"_fieldIndexMap",new Map);l(this,"_data",{fields:[],records:[]});this._isListObject=t!=null?t:!0}toggleListObject(e){this._isListObject=e}getData(e,t){const{path:n,row:s}=e,r=this._fieldIndexMap.get(n),i=t-s;if(i===0)return{v:this._data.fields[r]};let c;return this._isListObject?c=this._data.records[i-1][n]:c=this._data.records[i-1][r],e.isDate===!0?{v:j(c),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:c}}setSourceData(e){super.setSourceData(e);const{fields:t}=e;this._fieldIndexMap.clear(),t.forEach((n,s)=>{this._fieldIndexMap.set(n,s)})}getSourceInfo(){return{sourceId:this.id,sourceType:this.type,fields:this._data.fields,recordCount:this._data.records.length}}}class $ extends N{constructor(e){super(e);l(this,"type",S.Object)}getData(e){const n=e.path.split(".");let s=this._data;for(const r of n)if(s=s[r],s===void 0)return null;return e.isDate===!0?{v:j(s),s:{n:{pattern:"yyyy-m-d am/pm h:mm"}},t:a.CellValueType.NUMBER}:{v:s}}getSourceInfo(){return{sourceId:this.id,sourceType:S.Object}}}var J=Object.defineProperty,V=Object.getOwnPropertyDescriptor,L=(d,o,e,t)=>{for(var n=t>1?void 0:t?V(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&J(o,e,n),n},T=(d,o)=>(e,t)=>o(e,t,d);let y=class extends a.Disposable{constructor(o,e,t){super();l(this,"modelMap",new Map);l(this,"_cellBindInfoUpdate$",new O.Subject);l(this,"cellBindInfoUpdate$",this._cellBindInfoUpdate$.asObservable());this._univerInstanceService=o,this._sheetInterceptorService=e,this._sheetsSelectionsService=t,this._initRemoveCommand()}_initRemoveCommand(){this.disposeWithMe(this._sheetInterceptorService.interceptCommand({getMutations:o=>{const e=[],t=[],n=this._sheetsSelectionsService.getCurrentSelections(),s=M.getSheetCommandTarget(this._univerInstanceService);if(!s||!n||n.length===0)return{redos:[],undos:[]};const{unitId:r,subUnitId:i}=s;return(o.id===M.ClearSelectionContentCommand.id||o.id===M.ClearSelectionAllCommand.id)&&n.forEach(({range:c})=>{a.Range.foreach(c,(g,h)=>{this.getBindingNode(r,i,g,h)&&this.removeBindingNode(r,i,g,h)})}),{redos:e,undos:t}}}))}getBindingModelBySourceId(o){const e=[];return this.modelMap.forEach((t,n)=>{t.forEach((s,r)=>{const i=s.getBindingNodesBySourceId(o);if(i)for(const c of i)e.push({unitId:n,subunitId:r,sourceId:o,nodeId:c.nodeId,row:c.row,column:c.column})})}),e}addModel(o,e,t){var n;this.modelMap.has(o)||this.modelMap.set(o,new Map),(n=this.modelMap.get(o))==null||n.set(e,t)}getModel(o,e){var t;return(t=this.modelMap.get(o))==null?void 0:t.get(e)}setBindingNode(o,e,t){let n=this.getModel(o,e);n||(n=new D,this.addModel(o,e,n)),t.nodeId||(t.nodeId=a.generateRandomId());const{row:s,column:r}=t;if(s===void 0||r===void 0)throw new Error("row and column is required");const i=n.getBindingNode(s,r);n.setBindingNode(s,r,{...t,row:s,column:r}),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:t.sourceId,nodeId:t.nodeId,row:s,column:r,changeType:i?I.Update:I.Add,oldSourceId:i==null?void 0:i.sourceId})}removeBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s){const r=s.getBindingNode(t,n);r&&(s.removeBindingNode(t,n),this._cellBindInfoUpdate$.next({unitId:o,subunitId:e,sourceId:r.sourceId,nodeId:r.nodeId,row:t,column:n,changeType:I.Remove}))}}getBindingNode(o,e,t,n){const s=this.getModel(o,e);if(s)return s.getBindingNode(t,n)}createModel(o,e,t){const n=new D(t);return this.addModel(o,e,n),n}toJSON(o){const e={},t=this.modelMap.get(o);return t&&t.forEach((n,s)=>{e[s]=n.toJSON()}),e}fromJSON(o,e){Object.entries(e).forEach(([t,n])=>{this.createModel(o,t,n)})}dispose(){this.modelMap.clear(),this._cellBindInfoUpdate$.complete()}};y=L([T(0,a.IUniverInstanceService),T(1,a.Inject(M.SheetInterceptorService)),T(2,a.Inject(M.SheetsSelectionsService))],y);class b extends a.Disposable{constructor(){super();l(this,"sourceMap",new Map);l(this,"_sourceDataUpdate$",new O.Subject);l(this,"sourceDataUpdate$",this._sourceDataUpdate$.asObservable())}_ensureUnitMap(e){let t=this.sourceMap.get(e);return t||(t=new Map,this.sourceMap.set(e,t)),t}_getUnitMap(e){return this.sourceMap.get(e)}getSource(e,t){const n=this._getUnitMap(e);return n==null?void 0:n.get(t)}createSource(e,t,n,s){const r=s===void 0?a.generateRandomId():s;let i;switch(t){case S.List:i=new P(r,n);break;case S.Object:i=new $(r);break;default:throw new Error(`Invalid source type: ${t}`)}return this._ensureUnitMap(e).set(r,i),i}updateSourceData(e,t,n){const s=this._getUnitMap(e),r=t instanceof N?t.getId():t,i=s==null?void 0:s.get(r);if(i)i.setSourceData(n),this._sourceDataUpdate$.next({...i.getSourceInfo(),unitId:e,changeType:I.Add});else throw new Error(`Source not found: ${r}`)}removeSource(e,t){const n=this._getUnitMap(e),s=n==null?void 0:n.get(t);s&&(n==null||n.delete(t),this._sourceDataUpdate$.next({...s.getSourceInfo(),unitId:e,changeType:I.Remove}))}toJSON(e){const t=[],n=this._getUnitMap(e);if(n)for(const s of n.values())t.push(s.toJSON());return t}fromJSON(e,t){const n=this._ensureUnitMap(e);for(const s of t){let r;switch(s.type){case S.List:r=new P(s.id);break;case S.Object:r=new $(s.id);break;default:throw new Error(`Invalid source type: ${s.type}`)}r.fromJSON(s),n.set(s.id,r)}}dispose(){this._sourceDataUpdate$.complete(),this.sourceMap.clear()}}var A=Object.defineProperty,q=Object.getOwnPropertyDescriptor,W=(d,o,e,t)=>{for(var n=t>1?void 0:t?q(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&A(o,e,n),n},U=(d,o)=>(e,t)=>o(e,t,d);u.SheetsSourceBindService=class extends a.Disposable{constructor(e,t,n){super();l(this,"_bindingModel",C.Value);l(this,"_bindModelRTreeCollection",new Map);this._sheetInterceptorService=e,this._sheetsBindingManager=t,this._sheetsSourceManager=n,this._registerInterceptor(),this._registerSourceChange()}usePathMode(){this._bindingModel=C.Path}useValueMode(){this._bindingModel=C.Value}getBindingModel(){return this._bindingModel}createBindModel(e,t){return this._sheetsBindingManager.createModel(e,t)}setBindingNode(e,t,n){this._sheetsBindingManager.setBindingNode(e,t,n)}removeBindingNode(e,t,n,s){this._sheetsBindingManager.removeBindingNode(e,t,n,s)}getBindingNode(e,t,n,s){return this._sheetsBindingManager.getBindingNode(e,t,n,s)}getSource(e,t){return this._sheetsSourceManager.getSource(e,t)}createSource(e,t,n,s){return this._sheetsSourceManager.createSource(e,t,n,s)}getSourceBindingPathInfo(e){return{source:this._sheetsSourceManager.toJSON(e),cellBinding:this._sheetsBindingManager.toJSON(e)}}loadSourceBindingPathInfo(e,t){this._sheetsSourceManager.fromJSON(e,t.source),this._sheetsBindingManager.fromJSON(e,t.cellBinding)}_ensureRTreeCollection(e){return this._bindModelRTreeCollection.has(e)||this._bindModelRTreeCollection.set(e,new a.RTree),this._bindModelRTreeCollection.get(e)}_getRTeeCollection(e){return this._bindModelRTreeCollection.get(e)}_registerSourceChange(){this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe(e=>{const{sourceId:t,sourceType:n,unitId:s,changeType:r}=e;if(n===S.List){if(r===I.Remove){const c=this._sheetsBindingManager.getBindingModelBySourceId(t),g=e.recordCount;for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of c){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+g,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B})}}return}if(r===I.Update){const c=e.oldRecordCount,g=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:h,subunitId:p,nodeId:f,row:m,column:_}of g){const v=this._getRTeeCollection(s);if(v){const B={startRow:m,startColumn:_,endRow:m+c,endColumn:_},R={startRow:m,startColumn:_,endRow:m+e.recordCount,endColumn:_};v.remove({unitId:h,sheetId:p,id:f,range:B}),v.insert({unitId:h,sheetId:p,id:f,range:R})}}return}const i=this._sheetsSourceManager.getSource(s,t);if(i&&i.hasData()){const g=i.getSourceInfo().recordCount,h=this._sheetsBindingManager.getBindingModelBySourceId(t);for(const{unitId:p,subunitId:f,nodeId:m,row:_,column:v}of h){const B=this._ensureRTreeCollection(p),R={startRow:_,startColumn:v,endRow:_+g,endColumn:v};B.insert({unitId:p,sheetId:f,id:m,range:R})}}}})),this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe(e=>{const{unitId:t,subunitId:n,sourceId:s,nodeId:r,row:i,column:c,changeType:g}=e,h=this._ensureRTreeCollection(t),p=this._sheetsSourceManager.getSource(t,s);if(p&&p.hasData()){const f=p.getSourceInfo();if(f.sourceType===S.List){const m=f.recordCount,_={startRow:i,startColumn:c,endRow:i+m,endColumn:c};if(g===I.Add)h.insert({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Remove)h.remove({unitId:t,sheetId:n,id:r,range:_});else if(g===I.Update){const v=e.oldSourceId,B=this._sheetsSourceManager.getSource(t,v);if(B&&B.hasData()){const F=B.getSourceInfo().recordCount,K={startRow:i,startColumn:c,endRow:i+F,endColumn:c};h.remove({unitId:t,sheetId:n,id:r,range:K})}h.insert({unitId:t,sheetId:n,id:r,range:_})}}}}))}_getPathModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t),i=r==null?void 0:r.getBindingNode(n,s);if(i){const c=i.type;if(c===S.List)return{v:`#{${i.path}}`,s:{cl:{rgb:"blue"}}};if(c===S.Object)return{v:`[${i.path}]`,s:{cl:{rgb:"blue"}}}}}_getValueModeCellValue(e,t,n,s){const r=this._sheetsBindingManager.getModel(e,t);if(r){const c=r.getBindingNode(n,s);if(c){const{sourceId:g}=c,h=this._sheetsSourceManager.getSource(e,g);if(h&&h.hasData())return(h==null?void 0:h.getData(c,n,s))||{v:""}}}const i=this._getRTeeCollection(e);if(r&&i){const c={startRow:n,startColumn:s,endRow:n,endColumn:s},g=Array.from(i.bulkSearch([{unitId:e,sheetId:t,range:c}]));if(g.length>0){const h=r.getBindingNodeById(g[0]);if(h){const{sourceId:p}=h,f=this._sheetsSourceManager.getSource(e,p);if(f&&f.hasData())return(f==null?void 0:f.getData(h,n,s))||{v:""}}}}}_registerInterceptor(){this.disposeWithMe(this._sheetInterceptorService.intercept(M.INTERCEPTOR_POINT.CELL_CONTENT,{effect:a.InterceptorEffectEnum.Value|a.InterceptorEffectEnum.Style,priority:102,handler:(e,t,n)=>{const{row:s,col:r,unitId:i,subUnitId:c}=t;let g=null;return this._bindingModel===C.Path?g=this._getPathModeCellValue(i,c,s,r):g=this._getValueModeCellValue(i,c,s,r),n(g!==null?{...e,...g}:e)}}))}dispose(){this._bindModelRTreeCollection.clear()}},u.SheetsSourceBindService=W([U(0,a.Inject(M.SheetInterceptorService)),U(1,a.Inject(y)),U(2,a.Inject(b))],u.SheetsSourceBindService);var G=Object.defineProperty,H=Object.getOwnPropertyDescriptor,z=(d,o,e,t)=>{for(var n=t>1?void 0:t?H(o,e):o,s=d.length-1,r;s>=0;s--)(r=d[s])&&(n=(t?r(o,e,n):r(n))||n);return t&&n&&G(o,e,n),n},E=(d,o)=>(e,t)=>o(e,t,d);u.UniverSheetsBindingSourcePlugin=(w=class extends a.Plugin{constructor(o={},e,t){super(),this._config=o,this._injector=e,this._configService=t}onStarting(){[[y],[b],[u.SheetsSourceBindService]].forEach(o=>this._injector.add(o))}onReady(){a.touchDependencies(this._injector,[[y],[b],[u.SheetsSourceBindService]])}},l(w,"type",a.UniverInstanceType.UNIVER_SHEET),l(w,"pluginName","SHEET_BINDING_SOURCE_PLUGIN"),w),u.UniverSheetsBindingSourcePlugin=z([E(1,a.Inject(a.Injector)),E(2,a.IConfigService)],u.UniverSheetsBindingSourcePlugin),u.BindModeEnum=C,u.BindingModel=D,u.DataBindingNodeTypeEnum=S,u.SheetsSourceManager=b,u.SourceModelBase=N,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
1
|
+
(function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets","rxjs"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.UniverSheetsSourceBinding={},u.UniverCore,u.UniverSheets,u.rxjs))})(this,function(u,a,M,O){"use strict";var Q=Object.defineProperty;var X=(u,a,M)=>a in u?Q(u,a,{enumerable:!0,configurable:!0,writable:!0,value:M}):u[a]=M;var 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"})});
|
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.202501171606",
|
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.202501171606",
|
55
|
+
"@univerjs/sheets": "0.5.4-nightly.202501171606"
|
56
56
|
},
|
57
57
|
"devDependencies": {
|
58
58
|
"rxjs": "^7.8.1",
|