1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-19 08:28:09 +00:00
quot/app/src/components/editor.tsx

42 lines
1,006 B
TypeScript
Raw Normal View History

2022-08-23 09:49:34 +09:00
import "./editor.css";
2022-08-24 09:50:35 +09:00
import beforeunload from "../helpers/beforeunload";
import throttle from "../helpers/throttle";
2022-08-23 09:49:34 +09:00
2022-08-24 09:50:35 +09:00
const intervalMs = 333;
const { block, unblock } = beforeunload();
const updatePage = throttle(async function (params: {
id: number;
title: string;
text: string;
}) {
const res = await fetch(
new URL(`/pages?id=eq.${params.id}`, import.meta.env.QUOT_API_URL),
{
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify(params),
}
);
if (res.ok) unblock();
},
intervalMs);
export default (props: { id: number }) => {
return (
<textarea
id={String(props.id)}
class="editor"
autofocus
onInput={(e) => {
const text = e.currentTarget.value;
const lines = text.split("\n");
block();
updatePage({ id: props.id, title: lines[0] ?? "", text });
e.currentTarget.setAttribute("rows", String(Math.max(2, lines.length)));
}}
></textarea>
);
};