Files
mnote/wolai-frontend/src/components/editor/blocks/ProgressBlock.tsx
T
2025-11-23 10:55:04 +08:00

51 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { createReactBlockSpec } from "@blocknote/react";
export const progressBlock = createReactBlockSpec(
{
type: "progressMeter",
propSchema: {
percent: { default: 0, type: "number" },
auto: { default: true, type: "boolean" },
summary: { default: "", type: "string" },
},
content: "inline",
},
{
render: ({ block, editor }) => {
const percent = block.props.percent ?? 0;
const handleToggle = () => {
editor.updateBlock(block, { props: { auto: !block.props.auto } });
};
const handleBarClick = () => {
if (block.props.auto) return;
const input = window.prompt("设置进度(0-100", percent.toString());
if (!input) return;
const value = Number.parseInt(input, 10);
if (Number.isNaN(value)) return;
editor.updateBlock(block, {
props: { percent: Math.min(100, Math.max(0, value)) },
});
};
return (
<div className="wolai-progress">
<div className="wolai-progress__header">
<span className="wolai-progress__summary">{block.props.summary || "暂无条目"}</span>
<button type="button" className="wolai-progress__mode" onClick={handleToggle}>
{block.props.auto ? "自动" : "手动"}
</button>
</div>
<div className="wolai-progress__bar" onClick={handleBarClick}>
<div className="wolai-progress__fill" style={{ width: `${percent}%` }} />
</div>
<span className="wolai-progress__percent">{percent}%</span>
<div className="wolai-progress__description" />
</div>
);
},
},
)();