导航

v2.1.0
构建

富文本

把 Tiptap 放进编辑器外框:固定工具栏、输入字段的各种状态,以及作为数据的文档。

本页内容

Master UI 本身不提供编辑器。编辑带格式的文本,包括文档模型、选区、历史记录、粘贴以及输入法组字,是大型引擎的工作,而且已有成熟的方案。Master UI 为这类引擎提供的是一个外框:editor 契约负责为编辑区、顶部工具栏、各种状态和文档内容提供样式,让富文本字段的外观和行为都与页面上的其他输入字段一致。本指南用原生 JavaScript 把 Tiptap 放进这个外框。

富文本

<div class="grid gap-lg w-full max-w(720) mx-auto" data-example="rich-text">  <div class="field">    <span id="rich-text-label">Release notes</span>    <div class="editor">      <div class="toolbar editor-toolbar" role="toolbar" aria-label="Formatting" aria-controls="rich-text-document">        <select class="select select-sm" data-command="style" aria-label="Text style">          <option value="paragraph">Paragraph</option>          <option value="heading">Heading</option>          <option value="subheading">Subheading</option>        </select>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="bold" aria-label="Bold" aria-keyshortcuts="Control+B Meta+B" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4.6667 3H8.3333C12.3333 3 12.3333 7.6667 8.3333 7.6667H4.6667M4.6667 3V13H9C13.3333 13 13.3333 7.6667 9 7.6667" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="italic" aria-label="Italic" aria-keyshortcuts="Control+I Meta+I" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6.6667 3L12.6667 3" />            <path d="M3.3333 13L9.3333 13" />            <path d="M9.6667 3L6.3333 13" stroke-linecap="butt" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="underline" aria-label="Underline" aria-keyshortcuts="Control+U Meta+U" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4.3333 2.6667V7.6667C4.3333 12.3333 11.6667 12.3333 11.6667 7.6667V2.6667" />            <path d="M3 14L13 14" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="bulletList" aria-label="Bulleted list" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <circle cx="3" cy="4" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 4L13.5 4" />            <circle cx="3" cy="8" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 8L13.5 8" />            <circle cx="3" cy="12" r="0.5" fill="currentColor" stroke="none" />            <path d="M6 12L13.5 12" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="orderedList" aria-label="Numbered list" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M2 3.3333L3 2.6667V6M2 6H4" stroke-width="0.8" />            <path d="M2 9.6667C2 8.3333 4 8.3333 4 9.6667C4 10.6667 2 11 2 12.6667H4" stroke-width="0.8" />            <path d="M6.6667 4.6667L14 4.6667" />            <path d="M6.6667 11.3333L14 11.3333" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="blockquote" aria-label="Quotation" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M2.7667 3.3333H6.2333Q6.6667 3.3333 6.6667 3.7667V8.6667C6.6667 11.3333 4.6667 12.6667 2.3333 12.6667V10.6667C4 10.6667 4.3333 9.6667 4.3333 8H2.7667Q2.3333 8 2.3333 7.5667V3.7667Q2.3333 3.3333 2.7667 3.3333Z" />            <path d="M9.7667 3.3333H13.2333Q13.6667 3.3333 13.6667 3.7667V8.6667C13.6667 11.3333 11.6667 12.6667 9.3333 12.6667V10.6667C11 10.6667 11.3333 9.6667 11.3333 8H9.7667Q9.3333 8 9.3333 7.5667V3.7667Q9.3333 3.3333 9.7667 3.3333Z" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="link" popovertarget="rich-text-link" aria-label="Link" aria-pressed="false">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M9 4.3333L10 3.3333C11.0125 2.3208 12.6541 2.3208 13.6667 3.3333C14.6792 4.3459 14.6792 5.9875 13.6667 7L11.6667 9C10.6541 10.0125 9.0125 10.0125 8 9" />            <path d="M9 4.3333L10 3.3333C11.0125 2.3208 12.6541 2.3208 13.6667 3.3333C14.6792 4.3459 14.6792 5.9875 13.6667 7L11.6667 9C10.6541 10.0125 9.0125 10.0125 8 9" transform="rotate(180 8 8)" />          </svg>        </button>        <span class="toolbar-separator" role="separator" aria-orientation="vertical"></span>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="undo" aria-label="Undo" aria-keyshortcuts="Control+Z Meta+Z">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6 3L3.1414 5.8586A0.2 0.2 0 0 0 3.1414 6.1414L6 9" />            <path d="M2.715 6.4108A0.4996 0.4996 0 0 0 3 6.5H9.5A2.9999 2.9999 0 0 1 10.6043 12.2894Q10.0722 12.5 9.5 12.5L5.9755 12.5006A0.5 0.5 0 0 0 5.9266 13.4946L6 13.5H9.5A3.9999 3.9999 0 0 0 10.9724 5.7808Q10.263 5.5 9.5 5.5L2.9621 5.5014A0.5007 0.5007 0 0 0 2.715 5.5892A0.7044 0.7044 0 0 0 2.715 6.4108Z" fill="currentColor" stroke="none" fill-rule="evenodd" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-sm btn-square" data-command="redo" aria-label="Redo" aria-keyshortcuts="Control+Shift+Z Meta+Shift+Z">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M6 3L3.1414 5.8586A0.2 0.2 0 0 0 3.1414 6.1414L6 9" transform="translate(16 0) scale(-1 1)" />            <path d="M2.715 6.4108A0.4996 0.4996 0 0 0 3 6.5H9.5A2.9999 2.9999 0 0 1 10.6043 12.2894Q10.0722 12.5 9.5 12.5L5.9755 12.5006A0.5 0.5 0 0 0 5.9266 13.4946L6 13.5H9.5A3.9999 3.9999 0 0 0 10.9724 5.7808Q10.263 5.5 9.5 5.5L2.9621 5.5014A0.5007 0.5007 0 0 0 2.715 5.5892A0.7044 0.7044 0 0 0 2.715 6.4108Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="translate(16 0) scale(-1 1)" />          </svg>        </button>      </div>      <div class="flex flex-col" data-editor></div>    </div>    <p class="field-hint" id="rich-text-hint">Headings, lists, quotations and links. Pasted text keeps its structure, not its styling.</p>    <p class="field-error" id="rich-text-error">Write at least a sentence before you publish.</p>  </div>  <form class="popover grid gap-sm w(288)" id="rich-text-link" popover aria-label="Link">    <label class="field">Address      <input class="input input-sm" type="url" name="href" required placeholder="https://example.com" autocomplete="url">    </label>    <div class="flex justify-end gap-xs">      <button type="button" class="btn btn-sm btn-quiet" data-command="unlink">Remove link</button>      <button type="submit" class="btn btn-sm btn-primary">Apply</button>    </div>  </form>  <div class="flex flex-wrap items-center gap-sm">    <label class="check">      <input type="checkbox" role="switch" class="switch" data-state="readonly">Read only</label>    <label class="check">      <input type="checkbox" role="switch" class="switch" data-state="disabled">Disabled</label>    <span class="grow(1)"></span>    <button type="button" class="btn btn-sm" data-command="replace">Load other notes</button>    <button type="button" class="btn btn-sm" data-command="reset">Reset</button>  </div>  <div class="accordion">    <details class="accordion-item">      <summary class="accordion-trigger">The value, as JSON        <svg class="icon accordion-chevron" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(90 8 8)" />        </svg>      </summary>      <div class="accordion-content"> <pre class="code-block max-h(240)" tabindex="0" role="region" aria-label="The value, as JSON"><code data-value></code></pre>      </div>    </details>  </div></div>
输入文字,用工具栏或快捷键设置格式,添加链接,切换状态,并展开查看值。

