-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathcode-editor.tsx
More file actions
134 lines (123 loc) · 4.53 KB
/
Copy pathcode-editor.tsx
File metadata and controls
134 lines (123 loc) · 4.53 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
'use client';
import React, { useEffect, useInsertionEffect, useRef } from 'react';
import Editor from 'react-simple-code-editor';
import Prism from 'prismjs';
import 'prismjs/components/prism-markup';
import 'prismjs/components/prism-clike';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-css-extras';
import { cn } from '@/lib/utils';
interface CodeEditorProps {
value: string;
onValueChange?: (value: string) => void;
readOnly?: boolean;
placeholder?: string;
className?: string;
autoFocus?: boolean;
/** Exposes the internal textarea (e.g. to insert text at the cursor). */
textareaRef?: React.RefObject<HTMLTextAreaElement | null>;
}
/** Renders leading spaces of each line as middle dots for visible indentation. */
function showIndentDots(html: string): string {
return html.replace(/^ +/gm, (spaces) =>
`<span class="code-editor-indent">${'·'.repeat(spaces.length)}</span>`,
);
}
function highlightHtml(code: string): string {
return showIndentDots(Prism.highlight(code, Prism.languages.markup, 'markup'));
}
const STYLE_ID = 'code-editor-prism-theme';
const prismCss = `
.code-editor-root .token.tag { color: #e06c75; }
.code-editor-root .token.attr-name { color: #d19a66; }
.code-editor-root .token.attr-value { color: #98c379; }
.code-editor-root .token.punctuation { color: #abb2bf; }
.code-editor-root .token.comment { color: #5c6370; font-style: italic; }
.code-editor-root .token.entity { color: #d19a66; }
.code-editor-root .token.doctype { color: #5c6370; }
.code-editor-root .token.prolog { color: #5c6370; }
.code-editor-root .token.cdata { color: #5c6370; }
.code-editor-root .token.keyword { color: #c678dd; }
.code-editor-root .token.boolean,
.code-editor-root .token.number,
.code-editor-root .token.constant { color: #d19a66; }
.code-editor-root .token.string,
.code-editor-root .token.char,
.code-editor-root .token.attr-value .token.value { color: #98c379; }
.code-editor-root .token.function,
.code-editor-root .token.class-name { color: #61afef; }
.code-editor-root .token.operator { color: #56b6c2; }
.code-editor-root .token.property { color: #56b6c2; }
.code-editor-root .token.selector { color: #e5c07b; }
.code-editor-root .token.selector .token.class,
.code-editor-root .token.selector .token.id { color: #d19a66; }
.code-editor-root .token.selector .token.pseudo-class,
.code-editor-root .token.selector .token.pseudo-element,
.code-editor-root .token.selector .token.attribute { color: #56b6c2; }
.code-editor-root .token.selector .token.combinator { color: #abb2bf; }
.code-editor-root .token.builtin { color: #56b6c2; }
.code-editor-root .token.regex,
.code-editor-root .token.important { color: #d19a66; }
.code-editor-root .token.variable { color: #e06c75; }
.code-editor-root .token.parameter { color: #e06c75; }
.code-editor-root .token.function-variable { color: #61afef; }
.code-editor-root .code-editor-indent { color: #454b57; }
`;
let styleInjected = false;
function ensurePrismStyles() {
if (styleInjected) return;
if (typeof document === 'undefined') return;
if (document.getElementById(STYLE_ID)) { styleInjected = true; return; }
const style = document.createElement('style');
style.id = STYLE_ID;
style.textContent = prismCss;
document.head.appendChild(style);
styleInjected = true;
}
const noop = () => {};
export function CodeEditor({
value,
onValueChange,
readOnly = false,
placeholder = '',
className,
autoFocus = false,
textareaRef,
}: CodeEditorProps) {
useInsertionEffect(ensurePrismStyles, []);
const rootRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const textarea = rootRef.current?.querySelector('textarea') ?? null;
if (textarea) textarea.spellcheck = false;
if (textareaRef) textareaRef.current = textarea;
});
return (
<div
ref={rootRef}
className={cn(
'code-editor-root rounded-lg border border-transparent bg-input overflow-auto font-mono text-xs',
readOnly && 'cursor-default',
className,
)}
>
<Editor
value={value}
onValueChange={onValueChange ?? noop}
highlight={highlightHtml}
padding={12}
readOnly={readOnly}
placeholder={placeholder}
autoFocus={autoFocus}
textareaClassName="outline-none !min-h-[inherit]"
preClassName="!min-h-[inherit]"
style={{
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
fontSize: '11px',
lineHeight: '1.6',
minHeight: 'inherit',
}}
/>
</div>
);
}