generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
213 lines (184 loc) · 5.2 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
213
import { App, Notice, Plugin, PluginSettingTab, Setting, WorkspaceLeaf } from 'obsidian';
interface Settings {
interval: string;
options: string;
enabled: boolean;
viewTypes: string[];
}
const DEFAULT_SETTINGS: Settings = {
interval: '5',
options: 'split',
enabled: true,
viewTypes: ['markdown', 'canvas'],
}
export default class MyPlugin extends Plugin {
settings: Settings;
private timerId: number;
private enabled: boolean = false;
private direction: boolean = true;
async onload() {
await this.loadSettings();
this.addRibbonIcon('rotate-cw', 'Tab Rotation', (evt: MouseEvent) => {
this.enabled = !this.enabled
if (this.enabled) {
this.direction = true;
this.startTabRotation();
new Notice('Tap right rotation enabled!');
} else {
this.stopTabRotation();
new Notice('Tap rotation disabled!');
}
});
this.addSettingTab(new SettingTab(this.app, this));
this.addCommand({
id: 'rotate-right',
name: 'Start rotating to the right',
callback: () => {
if(this.enabled && !this.direction) {
this.direction = false;
this.stopTabRotation();
this.startTabRotation();
new Notice('Tap right rotation enabled!');
}
else if(!this.enabled) {
this.direction = true;
this.startTabRotation();
this.enabled = !this.enabled
new Notice('Tap right rotation enabled!');
}
},
});
this.addCommand({
id: 'rotate-left',
name: 'Start rotating to the left',
callback: () => {
if(this.enabled && !this.direction) {
this.direction = true;
this.stopTabRotation();
this.startTabRotation();
new Notice('Tap left rotation enabled!');
}
else if(!this.enabled) {
this.direction = false;
this.startTabRotation()
this.enabled = !this.enabled
new Notice('Tap left rotation enabled!');
}
},
});
this.addCommand({
id: 'stop-rotation',
name: 'Stop tab rotation',
callback: () => {
if(this.enabled) {
this.stopTabRotation();
this.enabled = !this.enabled
new Notice('Tap rotation disabled!');
}
},
});
}
startTabRotation() {
if (!isNumeric(this.settings.interval)) {
this.settings.interval = "5"
new Notice('The specified internval is not number. The default is set to 5 sec');
}
else if (parseFloat(this.settings.interval) < 0.1) {
this.settings.interval = "5"
new Notice('The specified internval is less than 0.1 sec which is too fast. The default is set to 5 sec');
}
this.timerId = window.setInterval(() => {
(this.direction)? this.rotateRightTabs(): this.rotateLeftTabs()
}, parseFloat(this.settings.interval) * 1000);
}
stopTabRotation() {
window.clearInterval(this.timerId);
}
getLeavesOfTypes(types: string[]): WorkspaceLeaf[] {
const leaves: WorkspaceLeaf[] = [];
if (this.settings.options == 'window') {
this.app.workspace.iterateRootLeaves((leaf) => {
leaves.push(leaf);
});
}
else {
// @ts-expect-error
let tabContainer = this.app.workspace.activeLeaf.parent
tabContainer.children.forEach((leaf: WorkspaceLeaf) => {
leaves.push(leaf);
});
}
return leaves;
}
async rotateRightTabs() {
const active = this.app.workspace.getLeaf();
const leaves: WorkspaceLeaf[] = this.getLeavesOfTypes(this.settings.viewTypes);
if (active) {
const index = leaves.indexOf(active);
if (index === leaves.length - 1) {
this.app.workspace.setActiveLeaf(leaves[0], {focus: true});
} else {
this.app.workspace.setActiveLeaf(leaves[index + 1], {focus: true});
}
}
}
async rotateLeftTabs() {
const active = this.app.workspace.getLeaf();
const leaves: WorkspaceLeaf[] = this.getLeavesOfTypes(this.settings.viewTypes);
if (active) {
const index = leaves.indexOf(active);
if (index === 0) {
this.app.workspace.setActiveLeaf(leaves[leaves.length - 1], {focus: true});
} else {
this.app.workspace.setActiveLeaf(leaves[index - 1], {focus: true});
}
}
}
async onunload() {
window.clearInterval(this.timerId);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Tab Rotation PlugIn'});
new Setting(containerEl)
.setName('Interval')
.setDesc('Set Interval (secs)')
.addText(text => text
.setPlaceholder('Enter interval (secs)')
.setValue(this.plugin.settings.interval)
.onChange(async (value) => {
this.plugin.settings.interval = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Select rotate range')
.addDropdown(dropDown => {
dropDown.addOption('split', 'Split');
dropDown.addOption('window', 'Window');
dropDown.onChange(async (value) => {
this.plugin.settings.options = value;
await this.plugin.saveSettings();
});
});
}
}
function isNumeric(str: string) {
if (typeof str != "string") {
return false;
}
return !isNaN(Number(str)) && !isNaN(parseFloat(str));
}