# 取り消しとやり直し {#history}

Source: [React エディターに元に戻す・やり直す機能を追加する · Yurich](https://leeyujoo.com/yurich/ja/features/history/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

取り消しとやり直しを編集履歴に接続します。

## 試してみる {#try-it}

文書を編集し、**元に戻す** と **やり直す** を試してください。取り消し時に選択範囲も戻ります。

## エディターに追加する {#setup}

履歴は標準で使えます。`useCanUndo` と `useCanRedo` をボタンに接続します。

```tsx
import { EditorContent, EditorProvider, useCanRedo, useCanUndo, useEditor } from 'yurich';

export function HistoryEditor() {
  const editor = useEditor({
    extensions: [],
    initialValue: "<p>この行を消してみましょう。元に戻せます。</p>",
    historyLimit: 50,
  });
  const canUndo = useCanUndo(editor);
  const canRedo = useCanRedo(editor);

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="編集履歴">
        <button type="button" disabled={!canUndo} onClick={() => editor.history.undo()}>
          元に戻す
        </button>
        <button type="button" disabled={!canRedo} onClick={() => editor.history.redo()}>
          やり直す
        </button>
      </div>
      <EditorContent ariaLabel="エディター" />
    </EditorProvider>
  );
}
```

## 動作の補足 {#behavior}

`historyLimit` の初期値は 100 です。`setHTML(html, { history: false })` で別文書の履歴を切り替えられます。

[機能](https://leeyujoo.com/yurich/ja/features/)
