generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
212 lines (182 loc) · 4.83 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {FileSystemAdapter, SuggestModal, App, Modal, Notice, Plugin, PluginSettingTab, Setting} from 'obsidian';
import {simpleGit, SimpleGit} from 'simple-git';
// Remember to rename these classes and interfaces!
interface ObsidianEasyGitSettings {
icon: boolean;
}
const DEFAULT_SETTINGS: ObsidianEasyGitSettings = {
icon: true
}
export default class ObsidianEasyGit extends Plugin {
settings: ObsidianEasyGitSettings;
git: SimpleGit
async onload() {
await this.loadSettings();
await this.init();
if (this.settings.icon) {
const ribbonIconEl = this.addRibbonIcon('dice', 'git operator', () => {
new IconModal(this.app, this).open()
});
ribbonIconEl.addClass('my-plugin-ribbon-class');
}
this.addCommand({
id: 'Git commit',
name: 'commit source',
callback: () => {
easyCommit(this)
/* if (this.settings.afterShell.length != 0) { */
/* exec(this.settings.afterShell) */
/* } */
}
});
this.addCommand({
id: 'Git pull',
name: 'pull remote source',
callback: () => {
easyPull(this)
}
});
this.addCommand({
id: 'Git push',
name: 'push source to remote',
callback: () => {
easyPush(this)
}
});
this.addSettingTab(new EasyGitSettingTab(this.app, this));
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async init() {
const adapter = this.app.vault.adapter as FileSystemAdapter;
const path = adapter.getBasePath();
let basePath = path;
this.git = simpleGit({
baseDir: basePath,
binary: 'git',
config: ["core.quotepath=off"]
});
}
}
function easyCommit(app: ObsidianEasyGit) {
new EasyGitModal(app.app, (text) => {
const msg = new Date().toLocaleString()
app.git.add(".")
app.git.commit(text + " " + msg, (err) => {
if (err != null) {
new Notice("Commit wrong:" + err.message)
} else {
new Notice("Commit ok")
easyPush(app)
}
})
}).open();
}
function easyPull(app: ObsidianEasyGit) {
app.git.pull()
new Notice('pull success')
}
function easyPush(app: ObsidianEasyGit) {
if (app.git.remote.length != 0) {
app.git.push()
new Notice("Push ok")
}
}
class EasyGitModal extends Modal {
commitMsg: string;
onSubmit: (result: string) => void;
constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.onSubmit = onSubmit;
}
onOpen() {
const {contentEl} = this;
contentEl.createEl("h1", {text: "editor commit message"});
new Setting(contentEl)
.setName("message")
.addText((text) =>
text.onChange((value) => {
this.commitMsg = value
}));
new Setting(contentEl)
.addButton((btn) =>
btn
.setButtonText("Submit")
.setCta()
.onClick(() => {
this.onSubmit(this.commitMsg);
this.close();
}));
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class EasyGitSettingTab extends PluginSettingTab {
plugin: ObsidianEasyGit;
constructor(app: App, plugin: ObsidianEasyGit) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for easygit.'});
/* new Setting(containerEl) */
/* .setName('AfterShell') */
/* .setDesc('run shell after exec commit') */
/* .addText(text => text */
/* .setPlaceholder('Enter your secret') */
/* .setValue(this.plugin.settings.afterShell) */
/* .onChange(async (value) => { */
/* console.log('Secret: ' + value); */
/* this.plugin.settings.afterShell = value; */
/* await this.plugin.saveSettings(); */
/* })); */
new Setting(containerEl)
.setName('IconOpen')
.setDesc('is icon open,default true')
.addToggle(tog => tog.setValue(this.plugin.settings.icon).onChange(
async (value) => {
this.plugin.settings.icon = value;
await this.plugin.saveSettings();
}
))
}
}
interface Oper {
operator: string;
callback: (arg0: ObsidianEasyGit) => void;
}
export class IconModal extends SuggestModal<Oper> {
plugin: ObsidianEasyGit;
// Returns all available suggestions.
constructor(app: App, plugin: ObsidianEasyGit) {
super(app);
this.plugin = plugin;
}
getSuggestions(): Oper[] {
const opers = [
{operator: "commit", callback: easyCommit},
{operator: "push", callback: easyPush},
{operator: "pull", callback: easyPull},
];
return opers
}
// Renders each suggestion item.
renderSuggestion(oper: Oper, el: HTMLElement) {
el.createEl("div", {text: oper.operator});
}
// Perform action on the selected suggestion.
onChooseSuggestion(oper: Oper) {
oper.callback(this.plugin)
}
}