React API
@imposia/react가 제공하는 React 컴포넌트, 훅, prop, 명령형 핸들입니다.
@imposia/react는 Core 컨트롤러 하나와 canonical iframe 하나를 React 수명 주기에 연결합니다. Core와 Viewer의 API 표면도 다시 내보내므로, React 앱에는 대개 다른 Imposia import가 필요 없습니다. @imposia/react/styles.css는 한 번만 불러오세요.
ImposiaPageViewer와 ImposiaDocument
ImposiaPageViewer는 Core 페이지 문서를 마운트한 뒤, 첫 확정 이후에 Page Viewer를 연결합니다. ImposiaDocument는 공개 prop과 핸들 타입이 같은 호환 래퍼입니다. 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를 받습니다. |
옵션 prop은 revision이 바뀔 때만 읽힙니다
documentOptions는 컨트롤러가 마운트될 때 캡처됩니다. 새 리졸버, 확장 집합, 페이지 기하 정보를 담은 옵션 객체를 전달하는 것만으로는 아무 일도 일어나지 않습니다. documentOptionsRevision을 바꿔 새 옵션으로 컨트롤러를 다시 마운트하세요. 아래의 publicationOptions와 publicationOptionsRevision에도 같은 규칙이 적용됩니다. source(와 snapshot)의 변경만 자체적으로 감지됩니다.
ImposiaPageViewerHandle
핸들 메서드는 마지막 확정 세대에 작용합니다. Viewer나 확정된 문서가 필요한 메서드는 첫 확정 전과 언마운트 후에 예외를 발생시키거나 Promise를 거부합니다.
| 멤버 | 반환값 | 제약 |
|---|---|---|
current | PageDocument | undefined | 마운트 중에는 현재 확정된 문서, 그 외에는 undefined입니다. |
viewerState | PageViewerState | undefined | 준비된 동안의 현재 표시 스냅샷입니다. |
goToPage(page), nextPage(), previousPage() | void | 확정된 전역 페이지 순서 안에서 이동합니다. |
setZoom(zoom) | void | 표시 배율을 반올림하고 범위로 제한합니다. |
setMode(mode) | void | continuous, single, spread를 받습니다. |
setSpreadCover(cover) | void | spread 표지 배치를 바꿉니다. |
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: "ko" },
entries: [{ id: "intro", title: "소개", html: "<h1>소개</h1>" }],
}}
/>;필수 prop인 snapshot은 PublicationSnapshot을 받습니다. 선택 prop은 snapshotRevision, publicationOptions, publicationOptionsRevision, viewerOptions(reader 제외), readerOptions(controller 제외), className, style, onReady, onError, onStateChange입니다. revision prop은 이름 그대로 갱신과 재마운트를 강제합니다. Reader와 컨트롤러 연결은 컴포넌트가 직접 제공하며, reader와 controller가 옵션 prop에서 제외된 이유입니다.
ImposiaPublicationViewerHandle
current와 resolveDestination을 제외한 모든 동작은 언마운트 후 호출하면 예외가 발생합니다. Reader 동작은 Publication Reader가 준비되기 전에 호출해도 예외가 발생합니다.
| 멤버 | 반환값 | 동작과 제약 |
|---|---|---|
current | PublicationDocument | undefined | 마운트 중의 현재 확정된 문서입니다. |
resolveDestination(id) | PublicationDestination | undefined | 확정 전, 언마운트 후, 알 수 없는 ID에는 undefined입니다. |
navigate(destination) | void | 이전 세대의 목적지에는 예외가 발생합니다. |
openTableOfContents(), closeTableOfContents(), toggleTableOfContents() | void | 목차(outline) 패널을 제어합니다. |
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;두 훅은 hostRef, 수명 주기 state, 그리고 마운트 effect가 만들기 전까지 undefined인 컨트롤러를 반환합니다. hostRef는 div 하나에 연결하세요. loading이나 error 상태에서도 마지막으로 확정된 문서 또는 Publication이 남아 있을 수 있습니다.
useImposiaDocument는 페이지 컴포넌트의 source, revision, 옵션, 콜백을 받습니다. HTML 원본은 HTML이나 기준 URL이 바뀔 때 갱신되고, light DOM 원본은 노드 정체성이 바뀔 때 갱신됩니다. sourceRevision은 갱신을 강제하고, documentOptionsRevision은 컨트롤러를 다시 마운트합니다.
useImposiaPublication은 Publication 컴포넌트의 snapshot, revision, 옵션, 콜백을 받습니다. 스냅샷 참조나 snapshotRevision이 바뀔 때 갱신되고, publicationOptionsRevision은 컨트롤러를 다시 마운트합니다. 두 훅 모두 정리 단계에서 컨트롤러를 제거하고 AbortError를 수명 주기 취소로 봅니다.
function Article({ html }: { html: string }) {
const { hostRef, state } = useImposiaDocument({ source: { html } });
return <div ref={hostRef} aria-busy={state.status === "loading"} />;
}