-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplugin.js
More file actions
82 lines (69 loc) · 2.3 KB
/
plugin.js
File metadata and controls
82 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import DataService from './data-service.js'
const { Plugin, Injector, setDependencies } = UniverCore;
export class DefaultPlugin extends Plugin {
static pluginName = 'DefaultPlugin';
constructor(_injector) {
super();
this._injector = _injector;
}
onStarting() {
this._injector.add([DataService.name, { useClass: DataService }])
}
onReady() {
this._dataService = this._injector.get(DataService.name);
this._dataService.registerReceiveDataCallback(data => {
return this.receiveData(data);
});
}
onRendered() {
}
receiveData(payload) {
const { messageName, commandName, data, workbookData } = payload;
if (messageName === null) {
if (commandName === 'SetWorkbook') {
this.setWorkbookData(JSON.parse(workbookData));
}
else if (commandName === 'GetWorkbook') {
return this.getWorkbookData();
}
else if (commandName == 'UpdateRange') {
this.updateRange(data);
}
}
return null;
}
setWorkbookData(data) {
this._sheet = this._sheet || this._dataService.getUniverSheet();
const { univerAPI } = this._sheet;
const activeWorkbook = univerAPI.getActiveWorkbook()
const unitId = activeWorkbook?.getId()
if (unitId) {
univerAPI.disposeUnit(unitId)
}
univerAPI.createWorkbook(data);
}
getWorkbookData() {
this._sheet = this._sheet || this._dataService.getUniverSheet();
const { univerAPI } = this._sheet;
const data = univerAPI.getActiveWorkbook().save();
delete data.id;
delete data.name;
delete data.sheetOrder;
delete data.appVersion;
delete data.resources;
return {
messageName: null,
commandName: null,
data: JSON.stringify(data)
};
}
updateRange(data) {
this._sheet = this._sheet || this._dataService.getUniverSheet();
const { univerAPI } = this._sheet;
const sheet = univerAPI.getActiveWorkbook().getActiveSheet();
const range = sheet.getRange(data.range);
range.setValue(data.value);
}
}
// 设置依赖
setDependencies(DefaultPlugin, [Injector]);