创建行内格式
用 mark() 给一行中的部分文字添加格式。选中 Yurich 后点击 高亮,选中的单词会被包在 <mark> 中。
交互示例
示例结果
import { mark, EditorContent, EditorProvider, useEditor, useIsActive } from 'yurich';
const Highlight = mark({ name: 'highlight', tag: 'mark', shortcut: 'Mod+Shift+H' });
export function HighlightEditor() {
const editor = useEditor({
extensions: [Highlight],
initialValue: "<p>给 Yurich 一点强调。</p>",
});
const active = useIsActive(editor, 'toggleHighlight');
return (
<EditorProvider editor={editor}>
<button
type="button"
aria-pressed={active}
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('toggleHighlight')}
>
高亮
</button>
<EditorContent ariaLabel="编辑器" />
</EditorProvider>
);
}
查看结果
在 editor.getHTML() 返回的 HTML 中,只有 Yurich 被 <mark> 标签包裹。
<p>给 <mark>Yurich</mark> 一点强调。</p>
下面是 editor.getJSON() 返回的文档树中 Yurich 对应的文本节点。marks 数组中的 highlight 是通过 mark() 注册的格式名称。
{
"type": "text",
"text": "Yurich",
"marks": [{ "type": "highlight" }]
}
标签名使用小写字母。mark() 支持用 aliases 指定等效的输入标签。