#职责划分

负责
Tiptap文档及其结构定义、选区、撤销与重做、按键与快捷键、粘贴、输入法,以及它设为可编辑的元素
editor外框及其状态、顶部工具栏、文档尺寸,以及段落、标题、列表、引用、链接和分隔线的外观
toolbar 及其行为脚本让整组控件在 Tab 焦点顺序中只占一处、用方向键在控件间移动焦点,以及管理按钮的按下状态
你的页面把两者连接起来:按钮执行哪个命令、处于什么状态,以及值交给哪里

@master/ui 不依赖任何编辑器,也没有 React、Svelte 或 Vue 组件封装编辑器。示例属于本站点,Tiptap 是站点的依赖:可以直接复制,也可以参照它改用 ProseMirror、Lexical 或其他引擎,契约保持不变。

#把引擎放进外框

Tiptap 会把一个元素设为可编辑,并把文档写入其中。让它在该元素上加上 editor-document,并带上输入字段控件应有的角色、名称和描述;同时让它在所渲染的节点上写入编辑器的类:editor 样式表从不按元素类型选择,因此标题之所以有样式,是因为它带有 editor-heading,而不是因为它是 h2。行内代码和代码块使用组件库自己的代码类。

main.js
const editor = new Editor({  element: root.querySelector('[data-editor]'),  content: notes,  extensions: [    StarterKit.configure({      heading: { levels: [2, 3] },      paragraph: { HTMLAttributes: { class: 'editor-paragraph' } },      bulletList: { HTMLAttributes: { class: 'editor-list' } },      orderedList: { HTMLAttributes: { class: 'editor-list editor-list-numbered' } },      listItem: { HTMLAttributes: { class: 'editor-item' } },      blockquote: { HTMLAttributes: { class: 'editor-quote' } },      code: { HTMLAttributes: { class: 'code' } },      codeBlock: { HTMLAttributes: { class: 'code-block' } },      horizontalRule: { HTMLAttributes: { class: 'editor-rule' } },      link: { openOnClick: false, autolink: true, defaultProtocol: 'https', HTMLAttributes: { class: 'editor-link' } },    }),    headingClass,  ],  editorProps: {    attributes: { class: 'editor-document', role: 'textbox', 'aria-multiline': 'true', 'aria-labelledby': 'rich-text-label', 'aria-describedby': 'rich-text-hint rich-text-error' },  },})

