generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
219 lines (187 loc) · 7.48 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
import { App, Editor, Notice, Plugin, PluginSettingTab, Setting, moment, MarkdownPreviewRenderer } from 'obsidian';
interface ImageUploaderSettings {
apiUrl: string;
apiToken: string;
appendSuffix: boolean;
suffixFormat: string;
accessToken: string;
customImageDomain: string;
}
const DEFAULT_SETTINGS: ImageUploaderSettings = {
apiUrl: 'https://api.example.com/upload',
apiToken: '',
appendSuffix: false,
suffixFormat: '-YYYYMMDDHHmmss',
accessToken: '',
customImageDomain: 'https://example.com/read',
}
export default class ImageUploaderPlugin extends Plugin {
settings: ImageUploaderSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new ImageUploaderSettingTab(this.app, this));
this.registerEvent(
this.app.workspace.on('editor-paste', (evt: ClipboardEvent, editor: Editor) => {
this.handlePaste(evt, editor);
})
);
this.registerEvent(
this.app.workspace.on('editor-drop', (evt: DragEvent, editor: Editor) => {
this.handleDrop(evt, editor);
})
);
// TODO not working on editing mode
MarkdownPreviewRenderer.registerPostProcessor(this.processImages.bind(this));
}
processImages(el: HTMLElement) {
const imgs = el.querySelectorAll('img');
imgs.forEach((img) => {
if (this.shouldProcessImage(img.src)) {
const originalSrc = img.getAttribute('src') || '';
img.src = this.addTokenToUrl(originalSrc);
}
});
}
shouldProcessImage(src: string): boolean {
return this.settings.customImageDomain !== null &&
this.settings.customImageDomain.trim() !== '' &&
src.startsWith(this.settings.customImageDomain);
}
addTokenToUrl(src: string): string {
const url = new URL(src);
url.searchParams.append('token', this.settings.accessToken);
return url.toString();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async handlePaste(evt: ClipboardEvent, editor: Editor) {
const files = evt.clipboardData?.files;
if (files && files.length > 0) {
evt.preventDefault();
for (let i = 0; i < files.length; i++) {
if (files[i].type.startsWith('image')) {
await this.uploadAndInsertImage(files[i], editor);
}
}
}
}
async handleDrop(evt: DragEvent, editor: Editor) {
const files = evt.dataTransfer?.files;
if (files && files.length > 0) {
evt.preventDefault();
for (let i = 0; i < files.length; i++) {
if (files[i].type.startsWith('image')) {
await this.uploadAndInsertImage(files[i], editor);
}
}
}
}
async uploadAndInsertImage(file: File, editor: Editor) {
try {
const formData = new FormData();
let fileName = file.name;
if (this.settings.appendSuffix) {
const suffix = moment().format(this.settings.suffixFormat);
const nameParts = fileName.split('.');
if (nameParts.length > 1) {
const extension = nameParts.pop();
fileName = `${nameParts.join('.')}${suffix}.${extension}`;
} else {
fileName = `${fileName}${suffix}`;
}
}
formData.append('file', file, fileName);
const response = await fetch(this.settings.apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.settings.apiToken}`
},
body: formData
});
if (!response.ok) {
throw new Error('Upload failed');
}
const data = await response.json();
const imageUrl = data.url; // 假设API返回的JSON中包含图片URL
editor.replaceSelection(`![${fileName}](${imageUrl})`);
} catch (error) {
console.error('Error uploading image:', error);
new Notice('Failed to upload image');
}
}
}
class ImageUploaderSettingTab extends PluginSettingTab {
plugin: ImageUploaderPlugin;
constructor(app: App, plugin: ImageUploaderPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Image Uploader Settings' });
new Setting(containerEl)
.setName('API URL')
.setDesc('Enter the URL of your image upload API')
.addText(text => text
.setPlaceholder('Enter API URL')
.setValue(this.plugin.settings.apiUrl)
.onChange(async (value) => {
this.plugin.settings.apiUrl = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('API Token')
.setDesc('Enter your API authentication token')
.addText(text => text
.setPlaceholder('Enter API token')
.setValue(this.plugin.settings.apiToken)
.onChange(async (value) => {
this.plugin.settings.apiToken = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Append Suffix to Filename')
.setDesc('Enable to append a custom suffix to the uploaded image filename')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.appendSuffix)
.onChange(async (value) => {
this.plugin.settings.appendSuffix = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Suffix Format')
.setDesc('Enter the format for the filename suffix (e.g., YYYYMMDDHHmmss)')
.addText(text => text
.setPlaceholder('Enter suffix format')
.setValue(this.plugin.settings.suffixFormat)
.onChange(async (value) => {
this.plugin.settings.suffixFormat = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('AccessToken')
.setDesc('Enter your access token')
.addText(text => text
.setPlaceholder('Enter your access token')
.setValue(this.plugin.settings.accessToken)
.onChange(async (value) => {
this.plugin.settings.accessToken = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Custom Image Domain')
.setDesc('Enter the domain for your custom images (e.g., https://example.com/read)')
.addText(text => text
.setPlaceholder('https://example.com/read')
.setValue(this.plugin.settings.customImageDomain)
.onChange(async (value) => {
this.plugin.settings.customImageDomain = value;
await this.plugin.saveSettings();
}));
}
}