feat(website): 文章发布时间精确到秒
This commit is contained in:
@@ -21,6 +21,7 @@ import {
|
||||
Button,
|
||||
Card,
|
||||
Checkbox,
|
||||
DatePicker,
|
||||
Form,
|
||||
Input,
|
||||
Modal,
|
||||
@@ -150,12 +151,15 @@ export default function ArticleEditor({ initial, insights, onApply, onClose }) {
|
||||
{field(
|
||||
'date',
|
||||
'发布日期',
|
||||
<Input
|
||||
<DatePicker
|
||||
aria-label='发布日期'
|
||||
validateStatus={errors.date ? 'error' : 'default'}
|
||||
status={errors.date ? 'error' : undefined}
|
||||
aria-invalid={Boolean(errors.date)}
|
||||
type='date'
|
||||
value={draft.date}
|
||||
type='dateTime'
|
||||
format='YYYY-MM-DD HH:mm:ss'
|
||||
placeholder='请选择发布日期'
|
||||
style={{ width: '100%' }}
|
||||
value={draft.date || undefined}
|
||||
onChange={(value) => change('date', value)}
|
||||
/>,
|
||||
true,
|
||||
|
||||
@@ -163,7 +163,7 @@ export default function InsightsPanel({ value, onChange, disabled, error }) {
|
||||
? '全部内容'
|
||||
: value.categories.find((c) => c.id === id)?.name || '未分类',
|
||||
},
|
||||
{ title: '发布日期', dataIndex: 'date', width: 140 },
|
||||
{ title: '发布日期', dataIndex: 'date', width: 180 },
|
||||
{
|
||||
title: '操作',
|
||||
key: 'actions',
|
||||
|
||||
@@ -53,9 +53,31 @@ export function categoryError(name, categories, id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const pad = (value) => String(value).padStart(2, '0');
|
||||
|
||||
// Publish time is stored as local "YYYY-MM-DD HH:mm:ss"; legacy "YYYY-MM-DD" stays valid.
|
||||
export function isValidPublishDate(value) {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/.exec(
|
||||
value,
|
||||
);
|
||||
if (!match) return false;
|
||||
const [year, month, day, hour = 0, minute = 0, second = 0] = match
|
||||
.slice(1)
|
||||
.map((part) => (part === undefined ? undefined : Number(part)));
|
||||
const date = new Date(year, month - 1, day, hour, minute, second);
|
||||
return (
|
||||
date.getFullYear() === year &&
|
||||
date.getMonth() === month - 1 &&
|
||||
date.getDate() === day &&
|
||||
date.getHours() === hour &&
|
||||
date.getMinutes() === minute &&
|
||||
date.getSeconds() === second
|
||||
);
|
||||
}
|
||||
|
||||
export function newArticle(category = 'all') {
|
||||
const now = new Date();
|
||||
const date = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
|
||||
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
||||
return {
|
||||
id: '',
|
||||
title: '',
|
||||
@@ -89,11 +111,7 @@ export function articleErrors(article, insights) {
|
||||
)
|
||||
errors.category = '请选择所属分类';
|
||||
const date = String(article.date ?? '');
|
||||
if (
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(date) ||
|
||||
!Number.isFinite(Date.parse(date)) ||
|
||||
new Date(date).toISOString().slice(0, 10) !== date
|
||||
)
|
||||
if (!isValidPublishDate(date))
|
||||
errors.date = date ? '请选择有效的发布日期' : '请选择发布日期';
|
||||
const body = String(article.body ?? '');
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user