feat(blog): complete full-stack blog module with rich text editor, SEO analyzer, JSON-LD schema, and RSS feed
Deploy Canina / deploy (push) Successful in 1m43s
Deploy Canina / deploy (push) Successful in 1m43s
This commit is contained in:
1 parent
eb63d22ac4
commit
5705354429
23 files changed
+124946
-120969
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
Heading1,
|
||||
Heading2,
|
||||
Heading3,
|
||||
Heading4,
|
||||
List,
|
||||
ListOrdered,
|
||||
Link as LinkIcon,
|
||||
@@ -18,19 +19,29 @@ import {
|
||||
Redo,
|
||||
Code,
|
||||
RemoveFormatting,
|
||||
Image as ImageIcon,
|
||||
Video,
|
||||
Music,
|
||||
Table as TableIcon,
|
||||
Quote,
|
||||
Minus,
|
||||
Maximize2,
|
||||
Minimize2,
|
||||
} from 'lucide-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: '#1d4ed8' },
|
||||
{ label: 'آبی کنینا', value: '#0052cc' },
|
||||
{ label: 'بنفش اختصاصی', value: '#7c3aed' },
|
||||
{ label: 'سبز درمانی', value: '#059669' },
|
||||
{ label: 'قرمز هشدار', value: '#dc2626' },
|
||||
@@ -54,27 +65,42 @@ interface ActiveStates {
|
||||
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,
|
||||
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 [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,
|
||||
@@ -82,12 +108,30 @@ export default function RichTextEditor({
|
||||
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;
|
||||
@@ -98,11 +142,11 @@ export default function RichTextEditor({
|
||||
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 if queryCommandValue is ambiguous
|
||||
// Check parent tag
|
||||
const selection = window.getSelection();
|
||||
let parentTag = '';
|
||||
if (selection && selection.rangeCount > 0) {
|
||||
@@ -112,7 +156,7 @@ export default function RichTextEditor({
|
||||
}
|
||||
while (node && node !== editorRef.current) {
|
||||
const tag = (node as HTMLElement).tagName?.toLowerCase();
|
||||
if (['h1', 'h2', 'h3', 'p', 'ul', 'ol'].includes(tag)) {
|
||||
if (['h1', 'h2', 'h3', 'h4', 'p', 'ul', 'ol', 'blockquote'].includes(tag)) {
|
||||
parentTag = tag;
|
||||
break;
|
||||
}
|
||||
@@ -129,7 +173,9 @@ export default function RichTextEditor({
|
||||
isH1: activeBlock === 'h1',
|
||||
isH2: activeBlock === 'h2',
|
||||
isH3: activeBlock === 'h3',
|
||||
isP: activeBlock === 'p' || (!['h1', 'h2', 'h3'].includes(activeBlock) && !isUL && !isOL),
|
||||
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')
|
||||
@@ -139,7 +185,7 @@ export default function RichTextEditor({
|
||||
: 'right',
|
||||
});
|
||||
} catch {
|
||||
// Ignored if document commands aren't ready
|
||||
// Ignored
|
||||
}
|
||||
}, [isCodeMode]);
|
||||
|
||||
@@ -169,18 +215,14 @@ export default function RichTextEditor({
|
||||
setTimeout(updateActiveStates, 20);
|
||||
};
|
||||
|
||||
// Robust formatBlock handler for H1, H2, H3, P
|
||||
const setBlockTag = (tagName: 'h1' | 'h2' | 'h3' | 'p') => {
|
||||
const setBlockTag = (tagName: 'h1' | 'h2' | 'h3' | 'h4' | 'p' | 'blockquote') => {
|
||||
if (isCodeMode) return;
|
||||
editorRef.current?.focus();
|
||||
|
||||
// Cross-browser formatBlock
|
||||
const blockValue = `<${tagName}>`;
|
||||
const success = document.execCommand('formatBlock', false, blockValue);
|
||||
if (!success) {
|
||||
document.execCommand('formatBlock', false, tagName);
|
||||
}
|
||||
|
||||
if (editorRef.current) {
|
||||
onChange(editorRef.current.innerHTML);
|
||||
}
|
||||
@@ -195,6 +237,97 @@ export default function RichTextEditor({
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
@@ -212,21 +345,26 @@ export default function RichTextEditor({
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`border border-gray-200 rounded-2xl bg-white overflow-hidden shadow-xs relative ${className}`}>
|
||||
<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/80 border-b border-gray-100 p-2 flex flex-wrap items-center gap-1 text-gray-700 select-none">
|
||||
<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-1 cursor-pointer disabled:opacity-40 ${
|
||||
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 (سئو)"
|
||||
title="تیتر اصلی H1"
|
||||
>
|
||||
<Heading1 className="w-3.5 h-3.5" />
|
||||
<span>H1</span>
|
||||
@@ -235,12 +373,12 @@ export default function RichTextEditor({
|
||||
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-1 cursor-pointer disabled:opacity-40 ${
|
||||
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"
|
||||
title="تیتر ثانویه H2"
|
||||
>
|
||||
<Heading2 className="w-3.5 h-3.5" />
|
||||
<span>H2</span>
|
||||
@@ -249,22 +387,36 @@ export default function RichTextEditor({
|
||||
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-1 cursor-pointer disabled:opacity-40 ${
|
||||
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"
|
||||
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.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'
|
||||
}`}
|
||||
@@ -274,18 +426,16 @@ export default function RichTextEditor({
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Basic Formats (Bold, Italic, Underline) */}
|
||||
{/* 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'
|
||||
activeStates.isBold ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="بولد (ضخیم)"
|
||||
title="ضخیم (Bold)"
|
||||
>
|
||||
<Bold className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -294,11 +444,9 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.isItalic ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="ایتالیک (کج)"
|
||||
title="مورب (Italic)"
|
||||
>
|
||||
<Italic className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -307,20 +455,29 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.isUnderline ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="خط زیرین (Underline)"
|
||||
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="پاکسازی قالببندی و استایلها"
|
||||
title="پاکسازی فرمت"
|
||||
>
|
||||
<RemoveFormatting className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -333,9 +490,7 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.align === 'right' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="راستچین"
|
||||
>
|
||||
@@ -346,9 +501,7 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.align === 'center' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="وسطچین"
|
||||
>
|
||||
@@ -359,9 +512,7 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.align === 'left' ? 'bg-purple-50 text-purple-700 font-bold' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="چپچین"
|
||||
>
|
||||
@@ -376,11 +527,9 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.isUL ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="لیست بالتدار (نقطهای)"
|
||||
title="لیست نقطهای"
|
||||
>
|
||||
<List className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -389,11 +538,9 @@ export default function RichTextEditor({
|
||||
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'
|
||||
activeStates.isOL ? 'bg-purple-600 text-white shadow-xs' : 'text-gray-700 hover:bg-gray-100'
|
||||
}`}
|
||||
title="لیست شمارهدار"
|
||||
title="لیست عددی"
|
||||
>
|
||||
<ListOrdered className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -406,9 +553,61 @@ export default function RichTextEditor({
|
||||
>
|
||||
<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>
|
||||
|
||||
{/* Text Color Picker & Custom HEX */}
|
||||
{/* 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 className="relative">
|
||||
<button
|
||||
type="button"
|
||||
@@ -420,10 +619,9 @@ export default function RichTextEditor({
|
||||
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="انتخاب رنگ متن"
|
||||
title="رنگ متن"
|
||||
>
|
||||
<Palette className="w-3.5 h-3.5 text-purple-600" />
|
||||
<span>رنگ متن</span>
|
||||
<span
|
||||
className="w-3 h-3 rounded-full border border-black/20"
|
||||
style={{ backgroundColor: customColor }}
|
||||
@@ -449,7 +647,6 @@ export default function RichTextEditor({
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="pt-2 border-t border-gray-100 flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
@@ -476,7 +673,7 @@ export default function RichTextEditor({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Background Color Picker & Custom HEX */}
|
||||
{/* Background Color Picker */}
|
||||
<div className="relative">
|
||||
<button
|
||||
type="button"
|
||||
@@ -488,10 +685,9 @@ export default function RichTextEditor({
|
||||
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="هایلایت و رنگ پسزمینه متن"
|
||||
title="هایلایت متن"
|
||||
>
|
||||
<PaintBucket className="w-3.5 h-3.5 text-amber-500" />
|
||||
<span>هایلایت</span>
|
||||
<span
|
||||
className="w-3 h-3 rounded-full border border-black/20"
|
||||
style={{ backgroundColor: customBgColor }}
|
||||
@@ -517,7 +713,6 @@ export default function RichTextEditor({
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="pt-2 border-t border-gray-100 flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
@@ -544,8 +739,8 @@ export default function RichTextEditor({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* HTML Code View vs Visual */}
|
||||
<div className="flex items-center bg-white p-0.5 rounded-xl border border-gray-200 shadow-2xs">
|
||||
{/* View Mode & Fullscreen */}
|
||||
<div className="flex items-center gap-1 ms-auto">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
@@ -553,26 +748,30 @@ export default function RichTextEditor({
|
||||
setShowColorPicker(false);
|
||||
setShowBgColorPicker(false);
|
||||
}}
|
||||
className={`px-2.5 py-1.5 rounded-lg text-xs font-bold transition-all flex items-center gap-1.5 cursor-pointer ${
|
||||
isCodeMode
|
||||
? 'bg-purple-600 text-white shadow-xs'
|
||||
: 'text-gray-700 hover:bg-gray-100'
|
||||
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"
|
||||
title="نمایش و ویرایش کد HTML"
|
||||
>
|
||||
<Code className="w-3.5 h-3.5" />
|
||||
<span>{isCodeMode ? 'نمای دیداری' : 'کد HTML'}</span>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
{/* Undo / Redo */}
|
||||
<div className="flex items-center gap-0.5 ms-auto">
|
||||
<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)"
|
||||
title="Undo"
|
||||
>
|
||||
<Undo className="w-3.5 h-3.5 text-gray-500" />
|
||||
</button>
|
||||
@@ -581,7 +780,7 @@ export default function RichTextEditor({
|
||||
disabled={isCodeMode}
|
||||
onClick={() => exec('redo')}
|
||||
className="p-1.5 hover:bg-gray-200 disabled:opacity-40 rounded-lg transition-colors cursor-pointer"
|
||||
title="حرکت به جلو (Redo)"
|
||||
title="Redo"
|
||||
>
|
||||
<Redo className="w-3.5 h-3.5 text-gray-500" />
|
||||
</button>
|
||||
@@ -594,7 +793,7 @@ export default function RichTextEditor({
|
||||
value={value || ''}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder="کد HTML را اینجا وارد یا ویرایش کنید..."
|
||||
className="w-full p-4 min-h-[140px] max-h-[350px] font-mono text-xs text-emerald-400 bg-slate-950 outline-none resize-y leading-relaxed"
|
||||
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}
|
||||
/>
|
||||
@@ -609,10 +808,79 @@ export default function RichTextEditor({
|
||||
onClick={updateActiveStates}
|
||||
onFocus={updateActiveStates}
|
||||
data-placeholder={placeholder}
|
||||
className="p-4 min-h-[140px] max-h-[350px] overflow-y-auto outline-none text-sm leading-relaxed text-gray-800 font-vazir empty:before:content-[attr(data-placeholder)] empty:before:text-gray-400 [&_h1]:text-2xl [&_h1]:font-black [&_h1]:my-2.5 [&_h1]:text-gray-900 [&_h2]:text-xl [&_h2]:font-bold [&_h2]:my-2 [&_h2]:text-gray-900 [&_h3]:text-lg [&_h3]:font-bold [&_h3]:my-1.5 [&_h3]:text-gray-900 [&_p]:my-1 [&_p]:leading-relaxed [&_ul]:list-disc [&_ul]:ms-6 [&_ul]:my-2 [&_ol]:list-decimal [&_ol]:ms-6 [&_ol]:my-2 [&_a]:text-blue-600 [&_a]:underline"
|
||||
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>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user