Showing preview only (289K chars total). Download the full file or copy to clipboard to get everything.
Repository: phiresky/isbn-visualization
Branch: master
Commit: 33f20b151b0c
Files: 68
Total size: 271.0 KB
Directory structure:
gitextract_ak0t4uty/
├── .dockerignore
├── .github/
│ └── workflows/
│ └── deploy.yml
├── .gitignore
├── .vscode/
│ ├── launch.json
│ └── settings.json
├── Dockerfile
├── LICENSE.md
├── README.md
├── eslint.config.mjs
├── flake.nix
├── index.html
├── package.json
├── scripts/
│ ├── gen-book-titles-sqlite.ts
│ ├── gen-prefixes.ts
│ ├── merge-stats.ts
│ ├── minify-images.sh
│ ├── minify-prefix-data.sh
│ ├── process-all.sh
│ ├── rarity/
│ │ ├── .gitignore
│ │ ├── Cargo.toml
│ │ └── src/
│ │ └── main.rs
│ ├── write-images/
│ │ ├── ImageTiler.ts
│ │ ├── index.ts
│ │ └── modules/
│ │ ├── aggregate-dense.ts
│ │ ├── index.ts
│ │ ├── publication_date.ts
│ │ ├── publishers.ts
│ │ ├── rarity.ts
│ │ └── single-sparse.ts
│ └── write-titles.ts
├── src/
│ ├── App.tsx
│ ├── components/
│ │ ├── Controls.tsx
│ │ ├── EanBarcode.tsx
│ │ ├── Highlight.tsx
│ │ ├── ImageTree.tsx
│ │ ├── IsbnGrid.tsx
│ │ ├── IsbnMap.tsx
│ │ ├── Legend.tsx
│ │ ├── MiniMap.tsx
│ │ ├── SingleBookCover.tsx
│ │ ├── StatsShow.tsx
│ │ └── TextTree.tsx
│ ├── config.ts
│ ├── index.css
│ ├── index.tsx
│ ├── lib/
│ │ ├── DetailLevelObservable.ts
│ │ ├── ImageLoader.ts
│ │ ├── RuntimeConfiguration.ts
│ │ ├── Store.ts
│ │ ├── TitleFetcher.ts
│ │ ├── delayRender.ts
│ │ ├── flight.ts
│ │ ├── google-books.ts
│ │ ├── info-map.ts
│ │ ├── json-fetch.ts
│ │ ├── prefix-data.ts
│ │ ├── shader-error.ts
│ │ ├── shaders.ts
│ │ ├── stats.ts
│ │ ├── types-select.d.ts
│ │ ├── types.d.ts
│ │ ├── util.ts
│ │ └── view-utils.ts
│ └── projections/
│ ├── bookshelf.ts
│ ├── index.ts
│ └── linear.ts
├── tsconfig.json
└── vite.config.ts
================================================
FILE CONTENTS
================================================
================================================
FILE: .dockerignore
================================================
dist
public
data
scripts/rarity/target
node_modules
================================================
FILE: .github/workflows/deploy.yml
================================================
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["master"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: true
jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm run build
env:
PUBLIC_BASE_PATH: /isbn-visualization
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
# Upload dist folder
path: "./dist"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
================================================
FILE: .gitignore
================================================
node_modules
dist
data
notes.md
/public/prefix-data
/public/images
/public
================================================
FILE: .vscode/launch.json
================================================
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "tsx",
"type": "node",
"request": "launch",
// Debug current file in VSCode
"program": "${file}",
/*
* Path to tsx binary
* Assuming locally installed
*/
"runtimeExecutable": "tsx",
/*
* Open terminal when debugging starts (Optional)
* Useful to see console.logs
*/
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
// Files to exclude from debugger (e.g. call stack)
"skipFiles": [
// Node.js internal core modules
"<node_internals>/**",
// Ignore all dependencies (optional)
"${workspaceFolder}/node_modules/**"
],
"args": ["publication_date", "3"]
}
]
}
================================================
FILE: .vscode/settings.json
================================================
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"data": true
}
}
================================================
FILE: Dockerfile
================================================
# Build rust
FROM rust:1.85 AS rust-builder
RUN apt-get update && apt-get install -y cmake
ADD scripts/rarity /app/scripts/rarity
WORKDIR /app/scripts/rarity
RUN cargo build --release
FROM rust:1.85 AS oxipng
RUN cargo install oxipng
# pnpm base
FROM node:22-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
FROM base AS prod-deps
COPY . /app
WORKDIR /app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# clean result
FROM base
RUN apt-get update && apt-get install -y pngquant zopfli pv zstd
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=rust-builder /app/scripts/rarity/target/release/rarity /app/scripts/rarity/target/release/rarity
COPY --from=oxipng /usr/local/cargo/bin/oxipng /usr/bin/oxipng
COPY . /app
WORKDIR /app
RUN pnpm -v # ensure pnpm is downloaded by corepack
CMD ["/app/scripts/process-all.sh"]
================================================
FILE: LICENSE.md
================================================
I like the concept of giving back, so I settled on the AGPL as the
default license for all my personal projects.
This isn't set in stone, so feel free to write me at
`phireskyde+git@gmail.com` if you need something else.
---
### GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright © 2007 Free Software Foundation, Inc.
<<http://fsf.org/>>
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
### Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed to
take away your freedom to share and change the works. By contrast, our
General Public Licenses are intended to guarantee your freedom to share
and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not price.
Our General Public Licenses are designed to make sure that you have the
freedom to distribute copies of free software (and charge for them if
you wish), that you receive source code or can get it if you want it,
that you can change the software or use pieces of it in new free
programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights with
two steps: (1) assert copyright on the software, and (2) offer you this
License which gives you legal permission to copy, distribute and/or
modify the software.
A secondary benefit of defending all users' freedom is that improvements
made in alternate versions of the program, if they receive widespread
use, become available for other developers to incorporate. Many
developers of free software are heartened and encouraged by the
resulting cooperation. However, in the case of software used on network
servers, this result may fail to come about. The GNU General Public
License permits making a modified version and letting the public access
it on a server without ever releasing its source code to the public.
The GNU Affero General Public License is designed specifically to ensure
that, in such cases, the modified source code becomes available to the
community. It requires the operator of a network server to provide the
source code of the modified version running there to the users of that
server. Therefore, public use of a modified version, on a publicly
accessible server, gives the public access to the source code of the
modified version.
An older license, called the Affero General Public License and published
by Affero, was designed to accomplish similar goals. This is a different
license, not a version of the Affero GPL, but Affero has released a new
version of the Affero GPL which permits relicensing under this license.
The precise terms and conditions for copying, distribution and
modification follow.
### TERMS AND CONDITIONS
#### 0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public
License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based on
the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices" to
the extent that it includes a convenient and prominently visible feature
that (1) displays an appropriate copyright notice, and (2) tells the
user that there is no warranty for the work (except to the extent that
warranties are provided), that licensees may convey the work under this
License, and how to view a copy of this License. If the interface
presents a list of user commands or options, such as a menu, a prominent
item in the list meets this criterion.
#### 1. Source Code.
The "source code" for a work means the preferred form of the work for
making modifications to it. "Object code" means any non-source form of a
work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that is
widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that Major
Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A "Major
Component", in this context, means a major essential component (kernel,
window system, and so on) of the specific operating system (if any) on
which the executable work runs, or a compiler used to produce the work,
or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all the
source code needed to generate, install, and (for an executable work)
run the object code and to modify the work, including scripts to control
those activities. However, it does not include the work's System
Libraries, or general-purpose tools or generally available free programs
which are used unmodified in performing those activities but which are
not part of the work. For example, Corresponding Source includes
interface definition files associated with source files for the work,
and the source code for shared libraries and dynamically linked
subprograms that the work is specifically designed to require, such as
by intimate data communication or control flow between those subprograms
and other parts of the work.
The Corresponding Source need not include anything that users can
regenerate automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same
work.
#### 2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey,
without conditions so long as your license otherwise remains in force.
You may convey covered works to others for the sole purpose of having
them make modifications exclusively for you, or provide you with
facilities for running those works, provided that you comply with the
terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for
you must do so exclusively on your behalf, under your direction and
control, on terms that prohibit them from making any copies of your
copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under the
conditions stated below. Sublicensing is not allowed; section 10 makes
it unnecessary.
#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article 11
of the WIPO copyright treaty adopted on 20 December 1996, or similar
laws prohibiting or restricting circumvention of such measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to the
covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
#### 4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice; keep
intact all notices stating that this License and any non-permissive
terms added in accord with section 7 apply to the code; keep intact all
notices of the absence of any warranty; and give all recipients a copy
of this License along with the Program.
You may charge any price or no price for each copy that you convey, and
you may offer support or warranty protection for a fee.
#### 5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the terms
of section 4, provided that you also meet all of these conditions:
- a\) The work must carry prominent notices stating that you modified it,
and giving a relevant date.
- b\) The work must carry prominent notices stating that it is released
under this License and any conditions added under section 7. This
requirement modifies the requirement in section 4 to "keep intact
all notices".
- c\) You must license the entire work, as a whole, under this License to
anyone who comes into possession of a copy. This License will therefore
apply, along with any applicable section 7 additional terms, to the
whole of the work, and all its parts, regardless of how they
are packaged. This License gives no permission to license the work in
any other way, but it does not invalidate such permission if you have
separately received it.
- d\) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your work need
not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work, and
which are not combined with it such as to form a larger program, in or
on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not used
to limit the access or legal rights of the compilation's users beyond
what the individual works permit. Inclusion of a covered work in an
aggregate does not cause this License to apply to the other parts of the
aggregate.
#### 6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of
sections 4 and 5, provided that you also convey the machine-readable
Corresponding Source under the terms of this License, in one of these
ways:
- a\) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium customarily used
for software interchange.
- b\) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a written
offer, valid for at least three years and valid for as long as you offer
spare parts or customer support for that product model, to give anyone
who possesses the object code either (1) a copy of the Corresponding
Source for all the software in the product that is covered by this
License, on a durable physical medium customarily used for software
interchange, for a price no more than your reasonable cost of physically
performing this conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
- c\) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This alternative is
allowed only occasionally and noncommercially, and only if you received
the object code with such an offer, in accord with subsection 6b.
- d\) Convey the object code by offering access from a designated place
(gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to copy
the object code is a network server, the Corresponding Source may be on
a different server (operated by you or a third party) that supports
equivalent copying facilities, provided you maintain clear directions
next to the object code saying where to find the Corresponding Source.
Regardless of what server hosts the Corresponding Source, you remain
obligated to ensure that it is available for as long as needed to
satisfy these requirements.
- e\) Convey the object code using peer-to-peer transmission, provided you
inform other peers where the object code and Corresponding Source of the
work are being offered to the general public at no charge under
subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be included
in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for
incorporation into a dwelling. In determining whether a product is a
consumer product, doubtful cases shall be resolved in favor of coverage.
For a particular product received by a particular user, "normally used"
refers to a typical or common use of that class of product, regardless
of the status of the particular user or of the way in which the
particular user actually uses, or expects or is expected to use, the
product. A product is a consumer product regardless of whether the
product has substantial commercial, industrial or non-consumer uses,
unless such uses represent the only significant mode of use of the
product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product
from a modified version of its Corresponding Source. The information
must suffice to ensure that the continued functioning of the modified
object code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied by
the Installation Information. But this requirement does not apply if
neither you nor any third party retains the ability to install modified
object code on the User Product (for example, the work has been
installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in
accord with this section must be in a format that is publicly documented
(and with an implementation available to the public in source code
form), and must require no special password or key for unpacking,
reading or copying.
#### 7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by this
License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove
any additional permissions from that copy, or from any part of it.
(Additional permissions may be written to require their own removal in
certain cases when you modify the work.) You may place additional
permissions on material, added by you to a covered work, for which you
have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders
of that material) supplement the terms of this License with terms:
- a\) Disclaiming warranty or limiting liability differently from the terms
of sections 15 and 16 of this License; or
- b\) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal Notices
displayed by works containing it; or
- c\) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
- d\) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
- e\) Declining to grant rights under trademark law for use of some trade
names, trademarks, or service marks; or
- f\) Requiring indemnification of licensors and authors of that material
by anyone who conveys the material (or modified versions of it) with
contractual assumptions of liability to the recipient, for any liability
that these contractual assumptions directly impose on those licensors
and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains a
further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms of
that license document, provided that the further restriction does not
survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you must
place, in the relevant source files, a statement of the additional terms
that apply to those files, or a notice indicating where to find the
applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions; the above
requirements apply either way.
#### 8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally,
unless and until the copyright holder explicitly and finally terminates
your license, and (b) permanently, if the copyright holder fails to
notify you of the violation by some reasonable means prior to 60 days
after the cessation.
Moreover, your license from a particular copyright holder is reinstated
permanently if the copyright holder notifies you of the violation by
some reasonable means, this is the first time you have received notice
of violation of this License (for any work) from that copyright holder,
and you cure the violation prior to 30 days after your receipt of the
notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
#### 9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or run a
copy of the Program. Ancillary propagation of a covered work occurring
solely as a consequence of using peer-to-peer transmission to receive a
copy likewise does not require acceptance. However, nothing other than
this License grants you permission to propagate or modify any covered
work. These actions infringe copyright if you do not accept this
License. Therefore, by modifying or propagating a covered work, you
indicate your acceptance of this License to do so.
#### 10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered work
results from an entity transaction, each party to that transaction who
receives a copy of the work also receives whatever licenses to the work
the party's predecessor in interest had or could give under the previous
paragraph, plus a right to possession of the Corresponding Source of the
work from the predecessor in interest, if the predecessor has it or can
get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may not
impose a license fee, royalty, or other charge for exercise of rights
granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that any
patent claim is infringed by making, using, selling, offering for sale,
or importing the Program or any portion of it.
#### 11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The work
thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims owned or
controlled by the contributor, whether already acquired or hereafter
acquired, that would be infringed by some manner, permitted by this
License, of making, using, or selling its contributor version, but do
not include claims that would be infringed only as a consequence of
further modification of the contributor version. For purposes of this
definition, "control" includes the right to grant patent sublicenses in
a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to make,
use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license, and
the Corresponding Source of the work is not available for anyone to
copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify or
convey a specific copy of the covered work, then the patent license you
grant is automatically extended to all recipients of the covered work
and works based on it.
A patent license is "discriminatory" if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on
the non-exercise of one or more of the rights that are specifically
granted under this License. You may not convey a covered work if you are
a party to an arrangement with a third party that is in the business of
distributing software, under which you make payment to the third party
based on the extent of your activity of conveying the work, and under
which the third party grants, to any of the parties who would receive
the covered work from you, a discriminatory patent license (a) in
connection with copies of the covered work conveyed by you (or copies
made from those copies), or (b) primarily for and in connection with
specific products or compilations that contain the covered work, unless
you entered into that arrangement, or that patent license was granted,
prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any
implied license or other defenses to infringement that may otherwise be
available to you under applicable patent law.
#### 12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not convey it at all. For example, if you agree to terms that
obligate you to collect a royalty for further conveying from those to
whom you convey the Program, the only way you could satisfy both those
terms and this License would be to refrain entirely from conveying the
Program.
#### 13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have permission
to link or combine any covered work with a work licensed under version 3
of the GNU General Public License into a single combined work, and to
convey the resulting work. The terms of this License will continue to
apply to the part which is the covered work, but the work with which it
is combined will remain governed by version 3 of the GNU General Public
License.
#### 14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the option
of following the terms and conditions either of that numbered version or
of any later version published by the Free Software Foundation. If the
Program does not specify a version number of the GNU Affero General
Public License, you may choose any version ever published by the Free
Software Foundation.
If the Program specifies that a proxy can decide which future versions
of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different permissions.
However, no additional obligations are imposed on any author or
copyright holder as a result of your choosing to follow a later version.
#### 15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF
THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
#### 16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#### 17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided above
cannot be given local legal effect according to their terms, reviewing
courts shall apply local law that most closely approximates an absolute
waiver of all civil liability in connection with the Program, unless a
warranty or assumption of liability accompanies a copy of the Program in
return for a fee.
END OF TERMS AND CONDITIONS
### How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively state
the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. For more information on this, and how to apply and follow the
GNU AGPL, see <<http://www.gnu.org/licenses/>>.
================================================
FILE: README.md
================================================
# ISBN Visualization
**Please read https://phiresky.github.io/blog/2025/visualizing-all-books-in-isbn-space/ for the live version and description of this project**
Screenshots:


