ナビゲーション

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 とそのビヘイビアコントロール全体で 1 つのタブストップ、コントロール間の矢印キーでの移動、押された状態のボタン
ページ両者の接続。ボタンが実行するコマンド、ボタンの状態、値の送り先

@master/ui はどのエディターにも依存しておらず、エディターを包む React、Svelte、Vue のコンポーネントもありません。この例はサイト独自のもので、Tiptap はサイトの依存関係です。例をコピーするか、ProseMirror、Lexical などのエンジンで同じ手順をたどってください。コントラクトは変わりません。

#エンジンを枠組みに入れる

Tiptap は要素を編集可能にし、その中にドキュメントを書き込みます。その要素に、フィールドのコントロールと同じロール、名前、説明とともに editor-document を付けるよう Tiptap に指定し、描画するノードにエディターのクラスを書き込ませます。editor のスタイルシートは要素名で選択しないため、見出しにスタイルが付くのは h2 だからではなく、editor-heading を持っているからです。インラインのコードとコードブロックには、ライブラリー自身のコードのクラスを使います。

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)で各レベルにクラスを付けます。2 番目のレベルには editor-heading、3 番目のレベルには editor-subheading です。ドキュメントは --editor-rows 行(既定は 6 行)から --editor-max-rows 行まで伸び、それを超えるとスクロールします。

#ツールバーをつなぐ

ツールバーは editor-toolbar を付けたツールバーで、ウェルの上部に平らに配置されます。ツールバーのビヘイビアにより、ツールバー全体が 1 つのタブストップになり、内部のコントロール間を矢印キーで移動できます。ボタンの押された状態は aria-pressed で示します。各ボタンが何を行い、何を伝えるかはページが決めます。

  • 押すとコマンドを実行し、フォーカスを戻します。 editor.chain().focus().toggleBold().run() を実行すると、読み手は Tiptap が保持していた選択範囲とともに、書いていたドキュメントに戻ります。
  • 選択範囲によって押された状態が決まります。 トランザクションのたびに、各ボタンの aria-pressed を editor.isActive(...) の結果にします。そのため、キャレットが太字のテキストの中にあれば「太字」は押された状態になり、テキストスタイルの選択欄にはキャレットがあるブロックの種類が表示されます。
  • 実行できないものは無効にします。 editor.can() はコマンドを試験的に実行します。取り消せる操作がなければ「元に戻す」を無効にし、エディターが読み取り専用または無効のときはすべてのコントロールを無効にします。
  • リンクはプロンプトではなく小さなフォームです。 リンクボタンは、URL フィールドを備えたポップオーバーを開きます。アドレスはフィールド自体の検証で確認します。「適用」は選択範囲にリンクを設定し、何も選択されていなければリンクをテキストとして挿入します。「削除」はリンクを解除します。Tiptap は javascript: などのアドレスを拒否し、リンクには rel="noopener noreferrer nofollow" が付きます。

アイコンボタンにはそれぞれ名前を付け、Tiptap がすでに処理しているショートカットを aria-keyshortcuts に指定します。これにより、スクリーンリーダーが両方を読み上げられます。

#状態

状態目印この例での設定方法
フォーカス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) で読み込みます。別のメモを読み込む と リセット はこの方法で読み込みます。この例では JSON を textContent で書き出すため、読み手が入力したものがマークアップとしてページに挿入されることはありません。エディターの外で内容を HTML として表示する必要がある場合は、サーバーでサニタイザーを通して JSON から描画します。

保存や検証には update を、ツールバーの描画には transaction を監視します。どちらもドキュメントを変更しません。ページはドキュメントに独自のキーハンドラーを追加しません。日本語、中国語、韓国語の入力メソッドは、文字の確定前に Enter キーや矢印キーを使います。Tiptap は入力が未確定かどうかを認識します。検証はキー操作ではなく blur で行います。

#破棄する

エディターは、リスナー、ビュー、履歴を保持しています。mount() が返す関数は、ツールバーのビヘイビアを解除し、ページのリスナーを削除し、editor.destroy() を呼び出します。フレームワークがコンポーネントをアンマウントするときと同じように、フィールドがページからなくなるときに呼び出してください。

#この例で扱わないもの

  • 選択範囲に追従するツールバー。 選択した文字の上に浮かぶツールバーには、選択範囲が描画される位置を追跡する仕組みが必要です。これは Tiptap の BubbleMenu 拡張機能とその配置処理の役割で、このガイドではツールバーを固定しています。
  • メンション、テーブル、画像、共同編集。 それぞれが Tiptap の拡張機能と独自のインターフェースです。
  • Markdown の入出力。 JSON をサーバーで変換してください。