first commit

This commit is contained in:
dahyeon committed 2025-11-07 02:50:20 +09:00
commit 120f150ee3
40 files changed
+7839

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
import { useEffect } from 'react';
import { LexicalComposer } from '@lexical/react/LexicalComposer';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
import { ListNode, ListItemNode } from '@lexical/list';
import { CodeNode, CodeHighlightNode } from '@lexical/code';
import { LinkNode, AutoLinkNode } from '@lexical/link';
import { TableNode, TableCellNode, TableRowNode } from '@lexical/table';
import { $getRoot, $createParagraphNode, $createTextNode } from 'lexical';
const viewerTheme = {
paragraph: 'mb-4 leading-relaxed text-[#e4e4e4]',
heading: {
h1: 'text-3xl font-bold mb-4 mt-6 text-[#e4e4e4] scroll-mt-20',
h2: 'text-2xl font-bold mb-3 mt-5 text-[#e4e4e4] scroll-mt-20',
h3: 'text-xl font-semibold mb-2 mt-4 text-[#e4e4e4] scroll-mt-20',
},
list: {
ul: 'list-disc ml-6 mb-4 text-[#e4e4e4]',
ol: 'list-decimal ml-6 mb-4 text-[#e4e4e4]',
listitem: 'mb-1',
},
text: {
bold: 'font-bold',
italic: 'italic',
underline: 'underline',
code: 'bg-[#1e1e1e] px-1.5 py-0.5 rounded font-mono text-sm text-[#4db8ff]',
},
code: 'bg-[#1e1e1e] p-4 rounded font-mono text-sm text-[#e4e4e4] mb-4 block overflow-x-auto',
link: 'text-[#4db8ff] hover:text-[#8cd7ff] underline cursor-pointer',
table: 'border-collapse table-auto w-full my-4',
tableCell: 'border border-[#3e3e42] px-3 py-2 text-[#e4e4e4] min-w-[100px]',
tableCellHeader: 'border border-[#3e3e42] px-3 py-2 text-[#e4e4e4] font-bold bg-[#2d2d30]',
};
interface LexicalViewerProps {
content: string;
}
function HeadingIdPlugin() {
const [editor] = useLexicalComposerContext();
useEffect(() => {
// Add data attribute and ID to heading elements for TOC
const updateHeadings = () => {
const headings = document.querySelectorAll('.lexical-viewer h1, .lexical-viewer h2, .lexical-viewer h3');
headings.forEach((heading, index) => {
if (heading instanceof HTMLElement) {
heading.setAttribute('data-lexical-heading', 'true');
if (!heading.id) {
const text = heading.textContent || '';
heading.id = `heading-${index}-${text.toLowerCase().replace(/\s+/g, '-').slice(0, 30)}`;
}
}
});
};
// Initial update with delay
setTimeout(updateHeadings, 100);
// Also register mutation listener
const unregister = editor.registerMutationListener(HeadingNode, () => {
setTimeout(updateHeadings, 50);
});
// Update periodically as backup
const interval = setInterval(updateHeadings, 1000);
return () => {
unregister();
clearInterval(interval);
};
}, [editor]);
return null;
}
function ContentPlugin({ content }: { content: string }) {
const [editor] = useLexicalComposerContext();
useEffect(() => {
try {
// Try to parse as JSON first (new format with formatting)
const editorState = editor.parseEditorState(content);
editor.setEditorState(editorState);
} catch {
// Fallback to plain text (legacy format)
editor.update(() => {
const root = $getRoot();
root.clear();
const lines = content.split('\n');
lines.forEach((line) => {
const paragraph = $createParagraphNode();
if (line.trim() === '') {
// Empty line for spacing
paragraph.append($createTextNode(''));
} else {
const text = $createTextNode(line);
paragraph.append(text);
}
root.append(paragraph);
});
});
}
// Make editor read-only
editor.setEditable(false);
}, [editor, content]);
return null;
}
function onError(error: Error) {
console.error('Lexical Viewer Error:', error);
}
export function LexicalViewer({ content }: LexicalViewerProps) {
const initialConfig = {
namespace: 'WikiViewer',
theme: viewerTheme,
onError,
editable: false,
nodes: [
HeadingNode,
ListNode,
ListItemNode,
QuoteNode,
CodeNode,
CodeHighlightNode,
LinkNode,
AutoLinkNode,
TableNode,
TableCellNode,
TableRowNode,
],
};
return (
<div className="prose prose-invert max-w-none">
<LexicalComposer initialConfig={initialConfig}>
<RichTextPlugin
contentEditable={
<ContentEditable
className="text-[#e4e4e4] text-base focus:outline-none lexical-viewer"
/>
}
placeholder={null}
ErrorBoundary={LexicalErrorBoundary}
/>
<ContentPlugin content={content} />
<HeadingIdPlugin />
</LexicalComposer>
</div>
);
}