为通用编辑器配置RTE configure-rte
了解如何在通用编辑器中配置富文本编辑器(RTE)。
概述 overview
通用编辑器在就地和属性面板中提供富文本编辑器(RTE),以允许作者在编辑文本时应用格式更改。
此RTE可使用组件筛选器进行配置。 本文档介绍了可用的配置选项以及示例。
- 您可以停用这些不需要的选项。
- 不支持激活与您的项目类型不兼容的选项。
配置结构 structure
RTE配置由两部分组成:
这些配置可以定义为具有属性rte的组件筛选器的一部分。
[
{
"id": "richtext",
"rte": {
"toolbar": {
// Toolbar configuration
},
"actions": {
// Action-specific configurations
}
},
"components": [
"richtext"
]
}
]
工具栏配置 toolbar
工具栏配置控制UI中可用的编辑选项及其组织方式。 这些是可用的部分
{
"toolbar": {
// Text formatting options
"format": ["bold", "italic", "underline", "strike", "code_inline", "text_color"],
// Text alignment options
"alignment": ["left", "center", "right", "justify"],
// Text direction options, right-to-left or left-to-right
"direction": ["rtl", "ltr"],
// Indentation controls
"indentation": ["indent", "outdent"],
// Block-level elements
"blocks": ["paragraph", "h1", "h2", "h3", "h4", "h5", "h6", "code_block", "blockquote"],
// List options
"list": ["bullet_list", "ordered_list"],
// Content insertion
"insert": ["link", "unlink", "image", "table", "special_characters"],
// Superscript/subscript
"sr_script": ["superscript", "subscript"],
// Editor utilities
"editor": ["removeformat", "clean_unsupported_tags", "paste_text", "fullscreen", "find_and_replace"],
// Advanced items (e.g. the class picker).
"advanced": ["classes"],
// Section ordering (optional)
"sections": ["format", "alignment", "list"]
}
}
操作配置 action
通过操作配置,您可以自定义单个编辑操作的行为和外观。 这些是可用的部分。
常用操作选项 common-action-options
大多数操作支持以下常用选项:
shortcut?: string — 覆盖操作的默认键盘快捷键(如果有)label?:字符串 — 覆盖UI中操作使用的标签hideInline?:布尔值 — 当true时,从上下文(内联)RTE编辑器工具栏中隐藏此操作
{
"actions": {
"bold": {
"label": "Bold",
"shortcut": "Mod-B",
"hideInline": true
}
}
}
设置操作格式 format
格式操作用于应用格式设置,并且支持HTML标记切换以便在语义变体之间进行选择。 以下部分可供使用。
{
"actions": {
"bold": {
"tag": "strong", // Use <strong> instead of <b>
"shortcut": "Mod-B", // Custom keyboard shortcut
"label": "Make Bold" // Custom button label
},
"italic": {
"tag": "em", // Use <em> instead of <i>
"shortcut": "Mod-I",
"label": "Italicize"
},
"strike": {
"tag": "del" // Use <del> instead of <s>
}
}
}
列出操作 list
列表操作支持内容封装,以控制HTML结构。 以下部分可供使用。
{
"actions": {
"bullet_list": {
"wrapInParagraphs": true, // <ul><li><p>content</p></li></ul>
"shortcut": "Mod-Shift-8", // Custom shortcut
"label": "Bullet List" // Custom label
},
"ordered_list": {
"wrapInParagraphs": false, // <ol><li>content</li></ol> (default)
"shortcut": "Mod-Shift-9"
}
}
}
表格操作 table-actions
表操作支持内容封装,以控制表单元格中的HTML结构:
{
"actions": {
"table": {
"wrapInParagraphs": false, // <td>content</td> (default)
"shortcut": "Mod-Alt-T", // Custom shortcut
"label": "Insert Table" // Custom label
}
}
}
表配置选项 table-configuration-options
wrapInParagraphs:false(默认) — 表单元格包含未换行的文本内容wrapInParagraphs:true— 表单元格将内容包裹在段落标记中
示例:
当wrapInParagraphs: false:
<!-- Single line -->
<td>Cell content</td>
<!-- Multiple paragraphs get <br> separation -->
<td>Line 1<br />Line 2</td>
当wrapInParagraphs: true:
<!-- Single paragraph -->
<td><p>Cell content</p></td>
<!-- Multiple paragraphs preserved -->
<td>
<p>Line 1</p>
<p>Line 2</p>
</td>
wrapInParagraphs: false)时,清理器会自动在多个段落之间插入<br>标记,以保留可视换行符。 这遵循HTML标准和主要富文本编辑器的常见实践。属性对话框 properties-dialog
表插件附带三个属性编辑器对话框,在启用这些标记时,这些对话框从 表 菜单打开:
- 表属性 — 编辑封闭表的宽度/高度、单元格间距、单元格边距、对齐方式、边框(宽度/样式/颜色)和背景颜色。 边框和单元格内边距会向同一事务中的每个单元格进行扇形更改,因此视觉效果与用户期望的结果相匹配。
- 行属性 — 编辑封入行的行类型(页眉/正文/页脚)、对齐方式、高度、边框(宽度/样式/颜色)和背景颜色。 行类型为仅部分(与TinyMCE的默认
table_header_type: "section"匹配):标头行在序列化时移入<thead>,但其单元格仍为<td>。 为<th>使用单元格属性 -> 标题单元格。 在序列化时,这些行将分组为实<thead>/<tbody>/<tfoot>节(与TinyMCE/Word匹配)。 请参阅下面的存储说明。 边框会扇出到行的单元格中,以便在边框折叠下呈现。 - 单元格属性 — 编辑选定的单元格类型(单元格/标题单元格)、宽度/高度、水平和垂直对齐方式、边框(宽度/样式/颜色)和背景颜色。 当
CellSelection跨越多个单元格时,修补程序将同时应用于每个选定的单元格。 单元格类型在table_cell(<td>)和table_header(<th>)之间切换单元格。
这三者均默认禁用(选择加入),这与RTE配置界面的其余部分相匹配,在消费者明确启用这些功能之前,这些功能处于禁用状态。 希望显示对话框时根据消费者选择启用:
{
actions: {
table: {
showTableProperties: true, // default false: show "Table Properties" menu item
showRowProperties: true, // default false: show "Row Properties" menu item
showCellProperties: true, // default false: show "Cell Properties" menu item
}
}
}
属性对话框选项 properties-dialog-options
|—|—|—|
| 选项 | 默认 | 效果 |
| showTableProperties | false | 在 表 下拉列表中显示 表属性 项 |
| showRowProperties | false | 显示 行 子菜单中的 行属性 项 |
| showCellProperties | false | 显示 单元格 子菜单中的 单元格属性 项 |
对话框在相应的ProseMirror节点上往返于htmlAttrs.style,即内联CSS(如TinyMCE/Word发出)而不是自定义数据属性。 单元格宽度还写入了架构级别colwidth,因此它在PM的表机制中得以保留。 单元格类型由节点标记(<td>/<th>)承载。
prosemirror-tables架构是平面的(table -> table_row -> cell),没有<thead>/<tbody>/<tfoot>节节点,因此行标题/正文/页脚意图在内部作为data-row-type标记保留在<tr>上。 当行的类型更改时,这些行在同一事务中重新排序为页眉 — >正文 — >页脚顺序,因此编辑器画布实时匹配导出的结构(页眉行提升到顶部,页脚行吸收到底部,如TinyMCE)。 当任何单元格跨越多行(rowspan > 1)时,将跳过此编辑器中的重新排序,其中移动行会损坏网格。 在退出时,sanitizeHTML将行分组为真正的<thead>/<tbody>/<tfoot>节(标题行为前,页脚行为后)并丢弃标记,从而生成相同的结构TinyMCE/Word发出。 在进入的路上,<tr>的封闭部分会被读回标记中,因此会对HTML往返进行分段。
链接操作 link
链接操作支持目标属性控制以管理链接行为。 以下部分可供使用。
{
"actions": {
"link": {
"hideTarget": false, // Show target attribute options (default)
"shortcut": "Mod-K", // Custom keyboard shortcut
"label": "Insert Link" // Custom button label
},
"unlink": {
"shortcut": "Mod-Shift-K", // Custom keyboard shortcut
"label": "Remove Link" // Custom button label
}
}
}
链接配置选项 link-options
hideTarget:false(默认) — 在链接中包含目标属性,允许_self、_blank等hideTarget:true— 从链接中完全排除目标属性
仅当光标位于现有链接内时,unlink操作才会出现。 它删除链接格式,同时保留文本内容。
图像操作 image
图像操作支持图片元素封装以生成响应式图像标记。 以下部分可供使用。
{
"actions": {
"image": {
"wrapInPicture": false, // Use <img> tag (default)
"shortcut": "Mod-Shift-I", // Custom keyboard shortcut
"label": "Insert Image" // Custom button label
}
}
}
图像配置选项 image-options
wrapInPicture:false(默认) — 生成简单的<img>元素wrapInPicture:true— 将图像包装在<picture>个元素中以用于响应式设计
全屏 full-screen
全屏操作可切换编辑器进入和退出全屏模式:
{
actions: {
fullscreen: {
label: "Fullscreen"; // Custom button label
}
}
}
当全屏处于活动状态时,编辑器包装器将接收类rte-fullscreen-wrapper。 使用者应用程序可以定位此类以应用自定义样式或CSS属性(例如z-index、维度、叠加)。 (在通用编辑器中不受支持)
缩进配置 indentation
缩进具有控制缩进行为范围的功能级配置,以及快捷方式和标签的单个操作配置。
{
"actions": {
// Feature-level configuration
"indentation": {
"scope": "all" // Controls what content can be indented (default: "all")
},
// Individual action configurations
"indent": {
"shortcut": "Tab", // Custom keyboard shortcut
"label": "Increase Indent" // Custom button label
},
"outdent": {
"shortcut": "Shift-Tab", // Custom keyboard shortcut
"label": "Decrease Indent" // Custom button label
}
}
}
缩进范围选项 indentation-options
-
scope:all(默认) — 缩进/减少缩进适用于所有内容:- 列表:嵌套/取消嵌套列表项
- 段落和标题:增加/减少一般缩进级别
-
scope:lists— 缩进/减少缩进仅适用于列表项:- 列表:嵌套/取消嵌套列表项
- 段落和标题:无缩进(这些按钮已禁用)
特殊字符 special-characters
special_characters插入操作将打开用于插入特殊字符(符号、数学运算符、货币符号、标点、箭头等)的字符选择器弹出框 在光标位置。
{
"toolbar": {
"insert": ["link", "unlink", "image", "table", "special_characters"],
"sections": ["insert"],
},
"actions": {
"special_characters": {
"label": "Special Characters"
}
}
}
包括现成可用的44个常用字符的默认集。 可通过两个配置选项自定义字符列表:
appendCharacters— 向默认集添加字符characters— 完全替换默认集
每个字符条目都有character(Unicode字符)和title(工具提示/可访问名称)。
将字符附加到默认值 append-special-characters
{
"actions": {
"special_characters": {
"appendCharacters": [
{ "character": "\u2605", "title": "Black star" },
{ "character": "\u2764", "title": "Heavy black heart" },
];
}
}
}
替换默认特殊字符 replace-special-characters
{
"actions": {
"special_characters": {
"characters": [
{ "character": "\u00A9", "title": "Copyright sign" },
{ "character": "\u00AE", "title": "Registered sign" },
{ "character": "\u2122", "title": "Trade mark sign" },
];
}
}
}
两个选项一起使用 both-special-character-options
此示例使用characters作为基数,然后使用appendCharacters附加其他字符。
{
"actions": {
"special_characters": {
"characters": [
{ "character": "\u00A9", "title": "Copyright sign" },
{ "character": "\u00AE", "title": "Registered sign" }
],
"appendCharacters": [
{ "character": "\u2605", "title": "Black star" }
]
}
}
}
CSS 类 css-classes
classes操作会添加一个下拉列表,以将可配置的CSS类应用于RTE内容。 它作为advanced部分中的项列出。 该插件仅在生成的HTML上写入/删除类名。 使用者应用程序拥有每个类的实际CSS,并负责在渲染RTE内容的任意位置(编辑器、预览、发布)加载它。
只有在配置了至少一个选项时,才会显示下拉列表。 该插件出厂时没有默认值。
{
toolbar: {
advanced: ["classes"],
sections: ["blocks", "format", "advanced"],
},
actions: {
classes: {
label: "Add Custom Class",
options: [
{ value: "rte-hero", label: "Hero title" },
{ value: "rte-lede", label: "Lede paragraph" },
{ value: "rte-callout", label: "Callout block" },
{ value: "rte-pill", label: "Pill (inline)" },
],
},
},
}
每个条目都具有值(写入HTML的CSS类名称)和标签(下拉列表中显示的可读文本)。
目标遵循最接近插入符号的规则:
- 托管范围中的选择为空:该类应用于该范围运行,而不是应用于块。 选择其他类将交换范围类;删除自定义类将清除它。
- 其他位置有空选择(仅游标):所选类应用到游标的最近块级祖先(
<p>、<h1>等)。 当光标位于封装段落的列表项或表单元格内时,<li>/<td>/<th>接收类,而不是内部段落。 - 实际选择:所选文本已包装在
<span class="...">中。 如果所选内容已经有一个范围,则会更新现有范围的类。 - 删除自定义类:从目标中删除托管类(范围运行或块,以插入符号解析到的为准)。 如果某个范围上唯一剩余的属性是托管类,则该范围本身将被解包。 仅当托管类当前处于活动状态时,删除自定义类选项才会显示。
不属于已配置选项列表的现有类将保持不变。 只有托管类可交换in/out。 使用者集类与插件安全地共存。
下拉菜单反射:
- 在仅放置光标时,下拉菜单反映最近的托管类:插入符号处的范围(如果有),否则显示光标下的块。
- 选择后,下拉列表会在环绕选择的范围上反映托管类。 仅当整个选定内容共享同一类时。 混合选择不显示任何选定内容。
块类型 block-types
blocks工具栏部分呈现一个下拉列表,该下拉列表在配置的块类型之间切换当前块。 支持的值为paragraph、h1-h6、code_block和blockquote。
blockquote是一个包装块(其ProseMirrorcontent为block+),不同于文本块的paragraph/h1-h6/code_block。 选择 Quote 将当前块包装在<blockquote>中。当插入符号位于其中时,下拉菜单会将 Quote 显示为已选择。 选择任何其他块类型(例如段落),而引用中会先将块从引用中提起,因此也会离开引用。 这反映了TinyMCE的块下拉列表。- 由于它是一个封装节点,因此
<blockquote>可以包含多个段落、列表甚至嵌套引号。 这就是外部创作内容往返不畅的原因:TinyMCE(旧版内容)和Word(粘贴)都会发出<blockquote><p>…</p></blockquote>(通常为多段落),并且结构会在加载、编辑和序列化时保留,而不是扁平化。 - 与所有块类型一样,使用者应用程序拥有CSS,该CSS在渲染RTE内容的任意位置都以可视方式显示
<blockquote>。
{
"toolbar": {
"blocks": ["paragraph", "h1", "h2", "h3", "code_block", "blockquote"],
"sections": ["blocks"],
},
"actions": {
"blockquote": {
"label": "Quote", // Custom dropdown label
},
},
}
粘贴为文本 paste-as-text
paste_text编辑器操作可启用标准的纯文本粘贴工作流。
- 默认快捷键: Mod-Shift-V(在macOS上为Cmd+Shift+V,在Windows/Linux上为Ctrl+Shift+V)
- 行为:从文本/纯文本粘贴(忽略源格式)
- 在列表中,新行将创建新列表项。
{
"toolbar": {
"editor": ["removeformat", "paste_text"]
},
"actions": {
"paste_text": {
"shortcut": "Mod-Shift-v",
"label": "Paste as Text"
}
}
}
查找和替换 find-replace
find_and_replace编辑器操作使用prosemirror-search(匹配高亮显示)以及由FIND_AND_REPLACE_PLUGIN_KEY键控的命令插件。
- 集成:当
toolbar.editor包含find_and_replace时,将createFindAndReplacePlugin()添加到编辑器的ProseMirror插件。 - API:使用
FIND_AND_REPLACE_PLUGIN_KEY.getState(state)访问find、replaceNext、replaceAll和getMatchCount。 - 样式:确保您的应用程序加载
.ProseMirror-search-match和.ProseMirror-active-search-match的CSS(来自prosemirror-search的样式/search.css或您自己的等效样式)。
{
"toolbar": {
"editor": ["find_and_replace"],
"sections": ["editor"],
},
"actions": {
"find_and_replace": {
"label": "Find and replace",
},
},
}
其他操作 other
所有其他操作都支持基本自定义。 以下部分可供使用。
{
"actions": {
"h1": {
"shortcut": "Mod-Alt-1",
"label": "Large Heading"
},
"paragraph": {
"shortcut": "Mod-Alt-0",
"label": "Normal Text"
},
"link": {
"shortcut": "Mod-K",
"label": "Insert Link",
"hideTarget": false // Show target attribute options (default: false)
}
}
}
完整示例 example
以下是完整配置的示例。
[
{
"id": "richtext",
"rte": {
// Configure which tools appear in toolbar
"toolbar": {
"format": [
"bold",
"italic",
"text_color"
],
"blocks": [
"paragraph",
"h1",
"h2"
],
"list": [
"bullet_list",
"ordered_list"
],
"insert": [
"link",
"unlink",
"image",
"special_characters"
],
"editor": [
"removeformat",
"paste_text"
],
"sections": [
"format",
"blocks",
"list",
"insert"
]
},
// Customize individual action behavior
"actions": {
// Format actions with HTML tag choices
"bold": {
"tag": "strong",
"shortcut": "Mod-B",
"label": "Bold"
},
"italic": {
"tag": "em",
"shortcut": "Mod-I"
},
// List actions with content wrapping
"bullet_list": {
"wrapInParagraphs": true,
"label": "Bullet List"
},
"ordered_list": {
"wrapInParagraphs": false
},
// Link actions with target control
"link": {
"hideTarget": false,
"shortcut": "Mod-K",
"label": "Add Link"
},
"unlink": {
"label": "Remove Link"
},
// Image actions with picture wrapping
"image": {
"wrapInPicture": false, // Use <img> tag instead of <picture>
"shortcut": "Mod-Shift-I",
"label": "Insert Image",
},
// Special characters with custom additions
"special_characters": {
"label": "Special Characters",
"appendCharacters": [{ "character": "\u2605", "title": "Black star" }],
},
// Other actions with basic customization
"paste_text": {
"shortcut": "Mod-Shift-v",
"label": "Paste as Text",
},
"h1": {
"shortcut": "Mod-Alt-1",
"label": "Main Heading"
}
}
}
}
]
操作选项详细信息 action-details
有几个选项提供了一些需要牢记的其他详细信息。
wrapInParagraphs wrapInParagraphs
列表的wrapInParagraphs选项控制HTML结构。
wrapInParagraphs: false(默认) wrapInParagraphs-false
<ul>
<li>Simple text content</li>
<li>Another item</li>
</ul>
wrapInParagraphs: true wrapInParagraphs-true
<ul>
<li><p>Text wrapped in paragraphs</p></li>
<li><p>Supports rich formatting within items</p></li>
</ul>
在需要时使用wrapInParagraphs: true:
- 列表项中的丰富格式
- 每个列表项包含多个段落
- 一致的块级样式
wrapInPicture
图像的wrapInPicture选项控制为图像内容生成的HTML结构。
wrapInPicture: false(默认) wrapinpicture-false
<img src="image.jpg" alt="Description" />
wrapInPicture: true wrapinpicture-true
<picture>
<img src="image.jpg" alt="Description" />
</picture>
在需要时使用wrapInPicture: true:
- 具有
<source>个元素的响应式图像支持。 - 艺术指导功能。
- 高级图像功能经得起未来考验。
- 图素结构一致。
wrapInPicture: true后,可以为不同的媒体查询和格式使用其他<source>元素来增强图像,使它们对于响应式设计更加灵活。链接目标选项 link-target
链接的hideTarget选项控制target属性是否包含在生成的链接中,以及链接创建对话框是否包含用于选择目标的字段。
hideTarget: false(默认) hideTarget-false
<a href="https://example.com" target="_self">Link text</a>
<a href="https://example.com" target="_blank">External link</a>
hideTarget: true hideTarget-true
<a href="https://example.com">Link text</a>
禁用图像上的链接 disableforimages
链接的disableForImages选项控制用户是否可以在图像和图片元素上创建链接。 这适用于内联<img>元素和块级<picture>元素。
disableForImages: false(默认) disableforimages-false
用户可以选择图像并将其包装在链接中。
<!-- Inline image with link -->
<a href="https://example.com">
<img src="image.jpg" alt="Description" />
</a>
<!-- Block-level picture with link -->
<a href="https://example.com">
<picture>
<img src="image.jpg" alt="Description" />
</picture>
</a>
disableForImages: true disableforimages-true
选择图像或图片时,将禁用链接按钮。 用户只能对文本内容创建链接。
<!-- Images remain standalone without links -->
<img src="image.jpg" alt="Description" />
<picture>
<img src="image.jpg" alt="Description" />
</picture>
<!-- Links work normally on text -->
<a href="https://example.com">Link text</a>
当您想要:disableForImages: true
- 通过防止链接的图像保持视觉一致性。
- 通过将图像和导航分离,简化内容结构。
- 强制实施限制图像链接的内容策略。
- 降低内容中的辅助功能复杂性。
标记选项 tag
格式操作允许在HTML变体之间进行切换。
bold<strong><b>italic<em><i>strike<del><s>选择语义标记(<strong>、<em>、<del>)以获得更好的可访问性和SEO。
内联代码 inline-code
code_inline格式选项将文本范围标记为内联代码,渲染<code>(例如,引用句子中的foo()调用)。 与bold/italic/strike不同,它没有标记切换选项,并且排除了所有其他标记。 内联代码是逐字呈现的,因此bold/italic/link不能应用在其之上。 默认键盘快捷键为Mod-e。
这不同于blocks下拉列表中可用的code_block块(<pre><code>)。 code_inline是行中的内联标记,而code_block是独立的多行块。 代码块仅保留纯文本(无标记),因此内联<code>从不嵌套在<pre>内。
Use the <code>foo()</code> helper here.
文本颜色 color
text_color格式选项向编辑器添加文本着色功能。
<!-- With color applied -->
<span style="color: #ff0000">Colored text</span>
<!-- Color removed -->
Plain text
该插件还会分析旧版<font color="...">元素以便向后兼容。
键盘快捷键 keyboard-shortcuts
快捷方式使用Mod-Key格式,其中:
- 在Mac上,
Mod=Cmd,在Windows/Linux上,Ctrl - 示例:
Mod-B,Mod-Shift-8,Mod-Alt-1
不支持的HTML unsupported-html
默认情况下,编辑器解析未知HTML标记时,会去除这些标记。 要保留它们,请通过unsupportedHtml配置选项选择加入:
const rteConfig = {
unsupportedHtml: true, // preserve unknown HTML tags (default: false)
};
false(默认)trueunsupportedHtmlOptions.structuralTags(可选) unsupportedhtmloptions
使用unsupportedHtmlOptions.structuralTags控制将哪些附加结构标记视为受支持的HTML。
const rteConfig = {
unsupportedHtml: true,
unsupportedHtmlOptions: {
structuralTags: ["div", "section"], // preserve configured tags as supported
},
};
[]["div"]<div>被视为受支持的HTML,而不是包装为不受支持。["div", "section"]<div>和<section>均被视为受支持的HTML,并且未包装为不受支持。unsupportedHtmlOptions仅在启用unsupportedHtml时影响行为。启用后,编辑器将渲染不支持的节点,这些节点具有包装器标记(unsupported-block/unsupported-inline)和包装器类。 使用者应用程序应提供此类的样式(例如,边框、填充、背景)。 块中的标记标签使用rte-unsupported-label,也可以对其进行自定义。
rte-unsupported-blockrte-unsupported-inlinerte-unsupported-label
使用者应用程序应提供这些类的样式(例如,边框、间距、背景和内联对齐方式)。
启用unsupportedHtml后,使用者应添加不支持的节点插件(例如createUnsupportedNodesPlugin()),以便复制不支持的块或内联节点将其内部文本放入剪贴板(仅限纯文本),并且用户可以将内容粘贴到其他位置。