refactor: re-use editor component and other minor changes

This commit is contained in:
alok8bb 2022-07-15 11:06:41 +05:30
parent f5d3a26ae1
commit c5bfc3dd35
No known key found for this signature in database
GPG Key ID: 748A8580B906551C
7 changed files with 111 additions and 45 deletions

View File

@ -12,7 +12,6 @@
"@lezer/highlight": "^1.0.0", "@lezer/highlight": "^1.0.0",
"@uiw/codemirror-themes": "^4.11.4", "@uiw/codemirror-themes": "^4.11.4",
"@uiw/react-codemirror": "^4.11.4", "@uiw/react-codemirror": "^4.11.4",
"axios": "^0.27.2",
"react": "^18.0.0", "react": "^18.0.0",
"react-dom": "^18.0.0", "react-dom": "^18.0.0",
"react-router-dom": "^6.3.0" "react-router-dom": "^6.3.0"

16
src/api/bin.ts Normal file
View File

@ -0,0 +1,16 @@
export const setPaste = async (code: string): Promise<Response> => {
return await fetch("http://localhost:8080/bin/paste", {
method: "POST",
headers: { "Content-type": "text/plain" },
body: code,
});
};
export const getPaste = async (pasteId: string) => {
return await fetch(`http://localhost:8080/bin/paste/${pasteId}`);
};
export interface SuccessResponse {
message: string;
endpoint: string;
}

View File

@ -1,48 +1,19 @@
import CodeMirror from "@uiw/react-codemirror"; import CodeMirror from "@uiw/react-codemirror";
import React, { useCallback, useState } from "react"; import { FunctionComponent } from "react";
import { GithubDark } from "./EditorTheme"; import Theme from "./EditorTheme";
import axios from "axios";
import { useNavigate } from "react-router-dom";
const Editor = () => { const Editor: FunctionComponent<propTypes> = props => {
const [code, setCode] = useState("");
let navigate = useNavigate();
// TODO: fix this callback thing later
const postPaste = useCallback(async () => {
let res = await axios.post("http://localhost:8080/bin/paste", code, {
headers: { "Content-type": "text/plain" },
});
if (res.status !== 200) {
alert("Something went wrong!");
console.log(res);
}
navigate(`/${res.data["endpoint"]}`);
}, [code]);
const handleSavePaste = async (
event: React.KeyboardEvent<HTMLInputElement>,
) => {
if ((event.ctrlKey || event.metaKey) && event.code === "KeyS") {
event.preventDefault();
postPaste();
}
};
const placeholder = `// paste code here, use ctrl + s to save`;
return ( return (
// TODO: custom implementation? // TODO: custom implementation?
// TODO: fix line wrap // TODO: fix line wrap
<CodeMirror <CodeMirror
className="editor" className="editor"
autoFocus={true} autoFocus={true}
onKeyDown={handleSavePaste} value={props.value}
placeholder={placeholder} onKeyDown={e => props.keyEvent(e)}
placeholder={props.placeholder}
height="100%" height="100%"
onChange={(value: string) => { onChange={val => props.setCode(val)}
setCode(value);
}}
style={{ style={{
width: "100%", width: "100%",
flex: 1, flex: 1,
@ -50,9 +21,27 @@ const Editor = () => {
fontFamily: "IBM Plex Mono", fontFamily: "IBM Plex Mono",
fontSize: "1rem", fontSize: "1rem",
}} }}
theme={GithubDark} editable={props.editable}
readOnly={props.readonly}
theme={Theme}
/> />
); );
}; };
Editor.defaultProps = {
placeholder: `// paste code here, use ctrl + s to save`,
readonly: false,
editable: true,
value: "",
};
interface propTypes {
readonly?: boolean;
editable?: boolean;
placeholder?: string;
value: string;
setCode: Function;
keyEvent: Function;
}
export default Editor; export default Editor;

View File

@ -1,7 +1,8 @@
import { tags as t } from "@lezer/highlight"; import { tags as t } from "@lezer/highlight";
import { createTheme } from "@uiw/codemirror-themes"; import { createTheme } from "@uiw/codemirror-themes";
export const GithubDark = createTheme({ // custom theme based on GitHub dark theme
export default createTheme({
theme: "dark", theme: "dark",
settings: { settings: {
background: "black", background: "black",

View File

@ -6,10 +6,8 @@
} }
body { body {
font-family: "IBM Plex Mono", monospace;
background: black; background: black;
color: white; color: white;
/* min-height: 100%; */
} }
#root { #root {

View File

@ -1,10 +1,33 @@
import { useCallback, useState } from "react";
import { useNavigate } from "react-router-dom";
import { setPaste, SuccessResponse } from "../api/bin";
import BottomBar from "../components/BottomBar"; import BottomBar from "../components/BottomBar";
import Editor from "../components/Editor"; import Editor from "../components/Editor";
function Home() { function Home() {
const [code, setCode] = useState("");
const navigate = useNavigate();
const postPaste = useCallback(async () => {
const res: Response = await setPaste(code);
if (res.status !== 200) {
alert("Something went wrong!");
console.log(res);
}
let data: SuccessResponse = await res.json();
navigate(`/${data.endpoint}`);
}, [code]);
const handleSaveKey = async (event: React.KeyboardEvent<HTMLDivElement>) => {
if ((event.ctrlKey || event.metaKey) && event.code === "KeyS") {
event.preventDefault();
postPaste();
}
};
return ( return (
<> <>
<Editor /> <Editor setCode={setCode} value={code} keyEvent={handleSaveKey} />
<BottomBar /> <BottomBar />
</> </>
); );

View File

@ -1,8 +1,48 @@
import { useEffect, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { getPaste } from "../api/bin";
import BottomBar from "../components/BottomBar";
import Editor from "../components/Editor";
function Paste() { function Paste() {
const { id } = useParams();
const [code, setCode] = useState("");
const navigate = useNavigate();
const getCode = async () => {
if (id == undefined) {
alert("Something went wrong!");
return navigate("/");
}
const res: Response = await getPaste(id);
if (res.status == 404) {
alert("Paste not found!");
navigate("/");
}
if (res.status == 200) {
setCode(await res.text());
}
};
useEffect(() => {
getCode();
}, []);
return ( return (
<div className="container"> <>
<h1>Paste Page</h1> <Editor
</div> setCode={() => {}}
value={code}
keyEvent={() => {}}
editable={false}
readonly={true}
placeholder={""}
/>
<BottomBar />
</>
); );
} }