标题不会为其级别渲染任何属性,因此用一个小扩展(示例中的 headingClass)为每个级别加上对应的类:二级标题用 editor-heading,三级标题用 editor-subheading。文档高度从 --editor-rows 行(默认 6 行)开始增长,到 --editor-max-rows 后开始滚动。

#连接工具栏

这里的工具栏是带有 editor-toolbar 的工具栏,贴合编辑区顶部。工具栏在 Tab 焦点顺序中只占一处,可用方向键在控件之间移动焦点,并通过 aria-pressed 表示按钮的按下状态;每个按钮的功能和标签由页面决定。

  • 按下按钮执行命令并交还焦点。 editor.chain().focus().toggleBold().run():读者回到文档中原先输入的位置,选区由 Tiptap 保留。
  • 选区决定哪些按钮处于按下状态。 每次处理编辑器事务时,把各按钮的 aria-pressed 设为 editor.isActive(...),因此只要光标位于粗体文字中,“粗体”按钮就显示为按下,文本样式下拉框也会显示光标所在的块类型。
  • 无法执行的操作会被禁用。 editor.can() 会试运行命令:没有可撤销的内容时禁用“撤销”;编辑器为只读或禁用状态时,禁用所有控件。
  • 通过小表单添加链接,无需使用浏览器的输入提示框。 链接按钮打开一个带 URL 输入框的弹出层。输入框自身的校验会检查地址;“应用”把链接加到选区上,没有选区时以文本形式插入,“移除”则去掉链接。Tiptap 会拒绝 javascript: 之类的地址,链接都带有 rel="noopener noreferrer nofollow"。

为每个图标按钮设置无障碍名称,并用 aria-keyshortcuts 写上 Tiptap 已处理的快捷键,让屏幕阅读器能同时读出两者。

#状态

状态标记示例如何设置
聚焦editor-document 上的 :focus文档获得焦点时由浏览器设置;外框浮起,并显示输入字段的光晕
无效editor-document 上的 aria-invalid="true"内容留空时:外框显示危险色边框,并显示输入字段的 field-error,直到读者再次输入
只读editor-document 上的 aria-readonly="true"editor.setEditable(false) 加上该属性;文字仍可阅读和复制
禁用editor 上的 aria-disabled="true"editor.setEditable(false)、该属性,并禁用所有控件

这些状态是 Tiptap 在其元素上维护的属性:请通过 editor.setOptions({ editorProps: { attributes } }) 设置,切勿直接写在元素上,否则下一次渲染会将其移除。

#值

Tiptap 的文档是数据。应用保存的是 editor.getJSON(),用 editor.commands.setContent(json) 载入已保存的文档,Load other notes 和 Reset 就是这样做的。示例用 textContent 输出 JSON:读者输入的任何内容都不会作为标记插入页面。如果需要在编辑器之外以 HTML 显示内容,请在服务器上根据 JSON 渲染,并经过清理器处理。

监听 update 来保存或校验,监听 transaction 来更新工具栏;两者都不会修改文档。页面不在文档上添加自己的按键处理:输入日文、中文或韩文的输入法在组字时会用到 Enter 和方向键,而 Tiptap 知道何时正在组字。请在 blur 时校验,而不是在按键时。

#销毁

编辑器持有监听器、视图及其历史记录。mount() 返回一个函数,它会卸载工具栏行为脚本、移除页面的监听器并调用 editor.destroy();在输入字段离开页面时调用它,就像框架在组件卸载时所做的那样。

#未涵盖的内容

  • 跟随选区的工具栏。 浮在选中文字上方的工具栏需要追踪选区的绘制位置,这由 Tiptap 的 BubbleMenu 扩展及其定位负责;本指南使用固定工具栏。
  • 提及、表格、图片和协同编辑。 每一项都是一个 Tiptap 扩展,各有自己的界面。
  • Markdown 导入与导出。 请在服务器上转换 JSON。