-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
156 lines (131 loc) · 4.95 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
import { App, Plugin, PluginSettingTab, Setting, TFile, moment, Notice } from 'obsidian'
interface CopyMetadataSettings {
useKeyupEvents: boolean;
timeout: number;
creationTimeFormat: string;
copyCreationTimeToClipboard: boolean;
appendCreationTimeFormat: string;
appendCreationTimeToFileName: boolean;
}
const DEFAULT_SETTINGS: CopyMetadataSettings = {
useKeyupEvents: false,
timeout: 10,
creationTimeFormat: 'YYYYMMDDHHmm',
copyCreationTimeToClipboard: true,
appendCreationTimeFormat: 'YYYYMMDDHHmm',
appendCreationTimeToFileName: false,
}
export default class CopyMetadata extends Plugin {
settings: CopyMetadataSettings
timer: { [key: string]: number } = {}
async onload () {
await this.loadSettings()
this.addSettingTab(new CopyMetadataSettingTab(this.app, this))
// Add commands
this.addCommand({
id: 'copy-creation-time-to-clipboard',
name: 'Copy creation time to clipboard',
callback: () => this.copyCreationTime(),
});
this.addCommand({
id: 'append-creation-time-to-file-name',
name: 'Append creation time to file name',
callback: () => this.appendCreationTimeToFileName(),
});
}
async copyCreationTime() {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
const creationTime = moment(activeFile.stat.ctime).format(this.settings.creationTimeFormat);
navigator.clipboard.writeText(creationTime);
}
}
async appendCreationTimeToFileName() {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
const creationTime = moment(activeFile.stat.ctime).format(this.settings.appendCreationTimeFormat);
// Create the new file name by appending the creation time to the existing name
const newFileName = `${activeFile.basename}${creationTime}.${activeFile.extension}`;
// Create the new file path by appending the new file name to the current directory
const newFilePath = `${activeFile.path.substring(0, activeFile.path.lastIndexOf("/"))}/${newFileName}`;
// Rename the file
try {
if (this.settings.appendCreationTimeToFileName) {
await this.app.fileManager.renameFile(activeFile, newFilePath);
new Notice('File name updated successfully.');
console.log('File name updated successfully.');
} else {
new Notice('Append creation time to file name setting is disabled!');
}
} catch (error) {
new Notice('Failed to update file name!');
console.log(error);
}
} else {
new Notice('No active file to update!');
}
}
async loadSettings () {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
}
async saveSettings () {
await this.saveData(this.settings)
}
/**
* Use a timeout to update the metadata only once the user has stopped typing.
* If the user keeps typing, then it will reset the timeout and start again from zero.
*
* Obsidian doesn't appear to correctly handle this situation otherwise, and pops an
* error to say "<File> has been modified externally, merging changes automatically."
*
* @param {TFile} file
*/
}
class CopyMetadataSettingTab extends PluginSettingTab {
plugin: CopyMetadata
constructor (app: App, plugin: CopyMetadata) {
super(app, plugin)
this.plugin = plugin
}
display (): void {
const { containerEl } = this
containerEl.empty()
// this.containerEl.createEl("h1", { text: "Copy Metadata" });
this.containerEl.createEl("h3", {
text: "Please try reopening the vault or restarting Obsidian if the following setting changes do not take effect.",
});
// this.containerEl.createEl("h2", { text: "Creation time" });
// Date format for creation time setting
new Setting(containerEl)
.setName('Copy creation time format')
.setDesc('MomentJS format, e.g., YYYY-MM-DDTHH:mm.')
.addText(text => text
.setPlaceholder('YYYY-MM-DDTHH:mm')
.setValue(this.plugin.settings.creationTimeFormat)
.onChange(async (value) => {
this.plugin.settings.creationTimeFormat = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Append creation time to file name')
.addToggle(toggle => {
toggle
.setValue(this.plugin.settings.appendCreationTimeToFileName)
.onChange(async (value) => {
this.plugin.settings.appendCreationTimeToFileName = value;
await this.plugin.saveSettings();
});
});
// Add a new setting for the append creation time format
new Setting(containerEl)
.setName('Append creation time format')
.setDesc('MomentJS format, e.g., YYYY-MM-DDTHH:mm.')
.addText(text => text
.setPlaceholder('YYYYMMDDHHmm')
.setValue(this.plugin.settings.appendCreationTimeFormat)
.onChange(async (value) => {
this.plugin.settings.appendCreationTimeFormat = value;
await this.plugin.saveSettings();
}));
}
}