-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
315 lines (267 loc) · 10.8 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () => {
// Get DOM elements
const imageUpload = document.getElementById('imageUpload');
const carouselSection = document.getElementById('carouselSection');
const exportFilenameListBtn = document.getElementById('exportFilenameList');
const exportMoodboardBtn = document.getElementById('exportMoodboard');
const exportMetadataBtn = document.getElementById('exportMetadata');
const uploadLabel = document.querySelector('.upload-label');
// Setup event listeners only if elements exist
if (imageUpload) {
imageUpload.addEventListener('change', handleImageUpload);
}
document.addEventListener('dragover', handleDragOver);
document.addEventListener('drop', handleDrop);
if (exportFilenameListBtn) {
exportFilenameListBtn.addEventListener('click', exportFilenameList);
}
if (exportMoodboardBtn) {
exportMoodboardBtn.addEventListener('click', exportMoodboard);
}
if (exportMetadataBtn) {
exportMetadataBtn.addEventListener('click', exportMetadata);
}
// Add drag and drop visual feedback if upload label exists
if (uploadLabel) {
uploadLabel.addEventListener('dragenter', () => uploadLabel.style.borderColor = '#666');
uploadLabel.addEventListener('dragleave', () => uploadLabel.style.borderColor = '#ccc');
}
// Dark mode detection and handling
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-theme', 'dark');
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
document.documentElement.setAttribute('data-theme', e.matches ? 'dark' : 'light');
});
function handleImageUpload(event) {
const files = event.target.files;
addImages(files);
}
function handleDragOver(event) {
event.preventDefault();
}
function handleDrop(event) {
event.preventDefault();
const files = event.dataTransfer.files;
addImages(files);
uploadLabel.style.borderColor = '#ccc';
}
function updateImageCounters() {
const containers = document.querySelectorAll('.img-container');
containers.forEach((container, index) => {
container.dataset.index = (index + 1).toString();
});
}
function addImages(files) {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const reader = new FileReader();
reader.onload = function(e) {
const imgContainer = document.createElement('div');
imgContainer.classList.add('img-container');
const img = document.createElement('img');
img.src = e.target.result;
img.classList.add('carousel-image');
imgContainer.appendChild(img);
const removeIcon = document.createElement('button');
removeIcon.classList.add('remove-icon');
removeIcon.addEventListener('click', () => {
imgContainer.remove();
updateImageCounters(); // Update counters after removal
});
imgContainer.appendChild(removeIcon);
imgContainer.dataset.filename = file.name;
carouselSection.appendChild(imgContainer);
updateImageCounters(); // Update counters after adding
};
reader.readAsDataURL(file);
}
}
new Sortable(carouselSection, {
animation: 150,
ghostClass: 'sortable-ghost',
onEnd: () => {
updateImageCounters(); // Update counters after reordering
console.log('Images reordered');
}
});
function exportFilenameList() {
const imgContainers = document.querySelectorAll('.img-container');
const filenames = Array.from(imgContainers).map(container => container.dataset.filename);
const blob = new Blob([filenames.join('\n')], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'carousel_filenames.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
async function exportMoodboard() {
const imgContainers = document.querySelectorAll('.img-container');
if (imgContainers.length === 0) {
alert('Please add some images first!');
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'mm', 'a4');
const pageWidth = doc.internal.pageSize.getWidth();
const pageHeight = doc.internal.pageSize.getHeight();
// Page margins
const margin = 20;
const availableWidth = pageWidth - (2 * margin);
const availableHeight = pageHeight - (2 * margin);
// Grid configuration
const columns = 2;
const rows = 3;
const spacing = 10;
// Calculate image dimensions
const imageWidth = (availableWidth - (spacing * (columns - 1))) / columns;
const imageHeight = (imageWidth * 5) / 4; // 4:5 aspect ratio
// Get additional content
const caption = document.getElementById('scratchCopy').value;
const tags = document.getElementById('tags').value;
let currentPage = 1;
let x = margin;
let y = margin;
// Add caption and tags to first page if present
if (caption || tags) {
if (caption) {
doc.setFontSize(12);
doc.text(caption, margin, y);
y += 10;
}
if (tags) {
doc.setFontSize(10);
doc.text(tags, margin, y);
y += 10;
}
y += 10; // Extra spacing after text
}
for (let i = 0; i < imgContainers.length; i++) {
const img = imgContainers[i].querySelector('img');
// Create a temporary canvas to handle image resizing
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Calculate proper dimensions while maintaining aspect ratio
let finalWidth = imageWidth;
let finalHeight = imageHeight;
const imgAspect = img.naturalWidth / img.naturalHeight;
const targetAspect = 4/5; // Target aspect ratio
if (imgAspect > targetAspect) {
// Wider image
finalHeight = finalWidth / imgAspect;
} else {
// Taller image
finalWidth = finalHeight * imgAspect;
}
// Center the image in its cell
const xOffset = (imageWidth - finalWidth) / 2;
const yOffset = (imageHeight - finalHeight) / 2;
try {
// Add the image
doc.addImage(
img.src,
'JPEG',
x + xOffset,
y + yOffset,
finalWidth,
finalHeight
);
// Add the number
doc.setFontSize(10);
doc.setTextColor(0, 0, 0); // Black text
// Draw number background
const numberText = (i + 1).toString();
const textWidth = doc.getTextWidth(numberText);
const padding = 3;
doc.setFillColor(255, 255, 255); // White background
doc.rect(
x + xOffset,
y + yOffset + finalHeight + 2, // 2mm gap below image
textWidth + (padding * 2),
6,
'F'
);
// Draw number text
doc.text(
numberText,
x + xOffset + padding,
y + yOffset + finalHeight + 6 // Position text within background
);
// Move to next position
if ((i + 1) % columns === 0) {
x = margin;
y += imageHeight + spacing;
// Check if we need a new page
if (y + imageHeight > pageHeight - margin) {
if (i < imgContainers.length - 1) {
doc.addPage();
currentPage++;
y = margin;
}
}
} else {
x += imageWidth + spacing;
}
} catch (error) {
console.error('Error adding image to PDF:', error);
}
}
doc.save('moodboard.pdf');
}
async function exportMetadata() {
const imgContainers = document.querySelectorAll('.img-container');
if (imgContainers.length === 0) {
alert('Please add some images first!');
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'mm', 'a4');
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 20;
let y = margin;
// Add title
doc.setFontSize(16);
doc.text('Image Metadata', margin, y);
y += 15;
// Add caption and tags if present
const caption = document.getElementById('scratchCopy').value;
const tags = document.getElementById('tags').value;
if (caption) {
doc.setFontSize(12);
doc.text('Caption:', margin, y);
y += 7;
doc.setFontSize(10);
const captionLines = doc.splitTextToSize(caption, pageWidth - (2 * margin));
doc.text(captionLines, margin, y);
y += (captionLines.length * 5) + 10;
}
if (tags) {
doc.setFontSize(12);
doc.text('Tags:', margin, y);
y += 7;
doc.setFontSize(10);
const tagLines = doc.splitTextToSize(tags, pageWidth - (2 * margin));
doc.text(tagLines, margin, y);
y += (tagLines.length * 5) + 10;
}
// Add image list
doc.setFontSize(12);
doc.text('Images:', margin, y);
y += 10;
doc.setFontSize(10);
imgContainers.forEach((container, index) => {
const filename = container.dataset.filename;
doc.text(`${index + 1}. ${filename}`, margin, y);
y += 7;
// Add new page if needed
if (y > pageHeight - margin) {
doc.addPage();
y = margin;
}
});
doc.save('carousel_metadata.pdf');
}
});