-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathMarkdownEditorComponent.tsx
More file actions
236 lines (221 loc) · 8.14 KB
/
MarkdownEditorComponent.tsx
File metadata and controls
236 lines (221 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"use client";
import { useEffect, useState } from "react";
import FormattedMarkdownOutput from "@/app/components/common/FormattedMarkdownOutput";
import { User } from "@clerk/backend";
export enum EMarkdownStyles {
Select = "Select",
Badge = "Badge",
Blockquote = "Blockquote",
Code = "Code",
Emphasis = "Emphasis",
Emphasis_Italic = "Emphasis_Italic",
Header_1 = "Header_1",
Header_2 = "Header_2",
Header_3 = "Header_3",
Header_4 = "Header_4",
Header_5 = "Header_5",
Header_6 = "Header_6",
Image = "Image",
Italic = "Italic",
List = "List",
Subscript = "Subscript",
Superscript = "Superscript",
Table = "Table",
Unordered_List = "Unordered_List",
}
export const markdownStyles = [
{ label: "Select a style", value: EMarkdownStyles.Select },
{ label: "Badge", value: EMarkdownStyles.Badge },
{ label: "Blockquote", value: EMarkdownStyles.Blockquote },
{ label: "Code", value: EMarkdownStyles.Code },
{ label: "Emphasis", value: EMarkdownStyles.Emphasis },
{ label: "Emphasis Italic", value: EMarkdownStyles.Emphasis_Italic },
{ label: "Header 1", value: EMarkdownStyles.Header_1 },
{ label: "Header 2", value: EMarkdownStyles.Header_2 },
{ label: "Header 3", value: EMarkdownStyles.Header_3 },
{ label: "Header 4", value: EMarkdownStyles.Header_4 },
{ label: "Header 5", value: EMarkdownStyles.Header_5 },
{ label: "Header 6", value: EMarkdownStyles.Header_6 },
{ label: "Image", value: EMarkdownStyles.Image },
{ label: "Italic", value: EMarkdownStyles.Italic },
{ label: "List", value: EMarkdownStyles.List },
{ label: "Subscript", value: EMarkdownStyles.Subscript },
{ label: "Superscript", value: EMarkdownStyles.Superscript },
{ label: "Table", value: EMarkdownStyles.Table },
{ label: "Unordered List", value: EMarkdownStyles.Unordered_List },
];
const getStyleString = (style: EMarkdownStyles) => {
let styleString = "\n";
switch (style) {
case EMarkdownStyles.Header_1:
case EMarkdownStyles.Header_2:
case EMarkdownStyles.Header_3:
case EMarkdownStyles.Header_4:
case EMarkdownStyles.Header_5:
case EMarkdownStyles.Header_6:
const styleName = style.toString();
const fontSize = parseInt(styleName.slice(-1));
styleString += "#".repeat(fontSize) + " Header " + fontSize;
break;
case EMarkdownStyles.Badge:
styleString += `[](https://choosealicense.com/licenses/mit/)`;
break;
case EMarkdownStyles.Blockquote:
styleString += `> Blockquotes`;
break;
case EMarkdownStyles.Code:
styleString += ` \`\`\`\console.log('hello, world!')\`\`\`\ `;
break;
case EMarkdownStyles.Emphasis:
styleString += `**Emphasis**`;
break;
case EMarkdownStyles.Emphasis_Italic:
styleString += `***Emphasis Italic***`;
break;
case EMarkdownStyles.Image:
styleString += ``;
break;
case EMarkdownStyles.Italic:
styleString += `*Italic*`;
break;
case EMarkdownStyles.List:
styleString += `1. First ordered list item\n2. Second ordered list item\n3. Third ordered list item`;
break;
case EMarkdownStyles.Subscript:
styleString += `H<sub>2</sub>O`;
break;
case EMarkdownStyles.Superscript:
styleString += `N<sup>2</sup>`;
break;
case EMarkdownStyles.Table:
styleString += `| Feature | Description |
|---|---|
| Headings | Different levels of headings to organize content |
| Paragraphs | Basic text formatting and indentation |
| Lists | Ordered and unordered lists for structured content |
| Links | Internal and external links for navigation |
| Images | Embedding images to enhance visual appeal |
| Code blocks | Highlighting code snippets for programming examples |
| Tables | Presenting data in a structured format |
| Blockquotes | Quoting text or highlighting important points |
| Horizontal rules | Separating sections of content |
| Footnotes | Adding additional information or references |`;
break;
case EMarkdownStyles.Unordered_List:
styleString += `* First unordered list item\n* Second unordered list item\n\t* First subunordered list item\n\t* Second subunordered list item\n* Third unordered list item`;
break;
default:
styleString = "";
}
return styleString;
};
export default function JsonValidatorComponent({
user,
isProUser,
}: {
user: User | null;
isProUser: boolean;
}) {
const [output, setOutput] = useState("");
const [input, setInput] = useState(initialInput);
const [style, setStyle] = useState(EMarkdownStyles.Select);
useEffect(() => {
setOutput(input);
}, [input]);
const title = "Input:";
// const debouncedOutput = useDebounce(output, 1000);
// useEffect(() => {
// if (debouncedOutput && debouncedOutput !== initialInput) {
// void saveHistory({
// user,
// isProUser,
// toolType: ToolType.MarkdownEditor,
// onError: () => {},
// metadata: {
// output,
// },
// });
// }
// }, [debouncedOutput]);
const onSelect = (option: EMarkdownStyles) => {
const styleString = getStyleString(option);
setStyle(option);
setInput((prev) => prev + styleString);
setStyle(EMarkdownStyles.Select);
};
return (
<div className="w-full h-full flex gap-4">
<div className="w-full h-full">
<div className="flex items-center mb-4 gap-4">
<p className="font-bold text-xl"> {title} </p>
<button
type="button"
className="rounded-md bg-indigo-500 px-3.5 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500"
onClick={() => setInput("")}
>
Clear
</button>
<div className="flex gap-4 items-center">
<select
className={`block w-fit rounded-md border-0 py-1.5 pl-3 pr-10
text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2
focus:ring-indigo-600 sm:text-sm sm:leading-6`}
onChange={(e) => {
const selectedValue = markdownStyles.find(
(value) => value.label === e.target.value
);
if (selectedValue) {
onSelect(selectedValue.value);
}
}}
value={style}
>
{markdownStyles.map((value) => (
<option key={value.value}>{value.label}</option>
))}
</select>
</div>
</div>
<textarea
className="px-8 py-2 block w-full rounded-lg border-0
bg-gray-700 text-white shadow-sm ring-1 ring-inset
ring-gray-300 focus:ring-2 focus:ring-inset
focus:ring-indigo-600 sm:text-sm sm:leading-6"
style={{ height: "calc(100% - 44px)" }}
value={input}
onInput={(e) => setInput(e.currentTarget.value)}
/>
</div>
<FormattedMarkdownOutput input={output} />
</div>
);
}
const initialInput = `# Header
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6
*Italic* _Italic_
**Emphasis** __Emphasis__
***Emphasis Italic*** ___Emphasis Italic___
Subscript: X<sub>2</sub>,Superscript: O<sup>2</sup>
> Blockquotes
## Images

## Table
| Feature | Description |
|---|---|
| Headings | Different levels of headings to organize content |
| Paragraphs | Basic text formatting and indentation |
| Lists | Ordered and unordered lists for structured content |
| Links | Internal and external links for navigation |
| Images | Embedding images to enhance visual appeal |
| Code blocks | Highlighting code snippets for programming examples |
| Tables | Presenting data in a structured format |
| Blockquotes | Quoting text or highlighting important points |
| Horizontal rules | Separating sections of content |
| Footnotes | Adding additional information or references |
## Code Block
\`\`\`\console.log('hello, world!')\`\`\`\
`;