Spaces:
Sleeping
Sleeping
File size: 8,707 Bytes
7b0525c | 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | import React from 'react';
import { Editor } from '@tiptap/react';
import {
Bold,
Italic,
Code,
List,
ListOrdered,
Quote,
Heading1,
Heading2,
Link as LinkIcon,
Terminal,
FileCode,
Eraser,
AlignLeft,
AlignCenter,
AlignRight,
AlignJustify,
Settings
} from 'lucide-react';
interface EditorContextMenuProps {
editor: Editor | null;
x: number;
y: number;
onClose: () => void;
onOpenSettings: () => void;
}
export const EditorContextMenu: React.FC<EditorContextMenuProps> = ({ editor, x, y, onClose, onOpenSettings }) => {
if (!editor) return null;
const handleAction = (e: React.MouseEvent, action: () => void) => {
e.preventDefault();
e.stopPropagation();
action();
onClose();
};
const isAlignActive = (align: string) => editor.isActive({ textAlign: align });
const MenuItem = ({
icon: Icon,
label,
onClick,
isActive = false,
kdb = ''
}: {
icon: any,
label: string,
onClick: (e: React.MouseEvent) => void,
isActive?: boolean,
kdb?: string
}) => (
<button
onMouseDown={onClick}
className={`w-full flex items-center justify-between px-3 py-2 text-sm transition-colors ${isActive
? 'bg-primary/20 text-primary'
: 'text-gray-300 hover:bg-white/10 hover:text-white'
}`}
>
<div className="flex items-center gap-2">
<Icon size={16} />
<span>{label}</span>
</div>
{kdb && <span className="text-xs text-gray-500 font-mono ml-4">{kdb}</span>}
</button>
);
const Divider = () => <div className="h-px bg-white/10 my-1 mx-2" />;
return (
<div
className="fixed z-[100] w-64 bg-[#1e1e1e] border border-white/10 rounded-lg shadow-2xl overflow-hidden py-1"
style={{
top: Math.min(y, window.innerHeight - 400),
left: Math.min(x, window.innerWidth - 260)
}}
onMouseDown={(e) => e.stopPropagation()}
onContextMenu={(e) => e.preventDefault()}
>
<MenuItem
icon={Heading1}
label="T铆tulo 1"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleHeading({ level: 1 }).run())}
isActive={editor.isActive('heading', { level: 1 })}
/>
<MenuItem
icon={Heading2}
label="T铆tulo 2"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleHeading({ level: 2 }).run())}
isActive={editor.isActive('heading', { level: 2 })}
/>
<Divider />
<MenuItem
icon={Bold}
label="Negrita"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleBold().run())}
isActive={editor.isActive('bold')}
kdb="Ctrl+B"
/>
<MenuItem
icon={Italic}
label="Cursiva"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleItalic().run())}
isActive={editor.isActive('italic')}
kdb="Ctrl+I"
/>
<Divider />
<MenuItem
icon={LinkIcon}
label="Enlace"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
const previousUrl = editor.getAttributes('link').href;
const url = window.prompt('URL del enlace:', previousUrl);
if (url === null) return;
if (url === '') {
editor.chain().focus().extendMarkRange('link').unsetLink().run();
return;
}
editor.chain().focus().extendMarkRange('link').setLink({ href: url }).run();
onClose();
}}
isActive={editor.isActive('link')}
/>
<MenuItem
icon={Code}
label="C贸digo Inline"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleCode().run())}
isActive={editor.isActive('code') && editor.getAttributes('code').showBackground !== false}
/>
<MenuItem
icon={Terminal}
label="Estilo Tarjeta"
onClick={(e) => handleAction(e, () => {
const isCodeActive = editor.isActive('code');
if (isCodeActive) {
editor.chain().focus().unsetCode().run();
} else {
editor.chain().focus().setCode().updateAttributes('code', { showBackground: false }).run();
}
})}
isActive={editor.isActive('code') && editor.getAttributes('code').showBackground === false}
/>
<MenuItem
icon={FileCode}
label="Bloque de C贸digo"
onClick={(e) => handleAction(e, () => editor.chain().focus().setCodeBlock({ language: 'text' }).run())}
isActive={editor.isActive('codeBlock')}
/>
<Divider />
<MenuItem
icon={Quote}
label="Cita"
onClick={(e) => handleAction(e, () => {
if (editor.isActive('customBlockquote')) {
editor.chain().focus().unsetBlockquote().run();
} else {
editor.chain().focus().setBlockquote().run();
}
})}
isActive={editor.isActive('customBlockquote')}
/>
<MenuItem
icon={List}
label="Lista con vi帽etas"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleBulletList().run())}
isActive={editor.isActive('bulletList')}
/>
<MenuItem
icon={ListOrdered}
label="Lista numerada"
onClick={(e) => handleAction(e, () => editor.chain().focus().toggleOrderedList().run())}
isActive={editor.isActive('orderedList')}
/>
<Divider />
<div className="flex justify-between px-2 py-1">
<button
onMouseDown={(e) => handleAction(e, () => editor.chain().focus().setTextAlign('left').run())}
className={`p-1 rounded hover:bg-white/10 ${isAlignActive('left') ? 'text-primary' : 'text-gray-400'}`}
>
<AlignLeft size={16} />
</button>
<button
onMouseDown={(e) => handleAction(e, () => editor.chain().focus().setTextAlign('center').run())}
className={`p-1 rounded hover:bg-white/10 ${isAlignActive('center') ? 'text-primary' : 'text-gray-400'}`}
>
<AlignCenter size={16} />
</button>
<button
onMouseDown={(e) => handleAction(e, () => editor.chain().focus().setTextAlign('right').run())}
className={`p-1 rounded hover:bg-white/10 ${isAlignActive('right') ? 'text-primary' : 'text-gray-400'}`}
>
<AlignRight size={16} />
</button>
<button
onMouseDown={(e) => handleAction(e, () => editor.chain().focus().setTextAlign('justify').run())}
className={`p-1 rounded hover:bg-white/10 ${isAlignActive('justify') ? 'text-primary' : 'text-gray-400'}`}
>
<AlignJustify size={16} />
</button>
</div>
<Divider />
<MenuItem
icon={Eraser}
label="Limpiar Formato"
onClick={(e) => handleAction(e, () => editor.chain().focus().unsetAllMarks().clearNodes().run())}
/>
<MenuItem
icon={Settings}
label="Configuraci贸n"
onClick={(e) => handleAction(e, onOpenSettings)}
/>
</div>
);
};
|