@papyrus-sdk/ui-react-native 0.1.0 → 0.1.1

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.
@@ -0,0 +1,431 @@
1
+ (function () {
2
+ const viewer = document.getElementById('viewer');
3
+ const DEFAULT_FONT_SIZE = 16;
4
+ const TEXT_PAGE_CHUNK = 1600;
5
+
6
+ let currentType = null;
7
+ let book = null;
8
+ let rendition = null;
9
+ let spineItems = [];
10
+ let textPages = [];
11
+ let textContainer = null;
12
+ let currentPage = 1;
13
+ let pageCount = 0;
14
+ let zoom = 1.0;
15
+
16
+ const sendMessage = (payload) => {
17
+ if (window.ReactNativeWebView && window.ReactNativeWebView.postMessage) {
18
+ window.ReactNativeWebView.postMessage(JSON.stringify(payload));
19
+ return;
20
+ }
21
+ if (window.parent && window.parent !== window) {
22
+ window.parent.postMessage(payload, '*');
23
+ }
24
+ };
25
+
26
+ const sendResponse = (id, ok, data, error) => {
27
+ sendMessage({ type: 'response', id, ok, data, error });
28
+ };
29
+
30
+ const sendState = (extra) => {
31
+ sendMessage({
32
+ type: 'state',
33
+ payload: {
34
+ pageCount,
35
+ currentPage,
36
+ zoom,
37
+ ...(extra || {}),
38
+ },
39
+ });
40
+ };
41
+
42
+ const sendEvent = (name, payload) => {
43
+ sendMessage({ type: 'event', name, payload });
44
+ };
45
+
46
+ const clearViewer = () => {
47
+ while (viewer.firstChild) {
48
+ viewer.removeChild(viewer.firstChild);
49
+ }
50
+ };
51
+
52
+ const decodeBase64 = (value) => {
53
+ const clean = value.replace(/\s/g, '');
54
+ const binary = atob(clean);
55
+ const len = binary.length;
56
+ const bytes = new Uint8Array(len);
57
+ for (let i = 0; i < len; i += 1) {
58
+ bytes[i] = binary.charCodeAt(i);
59
+ }
60
+ return bytes;
61
+ };
62
+
63
+ const decodeBase64ToText = (value) => {
64
+ const bytes = decodeBase64(value);
65
+ return new TextDecoder('utf-8').decode(bytes);
66
+ };
67
+
68
+ const normalizeHref = (href) => {
69
+ if (!href) return '';
70
+ return href.split('#')[0];
71
+ };
72
+
73
+ const getSpineIndexByHref = (href) => {
74
+ const normalized = normalizeHref(href);
75
+ if (!normalized) return -1;
76
+ return spineItems.findIndex((item) => normalizeHref(item.href) === normalized);
77
+ };
78
+
79
+ const buildOutline = async () => {
80
+ if (!book || !book.loaded || !book.loaded.navigation) return [];
81
+ const nav = await book.loaded.navigation;
82
+ const toc = nav && nav.toc ? nav.toc : [];
83
+
84
+ const mapItem = (item) => {
85
+ const title = item.label || item.title || '';
86
+ const pageIndex = getSpineIndexByHref(item.href || '');
87
+ const children = Array.isArray(item.subitems) ? item.subitems.map(mapItem) : [];
88
+ const outlineItem = { title, pageIndex };
89
+ if (children.length > 0) outlineItem.children = children;
90
+ return outlineItem;
91
+ };
92
+
93
+ return toc.map(mapItem);
94
+ };
95
+
96
+ const applyEpubZoom = () => {
97
+ if (!rendition || !rendition.themes) return;
98
+ const fontSize = `${Math.round(zoom * 100)}%`;
99
+ if (typeof rendition.themes.fontSize === 'function') {
100
+ rendition.themes.fontSize(fontSize);
101
+ } else if (typeof rendition.themes.override === 'function') {
102
+ rendition.themes.override('font-size', fontSize);
103
+ }
104
+ };
105
+
106
+ const applyTextZoom = () => {
107
+ if (!textContainer) return;
108
+ const fontSize = Math.max(12, Math.round(DEFAULT_FONT_SIZE * zoom));
109
+ textContainer.style.fontSize = `${fontSize}px`;
110
+ };
111
+
112
+ const renderTextPage = (pageIndex) => {
113
+ const text = textPages[pageIndex] || '';
114
+ if (!textContainer) {
115
+ textContainer = document.createElement('div');
116
+ textContainer.style.padding = '24px';
117
+ textContainer.style.lineHeight = '1.6';
118
+ textContainer.style.whiteSpace = 'pre-wrap';
119
+ textContainer.style.fontFamily = 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace';
120
+ viewer.appendChild(textContainer);
121
+ }
122
+ textContainer.textContent = text;
123
+ applyTextZoom();
124
+ };
125
+
126
+ const paginateText = (text) => {
127
+ const pages = [];
128
+ for (let i = 0; i < text.length; i += TEXT_PAGE_CHUNK) {
129
+ pages.push(text.slice(i, i + TEXT_PAGE_CHUNK));
130
+ }
131
+ return pages.length > 0 ? pages : [''];
132
+ };
133
+
134
+ const loadText = async (source) => {
135
+ let text = '';
136
+ if (source.kind === 'uri') {
137
+ const res = await fetch(source.uri);
138
+ text = await res.text();
139
+ } else if (source.kind === 'base64') {
140
+ text = decodeBase64ToText(source.data);
141
+ } else if (source.kind === 'text') {
142
+ text = source.text || '';
143
+ }
144
+
145
+ clearViewer();
146
+ textPages = paginateText(text);
147
+ pageCount = textPages.length;
148
+ currentPage = 1;
149
+ textContainer = null;
150
+ renderTextPage(0);
151
+ return { pageCount };
152
+ };
153
+
154
+ const loadEpub = async (source) => {
155
+ if (rendition && typeof rendition.destroy === 'function') {
156
+ rendition.destroy();
157
+ }
158
+ if (book && typeof book.destroy === 'function') {
159
+ book.destroy();
160
+ }
161
+
162
+ let data = null;
163
+ if (source.kind === 'uri') {
164
+ data = source.uri;
165
+ } else if (source.kind === 'base64') {
166
+ data = decodeBase64(source.data);
167
+ } else if (source.kind === 'text') {
168
+ const encoder = new TextEncoder();
169
+ data = encoder.encode(source.text || '').buffer;
170
+ }
171
+
172
+ book = ePub(data);
173
+ await book.ready;
174
+
175
+ spineItems = book.spine && book.spine.items ? book.spine.items : [];
176
+ pageCount = spineItems.length;
177
+ currentPage = 1;
178
+
179
+ clearViewer();
180
+ rendition = book.renderTo(viewer, {
181
+ width: '100%',
182
+ height: '100%',
183
+ flow: 'paginated',
184
+ spread: 'none',
185
+ });
186
+
187
+ if (rendition && rendition.hooks && rendition.hooks.content) {
188
+ rendition.hooks.content.register((contents) => {
189
+ const frame = contents && contents.document ? contents.document.defaultView.frameElement : null;
190
+ if (frame) {
191
+ frame.setAttribute('sandbox', 'allow-scripts allow-same-origin');
192
+ }
193
+ });
194
+ }
195
+
196
+ if (rendition && typeof rendition.on === 'function') {
197
+ rendition.on('selected', (cfiRange, contents) => {
198
+ const selection = contents && contents.window ? contents.window.getSelection() : null;
199
+ const text = selection ? selection.toString().trim() : '';
200
+ if (text) {
201
+ sendEvent('TEXT_SELECTED', { text, pageIndex: Math.max(0, currentPage - 1) });
202
+ }
203
+ if (rendition && rendition.annotations && typeof rendition.annotations.remove === 'function') {
204
+ rendition.annotations.remove(cfiRange, 'highlight');
205
+ }
206
+ });
207
+ }
208
+
209
+ await displayEpubPage(0);
210
+ applyEpubZoom();
211
+
212
+ const outline = await buildOutline();
213
+ return { pageCount, outline };
214
+ };
215
+
216
+ const displayEpubPage = async (pageIndex) => {
217
+ if (!rendition) return;
218
+ const item = spineItems[pageIndex];
219
+ if (!item) return;
220
+ const target = item.href || item.idref || item.cfiBase || pageIndex;
221
+ await rendition.display(target);
222
+ currentPage = pageIndex + 1;
223
+ sendState();
224
+ };
225
+
226
+ const getTextContent = async (pageIndex) => {
227
+ if (currentType === 'text') {
228
+ const text = textPages[pageIndex] || '';
229
+ return [{
230
+ str: text,
231
+ dir: 'ltr',
232
+ width: 0,
233
+ height: 0,
234
+ transform: [1, 0, 0, 1, 0, 0],
235
+ fontName: 'default',
236
+ }];
237
+ }
238
+
239
+ if (currentType === 'epub') {
240
+ if (!book) return [];
241
+ const item = spineItems[pageIndex];
242
+ if (!item) return [];
243
+ try {
244
+ const section = book.spine.get(item.idref || item.href);
245
+ const text = section && typeof section.text === 'function' ? await section.text() : '';
246
+ if (!text) return [];
247
+ return [{
248
+ str: text,
249
+ dir: 'ltr',
250
+ width: 0,
251
+ height: 0,
252
+ transform: [1, 0, 0, 1, 0, 0],
253
+ fontName: 'default',
254
+ }];
255
+ } catch (err) {
256
+ return [];
257
+ }
258
+ }
259
+
260
+ return [];
261
+ };
262
+
263
+ const getPageText = async (pageIndex) => {
264
+ const items = await getTextContent(pageIndex);
265
+ return items.map((item) => item.str).join(' ');
266
+ };
267
+
268
+ const searchText = async (query) => {
269
+ const normalized = query.toLowerCase();
270
+ const results = [];
271
+
272
+ for (let i = 0; i < pageCount; i += 1) {
273
+ const text = await getPageText(i);
274
+ if (!text) continue;
275
+ const lower = text.toLowerCase();
276
+ let pos = lower.indexOf(normalized, 0);
277
+ let matchIndex = 0;
278
+ while (pos !== -1) {
279
+ const start = Math.max(0, pos - 20);
280
+ const end = Math.min(text.length, pos + query.length + 20);
281
+ results.push({ pageIndex: i, text: text.substring(start, end), matchIndex });
282
+ matchIndex += 1;
283
+ pos = lower.indexOf(normalized, pos + 1);
284
+ }
285
+ }
286
+
287
+ return results;
288
+ };
289
+
290
+ const getPageDimensions = () => ({
291
+ width: viewer.clientWidth || 0,
292
+ height: viewer.clientHeight || 0,
293
+ });
294
+
295
+ const getPageIndex = (dest) => {
296
+ if (currentType !== 'epub') return null;
297
+ if (typeof dest === 'string') return getSpineIndexByHref(dest);
298
+ return null;
299
+ };
300
+
301
+ const selectText = async (pageIndex) => {
302
+ const text = await getPageText(pageIndex);
303
+ if (!text) return null;
304
+ return {
305
+ text,
306
+ rects: [{ x: 0, y: 0, width: 1, height: 1 }],
307
+ };
308
+ };
309
+
310
+ const applyZoom = () => {
311
+ if (currentType === 'text') {
312
+ applyTextZoom();
313
+ } else if (currentType === 'epub') {
314
+ applyEpubZoom();
315
+ }
316
+ };
317
+
318
+ const handleCommand = async (message) => {
319
+ const { id, kind, payload } = message;
320
+
321
+ try {
322
+ if (kind === 'load') {
323
+ currentType = payload.type;
324
+ zoom = 1.0;
325
+
326
+ if (currentType === 'text') {
327
+ const result = await loadText(payload.source);
328
+ sendState();
329
+ sendResponse(id, true, result);
330
+ return;
331
+ }
332
+
333
+ if (currentType === 'epub') {
334
+ const result = await loadEpub(payload.source);
335
+ sendState({ outline: result.outline || [] });
336
+ sendResponse(id, true, result);
337
+ return;
338
+ }
339
+
340
+ throw new Error('Unsupported document type');
341
+ }
342
+
343
+ if (kind === 'go-to-page') {
344
+ const page = Math.max(1, payload.page || 1);
345
+ if (currentType === 'text') {
346
+ currentPage = page;
347
+ renderTextPage(page - 1);
348
+ sendState();
349
+ } else if (currentType === 'epub') {
350
+ await displayEpubPage(page - 1);
351
+ }
352
+ sendResponse(id, true, { currentPage });
353
+ return;
354
+ }
355
+
356
+ if (kind === 'set-zoom') {
357
+ zoom = Math.max(0.5, Math.min(4.0, payload.zoom || 1.0));
358
+ applyZoom();
359
+ sendState();
360
+ sendResponse(id, true, { zoom });
361
+ return;
362
+ }
363
+
364
+ if (kind === 'set-rotation') {
365
+ sendResponse(id, true, {});
366
+ return;
367
+ }
368
+
369
+ if (kind === 'get-text-content') {
370
+ const items = await getTextContent(payload.pageIndex || 0);
371
+ sendResponse(id, true, items);
372
+ return;
373
+ }
374
+
375
+ if (kind === 'get-page-dimensions') {
376
+ sendResponse(id, true, getPageDimensions());
377
+ return;
378
+ }
379
+
380
+ if (kind === 'search-text') {
381
+ const results = await searchText(payload.query || '');
382
+ sendResponse(id, true, results);
383
+ return;
384
+ }
385
+
386
+ if (kind === 'select-text') {
387
+ const selection = await selectText(payload.pageIndex || 0);
388
+ sendResponse(id, true, selection);
389
+ return;
390
+ }
391
+
392
+ if (kind === 'get-outline') {
393
+ const outline = await buildOutline();
394
+ sendResponse(id, true, outline);
395
+ return;
396
+ }
397
+
398
+ if (kind === 'get-page-index') {
399
+ const index = getPageIndex(payload.dest);
400
+ sendResponse(id, true, index);
401
+ return;
402
+ }
403
+
404
+ if (kind === 'destroy') {
405
+ clearViewer();
406
+ sendResponse(id, true, {});
407
+ return;
408
+ }
409
+
410
+ sendResponse(id, false, null, 'Unknown command');
411
+ } catch (err) {
412
+ sendResponse(id, false, null, err && err.message ? err.message : String(err));
413
+ }
414
+ };
415
+
416
+ const onMessage = (event) => {
417
+ let message = null;
418
+ try {
419
+ message = JSON.parse(event.data);
420
+ } catch (err) {
421
+ return;
422
+ }
423
+ if (!message || !message.kind || !message.id) return;
424
+ handleCommand(message);
425
+ };
426
+
427
+ window.addEventListener('message', onMessage);
428
+ document.addEventListener('message', onMessage);
429
+
430
+ sendMessage({ type: 'ready' });
431
+ })();
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Papyrus Contributors
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.