-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
313 lines (258 loc) · 10 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { App, Plugin, TFile, MarkdownView, PluginSettingTab, Setting } from 'obsidian';
interface ObsidianAutosaveControlSettings {
saveInterval: number;
}
const DEFAULT_SETTINGS: ObsidianAutosaveControlSettings = {
saveInterval: 10, // default to 10 seconds
};
export default class ObsidianAutosaveControlPlugin extends Plugin {
settings: ObsidianAutosaveControlSettings;
originalVaultModify: (file: TFile, data: string) => Promise<void>;
lockedFiles: Map<string, { file: TFile; content: string; timeoutId: number }> = new Map();
isManualSave: boolean = false;
previousActiveFilePath: string | null = null;
statusIcon: HTMLElement;
async onload() {
console.log("loading plugin obsidian-autosave-control");
// Load settings
await this.loadSettings();
// Create and initialize status icon
this.addStatusIcon();
// Add settings tab
this.addSettingTab(new ObsidianAutosaveControlSettingTab(this.app, this));
// Save the original 'modify' method
this.originalVaultModify = this.app.vault.modify.bind(this.app.vault);
// Override 'modify' method
this.app.vault.modify = async (file: TFile, data: string): Promise<void> => {
const filePath = file.path;
if (this.isManualSave) {
// Allow manual saves to proceed
return await this.originalVaultModify(file, data);
}
if (this.lockedFiles.has(filePath)) {
console.log(`Preventing modification of locked file: ${filePath}`);
// Update the content in memory
const lockedFile = this.lockedFiles.get(filePath)!;
lockedFile.content = data;
// Reset the timeout
clearTimeout(lockedFile.timeoutId);
lockedFile.timeoutId = window.setTimeout(() => {
this.unlockAndSaveFile(filePath);
}, this.settings.saveInterval * 1000);
} else {
// First modification to this file, lock it and store content
console.log(`Locking file: ${filePath}`);
const timeoutId = window.setTimeout(() => {
this.unlockAndSaveFile(filePath);
}, this.settings.saveInterval * 1000);
this.lockedFiles.set(filePath, { file, content: data, timeoutId });
}
// Update the icon state
this.updateIconState();
// Do not save to disk immediately
return;
};
// Listen for file open events to initialize content for open files
this.registerEvent(
this.app.workspace.on('file-open', async (file: TFile | null) => {
if (file && this.lockedFiles.has(file.path)) {
// Update content from editor if available
const content = await this.getFileContent(file);
const lockedFile = this.lockedFiles.get(file.path)!;
lockedFile.content = content;
}
})
);
// Track active file changes
this.registerEvent(
this.app.workspace.on('active-leaf-change', (leaf) => {
if (leaf && leaf.view instanceof MarkdownView) {
const file = leaf.view.file;
if (file) {
const currentFilePath = file.path;
const previousFilePath = this.previousActiveFilePath;
// If there's a previous file, and it's different from the current
if (previousFilePath && previousFilePath !== currentFilePath) {
// Check if the previous file is locked
if (this.lockedFiles.has(previousFilePath)) {
// Forcefully save and unlock the previous file
this.unlockAndSaveFile(previousFilePath);
}
}
// Update the previous active file path
this.previousActiveFilePath = currentFilePath;
}
}
})
);
// Initialize previous active file path
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView && activeView.file) {
this.previousActiveFilePath = activeView.file.path;
}
// Handle window close and quit events
this.registerEvent(this.app.workspace.on('quit', () => {
console.log("App quit event detected. Saving all locked files.");
this.saveAllLockedFiles();
}));
this.registerEvent(this.app.workspace.on('window-close', () => {
console.log("Window close event detected. Saving all locked files.");
this.saveAllLockedFiles();
}));
// Handle file rename events
this.registerEvent(
this.app.vault.on('rename', (file: TFile, oldPath: string) => {
if (this.lockedFiles.has(oldPath)) {
console.log(`File renamed from ${oldPath} to ${file.path}`);
// Get the locked file entry
const lockedFile = this.lockedFiles.get(oldPath)!;
// Remove the old entry and add the new one
this.lockedFiles.delete(oldPath);
this.lockedFiles.set(file.path, { ...lockedFile, file });
// Update the status icon state
this.updateIconState();
}
})
);
// Register file close (active-leaf-change) event listener
this.registerEvent(
this.app.workspace.on('active-leaf-change', (leaf) => {
const previousFilePath = this.previousActiveFilePath;
if (previousFilePath && this.lockedFiles.has(previousFilePath)) {
console.log(`File close detected for: ${previousFilePath}. Saving before closing.`);
this.unlockAndSaveFile(previousFilePath);
}
if (leaf && leaf.view instanceof MarkdownView) {
this.previousActiveFilePath = leaf.view.file?.path ?? null;
}
})
);
}
// Create a status icon in the title bar
addStatusIcon() {
this.statusIcon = this.addStatusBarItem();
this.statusIcon.setText('●'); // Simple circle indicator
//this.statusIcon.setAttr('aria-label', 'Save Status');
this.statusIcon.addClass('save-status-icon');
// Set initial state to green (everything saved)
this.statusIcon.style.color = 'limegreen';
this.statusIcon.setAttribute('title', 'All changes saved');
}
// Update the icon's color and tooltip based on locked files
updateIconState() {
if (this.lockedFiles.size > 0) {
// Set to blue (indicating that some files are locked)
this.statusIcon.style.color = 'deepskyblue';
this.statusIcon.setAttribute('title', 'Changes pending save');
} else {
// Set to green (everything saved)
this.statusIcon.style.color = 'limegreen';
this.statusIcon.setAttribute('title', 'All changes saved');
}
}
async getFileContent(file: TFile): Promise<string> {
const filePath = file.path;
const leaves = this.app.workspace.getLeavesOfType('markdown');
for (const leaf of leaves) {
const view = leaf.view as MarkdownView;
if (view && view.file && view.file.path === filePath) {
return view.editor.getValue();
}
}
// If not open in an editor, read from cache
return await this.app.vault.cachedRead(file);
}
async unlockAndSaveFile(filePath: string) {
const lockedFile = this.lockedFiles.get(filePath);
if (!lockedFile) {
console.error(`No locked file to unlock and save for path: ${filePath}`);
return;
}
// Clear the timeout
clearTimeout(lockedFile.timeoutId);
// Remove from lockedFiles
this.lockedFiles.delete(filePath);
// Flag as manual save to prevent recursion
this.isManualSave = true;
// Now save the file
const content = lockedFile.content;
try {
await this.originalVaultModify(lockedFile.file, content);
console.log(`Saved file: ${filePath}`);
} catch (err) {
console.error(`Failed to save file: ${filePath}`, err);
}
// Reset manual save flag
this.isManualSave = false;
// Update the icon state
this.updateIconState();
}
async saveAllLockedFiles() {
console.log("Saving all locked files before closing.");
for (const filePath of this.lockedFiles.keys()) {
await this.unlockAndSaveFile(filePath);
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
updateTimeouts() {
this.lockedFiles.forEach((lockedFile, filePath) => {
clearTimeout(lockedFile.timeoutId);
// Set new timeout
lockedFile.timeoutId = window.setTimeout(() => {
this.unlockAndSaveFile(filePath);
}, this.settings.saveInterval * 1000);
});
}
onunload() {
console.log("plugin obsidian-autosave-control unloaded");
// Restore the original 'modify' method
this.app.vault.modify = this.originalVaultModify;
// Clear all timeouts
this.lockedFiles.forEach((lockedFile) => {
clearTimeout(lockedFile.timeoutId);
});
// Save all locked files on unload
this.saveAllLockedFiles();
}
}
class ObsidianAutosaveControlSettingTab extends PluginSettingTab {
plugin: ObsidianAutosaveControlPlugin;
constructor(app: App, plugin: ObsidianAutosaveControlPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Obsidian Autosave Control Settings' });
new Setting(containerEl)
.setName('Save Interval')
.setDesc(
'The period (in seconds) during which file changes are kept in memory before being saved to disk. Must be between 1 and 3600 seconds.'
)
.addText((text) =>
text
.setPlaceholder('Enter save interval in seconds')
.setValue(this.plugin.settings.saveInterval.toString())
.onChange(async (value) => {
let newValue = parseInt(value);
if (isNaN(newValue)) {
newValue = DEFAULT_SETTINGS.saveInterval;
}
if (newValue < 1) newValue = 1;
if (newValue > 3600) newValue = 3600;
this.plugin.settings.saveInterval = newValue;
console.log(`Save interval set to: ${newValue} seconds`);
// Save the settings
await this.plugin.saveSettings();
// Update existing timeouts to use new interval
this.plugin.updateTimeouts();
})
);
}
}