- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
903 lines
36 KiB
TypeScript
903 lines
36 KiB
TypeScript
import {
|
|
AlignCenter,
|
|
AlignLeft,
|
|
AlignRight,
|
|
Bold,
|
|
Code,
|
|
Heading1,
|
|
Heading2,
|
|
Heading3,
|
|
Heading4,
|
|
Image as ImageIcon,
|
|
Italic,
|
|
Link as LinkIcon,
|
|
List,
|
|
ListOrdered,
|
|
Maximize2,
|
|
Minimize2,
|
|
Minus,
|
|
Music,
|
|
PaintBucket,
|
|
Palette,
|
|
Quote,
|
|
Redo,
|
|
RemoveFormatting,
|
|
Table as TableIcon,
|
|
Underline,
|
|
Undo,
|
|
Video,
|
|
} from 'lucide-react';
|
|
import { useCallback,useEffect,useRef,useState } from 'react';
|
|
import MediaSelector from './MediaSelector';
|
|
|
|
interface RichTextEditorProps {
|
|
value: string;
|
|
onChange: (html: string) => void;
|
|
placeholder?: string;
|
|
className?: string;
|
|
minHeight?: string;
|
|
}
|
|
|
|
const PRESET_COLORS = [
|
|
{ label: 'سیاه / پیشفرض', value: '#111827' },
|
|
{ label: 'خاکستری', value: '#4b5563' },
|
|
{ label: 'آبی کنینا', value: '#0052cc' },
|
|
{ label: 'بنفش اختصاصی', value: '#7c3aed' },
|
|
{ label: 'سبز درمانی', value: '#059669' },
|
|
{ label: 'قرمز هشدار', value: '#dc2626' },
|
|
{ label: 'کهربایی / زرد', value: '#d97706' },
|
|
];
|
|
|
|
const PRESET_BG_COLORS = [
|
|
{ label: 'بیرنگ / حذف پسزمینه', value: 'transparent' },
|
|
{ label: 'هایلایت زرد', value: '#fef08a' },
|
|
{ label: 'هایلایت آبی', value: '#dbeafe' },
|
|
{ label: 'هایلایت بنفش', value: '#f3e8ff' },
|
|
{ label: 'هایلایت سبز', value: '#dcfce7' },
|
|
{ label: 'هایلایت قرمز', value: '#fee2e2' },
|
|
{ label: 'خاکستری ملایم', value: '#f3f4f6' },
|
|
];
|
|
|
|
interface ActiveStates {
|
|
isBold: boolean;
|
|
isItalic: boolean;
|
|
isUnderline: boolean;
|
|
isH1: boolean;
|
|
isH2: boolean;
|
|
isH3: boolean;
|
|
isH4: boolean;
|
|
isP: boolean;
|
|
isUL: boolean;
|
|
isOL: boolean;
|
|
isBlockquote: boolean;
|
|
align: 'right' | 'center' | 'left' | 'justify';
|
|
}
|
|
|
|
export default function RichTextEditor({
|
|
value,
|
|
onChange,
|
|
placeholder = 'محتوای مقاله یا متن خود را اینجا بنویسید...',
|
|
className = '',
|
|
minHeight = 'min-h-[300px]',
|
|
}: RichTextEditorProps) {
|
|
const editorRef = useRef<HTMLDivElement>(null);
|
|
const isTypingRef = useRef(false);
|
|
const isMountedRef = useRef(false);
|
|
const [isCodeMode, setIsCodeMode] = useState(false);
|
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
const [showColorPicker, setShowColorPicker] = useState(false);
|
|
const [showBgColorPicker, setShowBgColorPicker] = useState(false);
|
|
const colorPickerRef = useRef<HTMLDivElement>(null);
|
|
const bgColorPickerRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
if (colorPickerRef.current && !colorPickerRef.current.contains(event.target as Node)) {
|
|
setShowColorPicker(false);
|
|
}
|
|
if (bgColorPickerRef.current && !bgColorPickerRef.current.contains(event.target as Node)) {
|
|
setShowBgColorPicker(false);
|
|
}
|
|
};
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
return () => document.removeEventListener('mousedown', handleClickOutside);
|
|
}, []);
|
|
|
|
const [customColor, setCustomColor] = useState('#111827');
|
|
const [customBgColor, setCustomBgColor] = useState('#fef08a');
|
|
|
|
// Media selector modal state
|
|
const [mediaModalOpen, setMediaModalOpen] = useState(false);
|
|
const [mediaTargetType, setMediaTargetType] = useState<'image' | 'video' | 'audio'>('image');
|
|
const savedSelectionRef = useRef<Range | null>(null);
|
|
|
|
// Inline Image Alt Modal
|
|
const [showImagePrompt, setShowImagePrompt] = useState(false);
|
|
const [pendingImageUrl, setPendingImageUrl] = useState('');
|
|
const [imageAlt, setImageAlt] = useState('');
|
|
const [imageCaption, setImageCaption] = useState('');
|
|
|
|
const [activeStates, setActiveStates] = useState<ActiveStates>({
|
|
isBold: false,
|
|
isItalic: false,
|
|
isUnderline: false,
|
|
isH1: false,
|
|
isH2: false,
|
|
isH3: false,
|
|
isH4: false,
|
|
isP: false,
|
|
isUL: false,
|
|
isOL: false,
|
|
isBlockquote: false,
|
|
align: 'right',
|
|
});
|
|
|
|
// Save selection before opening modal
|
|
const saveSelection = () => {
|
|
const sel = window.getSelection();
|
|
if (sel && sel.rangeCount > 0) {
|
|
savedSelectionRef.current = sel.getRangeAt(0).cloneRange();
|
|
}
|
|
};
|
|
|
|
const restoreSelection = () => {
|
|
const sel = window.getSelection();
|
|
if (sel && savedSelectionRef.current) {
|
|
sel.removeAllRanges();
|
|
sel.addRange(savedSelectionRef.current);
|
|
}
|
|
};
|
|
|
|
// Query formatting states on cursor/selection change
|
|
const updateActiveStates = useCallback(() => {
|
|
if (!editorRef.current || isCodeMode) return;
|
|
try {
|
|
const doc = document as any;
|
|
const isBold = doc.queryCommandState ? doc.queryCommandState('bold') : false;
|
|
const isItalic = doc.queryCommandState ? doc.queryCommandState('italic') : false;
|
|
const isUnderline = doc.queryCommandState ? doc.queryCommandState('underline') : false;
|
|
const isUL = doc.queryCommandState ? doc.queryCommandState('insertUnorderedList') : false;
|
|
const isOL = doc.queryCommandState ? doc.queryCommandState('insertOrderedList') : false;
|
|
|
|
let block = (doc.queryCommandValue ? doc.queryCommandValue('formatBlock') : '') || '';
|
|
block = block.toLowerCase().replace(/[<>]/g, '');
|
|
|
|
// Check parent tag
|
|
const selection = window.getSelection();
|
|
let parentTag = '';
|
|
if (selection && selection.rangeCount > 0) {
|
|
let node: Node | null = selection.getRangeAt(0).commonAncestorContainer;
|
|
if (node && node.nodeType === 3 && node.parentNode) {
|
|
node = node.parentNode;
|
|
}
|
|
while (node && node !== editorRef.current) {
|
|
const tag = (node as HTMLElement).tagName?.toLowerCase();
|
|
if (['h1', 'h2', 'h3', 'h4', 'p', 'ul', 'ol', 'blockquote'].includes(tag)) {
|
|
parentTag = tag;
|
|
break;
|
|
}
|
|
node = node.parentNode;
|
|
}
|
|
}
|
|
|
|
const activeBlock = parentTag || block;
|
|
|
|
setActiveStates({
|
|
isBold,
|
|
isItalic,
|
|
isUnderline,
|
|
isH1: activeBlock === 'h1',
|
|
isH2: activeBlock === 'h2',
|
|
isH3: activeBlock === 'h3',
|
|
isH4: activeBlock === 'h4',
|
|
isBlockquote: activeBlock === 'blockquote',
|
|
isP: activeBlock === 'p' || (!['h1', 'h2', 'h3', 'h4', 'blockquote'].includes(activeBlock) && !isUL && !isOL),
|
|
isUL,
|
|
isOL,
|
|
align: doc.queryCommandState && doc.queryCommandState('justifyCenter')
|
|
? 'center'
|
|
: doc.queryCommandState && doc.queryCommandState('justifyLeft')
|
|
? 'left'
|
|
: 'right',
|
|
});
|
|
} catch {
|
|
// Ignored
|
|
}
|
|
}, [isCodeMode]);
|
|
|
|
// Sync value from props on initial mount or when external change occurs
|
|
useEffect(() => {
|
|
if (!editorRef.current) return;
|
|
if (!isMountedRef.current) {
|
|
editorRef.current.innerHTML = value || '';
|
|
isMountedRef.current = true;
|
|
return;
|
|
}
|
|
|
|
if (editorRef.current.innerHTML !== (value || '')) {
|
|
if (!isTypingRef.current && document.activeElement !== editorRef.current) {
|
|
editorRef.current.innerHTML = value || '';
|
|
}
|
|
}
|
|
}, [value, isCodeMode]);
|
|
|
|
const exec = (command: string, val: string | undefined = undefined) => {
|
|
if (isCodeMode) return;
|
|
editorRef.current?.focus();
|
|
document.execCommand(command, false, val);
|
|
if (editorRef.current) {
|
|
onChange(editorRef.current.innerHTML);
|
|
}
|
|
setTimeout(updateActiveStates, 20);
|
|
};
|
|
|
|
const setBlockTag = (tagName: 'h1' | 'h2' | 'h3' | 'h4' | 'p' | 'blockquote') => {
|
|
if (isCodeMode) return;
|
|
editorRef.current?.focus();
|
|
const blockValue = `<${tagName}>`;
|
|
const success = document.execCommand('formatBlock', false, blockValue);
|
|
if (!success) {
|
|
document.execCommand('formatBlock', false, tagName);
|
|
}
|
|
if (editorRef.current) {
|
|
onChange(editorRef.current.innerHTML);
|
|
}
|
|
setTimeout(updateActiveStates, 20);
|
|
};
|
|
|
|
const handleLink = () => {
|
|
if (isCodeMode) return;
|
|
const url = prompt('آدرس لینک (URL) را وارد کنید:', 'https://');
|
|
if (url) {
|
|
exec('createLink', url);
|
|
}
|
|
};
|
|
|
|
const handleInsertHorizontalRule = () => {
|
|
if (isCodeMode) return;
|
|
exec('insertHorizontalRule');
|
|
};
|
|
|
|
const handleInsertTable = () => {
|
|
if (isCodeMode) return;
|
|
const rows = 3;
|
|
const cols = 3;
|
|
let tableHtml = '<table class="w-full my-4 border-collapse border border-gray-300 rounded-lg overflow-hidden text-sm" dir="rtl"><thead><tr class="bg-gray-100 font-bold">';
|
|
for (let c = 0; c < cols; c++) {
|
|
tableHtml += `<th class="border border-gray-300 p-2 text-right">عنوان ${c + 1}</th>`;
|
|
}
|
|
tableHtml += '</tr></thead><tbody>';
|
|
for (let r = 0; r < rows; r++) {
|
|
tableHtml += '<tr class="hover:bg-gray-50">';
|
|
for (let c = 0; c < cols; c++) {
|
|
tableHtml += '<td class="border border-gray-300 p-2 text-right">داده</td>';
|
|
}
|
|
tableHtml += '</tr>';
|
|
}
|
|
tableHtml += '</tbody></table><p><br></p>';
|
|
|
|
editorRef.current?.focus();
|
|
document.execCommand('insertHTML', false, tableHtml);
|
|
if (editorRef.current) {
|
|
onChange(editorRef.current.innerHTML);
|
|
}
|
|
};
|
|
|
|
const openMediaModal = (type: 'image' | 'video' | 'audio') => {
|
|
saveSelection();
|
|
setMediaTargetType(type);
|
|
setMediaModalOpen(true);
|
|
};
|
|
|
|
const handleMediaSelect = (url: string) => {
|
|
setMediaModalOpen(false);
|
|
restoreSelection();
|
|
|
|
if (mediaTargetType === 'image') {
|
|
setPendingImageUrl(url);
|
|
setImageAlt('');
|
|
setImageCaption('');
|
|
setShowImagePrompt(true);
|
|
} else if (mediaTargetType === 'video') {
|
|
const videoHtml = `
|
|
<div class="my-6 rounded-2xl overflow-hidden shadow-lg border border-gray-200 bg-black/90 p-1">
|
|
<video controls class="w-full rounded-xl max-h-[450px]" src="${url}" preload="metadata">
|
|
مرورگر شما از تگ ویدئو پشتیبانی نمیکند.
|
|
</video>
|
|
</div>
|
|
<p><br></p>
|
|
`;
|
|
editorRef.current?.focus();
|
|
document.execCommand('insertHTML', false, videoHtml);
|
|
if (editorRef.current) onChange(editorRef.current.innerHTML);
|
|
} else if (mediaTargetType === 'audio') {
|
|
const audioHtml = `
|
|
<div class="my-4 p-4 rounded-2xl bg-amber-50/70 border border-amber-200 shadow-xs flex flex-col gap-2">
|
|
<span class="text-xs font-bold text-amber-800 flex items-center gap-1.5 font-vazir">
|
|
🎵 فایل صوتی / پادکست
|
|
</span>
|
|
<audio controls class="w-full" src="${url}">
|
|
مرورگر شما از پخش صوت پشتیبانی نمیکند.
|
|
</audio>
|
|
</div>
|
|
<p><br></p>
|
|
`;
|
|
editorRef.current?.focus();
|
|
document.execCommand('insertHTML', false, audioHtml);
|
|
if (editorRef.current) onChange(editorRef.current.innerHTML);
|
|
}
|
|
};
|
|
|
|
const confirmInsertImage = () => {
|
|
if (!pendingImageUrl) return;
|
|
const figureHtml = `
|
|
<figure class="my-6 text-center">
|
|
<img src="${pendingImageUrl}" alt="${imageAlt.replace(/"/g, '"')}" class="rounded-2xl max-w-full mx-auto shadow-md border border-gray-100 object-cover" />
|
|
${imageCaption ? `<figcaption class="text-xs text-gray-500 font-medium mt-2 italic font-vazir">${imageCaption}</figcaption>` : ''}
|
|
</figure>
|
|
<p><br></p>
|
|
`;
|
|
editorRef.current?.focus();
|
|
document.execCommand('insertHTML', false, figureHtml);
|
|
if (editorRef.current) onChange(editorRef.current.innerHTML);
|
|
setShowImagePrompt(false);
|
|
setPendingImageUrl('');
|
|
};
|
|
|
|
const handleInput = () => {
|
|
if (editorRef.current) {
|
|
isTypingRef.current = true;
|
|
onChange(editorRef.current.innerHTML);
|
|
setTimeout(() => {
|
|
isTypingRef.current = false;
|
|
}, 50);
|
|
updateActiveStates();
|
|
}
|
|
};
|
|
|
|
const clearFormatting = () => {
|
|
exec('removeFormat');
|
|
setBlockTag('p');
|
|
};
|
|
|
|
return (
|
|
<div
|
|
className={`border border-gray-200 rounded-2xl bg-white overflow-hidden shadow-xs relative transition-all ${
|
|
isFullscreen ? 'fixed inset-4 z-50 shadow-2xl flex flex-col bg-white' : className
|
|
}`}
|
|
>
|
|
{/* Toolbar */}
|
|
<div className="bg-gray-50/90 border-b border-gray-200 p-2 flex flex-wrap items-center gap-1.5 text-gray-700 select-none">
|
|
|
|
{/* Headings */}
|
|
<div className="flex items-center gap-0.5 bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('h1')}
|
|
className={`px-2 py-1.5 rounded-lg text-xs font-black transition-all flex items-center gap-0.5 cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isH1
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'text-gray-700 hover:bg-gray-100 hover:text-purple-700'
|
|
}`}
|
|
title="تیتر اصلی H1"
|
|
>
|
|
<Heading1 className="w-3.5 h-3.5" />
|
|
<span>H1</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('h2')}
|
|
className={`px-2 py-1.5 rounded-lg text-xs font-black transition-all flex items-center gap-0.5 cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isH2
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'text-gray-700 hover:bg-gray-100 hover:text-purple-600'
|
|
}`}
|
|
title="تیتر ثانویه H2"
|
|
>
|
|
<Heading2 className="w-3.5 h-3.5" />
|
|
<span>H2</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('h3')}
|
|
className={`px-2 py-1.5 rounded-lg text-xs font-black transition-all flex items-center gap-0.5 cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isH3
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'text-gray-700 hover:bg-gray-100 hover:text-purple-500'
|
|
}`}
|
|
title="زیرتیتر H3"
|
|
>
|
|
<Heading3 className="w-3.5 h-3.5" />
|
|
<span>H3</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('h4')}
|
|
className={`px-2 py-1.5 rounded-lg text-xs font-black transition-all flex items-center gap-0.5 cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isH4
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="زیرتیتر H4"
|
|
>
|
|
<Heading4 className="w-3.5 h-3.5" />
|
|
<span>H4</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('p')}
|
|
className={`px-2.5 py-1.5 rounded-lg text-xs font-bold transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isP && !activeStates.isH1 && !activeStates.isH2 && !activeStates.isH3 && !activeStates.isH4 && !activeStates.isBlockquote
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="پاراگراف عادی (P)"
|
|
>
|
|
P
|
|
</button>
|
|
</div>
|
|
|
|
{/* Basic Formats */}
|
|
<div className="flex items-center gap-0.5 bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('bold')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isBold ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="ضخیم (Bold)"
|
|
>
|
|
<Bold className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('italic')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isItalic ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="مورب (Italic)"
|
|
>
|
|
<Italic className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('underline')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isUnderline ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="زیرخط (Underline)"
|
|
>
|
|
<Underline className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => setBlockTag('blockquote')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isBlockquote ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="نقلقول (Quote)"
|
|
>
|
|
<Quote className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={clearFormatting}
|
|
className="p-1.5 rounded-lg text-gray-500 hover:bg-gray-100 hover:text-red-600 transition-colors cursor-pointer disabled:opacity-40"
|
|
title="پاکسازی فرمت"
|
|
>
|
|
<RemoveFormatting className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Alignment */}
|
|
<div className="flex items-center gap-0.5 bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('justifyRight')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.align === 'right' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="راستچین"
|
|
>
|
|
<AlignRight className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('justifyCenter')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.align === 'center' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="وسطچین"
|
|
>
|
|
<AlignCenter className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('justifyLeft')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.align === 'left' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="چپچین"
|
|
>
|
|
<AlignLeft className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Lists & Link */}
|
|
<div className="flex items-center gap-0.5 bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('insertUnorderedList')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isUL ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="لیست نقطهای"
|
|
>
|
|
<List className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('insertOrderedList')}
|
|
className={`p-1.5 rounded-lg transition-all cursor-pointer disabled:opacity-40 ${
|
|
activeStates.isOL ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="لیست عددی"
|
|
>
|
|
<ListOrdered className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={handleLink}
|
|
className="p-1.5 hover:bg-gray-100 rounded-lg transition-colors cursor-pointer text-blue-600 disabled:opacity-40"
|
|
title="درج لینک اینترنتی"
|
|
>
|
|
<LinkIcon className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={handleInsertTable}
|
|
className="p-1.5 hover:bg-gray-100 rounded-lg transition-colors cursor-pointer text-emerald-600 disabled:opacity-40"
|
|
title="درج جدول استاندارد"
|
|
>
|
|
<TableIcon className="w-4 h-4" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={handleInsertHorizontalRule}
|
|
className="p-1.5 hover:bg-gray-100 rounded-lg transition-colors cursor-pointer text-gray-500 disabled:opacity-40"
|
|
title="خط جداکننده (Divider)"
|
|
>
|
|
<Minus className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Inline Media Insert Buttons (Image, Video, Audio) */}
|
|
<div className="flex items-center gap-1 bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => openMediaModal('image')}
|
|
className="px-2 py-1.5 rounded-lg text-xs font-bold text-indigo-700 hover:bg-indigo-50 flex items-center gap-1 cursor-pointer transition-colors disabled:opacity-40"
|
|
title="درج تصویر بین متن (با Alt و Caption)"
|
|
>
|
|
<ImageIcon className="w-3.5 h-3.5" />
|
|
<span className="hidden sm:inline">عکس</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => openMediaModal('video')}
|
|
className="px-2 py-1.5 rounded-lg text-xs font-bold text-rose-700 hover:bg-rose-50 flex items-center gap-1 cursor-pointer transition-colors disabled:opacity-40"
|
|
title="درج ویدئو در بین متن"
|
|
>
|
|
<Video className="w-3.5 h-3.5" />
|
|
<span className="hidden sm:inline">ویدئو</span>
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => openMediaModal('audio')}
|
|
className="px-2 py-1.5 rounded-lg text-xs font-bold text-amber-700 hover:bg-amber-50 flex items-center gap-1 cursor-pointer transition-colors disabled:opacity-40"
|
|
title="درج صوت / پادکست در بین متن"
|
|
>
|
|
<Music className="w-3.5 h-3.5" />
|
|
<span className="hidden sm:inline">صوت</span>
|
|
</button>
|
|
</div>
|
|
|
|
{/* Text Color Picker */}
|
|
<div ref={colorPickerRef} className="relative">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => {
|
|
setShowColorPicker(!showColorPicker);
|
|
setShowBgColorPicker(false);
|
|
}}
|
|
className={`flex items-center gap-1.5 bg-white px-2.5 py-1.5 rounded-xl border border-gray-200 shadow-2xs text-xs font-bold cursor-pointer disabled:opacity-40 ${
|
|
showColorPicker ? 'ring-2 ring-purple-500 bg-purple-50' : 'hover:bg-gray-50'
|
|
}`}
|
|
title="رنگ متن"
|
|
>
|
|
<Palette className="w-3.5 h-3.5 text-purple-600" />
|
|
<span
|
|
className="w-3 h-3 rounded-full border border-black/20"
|
|
style={{ backgroundColor: customColor }}
|
|
/>
|
|
</button>
|
|
|
|
{showColorPicker && (
|
|
<div className="absolute top-full right-0 mt-1.5 p-3 bg-white rounded-2xl shadow-xl border border-gray-200 z-50 w-56 space-y-2.5 font-vazir text-right">
|
|
<span className="text-[11px] font-bold text-gray-500 block">رنگهای پیشنهادی:</span>
|
|
<div className="grid grid-cols-7 gap-1">
|
|
{PRESET_COLORS.map((c) => (
|
|
<button
|
|
key={c.value}
|
|
type="button"
|
|
onClick={() => {
|
|
exec('foreColor', c.value);
|
|
setCustomColor(c.value);
|
|
setShowColorPicker(false);
|
|
}}
|
|
className="w-6 h-6 rounded-lg border border-black/10 cursor-pointer hover:scale-110 transition-transform"
|
|
style={{ backgroundColor: c.value }}
|
|
title={c.label}
|
|
/>
|
|
))}
|
|
</div>
|
|
<div className="pt-2 border-t border-gray-100 flex items-center gap-2">
|
|
<input
|
|
type="color"
|
|
value={customColor}
|
|
onChange={(e) => {
|
|
setCustomColor(e.target.value);
|
|
exec('foreColor', e.target.value);
|
|
}}
|
|
className="w-8 h-8 rounded-lg cursor-pointer border-none bg-transparent"
|
|
/>
|
|
<input
|
|
type="text"
|
|
value={customColor}
|
|
onChange={(e) => {
|
|
setCustomColor(e.target.value);
|
|
exec('foreColor', e.target.value);
|
|
}}
|
|
placeholder="#111827"
|
|
className="w-full text-xs font-mono font-bold px-2 py-1 border border-gray-200 rounded-lg outline-none uppercase"
|
|
dir="ltr"
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Background Color Picker */}
|
|
<div ref={bgColorPickerRef} className="relative">
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => {
|
|
setShowBgColorPicker(!showBgColorPicker);
|
|
setShowColorPicker(false);
|
|
}}
|
|
className={`flex items-center gap-1.5 bg-white px-2.5 py-1.5 rounded-xl border border-gray-200 shadow-2xs text-xs font-bold cursor-pointer disabled:opacity-40 ${
|
|
showBgColorPicker ? 'ring-2 ring-purple-500 bg-purple-50' : 'hover:bg-gray-50'
|
|
}`}
|
|
title="هایلایت متن"
|
|
>
|
|
<PaintBucket className="w-3.5 h-3.5 text-amber-500" />
|
|
<span
|
|
className="w-3 h-3 rounded-full border border-black/20"
|
|
style={{ backgroundColor: customBgColor }}
|
|
/>
|
|
</button>
|
|
|
|
{showBgColorPicker && (
|
|
<div className="absolute top-full right-0 mt-1.5 p-3 bg-white rounded-2xl shadow-xl border border-gray-200 z-50 w-56 space-y-2.5 font-vazir text-right">
|
|
<span className="text-[11px] font-bold text-gray-500 block">رنگهای هایلایت:</span>
|
|
<div className="grid grid-cols-7 gap-1">
|
|
{PRESET_BG_COLORS.map((c) => (
|
|
<button
|
|
key={c.value}
|
|
type="button"
|
|
onClick={() => {
|
|
exec('hiliteColor', c.value);
|
|
setCustomBgColor(c.value);
|
|
setShowBgColorPicker(false);
|
|
}}
|
|
className="w-6 h-6 rounded-lg border border-black/10 cursor-pointer hover:scale-110 transition-transform"
|
|
style={{ backgroundColor: c.value }}
|
|
title={c.label}
|
|
/>
|
|
))}
|
|
</div>
|
|
<div className="pt-2 border-t border-gray-100 flex items-center gap-2">
|
|
<input
|
|
type="color"
|
|
value={customBgColor === 'transparent' ? '#ffffff' : customBgColor}
|
|
onChange={(e) => {
|
|
setCustomBgColor(e.target.value);
|
|
exec('hiliteColor', e.target.value);
|
|
}}
|
|
className="w-8 h-8 rounded-lg cursor-pointer border-none bg-transparent"
|
|
/>
|
|
<input
|
|
type="text"
|
|
value={customBgColor}
|
|
onChange={(e) => {
|
|
setCustomBgColor(e.target.value);
|
|
exec('hiliteColor', e.target.value);
|
|
}}
|
|
placeholder="#fef08a"
|
|
className="w-full text-xs font-mono font-bold px-2 py-1 border border-gray-200 rounded-lg outline-none uppercase"
|
|
dir="ltr"
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* View Mode & Fullscreen */}
|
|
<div className="flex items-center gap-1 ms-auto">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setIsCodeMode(!isCodeMode);
|
|
setShowColorPicker(false);
|
|
setShowBgColorPicker(false);
|
|
}}
|
|
className={`px-2.5 py-1.5 rounded-xl text-xs font-bold transition-all flex items-center gap-1.5 cursor-pointer ${
|
|
isCodeMode ? 'bg-purple-600 text-white shadow-xs' : 'bg-white border border-gray-200 text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
title="نمایش و ویرایش کد HTML"
|
|
>
|
|
<Code className="w-3.5 h-3.5" />
|
|
<span>{isCodeMode ? 'نمای دیداری' : 'HTML'}</span>
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsFullscreen(!isFullscreen)}
|
|
className="p-1.5 bg-white border border-gray-200 rounded-xl hover:bg-gray-100 text-gray-600 transition-colors cursor-pointer"
|
|
title={isFullscreen ? 'خروج از تمام صفحه' : 'تمام صفحه'}
|
|
>
|
|
{isFullscreen ? <Minimize2 className="w-3.5 h-3.5" /> : <Maximize2 className="w-3.5 h-3.5" />}
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('undo')}
|
|
className="p-1.5 hover:bg-gray-200 disabled:opacity-40 rounded-lg transition-colors cursor-pointer"
|
|
title="Undo"
|
|
>
|
|
<Undo className="w-3.5 h-3.5 text-gray-500" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={isCodeMode}
|
|
onClick={() => exec('redo')}
|
|
className="p-1.5 hover:bg-gray-200 disabled:opacity-40 rounded-lg transition-colors cursor-pointer"
|
|
title="Redo"
|
|
>
|
|
<Redo className="w-3.5 h-3.5 text-gray-500" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Editor Content Area */}
|
|
{isCodeMode ? (
|
|
<textarea
|
|
value={value || ''}
|
|
onChange={(e) => onChange(e.target.value)}
|
|
placeholder="کد HTML را اینجا وارد یا ویرایش کنید..."
|
|
className={`w-full p-4 ${minHeight} ${isFullscreen ? 'flex-1' : 'max-h-[500px]'} font-mono text-xs text-emerald-400 bg-slate-950 outline-none resize-y leading-relaxed`}
|
|
dir="ltr"
|
|
spellCheck={false}
|
|
/>
|
|
) : (
|
|
<div
|
|
ref={editorRef}
|
|
contentEditable
|
|
suppressContentEditableWarning
|
|
onInput={handleInput}
|
|
onKeyUp={updateActiveStates}
|
|
onMouseUp={updateActiveStates}
|
|
onClick={updateActiveStates}
|
|
onFocus={updateActiveStates}
|
|
data-placeholder={placeholder}
|
|
className={`p-5 ${minHeight} ${isFullscreen ? 'flex-1 overflow-y-auto' : 'max-h-[500px] overflow-y-auto'} outline-none text-sm leading-loose text-gray-800 font-vazir empty:before:content-[attr(data-placeholder)] empty:before:text-gray-400 [&_h1]:text-2xl [&_h1]:font-black [&_h1]:my-4 [&_h1]:text-gray-900 [&_h2]:text-xl [&_h2]:font-bold [&_h2]:my-3.5 [&_h2]:text-gray-900 [&_h3]:text-lg [&_h3]:font-bold [&_h3]:my-3 [&_h3]:text-gray-900 [&_h4]:text-base [&_h4]:font-bold [&_h4]:my-2 [&_h4]:text-gray-800 [&_p]:my-2 [&_p]:leading-loose [&_ul]:list-disc [&_ul]:ms-6 [&_ul]:my-3 [&_ol]:list-decimal [&_ol]:ms-6 [&_ol]:my-3 [&_blockquote]:border-r-4 [&_blockquote]:border-purple-600 [&_blockquote]:pr-4 [&_blockquote]:py-2 [&_blockquote]:my-4 [&_blockquote]:bg-purple-50/50 [&_blockquote]:rounded-l-xl [&_blockquote]:italic [&_blockquote]:text-gray-700 [&_a]:text-blue-600 [&_a]:underline [&_table]:border-collapse [&_table]:w-full [&_table]:my-4 [&_table_td]:border [&_table_td]:p-2.5 [&_table_th]:border [&_table_th]:p-2.5 [&_hr]:my-6 [&_hr]:border-gray-200`}
|
|
dir="rtl"
|
|
/>
|
|
)}
|
|
|
|
{/* Media Selector Modal */}
|
|
<MediaSelector
|
|
isOpen={mediaModalOpen}
|
|
onClose={() => setMediaModalOpen(false)}
|
|
onSelect={handleMediaSelect}
|
|
/>
|
|
|
|
{/* Inline Image Alt & Caption Prompt Modal */}
|
|
{showImagePrompt && (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-xs font-vazir">
|
|
<div className="bg-white rounded-3xl max-w-md w-full p-6 space-y-4 shadow-2xl border border-gray-100 text-right animate-in fade-in zoom-in-95 duration-150">
|
|
<div className="flex items-center gap-3 border-b border-gray-100 pb-3">
|
|
<div className="w-10 h-10 rounded-xl bg-purple-100 text-purple-600 flex items-center justify-center">
|
|
<ImageIcon className="w-5 h-5" />
|
|
</div>
|
|
<div>
|
|
<h4 className="font-bold text-gray-900">تنظیمات تصویر درون متن</h4>
|
|
<p className="text-xs text-gray-500">متن جایگزین (Alt برای سئو) و زیرنویس عکس را مشخص کنید</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="aspect-video w-full rounded-2xl bg-gray-100 overflow-hidden border border-gray-200">
|
|
<img src={pendingImageUrl} alt="preview" className="w-full h-full object-contain" />
|
|
</div>
|
|
|
|
<div className="space-y-3">
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">متن جایگزین تصویر (Alt Text - الزامی برای سئو) *</label>
|
|
<input
|
|
type="text"
|
|
value={imageAlt}
|
|
onChange={(e) => setImageAlt(e.target.value)}
|
|
placeholder="مثال: عکس تشویقی و مکمل غذایی سگ کنینا"
|
|
className="w-full px-3.5 py-2.5 rounded-xl border border-gray-200 text-xs focus:border-purple-500 outline-none"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">زیرنویس / کپشن عکس (اختیاری)</label>
|
|
<input
|
|
type="text"
|
|
value={imageCaption}
|
|
onChange={(e) => setImageCaption(e.target.value)}
|
|
placeholder="توضیحات تکمیلی زیر تصویر..."
|
|
className="w-full px-3.5 py-2.5 rounded-xl border border-gray-200 text-xs focus:border-purple-500 outline-none"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-end gap-2 pt-3 border-t border-gray-100">
|
|
<button
|
|
type="button"
|
|
onClick={() => { setShowImagePrompt(false); setPendingImageUrl(''); }}
|
|
className="px-4 py-2 rounded-xl text-xs font-bold text-gray-600 hover:bg-gray-100 cursor-pointer"
|
|
>
|
|
انصراف
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={confirmInsertImage}
|
|
className="px-5 py-2 rounded-xl text-xs font-bold bg-purple-600 text-white hover:bg-purple-700 cursor-pointer shadow-md shadow-purple-200"
|
|
>
|
|
درج تصویر در متن
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|