src/components/EditorLayout.jsx (view raw)
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
import {
lazy,
Suspense,
useCallback,
useDeferredValue,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { MODES } from '../lib/editorConstants.js';
import { parseMarkdownHeadings } from '../lib/markdownOutline.js';
import { parseOrgDocument } from '../lib/org/parseDocument.js';
import { buildPreviewHtml, ensureMarkedLoaded, ensureOrgLoaded } from '../lib/previewHtml.js';
import { STR } from '../lib/strings.js';
import DocumentOutline from './DocumentOutline.jsx';
const CodeEditor = lazy(() => import('./CodeEditor.jsx'));
const SPLIT_STORAGE_KEY = 'snow_editor_split';
const SPLIT_MIN = 0.25;
const SPLIT_MAX = 0.75;
function clampSplit(value) {
if (!Number.isFinite(value)) return 0.5;
return Math.min(SPLIT_MAX, Math.max(SPLIT_MIN, value));
}
function loadSplit() {
try {
const saved = window.localStorage.getItem(SPLIT_STORAGE_KEY);
if (saved !== null) return clampSplit(Number.parseFloat(saved));
} catch {
/* ignore */
}
return 0.5;
}
function persistSplit(value) {
try {
window.localStorage.setItem(SPLIT_STORAGE_KEY, String(value));
} catch {
/* ignore */
}
}
function useIsMobile() {
const [mobile, setMobile] = useState(() =>
typeof window !== 'undefined' ? window.matchMedia('(max-width: 768px)').matches : false,
);
useEffect(() => {
const media = window.matchMedia('(max-width: 768px)');
const update = () => setMobile(media.matches);
update();
media.addEventListener('change', update);
return () => media.removeEventListener('change', update);
}, []);
return mobile;
}
function useWideLayout() {
const [wide, setWide] = useState(() =>
typeof window !== 'undefined' ? window.matchMedia('(min-width: 900px)').matches : true,
);
useEffect(() => {
const media = window.matchMedia('(min-width: 900px)');
const update = () => setWide(media.matches);
update();
media.addEventListener('change', update);
return () => media.removeEventListener('change', update);
}, []);
return wide;
}
export function countWords(text) {
const trimmed = text.trim();
if (!trimmed) return 0;
return trimmed.split(/\s+/).filter(Boolean).length;
}
let hljsPromise = null;
function ensureHighlightLoaded() {
if (!hljsPromise) {
// The "common" build ships the ~40 popular languages only.
hljsPromise = import('highlight.js/lib/common').then((m) => m.default);
}
return hljsPromise;
}
export default function EditorLayout({
mode,
content,
onContentChange,
readOnly = false,
showEditor = true,
editorRef,
previewOnly = false,
}) {
const [markedReady, setMarkedReady] = useState(false);
const [orgReady, setOrgReady] = useState(false);
const [activeTab, setActiveTab] = useState('write');
const [split, setSplit] = useState(loadSplit);
const [dragging, setDragging] = useState(false);
const scrollToLineRef = useRef(null);
const previewScrollRef = useRef(null);
const previewContentRef = useRef(null);
const editorPanelRef = useRef(null);
const isMobile = useIsMobile();
const wideLayout = useWideLayout();
const deferredContent = useDeferredValue(content);
const previewIsStale = content !== deferredContent;
useEffect(() => {
if (mode !== MODES.MARKDOWN) return undefined;
let cancelled = false;
ensureMarkedLoaded().then(() => {
if (!cancelled) setMarkedReady(true);
});
return () => {
cancelled = true;
};
}, [mode]);
useEffect(() => {
if (mode !== MODES.ORG) return undefined;
let cancelled = false;
ensureOrgLoaded().then(() => {
if (!cancelled) setOrgReady(true);
});
return () => {
cancelled = true;
};
}, [mode]);
const orgMeta = useMemo(() => {
if (mode !== MODES.ORG) return null;
return parseOrgDocument(deferredContent);
}, [mode, deferredContent]);
const headings = useMemo(() => {
if (mode === MODES.ORG) return orgMeta?.headings ?? [];
return parseMarkdownHeadings(deferredContent);
}, [mode, orgMeta, deferredContent]);
const html = useMemo(() => {
return buildPreviewHtml(mode, deferredContent);
}, [mode, deferredContent, markedReady, orgReady]);
// Lazy syntax highlighting for fenced/SRC code blocks in the preview.
useEffect(() => {
const container = previewContentRef.current;
if (!container || !html || !html.includes('<pre')) return undefined;
let cancelled = false;
ensureHighlightLoaded().then((hljs) => {
if (cancelled || !previewContentRef.current) return;
const blocks = previewContentRef.current.querySelectorAll('pre code, pre.org-src');
blocks.forEach((el) => {
if (el.tagName === 'PRE' && el.querySelector('code')) return;
if (el.dataset.highlighted === 'yes') return;
hljs.highlightElement(el);
});
});
return () => {
cancelled = true;
};
}, [html]);
const handleRegisterScroll = useCallback((scrollFn) => {
scrollToLineRef.current = scrollFn;
}, []);
const handleOutlineSelect = useCallback((line) => {
scrollToLineRef.current?.(line);
}, []);
// Editor → preview proportional scroll sync.
const handleEditorScrollRatio = useCallback((ratio) => {
const preview = previewScrollRef.current;
if (!preview) return;
const max = preview.scrollHeight - preview.clientHeight;
if (max > 0) {
preview.scrollTop = ratio * max;
}
}, []);
// Draggable splitter (desktop only).
const handleDividerPointerDown = useCallback(
(event) => {
if (isMobile) return;
event.preventDefault();
setDragging(true);
const editorPanel = editorPanelRef.current;
const previewPanel = previewScrollRef.current?.closest('.panel-preview');
if (!editorPanel || !previewPanel) return;
const onMove = (moveEvent) => {
const left = editorPanel.getBoundingClientRect().left;
const right = previewPanel.getBoundingClientRect().right;
if (right - left <= 0) return;
setSplit(clampSplit((moveEvent.clientX - left) / (right - left)));
};
const onUp = () => {
setDragging(false);
setSplit((value) => {
persistSplit(value);
return value;
});
window.removeEventListener('pointermove', onMove);
window.removeEventListener('pointerup', onUp);
};
window.addEventListener('pointermove', onMove);
window.addEventListener('pointerup', onUp);
},
[isMobile],
);
const handleDividerReset = useCallback(() => {
setSplit(0.5);
persistSplit(0.5);
}, []);
const wordCount = useMemo(() => countWords(content), [content]);
const charCount = content.length;
const editorAriaLabel =
mode === MODES.ORG ? 'Org-mode editing area' : 'Markdown editing area';
const splitLayout = showEditor && !previewOnly;
const useTabs = isMobile && splitLayout;
const showWrite = splitLayout && (!useTabs || activeTab === 'write');
const showRead = previewOnly || !useTabs || activeTab === 'read';
const showOutline =
splitLayout && !useTabs && wideLayout && headings.length > 0;
const rendererReady = mode === MODES.ORG ? orgReady : markedReady;
const contentEmpty = !deferredContent || !deferredContent.trim();
const gridStyle =
splitLayout && !useTabs
? {
gridTemplateColumns: showOutline
? `10.5rem ${split}fr 2rem ${1 - split}fr`
: `${split}fr 2rem ${1 - split}fr`,
}
: undefined;
return (
<>
{useTabs && (
<div className="mobile-tabs" role="tablist" aria-label="Editor view">
<button
type="button"
role="tab"
aria-selected={activeTab === 'write'}
className={`mobile-tabs__btn${activeTab === 'write' ? ' is-active' : ''}`}
onClick={() => setActiveTab('write')}
>
{STR.TAB_WRITE}
</button>
<button
type="button"
role="tab"
aria-selected={activeTab === 'read'}
className={`mobile-tabs__btn${activeTab === 'read' ? ' is-active' : ''}`}
onClick={() => setActiveTab('read')}
>
{STR.TAB_READ}
</button>
</div>
)}
<main
className={`app-layout${previewOnly ? ' app-layout--preview-only' : ''}${showOutline ? ' app-layout--with-outline' : ''}${useTabs ? ' app-layout--tabs' : ''}${dragging ? ' app-layout--dragging' : ''}`}
style={gridStyle}
>
{showOutline && (
<DocumentOutline headings={headings} onSelectHeading={handleOutlineSelect} />
)}
{showWrite && (
<>
<section
ref={editorPanelRef}
className="panel panel-editor"
aria-label={mode === MODES.ORG ? 'Org-mode editor' : 'Markdown editor'}
>
<div className="panel-label">{STR.TAB_WRITE}</div>
<Suspense
fallback={<div className="editor editor--loading">Loading editor…</div>}
>
<CodeEditor
mode={mode}
value={content}
onChange={onContentChange}
readOnly={readOnly}
editorRef={editorRef}
ariaLabel={editorAriaLabel}
placeholderText="Start writing..."
onRegisterScroll={handleRegisterScroll}
onScrollRatio={handleEditorScrollRatio}
/>
</Suspense>
</section>
{!useTabs && (
<div
className="layout-divider"
role="separator"
aria-orientation="vertical"
aria-label={STR.RESIZE_PANELS}
title={STR.RESIZE_PANELS}
onPointerDown={handleDividerPointerDown}
onDoubleClick={handleDividerReset}
/>
)}
</>
)}
{showRead && (
<section
className={`panel panel-preview${previewOnly ? ' panel-preview--full' : ''}`}
aria-label="Document preview"
>
<div className="panel-label">{STR.TAB_READ}</div>
{orgMeta?.title && <p className="org-doc-title">{orgMeta.title}</p>}
<div
ref={previewScrollRef}
className={`preview-paper${previewIsStale ? ' preview-updating' : ''}`}
>
{contentEmpty ? (
<p className="preview-empty">{STR.PREVIEW_EMPTY}</p>
) : !rendererReady && !html ? (
<p className="preview-empty">{STR.PREVIEW_RENDERING}</p>
) : (
<div
ref={previewContentRef}
className="preview-content"
dangerouslySetInnerHTML={{ __html: html }}
/>
)}
</div>
</section>
)}
</main>
<div className="app-footer-stats app-footer-stats--inline">
<span>
{wordCount} {wordCount === 1 ? 'word' : 'words'}
</span>
<span className="footer-separator">·</span>
<span>
{charCount} {charCount === 1 ? 'character' : 'characters'}
</span>
</div>
</>
);
}
|