-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
445 lines (390 loc) · 13 KB
/
Copy pathmain.js
File metadata and controls
445 lines (390 loc) · 13 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
//@ts-check
//@ts-ignore
import { Sortable } from 'sortablejs'
//@ts-ignore
import { fileSave, fileOpen } from "browserFsAccess"
class ImageProcessor {
constructor(maxWidth = 500) {
this.maxWidth = maxWidth;
}
async render(blob) {
return new Promise((resolve, reject) => {
const image = new Image();
const url = URL.createObjectURL(blob);
image.onload = () => {
URL.revokeObjectURL(url);
const canvas = new OffscreenCanvas(this.maxWidth, this.maxWidth);
const context = canvas.getContext('2d');
// Calculate the aspect ratio and fit the image
const { width, height, ar } = this.calculateAspectRatioFit(image.width, image.height);
canvas.width = width;
canvas.height = height;
// Draw the image onto the canvas
context?.drawImage(image, 0, 0, width, height);
// Return the compressed image as a blob
canvas.convertToBlob({ type: 'image/webp', quality: 0.8 })
.then((compressedBlob) => {
resolve({ out: compressedBlob, ar });
})
.catch(reject);
};
image.onerror = (err) => {
URL.revokeObjectURL(url);
reject(err);
};
image.src = url;
});
}
calculateAspectRatioFit(srcWidth, srcHeight) {
const ar = srcWidth / srcHeight;
const ratio = Math.min(this.maxWidth / srcWidth, this.maxWidth / srcHeight);
const width = Math.round(srcWidth * ratio);
const height = Math.round(srcHeight * ratio);
return {
width,
height,
ar: this.formatAspectRatio(ar),
};
}
formatAspectRatio(ar) {
// Format the aspect ratio as 'width:height'
const gcd = this.greatestCommonDivisor(ar * 1000, 1000); // Approximate to avoid floating point issues
return `${Math.round(ar * 1000 / gcd)}:${Math.round(1000 / gcd)}`;
}
greatestCommonDivisor(a, b) {
while (b) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
}
const reduce = new ImageProcessor(500);
let selectionMode = false
let selection = []
let board = []
let currentBoard = {
title: "untitled",
path: "",
saved: false,
lastSave: []
}
function deleteSelection() {
selection.forEach((s) => {
const el = document.getElementById(s);
el?.remove()
})
}
function shuffle(c) {
board.sort(() => Math.random() - 0.5);
c.innerHTML = ""
board.forEach((e) => {
const gridItem = document.createElement('li');
gridItem.setAttribute('data-selected', 'false')
gridItem.id = e.id
gridItem.addEventListener('click', (e) => {
if (!selectionMode) { return }
const isSelected = e.target?.getAttribute('data-selected') === 'true'
console.log(isSelected)
if (isSelected) {
e.target?.setAttribute('data-selected', false)
} else {
e.target?.setAttribute('data-selected', true)
selection.push(e.target?.id)
}
})
const img = document.createElement('img');
img.src = e.data;
img.setAttribute('data-porta', e.isPortrait)
img.addEventListener('dragstart', (e) => e.preventDefault());
gridItem.appendChild(img);
c?.appendChild(gridItem);
});
updateBoard();
updateBoardMeta(currentBoard.title, false)
}
async function saveBoard() {
console.log("board", board.length)
const str = JSON.stringify(board);
const blob = new Blob([str], { type: 'application/octet-stream' });
const now = new Date().toISOString().slice(0, 10);
console.log(board)
if (blob) {
await fileSave(blob, {
fileName: currentBoard.title === "untitled" ? "untitled" + "-" + now + ".board" : currentBoard.title + ".board",
extensions: [".board"]
})
updateBoardMeta(currentBoard.title, true)
currentBoard.lastSave = board
}
}
/**
* @param {*} title
* @param {*} saved
*/
function updateBoardMeta(title, saved) {
const n = document.getElementById("board-title");
// @ts-ignore
n.value = title
// @ts-ignore
if (safari) { n.style.width = (title.length * 0.38) + "rem" }
currentBoard.title = title
const s = document.querySelector('[data-saved]');
s?.setAttribute('data-saved', saved);
currentBoard.saved = saved
}
function updateBoard() {
board.length = 0;
const gridItems = document.querySelectorAll('#grid li');
gridItems.forEach(el => {
const id = el.id;
const img = el.querySelector('img');
board.push({
id: id,
isPortrait: img?.dataset.porta === "true",
data: img?.src
});
updateBoardMeta(currentBoard.title, false);
});
}
/**
* @param {HTMLElement | null} c
*/
async function loadImages(c) {
try {
const images = await fileOpen({
mimeTypes: ['image/*'],
description: "Insert images",
multiple: true,
id: 'images',
excludeAcceptAllOption: true,
})
images.forEach(async (i) => {
const blob = new Blob([await i.arrayBuffer()], { type: i.type });
reduce.render(blob)
.then(({ out, ar }) => {
const reader = new FileReader();
const isPortrait = ar.includes(':')
&& parseInt(ar.split(':')[0]) < parseInt(ar.split(':')[1]);
console.log('is portrait:', isPortrait);
const id = genID()
reader.onload = function (event) {
const gridItem = document.createElement('li');
gridItem.setAttribute('data-selected', 'false')
gridItem.id = id
gridItem.addEventListener('click', (e) => {
if (!selectionMode) { return }
const isSelected = e.target?.getAttribute('data-selected') === 'true'
console.log(isSelected)
if (isSelected) {
e.target?.setAttribute('data-selected', false)
} else {
e.target?.setAttribute('data-selected', true)
selection.push(e.target?.id)
}
})
const img = document.createElement('img');
img.src = event.target?.result?.toString() || "";
img.setAttribute('data-porta', isPortrait)
img.addEventListener('dragstart', (e) => e.preventDefault());
gridItem.appendChild(img);
c?.insertBefore(gridItem, c.firstChild);
};
updateBoard();
reader.readAsDataURL(out);
})
.catch((err) => {
console.error('Error during image compression:', err);
});
});
} catch (err) {
console.error('Error opening the board:', err);
}
}
/**
* @param {HTMLElement | null} c
*/
async function openBoard(c) {
try {
const file = await fileOpen({
extensions: [".board"],
description: "Boards",
id: 'boards',
})
const buf = await file.arrayBuffer();
const str = new TextDecoder().decode(buf)
const data = JSON.parse(str);
if (c) {
//@ts-ignore
c.innerHTML = ""
}
data.forEach((e) => {
const gridItem = document.createElement('li');
gridItem.setAttribute('data-selected', 'false')
gridItem.id = e.id
gridItem.addEventListener('click', (e) => {
if (!selectionMode) { return }
const isSelected = e.target?.getAttribute('data-selected') === 'true'
console.log(isSelected)
if (isSelected) {
e.target?.setAttribute('data-selected', false)
} else {
e.target?.setAttribute('data-selected', true)
selection.push(e.target?.id)
}
})
const img = document.createElement('img');
img.src = e.data;
img.setAttribute('data-porta', e.isPortrait)
img.addEventListener('dragstart', (e) => e.preventDefault());
gridItem.appendChild(img);
c?.appendChild(gridItem);
});
updateBoard();
updateBoardMeta(file.name.split('.')[0], true)
currentBoard.lastSave = board
} catch (err) {
console.error('Error opening the board:', err);
}
}
let dark;
function toggleLight() {
dark = !dark
document?.body.classList.toggle('dark')
window.localStorage.setItem('theme-dark', String(dark))
}
async function isSafari() {
if (navigator.userAgentData) {
const brands = await navigator.userAgentData.getHighEntropyValues(['brands']);
return brands.brands.some(brand => brand.brand === "Safari" && !brands.brands.some(b => b.brand === "Chrome"));
} else {
// Fallback for browsers without `userAgentData`
const ua = navigator.userAgent;
return /Safari/.test(ua) && !/Chrome/.test(ua) && !/CriOS/.test(ua);
}
}
let safari;
document.addEventListener('DOMContentLoaded', async () => {
const gridContainer = document.getElementById('grid');
dark = window.localStorage.getItem('theme-dark')
console.log(dark)
if (!dark) {
window.localStorage.setItem('theme-dark', "false")
}
dark = dark === "true"
if (!dark) { document.body.classList.remove('dark') }
safari = await isSafari()
if (safari) { updateBoardMeta(currentBoard.title, true) }
const bt = document.getElementById("board-title");
bt?.addEventListener('input', (e) => {
// @ts-ignore
const value = e.target.value
currentBoard.title = value
if (safari) { bt.style.width = (value.length * 0.38) + "rem" }
})
document.addEventListener('keydown', (event) => {
if ((event.metaKey || event.ctrlKey) && event.altKey && event.code === 'KeyS') {
event.preventDefault()
selectionMode = !selectionMode
gridContainer?.setAttribute('data-mode-selection', selectionMode.toString())
if (!selectionMode && selection.length > 0) {
selection.forEach((s) => {
document.getElementById(s)?.setAttribute('data-selected', 'false')
})
selection.length = 0
}
} else if ((event.metaKey || event.ctrlKey) && event.code === 'KeyK') {
event.preventDefault()
if (!selectionMode || selection.length === 0) { return }
deleteSelection()
selectionMode = !selectionMode
gridContainer?.setAttribute('data-mode-selection', selectionMode.toString())
updateBoard();
} else if (event.ctrlKey && event.code === 'KeyS') {
event.preventDefault()
saveBoard();
} else if (event.ctrlKey && event.code == "KeyO") {
event.preventDefault()
openBoard(gridContainer)
} else if (event.ctrlKey && event.code == "KeyL") {
event.preventDefault()
toggleLight();
}
if (event.ctrlKey && event.code === "KeyN") {
event.preventDefault()
shuffle(gridContainer);
}
});
new Sortable(gridContainer, {
filter: '.selection',
animation: 150,
ghostClass: 'dragging',
onEnd: () => {
updateBoard()
},
});
function checkClip(event) {
const items = (event.clipboardData || event.originalEvent.clipboardData)?.items;
if (!items) return;
for (const item of items) {
if (item.kind === 'file' && item.type.startsWith('image/')) {
console.log('Image detected on clipboard');
return true;
}
}
console.log('No images found on clipboard');
return false;
}
document.addEventListener('paste', (event) => {
//@ts-ignore
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
if (!checkClip(event)) { loadImages(gridContainer); }
for (let index in items) {
const item = items[index];
if (item.kind === 'file' && item.type.startsWith('image/')) {
const blob = item.getAsFile();
reduce.render(blob)
.then(({ out, ar }) => {
const reader = new FileReader();
const isPortrait = ar.includes(':')
&& parseInt(ar.split(':')[0]) < parseInt(ar.split(':')[1]);
console.log('is portrait:', isPortrait);
const id = genID()
reader.onload = function (event) {
const gridItem = document.createElement('li');
gridItem.setAttribute('data-selected', 'false')
gridItem.id = id
gridItem.addEventListener('click', (e) => {
if (!selectionMode) { return }
const isSelected = e.target?.getAttribute('data-selected') === 'true'
console.log(isSelected)
if (isSelected) {
e.target?.setAttribute('data-selected', false)
} else {
e.target?.setAttribute('data-selected', true)
selection.push(e.target?.id)
}
})
const img = document.createElement('img');
img.src = event.target?.result?.toString() || "";
img.setAttribute('data-porta', isPortrait)
img.addEventListener('dragstart', (e) => e.preventDefault());
gridItem.appendChild(img);
gridContainer?.insertBefore(gridItem, gridContainer.firstChild);
};
updateBoard();
reader.readAsDataURL(out);
})
.catch((err) => {
console.error('Error during image compression:', err);
});
}
}
});
});
function genID() {
const timestamp = Date.now().toString(36); // Convert current timestamp to base-36
const randomValue = Math.random().toString(36).slice(2, 7); // Generate a random base-36 string of length 5
return timestamp + randomValue; // Concatenate timestamp and random part
}