-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgallery_page.js
More file actions
456 lines (416 loc) · 16.7 KB
/
Copy pathgallery_page.js
File metadata and controls
456 lines (416 loc) · 16.7 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Controller for the top-level gallery page.
(function () {
'use strict';
var grid = null;
var toastEl = null;
var importInput = null;
var shareOverlay = null;
var shareSheet = null;
var toastTimer = null;
var busyProjectId = null;
var importBtn = null;
var previewCache = {};
function isIosApp() {
try {
return !!(window && (window.FLICKGAME_HOST === 'ios-app' || window.FLICKGAME_IOS_APP));
} catch (e) {
return false;
}
}
function syncIosViewportState() {
if (!isIosApp()) return;
var root = document.documentElement;
var vv = window.visualViewport;
var width = vv && vv.width ? vv.width : (window.innerWidth || root.clientWidth || 0);
var height = vv && vv.height ? vv.height : (window.innerHeight || root.clientHeight || 0);
root.setAttribute('data-ios-app', 'true');
root.setAttribute('data-gallery-orientation', width > height ? 'landscape' : 'portrait');
}
function el(tag, attrs, children) {
var n = document.createElement(tag);
if (attrs) {
Object.keys(attrs).forEach(function (k) {
if (k === 'class') n.className = attrs[k];
else if (k === 'text') n.textContent = attrs[k];
else if (k === 'html') n.innerHTML = attrs[k];
else n.setAttribute(k, attrs[k]);
});
}
(children || []).forEach(function (c) {
if (c == null) return;
if (typeof c === 'string') n.appendChild(document.createTextNode(c));
else n.appendChild(c);
});
return n;
}
function showToast(message) {
if (!toastEl) return;
toastEl.textContent = message;
toastEl.classList.add('visible');
if (toastTimer) clearTimeout(toastTimer);
toastTimer = setTimeout(function () {
toastEl.classList.remove('visible');
}, 2400);
}
function nextDefaultTitle(items) {
var used = {};
(items || []).forEach(function (p) {
var m = /^flickgame #(\d+)$/i.exec((p && p.title) || '');
if (m) used[parseInt(m[1], 10)] = true;
});
var i = 1;
while (used[i]) i += 1;
return 'flickgame #' + i;
}
function resolveUniqueTitle(rawInput, items, currentId) {
var trimmed = (rawInput || '').trim();
if (!trimmed) return nextDefaultTitle(items);
var base = trimmed.replace(/\s+\d+$/, '').trim();
if (!base) return nextDefaultTitle(items);
var used = {};
(items || []).forEach(function (p) {
if (!p || p.id === currentId || typeof p.title !== 'string') return;
used[p.title] = true;
});
if (!used[base]) return base;
var n = 2;
while (used[base + ' ' + n]) n += 1;
return base + ' ' + n;
}
function formatTime(ms) {
try {
return new Date(ms).toLocaleString();
} catch (e) {
return '' + ms;
}
}
function navigate(url) {
window.location.href = url;
}
function decodeRleCanvas(rle, pixelCount) {
var out = new Uint8Array(pixelCount);
var index = 0;
for (var i = 0; i < rle.length; i += 2) {
var count = rle[i];
var ch = rle[i + 1];
for (var j = 0; j < count && index < pixelCount; j++) {
out[index++] = parseInt(ch, 16);
}
}
return out;
}
function contrastingInk(hex) {
var m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex || '');
if (!m) return '#ffffff';
var r = parseInt(m[1], 16);
var g = parseInt(m[2], 16);
var b = parseInt(m[3], 16);
var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
return lum < 0.5 ? '#ffffff' : '#000000';
}
function renderPreviewFromState(stateString) {
if (typeof stateString !== 'string' || !stateString) return null;
var parsed = JSON.parse(stateString);
if (parsed.palette_name === undefined) parsed.palette_name = 'dawnbringer-16';
if (parsed.background_color === undefined) parsed.background_color = '#000000';
if (parsed.foreground_color === undefined) parsed.foreground_color = '#ffffff';
if (parsed.version === undefined && typeof upgrade_v1_to_v2 === 'function') {
upgrade_v1_to_v2(parsed);
}
var previewWidth = parsed.width !== undefined ? parsed.width : 160;
var previewHeight = parsed.height !== undefined ? parsed.height : 100;
var sourceFrames = parsed.canvasses || [];
if (!sourceFrames.length) return null;
var palette = (typeof palettes !== 'undefined' && palettes && palettes[parsed.palette_name]) || (typeof palettes !== 'undefined' && palettes && palettes['dawnbringer-16']) || null;
if (!palette) return null;
var pixels = decodeRleCanvas(sourceFrames[0], previewWidth * previewHeight);
var canvas = document.createElement('canvas');
canvas.width = previewWidth;
canvas.height = previewHeight;
var ctx = canvas.getContext('2d');
if (!ctx) return null;
var imageData = ctx.createImageData(previewWidth, previewHeight);
var data = imageData.data;
for (var i = 0; i < pixels.length; i++) {
var color = palette[pixels[i]] || '#000000';
var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
var off = i << 2;
data[off] = rgb ? parseInt(rgb[1], 16) : 0;
data[off + 1] = rgb ? parseInt(rgb[2], 16) : 0;
data[off + 2] = rgb ? parseInt(rgb[3], 16) : 0;
data[off + 3] = 255;
}
ctx.putImageData(imageData, 0, 0);
return { src: canvas.toDataURL('image/png'), bg: parsed.background_color, w: previewWidth, h: previewHeight };
}
function previewSrcForProject(project) {
var cacheKey = (project.id || 'project') + ':' + (project.updatedAt || 0);
if (previewCache[cacheKey]) return previewCache[cacheKey];
var entry = null;
try {
if (typeof project.state === 'string' && project.state) {
entry = renderPreviewFromState(project.state);
}
} catch (e) {}
if (!entry) {
entry = { src: project.thumb || '', bg: '#000000', w: 160, h: 100 };
}
previewCache[cacheKey] = entry;
return entry;
}
function setBusy(projectId) {
busyProjectId = projectId || null;
if (grid) renderGrid();
}
function isBusy(projectId) {
return busyProjectId != null && busyProjectId === projectId;
}
function closeShareSheet() {
if (!shareOverlay) return;
shareOverlay.classList.remove('visible');
shareOverlay.setAttribute('aria-hidden', 'true');
}
function copyToClipboard(url) {
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(url).then(function () {
showToast('Link copied to clipboard.');
});
}
} catch (e) {}
window.prompt('Copy this link:', url);
return Promise.resolve();
}
function openShareSheet(project) {
if (!shareSheet || !shareOverlay) return;
while (shareSheet.firstChild) shareSheet.removeChild(shareSheet.firstChild);
var title = el('h2', { class: 'gallery-share-title', text: 'Share "' + (project.title || 'Untitled') + '"' });
var shareLinkBtn = el('button', { type: 'button', class: 'gallery-btn', text: 'Share Link' });
var saveFileBtn = el('button', { type: 'button', class: 'gallery-btn', text: 'Save HTML to Files' });
var cancelBtn = el('button', { type: 'button', class: 'gallery-btn', text: 'Cancel' });
shareLinkBtn.addEventListener('click', function () {
setBusy(project.id);
window.FlickGalleryStore.getProject(project.id).then(function (full) {
if (!full || typeof full.state !== 'string') throw new Error('Project not found');
return new Promise(function (resolve, reject) {
FlickgameShare.shareStateAsGist(full.state, {
onUnauthorized: function () { reject(new Error('Log in with GitHub from the editor first.')); },
onError: function (msg) { reject(new Error(msg)); },
onSuccess: resolve
});
});
}).then(function (result) {
var url = FlickgameShare.resolvePlayUrl(result.playUrl);
if (navigator.share) {
return navigator.share({ url: url }).catch(function () {
return copyToClipboard(url);
});
}
return copyToClipboard(url);
}).then(function () {
closeShareSheet();
}).catch(function (err) {
showToast(err && err.message ? err.message : 'Failed to share');
}).finally(function () {
setBusy(null);
});
});
saveFileBtn.addEventListener('click', function () {
setBusy(project.id);
window.FlickGalleryStore.getProject(project.id).then(function (full) {
if (!full || typeof full.state !== 'string') throw new Error('Project not found');
var safeName = ((full.title || 'flickgame').trim() || 'flickgame').replace(/[^a-z0-9_\-]+/gi, '_');
return FlickgameShare.downloadStandaloneHtml(full.state, safeName + '.html');
}).then(function () {
closeShareSheet();
}).catch(function (err) {
showToast(err && err.message ? err.message : 'Failed to export');
}).finally(function () {
setBusy(null);
});
});
cancelBtn.addEventListener('click', closeShareSheet);
shareSheet.appendChild(title);
shareSheet.appendChild(shareLinkBtn);
shareSheet.appendChild(saveFileBtn);
shareSheet.appendChild(cancelBtn);
shareOverlay.classList.add('visible');
shareOverlay.setAttribute('aria-hidden', 'false');
}
function renderProjectCard(project) {
var appearance = previewSrcForProject(project);
var bg = appearance.bg || '#000000';
var ink = contrastingInk(bg);
var card = el('div', { class: 'gallery-card' });
card.style.setProperty('--bg', bg);
card.style.setProperty('--ink', ink);
var thumbWrap = el('div', { class: 'gallery-thumb-wrap' });
var thumbFrame = el('div', { class: 'gallery-thumb-frame' });
thumbFrame.style.aspectRatio = (appearance.w || 160) + ' / ' + (appearance.h || 100);
var thumb = el('img', { class: 'gallery-thumb', alt: 'thumbnail for ' + (project.title || 'project') });
if (appearance.src) thumb.src = appearance.src;
thumbFrame.appendChild(thumb);
thumbWrap.appendChild(thumbFrame);
var playOverlay = el('span', { class: 'gallery-play', 'aria-hidden': 'true' });
playOverlay.innerHTML = '<svg viewBox="0 0 24 24"><polygon points="6,4 20,12 6,20" fill="currentColor"/></svg>';
thumbWrap.appendChild(playOverlay);
var xBtn = el('button', { type: 'button', class: 'gallery-x', 'aria-label': 'Delete' }, ['\u00d7']);
xBtn.disabled = isBusy(project.id);
xBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
if (isBusy(project.id)) return;
if (!confirm('Delete "' + (project.title || 'Untitled') + '"? This cannot be undone.')) return;
setBusy(project.id);
window.FlickGalleryStore.deleteProject(project.id).then(function () {
closeShareSheet();
return renderGrid();
}).catch(function (err) {
showToast(err && err.message ? err.message : 'Failed to delete');
}).finally(function () {
setBusy(null);
});
});
thumbWrap.appendChild(xBtn);
thumbWrap.addEventListener('click', function () {
if (isBusy(project.id)) return;
navigate('play.html?id=' + encodeURIComponent(project.id));
});
var title = el('div', { class: 'gallery-title', text: project.title || 'Untitled' });
var time = el('div', { class: 'gallery-time', text: 'Modified: ' + formatTime(project.updatedAt || project.createdAt || 0) });
var PENCIL_SVG = '<svg viewBox="0 0 16 16"><path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325"/></svg>';
var SHARE_SVG = '<svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5"/><path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0z"/></svg>';
var editBtn = el('button', { type: 'button', class: 'gallery-ibtn', 'aria-label': 'Edit' });
editBtn.innerHTML = PENCIL_SVG;
editBtn.disabled = isBusy(project.id);
editBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
if (isBusy(project.id)) return;
navigate('index.html?id=' + encodeURIComponent(project.id));
});
var shareBtn = el('button', { type: 'button', class: 'gallery-ibtn', 'aria-label': 'Share' });
shareBtn.innerHTML = SHARE_SVG;
shareBtn.disabled = isBusy(project.id);
shareBtn.addEventListener('click', function (e) {
e.preventDefault();
e.stopPropagation();
if (isBusy(project.id)) return;
openShareSheet(project);
});
var actions = el('div', { class: 'gallery-actions' });
actions.appendChild(editBtn);
actions.appendChild(shareBtn);
card.appendChild(thumbWrap);
card.appendChild(title);
card.appendChild(time);
card.appendChild(actions);
return card;
}
function renderTile(label, innerText, onClick) {
var tile = el('div', { class: 'gallery-card gallery-tile', role: 'button', tabindex: '0' }, [
el('div', { class: 'gallery-tile-inner' }, [
el('div', { class: innerText === '+' ? 'gallery-tile-plus' : '', text: innerText }),
el('div', { text: label })
])
]);
tile.addEventListener('click', onClick);
tile.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onClick();
}
});
return tile;
}
function renderGrid() {
return window.FlickGalleryStore.listProjects().then(function (items) {
grid.innerHTML = '';
grid.appendChild(renderTile('New', '+', function () {
navigate('index.html?new=1');
}));
items.forEach(function (project) {
grid.appendChild(renderProjectCard(project));
});
return items;
}).catch(function (err) {
grid.innerHTML = '';
showToast(err && err.message ? err.message : 'Failed to load gallery');
return [];
});
}
function handleImportFile(file) {
if (!file) return;
var reader = new FileReader();
reader.onload = function (e) {
var text = e.target.result;
var state;
try {
state = FlickgameShare.extractStateFromImportText(text);
} catch (err) {
showToast(err && err.message ? err.message : 'Failed to import');
return;
}
window.FlickGalleryStore.listProjects().then(function (items) {
var baseName = (file.name || 'imported').replace(/\.[^.]+$/, '');
var title = resolveUniqueTitle(baseName, items, null);
var thumb = '';
try {
var previewEntry = renderPreviewFromState(state);
thumb = previewEntry ? previewEntry.src : '';
} catch (e) {}
return window.FlickGalleryStore.putProject({
title: title,
state: state,
thumb: thumb
});
}).then(function (saved) {
navigate('index.html?id=' + encodeURIComponent(saved.id));
}).catch(function (err) {
showToast(err && err.message ? err.message : 'Failed to import');
});
};
reader.onerror = function () {
showToast('Failed to read file');
};
reader.readAsText(file);
}
function init() {
grid = document.getElementById('gallery-grid');
toastEl = document.getElementById('gallery-toast');
importInput = document.getElementById('gallery-import-input');
importBtn = document.getElementById('gallery-import-btn');
shareOverlay = document.getElementById('gallery-share-overlay');
shareSheet = document.getElementById('gallery-share-sheet');
syncIosViewportState();
if (shareOverlay) {
shareOverlay.addEventListener('click', function (e) {
if (e.target === shareOverlay) closeShareSheet();
});
}
if (importBtn) {
importBtn.addEventListener('click', function () {
if (importInput) importInput.click();
});
}
if (importInput) {
importInput.addEventListener('change', function (e) {
var file = e.target.files && e.target.files[0];
e.target.value = '';
handleImportFile(file);
});
}
if (isIosApp()) {
window.addEventListener('resize', syncIosViewportState);
window.addEventListener('orientationchange', function () {
syncIosViewportState();
requestAnimationFrame(syncIosViewportState);
});
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', syncIosViewportState);
}
}
renderGrid();
}
window.addEventListener('load', init);
})();