mirror of
https://github.com/kou029w/quot.git
synced 2025-01-31 14:28:06 +00:00
add page editor
This commit is contained in:
parent
cd01268305
commit
a9064197be
4 changed files with 79 additions and 13 deletions
|
@ -1,14 +1,41 @@
|
||||||
import "./editor.css";
|
import "./editor.css";
|
||||||
|
import beforeunload from "../helpers/beforeunload";
|
||||||
|
import throttle from "../helpers/throttle";
|
||||||
|
|
||||||
export default () => (
|
const intervalMs = 333;
|
||||||
<textarea
|
|
||||||
class="editor"
|
const { block, unblock } = beforeunload();
|
||||||
autofocus
|
|
||||||
onInput={(e) => {
|
const updatePage = throttle(async function (params: {
|
||||||
e.currentTarget.setAttribute(
|
id: number;
|
||||||
"rows",
|
title: string;
|
||||||
String(Math.max(2, e.currentTarget.value.split("\n").length))
|
text: string;
|
||||||
);
|
}) {
|
||||||
}}
|
const res = await fetch(
|
||||||
></textarea>
|
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>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
18
app/src/helpers/beforeunload.ts
Normal file
18
app/src/helpers/beforeunload.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
function beforeunload() {
|
||||||
|
function listener(e: BeforeUnloadEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
e.returnValue = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function block() {
|
||||||
|
window.addEventListener("beforeunload", listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
function unblock() {
|
||||||
|
window.removeEventListener("beforeunload", listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { block, unblock };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default beforeunload;
|
21
app/src/helpers/throttle.ts
Normal file
21
app/src/helpers/throttle.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
function throttle<Fn extends (...args: any[]) => any>(
|
||||||
|
fn: Fn,
|
||||||
|
ms: number
|
||||||
|
): (...args: Parameters<Fn>) => void {
|
||||||
|
let throttledFn = () => undefined;
|
||||||
|
let throttled: boolean = false;
|
||||||
|
|
||||||
|
return (...args: Parameters<Fn>): void => {
|
||||||
|
throttledFn = () => fn(...args);
|
||||||
|
if (throttled) return;
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
throttledFn();
|
||||||
|
throttled = false;
|
||||||
|
}, ms);
|
||||||
|
|
||||||
|
throttled = true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default throttle;
|
|
@ -22,7 +22,7 @@ export default (props: { id: number }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<Editor />
|
<Editor id={props.id} />
|
||||||
<pre>{() => JSON.stringify(page(), null, " ")}</pre>
|
<pre>{() => JSON.stringify(page(), null, " ")}</pre>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue