-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.tsx
More file actions
143 lines (127 loc) · 4.12 KB
/
Copy pathTextEditor.tsx
File metadata and controls
143 lines (127 loc) · 4.12 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
import { useEffect, useMemo, useRef, useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
import { uploadFile } from '../hooks/usePresignedUrlHook';
import { useFunnelState } from '../model/FunnelContext';
interface TextEditorProps {
onValidationChange?: (isValid: boolean) => void;
}
const MAX_LENGTH = 2000;
const IMAGE_WEIGHT = 200;
const formats = [
'font',
'header',
'bold',
'italic',
'underline',
'strike',
'blockquote',
'list',
'bullet',
'indent',
'link',
'image',
'align',
'color',
'background',
'size',
'h1',
];
const TextEditor = ({ onValidationChange }: TextEditorProps) => {
const { eventState, setEventState } = useFunnelState();
const quillRef = useRef<ReactQuill | null>(null);
const [isOverLimit, setIsOverLimit] = useState(false);
const imageHandler = async () => {
if (!quillRef.current) return;
const quillInstance = quillRef.current.getEditor();
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.click();
input.onchange = async () => {
const file = input.files?.[0];
if (!file) return;
try {
const imageUrl = await uploadFile(file);
const range = quillInstance.getSelection();
if (range) {
quillInstance.insertEmbed(range.index, 'image', imageUrl);
}
} catch (error) {
console.error('이미지 업로드 실패:', error);
}
};
};
const getImageCount = (htmlContent: string): number => {
const matches = htmlContent.match(/<img [^>]*src="[^"]*"[^>]*>/g);
return matches ? matches.length : 0;
};
const getPlainText = (htmlContent: string): string => {
return htmlContent.replace(/<[^>]*>/g, '').trim();
};
const getTotalContentLength = (htmlContent: string): number => {
const textLength = getPlainText(htmlContent).length;
const imageCount = getImageCount(htmlContent);
return textLength + imageCount * IMAGE_WEIGHT;
};
const handleChange = (value: string) => {
const totalLength = getTotalContentLength(value);
if (totalLength <= MAX_LENGTH) {
setEventState?.(prev => ({ ...prev, description: value }));
onValidationChange?.(getPlainText(value).length > 0);
setIsOverLimit(false);
} else {
const editorInstance = quillRef.current?.getEditor();
if (editorInstance) {
editorInstance.setContents(editorInstance.clipboard.convert(eventState.description));
}
setIsOverLimit(true);
}
};
useEffect(() => {
onValidationChange?.(getPlainText(eventState.description).length > 0);
}, [eventState.description, onValidationChange]);
const modules = useMemo(
() => ({
toolbar: {
container: [
[{ font: [] }],
[{ header: [1, 2, 3, 4, 5, 6, false] }],
['bold', 'italic', 'underline', 'strike'],
[{ color: [] }, { background: [] }],
[{ list: 'ordered' }, { list: 'bullet' }],
[{ align: [] }],
['link', 'image'],
['clean'],
],
handlers: {
image: imageHandler,
},
},
}),
[]
);
const totalLength = getTotalContentLength(eventState.description);
const imageCount = getImageCount(eventState.description);
return (
<div className="flex flex-col justify-start gap-2 mb-4">
<h1 className="font-bold text-black text-lg">이벤트에 대한 상세 설명</h1>
<ReactQuill
theme="snow"
value={eventState.description}
ref={quillRef}
modules={modules}
formats={formats}
onChange={handleChange}
className="custom-quill-editor"
/>
<div className="flex justify-between items-center mt-1">
<p className={`text-sm ${isOverLimit ? 'text-red-500' : 'text-gray-500'}`}>
{totalLength} / {MAX_LENGTH}자{imageCount > 0 && ` (이미지 ${imageCount}개 포함)`}
</p>
{isOverLimit && <p className="text-sm text-red-500 font-medium">{MAX_LENGTH}자를 초과할 수 없습니다.</p>}
</div>
</div>
);
};
export default TextEditor;