-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeavyMarkdownEditor.jsx
More file actions
172 lines (153 loc) · 5.86 KB
/
HeavyMarkdownEditor.jsx
File metadata and controls
172 lines (153 loc) · 5.86 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
"use client";
import React, { useState, useEffect } from 'react';
import MarkdownViewer from '../../MarkdownViewer/ror_components/MarkdownViewer';
import * as style from './HeavyMarkdownEditor.module.css';
const HeavyMarkdownEditor = (props) => {
const [markdown, setMarkdown] = useState(props.initialText || '# Start editing markdown here...');
const [isLoaded, setIsLoaded] = useState(false);
const [processedHtml, setProcessedHtml] = useState('');
useEffect(() => {
const loadMarkdown = async () => {
try {
const [{ default: ReactMarkdown }, { default: remarkGfm }, { renderToString }] = await Promise.all([
import('react-markdown'),
import('remark-gfm'),
import('react-dom/server')
]);
// Convert markdown to HTML using react-markdown, then use shared MarkdownViewer
const htmlString = renderToString(
React.createElement(ReactMarkdown, { remarkPlugins: [remarkGfm] }, markdown)
);
setProcessedHtml(htmlString);
setIsLoaded(true);
} catch (error) {
console.warn('Failed to load markdown components:', error);
setIsLoaded(true); // Show fallback
}
};
loadMarkdown();
}, [markdown]);
// Skeleton loader component that fills the preview space properly
const SkeletonLoader = () => (
<div className={`${style.skeleton} ${style.contentTransition} ${style.fadeIn}`}>
{/* Title */}
<div className={style.skeletonTitle}></div>
{/* First section of content */}
<div className={style.skeletonParagraph}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonMedium}`}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonShort}`}></div>
{/* Section title */}
<div className={`${style.skeletonTitle} ${style.skeletonMedium}`} style={{marginTop: '1.5em'}}></div>
{/* List items */}
<div className={style.skeletonParagraph}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonShort}`}></div>
<div className={style.skeletonParagraph}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonMedium}`}></div>
{/* Code block */}
<div className={style.skeletonCode}></div>
{/* More content */}
<div className={style.skeletonParagraph}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonShort}`}></div>
{/* Table */}
<div className={style.skeletonTable}></div>
{/* Final section */}
<div className={style.skeletonParagraph}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonMedium}`}></div>
<div className={`${style.skeletonParagraph} ${style.skeletonShort}`}></div>
<div className={style.skeletonParagraph}></div>
</div>
);
return (
<div
className={style.container}
style={{
width: '1200px',
maxWidth: 'calc(100vw - 2rem)',
margin: '0 auto',
minHeight: '600px' // Reserve space immediately
}}
>
<div className={style.header}>
<h2 className={style.title}>Heavy Markdown Editor</h2>
<p className={style.subtitle}>
Demonstrates bundle splitting - this component adds 1.1MB of markdown libraries!
</p>
</div>
<div
className={style.editorContainer}
style={{
display: 'grid',
gridTemplateColumns: '1fr 1fr',
gap: '1rem',
minHeight: '450px'
}}
>
<div className={style.inputPanel}>
<h3 className={style.panelTitle}>📝 Edit Markdown</h3>
<textarea
className={style.textarea}
style={{
width: '100%',
maxWidth: '100%',
boxSizing: 'border-box'
}}
value={markdown}
onChange={(e) => setMarkdown(e.target.value)}
placeholder="Enter your markdown here..."
/>
</div>
<div
className={style.previewPanel}
style={{
minWidth: '0', // Prevent grid item from growing beyond 1fr
width: '100%'
}}
>
<h3 className={style.panelTitle}>👁️ Live Preview</h3>
<div
className={style.preview}
style={{
height: '400px',
width: '100%',
boxSizing: 'border-box'
}}
>
{processedHtml ? (
<div className={`${style.contentTransition} ${style.fadeIn}`}>
<MarkdownViewer processedHtml={processedHtml} />
</div>
) : isLoaded ? (
<div className={`${style.contentTransition} ${style.fadeIn}`}>
<div className={style.fallback}>
<h2>Markdown Preview</h2>
<pre>{markdown}</pre>
</div>
</div>
) : (
<SkeletonLoader />
)}
</div>
</div>
</div>
<div className={style.navigation}>
<a href="/hello_world" className={style.link}>
← Back to Lightweight HelloWorld
</a>
<a href="/rsc_markdown_page" className={style.link}>
→ Try RSC Markdown Page
</a>
<div className={style.bundleInfo}>
<strong>Bundle Impact:</strong> Heavy component with markdown libraries (~120KB transferred, 385KB resources in production)
</div>
{props.title && (
<div className={style.bundleInfo} style={{marginTop: '0.5rem', fontSize: '0.85rem'}}>
<strong>Content:</strong> {props.title}
{props.author && <> by {props.author}</>}
{props.lastModified && <> (updated {new Date(props.lastModified).toLocaleDateString()})</>}
</div>
)}
</div>
</div>
);
};
export default HeavyMarkdownEditor;