Skip to content

Commit

Permalink
feat: support cutom time format
Browse files Browse the repository at this point in the history
  • Loading branch information
沧濯 committed Jul 27, 2024
1 parent ab194e8 commit 5c0b6a4
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To obtain the `AccessKey` and `AccessSecretKey`, you can fetch them from the key

### Plugin configuration in Obsidian

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

## Thanks

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "qiniu-image-uploader",
"name": "Qiniu Image Uploader",
"version": "1.1.3",
"version": "1.1.4",
"minAppVersion": "0.15.0",
"description": "Uploads images from your clipboard to qiniu.com and embeds uploaded image to your note.",
"author": "Jade Zheng",
Expand Down
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "1.1.3",
"name": "obsidian-qiniu-image-uploader",
"version": "1.1.4",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand All @@ -24,6 +24,7 @@
"proxy-agent": "^5.0.0",
"qiniu": "^7.9.0",
"tslib": "2.4.0",
"typescript": "4.7.4"
"typescript": "4.7.4",
"date-fns": "^3.6.0"
}
}
4 changes: 4 additions & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default {
"Name Prefix Desc": "The uploaded filename's prefix in Qiniu, the uploaded filename will be: {{prefix}}{{timestamp}}.",
"Name Prefix Input": "The image's prefix",

"Time Format": "Time format",
"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.",
"Time Format Input": "Time format",

"Region": "Region",
"Region Desc": "The region in which the bucket is located.",

Expand Down
4 changes: 4 additions & 0 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default {
"Name Prefix Desc": "配置上传文件的前缀,文件名将为:前缀{{timestamp}}",
"Name Prefix Input": "上传后的文件前缀",

"Time Format": "时间格式",
"Time Format Desc": "配置文件名中时间的格式,默认为时间戳格式,可按[文档](https://github.com/date-fns/date-fns/blob/main/docs/unicodeTokens.md)配置,例如:yyMMdd-HHmmss",
"Time Format Input": "时间格式",

"Region": "区域",
"Region Desc": "指明桶所在的区域",

Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { format } from 'date-fns';
import { Editor, Notice, Plugin } from 'obsidian';
import { DEFAULT_SETTINGS, PluginSettings, SettingTab } from './settings';
import { Uploader, buildUploaderFrom } from './uploader/uploader';
Expand Down Expand Up @@ -83,7 +84,12 @@ export default class QiniuImageUploader extends Plugin {
private genImageName(image: File) {
const parts = image.type.split('/');
const type = parts[parts.length - 1];
return `${this.settings.namePrefix}${Date.now()}.${type}`;

const timestr = this.settings.timeFormat === ''
? Date.now().toString()
: format(new Date(), this.settings.timeFormat);

return `${this.settings.namePrefix}${timestr}.${type}`;
}

async loadSettings() {
Expand Down
13 changes: 13 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface PluginSettings {
https: string;
domain: string;
namePrefix: string;
timeFormat: string;
region: string;
deleteSource: boolean;
}
Expand All @@ -20,6 +21,7 @@ export const DEFAULT_SETTINGS: PluginSettings = {
https: "No",
domain: "",
namePrefix: "ob-",
timeFormat: "",
region: "z1",
deleteSource: false,
};
Expand Down Expand Up @@ -138,5 +140,16 @@ export class SettingTab extends PluginSettingTab {
this.plugin.settings.namePrefix = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName(t("Time Format"))
.setDesc(t("Time Format Desc"))
.addText(text => text
.setPlaceholder(t("Time Format Input"))
.setValue(this.plugin.settings.timeFormat)
.onChange(async (value) => {
this.plugin.settings.namePrefix = value;
await this.plugin.saveSettings();
}));
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"1.1.0": "0.15.0",
"1.1.1": "0.15.0",
"1.1.2": "0.15.0",
"1.1.3": "0.15.0"
"1.1.3": "0.15.0",
"1.1.4": "0.15.0"
}

0 comments on commit 5c0b6a4

Please sign in to comment.