React API
@imposia/react が提供する React コンポーネント、Hook、props、命令型ハンドルです。
@imposia/react は、1 つの Core コントローラーと 1 つの canonical iframe を React のライフサイクルに結び付けます。Core と Viewer の API も再エクスポートするため、React アプリケーションでは通常、他の Imposia パッケージを import する必要はありません。@imposia/react/styles.css は一度だけ読み込んでください。
ImposiaPageViewer と ImposiaDocument
ImposiaPageViewer は Core のページ文書をマウントし、最初の確定後にページ Viewer を結び付けます。ImposiaDocument は同じ公開 props とハンドル型を持つ互換ラッパーです。ImposiaDocumentProps = ImposiaPageViewerProps、ImposiaDocumentHandle = ImposiaPageViewerHandle が成り立ちます。
const viewer = useRef<ImposiaPageViewerHandle>(null);
<ImposiaPageViewer
ref={viewer}
source={{ html: "<main><h1>レポート</h1></main>" }}
documentOptions={{ page: { size: "A4", margin: "18mm" } }}
viewerOptions={{ mode: "single", inspector: true }}
onError={console.error}
/>;| prop | 型 | 必須 | 役割 |
|---|---|---|---|
source | PageSource | はい | ページ分割する HTML または light DOM です。 |
sourceRevision | string | number | いいえ | ソースの同一性・内容の比較では変化を検出できないときに、更新を強制します。 |
documentOptions | PageDocumentOptions | いいえ | Core のページ分割オプションです。 |
documentOptionsRevision | string | number | いいえ | 新しい文書オプションで Core を再マウントします。 |
viewerOptions | PageViewerOptions | いいえ | ページ表示、Reader、Inspector のオプションです。 |
onViewerStateChange | (state: PageViewerState) => void | いいえ | ページ、ズーム、モード、実効モード、世代の変化を受け取ります。 |
className、style | string、CSSProperties | いいえ | ホスト div の表示です。 |
onReady | (document: PageDocument) => void | いいえ | 文書と Viewer の結び付けが準備できた後に実行されます。 |
onError | (error: unknown) => void | いいえ | Core または Viewer の結び付けの失敗を受け取ります。 |
onStateChange | (state: ImposiaDocumentState) => void | いいえ | idle、loading、ready、error を受け取ります。 |
オプション props は revision が変わったときだけ読まれます
documentOptions はコントローラーのマウント時に取り込まれます。変更したオプションオブジェクト — 新しいリゾルバー、拡張機能の集合、ページジオメトリ — を渡すだけでは何も起きません。documentOptionsRevision を変えて、新しいオプションでコントローラーを再マウントしてください。下の publicationOptions と publicationOptionsRevision にも同じ規則が適用されます。単独で検出されるのは source(と snapshot)の変更だけです。
ImposiaPageViewerHandle
ハンドルのメソッドは、最後に確定した世代に作用します。Viewer または確定を必要とするメソッドは、最初の確定前とアンマウント後には例外をスローするか、Promise が reject されます。
| メンバー | 戻り値 | 動作と制約 |
|---|---|---|
current | PageDocument | undefined | マウント中は現在の確定、それ以外は undefined です。 |
viewerState | PageViewerState | undefined | 準備済みの間の、現在の表示スナップショットです。 |
goToPage(page)、nextPage()、previousPage() | void | 確定済みのグローバルページ列を移動します。 |
setZoom(zoom) | void | 表示ズームを丸めて範囲内に収めます。 |
setMode(mode) | void | continuous、single、spread を受け取ります。 |
setSpreadCover(cover) | void | 見開きの表紙レイアウトを切り替えます。 |
openInspector()、closeInspector()、toggleInspector() | void | viewerOptions.inspector: true が必要です。 |
selectWarning(warning) | void | Inspector と、現在の世代の警告が必要です。 |
print() | Promise<void> | 最後に完成したページのネイティブ印刷を開きます。読者はここでPDF に保存を選べます。PDF バイトは返しません。 |
exportEpub(options) | Promise<Blob> | 現在の確定をリフロー型 EPUB として書き出します。 |
ImposiaPublicationViewer
const publication = useRef<ImposiaPublicationViewerHandle>(null);
<ImposiaPublicationViewer
ref={publication}
snapshot={{
metadata: { title: "ガイド", language: "ja" },
entries: [{ id: "intro", title: "はじめに", html: "<h1>はじめに</h1>" }],
}}
/>;必須の prop は snapshot で、PublicationSnapshot を受け取ります。任意の props は snapshotRevision、publicationOptions、publicationOptionsRevision、viewerOptions(reader を除く)、readerOptions(controller を除く)、className、style、onReady、onError、onStateChange です。revision 系の props は、名前のとおり更新と再マウントを強制します。Reader とコントローラーの結び付けはコンポーネント自身が用意します。reader と controller がオプション props から除かれているのは、そのためです。
ImposiaPublicationViewerHandle
current と resolveDestination を除くすべての操作は、アンマウント後に例外をスローするか、Promise が reject されます。Reader に関わる操作と print()、exportEpub() は、Publication の Reader が準備できる前も同様です。
| メンバー | 戻り値 | 動作と制約 |
|---|---|---|
current | PublicationDocument | undefined | マウント中は現在の確定です。 |
resolveDestination(id) | PublicationDestination | undefined | 確定前、アンマウント後、不明な ID には undefined です。 |
navigate(destination) | void | 古い移動先には例外をスローします。 |
openTableOfContents()、closeTableOfContents()、toggleTableOfContents() | void | 目次パネルを操作します。 |
openThumbnails()、closeThumbnails()、toggleThumbnails() | void | サムネイルパネルを操作します。 |
getThumbnails() | readonly PublicationThumbnail[] | 現在の世代のサムネイルです。 |
selectThumbnail(thumbnail) | void | 別の世代のサムネイルには例外をスローします。 |
restoreDeepLink(value) | PublicationDestination | undefined | 有効な現在のリンクを解決し、そこへ移動します。 |
openSearch()、closeSearch()、toggleSearch() | void | 検索パネルを操作します。 |
search(query) | readonly PublicationSearchResult[] | 現在の確定を検索します。 |
nextSearchResult()、previousSearchResult() | PublicationSearchResult | undefined | 結果間を移動します。 |
selectSearchResult(result) | void | 結果へ移動します。 |
setMode(mode)、setSpreadCover(cover) | void | ページ表示を変更します。 |
openInspector()、closeInspector()、toggleInspector() | void | viewerOptions.inspector: true が必要です。 |
selectWarning(warning) | void | 現在の世代の Inspector 警告が必要です。 |
print() | Promise<void> | 最新の確定のネイティブ印刷を開きます。ブラウザーのPDF に保存もここで選べます。 |
exportEpub(options) | Promise<Blob> | 最新の確定を書き出します。 |
useImposiaDocument と useImposiaPublication
useImposiaDocument(props: UseImposiaDocumentProps): UseImposiaDocumentResult;
useImposiaPublication(props: UseImposiaPublicationProps): UseImposiaPublicationResult;どちらの Hook も、hostRef、ライフサイクルの state、そしてマウントエフェクトが作るまで undefined のままの controller を返します。hostRef は 1 つの div に付けてください。loading や error の状態でも、最後に確定した文書・Publication は保持されることがあります。
useImposiaDocument は、ページコンポーネントと同じソース、revision、オプション、コールバックを受け取ります。HTML ソースは HTML または基準 URL が変わると更新され、light DOM ソースはノードの同一性が変わると更新されます。sourceRevision は更新を強制し、documentOptionsRevision はコントローラーを再マウントします。
useImposiaPublication は、Publication コンポーネントと同じスナップショット、revision、オプション、コールバックを受け取ります。スナップショットの参照または snapshotRevision が変わると更新し、publicationOptionsRevision はコントローラーを再マウントします。どちらの Hook もクリーンアップ時にコントローラーを破棄し、AbortError をライフサイクル上の中止として扱います。
function Article({ html }: { html: string }) {
const { hostRef, state } = useImposiaDocument({ source: { html } });
return <div ref={hostRef} aria-busy={state.status === "loading"} />;
}