reactjs - SlateJS:仅在活动块上方显示浮动工具栏
问题描述
我正在使用@udecode/slate-plugins创建一个SlateJS编辑器。我得到了大部分编辑器的工作,但是,正在尝试为表格块创建一个工具栏,当它被选中/激活时,它应该在表格上方可见。
该工具栏当前在表格上方正确显示,但是当我在选择/活动中的一个表中,当我在编辑器中插入多个表时,在每个表上方都可以看到工具栏。
如何使工具栏仅在活动表上可见而在其余表上隐藏?
演示: Codesandbox.io
表块代码
const Table = ({
attributes,
children,
className,
nodeProps
}: StyledElementProps) => {
const editor = useTSlate();
const isActive = someNode(editor, { match: { type: ELEMENT_TABLE } });
return (
<table {...attributes} className={className} {...nodeProps}>
<Tippy
interactive={true}
arrow={false}
visible={isActive}
onClickOutside={(instance) => instance.hide()}
content={
<div style={{ display: "flex", flexDirection: "row" }}>
<ToolbarTable
icon={<CgExtensionRemove />}
transform={deleteTable}
tooltip={{ content: "Delete Table" }}
/>
<ToolbarTable
icon={<AiOutlineInsertRowBelow />}
transform={addRow}
tooltip={{ content: "Insert Row" }}
/>
<ToolbarTable
icon={<AiOutlineDeleteRow />}
transform={deleteRow}
tooltip={{ content: "Delete Row" }}
/>
<ToolbarTable
icon={<AiOutlineInsertRowRight />}
transform={addColumn}
tooltip={{ content: "Insert Column" }}
/>
<ToolbarTable
icon={<AiOutlineDeleteColumn />}
transform={deleteColumn}
tooltip={{ content: "Delete Column" }}
/>
</div>
}
>
<tbody>{children}</tbody>
</Tippy>
</table>
);
};
解决方案
通过获取当前选择并检查表块是否包含选择来解决它。
const [isActive, setIsActive] = useState(false);
const domSelection = window.getSelection();
useEffect(() => {
setIsActive(
!!(
someNode(editor, { match: { type: ELEMENT_TABLE } }) &&
domSelection &&
attributes.ref.current &&
attributes.ref.current.contains(domSelection.anchorNode)
)
);
}, [attributes, domSelection, editor]);
推荐阅读
- vba - 如何在 Excel 上使用 VBA 宏在不同的工作表上应用不同的 FreezePanes
- php - 如何将碳转换为字符串,只取日期?
- css - npm install 后如何引用 Google Material-Design-Icons?
- android - Cordova 错误:JDK 1.8 或更高版本的需求检查失败
- json - JSON“字符 0 周围的值无效”,尽管 url 工作正常且 JSON 有效
- c++ - 在 C++ 中动态分配变量类型
- reactjs - React - componentWillReceiveProps 替代方案
- python - 为什么我的随机枢轴快速排序比固定枢轴快速排序慢?
- python - lmdb 包无法导入
- xcode - XPC 服务可以链接到嵌入它的框架吗?