文档

代码块

用独立块显示多行代码。

试一试

在示例段落中点击 代码块。结果是 <pre>editor.execute('toggleBold');</pre>。

交互示例
示例结果

添加到编辑器

启用 CodeBlock 并调用 toggleCodeBlock 创建 <pre>。

import { EditorContent, EditorProvider, CodeBlock, useEditor, useIsActive } from 'yurich';

export function CodeBlockEditor() {
  const editor = useEditor({
    extensions: [CodeBlock],
    initialValue: "<p>editor.execute('toggleBold');</p>",
  });
  const active = useIsActive(editor, 'toggleCodeBlock');

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        aria-pressed={active}
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('toggleCodeBlock')}
      >
        代码块
      </button>
      <EditorContent ariaLabel="编辑器" />
    </EditorProvider>
  );
}

行为说明

在块中粘贴纯文本会保留换行。语法高亮可由应用提供。

功能