Skip to content

Commit 7c8cad6

Browse files
authored
Merge pull request #11 from fhcsrrm/master
feat(toy): added timestamp tool
2 parents e773197 + bb30ef0 commit 7c8cad6

File tree

12 files changed

+160
-3
lines changed

12 files changed

+160
-3
lines changed

locales/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"view.devtoys.convertors.numberBase.label": "Number Base",
3131
"view.devtoys.convertors.numberBase.panel.title": "Number Base",
3232
"view.devtoys.convertors.numberBase.tooltip": "Convert Number Base",
33+
"view.devtoys.convertors.timestamp.label": "Timestamp",
34+
"view.devtoys.convertors.timestamp.panel.title": "UNIX Timestamp",
35+
"view.devtoys.convertors.timestamp.tooltip": "Convert UNIX Timestamp",
3336
"view.devtoys.convertors.tooltip": "All Convertors",
3437
"view.devtoys.generators.hash.label": "Hash",
3538
"view.devtoys.generators.hash.tooltip": "Hash Generator",

locales/zh-CN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"view.devtoys.convertors.numberBase.label": "进制转换",
3131
"view.devtoys.convertors.numberBase.panel.title": "进制转换",
3232
"view.devtoys.convertors.numberBase.tooltip": "2到36进制转换",
33+
"view.devtoys.convertors.timestamp.label": "时间戳",
34+
"view.devtoys.convertors.timestamp.panel.title": "UNIX时间戳转换",
35+
"view.devtoys.convertors.timestamp.tooltip": "UNIX 时间戳转换",
3336
"view.devtoys.convertors.tooltip": "全部转换器",
3437
"view.devtoys.generators.hash.label": "Hash",
3538
"view.devtoys.generators.hash.tooltip": "Hash 生成器",

src/Panel/Timestamp.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { PanelType } from "../shared";
2+
import { ToolPanel } from "../common/ToolPanel";
3+
import * as vscode from "vscode";
4+
import i18n from "../i18n";
5+
6+
export class Timestamp extends ToolPanel<Timestamp> {
7+
constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) {
8+
super(panel, extensionUri, PanelType.timestamp, "vue");
9+
}
10+
11+
public static createOrShow(extensionUri: vscode.Uri) {
12+
super.createOrShow(
13+
extensionUri,
14+
PanelType.timestamp,
15+
i18n.t("view.devtoys.convertors.timestamp.panel.title"),
16+
Timestamp
17+
);
18+
}
19+
20+
public dispose(): void {
21+
super.dispose();
22+
Timestamp.currentPanel = undefined;
23+
}
24+
}
25+
26+
ToolPanel.allPanel.add(Timestamp);

src/commands/showTool.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Jwt } from "../Panel/Jwt";
99
import { NumberBase } from "../Panel/NumberBase";
1010
import { Qrcode } from "../Panel/Qrcode";
1111
import { RegexTester } from "../Panel/RegexTester";
12+
import { Timestamp } from "../Panel/Timestamp";
1213
import { Url } from "../Panel/Url";
1314
import { UUID } from "../Panel/UUID";
1415
import { PanelType } from "../shared";
@@ -51,5 +52,8 @@ export default (context: vscode.ExtensionContext) => (node?: DevToysNode) => {
5152
case PanelType.qrcode:
5253
Qrcode.createOrShow(context.extensionUri);
5354
break;
55+
case PanelType.timestamp:
56+
Timestamp.createOrShow(context.extensionUri);
57+
break;
5458
}
5559
};

src/explorer/explorerNodeManager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ExplorerNodeManager implements Disposable {
4040
tooltip: i18n.t("view.devtoys.convertors.numberBase.tooltip"),
4141
panel: PanelType.numberBase,
4242
},
43+
{
44+
label: i18n.t("view.devtoys.convertors.timestamp.label"),
45+
tooltip: i18n.t("view.devtoys.convertors.timestamp.tooltip"),
46+
panel: PanelType.timestamp,
47+
},
4348
];
4449

4550
const generators: IToolData[] = [

src/shared.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ThemeIcon, Uri } from "vscode";
22

