添加 DOM 属性
您可以在编辑器内的多个 DOM 元素上设置额外的 HTML 属性。这些元素包括:
editor:
编辑器本身,不包括菜单和工具栏。
block:
块的主容器元素。包含块的内容及其嵌套块。
blockGroup:
编辑器中所有顶级块和嵌套块的包装元素。
blockContent:
块内容的包装元素。
inlineContent:
块富文本内容的包装元素。
在下面的演示中,我们在 block
元素上设置了自定义类,为每个块添加边框:
import "@blocknote/core/fonts/inter.css";
import { useCreateBlockNote } from "@blocknote/react";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import "./styles.css";
export default function App() {
// Creates a new editor instance.
const editor = useCreateBlockNote({
// Sets attributes on DOM elements in the editor.
domAttributes: {
// Adds a class to all `blockContainer` elements.
block: {
class: "hello-world-block",
},
},
initialContent: [
{
type: "paragraph",
content: "Welcome to this demo!",
},
{
type: "paragraph",
content: "You can see there's a border around each block",
},
{
type: "paragraph",
content: [
{
type: "text",
text: "This is because there's a CSS rule using the ",
styles: {},
},
{
type: "text",
text: "hello-world-block",
styles: { code: true },
},
{
type: "text",
text: " class we added",
styles: {},
},
],
},
{
type: "paragraph",
},
],
});
// Renders the editor instance using a React component.
return <BlockNoteView editor={editor} />;
}