-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.ts
86 lines (58 loc) · 1.66 KB
/
main.ts
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
83
84
import { App, Editor, MarkdownView, Notice, Plugin, WorkspaceLeaf } from 'obsidian';
import { SpreadsheetView, VIEW_TYPE_SPREADSHEET } from "./view"
async function create_new_file(app, folder_path, file_no){
if(folder_path){
try {
await app.vault.createFolder(folder_path);
} catch (err) {
console.log("issue in making folder");
console.log(err);
}
}
let file_name = "Untitled.sheet"
if(file_no){
file_name = "Untitled"+ file_no +".sheet"
}
let file_path = file_name;
if(folder_path){
file_path = folder_path + "/" + file_name;
}
try {
await app.vault.create(file_path, "");
await app.workspace.getLeaf(true).setViewState({
type: VIEW_TYPE_SPREADSHEET,
active: true,
state: { file: file_path }
});
new Notice('Create spreadsheet at : ' + file_path);
} catch (err) {
const error = err;
if (error.message.includes("File already exists")) {
return await create_new_file(app , folder_path , (file_no||0)+1 )
}
}
}
export default class SpreadsheetPlugin extends Plugin {
async onload() {
const ribbonIconEl = this.addRibbonIcon('table', 'New Spreadsheet', (evt: MouseEvent) => {
create_new_file(this.app, undefined, undefined);
});
let app = this.app;
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
menu.addItem((item) => {
item.setTitle("New spreadsheet").setIcon("document").onClick(function(){
create_new_file(app, file.path, 0 )
});
});
})
);
this.registerView(
VIEW_TYPE_SPREADSHEET,
(leaf: WorkspaceLeaf) => new SpreadsheetView(leaf)
);
this.registerExtensions(["sheet"], VIEW_TYPE_SPREADSHEET);
}
onunload() {
}
}