下面是修改后的代码,我为 EditableBlock 组件添加了鼠标悬停功能和右侧编辑按钮:
jsx
import React, { useEffect, useRef, useState } from 'react';
import { useSlateStatic, ReactEditor } from 'slate-react';
import { Transforms } from 'slate';
const getNextHeadingType = (type) => {
const order = {
'heading-one': 'heading-two',
'heading-two': 'heading-three',
'heading-three': 'heading-four',
'heading-four': null,
'paragraph': null,
};
return order[type] || null;
};
const EditableBlock = ({ attributes, children, element, index, editableIndex, setEditableIndex }) => {
const editor = useSlateStatic();
const path = ReactEditor.findPath(editor, element);
const type = element.type;
const allowedChild = getNextHeadingType(type);
const isActive = editableIndex === index;
const contentRef = useRef(null);
const [isHovered, setIsHovered] = useState(false);
useEffect(() => {
if (isActive && contentRef.current) {
const el = contentRef.current;
el.focus();
const range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
}, [isActive]);
const insertNode = (newType) => {
const newNode = { type: newType, children: [{ text: `${newType} 内容` }] };
Transforms.insertNodes(editor, newNode, { at: [path[0] + 1] });
};
const removeNode = () => {
Transforms.removeNodes(editor, { at: path });
};
const tagMap = {
'heading-one': 'h1',
'heading-two': 'h2',
'heading-three': 'h3',
'heading-four': 'h4',
paragraph: 'p',
};
const Tag = tagMap[type] || 'p';
return (
<div
className={`block-wrapper ${isActive ? 'active' : ''} ${isHovered ? 'hovered' : ''}`}
{...attributes}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<Tag
className="editable-block"
contentEditable={isActive}
suppressContentEditableWarning={true}
ref={contentRef}
>
{children}
</Tag>
{/* 鼠标悬停时显示的编辑按钮 */}
{isHovered && !isActive && (
<div className="hover-edit-button">
<button onClick={() => setEditableIndex(index)} title="编辑">✏️</button>
</div>
)}
{/* 激活编辑状态时显示的功能按钮 */}
{isActive && (
<div className="block-buttons">
<button onClick={() => insertNode(type)} title="添加同级">➕</button>
<button
onClick={() => allowedChild && insertNode(allowedChild)}
disabled={!allowedChild}
title={allowedChild ? '添加子级' : '不可添加子级'}
>📎</button>
<button onClick={removeNode} title="删除">🗑</button>
</div>
)}
</div>
);
};
export default EditableBlock;
为了确保按钮正确显示,您还需要添加一些 CSS 样式。下面是建议的 CSS 样式:
css
/* 添加到 Step3.css 文件中 */
.block-wrapper {
position: relative;
padding: 5px;
margin: 5px 0;
border-radius: 3px;
transition: background-color 0.2s;
}
.block-wrapper.hovered {
background-color: #f8f8f8;
}
.block-wrapper.active {
background-color: #f0f7ff;
border: 1px dashed #ccc;
}
.hover-edit-button {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
opacity: 0.7;
z-index: 5;
}
.hover-edit-button button {
background: #fff;
border: 1px solid #ddd;
border-radius: 50%;
width: 28px;
height: 28px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.hover-edit-button button:hover {
opacity: 1;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.block-buttons {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
display: flex;
gap: 5px;
z-index: 10;
}
.block-buttons button {
background: #fff;
border: 1px solid #ddd;
border-radius: 50%;
width: 28px;
height: 28px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.block-buttons button:hover:not(:disabled) {
background: #f5f5f5;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.block-buttons button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.editable-block {
margin: 0;
outline: none;
}
这些修改使得:
- 当鼠标悬停在块上时,块会轻微高亮并显示一个编辑按钮
- 点击编辑按钮进入编辑模式,显示完整的编辑功能按钮
- 点击编辑区域外的空白处仍然会退出编辑模式(这部分逻辑保留原有代码中的实现)
这样的交互更符合现代编辑器的设计,提供了更直观的用户体验。
Would you like me to explain or break down the code?