読み取り専用のビューアー
注文や申請の添付ファイルのように、読んでほしいが変更されたくない文書があります。この例では、それらを読み取り専用のビューアーで 1 つずつ表示し、リストで切り替え、ボタンでダウンロードできるようにします。
html
<select id="documents">
<option value="0">請求書</option>
<option value="1">納品書</option>
</select>
<div id="viewer" style="height: 480px"></div>ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const documents = [
{ src: '/orders/42/invoice.jpg', alt: '請求書' },
{ src: '/orders/42/delivery-note.jpg', alt: '納品書' },
]
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
...documents[0],
editable: false,
download: true,
wheelZoom: false,
})
document.querySelector('#documents')!.addEventListener('change', (event) => {
const index = Number((event.target as HTMLSelectElement).value)
pic.setOptions(documents[index]!)
})補足
editable: falseでは、ツールバーに「編集」ボタンがなく、回転ボタンは表示中の画像を回すだけです。download: trueは、文書をそのまま元のファイル名で保存するボタンを加えます。downloadイベントを使えば、代わりにサーバーから提供できます。wheelZoom: falseにするとホイールでページがスクロールするため、長いページの中のビューアーに向いています。- 新しい
srcを渡すと次の文書が読み込まれ、ビューアーにフィットします。