網站導覽

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 行(預設六行)長到 --editor-max-rows 行,之後就會捲動。

#連接工具列

工具列是加上 editor-toolbar 的工具列,平貼在編輯區頂端。它的行為讓它只占一個 Tab 停駐點,可以用方向鍵在其中移動,並以 aria-pressed 表示按鈕的按下狀態;每個按鈕做什麼、說明什麼,則由頁面決定。

  • 按下按鈕會執行指令並交回焦點。 editor.chain().focus().toggleBold().run():讀者會回到文件中原本書寫的位置,並保有 Tiptap 所保留的選取範圍。
  • 由選取範圍決定按下狀態。 每次發生 transaction 時,每個按鈕的 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) 則會載入已儲存的內容,就像「載入其他筆記」和「重設」所做的那樣。範例以 textContent 寫出 JSON:讀者輸入的任何內容都不會被當成標記放進頁面。若必須在編輯器以外以 HTML 顯示內容,請在伺服器上從 JSON 渲染,並經過清理器處理。

監聽 update 來儲存或驗證,監聽 transaction 來繪製工具列;兩者都不會改變文件。頁面不會在文件上加入自己的按鍵處理程式:以輸入法組字日文、中文或韓文時會使用 Enter 和方向鍵,而 Tiptap 知道何時正在組字。請在 blur 時驗證,而不是在按鍵時。

#銷毀

編輯器會保留監聽器、檢視,以及復原與重做紀錄。mount() 會傳回一個函式,用來卸除工具列行為、移除頁面的監聽器,並呼叫 editor.destroy();請在欄位離開頁面時呼叫它,就像框架在元件卸載時所做的那樣。

#未涵蓋的部分

  • 跟隨選取範圍的工具列。 浮在所選文字上方的工具列需要追蹤選取範圍繪製的位置,這由 Tiptap 的 BubbleMenu 擴充及其定位功能負責;本指南維持固定的工具列。
  • 提及、表格、圖片和協同編輯。 每一項都是一個 Tiptap 擴充,並有各自的介面。
  • Markdown 的匯入與匯出。 請在伺服器上轉換 JSON。