创建自定义块
用 block() 给整个段落添加格式。把光标放在段落里,点击 提示块,段落就会变成 <aside> 块。
交互示例
示例结果
import { block, EditorContent, EditorProvider, useEditor, useIsActive } from 'yurich';
const Callout = block({ name: 'callout', tag: 'aside' });
export function CalloutEditor() {
const editor = useEditor({
extensions: [Callout],
initialValue: "<p>工具栏长什么样,由你决定。</p>",
});
const active = useIsActive(editor, 'toggleCallout');
return (
<EditorProvider editor={editor}>
<button
type="button"
aria-pressed={active}
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('toggleCallout')}
>
提示块
</button>
<EditorContent ariaLabel="编辑器" />
</EditorProvider>
);
}
查看结果
在 editor.getHTML() 返回的 HTML 中,整个段落变成了一个 <aside> 块。
<aside>工具栏长什么样,由你决定。</aside>
下面是 editor.getJSON() 返回的文档中的第一个块。它的 type 是 callout,即通过 block() 注册的名称。
{
"type": "callout",
"content": [
{ "type": "text", "text": "工具栏长什么样,由你决定。" }
]
}