3+
34
export enum PanelType {
45
null = 0,
56
jsonToYaml = "jsonToYaml",
@@ -13,6 +14,7 @@ export enum PanelType {
1314
colorBlindnessSimulator = "colorBlindnessSimulator",
1415
jwt = "jwt",
1516
qrcode = "qrcode",
17+
timestamp = "timestamp"
1618
}
1719

1820
export enum Category {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import en from "./locales/en.json";
2+
import zh_CN from "./locales/zh-CN.json";
3+
import { createI18n } from "vue-i18n";
4+
5+
const i18n = createI18n({
6+
locale: window.displayLanguage || "en",
7+
messages: {
8+
en: en,
9+
"zh-cn": zh_CN,
10+
},
11+
});
12+
export default i18n;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<script setup lang="ts">
2+
import { watch, onMounted, ref } from "vue";
3+
import VsCodeButtonVue from "../common/VsCodeButton.vue";
4+
import VsCodeTextFieldVue from "../common/VsCodeTextField.vue";
5+
const now = ref(new Date());
6+
const timestamp = ref(now.value.getTime());
7+
const datetime = ref(now.value.toISOString().replace(/\..+/, ""));
8+
9+
const timer = ref<ReturnType<typeof setInterval>>();
10+
const handleToggleTimer = () => {
11+
if (timer.value) {
12+
clearInterval(timer.value);
13+
timer.value = undefined;
14+
} else {
15+
timer.value = setInterval(() => (now.value = new Date()), 1000);
16+
}
17+
};
18+
const handleDatetimeInput = ({ target }) => {
19+
const { control } = target;
20+
timestamp.value = control.valueAsNumber;
21+
};
22+
23+
const handleTimestampInput = ({ target }) => {
24+
const { control } = target;
25+
control.style.background = "";
26+
try {
27+
datetime.value = new Date(Number(target.value))
28+
.toISOString()
29+
.replace(/\..+/, "");
30+
if (/^[-+]/.test(datetime.value)) {
31+
throw new RangeError("Invalid time value")
32+
}
33+
} catch (e) {
34+
tsvscode.postMessage({
35+
type: "onError",
36+
value: "Invalid timestamp: " + e,
37+
});
38+
control.style.background = "var(--vscode-inputValidation-errorBackground)";
39+
}
40+
};
41+
onMounted(() => {
42+
handleToggleTimer();
43+
});
44+
</script>
45+
46+
<template>
47+
<h1>{{$t('tool.timestamp.title')}}</h1>
48+
<VsCodeTextFieldVue
49+
style="width: 100%"
50+
@input="handleTimestampInput"
51+
v-model="timestamp"
52+
type="number"
53+
>
54+
{{$t('tool.timestamp.timestamp.textField.label')}}
55+
</VsCodeTextFieldVue>
56+
<VsCodeTextFieldVue
57+
style="width: 100%"
58+
@input="handleDatetimeInput"
59+
v-model="datetime"
60+
type="datetime-local"
61+
>
62+
{{$t('tool.timestamp.dateTime.textField.label')}}
63+
</VsCodeTextFieldVue>
64+
<VsCodeTextFieldVue style="width: 100%" readonly :value="now.getTime()">
65+
{{$t('tool.timestamp.now.textField.label')}}
66+
</VsCodeTextFieldVue>
67+
68+
<VsCodeButtonVue @click="handleToggleTimer">
69+
{{ timer ? $t('tool.timestamp.stop.button.label') : $t('tool.timestamp.start.button.label') }}
70+
</VsCodeButtonVue>
71+
</template>
72+
73+
<style></style>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tool.timestamp.dateTime.textField.label": "Date Time",
3+
"tool.timestamp.now.textField.label": "Current UNIX Timestamp",
4+
"tool.timestamp.start.button.label": "Start",
5+
"tool.timestamp.stop.button.label": "Stop",
6+
"tool.timestamp.timestamp.textField.label": "UNIX Timestamp",
7+
"tool.timestamp.title": "UNIX Timestamp"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tool.timestamp.dateTime.textField.label": "日期时间",
3+
"tool.timestamp.now.textField.label": "当前 UNIX 时间戳",
4+
"tool.timestamp.start.button.label": "开始",
5+
"tool.timestamp.stop.button.label": "停止",
6+
"tool.timestamp.timestamp.textField.label": "UNIX 时间戳",
7+
"tool.timestamp.title": "UNIX 时间戳"
8+
}

0 commit comments

Comments
 (0)