-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
371 lines (336 loc) · 18.3 KB
/
Copy pathscript.js
File metadata and controls
371 lines (336 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
document.addEventListener('DOMContentLoaded', () => {
// --- GŁÓWNY ROUTER APLIKACJI ---
const fileInput = document.getElementById('fileInput');
const statusLabel = document.getElementById('status');
const MAIN_CONTAINER_SIGNATURE = 'YUFFIN';
const IMAGE_ARCHIVE_SIGNATURE = 'Yuffin';
fileInput.addEventListener('change', handleFileSelect);
document.querySelectorAll('.close-btn').forEach(btn => {
btn.addEventListener('click', () => {
const modal = document.getElementById(btn.dataset.modalId);
if (modal) {
modal.style.display = 'none';
cleanup(btn.dataset.modalId);
}
});
});
async function handleFileSelect(event) {
const file = event.target.files[0];
if (!file) return;
statusLabel.textContent = `Analyzing file: ${file.name}...`;
try {
const headerSlice = file.slice(0, 6);
const headerBuffer = await headerSlice.arrayBuffer();
const signature = new TextDecoder().decode(headerBuffer);
if (signature === MAIN_CONTAINER_SIGNATURE) {
mediaViewer.initialize(file);
} else if (signature === IMAGE_ARCHIVE_SIGNATURE) {
imageViewer.initialize(file, true);
} else {
throw new Error("Unknown or invalid file signature.");
}
} catch (error) {
handleInitError(error);
}
}
function cleanup(modalId) {
if (modalId === 'media-viewer-modal') mediaViewer.cleanup();
else if (modalId === 'image-viewer-modal') imageViewer.cleanup();
fileInput.value = '';
statusLabel.textContent = '';
}
function handleInitError(err) {
statusLabel.textContent = `ERROR: ${err.message}`;
alert(`Error loading file: ${err.message}`);
}
// --- MODUŁ ODTWARZACZA MEDIÓW (.YUF) ---
const mediaViewer = (() => {
const modal = document.getElementById('media-viewer-modal');
if (!modal) return { initialize: () => {}, cleanup: () => {} };
const title = document.getElementById('media-modal-title');
const mediaListElement = document.getElementById('mediaList');
const playerPanel = document.getElementById('player-panel');
let fileHandle, index, currentUrl;
async function initialize(file) {
fileHandle = file;
title.textContent = file.name;
const MAIN_HEADER_SIZE = 16;
const dataView = new DataView(await file.slice(0, MAIN_HEADER_SIZE).arrayBuffer());
const indexSize = dataView.getBigUint64(8, false);
const indexEndOffset = MAIN_HEADER_SIZE + Number(indexSize);
const indexSlice = file.slice(MAIN_HEADER_SIZE, indexEndOffset);
const indexJson = new TextDecoder().decode(Uint8Array.from(atob(await indexSlice.text()), c => c.charCodeAt(0)));
index = JSON.parse(indexJson);
renderList();
modal.style.display = 'flex';
}
function renderList() {
mediaListElement.innerHTML = '';
index.forEach(info => {
const li = document.createElement('li');
li.title = info.name;
li.innerHTML = `<span class="media-name">${info.name}</span><span class="media-size">${(info.size / 1024/1024).toFixed(2)} MB</span>`;
li.addEventListener('click', () => {
document.querySelectorAll('#mediaList li').forEach(el => el.classList.remove('active'));
li.classList.add('active');
if (info.mime === 'application/vnd.yuffin-image-archive') {
openNestedImageViewer(info);
} else {
play(info);
}
});
mediaListElement.appendChild(li);
});
}
function play(info) {
if (currentUrl) URL.revokeObjectURL(currentUrl);
const slice = fileHandle.slice(info.offset, info.offset + info.size);
const blob = new Blob([slice], { type: info.mime });
currentUrl = URL.createObjectURL(blob);
const tag = info.mime.startsWith('video/') ? 'video' : 'audio';
playerPanel.innerHTML = `<${tag} id="mediaPlayer" controls autoplay></${tag}>`;
playerPanel.querySelector('#mediaPlayer').src = currentUrl;
}
function openNestedImageViewer(info) {
const slice = fileHandle.slice(info.offset, info.offset + info.size);
const blobAsFile = new File([slice], info.name, { type: 'application/vnd.yuffin-image-archive' });
modal.style.display = 'none';
imageViewer.initialize(blobAsFile, false);
}
function cleanup() {
if (currentUrl) URL.revokeObjectURL(currentUrl);
currentUrl = null; mediaListElement.innerHTML = '';
playerPanel.innerHTML = 'Wybierz plik z listy, aby rozpocząć odtwarzanie.';
fileHandle = null; index = null;
}
return { initialize, cleanup };
})();
// --- MODUŁ PRZEGLĄDARKI OBRAZÓW (.YUFI) ---
const imageViewer = (() => {
const modal = document.getElementById('image-viewer-modal');
if (!modal) return { initialize: () => {}, cleanup: () => {} };
const title = document.getElementById('image-modal-title');
const gallery = document.getElementById('image-gallery-container');
const dirFilter = document.getElementById('dir-filter');
const viewToggle = document.getElementById('view-toggle');
const pagination = document.getElementById('pagination');
const chapterNavTop = document.getElementById('chapter-navigation-top');
const chapterNavBottom = document.getElementById('chapter-navigation-bottom');
const fullscreenBtn = document.getElementById('fullscreen-btn');
const lightbox = document.getElementById('lightbox');
const lightboxImage = document.getElementById('lightbox-image');
const lightboxCounter = document.getElementById('lightbox-counter');
const closeLightboxBtn = document.getElementById('close-lightbox-btn');
const prevLightboxBtn = document.getElementById('prev-lightbox-btn');
const nextLightboxBtn = document.getElementById('next-lightbox-btn');
let fileHandle, allImages = [], filteredImages = [], directories = [], sortedChapterDirs = [];
let createdUrls = []; let lightboxUrl = null;
const IMAGES_PER_PAGE = 36;
let currentPage = 1, currentChapterIndex = 0, currentLightboxIndex = 0;
let isOpenedFromLauncher = false;
async function initialize(file, fromLauncher) {
isOpenedFromLauncher = fromLauncher;
fileHandle = file;
title.textContent = file.name;
const HEADER_SIZE = 38, ENTRY_SIZE = 8;
const headerView = new DataView(await file.slice(0, HEADER_SIZE).arrayBuffer());
if (new TextDecoder().decode(headerView.buffer.slice(0, 6)) !== IMAGE_ARCHIVE_SIGNATURE) throw new Error("Invalid .yufi file signature (expected 'Yuffin').");
const imageCount = Number(headerView.getBigUint64(10, true));
const dirCount = headerView.getUint32(18, true);
const dirTableOffset = Number(headerView.getBigUint64(22, true));
const fileIndexOffset = Number(headerView.getBigUint64(30, true));
const dirTableText = await file.slice(dirTableOffset, fileIndexOffset).text();
directories = dirTableText.split('\0').slice(0, dirCount);
const fileIndexBuffer = await file.slice(fileIndexOffset, fileIndexOffset + imageCount * ENTRY_SIZE).arrayBuffer();
const fileIndexView = new DataView(fileIndexBuffer);
allImages = Array.from({ length: imageCount }, (_, i) => ({
offset: fileIndexView.getUint32(i * ENTRY_SIZE, true),
dirId: fileIndexView.getUint16(i * ENTRY_SIZE + 4, true),
globalIndex: i // Zapamiętaj globalny indeks
}));
populateFilter();
applyFilter();
modal.style.display = 'flex';
}
function naturalSortKey(s) {
return s.split(/(\d+)/).map(t => { const num = parseInt(t); return isNaN(num) ? t.toLowerCase() : num; });
}
function populateFilter() {
dirFilter.innerHTML = '<option value="-1">Wszystkie katalogi</option>';
const chapterPrefixRegex = /^chapter_/i;
sortedChapterDirs = directories
.filter(dir => chapterPrefixRegex.test(dir))
.sort((a, b) => naturalSortKey(a).toString().localeCompare(naturalSortKey(b).toString(), undefined, { numeric: true }));
const sortedDirs = [...directories].sort((a, b) => naturalSortKey(a).toString().localeCompare(naturalSortKey(b).toString(), undefined, { numeric: true }));
sortedDirs.forEach(dir => {
const dirId = directories.indexOf(dir);
const option = document.createElement('option');
option.value = dirId;
option.textContent = (dir === '' || dir === '.') ? '[Główny katalog]' : dir;
dirFilter.appendChild(option);
});
}
function applyFilter() {
const selectedDirId = parseInt(dirFilter.value, 10);
filteredImages = (selectedDirId === -1) ? allImages : allImages.filter(img => img.dirId === selectedDirId);
currentPage = 1;
updateDisplay();
}
function updateDisplay() {
gallery.className = viewToggle.checked ? 'comic-view' : 'grid-view';
if (viewToggle.checked) {
pagination.style.display = 'none';
if(sortedChapterDirs.length > 0) {
chapterNavTop.style.display = 'flex'; chapterNavBottom.style.display = 'flex';
const selectedDirName = directories[parseInt(dirFilter.value, 10)] || sortedChapterDirs[0];
currentChapterIndex = sortedChapterDirs.indexOf(selectedDirName);
if(currentChapterIndex === -1) currentChapterIndex = 0;
updateChapterNavigation(); renderCurrentChapter();
} else {
chapterNavTop.style.display = 'none'; chapterNavBottom.style.display = 'none';
renderAllImagesComic();
}
} else {
chapterNavTop.style.display = 'none'; chapterNavBottom.style.display = 'none';
pagination.style.display = 'flex';
renderPage(1);
}
}
function renderPage(page) {
currentPage = page; gallery.innerHTML = '';
const totalPages = Math.ceil(filteredImages.length / IMAGES_PER_PAGE);
const start = (page - 1) * IMAGES_PER_PAGE;
const end = start + IMAGES_PER_PAGE;
filteredImages.slice(start, end).forEach((info, indexInPage) => {
const itemDiv = document.createElement('div'); itemDiv.className = 'gallery-item';
const img = document.createElement('img');
itemDiv.appendChild(img); gallery.appendChild(itemDiv);
loadImage(info, img, start + indexInPage); // Przekaż indeks w przefiltrowanej liście
});
renderPagination(totalPages);
}
function renderAllImagesComic() {
gallery.innerHTML = '';
filteredImages.forEach((info, index) => {
const itemDiv = document.createElement('div'); itemDiv.className = 'gallery-item';
const img = document.createElement('img'); img.loading = 'lazy';
itemDiv.appendChild(img); gallery.appendChild(itemDiv);
loadImage(info, img, index);
});
}
function renderCurrentChapter() {
const chapterDirName = sortedChapterDirs[currentChapterIndex];
const chapterDirId = directories.indexOf(chapterDirName);
filteredImages = allImages.filter(img => img.dirId === chapterDirId);
renderAllImagesComic();
modal.querySelector('.modal-content').scrollTop = 0;
}
function updateChapterNavigation() {
const chapterName = sortedChapterDirs[currentChapterIndex];
document.getElementById('current-chapter-top').textContent = chapterName;
document.getElementById('current-chapter-bottom').textContent = chapterName;
const prevDisabled = currentChapterIndex === 0;
const nextDisabled = currentChapterIndex === sortedChapterDirs.length - 1;
document.getElementById('prev-chapter-top').disabled = prevDisabled;
document.getElementById('prev-chapter-bottom').disabled = prevDisabled;
document.getElementById('next-chapter-top').disabled = nextDisabled;
document.getElementById('next-chapter-bottom').disabled = nextDisabled;
}
function renderPagination(totalPages) {
pagination.innerHTML = totalPages > 1 ? `<button id="prev-page">‹</button> <span>Strona ${currentPage} / ${totalPages}</span> <button id="next-page">›</button>` : '';
if(totalPages > 1) {
document.getElementById('prev-page').disabled = currentPage === 1;
document.getElementById('next-page').disabled = currentPage === totalPages;
document.getElementById('prev-page').onclick = () => renderPage(currentPage - 1);
document.getElementById('next-page').onclick = () => renderPage(currentPage + 1);
}
}
async function loadImage(imageInfo, imgElement, filteredIndex) {
try {
const headerView = new DataView(await fileHandle.slice(imageInfo.offset, imageInfo.offset + 8).arrayBuffer());
const imageSize = headerView.getUint32(4, true);
const imageBlob = fileHandle.slice(imageInfo.offset + 8, imageInfo.offset + 8 + imageSize);
const url = URL.createObjectURL(imageBlob);
createdUrls.push(url);
imgElement.src = url;
imgElement.classList.add('loaded');
// --- NOWOŚĆ: Dodaj listener tylko w widoku siatki ---
if (!viewToggle.checked) {
imgElement.onclick = () => openLightbox(filteredIndex);
}
} catch (e) { console.error("Error loading image:", e); }
}
function cleanup() {
if (document.fullscreenElement) { document.exitFullscreen(); }
closeLightbox(); // Zamknij lightbox jeśli jest otwarty
gallery.innerHTML = ''; pagination.innerHTML = '';
createdUrls.forEach(url => URL.revokeObjectURL(url));
createdUrls = []; allImages = []; filteredImages = []; directories = []; fileHandle = null;
if (!isOpenedFromLauncher) {
document.getElementById('media-viewer-modal').style.display = 'flex';
}
}
function changeChapter(direction) {
const newIndex = currentChapterIndex + direction;
if (newIndex >= 0 && newIndex < sortedChapterDirs.length) {
currentChapterIndex = newIndex;
const chapterDirId = directories.indexOf(sortedChapterDirs[currentChapterIndex]);
dirFilter.value = chapterDirId;
updateChapterNavigation();
renderCurrentChapter();
}
}
function toggleFullscreen() {
if (!document.fullscreenElement) {
modal.requestFullscreen().catch(err => { alert(`Error: ${err.message}`); });
} else { document.exitFullscreen(); }
}
// --- NOWA LOGIKA LIGHTBOXA ---
function openLightbox(index) {
currentLightboxIndex = index;
changeLightboxImage(0); // Pokaż pierwszy obraz bez zmiany indeksu
lightbox.classList.add('visible');
}
function closeLightbox() {
lightbox.classList.remove('visible');
if (lightboxUrl) URL.revokeObjectURL(lightboxUrl);
lightboxUrl = null;
}
async function changeLightboxImage(direction) {
currentLightboxIndex += direction;
if (currentLightboxIndex < 0) currentLightboxIndex = filteredImages.length - 1;
if (currentLightboxIndex >= filteredImages.length) currentLightboxIndex = 0;
const imageInfo = filteredImages[currentLightboxIndex];
if (!imageInfo) return;
if (lightboxUrl) URL.revokeObjectURL(lightboxUrl);
const headerView = new DataView(await fileHandle.slice(imageInfo.offset, imageInfo.offset + 8).arrayBuffer());
const imageSize = headerView.getUint32(4, true);
const imageBlob = fileHandle.slice(imageInfo.offset + 8, imageInfo.offset + 8 + imageSize);
lightboxUrl = URL.createObjectURL(imageBlob);
lightboxImage.src = lightboxUrl;
lightboxCounter.textContent = `${currentLightboxIndex + 1} / ${filteredImages.length}`;
}
// Listenery
dirFilter.addEventListener('change', applyFilter);
viewToggle.addEventListener('change', updateDisplay);
fullscreenBtn.addEventListener('click', toggleFullscreen);
closeLightboxBtn.addEventListener('click', closeLightbox);
prevLightboxBtn.addEventListener('click', () => changeLightboxImage(-1));
nextLightboxBtn.addEventListener('click', () => changeLightboxImage(1));
['top', 'bottom'].forEach(pos => {
document.getElementById(`prev-chapter-${pos}`).onclick = () => changeChapter(-1);
document.getElementById(`next-chapter-${pos}`).onclick = () => changeChapter(1);
});
document.addEventListener('keydown', (e) => {
if (modal.style.display === 'flex' && e.key.toLowerCase() === 'f') {
e.preventDefault(); toggleFullscreen();
}
if (lightbox.classList.contains('visible')) {
if (e.key === 'Escape') closeLightbox();
if (e.key === 'ArrowRight') changeLightboxImage(1);
if (e.key === 'ArrowLeft') changeLightboxImage(-1);
}
});
return { initialize, cleanup };
})();
});