-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsettings.ts
More file actions
470 lines (426 loc) · 14 KB
/
settings.ts
File metadata and controls
470 lines (426 loc) · 14 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { App, PluginSettingTab, Setting } from 'obsidian';
import ImageManagerPlugin from './main';
import { Translations } from './utils/i18n';
import { normalizeVaultPath } from './utils/path';
export interface ImageManagerSettings {
imageFolder: string;
thumbnailSize: 'small' | 'medium' | 'large';
showImageInfo: boolean;
sortBy: 'name' | 'date' | 'size';
sortOrder: 'asc' | 'desc';
autoRefresh: boolean;
defaultAlignment: 'left' | 'center' | 'right';
useTrashFolder: boolean;
trashFolder: string;
autoCleanupTrash: boolean;
trashCleanupDays: number;
// 新增设置
enableImages: boolean;
enableVideos: boolean;
enableAudio: boolean;
enablePDF: boolean;
pageSize: number;
enablePreviewModal: boolean;
enableKeyboardNav: boolean;
// 国际化设置
language: 'zh' | 'en' | 'system';
// Quarantine 安全扫描
safeScanEnabled: boolean;
safeScanUnrefDays: number;
safeScanMinSize: number; // bytes
// 去重设置
duplicateThreshold: number;
// 自动整理规则
organizeRules: OrganizeRule[];
// 媒体处理默认参数
defaultProcessQuality: number;
defaultProcessFormat: 'webp' | 'jpeg' | 'png';
watermarkText: string;
}
export interface OrganizeRule {
name: string;
enabled: boolean;
pathTemplate: string;
renameTemplate: string;
matchExtensions: string;
}
export const DEFAULT_SETTINGS: ImageManagerSettings = {
imageFolder: '',
thumbnailSize: 'medium',
showImageInfo: true,
sortBy: 'name',
sortOrder: 'asc',
autoRefresh: true,
defaultAlignment: 'center',
useTrashFolder: true,
trashFolder: 'obsidian-media-toolkit-trash',
autoCleanupTrash: false,
trashCleanupDays: 30,
// 新增默认值
enableImages: true,
enableVideos: true,
enableAudio: true,
enablePDF: true,
pageSize: 50,
enablePreviewModal: true,
enableKeyboardNav: true,
// 国际化设置
language: 'system',
// Quarantine 安全扫描
safeScanEnabled: false,
safeScanUnrefDays: 30,
safeScanMinSize: 5 * 1024 * 1024, // 5MB
// 去重
duplicateThreshold: 90,
// 自动整理
organizeRules: [
{
name: 'Default',
enabled: false,
pathTemplate: 'Media/{year}/{month}',
renameTemplate: '{name}',
matchExtensions: 'jpg,jpeg,png,gif,webp'
}
],
// 媒体处理
defaultProcessQuality: 80,
defaultProcessFormat: 'webp',
watermarkText: ''
};
export class SettingsTab extends PluginSettingTab {
plugin: ImageManagerPlugin;
constructor(app: App, plugin: ImageManagerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
// 翻译辅助方法
private t(key: keyof Translations): string {
return this.plugin.t(key);
}
display(): void {
const { containerEl } = this;
containerEl.empty();
// 使用翻译
new Setting(containerEl)
.setName(this.t('pluginSettings'))
.setHeading();
// 媒体文件夹设置
new Setting(containerEl)
.setName(this.t('mediaFolder'))
.setDesc(this.t('mediaFolderDesc'))
.addText(text => text
.setPlaceholder('Attachments/media')
.setValue(this.plugin.settings.imageFolder)
.onChange(async (value) => {
this.plugin.settings.imageFolder = normalizeVaultPath(value);
this.plugin.clearCache();
await this.plugin.saveSettings();
}));
// 缩略图大小
new Setting(containerEl)
.setName(this.t('thumbnailSize'))
.setDesc(this.t('thumbnailSizeDesc'))
.addDropdown(dropdown => dropdown
.addOption('small', this.t('thumbnailSmall'))
.addOption('medium', this.t('thumbnailMedium'))
.addOption('large', this.t('thumbnailLarge'))
.setValue(this.plugin.settings.thumbnailSize)
.onChange(async (value: string) => {
this.plugin.settings.thumbnailSize = value as 'small' | 'medium' | 'large';
await this.plugin.saveSettings();
}));
// 排序方式
new Setting(containerEl)
.setName(this.t('defaultSortBy'))
.setDesc(this.t('sortByDesc'))
.addDropdown(dropdown => dropdown
.addOption('name', this.t('sortByName'))
.addOption('date', this.t('sortByDate'))
.addOption('size', this.t('sortBySize'))
.setValue(this.plugin.settings.sortBy)
.onChange(async (value: string) => {
this.plugin.settings.sortBy = value as 'name' | 'date' | 'size';
await this.plugin.saveSettings();
}));
// 排序顺序
new Setting(containerEl)
.setName(this.t('sortOrder'))
.setDesc(this.t('sortOrderDesc'))
.addDropdown(dropdown => dropdown
.addOption('asc', this.t('sortAsc'))
.addOption('desc', this.t('sortDesc'))
.setValue(this.plugin.settings.sortOrder)
.onChange(async (value: string) => {
this.plugin.settings.sortOrder = value as 'asc' | 'desc';
await this.plugin.saveSettings();
}));
// 显示图片信息
new Setting(containerEl)
.setName(this.t('showImageInfo'))
.setDesc(this.t('showImageInfoDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.showImageInfo)
.onChange(async (value) => {
this.plugin.settings.showImageInfo = value;
await this.plugin.saveSettings();
}));
// 自动刷新
new Setting(containerEl)
.setName(this.t('autoRefresh'))
.setDesc(this.t('autoRefreshDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.autoRefresh)
.onChange(async (value) => {
this.plugin.settings.autoRefresh = value;
await this.plugin.saveSettings();
}));
// 默认对齐方式
new Setting(containerEl)
.setName(this.t('defaultAlignment'))
.setDesc(this.t('alignmentDesc'))
.addDropdown(dropdown => dropdown
.addOption('left', this.t('alignLeft'))
.addOption('center', this.t('alignCenter'))
.addOption('right', this.t('alignRight'))
.setValue(this.plugin.settings.defaultAlignment)
.onChange(async (value: string) => {
this.plugin.settings.defaultAlignment = value as 'left' | 'center' | 'right';
await this.plugin.saveSettings();
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 安全删除设置
new Setting(containerEl)
.setName(this.t('safeDeleteSettings'))
.setHeading();
// 使用隔离文件夹
new Setting(containerEl)
.setName(this.t('useTrashFolder'))
.setDesc(this.t('useTrashFolderDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.useTrashFolder)
.onChange(async (value) => {
this.plugin.settings.useTrashFolder = value;
await this.plugin.saveSettings();
}));
// 隔离文件夹路径
new Setting(containerEl)
.setName(this.t('trashFolderPath'))
.setDesc(this.t('trashFolderPathDesc'))
.addText(text => text
.setPlaceholder('Media-toolkit-trash')
.setValue(this.plugin.settings.trashFolder)
.onChange(async (value) => {
this.plugin.settings.trashFolder = normalizeVaultPath(value);
await this.plugin.saveSettings();
}));
// 自动清理隔离文件夹
new Setting(containerEl)
.setName(this.t('autoCleanupTrash'))
.setDesc(this.t('autoCleanupTrashDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.autoCleanupTrash)
.onChange(async (value) => {
this.plugin.settings.autoCleanupTrash = value;
await this.plugin.saveSettings();
}));
// 清理天数
new Setting(containerEl)
.setName(this.t('cleanupDays'))
.setDesc(this.t('cleanupDaysDesc'))
.addText(text => text
.setPlaceholder('30')
.setValue(String(this.plugin.settings.trashCleanupDays))
.onChange(async (value) => {
const days = parseInt(value, 10);
if (!isNaN(days) && days > 0) {
this.plugin.settings.trashCleanupDays = days;
await this.plugin.saveSettings();
}
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 安全扫描设置
new Setting(containerEl)
.setName(this.t('safeScanSettings'))
.setHeading();
new Setting(containerEl)
.setName(this.t('safeScan'))
.setDesc(this.t('safeScanEnabledDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.safeScanEnabled)
.onChange(async (value) => {
this.plugin.settings.safeScanEnabled = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('safeScanUnrefDays'))
.setDesc(this.t('safeScanUnrefDaysDesc'))
.addText(text => text
.setPlaceholder('30')
.setValue(String(this.plugin.settings.safeScanUnrefDays))
.onChange(async (value) => {
const days = parseInt(value, 10);
if (!isNaN(days) && days > 0) {
this.plugin.settings.safeScanUnrefDays = days;
await this.plugin.saveSettings();
}
}));
new Setting(containerEl)
.setName(this.t('safeScanMinSize'))
.setDesc(this.t('safeScanMinSizeDesc'))
.addText(text => text
.setPlaceholder('5')
.setValue(String(Number((this.plugin.settings.safeScanMinSize / (1024 * 1024)).toFixed(2))))
.onChange(async (value) => {
const sizeMb = parseFloat(value);
if (!isNaN(sizeMb) && sizeMb >= 0) {
this.plugin.settings.safeScanMinSize = Math.round(sizeMb * 1024 * 1024);
await this.plugin.saveSettings();
}
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 重复检测设置
new Setting(containerEl)
.setName(this.t('duplicateDetectionSettings'))
.setHeading();
new Setting(containerEl)
.setName(this.t('duplicateThresholdSetting'))
.setDesc(this.t('duplicateThresholdDesc'))
.addText(text => text
.setPlaceholder('90')
.setValue(String(this.plugin.settings.duplicateThreshold))
.onChange(async (value) => {
const threshold = parseInt(value, 10);
if (!isNaN(threshold) && threshold >= 50 && threshold <= 100) {
this.plugin.settings.duplicateThreshold = threshold;
await this.plugin.saveSettings();
}
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 媒体类型过滤
new Setting(containerEl)
.setName(this.t('mediaTypes'))
.setHeading();
new Setting(containerEl)
.setName(this.t('enableImageSupport'))
.setDesc(this.t('enableImageSupportDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableImages)
.onChange(async (value) => {
this.plugin.settings.enableImages = value;
this.plugin.clearCache();
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('enableVideoSupport'))
.setDesc(this.t('enableVideoSupportDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableVideos)
.onChange(async (value) => {
this.plugin.settings.enableVideos = value;
this.plugin.clearCache();
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('enableAudioSupport'))
.setDesc(this.t('enableAudioSupportDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableAudio)
.onChange(async (value) => {
this.plugin.settings.enableAudio = value;
this.plugin.clearCache();
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('enablePDFSupport'))
.setDesc(this.t('enablePDFSupportDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enablePDF)
.onChange(async (value) => {
this.plugin.settings.enablePDF = value;
this.plugin.clearCache();
await this.plugin.saveSettings();
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 视图设置
new Setting(containerEl)
.setName(this.t('viewSettings'))
.setHeading();
// 语言设置
new Setting(containerEl)
.setName(this.t('interfaceLanguage'))
.setDesc(this.t('languageDesc'))
.addDropdown(dropdown => dropdown
.addOption('system', this.t('languageSystem'))
.addOption('zh', '中文')
.addOption('en', 'English')
.setValue(this.plugin.settings.language)
.onChange(async (value: string) => {
this.plugin.settings.language = value as 'zh' | 'en' | 'system';
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('pageSize'))
.setDesc(this.t('pageSizeDesc'))
.addText(text => text
.setPlaceholder('50')
.setValue(String(this.plugin.settings.pageSize))
.onChange(async (value) => {
const size = parseInt(value, 10);
if (!isNaN(size) && size > 0) {
this.plugin.settings.pageSize = size;
await this.plugin.saveSettings();
}
}));
new Setting(containerEl)
.setName(this.t('enablePreviewModal'))
.setDesc(this.t('enablePreviewModalDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enablePreviewModal)
.onChange(async (value) => {
this.plugin.settings.enablePreviewModal = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(this.t('enableKeyboardNav'))
.setDesc(this.t('enableKeyboardNavDesc'))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableKeyboardNav)
.onChange(async (value) => {
this.plugin.settings.enableKeyboardNav = value;
await this.plugin.saveSettings();
}));
// 分隔线
containerEl.createEl('hr', { cls: 'settings-divider' });
// 帮助信息
new Setting(containerEl)
.setName(this.t('keyboardShortcuts'))
.setHeading();
containerEl.createEl('p', {
text: this.t('shortcutsDesc'),
cls: 'settings-description'
});
const shortcutsList = containerEl.createEl('ul', { cls: 'settings-list' });
shortcutsList.createEl('li', { text: this.t('shortcutOpenLibrary') });
shortcutsList.createEl('li', { text: this.t('shortcutFindUnreferenced') });
shortcutsList.createEl('li', { text: this.t('shortcutOpenTrash') });
new Setting(containerEl)
.setName(this.t('commands'))
.setHeading();
containerEl.createEl('p', {
text: this.t('commandsDesc'),
cls: 'settings-description'
});
const commandsList = containerEl.createEl('ul', { cls: 'settings-list' });
commandsList.createEl('li', { text: this.t('cmdOpenLibrary') });
commandsList.createEl('li', { text: this.t('cmdFindUnreferenced') });
commandsList.createEl('li', { text: this.t('cmdTrashManagement') });
commandsList.createEl('li', { text: this.t('cmdAlignLeft') });
commandsList.createEl('li', { text: this.t('cmdAlignCenter') });
commandsList.createEl('li', { text: this.t('cmdAlignRight') });
}
}