xonecas.io
466
Me

marked-editor / commit df663c4

Adds missing client folder

Sean2023-01-18 df663c42af6f14aaeada4aa3cae7b662710c0947 parent b821788

2 files changed

client/src/components/editor/index.tsx+73−0
@@ -0,0 +1,73 @@
1+import React, { useEffect, useState } from "react";
2+import styled from "styled-components";
3+import { useDarkMode } from "usehooks-ts";
4+import { marked } from "marked";
5+import placeholderLine from "./placeholder";
6+
7+export default function Editor() {
8+ const { toggle } = useDarkMode();
9+ const [placeholder, setPlaceholder] = useState("");
10+ const [content, setContent] = useState("");
11+ const [raw, setRaw] = useState("");
12+
13+ const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
14+ setRaw(e.target.value);
15+ setContent(marked.parse(e.target.value));
16+ };
17+
18+ const onKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
19+ if (e.key === "Enter" && e.ctrlKey) {
20+ toggle();
21+ }
22+ };
23+
24+ useEffect(() => {
25+ if (!content) {
26+ setPlaceholder(placeholderLine());
27+ }
28+ }, [content]);
29+
30+ return (
31+ <EditorContainer>
32+ <EditorElement
33+ tabIndex={0}
34+ value={raw}
35+ placeholder={placeholder}
36+ onChange={onChange}
37+ onKeyDown={onKeyDown}
38+ ></EditorElement>
39+ <EditorPreview dangerouslySetInnerHTML={{ __html: content }} />
40+ </EditorContainer>
41+ );
42+}
43+
44+const EditorContainer = styled.div`
45+ display: grid;
46+ grid-template-columns: 1fr 1fr;
47+ column-gap: 2em;
48+ width: 100%;
49+ height: 100%;
50+ padding: 0 1em;
51+
52+ color: ${(props) => props.theme.fg};
53+ background: ${(props) => props.theme.bg};
54+`;
55+
56+const EditorElement = styled.textarea`
57+ display: block;
58+ height: 100%;
59+ padding: 1em 0;
60+ color: ${(props) => props.theme.fg};
61+ background: ${(props) => props.theme.bg};
62+
63+ &,
64+ &:focus,
65+ &:focus-visible {
66+ outline: 0;
67+ border: none;
68+ border-right: 4px solid ${(props) => props.theme.hl};
69+ }
70+`;
71+const EditorPreview = styled.div`
72+ min-height: 100%;
73+`;
client/src/components/editor/placeholder.ts+16−0
@@ -0,0 +1,16 @@
1+export function randomIntFromInterval(min: number, max: number) {
2+ return Math.floor(Math.random() * (max - min + 1) + min);
3+}
4+
5+export default function placeholderLine() {
6+ const lines = [
7+ "Not bored yet, are you?",
8+ "This is called a 'A writer's block'...",
9+ "Do you also stare out the window while zoned out?",
10+ "I bet you read this and forgot what you meant to note down.",
11+ "They say you can use markdown here...",
12+ "Have you pressed CTRL+ENTER ?",
13+ ];
14+
15+ return lines[randomIntFromInterval(0, lines.length - 1)];
16+}