Skip to content

Commit 5c0b6a4

Browse files
author
沧濯
committed
feat: support cutom time format
1 parent ab194e8 commit 5c0b6a4

File tree

9 files changed

+49
-9
lines changed

9 files changed

+49
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ To obtain the `AccessKey` and `AccessSecretKey`, you can fetch them from the key
2020

2121
### Plugin configuration in Obsidian
2222

23-
All setting items are required, except for the the `Name Prefix`.
23+
All setting items are required, except for the the `Name Prefix` and `Time Format` which are optional.
2424

2525
## Thanks
2626

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "qiniu-image-uploader",
33
"name": "Qiniu Image Uploader",
4-
"version": "1.1.3",
4+
"version": "1.1.4",
55
"minAppVersion": "0.15.0",
66
"description": "Uploads images from your clipboard to qiniu.com and embeds uploaded image to your note.",
77
"author": "Jade Zheng",

package-lock.json

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "obsidian-sample-plugin",
3-
"version": "1.1.3",
2+
"name": "obsidian-qiniu-image-uploader",
3+
"version": "1.1.4",
44
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {
@@ -24,6 +24,7 @@
2424
"proxy-agent": "^5.0.0",
2525
"qiniu": "^7.9.0",
2626
"tslib": "2.4.0",
27-
"typescript": "4.7.4"
27+
"typescript": "4.7.4",
28+
"date-fns": "^3.6.0"
2829
}
2930
}

src/lang/locale/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default {
2222
"Name Prefix Desc": "The uploaded filename's prefix in Qiniu, the uploaded filename will be: {{prefix}}{{timestamp}}.",
2323
"Name Prefix Input": "The image's prefix",
2424

25+
"Time Format": "Time format",
26+
"Time Format Desc": "The time format in the configuration file name, the default is the timestamp format, and you can configure the format according to the [document](https://github.com/date-fns/date-fns/blob/main/docs/unicodeTokens.md), for example: yyMMdd-HHmmss.",
27+
"Time Format Input": "Time format",
28+
2529
"Region": "Region",
2630
"Region Desc": "The region in which the bucket is located.",
2731

src/lang/locale/zh-cn.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default {
2222
"Name Prefix Desc": "配置上传文件的前缀,文件名将为:前缀{{timestamp}}",
2323
"Name Prefix Input": "上传后的文件前缀",
2424

25+
"Time Format": "时间格式",
26+
"Time Format Desc": "配置文件名中时间的格式,默认为时间戳格式,可按[文档](https://github.com/date-fns/date-fns/blob/main/docs/unicodeTokens.md)配置,例如:yyMMdd-HHmmss",
27+
"Time Format Input": "时间格式",
28+
2529
"Region": "区域",
2630
"Region Desc": "指明桶所在的区域",
2731

src/main.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { format } from 'date-fns';
12
import { Editor, Notice, Plugin } from 'obsidian';
23
import { DEFAULT_SETTINGS, PluginSettings, SettingTab } from './settings';
34
import { Uploader, buildUploaderFrom } from './uploader/uploader';
@@ -83,7 +84,12 @@ export default class QiniuImageUploader extends Plugin {
8384
private genImageName(image: File) {
8485
const parts = image.type.split('/');
8586
const type = parts[parts.length - 1];
86-
return `${this.settings.namePrefix}${Date.now()}.${type}`;
87+
88+
const timestr = this.settings.timeFormat === ''
89+
? Date.now().toString()
90+
: format(new Date(), this.settings.timeFormat);
91+
92+
return `${this.settings.namePrefix}${timestr}.${type}`;
8793
}
8894

8995
async loadSettings() {

src/settings.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface PluginSettings {
99
https: string;
1010
domain: string;
1111
namePrefix: string;
12+
timeFormat: string;
1213
region: string;
1314
deleteSource: boolean;
1415
}
@@ -20,6 +21,7 @@ export const DEFAULT_SETTINGS: PluginSettings = {
2021
https: "No",
2122
domain: "",
2223
namePrefix: "ob-",
24+
timeFormat: "",
2325
region: "z1",
2426
deleteSource: false,
2527
};
@@ -138,5 +140,16 @@ export class SettingTab extends PluginSettingTab {
138140
this.plugin.settings.namePrefix = value;
139141
await this.plugin.saveSettings();
140142
}));
143+
144+
new Setting(containerEl)
145+
.setName(t("Time Format"))
146+
.setDesc(t("Time Format Desc"))
147+
.addText(text => text
148+
.setPlaceholder(t("Time Format Input"))
149+
.setValue(this.plugin.settings.timeFormat)
150+
.onChange(async (value) => {
151+
this.plugin.settings.namePrefix = value;
152+
await this.plugin.saveSettings();
153+
}));
141154
}
142155
}

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"1.1.0": "0.15.0",
44
"1.1.1": "0.15.0",
55
"1.1.2": "0.15.0",
6-
"1.1.3": "0.15.0"
6+
"1.1.3": "0.15.0",
7+
"1.1.4": "0.15.0"
78
}

0 commit comments

Comments
 (0)