-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathEditorMetadataForm.tsx
More file actions
141 lines (127 loc) · 4.02 KB
/
Copy pathEditorMetadataForm.tsx
File metadata and controls
141 lines (127 loc) · 4.02 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
"use client";
import { useState } from "react";
import { useTranslations } from "next-intl";
import { useEditorStore } from "@/lib/editor-store";
import { Input } from "@/components/ui/input";
import { Label } from "@/app/components/ui/label";
import { cn } from "@/lib/utils";
export function EditorMetadataForm() {
const {
title,
description,
tags,
filename,
setTitle,
setDescription,
setTags,
setFilename,
} = useEditorStore();
const t = useTranslations("editorMetadata");
const [tagsInputValue, setTagsInputValue] = useState(() => tags.join(", "));
const [skipNextSync, setSkipNextSync] = useState(false);
const [prevTags, setPrevTags] = useState(tags);
if (tags !== prevTags) {
setPrevTags(tags);
if (skipNextSync) {
setSkipNextSync(false);
} else {
setTagsInputValue(tags.join(", "));
}
}
const handleTagsChange = (value: string) => {
setTagsInputValue(value);
const processedTags = value
.split(",")
.map((tag) => tag.trim())
.filter((tag) => tag.length > 0);
setSkipNextSync(true);
setTags(processedTags);
};
const handleTagsBlur = () => {
const filteredTags = tags.filter((tag) => tag.length > 0);
setSkipNextSync(true);
setTags(filteredTags);
setTagsInputValue(filteredTags.join(", "));
};
const handleFilenameBlur = () => {
if (filename && !filename.endsWith(".md")) {
setFilename(filename + ".md");
}
};
return (
<div className="space-y-4 rounded-lg border border-border bg-card p-4 shadow-sm">
<h2 className="text-lg font-semibold">{t("heading")}</h2>
<div className="grid gap-4 md:grid-cols-2">
{/* 标题 */}
<div className="space-y-2 md:col-span-2">
<Label htmlFor="title">
{t("title.label")} <span className="text-destructive">*</span>
</Label>
<Input
id="title"
placeholder={t("title.placeholder")}
value={title}
onChange={(e) => setTitle(e.target.value)}
required
className={cn(!title && "aria-invalid:border-destructive")}
/>
</div>
{/* 描述 */}
<div className="space-y-2 md:col-span-2">
<Label htmlFor="description">{t("description.label")}</Label>
<Input
id="description"
placeholder={t("description.placeholder")}
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
{/* 标签 */}
<div className="space-y-2">
<Label htmlFor="tags">
{t("tags.label")}{" "}
<span className="text-muted-foreground text-xs">
({t("tags.hint")})
</span>
</Label>
<Input
id="tags"
placeholder={t("tags.placeholder")}
value={tagsInputValue}
onChange={(e) => handleTagsChange(e.target.value)}
onBlur={handleTagsBlur}
/>
</div>
{/* 文件名 */}
<div className="space-y-2">
<Label htmlFor="filename">
{t("filename.label")} <span className="text-destructive">*</span>
</Label>
<Input
id="filename"
placeholder={t("filename.placeholder")}
value={filename}
onChange={(e) => setFilename(e.target.value)}
onBlur={handleFilenameBlur}
required
className={cn(!filename && "aria-invalid:border-destructive")}
/>
<p className="text-xs text-muted-foreground">{t("filename.hint")}</p>
</div>
</div>
{/* 预览标签 */}
{tags.length > 0 && (
<div className="flex flex-wrap gap-2">
{tags.map((tag, index) => (
<span
key={index}
className="inline-flex items-center rounded-full bg-primary/10 px-2.5 py-0.5 text-xs font-medium text-primary"
>
{tag}
</span>
))}
</div>
)}
</div>
);
}