## Setup
Fetch the main repo and (if you want) the precomputed data.
```bash
# code
git clone git@github.com:phiresky/isbn-visualization.git
# precomputed prefix data
git clone git@github.com:phiresky/isbn-visualization-json.git
# precomputed png datasets
git clone git@github.com:phiresky/isbn-visualization-images.git
cd isbn-visualization
mkdir public
ln -s $PWD/../isbn-visualization-images public/images
ln -s $PWD/../isbn-visualization-json/prefix-data public/prefix-data
```
Then install the JS dependencies. You'll need [pnpm](https://pnpm.io/). The easiest way is corepack, which is bundled with nodejs:
```bash
corepack enable
pnpm install
# run in dev mode (WARNING: perf in dev mode is worse than prod mode!)
pnpm run dev
# build in prod mode
pnpm run build
# serve from any static http server (example)
cd dist && python3 -m http.server
```
## Preprocessing scripts
This repo contains a few scripts to generate the relevant data for the web viewer.
### Running in docker
You can build a docker container containing all relevant code using
```bash
docker build -t phiresky/isbn-visualization .
```
### `scripts/process-all.sh`
A convenience script to run the JS build and all processing steps that have not been run yet:
Inputs:
- PUBLIC_BASE_PATH: the url prefix you will host the project under (e.g. /isbn-visualization)
- DATA_DIR: the directory the input data files are in and intermediary products will be stored
- OUTPUT_DIR_PUBLIC: the output dir that you will host on your webhost (under PUBLIC_BASE_PATH)
Run in docker:
```bash
docker run --rm -it \
-e PUBLIC_BASE_PATH=/isbn-visualization \
-e DATA_DIR=/data \
-e OUTPUT_DIR_PUBLIC=/public \
-v ./data:/data \
-v ./public:/public \
phiresky/isbn-visualization
```
Directly:
```
PUBLIC_BASE_PATH=/ OUTPUT_DIR_PUBLIC=./public DATA_DIR=./data ./scripts/process-all.sh
```
### `scripts/gen-prefixes.ts`
This script generates the json files representing the groups/publisher ranges.
- Input: `isbngrp_records.jsonl.seekable.zst`
- Output: `public/prefix-data/*.json` (split by size), `data/prefix-data.json` (the full data)
```bash
pnpm tsx scripts/gen-prefixes.ts .../aa_meta__aacid__isbngrp_records__20240920T194930Z--20240920T194930Z.jsonl.seekable.zst
# compress them with zopfli (if you don't want to install zopfli, use `gzip -9 public/prefix-data/*.json`)
scripts/minify-prefix-data.sh
```
### `scripts/rarity`
This one written in Rust for performance. You'll need the [Rust compiler](https://www.rust-lang.org/).
- Input: aa_meta**aacid**worldcat\_\_20241230T203056Z--20241230T203056Z.jsonl.seekable.zst
- Output: `data/library_holding_data.sqlite3`
```bash
cd scripts/rarity
export RUSTFLAGS="-C target-cpu=native"
cargo run --release -- ~/Downloads/annas_archive_meta__aacid__worldcat__20241230T203056Z--20241230T203056Z.jsonl.seekable.zst
```
It takes 20min-1h to process the 250GByte source file.
### `scripts/write-images`
This script generates the png datasets.
Use `pnpm tsx scripts/write-images list` to list datasets:
```
Special datasets: [ 'publishers', 'all', 'rarity' ]
Normal datasets: [...depends on
]
```
The syntax is `pnpm tsx scripts/write-images [dataset] [zoom-level|all]`
Use all to generate all zoom levels from 1-4.
Input:
- for the `all` and normal datasets: `data/aa_isbn13_codes_20241204T185335Z.benc.zst` (or set env var `INPUT_BENC=path`)
- for the `publisher` dataset: `data/prefix-data.json` (generated by `scripts/gen-prefixes.ts`, or set env var `INPUT_PREFIX_DATA=path`)
- for the `rarity` dataset: `data/library_holding_data.sqlite3` (generated by `scripts/rarity` or set env var `INPUT_HOLDING_SQLITE=path`)
Output:
- `public/images/tiled/[dataset]/zoom-{1,2,3,4}/*.png`
- `public/images/tiled/[dataset]/written.json` with the list of images (only if zoom level=all)
- `public/images/tiled/[dataset]/stats.json`
```bash
# you might want to run some these in parallel, each takes a 1-10 minutes.
for dataset in all publishers rarity publication_date cadal_ssno cerlalc duxiu_ssid edsebk gbooks goodreads ia isbndb isbngrp libby md5 nexusstc nexusstc_download oclc ol rgb trantor; do
pnpm tsx scripts/write-images $dataset all
done
```
Special datasets:
#### Dataset `all`
Aggregates all datasets, sets white pixels for every book in any of the datasets, black pixels otherwise.
Zoomed out views contain the average, so a pixel with 50% existing books will be brightness 50%.
#### Dataset `publication_date`
The red in each pixel is the average publication year (minus 1800, clamped to 0-255). The green pixel is the same. The blue pixel is the ratio of books present in the dataset (255 = 100%).
#### Dataset `publishers`
Publishers are assigned an incrementing integer ID by unique `registrant_name`. This integer is stored in the PNG RGB: `publisherId = red * 65536 + green * 256 + blue`.
Zoomed out views contain non-aggregated data (publisher ranges smaller than a pixel will not appear).
#### Dataset `rarity`
The variables holdingCount, editionCount, bookCount are set in the r,g,b colors respectively.
Zoomed out views contain the sum of each of the values. If one of the values is ≥ 255, all values are scaled down accordingly. For example:
`r=4,g=2,b=1` means that there is exactly one book in this pixel with 4 holdings and 2 editions
`r=10,g=3,b=3` means there's three books with a total of 10 holdings and 3 editions
`r=10,g=3,b=255` means thre's more than 254 books, with on average `10/255` holdings per book, and `3/255` editions per book.
`r=255,g=10,b=30` means there's more than 254 holdings, with on average `255/10` holdings per edition and `255/30` books per edition
#### Other datasets
The other datasets contain the data directly from the benc file (white=exists, black=does not exist)
### `scripts/merge-stats.ts`
Merges the statistics from the different datasets into a single file.
- Input: `public/images/tiled/*/stats.json`
- Output: `public/prefix-data/stats.json`
```bash
pnpm tsx scripts/merge-stats.ts
```
### `scripts/minify-images.sh` (optional)
Minify the images using [oxipng](https://github.com/shssoichiro/oxipng) and [pngquant](https://pngquant.org/) (for lossy datasets).
This reduces image size by 5-50%!
```bash
scripts/minify-images.sh public/images/tiled/
# or
scripts/minify-images.sh public/images/tiled/[dataset]
```
## Running the main web viewer
URLs and paths are configured in `src/config.ts`. The default "advanced config", stored in the URL, is configured in `src/lib/RuntimeConfiguration.ts`.
Development: `pnpm run dev`
Runs the app in the development mode.<br>
Open [http://localhost:5173](http://localhost:5173) to view it in the browser.
The page will reload if you make edits.<br>
You can use the following debug objects exposed in the dev console:
- `store`: the main state store which can be manipulated, e.g. `store.showGrid = false`
- `threejsRoot`: the main threejs objects, e.g. `console.log(threejsRoot.camera.zoom)`
- `isbnlib`: the `isbn3` library for parsing ISBNs
### `pnpm run build`
Builds the app for production to the `dist` folder.<br>
It bundles the project in production mode and optimizes the build for the best performance.
If the app should not be hosted in the root path of a domain, set the env var e.g. `PUBLIC_BASE_PATH=/isbn-visualization`.
### Deployment
You can deploy the `dist` folder to any static host provider (netlify, surge, now, etc.)
================================================
FILE: eslint.config.mjs
================================================
// @ts-check
import eslint from "@eslint/js";
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
import tseslint from "typescript-eslint";
export default tseslint.config(
{
ignores: ["public/", "data/", "dist/", "node_modules/", "scripts/rarity/"],
},
eslint.configs.recommended,
tseslint.configs.strictTypeChecked,
tseslint.configs.stylisticTypeChecked,
eslintPluginPrettierRecommended,
{
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
],
"@typescript-eslint/no-unnecessary-condition": [
"error",
{ allowConstantLoopConditions: true },
],
},
},
);
================================================
FILE: flake.nix
================================================
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
#rust-overlay = {
# url = "github:oxalica/rust-overlay";
# inputs = {
# nixpkgs.follows = "nixpkgs";
# };
#};
};
outputs = { self, nixpkgs, flake-utils, /*rust-overlay*/ }:
flake-utils.lib.eachDefaultSystem
(system:
let
#overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system /*overlays*/;
};
#rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
#nativeBuildInputs = with pkgs; [ rustToolchain pkg-config wasm-bindgen-cli ];
buildInputs = with pkgs; [ pnpm openssl nodejs ];
in
with pkgs;
{
devShells.default = mkShell {
# 👇 and now we can just inherit them
inherit buildInputs /*nativeBuildInputs*/;
# shellHook = ''
# # For rust-analyzer 'hover' tooltips to work.
# export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc}
# '';
};
}
);
}
================================================
FILE: index.html
================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" type="image/png" href="/src/assets/favicon.png" />
<title>ISBN Visualization</title>
<meta property="og:image" content="/src/assets/screenshot2.png" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="/src/index.tsx" type="module"></script>
</body>
</html>
================================================
FILE: package.json
================================================
{
"name": "@phiresky/isbn-visualization",
"version": "0.0.0",
"description": "",
"type": "module",
"scripts": {
"lint": "tsc && eslint .",
"start": "vite",
"dev": "vite",
"build": "pnpm lint && vite build",
"serve": "vite preview"
},
"license": "MIT",
"devDependencies": {
"@eslint/js": "^9.21.0",
"eslint": "^9.21.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.3",
"prettier": "^3.5.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.24.1",
"vite": "^6.1.1"
},
"dependencies": {
"@react-three/drei": "^10.0.1",
"@react-three/fiber": "9.0.4",
"@types/bencode": "^2.0.4",
"@types/better-sqlite3": "^7.6.12",
"@types/node": "^22.13.5",
"@types/react": "^19.0.10",
"@types/react-dom": "^19.0.4",
"@types/three": "^0.173.0",
"@vitejs/plugin-react-swc": "^3.8.0",
"bencode": "^4.0.0",
"better-sqlite3": "^11.8.1",
"isbn3": "^1.2.7",
"jsbarcode": "^3.11.6",
"lru-cache": "^11.0.2",
"mobx": "^6.13.6",
"mobx-react-lite": "^4.1.0",
"mobx-utils": "^6.1.0",
"prando": "^6.0.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-select": "^5.10.0",
"sharp": "^0.33.5",
"simple-zstd": "^1.4.2",
"three": "^0.173.0",
"tsx": "^4.19.3",
"zlib": "^1.0.5"
},
"packageManager": "pnpm@10.5.0+sha512.11106a5916c7406fe4b8cb8e3067974b8728f47308a4f5ac5e850304afa6f57e2847d7950dfe78877d8d36bfb401d381c4215db3a4c3547ffa63c14333a6fa51",
"pnpm": {
"onlyBuiltDependencies": [
"@swc/core",
"better-sqlite3",
"esbuild",
"sharp"
]
},
"prettier": {}
}
================================================
FILE: scripts/gen-book-titles-sqlite.ts
================================================
import sqlite from "better-sqlite3";
import { createReadStream } from "fs";
import fs from "fs/promises";
import readline from "readline";
import zlib from "zlib";
interface Record {
_index: "aarecords__9";
_id: string;
_source: {
id: "string";
file_unified_data: {
title_best: string;
author_best: string;
publisher_best: string;
identifiers_unified: {
aarecord_id: string[];
md5?: string[];
sha1?: string[];
isbn10?: string[];
isbn13?: string[];
};
};
};
}
function connect(dbName: string) {
const db = sqlite(dbName);
// enable wal mode
db.prepare("PRAGMA journal_mode = WAL").run();
// disable synchronous
db.prepare("PRAGMA synchronous = OFF").run();
// create table isbns (isbn13, book_id), books (book_id, publisher, author, title)
db.prepare(
"CREATE TABLE IF NOT EXISTS books (book_id INTEGER PRIMARY KEY, publisher TEXT, author TEXT, title TEXT)",
).run();
db.prepare(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_books_publisher_author_title ON books (publisher, author, title)",
).run();
db.prepare(
"CREATE TABLE IF NOT EXISTS isbns (isbn13 INTEGER, book_id INTEGER REFERENCES books(book_id), primary key (isbn13, book_id))",
).run();
return db;
}
async function load(dbName: string, dataDir: string) {
const db = connect(dbName);
// readdir, find all dataDir/aarecords__*.json.gz
const files = (await fs.readdir(dataDir)).filter((f) =>
/^aarecords__[^.]+\.json\.gz$/.exec(f),
);
for (const file of files) {
console.log(`Loading ${file}`);
// stream read gzipped jsonl file
const stream = createReadStream(`${dataDir}/${file}`);
const gunzip = zlib.createGunzip();
const rl = readline.createInterface({
input: stream.pipe(gunzip),
crlfDelay: Infinity,
});
// insert or return id
const book = db.prepare<[string, string, string], { book_id: number }>(
"INSERT INTO books (publisher, author, title) VALUES (?, ?, ?) ON CONFLICT (publisher, author, title) DO UPDATE SET publisher = excluded.publisher RETURNING book_id",
);
const isbns = db.prepare(
"INSERT OR IGNORE INTO isbns (isbn13, book_id) VALUES (?, ?)",
);
db.exec("BEGIN TRANSACTION");
for await (const line of rl) {
// parse json
const record = JSON.parse(line) as Record;
// insert into books
const { title_best, author_best, publisher_best } =
record._source.file_unified_data;
const { isbn13 = [], isbn10 } =
record._source.file_unified_data.identifiers_unified;
if (!title_best) {
// console.log(`No title for ${aarecord_id[0]}`);
continue;
}
const rop = book.get(publisher_best, author_best, title_best);
if (!rop) throw new Error("book.get failed");
const book_id = rop.book_id;
if (isbn13.length === 0) {
// console.log(`No ISBN for ${aarecord_id[0]} ${title_best}`);
if (isbn10?.length) console.log(`no isbn13, but has isbn10: ${isbn10}`);
}
// insert into isbns
for (const isbn of isbn13) {
isbns.run(isbn, book_id);
}
}
db.exec("END TRANSACTION");
}
}
// cmdline args
const dbName = process.argv[2];
const dataDir = process.argv[3];
if (!dbName || !dataDir) {
console.error("Usage: gen-sqlite <db-name> <data-dir>");
process.exit(1);
}
void load(dbName, dataDir);
================================================
FILE: scripts/gen-prefixes.ts
================================================
import { createReadStream } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { createInterface } from "node:readline";
import { ZSTDDecompress } from "simple-zstd";
import {
addRecord,
Digit,
InfoMap,
LazyInfoMap,
PrefixInfo,
} from "../src/lib/info-map";
import { addIsbnGroups } from "../src/lib/prefix-data";
import { IsbnPrefixWithDashes } from "../src/lib/util";
interface JsonRecord {
aacid: string;
metadata: {
id: string;
record: {
registrant_name: "foo";
agency_name: "New Zealand";
country_name: "New Zealand";
isbns: [
{ isbn: IsbnPrefixWithDashes; isbn_type: "prefix" },
{ isbn: "..."; isbn_type: "isbn13" },
];
};
};
}
async function go() {
const fname = process.argv[2];
if (!fname) throw new Error("no input filename provided");
const map: InfoMap = {};
let recordCount = 0;
for await (const line of createInterface(
createReadStream(fname).pipe(ZSTDDecompress()),
)) {
const obj = JSON.parse(line) as JsonRecord;
if (recordCount % 100000 === 0)
console.log(`${recordCount}/2700000 records...`);
recordCount++;
for (const isbn of obj.metadata.record.isbns) {
if (isbn.isbn_type === "prefix") {
// console.log(isbn.isbn);
// if (isbn.isbn.length > 9) continue;
const r = obj.metadata.record;
addRecord(map, isbn.isbn, {
// id: obj.metadata.id,
registrant_name: r.registrant_name,
agency_name: r.agency_name,
country_name: r.country_name,
source: "isbngrp",
prefix: isbn.isbn,
});
}
}
}
addIsbnGroups(map, {
testMode: false,
addUnassigned: true,
});
const maxDepth = 7;
const maxInlineDeepChildren = 10;
const outDir = (process.env.OUTPUT_DIR_PUBLIC ?? "public") + "/prefix-data";
const outFileFull = (process.env.DATA_DIR ?? "data") + "/prefix-data.json";
let nextPublisherId = 1;
let nextGroupId = 1;
const publishersIdCache = new Map<string, number>();
function countUniquePublishers(map: InfoMap): Set<string> {
const out = new Set<string>();
for (const [_digit, info] of Object.entries(map) as [Digit, PrefixInfo][]) {
if (info.children) {
const children = countUniquePublishers(info.children);
info.totalChildren = children.size;
for (const child of children) {
out.add(child);
}
}
if (info.info) {
for (const record of info.info) {
if (record.source === "isbngrp") {
out.add(record.registrant_name);
}
}
}
}
return out;
}
countUniquePublishers(map);
function recurseAssignNumericIds(map: InfoMap) {
for (const [_digit, info] of Object.entries(map) as [Digit, PrefixInfo][]) {
if (info.info) {
const record = info.info[0];
if (record.source === "isbngrp") {
const cached = publishersIdCache.get(record.registrant_name);
if (cached) {
record.numericId = cached;
} else {
record.numericId = nextPublisherId++;
publishersIdCache.set(record.registrant_name, record.numericId);
}
} else {
if (record.name !== "Unassigned") {
record.numericId = nextGroupId++;
}
}
}
if (info.children) {
recurseAssignNumericIds(info.children);
}
}
}
recurseAssignNumericIds(map);
console.log(
`assigned ${nextPublisherId} publisher ids, ${nextGroupId} group ids`,
);
async function recurseOrRemoveAndWrite(
layer: InfoMap,
depth: number,
prefix: string,
): Promise<LazyInfoMap> {
await mkdir(outDir, { recursive: true });
if (depth >= maxDepth && Object.keys(layer).length) {
const fname = `${prefix}.json`;
await writeFile(`${outDir}/${fname}`, JSON.stringify(layer));
return { lazy: fname };
} else {
const out: LazyInfoMap = {};
for (const [digit, info] of Object.entries(layer) as [
Digit,
PrefixInfo,
][]) {
out[digit] = {
...info,
children:
info.totalChildren <= maxInlineDeepChildren
? info.children
: await recurseOrRemoveAndWrite(
info.children ?? {},
depth + 1,
`${prefix}${digit}`,
),
};
}
return out;
}
}
await writeFile(outFileFull, JSON.stringify(map));
console.log(`wrote ${recordCount} records to ${outFileFull}`);
const lazyMap = await recurseOrRemoveAndWrite(map, 0, "");
await writeFile(`${outDir}/root.json`, JSON.stringify(lazyMap));
console.log(`wrote lazy map to ${outDir}/root.json`);
}
void go();
================================================
FILE: scripts/merge-stats.ts
================================================
import { readFileSync, writeFileSync } from "fs";
import { mergeStats, StatsMap } from "../src/lib/stats";
import { IsbnPrefixWithoutDashes } from "../src/lib/util";
const dir = process.env.OUTPUT_DIR_PUBLIC ?? "public";
const out: StatsMap = {};
for (const dataset of ["all", "publication_date", "rarity", "publishers"]) {
const f = JSON.parse(
readFileSync(`${dir}/images/tiled/${dataset}/stats.json`, "utf-8"),
) as StatsMap;
for (const k of Object.keys(f) as IsbnPrefixWithoutDashes[]) {
if (out[k]) {
const v = f[k];
if (v === undefined) continue;
mergeStats(out[k], v);
} else out[k] = f[k];
}
}
const outFile = `${dir}/prefix-data/stats.json`;
console.log(`Writing to ${outFile}`);
writeFileSync(outFile, JSON.stringify(out));
================================================
FILE: scripts/minify-images.sh
================================================
#!/bin/bash
set -euo pipefail
lines="$(find "$1" -name '*.png' | wc -l)"
find "$1" -name '*.png' | sort | pv -l --size=$lines | while read f; do
if [[ ! -f "$f.timestamp" ]] || [[ "$f" -nt "$f.timestamp" ]] ; then
echo -n "Re-compressing $f "
cp "$f" "$f.orig" --preserve=all
# if in rarity or publishers dir, don't quantize (lossy)
if [[ "$f" == *"/rarity/"* ]] || [[ "$f" == *"/publishers/"* ]] || [[ "$f" == *"/publication_date/zoom-4"* ]]; then
echo losslessly...
true
else
echo lossily...
pngquant "$f" --ext .png --skip-if-larger --force || true
fi
oxipng "$f" -r -o max --strip all
touch "$f.timestamp"
fi
done
================================================
FILE: scripts/minify-prefix-data.sh
================================================
#!/bin/bash
set -euo pipefail
JOBS="${JOBS:-$(nproc)}"
OUTPUT_DIR_PUBLIC="${OUTPUT_DIR_PUBLIC:-public}"
echo compressing files in $OUTPUT_DIR_PUBLIC/prefix-data with zopfli using $JOBS threads
for f in $OUTPUT_DIR_PUBLIC/prefix-data/*.json; do
(
# .. do your stuff here
echo "zopfli $f.."
zopfli "$f" && rm "$f"
) &
# allow to execute up to $N jobs in parallel
while [[ $(jobs -r -p | wc -l) -ge $JOBS ]]; do
# now there are $N jobs already running, so wait here for any job
# to be finished so there is a place to start next one.
wait -n
done
done
# no more jobs to be started but wait for pending jobs
# (all need to be finished)
wait
echo "all done"
================================================
FILE: scripts/process-all.sh
================================================
#!/bin/bash
set -euo pipefail
# for each env var, check if file exists and make path absolute
# default INPUT_ISBNGRP_DUMP to DATA_DIR/aa_meta__aacid__isbngrp_records__20240920T194930Z--20240920T194930Z.jsonl.seekable.zst
INPUT_ISBNGRP_DUMP="${INPUT_ISBNGRP_DUMP:-"$DATA_DIR/annas_archive_meta__aacid__isbngrp_records__20240920T194930Z--20240920T194930Z.jsonl.seekable.zst"}"
INPUT_WORLDCAT_DUMP="${INPUT_WORLDCAT_DUMP:-"$DATA_DIR/annas_archive_meta__aacid__worldcat__20241230T203056Z--20241230T203056Z.jsonl.seekable.zst"}"
INPUT_BENC="${INPUT_BENC:-"$DATA_DIR/aa_isbn13_codes_20241204T185335Z.benc.zst"}"
# annas_archive_meta__aacid__worldcat__20241230T203056Z--20241230T203056Z.jsonl.seekable.zst
for var in INPUT_ISBNGRP_DUMP INPUT_WORLDCAT_DUMP INPUT_BENC OUTPUT_DIR_PUBLIC DATA_DIR; do
if [ -z "${!var-}" ]; then
echo "Required env variable not set: $var"
exit 1
fi
if [ ! -f "${!var}" ] && [ ! -d "${!var}" ]; then
echo "File not found: ${!var} (from $var)"
exit 1
fi
export $var="$(realpath "${!var}")"
done
# go to repo root
cd "$(dirname "$0")/.."
# build web components to out dir
if [ ! -f "$OUTPUT_DIR_PUBLIC/index.html" ]; then
echo "Running pnpm build"
rm -rf "$OUTPUT_DIR_PUBLIC/assets" # ensure we don't have old assets
pnpm build
cp -r dist/* "$OUTPUT_DIR_PUBLIC/"
else
echo "Skipping pnpm build as $OUTPUT_DIR_PUBLIC/index.html already exists"
fi
# run only if DATA_DIR/prefix-data.json does not exist
if [ ! -f "$DATA_DIR/prefix-data.json" ]; then
echo "Running gen-prefixes.ts"
pnpm tsx scripts/gen-prefixes.ts "$INPUT_ISBNGRP_DUMP"
else
echo "Skipping gen-prefixes.ts as $DATA_DIR/prefix-data.json already exists"
fi
if [ ! -f "$OUTPUT_DIR_PUBLIC/prefix-data/root.json.gz" ]; then
echo "Running scripts/minify-prefix-data.sh"
scripts/minify-prefix-data.sh
else
echo "Skipping scripts/minify-prefix-data.sh as $OUTPUT_DIR_PUBLIC/prefix-data/root.json.gz already exists"
fi
# run only if DATA_DIR/library_holding_data.sqlite3 does not exist
if [ ! -f "$DATA_DIR/library_holding_data.sqlite3" ]; then
echo "Running scripts/rarity"
scripts/rarity/target/release/rarity "$INPUT_WORLDCAT_DUMP"
else
echo "Skipping scripts/rarity as $DATA_DIR/library_holding_data.sqlite3 already exists"
fi
JOBS="${JOBS:-$(nproc)}"
for dataset in all publishers rarity publication_date cadal_ssno cerlalc duxiu_ssid edsebk gbooks goodreads ia isbndb isbngrp libby md5 nexusstc nexusstc_download oclc ol rgb trantor; do
if [ ! -f "$OUTPUT_DIR_PUBLIC/images/tiled/$dataset/written.json" ]; then
echo "Running scripts/write-images $dataset all"
pnpm tsx scripts/write-images $dataset all &
else
echo "Skipping scripts/write-images $dataset all as $OUTPUT_DIR_PUBLIC/images/tiled/$dataset/written.json already exists"
fi
# allow to execute up to $N jobs in parallel
while [[ $(jobs -r -p | wc -l) -ge $JOBS ]]; do
# now there are $N jobs already running, so wait here for any job
# to be finished so there is a place to start next one.
wait -n
done
done
wait
# merge-stats
if [ ! -f "$OUTPUT_DIR_PUBLIC/prefix-data/stats.json" ] && [ ! -f "$OUTPUT_DIR_PUBLIC/prefix-data/stats.json.gz" ] ; then
echo "Running scripts/merge-stats.ts"
pnpm tsx scripts/merge-stats.ts
else
echo "Skipping scripts/merge-stats.ts as $OUTPUT_DIR_PUBLIC/prefix-data/stats.json already exists"
fi
# minify-images
for dataset in "$OUTPUT_DIR_PUBLIC/images/tiled/"*; do
echo "Running scripts/minify-images.sh $dataset &"
scripts/minify-images.sh "$dataset" &
# allow to execute up to $N jobs in parallel
while [[ $(jobs -r -p | wc -l) -ge $JOBS ]]; do
# now there are $N jobs already running, so wait here for any job
# to be finished so there is a place to start next one.
wait -n
done
done
wait
if [ ! -d "$OUTPUT_DIR_PUBLIC/title-data" ]; then
echo "Running scripts/write-titles.ts"
pnpm tsx scripts/write-titles.ts
else
echo "Skipping scripts/write-titles.ts as $OUTPUT_DIR_PUBLIC/title-data already exists"
fi
================================================
FILE: scripts/rarity/.gitignore
================================================
/target
================================================
FILE: scripts/rarity/Cargo.toml
================================================
[package]
name = "rarity"
version = "0.1.0"
edition = "2021"
[dependencies]
simd-json = { version = "*", default-features = false, features = ["serde_impl", "known-key"] }
rusqlite = { version = "0.30", features = ["bundled"] }
zstd = "0.13.2"
humansize = "*"
serde = { version = "1.0", features = ["derive"] }
parking_lot = "0.12.3"
crossbeam-channel = "0.5.14"
num_cpus = "1.16.0"
snmalloc-rs = { version = "0.3.7", features = ["lto", "native-cpu"] }
memory-stats = "1.2.0"
regex = "1.11.1"
[profile.release]
codegen-units = 1
lto = "fat"
================================================
FILE: scripts/rarity/src/main.rs
================================================
#[global_allocator]
// better performance than the default malloc
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
use crossbeam_channel::{bounded, Sender};
use humansize::{format_size, BINARY};
use parking_lot::Mutex as PLMutex;
use rusqlite::{params, Connection};
use serde::Deserialize;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::sync::{Arc, LazyLock};
use std::time::{Duration, Instant};
use zstd::Decoder;
const CHANNEL_BATCH_SIZE: usize = 10000;
// Type aliases
type OclcIdNumeric = u64;
type Isbn = String;
// Enum to represent the different metadata types
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
enum RawRecord {
#[serde(rename = "title_json")]
TitleJson { record: TitleRecord },
#[serde(rename = "search_holdings_summary_all_editions")]
SearchHoldings {
// oclc_number: String,
// from_filenames: Vec<String>,
record: HoldingsRecord,
},
#[serde(untagged)]
Other {},
}
#[derive(Deserialize, Debug)]
struct TitleRecord {
#[serde(rename = "oclcNumber")]
oclc_number: String,
title: Option<String>,
creator: Option<String>,
//#[serde(rename = "totalEditions")]
//total_editions: u32,
// isbn13: Option<String>,
isbns: Vec<Isbn>,
#[serde(rename = "machineReadableDate")]
machine_readable_date: Option<String>,
date: Option<String>,
#[serde(rename = "publicationDate")]
publication_date: Option<String>,
}
#[derive(Deserialize, Debug)]
struct HoldingsRecord {
oclc_number: OclcIdNumeric,
total_holding_count: u32,
total_editions: u32,
}
#[derive(Deserialize, Debug)]
struct JsonRecord {
metadata: RawRecord,
}
// Result type for parsed records
#[derive(Debug)]
enum ParsedRecord {
Title {
oclc_num: OclcIdNumeric,
title: Option<String>,
creator: Option<String>,
isbn: Vec<i64>,
publication_date: Option<i64>,
},
Holdings {
oclc_num: OclcIdNumeric,
holdings: (u32, u32),
},
}
fn format_si_number(num: u64) -> String {
format_size(num, BINARY)
}
struct ZstdStreamWithProgress<R: io::Read> {
reader: R,
bytes_read: u64,
bytes_read_last: u64,
total_size: u64,
last_update: Instant,
}
impl<R: io::Read> ZstdStreamWithProgress<R> {
fn new(reader: R, total_size: u64) -> Self {
Self {
reader,
bytes_read: 0,
bytes_read_last: 0,
total_size,
last_update: Instant::now(),
}
}
}
impl<R: io::Read> io::Read for ZstdStreamWithProgress<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let bytes = self.reader.read(buf)?;
self.bytes_read += bytes as u64;
if self.last_update.elapsed() >= Duration::from_secs(1) {
eprintln!(
"read {} / {} ({:.2}%, {}/s)",
format_si_number(self.bytes_read),
format_si_number(self.total_size),
(self.bytes_read as f64 / self.total_size as f64) * 100.0,
format_si_number(
(self.bytes_read - self.bytes_read_last) / self.last_update.elapsed().as_secs()
)
);
self.last_update = Instant::now();
self.bytes_read_last = self.bytes_read;
}
Ok(bytes)
}
}
fn process_batch(lines: Vec<String>, record_count: u64) -> Vec<ParsedRecord> {
lines
.into_iter()
.enumerate()
.flat_map(|(i, line)| {
let mut json_buffer = line.into_bytes();
let record: JsonRecord = match simd_json::serde::from_slice(&mut json_buffer) {
Ok(v) => v,
Err(e) => {
eprintln!(
"Error parsing JSON at record {}: {}",
record_count + i as u64,
e
);
return vec![];
}
};
match record.metadata {
RawRecord::TitleJson { record } => {
if let Ok(oclc_num) = record.oclc_number.parse() {
return vec![ParsedRecord::Title {
oclc_num,
isbn: record
.isbns
.iter()
.filter_map(|isbn| {
let int: i64 = isbn.parse().ok()?;
if int < 978_000_000_000_0 || int >= 980_000_000_000_0 {
return None;
}
Some(int)
})
.collect(),
publication_date: parse_publication_date(&record),
title: record.title,
creator: record.creator,
}];
}
}
RawRecord::SearchHoldings { record, .. } => {
return vec![ParsedRecord::Holdings {
oclc_num: record.oclc_number,
holdings: (record.total_holding_count, record.total_editions),
}];
}
_ => {}
}
vec![]
})
.collect()
}
// try each of the three date fields in order (machineReadableDate, publicationDate, date), parse them with the regex ".*\b([12]\d\d\d)\b.*", fall back to next if regex fails
fn parse_single_date(date: &str) -> Option<i64> {
static RE: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r".*\b([12]\d\d\d)\b.*").unwrap());
RE.captures(date)
.and_then(|cap| cap.get(1))
.and_then(|m| m.as_str().parse().ok())
}
fn parse_publication_date(record: &TitleRecord) -> Option<i64> {
record
.machine_readable_date
.as_ref()
.and_then(|date| parse_single_date(date))
.or_else(|| {
record
.publication_date
.as_ref()
.and_then(|date| parse_single_date(date))
})
.or_else(|| {
record
.date
.as_ref()
.and_then(|date| parse_single_date(date))
})
}
fn reader_thread(reader: impl BufRead, sender: Sender<Vec<String>>) -> io::Result<()> {
let mut batch = Vec::with_capacity(CHANNEL_BATCH_SIZE);
for line in reader.lines() {
batch.push(line?);
if batch.len() >= CHANNEL_BATCH_SIZE {
let mut new_batch = Vec::with_capacity(CHANNEL_BATCH_SIZE);
std::mem::swap(&mut batch, &mut new_batch);
sender
.send(new_batch)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
}
}
// Send the final batch if it's not empty
if !batch.is_empty() {
let _ = sender.send(batch);
}
Ok(())
}
fn setup_database(conn: &Connection) -> rusqlite::Result<()> {
// performance pragmas
conn.execute_batch("PRAGMA synchronous = OFF")?;
conn.execute_batch("PRAGMA journal_mode = WAL")?;
conn.execute_batch("PRAGMA cache_size = 100000")?;
conn.execute_batch("PRAGMA temp_store = MEMORY")?;
conn.execute_batch("PRAGMA mmap_size = 30000000000")?;
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS isbn_data (
oclc_number INTEGER NOT NULL,
isbn13 INTEGER NOT NULL,
publication_date INTEGER,
title TEXT,
creator TEXT,
PRIMARY KEY (oclc_number, isbn13)
);
CREATE INDEX IF NOT EXISTS isbn_oclc_number ON isbn_data (isbn13);
",
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS holdings_data (
oclc_number INTEGER PRIMARY KEY,
holding_count INTEGER NOT NULL,
edition_count INTEGER NOT NULL
)",
[],
)?;
Ok(())
}
fn main() -> io::Result<()> {
let args: Vec<String> = std::env::args().collect();
let fname = args.get(1).expect("no input filename provided");
// output env var DATA_DIR
let out_dir = std::env::var("DATA_DIR").unwrap_or_else(|_| "../../data".to_string());
// Initialize SQLite database
let conn = Connection::open(format!("{}/library_holding_data.sqlite3", out_dir))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
setup_database(&conn).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let file = File::open(fname)?;
let file_size = file.metadata()?.len();
let progress_reader = ZstdStreamWithProgress::new(file, file_size);
let decoder = Decoder::new(progress_reader)?;
let reader = BufReader::new(decoder);
// Shared database connection
let db = Arc::new(PLMutex::new(conn));
let record_count = Arc::new(PLMutex::new(0u64));
let parser_threads: usize = num_cpus::get();
// Channel for passing batches of lines
let (sender, receiver) = bounded(parser_threads * 4);
// Spawn reader thread
let reader_handle = std::thread::spawn(move || reader_thread(reader, sender));
// Process batches in parallel
let processing_threads: Vec<_> = (0..parser_threads)
.map(|_| {
let receiver = receiver.clone();
let db = Arc::clone(&db);
let record_count = Arc::clone(&record_count);
std::thread::spawn(move || {
while let Ok(batch) = receiver.recv() {
let current_count = {
let mut count = record_count.lock();
*count += batch.len() as u64;
*count
};
if current_count % 1000000 < CHANNEL_BATCH_SIZE as u64 {
println!(
"{} records... {{ memory: {} }}",
current_count,
format_si_number(get_memory_usage())
);
}
let parsed_records = process_batch(batch, current_count);
store_to_db(&db, parsed_records).unwrap();
}
})
})
.collect();
// Wait for reader to finish
reader_handle.join().expect("Reader thread panicked")?;
// Wait for all processing threads to finish
for handle in processing_threads {
handle.join().expect("Processing thread panicked");
}
Ok(())
}
fn store_to_db(
db: &Arc<PLMutex<Connection>>,
records: Vec<ParsedRecord>,
) -> Result<(), rusqlite::Error> {
let mut db = db.lock();
let tx = db.transaction().unwrap();
for record in records {
match record {
ParsedRecord::Title {
oclc_num,
isbn,
publication_date,
title,
creator,
} => {
for isbn in isbn {
tx.prepare_cached(
"INSERT OR IGNORE INTO isbn_data (oclc_number, isbn13, publication_date, title, creator) VALUES (?1, ?2, ?3, ?4, ?5)",
)?
.execute(params![oclc_num, isbn, publication_date, title, creator])?;
}
}
ParsedRecord::Holdings { oclc_num, holdings } => {
tx.prepare_cached(
"INSERT OR IGNORE INTO holdings_data (oclc_number, holding_count, edition_count) VALUES (?1, ?2, ?3)")?.execute(
params![oclc_num, holdings.0 as i64, holdings.1 as i64],
)?;
}
}
}
tx.commit().unwrap();
Ok(())
}
fn get_memory_usage() -> u64 {
memory_stats::memory_stats()
.map(|e| e.physical_mem as u64)
.unwrap_or(0)
}
================================================
FILE: scripts/write-images/ImageTiler.ts
================================================
import { mkdir } from "fs/promises";
import sharp from "sharp";
import { ImageTile, channelMax } from ".";
import {
IMG_WIDTH,
IsbnPrefixWithoutDashes,
IsbnRelative,
ProjectionConfig,
relativeToIsbnPrefix,
statsConfig,
totalIsbns,
} from "../../src/lib/util";
import { bookshelfConfig } from "../../src/projections/bookshelf";
export class StatsAggregator {
statistics = new Map<IsbnPrefixWithoutDashes, Record<string, number>>();
addStatistic(isbn: IsbnRelative, obj: Record<string, number>) {
const isbnFull = relativeToIsbnPrefix(isbn);
for (
let i = statsConfig.minPrefixLength;
i <= statsConfig.maxPrefixLength;
i++
) {
const prefix = isbnFull.slice(0, i) as IsbnPrefixWithoutDashes;
let stats = this.statistics.get(prefix);
if (!stats) {
stats = {};
this.statistics.set(prefix, stats);
}
for (const [key, value] of Object.entries(obj)) {
stats[key] = (stats[key] || 0) + value;
}
}
}
}
export class ImageTiler {
images = new Map<number, ImageTile>();
written = new Set<number>();
config: ProjectionConfig;
totalBooksPerPixel: number;
// only set for first zoom level
stats?: StatsAggregator;
postprocessPixels?: (
img: ImageTile,
totalBooksPerPixel: number,
) => void | Promise<void>;
constructor(
private prefixLength: number,
private tiledDir: string,
) {
const { width, height } =
prefixLength === 4
? { width: 100000, height: 20000 }
: { width: IMG_WIDTH * Math.sqrt(10 ** (prefixLength - 1)) };
this.config =
/* linearConfig({
scale: Math.sqrt(scale),
aspectRatio: 5 / 4,
});*/
bookshelfConfig({ width, height });
this.totalBooksPerPixel =
totalIsbns / this.config.pixelWidth / this.config.pixelHeight;
console.log(`total books per pixel: ${this.totalBooksPerPixel}`);
}
logProgress(progress: number) {
console.log(
`Progress for ${this.tiledDir}: ${(progress * 100).toFixed(2)}%...`,
);
}
async init() {
console.log(`Generating ${this.tiledDir}...`);
await mkdir(this.tiledDir, { recursive: true });
}
#getImage(relativeIsbn: number): ImageTile {
const prefix = Math.floor(relativeIsbn / 10 ** (10 - this.prefixLength));
const startIsbn = prefix * 10 ** (10 - this.prefixLength);
const endIsbn = startIsbn + 10 ** (10 - this.prefixLength) - 1;
const start = this.config.relativeIsbnToCoords(startIsbn as IsbnRelative);
const end = this.config.relativeIsbnToCoords(endIsbn as IsbnRelative);
let image = this.images.get(prefix);
if (this.written.has(prefix))
throw Error(`tile ${prefix} already finalized`);
if (!image) {
const width = Math.ceil(end.x + end.width - start.x);
const height = Math.ceil(end.y + end.height - start.y);
image = {
x: start.x,
y: start.y,
width,
height,
img: new Float32Array(width * height * 3),
};
this.images.set(prefix, image);
}
return image;
}
colorIsbn(
relativeIsbn: IsbnRelative,
color: [number, number, number],
options: {
addToPixel: boolean;
scaleColors: boolean;
scaleColorByTileScale: boolean;
} = { addToPixel: true, scaleColorByTileScale: true, scaleColors: true },
) {
const channels = 3;
const image = this.#getImage(relativeIsbn);
// const x = Math.floor((position / scale) % dimensions.width);
// const y = Math.floor(position / scale / dimensions.width);
// eslint-disable-next-line prefer-const
let { x, y, width, height } =
this.config.relativeIsbnToCoords(relativeIsbn);
x -= image.x;
y -= image.y;
// if we are scaling by tile scale, we want to consider pixels that are < 50% filled. If not,
// we want to only include those >= 50% filled. Since the center of a pixel is at (0.5, 0.5), this means rounding gives us the bound (lower bound inclusive, upper bound exclusive)
const minX = options.scaleColorByTileScale ? Math.floor(x) : Math.round(x);
let maxX = options.scaleColorByTileScale
? Math.ceil(x + width)
: Math.round(x + width);
const minY = options.scaleColorByTileScale ? Math.floor(y) : Math.round(y);
let maxY = options.scaleColorByTileScale
? Math.ceil(y + height)
: Math.round(y + height);
// but, if no pixel would be put, put a pixel
if (minX === maxX) maxX++;
if (minY === maxY) maxY++;
for (let xo = minX; xo < maxX; xo++) {
for (let yo = minY; yo < maxY; yo++) {
const pixelIndex = (yo * image.width + xo) * channels;
// we may have some pixels that we only want to fractionally fill
let scaleColor = options.scaleColors ? channelMax : 1;
if (options.scaleColorByTileScale) {
const filWidth = Math.min(x + width, xo + 1) - Math.max(x, xo);
const filHeight = Math.min(y + height, yo + 1) - Math.max(y, yo);
scaleColor *= filWidth * filHeight;
}
if (options.addToPixel) {
image.img[pixelIndex] += color[0] * scaleColor;
image.img[pixelIndex + 1] += color[1] * scaleColor;
image.img[pixelIndex + 2] += color[2] * scaleColor;
} else {
image.img[pixelIndex] = color[0] * scaleColor;
image.img[pixelIndex + 1] = color[1] * scaleColor;
image.img[pixelIndex + 2] = color[2] * scaleColor;
}
}
}
}
async #writeAndPurgeImage(prefix: number) {
await this.writeImage(prefix);
this.images.delete(prefix);
this.written.add(prefix);
}
async writeImage(prefix: number) {
if (this.written.has(prefix)) throw Error("image already written");
const image = this.images.get(prefix);
if (!image) throw Error("no image");
if (this.postprocessPixels)
await this.postprocessPixels(image, this.totalBooksPerPixel);
const img = sharp(image.img, {
raw: {
width: image.width,
height: image.height,
channels: 3,
premultiplied: false,
},
});
const paddedPrefix = String(prefix).padStart(this.prefixLength, "0");
/*const withSubdirs = paddedPrefix
.replace(/(.{4})/g, "$1/")
.replace(/\/$/, "");
if (withSubdirs.includes("/")) {
await mkdir(dirname(withSubdirs), { recursive: true });
}*/
const fname = `${this.tiledDir}/${paddedPrefix}.png`;
console.log(`writing tile ${fname}`);
await img.toFile(fname);
// await new Promise((resolve) => setTimeout(resolve, 1000));
img.destroy();
}
async writeAll() {
await this.purgeToLength(0);
}
async purgeToLength(len: number) {
while (this.images.size > len) {
const image = this.images.keys().next();
if (image.value === undefined) throw Error("impossibor");
await this.#writeAndPurgeImage(image.value);
}
}
async finish() {
console.log(`writing ${this.images.size} remaining tiles`);
await this.writeAll();
console.log(`wrote ${this.written.size} tiles`);
console.log("Done.");
}
}
================================================
FILE: scripts/write-images/index.ts
================================================
import { writeFile } from "fs/promises";
import { ImageTiler, StatsAggregator } from "./ImageTiler";
import * as modules from "./modules";
import { loadSparseDataToMemory } from "./modules/single-sparse";
export type IsbnData = Partial<Record<string, Uint32Array>>;
/** sharp / vips uses a channel max of 1e16 for float32 images for some reason */
export const channelMax = 65535;
/** info of one tile of a tiled image */
export interface ImageTile {
x: number;
y: number;
width: number;
height: number;
img: Float32Array;
}
export type ProcessSingleZoom = (tiler: ImageTiler) => Promise<void>;
async function processAllZoomLevels(
dataset: string,
minLevel = 1,
maxLevel = 4,
): Promise<void> {
const stats = new StatsAggregator();
const processIsbnData = await loadData(dataset, stats);
const written = [];
const dir = `${process.env.OUTPUT_DIR_PUBLIC ?? "public"}/images/tiled/${dataset}`;
for (let level = minLevel; level <= maxLevel; level++) {
const tiledDir = `${dir}/zoom-${level}`;
const tiler = new ImageTiler(level, tiledDir);
if (level === minLevel) tiler.stats = stats;
await tiler.init();
await processIsbnData(tiler);
await tiler.finish();
const w = tiler.written;
for (const prefix of w) {
written.push(prefix.toString().padStart(level, "0"));
}
if (level === minLevel) {
await writeFile(
`${dir}/stats.json`,
JSON.stringify(Object.fromEntries(stats.statistics)),
);
}
}
if (minLevel === 1 && maxLevel === 4) {
await writeFile(`${dir}/written.json`, JSON.stringify(written));
}
}
const specialDatasets = ["publishers", "all", "rarity", "publication_date"];
async function loadData(
dataset: string,
stats: StatsAggregator,
): Promise<ProcessSingleZoom> {
if (dataset === "publishers") {
return await modules.publishers();
} else if (dataset === "rarity") {
return modules.rarity(stats);
} else if (dataset === "all") {
return await modules.all(stats);
} else if (dataset === "publication_date") {
return modules.publication_date(stats);
} else {
return await modules.single(dataset);
}
}
async function main() {
// Main execution
const dataset = process.argv[2];
if (!dataset) throw Error("dataset arg required, use list to list");
if (dataset === "list") {
console.log(specialDatasets, Object.keys(await loadSparseDataToMemory()));
return;
}
const level = process.argv[3];
if (!level) throw Error("level arg required (1,2,3,4 or all)");
if (level === "all") {
await processAllZoomLevels(dataset);
} else {
await processAllZoomLevels(dataset, +level, +level);
}
}
void main();
================================================
FILE: scripts/write-images/modules/aggregate-dense.ts
================================================
import { IsbnData, ProcessSingleZoom } from "..";
import { IsbnRelative, totalIsbns } from "../../../src/lib/util";
import { ImageTiler, StatsAggregator } from "../ImageTiler";
import { loadSparseDataToMemory } from "./single-sparse";
export async function colorImageWithDenseIsbns(
tiler: ImageTiler,
isbnsBinaryUint8: Uint8Array,
): Promise<void> {
if (isbnsBinaryUint8.length !== totalIsbns) throw Error("wrong length");
const addcolor = [1, 1, 1] as [number, number, number];
for (let i = 0; i < isbnsBinaryUint8.length; i++) {
const relativeIsbn = i as IsbnRelative;
if (relativeIsbn % 2e6 === 0) {
tiler.logProgress(relativeIsbn / totalIsbns);
await tiler.purgeToLength(1);
}
if (isbnsBinaryUint8[i]) {
tiler.colorIsbn(relativeIsbn, addcolor);
tiler.stats?.addStatistic(relativeIsbn, { dataset_all: 1 });
}
}
}
export function aggregateDatasets(
datasets: IsbnData,
stats: StatsAggregator,
): Uint8Array {
const out = new Uint8Array(totalIsbns);
for (const dataset in datasets) {
console.log("adding data for dataset", dataset);
const data = datasets[dataset];
let position = 0;
let isbnStreak = true;
if (!data) throw Error("no data");
for (const value of data) {
if (isbnStreak) {
for (let j = 0; j < value; j++) {
out[position as IsbnRelative] = 1;
stats.addStatistic(position as IsbnRelative, {
[`dataset_${dataset}`]: 1,
});
position++;
}
} else {
position += value;
}
isbnStreak = !isbnStreak;
}
}
return out;
}
export default async function aggregateDense(
stats: StatsAggregator,
): Promise<ProcessSingleZoom> {
const dataSet = await loadSparseDataToMemory();
const data = aggregateDatasets(dataSet, stats);
return (tiler) => colorImageWithDenseIsbns(tiler, data);
}
================================================
FILE: scripts/write-images/modules/index.ts
================================================
export { default as all } from "./aggregate-dense";
export { default as publication_date } from "./publication_date";
export { default as publishers } from "./publishers";
export { default as rarity } from "./rarity";
export { default as single } from "./single-sparse";
================================================
FILE: scripts/write-images/modules/publication_date.ts
================================================
import sqlite3 from "better-sqlite3";
import { channelMax, ImageTile, ProcessSingleZoom } from "..";
import {
fullIsbnToRelative,
Isbn13Number,
IsbnRelative,
IsbnStrWithChecksum,
totalIsbns,
} from "../../../src/lib/util";
import { ImageTiler, StatsAggregator } from "../ImageTiler";
export function loadPublicationDateData(
dbName: string,
stats: StatsAggregator,
) {
const db = sqlite3(dbName);
let i = 0;
const maxOclcNumber = db
.prepare("select max(oclc_number) from isbn_data")
.pluck()
.get() as number;
const isbns = new Uint8Array(totalIsbns);
for (const row of db
.prepare<
[],
{
oclc_number: number;
isbn13: Isbn13Number;
publication_date: number | null;
}
>("select * from isbn_data where publication_date is not null")
.iterate()) {
if (++i % 1000000 === 0)
console.log(
"loading publication date data",
((row.oclc_number / maxOclcNumber) * 100).toFixed(1) + "%",
i,
row,
);
// isbns.set(+row.isbn as Isbn13Number, row.oclc_number);
const isbnRel = fullIsbnToRelative(
String(row.isbn13) as IsbnStrWithChecksum,
);
if (isbnRel < 0 || isbnRel >= totalIsbns) {
throw new Error(`invalid isbn: ${row.isbn13} ${isbnRel}`);
}
if (row.publication_date !== null) {
// range 1800 - 2055
isbns[isbnRel] = Math.min(255, Math.max(1, row.publication_date - 1800));
stats.addStatistic(isbnRel, {
publication_date: row.publication_date,
publication_date_count: 1,
});
}
}
return isbns;
}
export default function rarityModule(
stats: StatsAggregator,
): ProcessSingleZoom {
const dataset = loadPublicationDateData(
process.env.INPUT_HOLDING_SQLITE ?? "data/library_holding_data.sqlite3",
stats,
);
return (tiler) => processPublicationData(tiler, dataset);
}
async function processPublicationData(
tiler: ImageTiler,
dataset: Uint8Array,
): Promise<void> {
tiler.postprocessPixels = postprocessPixels;
for (let i = 0; i < totalIsbns; i++) {
const relativeIsbn = i as IsbnRelative;
if (relativeIsbn % 2e6 === 0) {
tiler.logProgress(relativeIsbn / totalIsbns);
await tiler.purgeToLength(1);
}
const publicationDate = dataset[i]; // - 1800
if (publicationDate)
tiler.colorIsbn(relativeIsbn, [publicationDate, 1, 1], {
addToPixel: true,
scaleColors: false,
scaleColorByTileScale: false,
});
}
}
function postprocessPixels(image: ImageTile, totalBooksPerPixel: number) {
for (let i = 0; i < image.img.length; i += 3) {
let publicationDate = image.img[i];
const bookCount = image.img[i + 1];
// verify all are ints
if (!Number.isInteger(publicationDate)) {
throw new Error("non-integer value");
}
// compute average date
if (bookCount > 0) {
publicationDate /= bookCount;
}
if (bookCount === 0 && publicationDate !== 0) {
console.log({ i, publicationDate, bookCount });
throw new Error("invalid publication date");
}
if (bookCount > 0 && (publicationDate < 0 || publicationDate > 255)) {
console.log({ i, publicationDate, bookCount });
throw new Error("invalid publication date");
}
// scale to channelMax
publicationDate *= channelMax / 255;
image.img[i] = publicationDate;
image.img[i + 1] = publicationDate;
image.img[i + 2] = (bookCount / totalBooksPerPixel) * channelMax;
}
}
================================================
FILE: scripts/write-images/modules/publishers.ts
================================================
import { readFile } from "fs/promises";
import { ProcessSingleZoom } from "..";
import { InfoMap, LazyPrefixInfo } from "../../../src/lib/info-map";
import { getGroupHierarchy } from "../../../src/lib/prefix-data";
import {
IsbnRelative,
lastIsbnInPrefix,
relativeToIsbnPrefix,
removeDashes,
totalIsbns,
} from "../../../src/lib/util";
import { ImageTiler } from "../ImageTiler";
export async function processPublishersData(
tiler: ImageTiler,
publishersData: LazyPrefixInfo,
): Promise<void> {
let color: [number, number, number] | null = null;
let curPrefixEnd = -1;
for (
let relativeIsbn = 0 as IsbnRelative;
relativeIsbn < totalIsbns;
relativeIsbn++
) {
if (relativeIsbn % 2e6 === 0) {
tiler.logProgress(relativeIsbn / totalIsbns);
await tiler.purgeToLength(1);
}
if (relativeIsbn > curPrefixEnd) {
const isbn = relativeToIsbnPrefix(relativeIsbn);
const data = getGroupHierarchy(publishersData, isbn);
if (typeof data === "function") {
throw Error(
"found lazy data in full data dump from /data, this is impossible",
);
}
if (data.outers.length >= 2) {
const pr = data.outers[1]?.info?.[0].prefix;
if (!pr) throw Error("not handled");
curPrefixEnd = lastIsbnInPrefix(removeDashes(pr));
} else {
curPrefixEnd = relativeIsbn + 9;
}
if (data.outers.length === 0) {
// throw Error(`no data for ${isbn}, previous ended at ${curPrefixEnd}`);
color = null;
continue;
}
color = null;
const publisherId = data.outers[1]?.info?.[0].numericId;
// publisherId to RGB
if (publisherId) {
color = [0, 0, 0];
color[0] = ((publisherId & 0xff0000) >> 16) / 255;
color[1] = ((publisherId & 0x00ff00) >> 8) / 255;
color[2] = (publisherId & 0x0000ff) / 255;
tiler.stats?.addStatistic(relativeIsbn, {
publisher_blocks: 1,
});
}
/* console.log(
`color from ${isbn} to ${curPrefixEnd + isbnEANStart}: ${color}`
);*/
}
if (color) {
tiler.colorIsbn(relativeIsbn, color, {
addToPixel: false,
scaleColors: true,
scaleColorByTileScale: false,
});
}
}
}
export async function loadPublishersData() {
const publishersData = {
children: JSON.parse(
await readFile(
process.env.INPUT_PREFIX_DATA ?? `data/prefix-data.json`,
"utf8",
),
) as InfoMap,
totalChildren: 0,
};
return publishersData;
}
export default async function publishersModule(): Promise<ProcessSingleZoom> {
const publishersData = await loadPublishersData();
return (tiler) => processPublishersData(tiler, publishersData);
}
================================================
FILE: scripts/write-images/modules/rarity.ts
================================================
import sqlite3 from "better-sqlite3";
import { channelMax, ImageTile, ProcessSingleZoom } from "..";
import {
fullIsbnToRelative,
Isbn13Number,
IsbnRelative,
IsbnStrWithChecksum,
totalIsbns,
} from "../../../src/lib/util";
import { ImageTiler, StatsAggregator } from "../ImageTiler";
export function loadRarityData(dbName: string, stats: StatsAggregator) {
const db = sqlite3(dbName);
let i = 0;
const maxOclcNumber = db
.prepare("select max(oclc_number) from isbn_data")
.pluck()
.get() as number;
const isbns = new Uint8Array(totalIsbns * 2);
for (const row of db
.prepare<
[],
{
oclc_number: number;
isbn13: Isbn13Number;
publication_date: number;
holding_count: number;
edition_count: number;
}
>(
"select * from isbn_data join holdings_data on isbn_data.oclc_number = holdings_data.oclc_number",
)
.iterate()) {
if (++i % 1000000 === 0)
console.log(
"loading rarity data",
((row.oclc_number / maxOclcNumber) * 100).toFixed(1) + "%",
i,
row,
);
// isbns.set(+row.isbn as Isbn13Number, row.oclc_number);
const isbnRel = fullIsbnToRelative(
String(row.isbn13) as IsbnStrWithChecksum,
);
if (isbnRel < 0 || isbnRel >= totalIsbns) {
throw new Error(`invalid isbn: ${row.isbn13} ${isbnRel}`);
}
const existingHolding = isbns[2 * isbnRel];
const existingEdition = isbns[2 * isbnRel + 1];
isbns[2 * isbnRel] = Math.min(row.holding_count + existingHolding, 255);
// add 1 to edition count as a "exists" marker
isbns[2 * isbnRel + 1] = Math.min(
(existingEdition || 1) + row.edition_count,
255,
);
stats.addStatistic(isbnRel, {
rarity_holdingCount: row.holding_count,
rarity_editionCount: row.edition_count,
rarity_exists: 1,
});
/*if (existingHolding || existingEdition) {
console.log("multiple entries for ", row, {
existingHolding,
existingEdition,
});
}*/
}
return isbns;
}
/*if (require.main === module) {
const dbName = process.argv[2];
if (!dbName) throw new Error("no db name provided");
loadRarityData(dbName);
}*/
export default function rarityModule(
stats: StatsAggregator,
): ProcessSingleZoom {
const dataset = loadRarityData(
process.env.INPUT_HOLDING_SQLITE ?? "data/library_holding_data.sqlite3",
stats,
);
return (tiler) => processRarityData(tiler, dataset);
}
async function processRarityData(
tiler: ImageTiler,
dataset: Uint8Array,
): Promise<void> {
tiler.postprocessPixels = postprocessPixels;
for (let i = 0; i < totalIsbns; i++) {
const relativeIsbn = i as IsbnRelative;
if (relativeIsbn % 2e6 === 0) {
tiler.logProgress(relativeIsbn / totalIsbns);
await tiler.purgeToLength(1);
}
const holdingCount = dataset[2 * i];
let editionCount = dataset[2 * i + 1];
const exists = editionCount > 0; // we added 1 to editionCount as an "exists" marker
if (exists) editionCount -= 1;
if (holdingCount || editionCount || exists) {
tiler.colorIsbn(relativeIsbn, [holdingCount, editionCount, 1], {
addToPixel: true,
scaleColors: false,
scaleColorByTileScale: false,
});
}
}
}
function postprocessPixels(image: ImageTile) {
for (let i = 0; i < image.img.length; i += 3) {
let holdingsCount = image.img[i];
let editionCount = image.img[i + 1];
let bookCount = image.img[i + 2];
// verify all are ints
if (
!Number.isInteger(holdingsCount) ||
!Number.isInteger(editionCount) ||
!Number.isInteger(bookCount)
) {
throw new Error("non-integer value");
}
// verify all are positive
if (holdingsCount < 0 || editionCount < 0 || bookCount < 0) {
throw new Error("negative value");
}
// verify all are 0 if bookCount is 0
if (bookCount === 0 && (holdingsCount || editionCount)) {
throw new Error("non-zero value with zero book count");
}
// scale the colors
const maxValue = Math.max(holdingsCount, editionCount, bookCount);
const needScaleDown = maxValue >= 255;
if (needScaleDown) {
const scale = 255 / maxValue;
holdingsCount *= scale;
editionCount *= scale;
bookCount *= scale;
}
// scale to channelMax
holdingsCount *= channelMax / 255;
editionCount *= channelMax / 255;
bookCount *= channelMax / 255;
/*console.log({
holdingsCount,
editionCount,
bookCount,
maxValue,
foo: image.img.slice(i, i + 3),
});*/
image.img[i] = holdingsCount;
image.img[i + 1] = editionCount;
image.img[i + 2] = bookCount;
}
}
================================================
FILE: scripts/write-images/modules/single-sparse.ts
================================================
import bencode from "bencode";
import { createReadStream } from "node:fs";
import { ZSTDDecompress } from "simple-zstd";
import { IsbnData, ProcessSingleZoom } from "..";
import { IsbnRelative } from "../../../src/lib/util";
import { ImageTiler } from "../ImageTiler";
export const INPUT_FILENAME =
process.env.INPUT_BENC ??
`${process.env.DATA_DIR ?? "data"}/aa_isbn13_codes_20241204T185335Z.benc.zst`;
export async function colorImageWithSparseIsbns(
tiler: ImageTiler,
packedIsbnsBinary: Uint32Array,
): Promise<void> {
const addcolor = [1, 1, 1] as [number, number, number];
let position = 0;
let isbnStreak = true;
for (const value of packedIsbnsBinary) {
if (isbnStreak) {
for (let j = 0; j < value; j++) {
const isbn = position as IsbnRelative;
tiler.colorIsbn(isbn, addcolor);
// tiler.stats?.addStatistic(isbn, { count: 1 });
position++;
}
} else {
position += value;
await tiler.purgeToLength(1);
}
isbnStreak = !isbnStreak;
}
}
export async function loadSparseDataToMemory(): Promise<IsbnData> {
// Read and decompress the input file
const fileStream = createReadStream(INPUT_FILENAME);
return new Promise((resolve) => {
const chunks: Buffer[] = [];
fileStream
.pipe(ZSTDDecompress())
.on("data", (chunk: Buffer) => chunks.push(chunk))
.on("end", () => {
const data = Buffer.concat(chunks);
const isbnData = bencode.decode(data) as Record<string, Uint8Array>;
// Convert Uint8Array to Uint32Array
const isbnData2: IsbnData = {};
for (const [k, v] of Object.entries(isbnData)) {
if (v.byteOffset !== 0) {
throw new Error(
`packedIsbnsBinaryUint8 must be aligned to 0, is ${v.byteOffset}`,
);
}
const packedIsbnsBinary = new Uint32Array(v.buffer);
isbnData2[k] = packedIsbnsBinary;
}
resolve(isbnData2);
});
});
}
export default async function singleSparse(
dataset: string,
): Promise<ProcessSingleZoom> {
const data = await loadSparseDataToMemory();
const dataa = data[dataset];
if (!dataa) {
throw new Error(`dataset ${dataset} not found`);
}
return (tiler) => colorImageWithSparseIsbns(tiler, dataa);
}
================================================
FILE: scripts/write-titles.ts
================================================
import sqlite3 from "better-sqlite3";
import { mkdirSync, writeFileSync } from "fs";
import path from "path";
import {
Isbn13Number,
IsbnRelative,
relativeToFullIsbn,
splitNameJson,
totalIsbns,
} from "../src/lib/util";
export function loadPublicationDateData(dbName: string) {
const db = sqlite3(dbName);
// perf options
db.pragma("cache_size = 100000");
//mmap
db.pragma("journal_mode = WAL");
db.pragma("synchronous = OFF");
db.pragma("temp_store = MEMORY");
db.pragma("mmap_size = 300000000000");
const blockSize = 10000;
const prefixLength = 12 - Math.log10(blockSize);
const dirSegmentLength = 3;
for (let isbn = 0; isbn < totalIsbns; isbn += blockSize) {
const first = relativeToFullIsbn(isbn as IsbnRelative);
const next = relativeToFullIsbn((isbn + blockSize) as IsbnRelative);
const rows = db
.prepare<
[Isbn13Number, Isbn13Number],
{
isbn13: Isbn13Number;
title: string | null;
creator: string | null;
}
>(
"select isbn13,title as title, creator as creator from isbn_data where isbn13 >= ? and isbn13 < ? group by isbn13 order by isbn13",
)
.all(+first as Isbn13Number, +next as Isbn13Number);
for (const row of rows) {
const maxL = 70;
if (row.title && row.title.length > maxL)
row.title = row.title.slice(0, maxL) + "...";
if (row.creator && row.creator.length > maxL)
row.creator = row.creator.slice(0, maxL) + "...";
}
if (isbn % 1000000 === 0)
console.log(
`loading range ${first}, done: ${((isbn / totalIsbns) * 100).toFixed(
1,
)}%`,
);
if (rows.length === 0) continue;
const prefixStr = first.slice(0, prefixLength);
const fname =
`${process.env.OUTPUT_DIR_PUBLIC ?? "public"}/title-data/` +
splitNameJson(prefixStr, dirSegmentLength);
mkdirSync(path.dirname(fname), { recursive: true });
writeFileSync(fname, JSON.stringify(rows));
}
}
loadPublicationDateData(
`${process.env.DATA_DIR ?? "data"}/library_holding_data.sqlite3`,
);
================================================
FILE: src/App.tsx
================================================
import { useMemo, type FC } from "react";
import { IsbnMap } from "./components/IsbnMap";
import { bookshelfConfig } from "./projections/bookshelf";
const App: FC = () => {
const config = useMemo(
() =>
bookshelfConfig({
width: Math.min(
1500,
document.body.clientWidth,
(document.body.clientHeight / 2) * Math.sqrt(10),
),
}),
[],
);
return <IsbnMap config={config} />;
};
export default App;
================================================
FILE: src/components/Controls.tsx
================================================
import isbnlib from "isbn3";
import { observer, useLocalObservable } from "mobx-react-lite";
import { fromPromise } from "mobx-utils";
import React, { useMemo, useRef } from "react";
import { OptionProps, components } from "react-select";
import AsyncSelect from "react-select/async";
import Select from "react-select/base";
import { default as config, default as staticConfig } from "../config";
import { GoogleBooksItem, googleBooksQuery } from "../lib/google-books";
import { Store } from "../lib/Store";
import { IsbnPrefixWithoutDashes, IsbnStrWithChecksum } from "../lib/util";
import { Legend } from "./Legend";
export const Controls: React.FC<{ store: Store }> = observer(function Controls({
store,
}) {
const state = useLocalObservable(() => ({
showSettings: false,
showDatasetChooser: false,
}));
const stats = useMemo(
() =>
fromPromise(
store.statsCalculator.getStats(
"978" as IsbnPrefixWithoutDashes,
"979" as IsbnPrefixWithoutDashes,
),
),
[],
);
return (
<div className={`controls ${state.showSettings ? "advanced" : ""}`}>
<div className="head">
<b style={{ fontSize: "120%" }}>ISBN Visualization</b>{" "}
{stats.case({
fulfilled(stats) {
return (
<small style={{ alignSelf: "flex-end" }}>
Showing{" "}
{(
stats[`dataset_${store.runtimeConfig.dataset}`] ??
stats.dataset_all ??
0
).toLocaleString()}{" "}
books
</small>
);
},
})}
{state.showSettings && (
<>
<button onClick={() => (state.showSettings = !state.showSettings)}>
<small>⚙ Done</small>
</button>
<button
onClick={() => {
store.switchDataset(store.runtimeConfig.dataset, true);
}}
>
Reset Settings
</button>
</>
)}
{!state.showSettings && (
<button
className="preset"
onClick={() => (state.showDatasetChooser = true)}
>
<LoadProgress store={store} /> Preset:{" "}
{(() => {
const ds = staticConfig.datasetOptions.find(
(e) => e.id === store.runtimeConfig.dataset,
);
if (!ds) return null;
return (
<>
<b>{ds.name}</b>
<br />
<i>{ds.description}</i>
</>
);
})()}
</button>
)}
</div>
<Legend store={store} />
{state.showSettings ? (
<Settings store={store} />
) : (
<MainStuff store={store} />
)}
{state.showDatasetChooser && (
<div className="dataset-chooser-wrap">
<div className="dataset-chooser">
<h4
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "baseline",
marginTop: "0.5ex",
}}
>
<div>
Choose a Preset{" "}
<button
onClick={() => {
state.showSettings = !state.showSettings;
state.showDatasetChooser = false;
}}
>
<small>⚙ {state.showSettings ? "Done" : "Advanced"}</small>
</button>
</div>
<button
onClick={() => {
state.showDatasetChooser = false;
}}
>
<small>Close</small>
</button>
</h4>
{staticConfig.datasetOptions.map((d) => (
<React.Fragment key={d.id}>
<button
className="choose-dataset"
onClick={() => {
state.showDatasetChooser = false;
store.switchDataset(d.id, true);
}}
>
<b>{d.name}</b> [{d.id}]<br />
{d.description && <i>{d.description}</i>}
</button>
</React.Fragment>
))}
</div>
</div>
)}
</div>
);
});
const BookOption: React.FC<OptionProps<MinimalGoogleBooksItem>> = (p) => {
return (
<components.Option {...p}>
<b>{p.data.volumeInfo.title}</b>
<br />
{p.data.volumeInfo.authors?.join(", ")}
</components.Option>
);
};
export interface MinimalGoogleBooksItem {
id: string;
volumeInfo: {
title?: string;
authors?: string[];
industryIdentifiers?: GoogleBooksItem["volumeInfo"]["industryIdentifiers"];
};
}
const MainStuff: React.FC<{ store: Store }> = observer(function MainStuff({
store,
}) {
const selectRef = useRef<Select<MinimalGoogleBooksItem>>(null);
return (
<>
<p>
Drag/Zoom like a map. Tap to show details of an ISBN! Right-click-drag
to show stats.
</p>
<label className="form-row">
<div>Show publisher details:</div>
<input
type="checkbox"
checked={store.runtimeConfig.showPublisherNames}
onChange={(e) => {
store.runtimeConfig.showPublisherNames = e.currentTarget.checked;
store.runtimeConfig.publishersBrightness = e.currentTarget.checked
? 0.5
: 0.01;
}}
/>
</label>
<p />
<label>
Search for a book via Google Books or ISBN:
<AsyncSelect<MinimalGoogleBooksItem>
store={store}
ref={selectRef}
loadOptions={async (e) => {
// if it's an isbn with 13 digits and maybe spaces, use that
const eAsNum = e.replace(/[^0-9]/g, "");
if (eAsNum.length === 13) {
return [
{
id: `isbn-${e}`,
volumeInfo: {
title: isbnlib.hyphenate(eAsNum) || eAsNum,
authors: ["Go to ISBN"],
industryIdentifiers: [
{
type: "ISBN_13",
identifier: eAsNum as IsbnStrWithChecksum,
},
],
},
},
];
}
const options = await googleBooksQuery(e);
return options.filter(
(e) =>
e.volumeInfo.title &&
e.volumeInfo.industryIdentifiers?.some(
(i) => i.type === "ISBN_13",
),
);
}}
defaultOptions={config.exampleBooks}
placeholder="Click for examples..."
getOptionLabel={(e) => e.volumeInfo.title ?? "?"}
getOptionValue={(e) => e.id}
// blurInputOnSelect={true} not working
onChange={(e) => {
console.log("found book", e);
const isbn13 = e?.volumeInfo.industryIdentifiers?.find(
(i) => i.type === "ISBN_13",
)?.identifier;
if (!isbn13) throw Error("no isbn13");
store.updateHighlightedIsbn(isbn13);
store.zoomAnimateToHighlight();
setTimeout(() => {
// hack to hide keyboard on mobile
selectRef.current?.blur();
selectRef.current?.blurInput();
}, 100);
}}
components={{ Option: BookOption }}
/>
</label>
</>
);
});
const Settings: React.FC<{ store: Store }> = observer(function Settings({
store,
}) {
const config = store.runtimeConfig;
return (
<>
<fieldset>
<label className="form-row">
<div>Dataset:</div>
<select
value={config.dataset}
onChange={(e) => (config.dataset = e.currentTarget.value)}
style={{ maxWidth: "200px" }}
>
{staticConfig.datasetOptions.map((d) => (
<option key={d.id} value={d.id}>
{d.name} [{d.id}]
</option>
))}
</select>
</label>
<label className="form-row">
<div>Group text vertical:</div>
<input
type="checkbox"
checked={config.groupTextVertical}
onChange={(e) =>
(config.groupTextVertical = e.currentTarget.checked)
}
/>
</label>
<label className="form-row">
<div>Show grid:</div>
<input
type="checkbox"
checked={config.showGrid}
onChange={(e) => (config.showGrid = e.currentTarget.checked)}
/>
</label>
<label className="form-row">
<div>Grid color:</div>
<input
type="text"
value={config.gridColor}
onChange={(e) => (config.gridColor = e.currentTarget.value)}
/>
</label>
<label className="form-row">
<div>Glow brightness:</div>
<input
type="range"
value={config.shaderGlow}
min={0}
max={10}
onChange={(e) => (config.shaderGlow = +e.currentTarget.value)}
/>
</label>
<small>(to make it easier to see sparse data)</small>
</fieldset>
<fieldset>
<legend>Publisher settings</legend>
<label className="form-row">
<div>Overlay publisher names:</div>
<input
type="checkbox"
checked={config.showPublisherNames}
onChange={(e) =>
(config.showPublisherNames = e.currentTarget.checked)
}
/>
</label>
<label className="form-row">
<div>Color publisher ranges:</div>
<input
type="checkbox"
checked={config.publishersBrightness > 0}
onChange={(e) =>
(config.publishersBrightness = e.currentTarget.checked ? 0.7 : 0)
}
/>
</label>
<label className="form-row">
<div>Publisher ranges brightness:</div>
<input
type="range"
value={config.publishersBrightness}
min={0}
max={1}
step={0.01}
onChange={(e) =>
(config.publishersBrightness = +e.currentTarget.value)
}
/>
</label>
<small>
(each publisher's range is highlighted with a random color)
</small>
<label className="form-row">
<div>Publisher range colors:</div>
<select
value={config.publishersColorSchema}
onChange={(e) =>
(config.publishersColorSchema = e.currentTarget.value as
| "dark"
| "hsl")
}
>
{[
{ id: "hsl", name: "colorful" },
{ id: "dark", name: "brown-blue" },
].map((d) => (
<option key={d.id} value={d.id}>
{d.name} [{d.id}]
</option>
))}
</select>
</label>
</fieldset>
<fieldset>
<legend>Zoom Settings</legend>
<label className="form-row">
<div>Min zoom level for text:</div>
<div>{config.textMinZoomLevel}</div>
<input
type="range"
value={config.textMinZoomLevel}
min={0.04}
max={0.2}
step={0.01}
onChange={(e) => (config.textMinZoomLevel = +e.currentTarget.value)}
/>
</label>
<label className="form-row">
<div>Text levels to show</div>
<div>{config.textLevelCount}</div>
<input
type="range"
value={config.textLevelCount}
min={1}
max={4}
step={0.01}
onChange={(e) => (config.textLevelCount = +e.currentTarget.value)}
/>
</label>
<label className="form-row">
<div>Min zoom level to switch images:</div>
<div>{config.imgMinZoomLevel}</div>
<input
type="range"
value={config.imgMinZoomLevel}
min={0.9}
max={2.0}
step={0.01}
onChange={(e) => (config.imgMinZoomLevel = +e.currentTarget.value)}
/>
</label>
<label className="form-row">
<div>Bookshelf styling on zoom:</div>
<input
type="checkbox"
checked={config.doBookshelfEffect}
onChange={(e) =>
(config.doBookshelfEffect = e.currentTarget.checked)
}
/>
</label>
</fieldset>
<fieldset>
<legend>Data Filters</legend>
<label className="form-row">
<div>Minimum Publication Year:</div>
<div>
{config.filterMinimumPublicationYear === -1
? "Off"
: config.filterMinimumPublicationYear}
</div>
<input
type="range"
min={1900}
max={2030}
value={
config.filterMinimumPublicationYear === -1
? 1900
: config.filterMinimumPublicationYear
}
onChange={(e) => {
const value = +e.currentTarget.value;
config.filterMinimumPublicationYear = value === 1900 ? -1 : value;
}}
/>
</label>
<label className="form-row">
<div>Maximum Publication Year:</div>
<div>
{config.filterMaximumPublicationYear === -1
? "Off"
: config.filterMaximumPublicationYear}
</div>
<input
type="range"
min={1900}
max={2030}
value={
config.filterMaximumPublicationYear === -1
? 2030
: config.filterMaximumPublicationYear
}
onChange={(e) => {
const value = +e.currentTarget.value;
config.filterMaximumPublicationYear = value === 2030 ? -1 : value;
}}
/>
</label>
</fieldset>
<label className="form-row">
<div>Custom shader:</div>
<input
type="checkbox"
checked={!!config.customShader}
onChange={(e) => {
if (e.currentTarget.checked) {
config.customShader = store.shaderUtil.shaderColorFn;
} else {
config.customShader = "";
}
}}
/>
</label>
<textarea
value={config.customShader || store.shaderUtil.shaderColorFn}
style={{ height: "8em", width: "100%" }}
onChange={(e) => {
store.shaderError = "";
config.customShader = e.currentTarget.value;
}}
/>
{store.shaderError && (
<div>
Shader Error:{" "}
<pre
style={{
maxHeight: "300px",
overflowY: "scroll",
border: "1px solid black",
}}
>
{store.shaderError}
</pre>
</div>
)}
</>
);
});
const LoadProgress = observer(function LoadProgress({
store,
}: {
store: Store;
}) {
if (store.inProgress.size === 0) return /* green checkmark emoji */ "✅";
const errors = [...store.inProgress].filter((e) => e[1]);
/* red cross emoji */
if (errors.length > 0)
return errors.map((e, i) => (
<div key={i}>
❌ {e[0]}: {String(e[1])}
</div>
));
return <div className="lds-dual-ring" style={{ height: "1em" }} />;
});
================================================
FILE: src/components/EanBarcode.tsx
================================================
export function EanBarcode(props: { ean: string }) {
return <span className="ean13">{props.ean}</span>;
}
================================================
FILE: src/components/Highlight.tsx
================================================
import { Html } from "@react-three/drei";
import { hyphenate } from "isbn3";
import { observer } from "mobx-react-lite";
import React from "react";
import { LazyPrefixInfo } from "../lib/info-map";
import { Store } from "../lib/Store";
import { relativeToFullIsbn, removeDashes, siNumber } from "../lib/util";
import { getPlanePosition } from "../lib/view-utils";
import { AbbrevStats, maxZoomForStats } from "./StatsShow";
export const PublisherHighlightShow: React.FC<{ store: Store }> = observer(
function PublisherHighlightShow({ store }) {
if (store.highlightedIsbn.type === "done") return null;
if (!store.highlightedPublisher) return null;
const isbn = store.highlightedPublisher.relative;
const isbnFull = relativeToFullIsbn(isbn);
const loc = getPlanePosition(store.projection, isbn, isbn);
const publisher = store.highlightedPublisher.data?.[1]?.info?.[0];
return (
<>
<group position={[loc.position[0], loc.position[1], 2]}>
<group position={[0, -loc.height / 2, 0]}>
{/*<HighlightCircle store={store} />*/}
<Html
style={{ pointerEvents: "none" }}
zIndexRange={[20, 20]}
// wrapperClass="highlight-wrapper"
>
<div className="isbn-highlight">
ISBN {hyphenate(isbnFull) || isbnFull}
<br />
{(store.highlightedPublisher.data &&
store.highlightedPublisher.obj && (
<GroupInfo
groupInfo={store.highlightedPublisher.data}
obj={store.highlightedPublisher.obj}
/>
)) ?? <div>Unassigned or unknown range</div>}
<br />
{publisher && (
<AbbrevStats
prefixStart={publisher.prefix}
prefixEnd={publisher.prefix}
store={store}
/>
)}
<b>Click to show book details</b>
<br />
{store.floatZoomFactor < maxZoomForStats && (
<small>Right-click-drag to show region stats</small>
)}
</div>
</Html>
</group>
</group>
</>
);
},
);
export const HighlightShow: React.FC<{ store: Store }> = observer(
function HighlightShow({ store }) {
if (store.highlightedIsbn.type === "todo") return null;
const isbn = store.highlightedIsbn.relative;
const loc = getPlanePosition(store.projection, isbn, isbn);
return (
<>
<group position={[loc.position[0], loc.position[1], 2]}>
{/* <Plane args={[loc.width, loc.height]} material={material} />*/}
<Html style={{ pointerEvents: "none" }} zIndexRange={[19, 19]}>
<HighlightCircle store={store} />
</Html>
<group position={[0, -loc.height / 2, 0]}>
{/*<HighlightCircle store={store} />*/}
<Html
style={{ pointerEvents: "none" }}
zIndexRange={[20, 20]}
// wrapperClass="highlight-wrapper"
>
<IsbnInfo store={store} />
</Html>
</group>
</group>
</>
);
},
);
const HighlightCircle: React.FC<{ store: Store }> = observer(
function HighlightCircle(props: { store: Store }) {
const store = props.store;
const circleRadius = 30;
const circleStroke = 4;
if (store.floatZoomFactor > 5000) return null;
const svg = (s: React.CSSProperties) => (
<svg
style={{
position: "absolute",
top: -circleRadius,
left: -circleRadius,
...s,
}}
width={circleRadius * 2}
height={circleRadius * 2}
viewBox={`0 0 ${circleRadius * 2} ${circleRadius * 2}`}
>
{" "}
<circle
cx={circleRadius}
cy={circleRadius}
r={circleRadius - circleStroke}
stroke="white"
strokeWidth={circleStroke}
fill="none"
/>
</svg>
);
return (
<div>
{svg({ filter: "drop-shadow(0 0 4px black)", zIndex: 0 })}
{/*svg({ zIndex: 30 })*/}
</div>
);
},
);
const IsbnInfo = observer(function IsbnInfo(props: { store: Store }) {
const o = props.store.highlightedIsbn;
if (o.type === "todo") return "Hover to see ISBN info";
let groupInfo;
if (o.obj) {
const i = o.obj;
if (!i.prefix) return <div>imposs: no prefix?</div>;
const prefixLen = i.prefix.length + i.group.length;
const totalDigits = 13 - 1; // 13 minus check digit
const numBooksInGroup = 10 ** (totalDigits - prefixLen);
const numBooksInPublisher =
10 ** (totalDigits - prefixLen - i.publisher.length);
groupInfo = (
<div>
<GroupInfo obj={o.obj} groupInfo={o.groupInfo} />
<br />
{/*Article: {i.article}*/}
{o.rarity &&
(o.rarity.bookCount === 0 ? (
<>(no holding data)</>
) : (
<>
{o.rarity.holdingCount === 255 ? ">250" : o.rarity.holdingCount}{" "}
known libraries hold copies of{" "}
{o.rarity.editionCount >= 254 ? ">250" : o.rarity.editionCount}{" "}
editions of this book
</>
))}
<br />
<details>
<summary style={{ pointerEvents: "auto", cursor: "pointer" }}>
Details
</summary>
Num possible ISBNs in group: {siNumber(numBooksInGroup)} <br />
Num possible ISBNs in publisher: {siNumber(numBooksInPublisher)}{" "}
<br />
{o.groupInfo.flatMap((g) =>
(g.info ?? []).map((info, i) => (
<li key={info.prefix + i}>
{info.source === "publisher-ranges" ? (
<>
Group {info.prefix}: {info.name}
</>
) : (
<>
Publisher {info.prefix}: {info.registrant_name} (
{info.country_name})
</>
)}
</li>
)),
)}
</details>
Look up book on
<ul>
{props.store.externalSearchEngines.map((d) => (
<li key={d.name}>
<a href={d.url.replace("%s", o.isbn)} target="_blank">
{d.name}
</a>
</li>
))}
</ul>
</div>
);
} else {
groupInfo = <div>Unassigned or unknown range</div>;
}
const isbn = o.obj?.isbn13h ?? relativeToFullIsbn(o.relative);
return (
<>
<div
className="isbn-highlight"
style={{ pointerEvents: "auto" }}
onWheelCapture={(e) => {
e.stopPropagation();
}}
>
<button
className="float-button"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
props.store.highlightedIsbn = { type: "todo" };
}}
>
Close
</button>
<button
className="float-button"
onClick={() => {
props.store.zoomAnimateToHighlight();
}}
>
Fly to book
</button>
Book:{" "}
{o.googleBookDetails === "todo" ? (
<>{isbn}...</>
) : o.googleBookDetails === null ? (
<>{isbn} (not found on Google Books)</>
) : (
<>
<img
src={
o.googleBookDetails.volumeInfo.imageLinks?.smallThumbnail ??
undefined
}
style={{ float: "left" }}
/>
<b>{o.googleBookDetails.volumeInfo.title}</b>
<br />
by {o.googleBookDetails.volumeInfo.authors?.join(", ")}
<br />
ISBN: {isbn}
</>
)}
<br />
{groupInfo}
</div>
</>
);
});
function GroupInfo({
obj,
groupInfo,
}: {
obj: ISBN;
groupInfo: LazyPrefixInfo[];
}) {
const publisherPrefix = `${obj.prefix}${obj.group}${obj.publisher}`;
const computedPublisherInfo = groupInfo.find((g) => {
const myPrefix = g.info?.[0]?.prefix;
return myPrefix && removeDashes(myPrefix) === publisherPrefix;
});
return (
<>
Group {obj.prefix}-{obj.group}: <i>{obj.groupname}</i> <br />
Publisher {obj.publisher}:{" "}
<i>
{computedPublisherInfo?.info &&
computedPublisherInfo.info[0].source === "isbngrp"
? computedPublisherInfo.info[0].registrant_name
: "unknown"}
</i>{" "}
{(computedPublisherInfo?.info?.length ?? 0) > 1
? `(+${(computedPublisherInfo?.info?.length ?? 0) - 1} more)`
: ""}
</>
);
}
================================================
FILE: src/components/ImageTree.tsx
================================================
import { Plane } from "@react-three/drei";
import { observer, useLocalObservable } from "mobx-react-lite";
import { fromPromise } from "mobx-utils";
import { useEffect, useState } from "react";
import { Blending } from "three";
import { DetailLevelObservable } from "../lib/DetailLevelObservable";
import { Store } from "../lib/Store";
import {
digits,
isbnPrefixAppend,
isbnPrefixToRelative,
IsbnPrefixWithDashes,
IsbnPrefixWithoutDashes,
ProjectionConfig,
removeDashes,
} from "../lib/util";
export const ImageTree: React.FC<{
config: ProjectionConfig;
store: Store;
prefix: IsbnPrefixWithDashes;
blending: Blending;
}> = observer(function _ImageTree(props) {
const groupPrefix = removeDashes(props.prefix);
const view = props.store.getDetailLevel(groupPrefix);
if (!view.container) return null;
return <GroupShowInner {...props} view={view} />;
});
const GroupShowInner: React.FC<{
config: ProjectionConfig;
store: Store;
prefix: IsbnPrefixWithDashes;
view: DetailLevelObservable;
blending: Blending;
}> = observer(function _GroupShowInner({ view, ...props }) {
const { position, width, height } = view.planePosition;
const groupPrefix = removeDashes(props.prefix);
const [hasChildren, setHasChildren] = useState(false);
useEffect(() => {
void (async () => {
setHasChildren(
(
await Promise.all(
props.store.shaderUtil.shaderProgram.requiredTextures.map(
(dataset) =>
props.store
.imageLoader(dataset)
.getHasChildren(isbnPrefixToRelative(groupPrefix)),
),
)
).some((e) => e),
);
})();
}, [groupPrefix, props.store.shaderUtil.shaderProgram.requiredTextures]);
return (
<>
{view.image && (
<PrefixImage
store={props.store}
prefix={groupPrefix}
position={[
position[0],
position[1],
position[2] + groupPrefix.length / 10,
]}
args={[width, height]}
blending={props.blending}
/>
)}
{view.imageChildren &&
hasChildren &&
digits.map((i) => {
return (
<ImageTree
key={props.prefix + i}
prefix={isbnPrefixAppend(props.prefix, String(i))}
config={props.config}
store={props.store}
blending={props.blending}
/>
);
})}
</>
);
});
const PrefixImage: React.FC<{
store: Store;
prefix: IsbnPrefixWithoutDashes;
position: [number, number, number];
args: [number, number];
blending: Blending;
}> = observer((props) => {
const prefix = isbnPrefixToRelative(props.prefix);
const { material } = useLocalObservable(() => ({
get _material() {
return fromPromise(props.store.shaderUtil.getIsbnShaderMaterial(prefix));
},
get material() {
return this._material.case({
fulfilled: (m) => {
if (m) {
m.refreshUniforms();
return m.material;
}
},
});
},
}));
if (!material) return null;
return (
<Plane
// ref={pl}
key={material.id} // react threejs does not update material https://github.com/pmndrs/react-three-fiber/issues/2839
material={material}
position={props.position}
args={props.args}
/>
);
});
================================================
FILE: src/components/IsbnGrid.tsx
================================================
import { Grid } from "@react-three/drei";
import { computed } from "mobx";
import { observer, useLocalObservable } from "mobx-react-lite";
import config from "../config";
import { Store } from "../lib/Store";
import { totalIsbns } from "../lib/util";
export const IsbnGrid: React.FC<{ store: Store }> = observer(function IsbnGrid({
store,
}) {
const zoomLevel = computed(() => {
const zoom = store.floatZoomFactor;
const zoomLevel = Math.round(Math.log10(zoom) * 2) - 1;
if (zoomLevel < 0) return 0;
const maxZoom = 6;
if (zoomLevel > maxZoom) return maxZoom;
return zoomLevel;
}).get();
const maxShowZoom = store.runtimeConfig.doBookshelfEffect ? 6 : 8;
const color =
zoomLevel > 4 ? config.bookshelfColorHex : store.runtimeConfig.gridColor;
return (
<>
<IsbnGridLevel
key={zoomLevel}
zoomLevel={zoomLevel}
store={store}
thickness={3}
z={1.2}
color={color}
/>
{store.runtimeConfig.gridLevels >= 2 && zoomLevel + 1 <= maxShowZoom && (
<IsbnGridLevel
key={zoomLevel + 1}
zoomLevel={zoomLevel + 1}
store={store}
thickness={2}
z={1.1}
color={"#333333"}
/>
)}
{store.runtimeConfig.gridLevels >= 3 && zoomLevel + 2 <= maxShowZoom && (
<IsbnGridLevel
key={zoomLevel + 2}
zoomLevel={zoomLevel + 2}
store={store}
thickness={1}
z={1.0}
color={"#333333"}
/>
)}
</>
);
});
const IsbnGridLevel: React.FC<{
store: Store;
zoomLevel: number;
thickness: number;
z: number;
color: string;
}> = observer(function IsbnGridLevel(props) {
const { store } = props;
const thickness = props.zoomLevel > 4 ? props.thickness + 1 : props.thickness;
const pwidth = store.projection.pixelWidth;
const pheight = store.projection.pixelHeight;
const outerGridWidth = 10;
const outerGridHeight = totalIsbns / 1e9;
const width = outerGridWidth * 10 ** Math.floor(props.zoomLevel / 2);
const height = outerGridHeight * 10 ** Math.floor((props.zoomLevel + 1) / 2);
let innerOnly = 1;
if (props.zoomLevel > 5) {
innerOnly = 100;
}
const { position } = useLocalObservable(
() => ({
get position() {
let position: [number, number, number] = [
store.projection.pixelWidth / 2,
-store.projection.pixelHeight / 2,
props.z,
];
if (props.zoomLevel > 5) {
position = [
store.view.minX + store.view.width / 2,
-(store.view.minY + store.view.height / 2),
props.z,
];
position[0] -= position[0] % (pwidth / width);
position[1] -= position[1] % (pheight / height);
}
return position;
},
}),
{ position: computed.struct },
);
return (
<Grid
args={[width / innerOnly, height / innerOnly]}
cellSize={0}
sectionColor={props.color}
sectionThickness={thickness}
sectionSize={1}
scale={[pwidth / width, 1, pheight / height]}
position={position}
rotation={[Math.PI / 2, 0, 0]}
fadeDistance={1000}
// cellThickness={2}
/>
);
});
================================================
FILE: src/components/IsbnMap.tsx
================================================
import { OrbitControls, OrthographicCamera, Plane } from "@react-three/drei";
import { Canvas, ThreeEvent } from "@react-three/fiber";
import * as isbnlib from "isbn3";
import { observer } from "mobx-react-lite";
import React, { useEffect, useMemo, useState } from "react";
import * as THREE from "three";
import { MeshStandardMaterial, NoToneMapping } from "three";
import { shaderErrorToString } from "../lib/shader-error";
import { Store } from "../lib/Store";
import { IsbnPrefixWithDashes, ProjectionConfig } from "../lib/util";
import { Controls } from "./Controls";
import { HighlightShow, PublisherHighlightShow } from "./Highlight";
import { ImageTree } from "./ImageTree";
import { IsbnGrid } from "./IsbnGrid";
import { MiniMap } from "./MiniMap";
import { StatsShow } from "./StatsShow";
import { TextTree } from "./TextTree";
Object.assign(window, { isbnlib });
let pointerMoved = 0;
let isPointerDown = -1;
let cancelHighlight = false;
export const IsbnMap: React.FC<{ config: ProjectionConfig }> = observer(
function IsbnView(props: { config: ProjectionConfig }) {
const [store] = useState(() => new Store(props.config));
Object.assign(window, { store });
useEffect(() => {
function cancelHighlightListener() {
if (cancelHighlight) store.highlightedPublisher = null;
else cancelHighlight = true;
}
function cancelZoom() {
// cancel flight on scroll
cancelAnimationFrame(store.animationRequestId);
}
window.addEventListener("wheel", cancelZoom);
window.addEventListener("pointermove", cancelHighlightListener);
return () => {
window.removeEventListener("wheel", cancelZoom);
window.removeEventListener("pointermove", cancelHighlightListener);
};
}, []);
const transparent = useMemo(
() =>
new MeshStandardMaterial({
color: "green",
transparent: true,
opacity: 0,
}),
[],
);
return (
<>
<Canvas
style={{
width: "100%",
height: "100%",
background: "black",
// position: "relative",
}}
flat={true}
onCreated={(threejsRoot) => {
Object.assign(window, { threejsRoot });
// threejsRoot.gl.debug.onShaderError = e => store.shaderError = e;
store.camera = threejsRoot.camera as THREE.OrthographicCamera;
threejsRoot.gl.debug.onShaderError = (...args) => {
const err = shaderErrorToString(...args);
console.warn(err);
store.shaderError = err;
};
}}
scene={{ background: new THREE.Color("#1d2636") }}
gl={{ toneMapping: NoToneMapping }}
>
<OrthographicCamera makeDefault position={[0, 0, 100]} zoom={0.8} />
<OrbitControls
ref={(e) => {
store.orbitControls = e;
}}
enableDamping={false}
makeDefault
enableRotate={false}
enablePan={true}
mouseButtons={{ LEFT: THREE.MOUSE.PAN }}
zoomToCursor={true}
minZoom={0.5}
maxZoom={20000}
touches={{ ONE: THREE.TOUCH.PAN, TWO: THREE.TOUCH.DOLLY_PAN }}
onChange={(e) => {
store.updateView(e);
}}
/>
{/*<ambientLight intensity={3} />*/}
<Plane
position={[0, 0, 0]}
material={transparent}
args={[props.config.pixelWidth, props.config.pixelHeight]}
onPointerDown={(e: ThreeEvent<PointerEvent>) => {
cancelAnimationFrame(store.animationRequestId);
pointerMoved = 0;
isPointerDown = e.button;
if (e.button === 2) {
store.highlightedPublisher = null;
const x = e.point.x + props.config.pixelWidth / 2;
const y = props.config.pixelHeight / 2 - e.point.y;
store.updateHighlightedStats(x, y, "start");
}
}}
onPointerUp={(e: ThreeEvent<PointerEvent>) => {
isPointerDown = -1;
if (
(e.nativeEvent.target as Element | null)?.tagName !== "CANVAS"
)
return;
if (pointerMoved < 4) {
e.stopPropagation();
const x = e.point.x + props.config.pixelWidth / 2;
const y = props.config.pixelHeight / 2 - e.point.y;
if (e.button === 2) {
// store.updateStats(x, y, "end");
store.highlightedStats = null;
} else {
store.updateHighlight(x, y, false);
}
}
}}
onPointerMove={(e: ThreeEvent<PointerEvent>) => {
cancelHighlight = false;
pointerMoved++;
const x = e.point.x + props.config.pixelWidth / 2;
const y = props.config.pixelHeight / 2 - e.point.y;
if (isPointerDown === 2) {
if (
(e.nativeEvent.target as Element | null)?.tagName !== "CANVAS"
)
return;
store.updateHighlightedStats(x, y, "end");
}
if (isPointerDown === -1 && e.pointerType === "mouse") {
if (
(e.nativeEvent.target as Element | null)?.tagName !== "CANVAS"
)
return;
e.stopPropagation();
store.updateHighlight(x, y, true);
}
}}
/>
<group
position={[
-props.config.pixelWidth / 2,
props.config.pixelHeight / 2,
0,
]}
>
<PublisherHighlightShow store={store} />
<HighlightShow store={store} />
<StatsShow store={store} />
{store.runtimeConfig.showPublisherNames && (
<>
<TextTree
config={props.config}
prefix={"978-" as IsbnPrefixWithDashes}
store={store}
/>
<TextTree
config={props.config}
prefix={"979-" as IsbnPrefixWithDashes}
store={store}
/>
</>
)}
<ImageTree
store={store}
config={props.config}
prefix={"978-" as IsbnPrefixWithDashes}
blending={THREE.NormalBlending}
/>
<ImageTree
store={store}
config={props.config}
prefix={"979-" as IsbnPrefixWithDashes}
blending={THREE.NormalBlending}
/>
{store.runtimeConfig.showGrid && <IsbnGrid store={store} />}
</group>
</Canvas>
<Controls store={store} />
<MiniMap store={store} />
</>
);
},
);
================================================
FILE: src/components/Legend.tsx
================================================
import { observer } from "mobx-react-lite";
import Select, {
components,
OptionProps,
SingleValueProps,
} from "react-select";
import { defaultColorSchemeMeaning } from "../config";
import { Store } from "../lib/Store";
export const gradientsPngUrl = new URL(
"../assets/gradients.png",
import.meta.url,
).toString();
const w = 230;
const h = 20;
const totalGradientsInPng = 9;
const options = Array.from({ length: totalGradientsInPng })
.map((_, value) => ({
value,
}))
.filter((e) => e.value !== 1);
export const Legend: React.FC<{ store: Store }> = observer(
function Legend(props) {
const dataset = props.store.currentDataset;
let meaning = dataset.colorSchemeMeaning;
if (meaning === null) return;
meaning ??= defaultColorSchemeMeaning;
return (
<div>
<Select<{ value: number }>
isSearchable={false}
value={options.find(
(o) => o.value === props.store.runtimeConfig.colorGradient,
)}
getOptionValue={(e) => e.value.toString()}
onChange={(e) => {
if (e) props.store.runtimeConfig.colorGradient = e.value;
}}
options={options}
components={{
Option: ColorGradientOption,
SingleValue: ColorGradientSingleValue,
}}
store={props.store}
/>
</div>
);
},
);
const ColorGradientOption: React.FC<OptionProps<{ value: number }>> = (p) => {
return (
<components.Option {...p}>
<Gradient value={p.data.value} />
</components.Option>
);
};
const ColorGradientSingleValue: React.FC<SingleValueProps<{ value: number }>> =
observer((p) => {
const meaning =
p.selectProps.store.currentDataset.colorSchemeMeaning ??
defaultColorSchemeMeaning;
return (
<components.SingleValue {...p}>
{/* <div style={{ textAlign: "center" }}>Legend</div> */}
<div style={{ position: "relative", marginLeft: "2.2em" }}>
<Gradient value={p.data.value} />
{meaning.markers.map((m) => (
<div
key={m.value}
style={{
left: w * m.value,
top: 0,
position: "absolute",
transform: "translate(-50%, 0)",
}}
>
<div
style={{
width: 0,
borderLeft: "1px solid black",
height: h,
marginLeft: "50%",
}}
/>
{m.label}
</div>
))}
<div style={{ height: "1.5em" }} />
</div>
</components.SingleValue>
);
});
const Gradient: React.FC<{ value: number }> = (props) => (
<div
style={{
backgroundImage: `url(${gradientsPngUrl})`,
width: w,
height: h,
backgroundPosition: `0px ${-1 * 20 * props.value}px`,
backgroundSize: `${w}px ${h * totalGradientsInPng}px`,
}}
/>
);
================================================
FILE: src/components/MiniMap.tsx
================================================
/* eslint-disable @typescript-eslint/unbound-method -- mobx binds observable methods */
import { computed } from "mobx";
import { Observer, observer, useLocalObservable } from "mobx-react-lite";
import React, { useRef } from "react";
import { Store } from "../lib/Store";
import {
firstIsbnInPrefix,
isbnPrefixFromRelative,
IsbnPrefixRelative,
lastIsbnInPrefix,
} from "../lib/util";
import { getPlanePosition } from "../lib/view-utils";
// Default blocks configuration
const DEFAULT_BLOCKS = [
{ pos: "00", text: "EN" }, // Row 0, Col 0
{ pos: "01", text: "EN" }, // Row 0, Col 1
{ pos: "02", text: "FR" }, // Row 0, Col 2
{ pos: "03", text: "DE" }, // Row 0, Col 3
{ pos: "04", text: "JP" }, // Row 0, Col 4
{ pos: "05", text: "SU" }, // R1ow 0, Col 5
{ pos: "07", text: "CN" }, // Row 0, Col 7
{ pos: "18", text: "US" }, // Row 1, Col 8
{ pos: "065", text: "BR" },
// XX blocks for column 6 (prefix '0' for row 0)
...Array.from({ length: 4 }, (_, i) => ({
pos: `06${i}`,
text: `6${i}`,
})),
// XX blocks for column 8 (prefix '0' for row 0)
...Array.from({ length: 10 }, (_, i) => ({
pos: `08${i}`,
text: ["CS", "IN", "NO", "PL", "ES", "BR", "YU", "DK", "IT", "KR"][i],
})),
// XX blocks for column 9 (prefix '0' for row 0)
...Array.from({ length: 10 }, (_, i) => ({
pos: `09${i}`,
text: ["NL", "SE", "", "IN", "NL"][i] ?? `9${i}`,
})),
{ pos: "110", text: "FR" },
{ pos: "111", text: "KR" },
{ pos: "112", text: "IT" },
{ pos: "113", text: "ES" },
] as BlockConfig[];
interface Overlay {
x: number;
y: number;
width: number;
height: number;
}
interface BlockConfig {
pos: IsbnPrefixRelative; // Two digits (row/col) for main blocks, four digits (row/col/subdivision) for XX
text: string;
color?: string;
}
interface MinimapSVGProps {
blocks?: BlockConfig[];
store: Store;
}
// Constants
const SQRT10 = Math.sqrt(10);
const WIDTH = 100;
const HEIGHT = WIDTH * (2 / SQRT10);
const ROW_HEIGHT = HEIGHT / 2;
const CELL_WIDTH = WIDTH / 10;
const XX_HEIGHT = ROW_HEIGHT / 10;
let dragDistance = 0;
const MinimapSVG: React.FC<MinimapSVGProps> = observer(
({ blocks = DEFAULT_BLOCKS, store }) => {
const svgRef = useRef<SVGSVGElement>(null);
const scale = store.projection.pixelWidth / WIDTH;
const state = useLocalObservable(() => ({
isDragging: false,
dragStart: { x: 0, y: 0 },
get overlay() {
const fakeScale = 0.5;
const w = store.view.width / store.projection.pixelWidth;
const overlay = {
x: store.view.minX / scale + fakeScale,
y: store.view.minY / scale + fakeScale,
width: Math.max(0.5, store.view.width / scale - w * fakeScale * 2),
height: Math.max(0.5, store.view.height / scale - w * fakeScale * 2),
};
return overlay;
},
setOverlay(o: Overlay) {
store.setView(
(o.x + o.width / 2) * scale,
(o.y + o.height / 2) * scale,
);
},
// Convert screen coordinates to SVG coordinates
getLocalCoordinates(event: React.MouseEvent | MouseEvent): {
x: number;
y: number;
} {
if (!svgRef.current) return { x: 0, y: 0 };
const CTM = svgRef.current.getScreenCTM();
if (!CTM) return { x: 0, y: 0 };
const point = svgRef.current.createSVGPoint();
point.x = event.clientX;
point.y = event.clientY;
const transformed = point.matrixTransform(CTM.inverse());
return {
x: transformed.x,
y: transformed.y,
};
},
handleMouseDown(event: React.MouseEvent) {
dragDistance = 0;
event.preventDefault();
if (store.floatZoomFactor <= 1) return;
const coords = this.getLocalCoordinates(event);
this.isDragging = true;
this.dragStart = {
x: coords.x - this.overlay.x,
y: coords.y - this.overlay.y,
};
window.addEventListener("pointermove", this.handleMouseMove);
window.addEventListener("pointerup", this.handleMouseUp);
},
handleMouseMove(event: MouseEvent) {
if (!this.isDragging) return;
dragDistance++;
const coords = this.getLocalCoordinates(event);
const newX = Math.max(
0,
Math.min(WIDTH - this.overlay.width, coords.x - this.dragStart.x),
);
const newY = Math.max(
0,
Math.min(HEIGHT - this.overlay.height, coords.y - this.dragStart.y),
);
this.setOverlay({
...this.overlay,
x: newX,
y: newY,
});
},
handleMouseUp() {
this.isDragging = false;
window.removeEventListener("pointermove", this.handleMouseMove);
window.removeEventListener("pointerup", this.handleMouseUp);
},
}));
return (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
ref={svgRef}
// onMouseLeave={state.handleMouseUp}
onPointerDown={state.handleMouseDown}
>
{/* Background */}
<rect width={WIDTH} height={HEIGHT} fill="#1a1a1a" />
{/* Blocks */}
{blocks.map((b) => (
<RenderBlock key={b.pos} block={b} store={store} />
))}
{/* Grid lines */}
<g stroke="#333" strokeWidth="0.25">
{Array.from({ length: 11 }, (_, i) => (
<line
key={`vline-${i}`}
x1={i * CELL_WIDTH}
y1={0}
x2={i * CELL_WIDTH}
y2={HEIGHT}
/>
))}
{Array.from({ length: 3 }, (_, i) => (
<line
key={`hline-${i}`}
x1={0}
y1={i * ROW_HEIGHT}
x2={WIDTH}
y2={i * ROW_HEIGHT}
/>
))}
</g>
<Observer>
{() => {
const overlay = state.overlay;
const widthRatio = overlay.width / WIDTH;
return (
<>
<rect
pointerEvents="none"
x={overlay.x}
y={overlay.y}
rx={5 * widthRatio}
width={overlay.width}
height={overlay.height}
fill={`rgba(255,255,255,${Math.max(
0,
Math.min(1, 1 - widthRatio),
)})`}
stroke="#fff"
strokeWidth="0.5"
/>
{/* if very small, draw a white circle around the rect */}
{widthRatio < 0.02 && (
<circle
cx={overlay.x + overlay.width / 2}
cy={overlay.y + overlay.height / 2}
r={3}
fill="none"
stroke="#fff"
strokeWidth="0.5"
/>
)}
</>
);
}}
</Observer>
</svg>
);
},
);
// Helper to parse position string
const parsePosition = (pos: string) => {
if (pos.length === 2) {
// Main block: row/column
return {
row: parseInt(pos[0]),
column: parseInt(pos[1]),
subdivision: null,
};
} else if (pos.length === 3) {
// XX block: row/column/subdivision
return {
row: parseInt(pos[0]),
column: parseInt(pos[1]),
subdivision: parseInt(pos.slice(2)),
};
}
throw new Error(
"Position must be 2 digits for main blocks or 3 digits for XX blocks",
);
};
// Color generation
const generateColor = (pos: string): string => {
const { column, subdivision } = parsePosition(pos);
const isXX = subdivision !== null;
if (isXX) {
const baseHues: Record<number, number> = {
6: 180, // Cyan-based
8: 280, // Purple-based
9: 30, // Orange-based
};
const hue = (baseHues[column] || 0) + subdivision * 10;
return `hsl(${hue}, 80%, ${60 + subdivision * 2}%)`;
} else {
const baseColors: Record<string, string> = {
"00": "#4a90e2", // EN
"01": "#4a90e2", // EN
"02": "#50c878", // FR
"03": "#daa520", // DE
"04": "#ff6b6b", // JP
"05": "#9370db", // RU
"07": "#ff4500", // CN
"18": "#4169e1", // US
};
return baseColors[pos] || "#808080";
}
};
// Helper to determine if a position represents an XX block
const isXXBlock = (pos: string): boolean => pos.length === 3;
// Helper to get block dimensions
const getBlockDimensions = (pos: string) => {
const { row, column, subdivision } = parsePosition(pos);
if (subdivision !== null) {
return {
x: column * CELL_WIDTH,
y: row * ROW_HEIGHT + subdivision * XX_HEIGHT,
width: CELL_WIDTH,
height: XX_HEIGHT,
};
} else {
return {
x: column * CELL_WIDTH,
y: row * ROW_HEIGHT,
width: CELL_WIDTH,
height: ROW_HEIGHT,
};
}
};
const RenderBlock: React.FC<{ block: BlockConfig; store: Store }> = observer(
({ block, store }) => {
const { pos, text } = block;
const dims = getBlockDimensions(pos);
const isHovered = computed(() => store.minimapHoveredCell === pos).get();
const isXX = isXXBlock(pos);
function setHovered(pos: IsbnPrefixRelative | null) {
store.minimapHoveredCell = pos;
if (pos) {
const p = isbnPrefixFromRelative(pos);
store.highlightedStats = { prefixStart: p, prefixEnd: p };
} else {
store.highlightedStats = null;
}
}
return (
<g
onPointerEnter={() => {
setHovered(pos);
}}
onPointerLeave={() => {
setHovered(null);
}}
onClick={() => {
if (dragDistance > 4) return;
const start = firstIsbnInPrefix(isbnPrefixFromRelative(pos));
const end = lastIsbnInPrefix(isbnPrefixFromRelative(pos));
const p = getPlanePosition(store.projection, start, end);
store.zoomAnimateTo(
p.xStart + p.width / 2,
p.yStart + p.height / 2,
{ 2: 2, 3: 0.9 * Math.sqrt(10) ** 2 }[pos.length] ?? 1,
1,
);
}}
style={{ cursor: "pointer" }}
>
<rect
{...dims}
fill={block.color ?? generateColor(pos)}
opacity={isHovered ? 1 : 0.8}
stroke={isHovered ? "#fff" : isXX ? "#444" : "none"}
strokeWidth={isHovered ? "0.2" : "0.1"}
/>
<text
x={dims.x + dims.width / 2}
y={dims.y + dims.height / 2}
textAnchor="middle"
dominantBaseline="middle"
fill="white"
fontSize={isXX ? 2.5 : 4}
>
{text}
</text>
</g>
);
},
);
export const MiniMap: React.FC<{ store: Store }> = observer(
function MiniMap(props) {
return (
<div className="minimap">
<MinimapSVG store={props.store} />
{props.store.resetZoomButton && (
<button
style={{
position: "absolute",
bottom: 0,
left: "50%",
transform: "translateX(-50%)",
marginBottom: "0.5ex",
cursor: "pointer",
}}
onClick={() => {
props.store.resetZoomButton = false;
props.store.zoomAnimateTo(
props.store.projection.pixelWidth / 2,
props.store.projection.pixelHeight / 2,
1,
1,
);
}}
>
<small>Reset Zoom</small>
</button>
)}
</div>
);
},
);
================================================
FILE: src/components/SingleBookCover.tsx
================================================
import * as isbnlib from "isbn3";
import { observer } from "mobx-react-lite";
import { fromPromise } from "mobx-utils";
import { useMemo } from "react";
import { Store } from "../lib/Store";
import { IsbnStrWithChecksum } from "../lib/util";
import { EanBarcode } from "./EanBarcode";
function dot(v1: [number, number], v2: [number, number]) {
return v1[0] * v2[0] + v1[1] * v2[1];
}
// Helper function to emulate GLSL's fract function
// Returns the fractional part of a number
function fract(x: number) {
return x - Math.floor(x);
}
// Random function translated from GLSL
// Takes an array of 2 numbers (representing vec2)
function rande(co: [number, number]) {
return fract(Math.sin(dot(co, [12.9898, 78.233])) * 2);
}
export function bookHeight(bookIndex: [number, number]) {
const minBookHeight = 0.8;
const maxBookHeight = 0.95;
const r = 1.2;
const re = rande([bookIndex[0] * r, bookIndex[1] * r]);
return minBookHeight + (maxBookHeight - minBookHeight) * re;
}
export const SingleBookCover = observer(function SingleBookCover({
isbn,
store,
}: {
store: Store;
isbn: IsbnStrWithChecksum;
}) {
const fetchTitleJson = useMemo(
() => fromPromise(store.titleFetcher.fetchTitle(isbn)),
[isbn],
);
const titleInfo = store.cachedGoogleBooks.get(isbn);
const [y1, x1, y2, x2, y3, x3, _checksum] = isbn.slice(-7);
const [x, y] = [+(x1 + x2 + x3), +(y1 + y2 + y3)];
const bookHeightE = bookHeight([x, 999 - y]);
// console.log(isbn, x, y);
const title =
titleInfo?.volumeInfo.title ??
fetchTitleJson.case({ fulfilled: (t) => t?.title });
const author =
titleInfo?.volumeInfo.authors?.join(", ") ??
fetchTitleJson.case({ fulfilled: (t) => t?.creator });
return (
<div
className="single-book"
style={{ width: (bookHeightE * 100).toFixed(0) + "%" }}
>
<div className="isbn-and-barcode">
<div>
<div className="isbn">ISBN {isbnlib.hyphenate(isbn)}</div>
<EanBarcode ean={isbn} />
</div>
</div>
<div
className={`titleinfo ${
(title?.length ?? 0 + (author?.length ?? 0)) > 40
? "muchtext"
: "littletext"
}`}
>
<div className={`title ${!title ? "unknown" : ""}`}>{title}</div>
<div className={`author ${!author ? "unknown" : ""}`}>
{author ? `by ${author}` : ""}
</div>
</div>
</div>
);
});
================================================
FILE: src/components/StatsShow.tsx
================================================
import { Html, Plane } from "@react-three/drei";
import { observer, useLocalObservable } from "mobx-react-lite";
import { fromPromise } from "mobx-utils";
import React, { useMemo } from "react";
import { MeshBasicMaterial } from "three";
import { BlockStats } from "../lib/stats";
import { Store } from "../lib/Store";
import {
firstIsbnInPrefix,
IsbnPrefixWithDashes,
IsbnPrefixWithoutDashes,
lastIsbnInPrefix,
removeDashes,
} from "../lib/util";
import { getPlanePosition } from "../lib/view-utils";
export const maxZoomForStats = 40;
export const StatsShow: React.FC<{ store: Store }> = observer(
function StatsShow({ store }) {
const material = useMemo(
() =>
new MeshBasicMaterial({
color: "#ccffcc",
transparent: true,
opacity: 0.9,
}),
[],
);
const state = useLocalObservable(() => ({
get edge() {
if (!store.highlightedStats) return null;
let p1: string = store.highlightedStats.prefixStart;
let p2: string = store.highlightedStats.prefixEnd;
p1 = p1.slice(0, p2.length);
p2 = p2.slice(0, p1.length);
if (p2 < p1) [p1, p2] = [p2, p1];
while (p1.slice(0, -1) !== p2.slice(0, -1)) {
p1 = p1.slice(0, -1);
p2 = p2.slice(0, -1);
}
/*if (p2 > p1) {
p2 = String(+p2 - 1);
}*/
return [p1 as IsbnPrefixWithoutDashes, p2 as IsbnPrefixWithoutDashes];
},
get stats() {
if (!this.edge) return null;
return fromPromise(
store.statsCalculator.getStats(this.edge[0], this.edge[1]),
);
},
}));
if (!state.edge) return null;
const [p1, p2] = state.edge;
const start = firstIsbnInPrefix(p1);
const end = lastIsbnInPrefix(p2);
const plane = getPlanePosition(store.projection, start, end);
return (
<group position={[plane.position[0], plane.position[1], 2]}>
<Plane args={[plane.width, plane.height]} material={material} />
{store.floatZoomFactor > maxZoomForStats && (
<Html zIndexRange={[21, 21]}>
<div
className="stats-highlight"
style={{ transform: "translate(-50%, -50%)" }}
>
Zoom out to view stats
</div>
</Html>
)}
<Html
zIndexRange={[23, 23]}
position={[plane.width / 2, -plane.height / 2, 3]}
>
<div className="stats-highlight">
{state.stats?.case({
pending: () => <>Loading...</>,
rejected: () => <>Error</>,
fulfilled: (stats) => (
<>
<AbbrevStats
store={store}
prefixStart={
p1.replace(/^.../, (e) => e + "-") as IsbnPrefixWithDashes
}
prefixEnd={
p2.replace(/^.../, (e) => e + "-") as IsbnPrefixWithDashes
}
/>
<details>
<summary>Details</summary>
<table className="stats-table">
<tbody>
<tr>
<td>{(stats.dataset_all ?? 0).toLocaleString()}</td>
<td>
<b>books total</b>
</td>
<td></td>
</tr>
{Object.entries(stats).map((s) => (
<tr key={s[0]}>
<td>{s[1]?.toLocaleString()}</td>
<td>in {s[0]}</td>
<td>
(
{(((s[1] ?? 0) / (stats.dataset_all ?? 0)) * 100)
.toFixed(2)
.padStart(5, " ")}
%)
</td>
</tr>
))}
</tbody>
</table>
</details>
<small>Right click to close</small>
</>
),
})}
</div>
</Html>
</group>
);
},
);
export const AbbrevStats: React.FC<{
store: Store;
prefixStart: IsbnPrefixWithDashes;
prefixEnd: IsbnPrefixWithDashes;
}> = observer(function AbbrevStats({ store, prefixStart, prefixEnd }) {
const [p1, p2] = [removeDashes(prefixStart), removeDashes(prefixEnd)];
const stats = useMemo(
() => fromPromise(store.statsCalculator.getStats(p1, p2)),
[p1, p2],
);
return (
<div>
{stats.case({
fulfilled: (r) => (
<StatsSummary
stats={r}
prefixStart={prefixStart}
prefixEnd={prefixEnd}
/>
),
})}
</div>
);
});
function StatsSummary(props: {
stats: BlockStats;
prefixStart: IsbnPrefixWithDashes;
prefixEnd: IsbnPrefixWithDashes;
}) {
const r = props.stats;
if (!r.dataset_all) return <></>;
return (
<div>
Stats for <b>{props.prefixStart}</b>
{props.prefixStart !== props.prefixEnd ? <> to {props.prefixEnd}</> : ""}:
<br />
<b>Known books:</b> {r.dataset_all.toLocaleString()}
<br />
<b>dataset_md5:</b> {r.dataset_md5 ?? 0} (
{(((r.dataset_md5 ?? 0) / r.dataset_all) * 100).toFixed(2)}
%)
<br />
<b>Average publication year:</b>{" "}
{r.publication_date_count && (
<>
{((r.publication_date ?? 0) / r.publication_date_count).toFixed(0)} (
{r.publication_date_count.toFixed(0)} samples)
<br />
</>
)}
<b>Average holdings:</b>{" "}
{((r.rarity_holdingCount ?? 0) / (r.rarity_exists ?? 0)).toFixed(1)}{" "}
libraries
<br />
<b>Average editions:</b>{" "}
{((r.rarity_editionCount ?? 0) / (r.rarity_exists ?? 0)).toFixed(1)}
</div>
);
}
================================================
FILE: src/components/TextTree.tsx
================================================
import { Html } from "@react-three/drei";
import { observer } from "mobx-react-lite";
import { useEffect, useState } from "react";
import { useDelay } from "../lib/delayRender";
import { DetailLevelObservable } from "../lib/DetailLevelObservable";
import { DIGITS, LazyPrefixInfo } from "../lib/info-map";
import { getGroup, resolveOnePrefixLevel } from "../lib/prefix-data";
import { Store } from "../lib/Store";
import {
calculateCheckDigit,
digits,
isbnPrefixAppend,
IsbnPrefixWithDashes,
IsbnStrWithChecksum,
ProjectionConfig,
removeDashes,
} from "../lib/util";
import { SingleBookCover } from "./SingleBookCover";
export const TextTree: React.FC<{
config: ProjectionConfig;
store: Store;
prefix: IsbnPrefixWithDashes;
}> = observer(function _TextTree(props) {
const groupPrefix = removeDashes(props.prefix);
const view = props.store.getDetailLevel(groupPrefix);
if (!view.container) return null;
return <GroupShowInner {...props} view={view} />;
});
const GroupShowInner: React.FC<{
config: ProjectionConfig;
store: Store;
prefix: IsbnPrefixWithDashes;
view: DetailLevelObservable;
}> = observer(function _GroupShowInner({ view, ...props }) {
const { position, width, height } = view.planePosition;
const [groupO, setGroupO] = useState<LazyPrefixInfo | null>(null);
useEffect(() => {
void (async () => {
// resolve group plus one child level
let g = getGroup(props.store.rootPrefixInfo, props.prefix);
const jsonRoot = props.store.runtimeConfig.jsonRoot;
if (typeof g === "function")
g = await props.store.trackAsyncProgress(
`resolvePublishers(${props.prefix})`,
g(jsonRoot),
);
if (g?.children && "lazy" in g.children) {
await resolveOnePrefixLevel(g, jsonRoot);
}
setGroupO(g);
})();
}, [props.prefix]);
return (
<>
<RenderGroup
store={props.store}
prefix={props.prefix}
group={groupO}
position={position}
width={width}
height={height}
view={view}
/>
{view.textChildren &&
// groupO?.children &&
digits.map((i) => {
return (
<TextTree
key={props.prefix + i}
prefix={isbnPrefixAppend(
(groupO?.info?.[0].prefix
? groupO.info[0].prefix + "-"
: props.prefix) as IsbnPrefixWithDashes,
String(i),
)}
config={props.config}
store={props.store}
/>
);
})}
</>
);
});
const RenderGroup: React.FC<{
store: Store;
group: LazyPrefixInfo | null;
prefix: IsbnPrefixWithDashes;
position: [number, number, number];
width: number;
height: number;
view: DetailLevelObservable;
}> = observer(function _RenderGroup({
store,
group,
prefix,
position,
width,
height,
view,
}) {
const shouldDelay = useDelay();
if (!shouldDelay) return null;
const plainPrefix = removeDashes(prefix);
const isSingleBook = plainPrefix.length === 11;
// console.log("RenderGroup " + prefix);
if (!view.textOpacity || (!group?.children && !isSingleBook)) return null;
const smSize = Math.min(width, height);
const vertical = height > width;
const showVertical = store.runtimeConfig.groupTextVertical || isSingleBook;
if (group || isSingleBook) {
return (
<group position={[position[0], position[1], 20 - plainPrefix.length]}>
<Html
scale={smSize / 2 / Math.sqrt(10)}
zIndexRange={[12 - plainPrefix.length, 12 - plainPrefix.length]}
center
transform
sprite={false}
rotation={[0, 0, !vertical && showVertical ? Math.PI / 2 : 0]}
pointerEvents="none"
className={
(!vertical && !showVertical ? "vertical " : "") + "group-name-wrap"
}
style={{ opacity: view.textOpacity }}
>
{isSingleBook ? (
<ChildBooks
store={store}
showVertical={showVertical}
prefix={prefix}
vertical={vertical}
/>
) : group ? (
<ChildGroupNames
store={store}
showVertical={showVertical}
group={group}
prefix={prefix}
vertical={vertical}
/>
) : (
"impossible"
)}
</Html>
</group>
);
}
return null;
});
function ChildBooks(props: {
prefix: IsbnPrefixWithDashes;
vertical: boolean;
showVertical: boolean;
store: Store;
}) {
const plainPrefix = removeDashes(props.prefix);
return (
<div>
{DIGITS.map((digit) => (
<div key={digit} className={"single-book-wrap"}>
<SingleBookCover
store={props.store}
isbn={
(plainPrefix +
digit +
calculateCheckDigit(plainPrefix + digit)) as IsbnStrWithChecksum
}
/>
</div>
))}
</div>
);
}
function ChildGroupNames(props: {
prefix: IsbnPrefixWithDashes;
group: LazyPrefixInfo;
vertical: boolean;
showVertical: boolean;
store: Store;
}) {
if (!props.group.children) return null;
if ("lazy" in props.group.children) {
console.warn("lazy group, should be impossible", props.prefix);
return null;
}
const children = props.group.children;
const prefixWithAppendedDash = props.group.info?.[0].prefix
? props.group.info[0].prefix + "-"
: props.prefix;
return (
<div>
{DIGITS.map((digit) => {
const child = children[digit];
return (
<div
key={digit}
className={
"group-name " +
(!props.vertical && !props.showVertical ? "vertical " : "")
}
>
{child && (
<GroupNameTxt
prefix={prefixWithAppendedDash + digit}
group={child}
/>
)}
</div>
);
})}
</div>
);
}
const GroupNameTxt = function GroupName(props: {
prefix: string;
group: LazyPrefixInfo;
}) {
const firstInfo = props.group.info?.[0];
const infoCount = props.group.info?.length ?? 0;
if (!firstInfo) {
return (
<small>
{`${props.group.totalChildren} publisher${
props.group.totalChildren > 1 ? "s" : ""
}`}
<br />
<small>{`(${props.prefix})`}</small>
</small>
);
}
if (firstInfo.source === "isbngrp") {
return (
<>
{firstInfo.registrant_name}
<br />
<small>
{`(${firstInfo.prefix}) ${
infoCount > 1 ? `(+${infoCount - 1} more)` : ""
}`}
</small>
</>
);
}
return (
<>
{firstInfo.name}
<br />
<small>{`(${firstInfo.prefix}-)`}</small>
</>
);
};
================================================
FILE: src/config.ts
================================================
import { MinimalGoogleBooksItem } from "./components/Controls";
import { RuntimeConfiguration } from "./lib/RuntimeConfiguration";
import { IsbnStrWithChecksum } from "./lib/util";
export interface DatasetOption {
id: string;
name: string;
description?: string;
runtimeConfig?: Partial<RuntimeConfiguration>;
colorSchemeMeaning?: ColorSchemeMeaning | null;
}
export interface ColorSchemeMeaning {
title: string;
markers: { value: number; label: string }[];
}
export const defaultColorSchemeMeaning = {
title: "Books",
markers: [
{ value: 0, label: "0%" },
{ value: 0.5, label: "50% allocated" },
{ value: 1, label: "100%" },
],
};
export default {
bookshelfColor: [0.5, 0.1, 0.1, 1.0],
bookshelfColorHex: "#7f1a1a",
datasetOptions: [
{
id: "all",
name: "All Known Books",
description: "Books in various sources",
},
{
id: "publication_date",
name: "Publication Date",
description: "Shows the publication year of books",
runtimeConfig: {
shaderGlow: 4,
colorGradient: 2,
},
colorSchemeMeaning: {
title: "Publication year",
markers: [
{ value: 0, label: "≤1985" },
{ value: 0.25, label: "" },
// { value: (2000 - 1985) / (2025 - 1985), label: "2000" },
{ value: 0.5, label: String((2025 + 1985) / 2) },
{ value: 0.75, label: "" },
{ value: 1, label: "2025" },
],
},
},
{
id: "all-md5",
name: "All ISBNs (red), md5s (green)",
description:
"Shows which proportion of books have at least one file in AA.",
runtimeConfig: {
colorGradient: 4,
},
colorSchemeMeaning: {
title: "File Availability",
markers: [
{ value: 0, label: "Missing" },
{ value: 0.5, label: "50% present" },
{ value: 1, label: "100%" },
],
},
},
{
id: "rarity",
name: "Rarity data",
description:
"Shows which books are rare, based on how many libraries they are in.",
colorSchemeMeaning: {
title: "Rarity",
markers: Array.from({ length: 21 }).map((_, i) => ({
value: (i / 20.0) ** 2,
label: { 0: "0 libraries", 10: "10", 20: "20+" }[i] ?? "",
})),
},
runtimeConfig: {
colorGradient: 4,
},
},
{
id: "publishers",
name: "Publisher Ranges",
description:
"Assigns a random color to each unique publisher so the prefixes of each one are visible.",
runtimeConfig: {
publishersColorSchema: "hsl",
},
colorSchemeMeaning: null,
},
{
id: "gbooks",
name: "Google Books",
description: "Books that are or were present in Google Books are white.",
},
{ id: "md5", name: "Files in AA" },
{ id: "cadal_ssno", name: "CADAL SSNOs" },
{ id: "cerlalc", name: "CERLALC data leak" },
{ id: "duxiu_ssid", name: "DuXiu SSIDs" },
{ id: "edsebk", name: "EBSCOhost’s eBook Index" },
{ id: "goodreads", name: "Goodreads" },
{ id: "ia", name: "Internet Archive" },
{ id: "isbndb", name: "ISBNdb" },
{ id: "isbngrp", name: "ISBN Global Register of Publishers" },
{ id: "libby", name: "Libby" },
{ id: "nexusstc", name: "Nexus/STC" },
{ id: "oclc", name: "OCLC/Worldcat" },
{ id: "ol", name: "OpenLibrary" },
{ id: "rgb", name: "Russian State Library" },
{ id: "trantor", name: "Imperial Library of Trantor" },
] as DatasetOption[],
exampleBooks: [
{
id: "gatsby",
volumeInfo: {
title: "The Great Gatsby",
authors: ["F. Scott Fitzgerald"],
industryIdentifiers: [
{
type: "ISBN_13",
identifier: "9780743273565" as IsbnStrWithChecksum,
},
],
},
},
{
id: "ctacher",
volumeInfo: {
title: "The Catcher in the Rye",
authors: ["J.D. Salinger"],
industryIdentifiers: [
{
type: "ISBN_13",
identifier: "9780316769488" as IsbnStrWithChecksum,
},
],
},
},
{
id: "got",
volumeInfo: {
title: "A Game of Thrones",
authors: ["George R. R. Martin"],
industryIdentifiers: [
{
type: "ISBN_13",
identifier: "9780553381689" as IsbnStrWithChecksum,
},
],
},
},
{
id: "hp1",
volumeInfo: {
title: "Harry Potter and the Philosopher's Stone",
authors: ["J.K. Rowling"],
industryIdentifiers: [
{
type: "ISBN_13",
identifier: "9780590353427" as IsbnStrWithChecksum,
},
],
},
},
] as MinimalGoogleBooksItem[],
externalSearchEngines: [
{
name: "Google Books",
url: "https://books.google.com/books?vid=ISBN%s", //"https://www.google.com/search?udm=36&q=isbn%3A%s",
},
{ name: "Worldcat", url: "https://worldcat.org/isbn/%s" },
],
jsonCompression: "gzip",
};
================================================
FILE: src/index.css
================================================
html,
body {
width: 100%;
height: 100%;
box-sizing: border-box;
margin: 0;
font-family: system-ui, sans-serif;
}
* {
box-sizing: border-box;
}
#root {
width: 100%;
height: 100%;
--group-width: 252px;
--group-height: calc(var(--group-width) / sqrt(10));
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
.no-cursor {
pointer-events: none;
}
.controls {
position: absolute;
top: 1rem;
left: 1rem;
background: #ffffff;
padding: 1.5rem;
z-index: 100;
width: 400px;
max-width: calc(100vw - 2rem);
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
transition: all 0.3s ease;
max-height: calc(100vh - 2rem);
border: 1px solid rgba(229, 231, 235, 0.5);
}
.controls:hover {
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
.controls .head {
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content: space-between;
gap: 0.5rem;
margin-bottom: 0.75rem;
flex-wrap: wrap;
}
.controls .head b {
font-weight: 600;
color: #111827;
line-height: 1.1;
margin-right: 0.5rem;
}
button {
padding: 0.375rem 0.75rem;
border-radius: 6px;
border: 1px solid #e5e7eb;
background: #f9fafb;
color: #374151;
font-size: 0.875rem;
transition: all 0.2s ease;
cursor: pointer;
}
.controls button.preset {
display: block;
width: 100%;
}
.controls button:hover {
background: #f3f4f6;
border-color: #d1d5db;
}
.controls button:active {
background: #e5e7eb;
transform: translateY(1px);
}
.controls.advanced {
background: #f8fafc;
}
@media (max-width: 640px) {
.controls {
top: 0;
left: 0;
right: 0;
padding: 0.75rem;
max-width: 100vw;
border-radius: 0;
max-height: 70vh;
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
}
.controls .head {
padding-bottom: 0.5rem;
margin-bottom: 0.5rem;
gap: 0.375rem;
}
.controls .head b {
margin-right: 0.375rem;
}
.controls button {
padding: 0.25rem 0.5rem;
font-size: 0.813rem;
}
.controls p {
margin: 0.5rem 0;
font-size: 0.875rem;
line-height: 1.25;
}
.controls label {
font-size: 0.875rem;
}
}
.controls.advanced {
max-height: 100vh;
overflow-y: auto;
}
.controls .form-row {
display: flex;
align-items: center;
gap: 1rem;
}
.controls .form-row > div:first-child {
flex: 1;
}
.stats-table {
td:first-child {
text-align: right;
}
}
.group-name {
color: white;
/*text-shadow: rgb(0, 0, 0) 2px 0px 0px, rgb(0, 0, 0) 1.75517px 0.958851px 0px,
rgb(0, 0, 0) 1.0806px 1.68294px 0px, rgb(0, 0, 0) 0.141474px 1.99499px 0px,
rgb(0, 0, 0) -0.832294px 1.81859px 0px,
rgb(0, 0, 0) -1.60229px 1.19694px 0px, rgb(0, 0, 0) -1.97999px 0.28224px 0px,
rgb(0, 0, 0) -1.87291px -0.701566px 0px,
rgb(0, 0, 0) -1.30729px -1.51361px 0px,
rgb(0, 0, 0) -0.421592px -1.95506px 0px,
rgb(0, 0, 0) 0.567324px -1.91785px 0px,
rgb(0, 0, 0) 1.41734px -1.41108px 0px,
rgb(0, 0, 0) 1.92034px -0.558831px 0px, rgb(0, 0, 0) 0 0 8px,
rgb(0, 0, 0) 0 0 8px, rgb(0, 0, 0) 0 0 8px, rgb(0, 0, 0) 0 0 8px,
rgb(0, 0, 0) 0 0 8px, rgb(0, 0, 0) 0 0 8px;*/
/* border: 1px solid black; */
text-align: center;
align-content: center;
overflow: hidden;
width: var(--group-width);
font-size: 1.2rem;
height: var(--group-height);
-webkit-text-stroke: 7px black;
text-stroke: 7px black;
paint-order: stroke fill;
}
.group-name.vertical {
float: left;
width: var(--group-height);
height: var(--group-width);
font-size: 0.9rem;
}
.group-name-wrap.vertical {
width: calc(var(--group-width) * sqrt(10));
}
.group-name span {
/* background: black; */
}
.group-name small {
font-size: 75%;
color: #ddd;
}
.lds-dual-ring {
/* change color here */
color: #1c4c5b;
}
.lds-dual-ring,
.lds-dual-ring:after {
box-sizing: border-box;
}
.lds-dual-ring {
display: inline-block;
width: 20px;
height: 20px;
}
.lds-dual-ring:after {
content: " ";
display: block;
width: 16px;
height: 16px;
margin: 2px;
border-radius: 50%;
border: 6.4px solid currentColor;
border-color: currentColor transparent currentColor transparent;
animation: lds-dual-ring 1.2s linear infinite;
}
@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@font-face {
font-family: "Libre Barcode EAN13 Text";
src: url(./LibreBarcodeEAN13Text-Regular.ttf) format("truetype");
}
.ean13 {
font-family: "Libre Barcode EAN13 Text", "Adobe NotDef";
/* Setting this explicitly was necessary for IOS, version 13.7, Safari and Chrome.*/
font-size: 3rem;
font-feature-settings: "calt" 1;
overflow: auto;
}
.single-book-wrap {
width: var(--group-width);
height: var(--group-height);
overflow: hidden;
}
.single-book {
color: black;
width: 100%;
height: 100%;
/* border-right: 5px solid green; */
display: flex;
padding-left: 3%; /** shader bookshelf height 0.03 */
flex-direction: row;
align-items: center;
}
.single-book .titleinfo {
flex-grow: 1;
text-align: center;
}
.single-book .titleinfo .author {
font-size: 0.8rem;
}
.single-book .titleinfo.muchtext {
font-size: 0.7rem;
}
.single-book .titleinfo.muchtext .author {
font-size: 0.6rem;
}
.single-book .unknown {
opacity: 0.5;
}
.single-book .isbn {
font-size: 0.3rem;
}
.single-book .isbn-and-barcode > div {
display: block;
transform: rotate(90deg);
text-align: center;
}
.single-book .ean13 {
display: block;
font-size: 2.8rem;
}
.dataset-chooser-wrap {
background: rgba(0, 0, 0, 0.6);
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.dataset-chooser {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
min-width: 300px;
max-width: 400px;
max-height: 70vh;
overflow: auto;
padding: 1em;
box-shadow: 0 0 4px 4px black;
border-radius: 12px;
}
.dataset-chooser button {
display: inline-block;
}
.dataset-chooser button.choose-dataset {
margin-bottom: 1em;
width: 100%;
}
.isbn-highlight,
.stats-highlight {
background: white;
box-shadow: rgba(0, 0, 0, 0.2) 0px 8px 24px;
border-radius: 8px;
padding: 1rem;
min-width: 350px;
max-width: 450px;
font-size: 0.9375rem;
line-height: 1.5;
border: 1px solid rgba(0, 0, 0, 0.1);
max-height: 400px;
overflow-y: auto;
}
.isbn-highlight {
transform: translateX(-50%) translateY(10px);
}
.isbn-highlight h2 {
font-size: 1.25rem;
font-weight: 600;
margin: 0 0 0.5rem 0;
color: #111827;
}
.isbn-highlight .isbn-title {
font-size: 1rem;
font-weight: 600;
margin-bottom: 0.75rem;
}
.isbn-highlight .group-info {
margin: 0.75rem 0;
padding: 0.75rem 0;
border-top: 1px solid #e5e7eb;
border-bottom: 1px solid #e5e7eb;
}
.isbn-highlight .stats-section {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.5rem 1rem;
margin: 0.75rem 0;
}
.isbn-highlight .stats-label {
font-weight: 500;
color: #4b5563;
}
.isbn-highlight .stats-value {
color: #111827;
}
.isbn-highlight .instructions {
margin-top: 0.75rem;
font-weight: 500;
color: #4b5563;
}
.isbn-highlight small {
display: block;
margin-top: 0.25rem;
color: #6b7280;
}
.isbn-highlight button {
padding: 0.375rem 0.75rem;
border-radius: 6px;
border: 1px solid #e5e7eb;
background: #f9fafb;
color: #374151;
font-size: 0.875rem;
transition: all 0.2s ease;
cursor: pointer;
}
.isbn-highlight button:hover {
background: #f3f4f6;
border-color: #d1d5db;
}
.isbn-highlight img {
margin-right: 0.5rem;
max-height: 85px;
border: 1px solid #e5e7eb;
border-radius: 4px;
}
.isbn-highlight details {
margin-top: 0.75rem;
}
.isbn-highlight summary {
color: #4b5563;
font-weight: 500;
margin-bottom: 0.5rem;
}
.isbn-highlight ul {
margin: 0.5rem 0;
padding-left: 1.5rem;
}
.isbn-highlight li {
margin: 0.25rem 0;
}
.isbn-highlight a {
color: #2563eb;
text-decoration: none;
}
.isbn-highlight a:hover {
text-decoration: underline;
}
.float-button {
float: right;
margin-left: 1ex;
}
.minimap {
position: absolute;
right: 0;
background: white;
top: 0;
margin: 1rem;
width: 300px;
height: calc(300px * 2 / sqrt(10));
z-index: 100;
border-radius: 1em;
overflow: hidden;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
border: 2px solid black;
background: black;
}
@media (max-width: 640px) {
.minimap {
top: auto;
bottom: 0;
margin: 0;
width: 180px;
height: calc(180px * 2 / sqrt(10));
}
.minimap button {
padding: 0.1rem;
}
}
================================================
FILE: src/index.tsx
================================================
import { isWebGL2Available } from "@react-three/drei";
import { configure } from "mobx";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./index.css";
configure({ enforceActions: "never", computedRequiresReaction: true });
const root = document.getElementById("root");
if (!root) throw new Error("No root element found with id 'root'");
createRoot(root).render(
<StrictMode>
<App />
</StrictMode>,
);
function testCompat() {
const available = isWebGL2Available();
if (!available) alert("WebGL2 not available, please upgrade your browser!");
}
testCompat();
================================================
FILE: src/lib/DetailLevelObservable.ts
================================================
import { computed, makeObservable } from "mobx";
import { Store } from "./Store";
import {
IMG_WIDTH,
IsbnPrefixWithoutDashes,
IsbnRelative,
firstIsbnInPrefix,
lastIsbnInPrefix,
} from "./util";
import { getPlanePosition, simplifyView } from "./view-utils";
const minPrefixLength = 3; // 978-, 979
const maxPrefixLength = 11;
export class DetailLevelObservable {
planePosition: ReturnType<typeof getPlanePosition>;
rect: { xStart: number; xEnd: number; yStart: number; yEnd: number };
isbnStart: IsbnRelative;
isbnEnd: IsbnRelative;
parent: DetailLevelObservable | null = null;
constructor(
private store: Store,
private prefix: IsbnPrefixWithoutDashes,
) {
makeObservable(this, {
viewVisible: computed,
container: computed,
textOpacity: computed,
textChildren: computed,
imageChildren: computed,
image: computed,
});
this.parent =
prefix.length > 2
? store.getDetailLevel(prefix.slice(0, -1) as IsbnPrefixWithoutDashes)
: null;
this.isbnStart = firstIsbnInPrefix(prefix);
this.isbnEnd = lastIsbnInPrefix(prefix);
this.planePosition = getPlanePosition(
store.projection,
this.isbnStart,
this.isbnEnd,
);
this.rect = this.planePosition;
}
get viewVisible() {
if (this.parent?.viewVisible === "invisible") return "invisible";
if (this.parent?.viewVisible === "visible") return "visible";
const v = simplifyView(this.store.view, this.rect);
return v;
}
get container() {
// return this.prefix.length < 8;
// console.log("update container", this.prefix);
if (this.viewVisible === "invisible") return false;
return true;
}
get textOpacity() {
const innermost = this.prefix.length === maxPrefixLength;
const outermost = this.prefix.length === minPrefixLength;
const textSwitchLevel = this.store.runtimeConfig.textMinZoomLevel;
const textSwitchLevelFull = textSwitchLevel * 1.5 - textSwitchLevel;
const opa1 = outermost
? 1
: Math.max(
0,
Math.min(
1,
(getScale(this.rect, this.store, 0) - textSwitchLevel) /
textSwitchLevelFull,
),
);
// show 2 levels at the same time
const showLevels = this.store.runtimeConfig.textLevelCount;
const opa2 = innermost
? 1
: Math.max(
0,
Math.min(
1,
1 -
(getScale(this.rect, this.store, -showLevels) - textSwitchLevel) /
textSwitchLevelFull,
),
);
return Math.min(opa1, opa2);
}
get textChildren() {
const outermost = this.prefix.length === minPrefixLength;
return (
this.prefix.length <= maxPrefixLength &&
(outermost ||
getScale(this.rect, this.store, -1) >=
this.store.runtimeConfig.textMinZoomLevel)
);
}
#imageRelativeLevel(relativeLevel: number) {
if (this.viewVisible === "invisible") return false;
if (this.prefix.length === minPrefixLength) return true;
const nextLargerImgScale = getScale(
this.rect,
this.store,
relativeLevel + 1,
);
return (
this.prefix.length <= 6 &&
nextLargerImgScale >= this.store.runtimeConfig.imgMinZoomLevel
);
}
get image() {
return this.#imageRelativeLevel(0);
}
get imageChildren() {
return this.#imageRelativeLevel(-1);
}
}
export interface DetailLevel {
container: boolean;
text: boolean;
children: boolean;
}
export function getScale(
rect: { xEnd: number; xStart: number; yEnd: number; yStart: number },
store: Store,
relativeLevel: number,
) {
const imgWidthInPixels = (rect.xEnd - rect.xStart) * store.floatZoomFactor;
const isVertical = rect.xEnd - rect.xStart < rect.yEnd - rect.yStart;
const imgScale =
imgWidthInPixels / (isVertical ? IMG_WIDTH / Math.sqrt(10) : IMG_WIDTH);
const nextImgScale = imgScale * 10 ** (relativeLevel / 2);
return nextImgScale;
}
================================================
FILE: src/lib/ImageLoader.ts
================================================
import {
LinearFilter,
LinearMipMapLinearFilter,
MagnificationTextureFilter,
MinificationTextureFilter,
NearestFilter,
Texture,
TextureLoader,
} from "three";
import { Store } from "./Store";
import { IsbnPrefixRelative } from "./util";
export class ImageLoader {
path: string;
loader: TextureLoader = new TextureLoader();
textures = new Map<IsbnPrefixRelative, Texture>();
existing: Promise<Set<IsbnPrefixRelative>>;
hasChildren: Promise<Set<IsbnPrefixRelative>>;
static maxZoomPrefixLength = 4; // images with nearest zoom have prefix length 4
minFilter: MinificationTextureFilter = LinearMipMapLinearFilter;
magFilter: MagnificationTextureFilter = LinearFilter;
constructor(
root: string,
private dataset: string,
private store: Store,
) {
this.path = `${root}/${dataset}`;
this.minFilter = dataset === "publishers" ? NearestFilter : NearestFilter;
this.magFilter = dataset === "publishers" ? NearestFilter : NearestFilter;
this.existing = store.trackAsyncProgress(
`${this.path}/written.json`,
this.loadExisting(),
);
this.hasChildren = this.loadHasChildren();
}
private async loadExisting() {
try {
const res = await fetch(`${this.path}/written.json`);
const json = (await res.json()) as IsbnPrefixRelative[];
return new Set(json);
} catch (cause) {
throw Error(`Could not load written.json for ${this.dataset}`, { cause });
}
}
private async loadHasChildren() {
const existing = await this.existing;
const out = new Set<IsbnPrefixRelative>();
for (const prefix of existing) {
for (let i = 1; i < prefix.length; i++) {
out.add(prefix.slice(0, i) as Is
gitextract_ak0t4uty/ ├── .dockerignore ├── .github/ │ └── workflows/ │ └── deploy.yml ├── .gitignore ├── .vscode/ │ ├── launch.json │ └── settings.json ├── Dockerfile ├── LICENSE.md ├── README.md ├── eslint.config.mjs ├── flake.nix ├── index.html ├── package.json ├── scripts/ │ ├── gen-book-titles-sqlite.ts │ ├── gen-prefixes.ts │ ├── merge-stats.ts │ ├── minify-images.sh │ ├── minify-prefix-data.sh │ ├── process-all.sh │ ├── rarity/ │ │ ├── .gitignore │ │ ├── Cargo.toml │ │ └── src/ │ │ └── main.rs │ ├── write-images/ │ │ ├── ImageTiler.ts │ │ ├── index.ts │ │ └── modules/ │ │ ├── aggregate-dense.ts │ │ ├── index.ts │ │ ├── publication_date.ts │ │ ├── publishers.ts │ │ ├── rarity.ts │ │ └── single-sparse.ts │ └── write-titles.ts ├── src/ │ ├── App.tsx │ ├── components/ │ │ ├── Controls.tsx │ │ ├── EanBarcode.tsx │ │ ├── Highlight.tsx │ │ ├── ImageTree.tsx │ │ ├── IsbnGrid.tsx │ │ ├── IsbnMap.tsx │ │ ├── Legend.tsx │ │ ├── MiniMap.tsx │ │ ├── SingleBookCover.tsx │ │ ├── StatsShow.tsx │ │ └── TextTree.tsx │ ├── config.ts │ ├── index.css │ ├── index.tsx │ ├── lib/ │ │ ├── DetailLevelObservable.ts │ │ ├── ImageLoader.ts │ │ ├── RuntimeConfiguration.ts │ │ ├── Store.ts │ │ ├── TitleFetcher.ts │ │ ├── delayRender.ts │ │ ├── flight.ts │ │ ├── google-books.ts │ │ ├── info-map.ts │ │ ├── json-fetch.ts │ │ ├── prefix-data.ts │ │ ├── shader-error.ts │ │ ├── shaders.ts │ │ ├── stats.ts │ │ ├── types-select.d.ts │ │ ├── types.d.ts │ │ ├── util.ts │ │ └── view-utils.ts │ └── projections/ │ ├── bookshelf.ts │ ├── index.ts │ └── linear.ts ├── tsconfig.json └── vite.config.ts
SYMBOL INDEX (263 symbols across 42 files)
FILE: scripts/gen-book-titles-sqlite.ts
type Record (line 6) | interface Record {
function connect (line 27) | function connect(dbName: string) {
function load (line 46) | async function load(dbName: string, dataDir: string) {
FILE: scripts/gen-prefixes.ts
type JsonRecord (line 15) | interface JsonRecord {
function go (line 31) | async function go() {
FILE: scripts/rarity/src/main.rs
constant CHANNEL_BATCH_SIZE (line 15) | const CHANNEL_BATCH_SIZE: usize = 10000;
type OclcIdNumeric (line 18) | type OclcIdNumeric = u64;
type Isbn (line 19) | type Isbn = String;
type RawRecord (line 24) | enum RawRecord {
type TitleRecord (line 39) | struct TitleRecord {
type HoldingsRecord (line 56) | struct HoldingsRecord {
type JsonRecord (line 63) | struct JsonRecord {
type ParsedRecord (line 69) | enum ParsedRecord {
function format_si_number (line 83) | fn format_si_number(num: u64) -> String {
type ZstdStreamWithProgress (line 87) | struct ZstdStreamWithProgress<R: io::Read> {
function new (line 96) | fn new(reader: R, total_size: u64) -> Self {
function read (line 108) | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
function process_batch (line 130) | fn process_batch(lines: Vec<String>, record_count: u64) -> Vec<ParsedRec...
function parse_single_date (line 184) | fn parse_single_date(date: &str) -> Option<i64> {
function parse_publication_date (line 192) | fn parse_publication_date(record: &TitleRecord) -> Option<i64> {
function reader_thread (line 211) | fn reader_thread(reader: impl BufRead, sender: Sender<Vec<String>>) -> i...
function setup_database (line 233) | fn setup_database(conn: &Connection) -> rusqlite::Result<()> {
function main (line 265) | fn main() -> io::Result<()> {
function store_to_db (line 334) | fn store_to_db(
function get_memory_usage (line 370) | fn get_memory_usage() -> u64 {
FILE: scripts/write-images/ImageTiler.ts
class StatsAggregator (line 15) | class StatsAggregator {
method addStatistic (line 18) | addStatistic(isbn: IsbnRelative, obj: Record<string, number>) {
class ImageTiler (line 37) | class ImageTiler {
method constructor (line 48) | constructor(
method logProgress (line 67) | logProgress(progress: number) {
method init (line 72) | async init() {
method #getImage (line 76) | #getImage(relativeIsbn: number): ImageTile {
method colorIsbn (line 99) | colorIsbn(
method #writeAndPurgeImage (line 152) | async #writeAndPurgeImage(prefix: number) {
method writeImage (line 157) | async writeImage(prefix: number) {
method writeAll (line 184) | async writeAll() {
method purgeToLength (line 187) | async purgeToLength(len: number) {
method finish (line 195) | async finish() {
FILE: scripts/write-images/index.ts
type IsbnData (line 6) | type IsbnData = Partial<Record<string, Uint32Array>>;
type ImageTile (line 12) | interface ImageTile {
type ProcessSingleZoom (line 20) | type ProcessSingleZoom = (tiler: ImageTiler) => Promise<void>;
function processAllZoomLevels (line 21) | async function processAllZoomLevels(
function loadData (line 54) | async function loadData(
function main (line 70) | async function main() {
FILE: scripts/write-images/modules/aggregate-dense.ts
function colorImageWithDenseIsbns (line 6) | async function colorImageWithDenseIsbns(
function aggregateDatasets (line 24) | function aggregateDatasets(
function aggregateDense (line 55) | async function aggregateDense(
FILE: scripts/write-images/modules/publication_date.ts
function loadPublicationDateData (line 12) | function loadPublicationDateData(
function rarityModule (line 60) | function rarityModule(
function processPublicationData (line 69) | async function processPublicationData(
function postprocessPixels (line 90) | function postprocessPixels(image: ImageTile, totalBooksPerPixel: number) {
FILE: scripts/write-images/modules/publishers.ts
function processPublishersData (line 14) | async function processPublishersData(
function loadPublishersData (line 76) | async function loadPublishersData() {
function publishersModule (line 89) | async function publishersModule(): Promise<ProcessSingleZoom> {
FILE: scripts/write-images/modules/rarity.ts
function loadRarityData (line 12) | function loadRarityData(dbName: string, stats: StatsAggregator) {
function rarityModule (line 79) | function rarityModule(
function processRarityData (line 88) | async function processRarityData(
function postprocessPixels (line 113) | function postprocessPixels(image: ImageTile) {
FILE: scripts/write-images/modules/single-sparse.ts
constant INPUT_FILENAME (line 7) | const INPUT_FILENAME =
function colorImageWithSparseIsbns (line 11) | async function colorImageWithSparseIsbns(
function loadSparseDataToMemory (line 38) | async function loadSparseDataToMemory(): Promise<IsbnData> {
function singleSparse (line 65) | async function singleSparse(
FILE: scripts/write-titles.ts
function loadPublicationDateData (line 12) | function loadPublicationDateData(dbName: string) {
FILE: src/components/Controls.tsx
method fulfilled (line 36) | fulfilled(stats) {
type MinimalGoogleBooksItem (line 155) | interface MinimalGoogleBooksItem {
FILE: src/components/EanBarcode.tsx
function EanBarcode (line 1) | function EanBarcode(props: { ean: string }) {
FILE: src/components/Highlight.tsx
function GroupInfo (line 251) | function GroupInfo({
FILE: src/components/ImageTree.tsx
method _material (line 98) | get _material() {
method material (line 101) | get material() {
FILE: src/components/IsbnGrid.tsx
method position (line 81) | get position() {
FILE: src/components/IsbnMap.tsx
function cancelHighlightListener (line 30) | function cancelHighlightListener() {
function cancelZoom (line 34) | function cancelZoom() {
FILE: src/components/MiniMap.tsx
constant DEFAULT_BLOCKS (line 15) | const DEFAULT_BLOCKS = [
type Overlay (line 46) | interface Overlay {
type BlockConfig (line 53) | interface BlockConfig {
type MinimapSVGProps (line 59) | interface MinimapSVGProps {
constant SQRT10 (line 65) | const SQRT10 = Math.sqrt(10);
constant WIDTH (line 66) | const WIDTH = 100;
constant HEIGHT (line 67) | const HEIGHT = WIDTH * (2 / SQRT10);
constant ROW_HEIGHT (line 68) | const ROW_HEIGHT = HEIGHT / 2;
constant CELL_WIDTH (line 69) | const CELL_WIDTH = WIDTH / 10;
constant XX_HEIGHT (line 70) | const XX_HEIGHT = ROW_HEIGHT / 10;
method overlay (line 82) | get overlay() {
method setOverlay (line 93) | setOverlay(o: Overlay) {
method getLocalCoordinates (line 100) | getLocalCoordinates(event: React.MouseEvent | MouseEvent): {
method handleMouseDown (line 120) | handleMouseDown(event: React.MouseEvent) {
method handleMouseMove (line 134) | handleMouseMove(event: MouseEvent) {
method handleMouseUp (line 154) | handleMouseUp() {
function setHovered (line 319) | function setHovered(pos: IsbnPrefixRelative | null) {
FILE: src/components/SingleBookCover.tsx
function dot (line 9) | function dot(v1: [number, number], v2: [number, number]) {
function fract (line 15) | function fract(x: number) {
function rande (line 21) | function rande(co: [number, number]) {
function bookHeight (line 25) | function bookHeight(bookIndex: [number, number]) {
FILE: src/components/StatsShow.tsx
method edge (line 30) | get edge() {
method stats (line 46) | get stats() {
function StatsSummary (line 154) | function StatsSummary(props: {
FILE: src/components/TextTree.tsx
function ChildBooks (line 154) | function ChildBooks(props: {
function ChildGroupNames (line 179) | function ChildGroupNames(props: {
FILE: src/config.ts
type DatasetOption (line 5) | interface DatasetOption {
type ColorSchemeMeaning (line 12) | interface ColorSchemeMeaning {
FILE: src/index.tsx
function testCompat (line 17) | function testCompat() {
FILE: src/lib/DetailLevelObservable.ts
class DetailLevelObservable (line 15) | class DetailLevelObservable {
method constructor (line 21) | constructor(
method viewVisible (line 47) | get viewVisible() {
method container (line 53) | get container() {
method textOpacity (line 59) | get textOpacity() {
method textChildren (line 89) | get textChildren() {
method #imageRelativeLevel (line 99) | #imageRelativeLevel(relativeLevel: number) {
method image (line 113) | get image() {
method imageChildren (line 116) | get imageChildren() {
type DetailLevel (line 120) | interface DetailLevel {
function getScale (line 125) | function getScale(
FILE: src/lib/ImageLoader.ts
class ImageLoader (line 12) | class ImageLoader {
method constructor (line 21) | constructor(
method loadExisting (line 36) | private async loadExisting() {
method loadHasChildren (line 45) | private async loadHasChildren() {
method getHasChildren (line 55) | async getHasChildren(prefix: IsbnPrefixRelative): Promise<boolean> {
method getTexture (line 59) | async getTexture(prefix: IsbnPrefixRelative): Promise<Texture | null> {
FILE: src/lib/RuntimeConfiguration.ts
type RuntimeConfiguration (line 4) | interface RuntimeConfiguration {
function isMobile (line 62) | function isMobile() {
function defaultRuntimeConfig (line 66) | function defaultRuntimeConfig(dataset: string): RuntimeConfiguration {
function loadRuntimeConfigFromURL (line 111) | function loadRuntimeConfigFromURL(): RuntimeConfiguration {
function saveRuntimeConfigToURL (line 134) | function saveRuntimeConfigToURL(_config: RuntimeConfiguration) {
function debounce (line 153) | function debounce<A>(
FILE: src/lib/Store.ts
type RarityInfo (line 37) | interface RarityInfo {
class Store (line 42) | class Store {
method constructor (line 88) | constructor(projectionConfig: ProjectionConfig) {
method floatZoomFactor (line 113) | get floatZoomFactor() {
method addExternalSearchEngines (line 116) | addExternalSearchEngines(params: URLSearchParams) {
method imageLoader (line 132) | imageLoader(dataset: string) {
method getBookDetail (line 140) | async getBookDetail(isbn: IsbnStrWithChecksum) {
method googleBooksQueryIsbn (line 149) | async googleBooksQueryIsbn(isbn: IsbnStrWithChecksum) {
method updateHighlight (line 160) | updateHighlight(x: number, y: number, isHover: boolean) {
method updateHighlightedPublisher (line 171) | updateHighlightedPublisher(relativeIsbn: IsbnRelative) {
method updateHighlightedStats (line 203) | updateHighlightedStats(x: number, y: number, mode: "start" | "end") {
method trackAsyncProgress (line 214) | trackAsyncProgress<T>(_id: string, p: Promise<T>) {
method updateHighlightedIsbn (line 231) | updateHighlightedIsbn(
method debounceFetchGroupData (line 296) | debounceFetchGroupData(newFunction: () => Promise<void>) {
method updateView (line 304) | updateView(e?: OrbitControlsChangeEvent) {
method zoomAnimateToHighlight (line 324) | zoomAnimateToHighlight() {
method setView (line 333) | setView(targetX: number, targetY: number, zoom?: number) {
method zoomAnimateTo (line 346) | zoomAnimateTo(
method getRarityOfIsbn (line 394) | async getRarityOfIsbn(isbn: IsbnRelative): Promise<RarityInfo | null> {
method currentDataset (line 433) | get currentDataset() {
method switchDataset (line 441) | switchDataset(dataset: string, resetSettings: boolean) {
FILE: src/lib/TitleFetcher.ts
type TitleFetchedInfo (line 10) | interface TitleFetchedInfo {
class TitleFetcher (line 16) | class TitleFetcher {
method constructor (line 21) | constructor(private store: Store) {}
method fetchTitle (line 22) | async fetchTitle(
FILE: src/lib/delayRender.ts
function clearT (line 5) | function clearT() {
function useDelay (line 15) | function useDelay() {
FILE: src/lib/flight.ts
class Point2D (line 3) | class Point2D {
method constructor (line 4) | constructor(
method plus (line 8) | plus(p: Point2D) {
method minus (line 11) | minus(p: Point2D) {
method mul (line 14) | mul(s: number) {
method div (line 17) | div(s: number) {
method length (line 20) | length() {
method toString (line 23) | toString() {
class Point3D (line 28) | class Point3D implements Vector3Like {
method constructor (line 29) | constructor(
method plus (line 35) | plus(p: Point3D) {
method minus (line 38) | minus(p: Point3D) {
method mul (line 41) | mul(s: number) {
method div (line 44) | div(s: number) {
method neg (line 47) | neg() {
method length (line 50) | length() {
method distance (line 53) | distance(p: Point3D) {
method normalize (line 58) | normalize() {
method toString (line 62) | toString() {
type Trajectory (line 68) | interface Trajectory extends Object {
function getTrajectoryReal2 (line 73) | function getTrajectoryReal2(
function getTrajectoryReal (line 149) | function getTrajectoryReal(before: Point3D, after: Point3D) {
function getTrajectoryPoints (line 170) | function getTrajectoryPoints(
function calculateTrajectory (line 188) | function calculateTrajectory(x1: number, y1: number, x2: number, y2: num...
class TimeInterpolatingTrajectory (line 205) | class TimeInterpolatingTrajectory implements Trajectory {
method constructor (line 212) | constructor(inner: Trajectory) {
method position (line 301) | position(t: number): Point3D {
class DirectBlubSpaceTrajectory (line 327) | class DirectBlubSpaceTrajectory implements Trajectory {
method constructor (line 328) | constructor(
method position (line 337) | position(t: number): Point3D {
method reverse (line 352) | reverse(): Trajectory & { origin: Point3D } {
class RealSpaceTrajectory (line 369) | class RealSpaceTrajectory implements Trajectory {
method constructor (line 371) | constructor(
method position (line 379) | position(t: number): Point3D {
class CompositeTrajectory (line 391) | class CompositeTrajectory implements Trajectory {
method constructor (line 392) | constructor(private trajectories: Trajectory[]) {
method position (line 410) | position(t: number): Point3D {
function plotSmartTrajectory (line 428) | function plotSmartTrajectory(
function plotSmartTrajectoryInner (line 437) | function plotSmartTrajectoryInner(
function lineIntersectsCircle (line 543) | function lineIntersectsCircle(
function makeFullZoomOutTrajectory (line 576) | function makeFullZoomOutTrajectory(
function circleTangentPointFrom0Origin (line 609) | function circleTangentPointFrom0Origin(p: Point2D, radius: number): Poin...
function zoomToBlubRadius (line 619) | function zoomToBlubRadius(zoom: number): number {
function blubRadiusToZoom (line 623) | function blubRadiusToZoom(radius: number): number {
function toBlubSpace (line 627) | function toBlubSpace(dist: number, zoom: number, segmentSize: number): P...
function fromBlubSpace (line 633) | function fromBlubSpace(
FILE: src/lib/google-books.ts
type GoogleBooksResponse (line 3) | interface GoogleBooksResponse {
type GoogleBooksItem (line 7) | interface GoogleBooksItem {
function googleBooksQuery (line 79) | async function googleBooksQuery(query: string) {
function googleBooksQueryIsbn (line 87) | async function googleBooksQueryIsbn(
FILE: src/lib/info-map.ts
type Digit (line 3) | type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
constant DIGITS (line 4) | const DIGITS: Digit[] = [
type PrefixInfoData (line 16) | type PrefixInfoData =
type PrefixInfo (line 33) | interface PrefixInfo {
type InfoMap (line 38) | type InfoMap = Partial<Record<Digit, PrefixInfo>>;
type NeedLazyLoad (line 40) | interface NeedLazyLoad {
type LazyPrefixInfo (line 43) | interface LazyPrefixInfo {
type LazyInfoMap (line 48) | type LazyInfoMap = NeedLazyLoad | Partial<Record<Digit, LazyPrefixInfo>>;
function addRecord (line 50) | function addRecord(
FILE: src/lib/json-fetch.ts
function fetchJson (line 1) | async function fetchJson<T>(fname: string) {
FILE: src/lib/prefix-data.ts
function resolveOnePrefixLevel (line 31) | async function resolveOnePrefixLevel(
function addIsbnGroups (line 48) | function addIsbnGroups(
type LazyPrefixInfoWithParents (line 105) | interface LazyPrefixInfoWithParents {
function getGroupHierarchy (line 109) | function getGroupHierarchy(
function getGroup (line 141) | function getGroup(
FILE: src/lib/shader-error.ts
function handleSource (line 2) | function handleSource(string: string, errorLine: number) {
function getShaderErrors (line 17) | function getShaderErrors(
FILE: src/lib/shaders.ts
type UniformNames (line 25) | type UniformNames = keyof typeof uniforms;
type TypedUniforms (line 256) | type TypedUniforms = Record<
class ShaderUtil (line 260) | class ShaderUtil {
method constructor (line 261) | constructor(private store: Store) {
method shaderColorFn (line 273) | get shaderColorFn() {
method shaderProgram (line 336) | get shaderProgram() {
method getIsbnShaderMaterial (line 366) | async getIsbnShaderMaterial(
FILE: src/lib/stats.ts
type StatsMap (line 5) | type StatsMap = Partial<Record<IsbnPrefixWithoutDashes, BlockStats>>;
type BlockStats (line 6) | type BlockStats = Partial<Record<string, number>>;
class StatsCalculator (line 7) | class StatsCalculator {
method constructor (line 9) | constructor(private store: Store) {}
method #getRanges (line 10) | #getRanges(
method #fetchStats (line 42) | async #fetchStats(): Promise<StatsMap> {
method getStats (line 50) | async getStats(
function mergeStats (line 65) | function mergeStats(target: BlockStats, source: BlockStats) {
FILE: src/lib/types-select.d.ts
type Props (line 8) | interface Props<
FILE: src/lib/util.ts
type Nominal (line 7) | type Nominal<Type, Identifier> = Type & {
type IsbnPrefixWithDashes (line 11) | type IsbnPrefixWithDashes = Nominal<string, "IsbnPrefixWithDashes">;
type IsbnPrefixWithoutDashes (line 13) | type IsbnPrefixWithoutDashes = Nominal<
type IsbnRelative (line 19) | type IsbnRelative = Nominal<number, "IsbnRelative">;
type Isbn13Number (line 20) | type Isbn13Number = Nominal<number, "Isbn13Number">;
type IsbnStrWithChecksum (line 21) | type IsbnStrWithChecksum = Nominal<string, "IsbnStrWithChecksum">;
type IsbnPrefixRelative (line 23) | type IsbnPrefixRelative = Nominal<string, "IsbnPrefixRelative">;
function removeDashes (line 24) | function removeDashes(
function isbnPrefixAppend (line 30) | function isbnPrefixAppend(
function isbnPrefixToRelative (line 36) | function isbnPrefixToRelative(
function isbnPrefixFromRelative (line 41) | function isbnPrefixFromRelative(
function isbnToRelative (line 48) | function isbnToRelative(isbn: Isbn13Number): IsbnRelative {
function relativeToIsbnPrefix (line 51) | function relativeToIsbnPrefix(
function relativeToFullIsbn (line 56) | function relativeToFullIsbn(
function fullIsbnToRelative (line 62) | function fullIsbnToRelative(isbn: IsbnStrWithChecksum): IsbnRelative {
type ProjectionConfig (line 67) | interface ProjectionConfig {
function firstIsbnInPrefix (line 85) | function firstIsbnInPrefix(
function lastIsbnInPrefix (line 91) | function lastIsbnInPrefix(
function libIsbnToNumber (line 97) | function libIsbnToNumber(isbn: ISBN): Isbn13Number {
constant IMG_WIDTH (line 103) | const IMG_WIDTH = 2000;
function hsl2rgb (line 106) | function hsl2rgb(
function siNumber (line 116) | function siNumber(n: number) {
function splitNameJson (line 128) | function splitNameJson(prefixStr: string, dirSegmentLength: number) {
FILE: src/lib/view-utils.ts
type ViewParams (line 3) | interface ViewParams {
type ViewParams2 (line 12) | type ViewParams2 = ViewParams | "visible" | "invisible";
function getPlanePosition (line 13) | function getPlanePosition(
function simplifyView (line 32) | function simplifyView(
FILE: src/projections/bookshelf.ts
function bookshelfConfig (line 3) | function bookshelfConfig({
FILE: src/projections/linear.ts
function linearConfig (line 3) | function linearConfig({
Condensed preview — 68 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (292K chars).
[
{
"path": ".dockerignore",
"chars": 51,
"preview": "dist\npublic\ndata\nscripts/rarity/target\nnode_modules"
},
{
"path": ".github/workflows/deploy.yml",
"chars": 1428,
"preview": "# Simple workflow for deploying static content to GitHub Pages\nname: Deploy static content to Pages\n\non:\n # Runs on pus"
},
{
"path": ".gitignore",
"chars": 74,
"preview": "node_modules\ndist\ndata\nnotes.md\n/public/prefix-data\n/public/images\n/public"
},
{
"path": ".vscode/launch.json",
"chars": 1003,
"preview": "{\n // Use IntelliSense to learn about possible attributes.\n // Hover to view descriptions of existing attributes.\n //"
},
{
"path": ".vscode/settings.json",
"chars": 289,
"preview": "{\n \"editor.formatOnSave\": true,\n \"editor.codeActionsOnSave\": {\n \"source.organizeImports\": \"explicit\"\n },\n \"files."
},
{
"path": "Dockerfile",
"chars": 907,
"preview": "# Build rust\nFROM rust:1.85 AS rust-builder\nRUN apt-get update && apt-get install -y cmake\nADD scripts/rarity /app/scrip"
},
{
"path": "LICENSE.md",
"chars": 34529,
"preview": "I like the concept of giving back, so I settled on the AGPL as the\ndefault license for all my personal projects.\n\nThis i"
},
{
"path": "README.md",
"chars": 7705,
"preview": "# ISBN Visualization\n\n**Please read https://phiresky.github.io/blog/2025/visualizing-all-books-in-isbn-space/ for the li"
},
{
"path": "eslint.config.mjs",
"chars": 933,
"preview": "// @ts-check\n\nimport eslint from \"@eslint/js\";\nimport eslintPluginPrettierRecommended from \"eslint-plugin-prettier/recom"
},
{
"path": "flake.nix",
"chars": 1128,
"preview": "{\n inputs = {\n nixpkgs.url = \"github:NixOS/nixpkgs/nixos-unstable\";\n #rust-overlay = {\n # url = \"github:oxali"
},
{
"path": "index.html",
"chars": 579,
"preview": "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-w"
},
{
"path": "package.json",
"chars": 1667,
"preview": "{\n \"name\": \"@phiresky/isbn-visualization\",\n \"version\": \"0.0.0\",\n \"description\": \"\",\n \"type\": \"module\",\n \"scripts\": "
},
{
"path": "scripts/gen-book-titles-sqlite.ts",
"chars": 3422,
"preview": "import sqlite from \"better-sqlite3\";\nimport { createReadStream } from \"fs\";\nimport fs from \"fs/promises\";\nimport readlin"
},
{
"path": "scripts/gen-prefixes.ts",
"chars": 4790,
"preview": "import { createReadStream } from \"node:fs\";\nimport { mkdir, writeFile } from \"node:fs/promises\";\nimport { createInterfac"
},
{
"path": "scripts/merge-stats.ts",
"chars": 774,
"preview": "import { readFileSync, writeFileSync } from \"fs\";\nimport { mergeStats, StatsMap } from \"../src/lib/stats\";\nimport { Isbn"
},
{
"path": "scripts/minify-images.sh",
"chars": 747,
"preview": "#!/bin/bash\nset -euo pipefail\n\nlines=\"$(find \"$1\" -name '*.png' | wc -l)\"\n\nfind \"$1\" -name '*.png' | sort | pv -l --size"
},
{
"path": "scripts/minify-prefix-data.sh",
"chars": 727,
"preview": "#!/bin/bash\nset -euo pipefail\n\nJOBS=\"${JOBS:-$(nproc)}\"\n\nOUTPUT_DIR_PUBLIC=\"${OUTPUT_DIR_PUBLIC:-public}\"\n\necho compress"
},
{
"path": "scripts/process-all.sh",
"chars": 4034,
"preview": "#!/bin/bash\nset -euo pipefail\n\n# for each env var, check if file exists and make path absolute\n\n# default INPUT_ISBNGRP_"
},
{
"path": "scripts/rarity/.gitignore",
"chars": 8,
"preview": "/target\n"
},
{
"path": "scripts/rarity/Cargo.toml",
"chars": 543,
"preview": "[package]\nname = \"rarity\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\nsimd-json = { version = \"*\", default-featur"
},
{
"path": "scripts/rarity/src/main.rs",
"chars": 11905,
"preview": "#[global_allocator]\n// better performance than the default malloc\nstatic ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnM"
},
{
"path": "scripts/write-images/ImageTiler.ts",
"chars": 7097,
"preview": "import { mkdir } from \"fs/promises\";\nimport sharp from \"sharp\";\nimport { ImageTile, channelMax } from \".\";\nimport {\n IM"
},
{
"path": "scripts/write-images/index.ts",
"chars": 2685,
"preview": "import { writeFile } from \"fs/promises\";\nimport { ImageTiler, StatsAggregator } from \"./ImageTiler\";\nimport * as modules"
},
{
"path": "scripts/write-images/modules/aggregate-dense.ts",
"chars": 1889,
"preview": "import { IsbnData, ProcessSingleZoom } from \"..\";\nimport { IsbnRelative, totalIsbns } from \"../../../src/lib/util\";\nimpo"
},
{
"path": "scripts/write-images/modules/index.ts",
"chars": 271,
"preview": "export { default as all } from \"./aggregate-dense\";\nexport { default as publication_date } from \"./publication_date\";\nex"
},
{
"path": "scripts/write-images/modules/publication_date.ts",
"chars": 3499,
"preview": "import sqlite3 from \"better-sqlite3\";\nimport { channelMax, ImageTile, ProcessSingleZoom } from \"..\";\nimport {\n fullIsbn"
},
{
"path": "scripts/write-images/modules/publishers.ts",
"chars": 2768,
"preview": "import { readFile } from \"fs/promises\";\nimport { ProcessSingleZoom } from \"..\";\nimport { InfoMap, LazyPrefixInfo } from "
},
{
"path": "scripts/write-images/modules/rarity.ts",
"chars": 4723,
"preview": "import sqlite3 from \"better-sqlite3\";\nimport { channelMax, ImageTile, ProcessSingleZoom } from \"..\";\nimport {\n fullIsbn"
},
{
"path": "scripts/write-images/modules/single-sparse.ts",
"chars": 2303,
"preview": "import bencode from \"bencode\";\nimport { createReadStream } from \"node:fs\";\nimport { ZSTDDecompress } from \"simple-zstd\";"
},
{
"path": "scripts/write-titles.ts",
"chars": 2105,
"preview": "import sqlite3 from \"better-sqlite3\";\nimport { mkdirSync, writeFileSync } from \"fs\";\nimport path from \"path\";\nimport {\n "
},
{
"path": "src/App.tsx",
"chars": 469,
"preview": "import { useMemo, type FC } from \"react\";\n\nimport { IsbnMap } from \"./components/IsbnMap\";\nimport { bookshelfConfig } fr"
},
{
"path": "src/components/Controls.tsx",
"chars": 15890,
"preview": "import isbnlib from \"isbn3\";\nimport { observer, useLocalObservable } from \"mobx-react-lite\";\nimport { fromPromise } from"
},
{
"path": "src/components/EanBarcode.tsx",
"chars": 108,
"preview": "export function EanBarcode(props: { ean: string }) {\n return <span className=\"ean13\">{props.ean}</span>;\n}\n"
},
{
"path": "src/components/Highlight.tsx",
"chars": 8832,
"preview": "import { Html } from \"@react-three/drei\";\nimport { hyphenate } from \"isbn3\";\nimport { observer } from \"mobx-react-lite\";"
},
{
"path": "src/components/ImageTree.tsx",
"chars": 3413,
"preview": "import { Plane } from \"@react-three/drei\";\nimport { observer, useLocalObservable } from \"mobx-react-lite\";\nimport { from"
},
{
"path": "src/components/IsbnGrid.tsx",
"chars": 3245,
"preview": "import { Grid } from \"@react-three/drei\";\nimport { computed } from \"mobx\";\nimport { observer, useLocalObservable } from "
},
{
"path": "src/components/IsbnMap.tsx",
"chars": 7058,
"preview": "import { OrbitControls, OrthographicCamera, Plane } from \"@react-three/drei\";\nimport { Canvas, ThreeEvent } from \"@react"
},
{
"path": "src/components/Legend.tsx",
"chars": 2991,
"preview": "import { observer } from \"mobx-react-lite\";\nimport Select, {\n components,\n OptionProps,\n SingleValueProps,\n} from \"re"
},
{
"path": "src/components/MiniMap.tsx",
"chars": 11680,
"preview": "/* eslint-disable @typescript-eslint/unbound-method -- mobx binds observable methods */\nimport { computed } from \"mobx\";"
},
{
"path": "src/components/SingleBookCover.tsx",
"chars": 2430,
"preview": "import * as isbnlib from \"isbn3\";\nimport { observer } from \"mobx-react-lite\";\nimport { fromPromise } from \"mobx-utils\";\n"
},
{
"path": "src/components/StatsShow.tsx",
"chars": 6022,
"preview": "import { Html, Plane } from \"@react-three/drei\";\nimport { observer, useLocalObservable } from \"mobx-react-lite\";\nimport "
},
{
"path": "src/components/TextTree.tsx",
"chars": 6922,
"preview": "import { Html } from \"@react-three/drei\";\nimport { observer } from \"mobx-react-lite\";\nimport { useEffect, useState } fro"
},
{
"path": "src/config.ts",
"chars": 5093,
"preview": "import { MinimalGoogleBooksItem } from \"./components/Controls\";\nimport { RuntimeConfiguration } from \"./lib/RuntimeConfi"
},
{
"path": "src/index.css",
"chars": 8740,
"preview": "html,\nbody {\n width: 100%;\n height: 100%;\n box-sizing: border-box;\n margin: 0;\n font-family: system-ui, sans-serif;"
},
{
"path": "src/index.tsx",
"chars": 640,
"preview": "import { isWebGL2Available } from \"@react-three/drei\";\nimport { configure } from \"mobx\";\nimport { StrictMode } from \"rea"
},
{
"path": "src/lib/DetailLevelObservable.ts",
"chars": 3999,
"preview": "import { computed, makeObservable } from \"mobx\";\nimport { Store } from \"./Store\";\nimport {\n IMG_WIDTH,\n IsbnPrefixWith"
},
{
"path": "src/lib/ImageLoader.ts",
"chars": 2715,
"preview": "import {\n LinearFilter,\n LinearMipMapLinearFilter,\n MagnificationTextureFilter,\n MinificationTextureFilter,\n Neares"
},
{
"path": "src/lib/RuntimeConfiguration.ts",
"chars": 6223,
"preview": "import config from \"../config\";\n\n/** Any value not set is set according to the dataset-specific defaults. */\nexport inte"
},
{
"path": "src/lib/Store.ts",
"chars": 15181,
"preview": "import { OrbitControlsChangeEvent } from \"@react-three/drei\";\nimport * as isbnlib from \"isbn3\";\nimport { autorun, makeAu"
},
{
"path": "src/lib/TitleFetcher.ts",
"chars": 1155,
"preview": "import { fetchJson } from \"./json-fetch\";\nimport { Store } from \"./Store\";\nimport {\n Isbn13Number,\n IsbnPrefixWithoutD"
},
{
"path": "src/lib/delayRender.ts",
"chars": 631,
"preview": "import { useEffect, useState } from \"react\";\n\nconst maxPerFrame = 1;\nconst pending: (() => void)[] = [];\nfunction clearT"
},
{
"path": "src/lib/flight.ts",
"chars": 18206,
"preview": "import { Vector3Like } from \"three/src/math/Vector3\";\n\nclass Point2D {\n constructor(\n public x: number,\n public y"
},
{
"path": "src/lib/google-books.ts",
"chars": 2167,
"preview": "import { IsbnStrWithChecksum } from \"./util\";\n\ninterface GoogleBooksResponse {\n items?: GoogleBooksItem[];\n totalItems"
},
{
"path": "src/lib/info-map.ts",
"chars": 1671,
"preview": "import { IsbnPrefixWithDashes } from \"./util\";\n\nexport type Digit = \"0\" | \"1\" | \"2\" | \"3\" | \"4\" | \"5\" | \"6\" | \"7\" | \"8\" "
},
{
"path": "src/lib/json-fetch.ts",
"chars": 700,
"preview": "export async function fetchJson<T>(fname: string) {\n const config = (await import(\"../config\")).default;\n const gzip ="
},
{
"path": "src/lib/prefix-data.ts",
"chars": 4166,
"preview": "import isbnlib from \"isbn3\";\nimport {\n addRecord,\n Digit,\n InfoMap,\n LazyInfoMap,\n LazyPrefixInfo,\n PrefixInfo,\n "
},
{
"path": "src/lib/shader-error.ts",
"chars": 2053,
"preview": "// https://github.com/Mugen87/three.js/blob/a94de2aa8b2ef1830adeea3be5d1d1eca5b6e1f4/src/renderers/webgl/WebGLProgram.js"
},
{
"path": "src/lib/shaders.ts",
"chars": 16663,
"preview": "import { makeAutoObservable } from \"mobx\";\nimport * as THREE from \"three\";\nimport { ShaderMaterial } from \"three\";\nimpor"
},
{
"path": "src/lib/stats.ts",
"chars": 2251,
"preview": "import { fetchJson } from \"./json-fetch\";\nimport { Store } from \"./Store\";\nimport { IsbnPrefixWithoutDashes } from \"./ut"
},
{
"path": "src/lib/types-select.d.ts",
"chars": 566,
"preview": "import type {} from \"react-select/base\";\nimport { Store } from \"./Store\";\n// This import is necessary for module augment"
},
{
"path": "src/lib/types.d.ts",
"chars": 242,
"preview": "declare module \"isbn3/lib/calculate_check_digit\" {\n export default function calculateCheckDigit(isbn: string): string;\n"
},
{
"path": "src/lib/util.ts",
"chars": 4142,
"preview": "import calculateCheckDigit from \"isbn3/lib/calculate_check_digit\";\n\nexport { calculateCheckDigit };\n\ndeclare const __nom"
},
{
"path": "src/lib/view-utils.ts",
"chars": 1391,
"preview": "import { IsbnRelative, ProjectionConfig } from \"./util\";\n\nexport interface ViewParams {\n minX: number;\n minY: number;\n"
},
{
"path": "src/projections/bookshelf.ts",
"chars": 3015,
"preview": "import { IsbnRelative, ProjectionConfig, totalIsbns } from \"../lib/util\";\n\nexport function bookshelfConfig({\n width = 1"
},
{
"path": "src/projections/index.ts",
"chars": 0,
"preview": ""
},
{
"path": "src/projections/linear.ts",
"chars": 1483,
"preview": "import { IsbnRelative, ProjectionConfig, totalIsbns } from \"../lib/util\";\n\nexport function linearConfig({\n scale = 50,\n"
},
{
"path": "tsconfig.json",
"chars": 460,
"preview": "{\n \"compilerOptions\": {\n \"strict\": true,\n \"target\": \"ESNext\",\n \"module\": \"ESNext\",\n \"moduleResolution\": \"no"
},
{
"path": "vite.config.ts",
"chars": 347,
"preview": "import react from \"@vitejs/plugin-react-swc\";\nimport { defineConfig } from \"vite\";\n\n// https://vite.dev/config/\nexport d"
}
]
About this extraction
This page contains the full source code of the phiresky/isbn-visualization GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 68 files (271.0 KB), approximately 73.4k tokens, and a symbol index with 263 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.