npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toralab/tora-viewer

v0.2.0

Published

漫画や写真集などの画像一覧を閲覧できるビューアーを作成できます

Downloads

7

Readme

Tora Viewer

npm version CI

漫画や写真集などの画像一覧を閲覧できるビューアーを作成できます。

依存ライブラリが必要ないためどんな構成にも組み込みやすいです。

デモ

インストール

npm i @toralab/tora-viewer

またはCDNが使えます。

<script src="https://cdn.jsdelivr.net/npm/@toralab/tora-viewer"></script>

利用方法

基本的な使い方は以下の通りです。第1引数に画像URLの配列を渡します。

import toraViewer from '@toralab/tora-viewer';

toraViewer([
  'https://example.com/1.png',
  'https://example.com/2.png',
  'https://example.com/3.png',
  'https://example.com/4.png',
]);

CDN利用の場合

<script src="https://cdn.jsdelivr.net/npm/@toralab/tora-viewer"></script>
<script>
toraViewer([
  'https://example.com/1.png',
  'https://example.com/2.png',
  'https://example.com/3.png',
  'https://example.com/4.png',
]);
</script>

オプション(第2引数)

第2引数にビューアーの細かい設定を入れることができます。

toraViewer([
  'https://example.com/1.png',
  'https://example.com/2.png',
  'https://example.com/3.png',
  'https://example.com/4.png',
], {
  // 上部に表示されるタイトル(無指定なら何も表示しない)
  title: '漫画のタイトル',
  // 1ページのサイズ
  pageSize: {
    width: 840, // デフォルト値: 840
    height: 1188, // デフォルト値: 1188
  },
  // 「設定: ページ表示」の初期値
  // 次のいずれかを設定できる 'normal', 'spread', 'auto'
  // 'normal'(1ページ) 1ページづつ表示
  // 'spread'(見開き) 2ページづつ表示
  // 'auto'(自動) 画面サイズにより1ページ・見開きを自動的に切り替える(デフォルト値)
  pageStyle: 'auto',
  // 「設定: 読む方向」の初期値
  // 次のいずれかを設定できる 'horizontal-ltr', 'horizontal-rtl'
  // 'horizontal-ltr'(左から右)
  // 'horizontal-rtl'(右から左) (デフォルト値)
  direction: 'horizontal-rtl',
  // タイトルやページ遷移ボタンの非表示になるまでの時間(ミリ秒)
  // 0を指定すると時間経過で非表示にならなくなる。
  // デフォルト値: 3000(ms)
  controlShowTime: 3000,
  // 最後のページ要素
  // 最後のページを自由に埋め込むことができる。
  lastPageElement: document.getElementById('#last'),
  // 挿入する要素
  // 指定すると全画面表示ではなくなり、指定された要素に表示される。
  insertTarget: document.getElementById('#embed')
});

第1引数の指定の仕方

第1引数には画像URLの配列以外にオブジェクト配列やPromise配列が利用できます。(下で示す例のように混在も可能です。)

import toraViewer from '@toralab/tora-viewer';

toraViewer([
  'https://example.com/1.png',
  {
    url: 'https://example.com/2.png',
    thumbnailUrl: 'https://example.com/2-thumb.png',
  },
  Promise.resolve('https://example.com/3.png'),
  Promise.resolve({
    url: 'https://example.com/3.png',
    thumbnailUrl: 'https://example.com/3-thumb.png',
  }),
]);

Objectで指定できるthumbnailUrlはサムネイル画像はスライダーでページ遷移時に表示されます。thumbnailUrlを指定しなかった場合、urlで指定した画像がサムネイル画像として利用されます。(文字列で指定した場合も同様です)

画像のURL一覧を少しづつ読み込みたい場合はtoraViewer.asyncLoadBuilderを使うことができます。

const asyncImages = toraViewer.asyncLoadBuilder(function (limit, offset) {
  // 必要なタイミングで関数が実行されlimit,offsetが渡されるので画像URL一覧取得処理を実行する

  const url = `https://example.com/api/page.json?limit=${limit}&offset=${offset}`;
  // APIのレスポンスBody例1.
  // [
  //   "https://example.com/1.png",
  //   "https://example.com/2.png",
  //   "https://example.com/3.png",
  //   ...
  //   "https://example.com/20.png"
  // ]
  //
  // APIのレスポンスBody例2.
  // [
  //   { "url": "https://example.com/1.png", "thumbnailUrl": "https://example.com/1-thumb.png" },
  //   { "url": "https://example.com/2.png", "thumbnailUrl": "https://example.com/2-thumb.png" },
  //   { "url": "https://example.com/3.png", "thumbnailUrl": "https://example.com/3-thumb.png" },
  //   ...
  //   { "url": "https://example.com/20.png", "thumbnailUrl": "https://example.com/20-thumb.png" }
  // ]
  return fetch(url).then(function (res) {
    return res.json();
  })
})
// 1回の処理で読み込むページ数を指定する。省略可(デフォルト値:20)
.limit(20)
// 全ページ数を指定する
.build(100);
toraViewer(asyncImages);

async/awaitが使える場合は下記のように直すことができます。

const asyncImages = toraViewer.asyncLoadBuilder(async function (limit, offset) {
  // 必要なタイミングで関数が実行されlimit,offsetが渡されるので画像URL一覧取得処理を実行する
  const url = `https://example.com/api/page.json?limit=${limit}&offset=${offset}`;
  const res = await fetch(url)
  return await res.json();
})
// 1回の処理で読み込むページ数を指定する。省略可(デフォルト値:20)
.limit(20)
// 全ページ数を指定する
.build(100);
toraViewer(asyncImages);

メソッド

生成時にインスタンスを変数で受け取って、ビューアーを操作できます。

const viewer = toraViewer([
  'https://example.com/1.png',
  'https://example.com/2.png',
  'https://example.com/3.png',
  'https://example.com/4.png',
]);

// 次のページ(「設定: 読む方向」が「右から左」なら左のページ、「左から右」なら右のページ)
viewer.goNext();
// 前のページ(「設定: 読む方向」が「左から右」なら右のページ、「右から左」なら左のページ)
viewer.goPrev();
// 左のページに移動
viewer.goLeft();
// 右のページに移動
viewer.goRight();

// 指定のIndexに移動(0始まりなので例の場合は3ページ目に移動)
viewer.goTo(2);

// 設定を開く
viewer.openSettings();
// 設定を閉じる
viewer.closeSettings();

// 現在のindexを表示(0始まりなので現在3ページ目の場合は2)
console.log(viewer.currentIndex); // -> 2
// 読み終りページにいればtrue ※コンテンツの最後とは異なる
console.log(viewer.isLastPage); // false

// ページが切り替わるたびに実行されるイベント
viewer.onCurrentIndexChanged(function (index) {
  console.log(index); // -> 2
});

利用技術

  • TypeScript, JSX
  • Vite
  • Font Awesome

将来的に導入するもの(現時点でできていないもの)

  • 各種文言のカスタマイズ
  • 使える設定のカスタマイズ
  • テーマの変更