@yanqirenshi/frasoga 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +273 -3
- package/dist/BookFigure.d.ts +7 -0
- package/dist/BookTable.d.ts +4 -0
- package/dist/StructureBook.d.ts +15 -0
- package/dist/StructurePart.d.ts +25 -0
- package/dist/StructureToc.d.ts +10 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +185 -0
- package/dist/labels.d.ts +3 -0
- package/dist/style.css +1 -0
- package/dist/types.d.ts +70 -0
- package/package.json +40 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yanqirenshi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,273 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @yanqirenshi/frasoga
|
|
2
|
+
|
|
3
|
+
Frasoga は、Tyggja が出力した構造化 JSON (`version: 2`) を、一文ずつ読み進められる React コンポーネントのライブラリです。
|
|
4
|
+
React 18 以上で使えます。
|
|
5
|
+
|
|
6
|
+
## インストール
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @yanqirenshi/frasoga
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`react` と `react-dom` は peerDependencies です。
|
|
13
|
+
利用側のものが使われます。
|
|
14
|
+
|
|
15
|
+
## スタイル
|
|
16
|
+
|
|
17
|
+
CSS は自動では読み込まれません。
|
|
18
|
+
利用側で一度だけ import してください。
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import '@yanqirenshi/frasoga/style.css';
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
クラス名はすべて `frasoga-` で始まります。
|
|
25
|
+
色、余白、フォントは `--frasoga-*` の CSS 変数で変えられます。
|
|
26
|
+
変数は次のとおりです。
|
|
27
|
+
指定しなかったものは既定値になります。
|
|
28
|
+
|
|
29
|
+
| 変数 | 用途 | 既定値 |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `--frasoga-bg` | 背景色 | `#fbfaf7` |
|
|
32
|
+
| `--frasoga-text` | 文字色 | `#2b2b2b` |
|
|
33
|
+
| `--frasoga-muted` | 補助の文字色 | `#8a8a8a` |
|
|
34
|
+
| `--frasoga-highlight` | 強調中の文の背景 | `#ffe58a` |
|
|
35
|
+
| `--frasoga-accent` | 進捗バーの色 | `#e0a800` |
|
|
36
|
+
| `--frasoga-border` | 区切り線の色 | `#e6e3dc` |
|
|
37
|
+
| `--frasoga-toc-bg` | 目次の背景 | `#f5f2eb` |
|
|
38
|
+
| `--frasoga-hover` | ボタンのホバー背景 | `#f2eee4` |
|
|
39
|
+
| `--frasoga-table-border` | 表の罫線 | `#cbc5b9` |
|
|
40
|
+
| `--frasoga-font-family` | フォント | `'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', 'Meiryo', sans-serif` |
|
|
41
|
+
| `--frasoga-font-size` | 本文の文字サイズ | `20px` |
|
|
42
|
+
| `--frasoga-line-height` | 本文の行間 | `2` |
|
|
43
|
+
| `--frasoga-content-width` | 本文の最大幅 | `720px` |
|
|
44
|
+
| `--frasoga-toc-width` | 目次の幅 | `clamp(7rem, 28vw, 20rem)` |
|
|
45
|
+
| `--frasoga-padding` | 本文の左右の余白 | `24px` |
|
|
46
|
+
|
|
47
|
+
```css
|
|
48
|
+
.my-reader {
|
|
49
|
+
--frasoga-highlight: #cfe8ff;
|
|
50
|
+
--frasoga-font-size: 18px;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`StructureBook` は親要素の高さに合わせます。
|
|
55
|
+
画面いっぱいにするときは、利用側で親要素に高さを指定してください。
|
|
56
|
+
|
|
57
|
+
## コンポーネント
|
|
58
|
+
|
|
59
|
+
データの取得と `version: 2` の確認は、利用側で行います。
|
|
60
|
+
未知のプロパティは無視されます。
|
|
61
|
+
|
|
62
|
+
### StructurePart (本文 1 ファイル分と一文送り)
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { StructurePart } from '@yanqirenshi/frasoga';
|
|
66
|
+
|
|
67
|
+
<StructurePart
|
|
68
|
+
part={part}
|
|
69
|
+
title="書名"
|
|
70
|
+
figureSrc={(src) => `/book-data/${src}`}
|
|
71
|
+
onSelectFile={(file) => { /* section_ref のボタンを押したとき */ }}
|
|
72
|
+
onPastEnd={() => { /* 最後の文で → を押したとき */ }}
|
|
73
|
+
onPastStart={() => { /* 最初の文で ← を押したとき */ }}
|
|
74
|
+
/>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
1. `part` (必須): 本文 1 ファイル分の JSON
|
|
78
|
+
2. `title`: 上部バーに出す書名
|
|
79
|
+
3. `figureSrc`: figure の `src` から画像 URL を作る (既定はそのまま返す)
|
|
80
|
+
4. `current` / `defaultCurrent` / `onCurrentChange`: 強調中の文の位置 (制御と非制御の両方に対応。`part.file` が変わると、非制御の位置は `defaultCurrent` (既定 0) から始まる。制御の `current` が範囲外のときは、表示だけ先頭か末尾に丸め、`onCurrentChange` では知らせない)
|
|
81
|
+
5. `keyboard`: `false` で ←→ のキー操作を登録しない (既定 `true`)
|
|
82
|
+
|
|
83
|
+
### StructureToc (目次だけ)
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { StructureToc } from '@yanqirenshi/frasoga';
|
|
87
|
+
|
|
88
|
+
<StructureToc index={index} file={file} onSelectFile={setFile} />
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### StructureBook (目次と本文を並べた画面全体)
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
import { StructureBook } from '@yanqirenshi/frasoga';
|
|
95
|
+
|
|
96
|
+
<div style={{ height: '100vh' }}>
|
|
97
|
+
<StructureBook
|
|
98
|
+
index={index}
|
|
99
|
+
part={part}
|
|
100
|
+
file={file}
|
|
101
|
+
onSelectFile={setFile}
|
|
102
|
+
fallback={<p>読み込み中…</p>}
|
|
103
|
+
/>
|
|
104
|
+
</div>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`part` を渡さないと、本文の場所に `fallback` が出ます。
|
|
108
|
+
`StructurePart` の `figureSrc`、`current` 系、`onPastEnd`、`onPastStart`、`keyboard` はそのまま渡せます。
|
|
109
|
+
|
|
110
|
+
### 画面の文言 (labels)
|
|
111
|
+
|
|
112
|
+
3 つのコンポーネントはどれも `labels?: Partial<FrasogaLabels>` を受け取ります。
|
|
113
|
+
指定しなかった項目は、次の既定値になります。
|
|
114
|
+
|
|
115
|
+
1. `toc`: 目次の見出し (既定「目次」。`StructureToc`、`StructureBook` で使う)
|
|
116
|
+
2. `front`: 巻頭 (index の `id` が `null` のファイル) の目次の表示 (既定「巻頭」。`StructureToc`、`StructureBook` で使う)
|
|
117
|
+
3. `body`: 本文領域の `aria-label` (既定「本文」。`StructureBook` で使う)
|
|
118
|
+
4. `hint`: 上部バーの操作の案内 (既定「← 前の行 → 次の行」。`StructurePart`、`StructureBook` で使う)
|
|
119
|
+
|
|
120
|
+
`StructureBook` は、受け取った `labels` を子にそのまま渡します。
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
<StructureBook
|
|
124
|
+
index={index}
|
|
125
|
+
part={part}
|
|
126
|
+
labels={{ toc: '目錄', front: '卷首', body: '正文', hint: '← 上一行 → 下一行' }}
|
|
127
|
+
/>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 型
|
|
131
|
+
|
|
132
|
+
`BookIndex`、`TocEntry`、`BookFile`、`Part`、`BodyNode`、`Sentence`、`Cell`、`FrasogaLabels` を export しています。
|
|
133
|
+
|
|
134
|
+
## Next (App Router) の Server Component から使う
|
|
135
|
+
|
|
136
|
+
JSON は Server Component で読み、props で渡します。
|
|
137
|
+
ライブラリの先頭には `"use client";` が付いているので、Server Component から直接 import できます。
|
|
138
|
+
ただし、関数の props (`figureSrc`、`onSelectFile` など) は Server Component から渡せません。
|
|
139
|
+
これらは `"use client"` の薄い包みの中で定義し、Server Component は包みにデータだけを渡します。
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
// app/book/[file]/BookView.tsx
|
|
143
|
+
'use client';
|
|
144
|
+
|
|
145
|
+
import { useRouter } from 'next/navigation';
|
|
146
|
+
import { StructureBook, type BookIndex, type Part } from '@yanqirenshi/frasoga';
|
|
147
|
+
import '@yanqirenshi/frasoga/style.css';
|
|
148
|
+
|
|
149
|
+
export function BookView({ index, part, file }: { index: BookIndex; part: Part; file: string }) {
|
|
150
|
+
const router = useRouter();
|
|
151
|
+
return (
|
|
152
|
+
<div style={{ height: '100vh' }}>
|
|
153
|
+
<StructureBook
|
|
154
|
+
index={index}
|
|
155
|
+
part={part}
|
|
156
|
+
file={file}
|
|
157
|
+
onSelectFile={(next) => router.push(`/book/${next}`)}
|
|
158
|
+
// 入れ子のルートでも壊れないよう、絶対パスで返す (public/book-data/ に画像を置く場合)
|
|
159
|
+
figureSrc={(src) => `/book-data/${src}`}
|
|
160
|
+
/>
|
|
161
|
+
</div>
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
// app/book/[file]/page.tsx
|
|
168
|
+
import { readFile } from 'node:fs/promises';
|
|
169
|
+
import type { BookIndex, Part } from '@yanqirenshi/frasoga';
|
|
170
|
+
import { BookView } from './BookView';
|
|
171
|
+
|
|
172
|
+
const read = async <T,>(file: string) =>
|
|
173
|
+
JSON.parse(await readFile(`./books/structure/${file}`, 'utf8')) as T;
|
|
174
|
+
|
|
175
|
+
export default async function Page({ params }: { params: Promise<{ file: string }> }) {
|
|
176
|
+
const { file } = await params;
|
|
177
|
+
const index = await read<BookIndex>('index.json');
|
|
178
|
+
const part = await read<Part>(file);
|
|
179
|
+
return <BookView index={index} part={part} file={file} />;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
関数の props を使わなければ、`StructureToc` と `StructurePart` は Server Component から直接描けます。
|
|
184
|
+
その場合、目次と `section_ref` のボタンは押しても何も起きません。
|
|
185
|
+
|
|
186
|
+
## ビルド
|
|
187
|
+
|
|
188
|
+
```sh
|
|
189
|
+
npm ci
|
|
190
|
+
npm run build
|
|
191
|
+
npm test
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
`npm run build` は `dist/` に `index.js` (ESM)、`index.d.ts` (型定義)、`style.css` を出力します。
|
|
195
|
+
|
|
196
|
+
## リポジトリの構成
|
|
197
|
+
|
|
198
|
+
1. `src/`: ライブラリ本体 (入口は `src/index.ts`)
|
|
199
|
+
2. `demo/`: デモの Web アプリ
|
|
200
|
+
3. `export/`: HTML と Markdown の書き出し (ライブラリには含まれません)
|
|
201
|
+
4. `tests/`: 自動テスト
|
|
202
|
+
|
|
203
|
+
`src/` は `demo/` と `export/` に依存しません。
|
|
204
|
+
|
|
205
|
+
## デモ
|
|
206
|
+
|
|
207
|
+
### データの準備
|
|
208
|
+
|
|
209
|
+
Tyggja の `structure/original/` 出力 (`version: 2`) を用意します。
|
|
210
|
+
目次は、指定した版の `index.json` に記載されたファイルの順序で表示されます。
|
|
211
|
+
画像は各版で共有し、`figures/` を `structure/` と同じ階層に置きます。
|
|
212
|
+
|
|
213
|
+
```text
|
|
214
|
+
example/
|
|
215
|
+
├── structure/
|
|
216
|
+
│ ├── original/
|
|
217
|
+
│ │ ├── index.json
|
|
218
|
+
│ │ └── 001_s1.json
|
|
219
|
+
│ └── edited/
|
|
220
|
+
│ ├── index.json
|
|
221
|
+
│ └── 001_s1.json
|
|
222
|
+
└── figures/
|
|
223
|
+
└── p001_1_0.png
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
JSON の形式は Tyggja の `schema/structure-index.schema.json` と `schema/structure-part.schema.json` を参照してください。
|
|
227
|
+
|
|
228
|
+
リポジトリ直下に `.env.local` を作成し、表示する版のフォルダを指定します。
|
|
229
|
+
別の版を表示するときは `original` を版名に置き換え、開発サーバーを起動し直します。
|
|
230
|
+
未設定時は `demo/vite.config.ts` の既定のパスが使われます。
|
|
231
|
+
|
|
232
|
+
```dotenv
|
|
233
|
+
FRASOGA_STRUCTURE_DIR=C:/Books/example/structure/original
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### 起動
|
|
237
|
+
|
|
238
|
+
```sh
|
|
239
|
+
npm ci
|
|
240
|
+
npm run dev
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
ターミナルに表示された URL をブラウザーで開きます。
|
|
244
|
+
開発サーバーは指定されたフォルダから JSON と画像を読み込みます。
|
|
245
|
+
|
|
246
|
+
### デモのビルド
|
|
247
|
+
|
|
248
|
+
```sh
|
|
249
|
+
npm run build:demo
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
ビルド時点の JSON と画像を `dist-demo/book-data/` に同梱します。
|
|
253
|
+
元のデータを更新した場合は、再ビルドすると反映されます。
|
|
254
|
+
|
|
255
|
+
## HTML・Markdown に書き出す
|
|
256
|
+
|
|
257
|
+
```sh
|
|
258
|
+
npm run export -- "C:/Books/example/structure/original"
|
|
259
|
+
npm run export -- "C:/Books/example/structure/original" --format md
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
分割されたセクションを結合し、書籍フォルダの `publish/<版名>/` に出力します。
|
|
263
|
+
既定の HTML は `index.html`、`--format md` は `book.md` を作ります。
|
|
264
|
+
HTML の CSS はファイルに埋め込まれます。
|
|
265
|
+
どちらの形式も `../../figures/` を参照します。
|
|
266
|
+
|
|
267
|
+
柱・ノンブルも出力するには `--keep-decorations` を指定します。
|
|
268
|
+
既存の出力ファイルを置き換えるには `--force` を指定します。
|
|
269
|
+
|
|
270
|
+
### 旧配置のデータ
|
|
271
|
+
|
|
272
|
+
`structure/index.json` がある旧配置では、`structure/` を指定するとターミナルに案内が表示され、従来どおり書籍フォルダ直下に書き出します。
|
|
273
|
+
デモの設定が未指定で、既定の `structure/original/index.json` がなく旧配置の `structure/index.json` がある場合は、旧配置を読み込みます。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type StructurePartProps } from './StructurePart';
|
|
3
|
+
import type { BookIndex, Part } from './types';
|
|
4
|
+
export type StructureBookProps = Omit<StructurePartProps, 'part' | 'title'> & {
|
|
5
|
+
index: BookIndex;
|
|
6
|
+
/** 未指定のときは fallback を本文の場所に出す */
|
|
7
|
+
part?: Part;
|
|
8
|
+
/** 選択中のファイル。未指定なら内部で管理する */
|
|
9
|
+
file?: string;
|
|
10
|
+
onSelectFile?: (file: string) => void;
|
|
11
|
+
/** 読み込み中や失敗の表示 */
|
|
12
|
+
fallback?: ReactNode;
|
|
13
|
+
};
|
|
14
|
+
/** 目次と本文を並べた画面全体。高さは親要素に合わせる。 */
|
|
15
|
+
export declare function StructureBook({ index, part, file, onSelectFile, fallback, labels, ...partProps }: StructureBookProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FrasogaLabels, Part } from './types';
|
|
2
|
+
export type StructurePartProps = {
|
|
3
|
+
part: Part;
|
|
4
|
+
/** 上部バーに出す書名 */
|
|
5
|
+
title?: string;
|
|
6
|
+
/** figure の src から画像 URL を作る。既定はそのまま返す */
|
|
7
|
+
figureSrc?: (src: string) => string;
|
|
8
|
+
/** section_ref のボタンを押したとき */
|
|
9
|
+
onSelectFile?: (file: string) => void;
|
|
10
|
+
/** 強調中の文の位置 (制御) */
|
|
11
|
+
current?: number;
|
|
12
|
+
/** 強調中の文の初期位置 (非制御) */
|
|
13
|
+
defaultCurrent?: number;
|
|
14
|
+
onCurrentChange?: (index: number) => void;
|
|
15
|
+
/** 最後の文で → を押したとき */
|
|
16
|
+
onPastEnd?: () => void;
|
|
17
|
+
/** 最初の文で ← を押したとき */
|
|
18
|
+
onPastStart?: () => void;
|
|
19
|
+
/** false なら ←→ のキー操作を登録しない。既定 true */
|
|
20
|
+
keyboard?: boolean;
|
|
21
|
+
/** 画面の文言 (hint を使う) */
|
|
22
|
+
labels?: Partial<FrasogaLabels>;
|
|
23
|
+
};
|
|
24
|
+
/** 本文 1 ファイル分を一文ずつ読む。part.file が変わると、非制御の位置は defaultCurrent (既定 0) から始まる。 */
|
|
25
|
+
export declare function StructurePart(props: StructurePartProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BookIndex, FrasogaLabels } from './types';
|
|
2
|
+
export type StructureTocProps = {
|
|
3
|
+
index: BookIndex;
|
|
4
|
+
/** 選択中のファイル */
|
|
5
|
+
file?: string;
|
|
6
|
+
onSelectFile?: (file: string) => void;
|
|
7
|
+
/** 画面の文言 (toc、front を使う) */
|
|
8
|
+
labels?: Partial<FrasogaLabels>;
|
|
9
|
+
};
|
|
10
|
+
export declare function StructureToc({ index, file, onSelectFile, labels }: StructureTocProps): import("react").JSX.Element;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { StructureBook } from './StructureBook';
|
|
2
|
+
export type { StructureBookProps } from './StructureBook';
|
|
3
|
+
export { StructurePart } from './StructurePart';
|
|
4
|
+
export type { StructurePartProps } from './StructurePart';
|
|
5
|
+
export { StructureToc } from './StructureToc';
|
|
6
|
+
export type { StructureTocProps } from './StructureToc';
|
|
7
|
+
export type { BookIndex, TocEntry, BookFile, Part, BodyNode, Sentence, Cell, FrasogaLabels } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as e, jsxs as _ } from "react/jsx-runtime";
|
|
3
|
+
import { useState as I, useMemo as T, useRef as M, useCallback as V, useEffect as $, useId as O } from "react";
|
|
4
|
+
function P({ src: s, text: a, figureSrc: o }) {
|
|
5
|
+
const [i, d] = I(!1);
|
|
6
|
+
return /* @__PURE__ */ e("figure", { children: s && !i ? /* @__PURE__ */ e("img", { src: o(s), alt: a.join(" ") || "本文の図", onError: () => d(!0) }) : /* @__PURE__ */ e("div", { children: a.length ? a.join(`
|
|
7
|
+
`) : "図の画像を読み込めませんでした。" }) });
|
|
8
|
+
}
|
|
9
|
+
function q({ cells: s }) {
|
|
10
|
+
const a = new Map(s.map((t) => [`${t.row}:${t.col}`, t])), o = /* @__PURE__ */ new Set();
|
|
11
|
+
let i = 0, d = 0;
|
|
12
|
+
for (const t of s) {
|
|
13
|
+
i = Math.max(i, t.row + t.row_span), d = Math.max(d, t.col + t.col_span);
|
|
14
|
+
for (let r = t.row; r < t.row + t.row_span; r++)
|
|
15
|
+
for (let c = t.col; c < t.col + t.col_span; c++)
|
|
16
|
+
(r !== t.row || c !== t.col) && o.add(`${r}:${c}`);
|
|
17
|
+
}
|
|
18
|
+
return /* @__PURE__ */ e("div", { className: "frasoga-table", children: /* @__PURE__ */ e("table", { children: /* @__PURE__ */ e("tbody", { children: Array.from({ length: i }, (t, r) => /* @__PURE__ */ e("tr", { children: Array.from({ length: d }, (c, h) => {
|
|
19
|
+
const l = `${r}:${h}`, f = a.get(l);
|
|
20
|
+
return f ? /* @__PURE__ */ e("td", { rowSpan: f.row_span, colSpan: f.col_span, children: f.text }, h) : o.has(l) ? null : /* @__PURE__ */ e("td", {}, h);
|
|
21
|
+
}) }, r)) }) }) });
|
|
22
|
+
}
|
|
23
|
+
const z = {
|
|
24
|
+
toc: "目次",
|
|
25
|
+
front: "巻頭",
|
|
26
|
+
body: "本文",
|
|
27
|
+
hint: "← 前の行 → 次の行"
|
|
28
|
+
}, L = (s) => ({ ...z, ...s });
|
|
29
|
+
function G(s) {
|
|
30
|
+
const a = [];
|
|
31
|
+
let o = 0;
|
|
32
|
+
const i = (t) => t.filter((r) => r.trim()).map((r) => ({ index: o++, text: r })), d = (t) => {
|
|
33
|
+
for (const r of t)
|
|
34
|
+
switch (r.type) {
|
|
35
|
+
case "section": {
|
|
36
|
+
const c = i([r.title.trim()]);
|
|
37
|
+
c.length && a.push({ kind: "heading", level: r.level, lines: c }), d(r.children);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case "paragraph": {
|
|
41
|
+
const c = i(r.sentences.map(({ text: h }) => h));
|
|
42
|
+
c.length && a.push({ kind: "paragraph", lines: c });
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case "list":
|
|
46
|
+
a.push({ kind: "list", items: r.items.map(({ sentences: c }) => i(c.map(({ text: h }) => h))) });
|
|
47
|
+
break;
|
|
48
|
+
case "section_ref":
|
|
49
|
+
case "figure":
|
|
50
|
+
case "caption":
|
|
51
|
+
case "table":
|
|
52
|
+
a.push({ kind: "content", node: r });
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
return d(s), { blocks: a, count: o };
|
|
57
|
+
}
|
|
58
|
+
const A = ["h2", "h3", "h4", "h5", "h6"];
|
|
59
|
+
function J({
|
|
60
|
+
part: s,
|
|
61
|
+
title: a,
|
|
62
|
+
figureSrc: o = (g) => g,
|
|
63
|
+
onSelectFile: i,
|
|
64
|
+
current: d,
|
|
65
|
+
defaultCurrent: t = 0,
|
|
66
|
+
onCurrentChange: r,
|
|
67
|
+
onPastEnd: c,
|
|
68
|
+
onPastStart: h,
|
|
69
|
+
keyboard: l = !0,
|
|
70
|
+
labels: f
|
|
71
|
+
}) {
|
|
72
|
+
const { blocks: g, count: p } = T(() => G(s.body), [s.body]), [D, K] = I(t), H = Math.max(p - 1, 0), w = Math.min(Math.max(d ?? D, 0), H), x = M([]), v = V((n) => {
|
|
73
|
+
K(n), r == null || r(n);
|
|
74
|
+
}, [r]), E = M({ current: w, count: p, select: v, onPastEnd: c, onPastStart: h });
|
|
75
|
+
E.current = { current: w, count: p, select: v, onPastEnd: c, onPastStart: h }, $(() => {
|
|
76
|
+
if (!l) return;
|
|
77
|
+
const n = (u) => {
|
|
78
|
+
const { current: m, count: b, select: j, onPastEnd: y, onPastStart: N } = E.current;
|
|
79
|
+
!b || u.defaultPrevented || u.altKey || u.ctrlKey || u.metaKey || u.target instanceof HTMLElement && u.target.closest("input, textarea, select, [contenteditable]") || (u.key === "ArrowRight" ? (u.preventDefault(), m >= b - 1 ? y == null || y() : j(m + 1)) : u.key === "ArrowLeft" && (u.preventDefault(), m <= 0 ? N == null || N() : j(m - 1)));
|
|
80
|
+
};
|
|
81
|
+
return window.addEventListener("keydown", n), () => window.removeEventListener("keydown", n);
|
|
82
|
+
}, [l]), $(() => {
|
|
83
|
+
var n;
|
|
84
|
+
(n = x.current[w]) == null || n.scrollIntoView({ block: "center", behavior: "smooth" });
|
|
85
|
+
}, [w]);
|
|
86
|
+
const k = ({ index: n, text: u }) => /* @__PURE__ */ e(
|
|
87
|
+
"span",
|
|
88
|
+
{
|
|
89
|
+
ref: (m) => {
|
|
90
|
+
x.current[n] = m;
|
|
91
|
+
},
|
|
92
|
+
className: "frasoga-line" + (n === w ? " frasoga-line--current" : ""),
|
|
93
|
+
onClick: () => v(n),
|
|
94
|
+
children: u
|
|
95
|
+
},
|
|
96
|
+
n
|
|
97
|
+
), R = (n) => {
|
|
98
|
+
switch (n.type) {
|
|
99
|
+
case "section_ref":
|
|
100
|
+
return /* @__PURE__ */ _("button", { type: "button", onClick: () => i == null ? void 0 : i(n.file), children: [
|
|
101
|
+
n.title,
|
|
102
|
+
" →"
|
|
103
|
+
] });
|
|
104
|
+
case "figure":
|
|
105
|
+
return /* @__PURE__ */ e(P, { src: n.src, text: n.text, figureSrc: o });
|
|
106
|
+
case "caption":
|
|
107
|
+
return /* @__PURE__ */ e("p", { className: "frasoga-caption", children: n.text });
|
|
108
|
+
case "table":
|
|
109
|
+
return /* @__PURE__ */ e(q, { cells: n.cells });
|
|
110
|
+
default:
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
return /* @__PURE__ */ _("div", { className: "frasoga-part", children: [
|
|
115
|
+
/* @__PURE__ */ _("header", { className: "frasoga-part__bar", children: [
|
|
116
|
+
/* @__PURE__ */ e("span", { className: "frasoga-part__title", children: a }),
|
|
117
|
+
/* @__PURE__ */ e("span", { className: "frasoga-part__hint", children: L(f).hint }),
|
|
118
|
+
/* @__PURE__ */ _("span", { className: "frasoga-part__count", children: [
|
|
119
|
+
p ? w + 1 : 0,
|
|
120
|
+
" / ",
|
|
121
|
+
p
|
|
122
|
+
] }),
|
|
123
|
+
/* @__PURE__ */ e("div", { className: "frasoga-part__progress", style: { width: `${p ? (w + 1) / p * 100 : 0}%` } })
|
|
124
|
+
] }),
|
|
125
|
+
/* @__PURE__ */ e("article", { className: "frasoga-part__body", children: g.map((n, u) => {
|
|
126
|
+
switch (n.kind) {
|
|
127
|
+
case "heading": {
|
|
128
|
+
const m = A[Math.min(n.level - 1, A.length - 1)];
|
|
129
|
+
return /* @__PURE__ */ e(m, { children: n.lines.map(k) }, u);
|
|
130
|
+
}
|
|
131
|
+
case "paragraph":
|
|
132
|
+
return /* @__PURE__ */ e("p", { children: n.lines.map(k) }, u);
|
|
133
|
+
case "list":
|
|
134
|
+
return /* @__PURE__ */ e("ul", { children: n.items.map((m, b) => /* @__PURE__ */ e("li", { children: m.map(k) }, b)) }, u);
|
|
135
|
+
case "content":
|
|
136
|
+
return /* @__PURE__ */ e("div", { className: "frasoga-content", children: R(n.node) }, u);
|
|
137
|
+
}
|
|
138
|
+
}) })
|
|
139
|
+
] });
|
|
140
|
+
}
|
|
141
|
+
function Q(s) {
|
|
142
|
+
return /* @__PURE__ */ e(J, { ...s }, s.part.file);
|
|
143
|
+
}
|
|
144
|
+
function B(s, a = /* @__PURE__ */ new Map()) {
|
|
145
|
+
for (const o of s)
|
|
146
|
+
a.set(o.id, o), B(o.children, a);
|
|
147
|
+
return a;
|
|
148
|
+
}
|
|
149
|
+
function U({ index: s, file: a, onSelectFile: o, labels: i }) {
|
|
150
|
+
const { toc: d, front: t } = L(i), r = O(), c = M(null), h = B(s.toc);
|
|
151
|
+
return $(() => {
|
|
152
|
+
var l;
|
|
153
|
+
(l = c.current) == null || l.scrollIntoView({ block: "nearest" });
|
|
154
|
+
}, [a]), /* @__PURE__ */ _("aside", { className: "frasoga-toc", children: [
|
|
155
|
+
/* @__PURE__ */ e("h2", { id: r, className: "frasoga-toc__title", children: d }),
|
|
156
|
+
/* @__PURE__ */ e("nav", { className: "frasoga-toc__nav", "aria-labelledby": r, children: /* @__PURE__ */ e("ol", { children: s.files.map((l) => {
|
|
157
|
+
const f = l.id ? h.get(l.id) : void 0, g = l.file === a;
|
|
158
|
+
return /* @__PURE__ */ e("li", { style: { "--frasoga-depth": Math.min(((f == null ? void 0 : f.level) ?? 1) - 1, 5) }, children: /* @__PURE__ */ e(
|
|
159
|
+
"button",
|
|
160
|
+
{
|
|
161
|
+
type: "button",
|
|
162
|
+
ref: g ? c : void 0,
|
|
163
|
+
"aria-current": g ? "true" : void 0,
|
|
164
|
+
onClick: () => o == null ? void 0 : o(l.file),
|
|
165
|
+
children: (f == null ? void 0 : f.title) || (l.id === null ? t : l.file)
|
|
166
|
+
}
|
|
167
|
+
) }, l.file);
|
|
168
|
+
}) }) })
|
|
169
|
+
] });
|
|
170
|
+
}
|
|
171
|
+
function Y({ index: s, part: a, file: o, onSelectFile: i, fallback: d, labels: t, ...r }) {
|
|
172
|
+
var g;
|
|
173
|
+
const [c, h] = I(), l = o ?? c ?? ((g = s.files[0]) == null ? void 0 : g.file), f = (p) => {
|
|
174
|
+
h(p), i == null || i(p);
|
|
175
|
+
};
|
|
176
|
+
return /* @__PURE__ */ _("div", { className: "frasoga-book", children: [
|
|
177
|
+
/* @__PURE__ */ e(U, { index: s, file: l, onSelectFile: f, labels: t }),
|
|
178
|
+
/* @__PURE__ */ e("div", { className: "frasoga-book__main", role: "region", "aria-label": L(t).body, tabIndex: 0, children: a ? /* @__PURE__ */ e(Q, { ...r, labels: t, part: a, title: s.title, onSelectFile: f }) : /* @__PURE__ */ e("div", { className: "frasoga-content", children: d }) }, l)
|
|
179
|
+
] });
|
|
180
|
+
}
|
|
181
|
+
export {
|
|
182
|
+
Y as StructureBook,
|
|
183
|
+
Q as StructurePart,
|
|
184
|
+
U as StructureToc
|
|
185
|
+
};
|
package/dist/labels.d.ts
ADDED
package/dist/style.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.frasoga-book,.frasoga-book *,.frasoga-part,.frasoga-part *,.frasoga-toc,.frasoga-toc *{box-sizing:border-box}.frasoga-book,.frasoga-part,.frasoga-toc{color:var(--frasoga-text, #2b2b2b);font-family:var(--frasoga-font-family, "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif)}.frasoga-book,.frasoga-part{background:var(--frasoga-bg, #fbfaf7)}.frasoga-book{display:grid;grid-template-columns:var(--frasoga-toc-width, clamp(7rem, 28vw, 20rem)) minmax(0,1fr);height:100%;overflow:hidden}.frasoga-book__main{min-height:0;overflow-y:auto;overscroll-behavior-y:contain;overflow-wrap:anywhere}.frasoga-toc{display:flex;flex-direction:column;min-height:0;background:var(--frasoga-toc-bg, #f5f2eb);border-inline-end:1px solid var(--frasoga-border, #e6e3dc)}.frasoga-toc__title{margin:0;padding:12px 20px;border-bottom:1px solid var(--frasoga-border, #e6e3dc);font-size:1rem}.frasoga-toc__nav{flex:1;min-height:0;overflow-y:auto;overscroll-behavior-y:contain;overflow-wrap:anywhere}.frasoga-toc ol{margin:0;padding:8px;list-style:none}.frasoga-toc button,.frasoga-content button{color:inherit;font:inherit;background:transparent;border:0;border-radius:6px;cursor:pointer;text-align:left}.frasoga-toc button{width:100%;padding:8px 12px;padding-inline-start:calc(12px + var(--frasoga-depth, 0) * clamp(4px,1vw,12px))}.frasoga-toc button:hover,.frasoga-content button:hover{background:var(--frasoga-hover, #f2eee4)}.frasoga-toc button[aria-current]{background:var(--frasoga-highlight, #ffe58a);font-weight:700}.frasoga-part__bar{position:sticky;top:0;display:flex;gap:16px;align-items:center;padding:12px var(--frasoga-padding, 24px);background:var(--frasoga-bg, #fbfaf7);border-bottom:1px solid var(--frasoga-border, #e6e3dc);z-index:1}.frasoga-part__title{font-weight:700;flex:1}.frasoga-part__hint,.frasoga-part__count{color:var(--frasoga-muted, #8a8a8a);font-size:14px}.frasoga-part__count{white-space:nowrap}.frasoga-part__progress{position:absolute;left:0;bottom:-1px;height:3px;background:var(--frasoga-accent, #e0a800);transition:width .2s}.frasoga-part__body{max-width:var(--frasoga-content-width, 720px);margin:0 auto;padding:40vh var(--frasoga-padding, 24px);font-size:var(--frasoga-font-size, 20px);line-height:var(--frasoga-line-height, 2)}.frasoga-part__body h2{font-size:26px;margin:48px 0 16px}.frasoga-part__body p{margin:0 0 32px}.frasoga-line{display:block;padding:2px 10px;margin:0 -10px;border-radius:6px;cursor:pointer;opacity:.35;transition:background .15s,opacity .15s}.frasoga-line--current{background:var(--frasoga-highlight, #ffe58a);opacity:1}.frasoga-content{margin-bottom:24px}.frasoga-content button{padding:4px 8px;text-decoration:underline}.frasoga-content figure{margin:0;white-space:pre-line}.frasoga-content img{display:block;max-width:100%;height:auto;margin:auto}.frasoga-content .frasoga-caption{font-size:.9em;white-space:pre-line}.frasoga-table{overflow-x:auto}.frasoga-table table{border-collapse:collapse;font-size:.8em}.frasoga-table td{padding:6px 10px;border:1px solid var(--frasoga-table-border, #cbc5b9);vertical-align:top;white-space:pre-line}@media (max-width: 540px){.frasoga-toc__title{padding-inline:12px}.frasoga-toc ol{padding-inline:4px}.frasoga-toc button{padding-inline-end:6px;padding-inline-start:calc(6px + var(--frasoga-depth, 0) * 4px);font-size:.875rem}.frasoga-book__main .frasoga-part__bar{gap:8px;padding-inline:12px}.frasoga-book__main .frasoga-part__body{padding-inline:16px;font-size:1rem}}@media (max-width: 760px){.frasoga-book__main .frasoga-part__hint{display:none}}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export type TocEntry = {
|
|
2
|
+
id: string;
|
|
3
|
+
title: string;
|
|
4
|
+
level: number;
|
|
5
|
+
children: TocEntry[];
|
|
6
|
+
};
|
|
7
|
+
export type BookFile = {
|
|
8
|
+
file: string;
|
|
9
|
+
id: string | null;
|
|
10
|
+
};
|
|
11
|
+
export type BookIndex = {
|
|
12
|
+
title: string;
|
|
13
|
+
files: BookFile[];
|
|
14
|
+
toc: TocEntry[];
|
|
15
|
+
};
|
|
16
|
+
export type Sentence = {
|
|
17
|
+
text: string;
|
|
18
|
+
};
|
|
19
|
+
export type Cell = {
|
|
20
|
+
row: number;
|
|
21
|
+
col: number;
|
|
22
|
+
row_span: number;
|
|
23
|
+
col_span: number;
|
|
24
|
+
text: string;
|
|
25
|
+
};
|
|
26
|
+
export type BodyNode = {
|
|
27
|
+
id: string;
|
|
28
|
+
} & ({
|
|
29
|
+
type: 'section';
|
|
30
|
+
title: string;
|
|
31
|
+
level: number;
|
|
32
|
+
children: BodyNode[];
|
|
33
|
+
} | {
|
|
34
|
+
type: 'section_ref';
|
|
35
|
+
title: string;
|
|
36
|
+
file: string;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'paragraph';
|
|
39
|
+
sentences: Sentence[];
|
|
40
|
+
} | {
|
|
41
|
+
type: 'list';
|
|
42
|
+
items: {
|
|
43
|
+
sentences: Sentence[];
|
|
44
|
+
}[];
|
|
45
|
+
} | {
|
|
46
|
+
type: 'figure';
|
|
47
|
+
src: string | null;
|
|
48
|
+
text: string[];
|
|
49
|
+
} | {
|
|
50
|
+
type: 'caption';
|
|
51
|
+
text: string;
|
|
52
|
+
} | {
|
|
53
|
+
type: 'table';
|
|
54
|
+
cells: Cell[];
|
|
55
|
+
});
|
|
56
|
+
export type Part = {
|
|
57
|
+
file: string;
|
|
58
|
+
body: BodyNode[];
|
|
59
|
+
};
|
|
60
|
+
/** 画面に出る固定の文言 */
|
|
61
|
+
export type FrasogaLabels = {
|
|
62
|
+
/** 目次の見出し */
|
|
63
|
+
toc: string;
|
|
64
|
+
/** index の id が null のファイル (巻頭) の目次の表示 */
|
|
65
|
+
front: string;
|
|
66
|
+
/** 本文領域の aria-label */
|
|
67
|
+
body: string;
|
|
68
|
+
/** 上部バーの操作の案内 */
|
|
69
|
+
hint: string;
|
|
70
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@yanqirenshi/frasoga",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@yanqirenshi/frasoga",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": ["dist"],
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./style.css": "./dist/style.css"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": ["**/*.css"],
|
|
17
|
+
"publishConfig": { "access": "public" },
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite --config demo/vite.config.ts",
|
|
20
|
+
"build": "vite build && tsc -p tsconfig.build.json",
|
|
21
|
+
"build:demo": "tsc --noEmit && vite build --config demo/vite.config.ts",
|
|
22
|
+
"export": "node export/export-book.mjs",
|
|
23
|
+
"pretest": "npm run build",
|
|
24
|
+
"test": "node --test"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": ">=18",
|
|
28
|
+
"react-dom": ">=18"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^22.0.0",
|
|
32
|
+
"@types/react": "^18.3.3",
|
|
33
|
+
"@types/react-dom": "^18.3.0",
|
|
34
|
+
"@vitejs/plugin-react": "^4.3.1",
|
|
35
|
+
"react": "^18.3.1",
|
|
36
|
+
"react-dom": "^18.3.1",
|
|
37
|
+
"typescript": "^5.5.4",
|
|
38
|
+
"vite": "^5.4.0"
|
|
39
|
+
}
|
|
40
|
+
}
|