-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullscreen.ts
399 lines (346 loc) · 16.4 KB
/
fullscreen.ts
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
import { GalleryItem } from './types';
import { App, TFile } from 'obsidian';
import { GlobalTagCache } from './tagCache';
export class FullscreenView {
private container: HTMLElement;
private currentItem: GalleryItem;
private items: GalleryItem[];
private currentIndex: number;
private resolveLocalPath: (src: string) => string;
private handleMouseMove: (e: MouseEvent) => void;
private handleMouseUp: () => void;
private app: App;
private tagInput: HTMLInputElement | null = null;
private tagCache: GlobalTagCache;
private suggestionsContainer: HTMLElement | null = null;
private selectedSuggestionIndex: number = -1;
constructor(app: App, items: GalleryItem[], startIndex: number, resolveLocalPath: (src: string) => string) {
this.app = app;
this.items = items.filter(item => item && !item.isVideo);
this.currentIndex = this.items.findIndex(item => item.src === items[startIndex]?.src);
if (this.currentIndex === -1) this.currentIndex = 0;
this.currentItem = this.items[this.currentIndex] || this.items[0];
this.resolveLocalPath = resolveLocalPath;
this.createContainer();
this.tagCache = GlobalTagCache.getInstance();
this.initializeTagCache();
}
private initializeTagCache() {
this.items.forEach(item => {
item.tags.forEach(tag => this.tagCache.addTag(tag));
});
}
private createContainer() {
this.container = document.createElement('div');
this.container.className = 'galleryx-fullscreen-container';
this.container.style.position = 'fixed';
this.container.style.top = '0';
this.container.style.left = '0';
this.container.style.width = '100%';
this.container.style.height = '100%';
this.container.style.backgroundColor = 'rgba(0, 0, 0, 0.9)';
this.container.style.zIndex = '9999';
this.container.innerHTML = `
<div class="galleryx-fullscreen-content" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;"></div>
<div class="galleryx-fullscreen-metadata" style="position: absolute; bottom: 10px; left: 10px; right: 10px; color: white; width: 100%;"></div>
<button class="galleryx-fullscreen-close" style="position: absolute; top: 10px; right: 10px; background: none; border: none; color: white; font-size: 24px; cursor: pointer;">×</button>
${this.items.length > 1 ? `
<button class="galleryx-fullscreen-prev" style="position: absolute; top: 50%; left: 10px; background: none; border: none; color: white; font-size: 24px; cursor: pointer;">‹</button>
<button class="galleryx-fullscreen-next" style="position: absolute; top: 50%; right: 10px; background: none; border: none; color: white; font-size: 24px; cursor: pointer;">›</button>
` : ''}
`;
this.container.querySelector('.galleryx-fullscreen-close')?.addEventListener('click', () => this.close());
if (this.items.length > 1) {
this.container.querySelector('.galleryx-fullscreen-prev')?.addEventListener('click', () => this.navigate(-1));
this.container.querySelector('.galleryx-fullscreen-next')?.addEventListener('click', () => this.navigate(1));
}
this.container.addEventListener('click', (e) => {
if (e.target === this.container) {
this.close();
}
});
document.addEventListener('keydown', this.handleKeyDown.bind(this));
document.body.appendChild(this.container);
this.updateContent();
}
private updateContent() {
const contentEl = this.container.querySelector('.galleryx-fullscreen-content');
const metadataEl = this.container.querySelector('.galleryx-fullscreen-metadata');
if (!contentEl || !metadataEl) return;
contentEl.innerHTML = '';
const img = document.createElement('img');
img.className = 'galleryx-fullscreen-image';
if (this.currentItem.isLocal) {
img.src = this.resolveLocalPath(this.currentItem.src);
} else {
img.src = this.currentItem.src;
}
img.onerror = () => this.handleMediaError(contentEl, 'Image failed to load');
// Set initial styles
img.style.maxWidth = '100%';
img.style.maxHeight = '100%';
img.style.objectFit = 'contain';
img.style.transition = 'transform 0.1s ease-out';
contentEl.appendChild(img);
metadataEl.innerHTML = `
<div style="display: flex; width: 100%; max-width: 100%; background-color: rgba(0, 0, 0, 0.7); padding: 10px; border-radius: 5px;">
<div style="flex: 0 0 auto; margin-right: 20px;">
<p style="margin: 0 0 5px 0;">Source:</p>
<p style="margin: 0 0 10px 0; word-break: break-all;">${this.currentItem.src}</p>
<button class="galleryx-copy-metadata" style="background: none; border: 1px solid white; color: white; padding: 5px 10px; cursor: pointer;">Copy Source</button>
</div>
<div style="flex: 1; overflow: hidden;">
<p style="margin: 0 0 5px 0;">Tags:</p>
<input type="text" class="galleryx-tags-input" value="${this.currentItem.tags.join(', ')}" style="width: 100%; background: rgba(255,255,255,0.1); color: white; border: none; padding: 5px;">
<button class="galleryx-update-tags" style="background: none; border: 1px solid white; color: white; padding: 5px 10px; cursor: pointer; margin-top: 5px;">Update Tags</button>
</div>
</div>
`;
const copyButton = metadataEl.querySelector('.galleryx-copy-metadata');
copyButton?.addEventListener('click', () => this.copyMetadataToClipboard());
const updateTagsButton = metadataEl.querySelector('.galleryx-update-tags');
const tagsInput = metadataEl.querySelector('.galleryx-tags-input') as HTMLInputElement;
this.tagInput = tagsInput; // Store the reference to the tag input
// Clear and hide suggestions when updating content
this.clearSuggestions();
if (this.suggestionsContainer) {
this.suggestionsContainer.remove();
}
this.suggestionsContainer = document.createElement('div');
this.suggestionsContainer.className = 'galleryx-tag-suggestions';
document.body.appendChild(this.suggestionsContainer);
tagsInput.addEventListener('input', () => this.showSuggestions());
tagsInput.addEventListener('keydown', (e) => this.handleTagInputKeydown(e));
updateTagsButton?.addEventListener('click', () => {
const newTags = tagsInput.value.split(',').map(tag => tag.trim()).filter(tag => tag);
this.updateTags(newTags);
});
// Add zoom functionality
let scale = 1;
contentEl.addEventListener('wheel', (e: WheelEvent) => {
e.preventDefault();
const delta = e.deltaY > 0 ? 0.9 : 1.1;
scale *= delta;
scale = Math.min(Math.max(0.5, scale), 5); // Limit zoom between 0.5x and 5x
updateTransform();
});
// Add drag functionality
let isDragging = false;
let startX: number, startY: number;
let translateX = 0, translateY = 0;
img.addEventListener('mousedown', (e: MouseEvent) => {
if (e.button === 2) { // Right mouse button
e.preventDefault();
isDragging = true;
startX = e.clientX - translateX;
startY = e.clientY - translateY;
}
});
this.handleMouseMove = (e: MouseEvent) => {
if (isDragging) {
translateX = e.clientX - startX;
translateY = e.clientY - startY;
updateTransform();
}
};
this.handleMouseUp = () => {
isDragging = false;
};
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
// Helper function to update transform
const updateTransform = () => {
img.style.transform = `scale(${scale}) translate(${translateX}px, ${translateY}px)`;
};
}
private handleMediaError(contentEl: Element, message: string) {
contentEl.innerHTML = `<div class="galleryx-error-message">${message}</div>`;
}
private copyMetadataToClipboard() {
const metadata = `${this.currentItem.src}`;
navigator.clipboard.writeText(metadata).then(() => {
// Provide visual feedback
const copyButton = this.container.querySelector('.galleryx-copy-metadata');
if (copyButton) {
const originalText = copyButton.textContent;
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = originalText;
}, 2000);
}
}).catch(err => {
console.error('Failed to copy metadata: ', err);
});
}
private showSuggestions() {
if (!this.tagInput || !this.suggestionsContainer) return;
const inputValue = this.tagInput.value.toLowerCase();
const lastTag = inputValue.split(',').pop()?.trim() || '';
if (lastTag.length === 0) {
this.suggestionsContainer.style.display = 'none';
return;
}
const suggestions = this.tagCache.getAllTags()
.filter(tag => tag.toLowerCase().includes(lastTag))
.sort((a, b) => a.localeCompare(b));
this.suggestionsContainer.innerHTML = '';
suggestions.forEach((suggestion, index) => {
const suggestionEl = document.createElement('div');
suggestionEl.textContent = suggestion;
suggestionEl.className = 'galleryx-tag-suggestion';
suggestionEl.style.padding = '5px';
suggestionEl.style.cursor = 'pointer';
suggestionEl.addEventListener('click', () => this.selectSuggestion(suggestion));
suggestionEl.addEventListener('mouseover', () => this.setSelectedSuggestion(index));
this.suggestionsContainer?.appendChild(suggestionEl);
});
if (this.suggestionsContainer) {
// Position the suggestions container above the input
const inputRect = this.tagInput.getBoundingClientRect();
this.suggestionsContainer.style.position = 'absolute';
this.suggestionsContainer.style.left = `${inputRect.left}px`;
this.suggestionsContainer.style.bottom = `${window.innerHeight - inputRect.top}px`; // Position above the input
this.suggestionsContainer.style.width = `${inputRect.width}px`;
this.suggestionsContainer.style.maxHeight = '150px';
this.suggestionsContainer.style.overflowY = 'auto';
this.suggestionsContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
this.suggestionsContainer.style.color = 'white';
this.suggestionsContainer.style.zIndex = '10000';
this.suggestionsContainer.style.display = suggestions.length > 0 ? 'block' : 'none';
}
this.selectedSuggestionIndex = -1;
}
private selectSuggestion(suggestion: string) {
if (!this.tagInput) return;
const tags = this.tagInput.value.split(',');
tags[tags.length - 1] = suggestion;
this.tagInput.value = tags.join(', ') + ', ';
this.suggestionsContainer!.style.display = 'none';
this.tagInput.focus();
}
private setSelectedSuggestion(index: number) {
this.selectedSuggestionIndex = index;
const suggestions = this.suggestionsContainer!.children;
for (let i = 0; i < suggestions.length; i++) {
suggestions[i].classList.toggle('selected', i === index);
}
}
private clearSuggestions() {
if (this.suggestionsContainer) {
this.suggestionsContainer.innerHTML = '';
this.suggestionsContainer.style.display = 'none';
}
this.selectedSuggestionIndex = -1;
}
private handleTagInputKeydown(event: KeyboardEvent) {
if (!this.suggestionsContainer) return;
const suggestions = this.suggestionsContainer.children;
switch (event.key) {
case 'ArrowDown':
event.preventDefault();
this.setSelectedSuggestion((this.selectedSuggestionIndex + 1) % suggestions.length);
break;
case 'ArrowUp':
event.preventDefault();
this.setSelectedSuggestion((this.selectedSuggestionIndex - 1 + suggestions.length) % suggestions.length);
break;
case 'Enter':
if (this.selectedSuggestionIndex >= 0) {
event.preventDefault();
this.selectSuggestion(suggestions[this.selectedSuggestionIndex].textContent!);
}
break;
case 'Escape':
this.suggestionsContainer.style.display = 'none';
break;
}
}
private async updateTags(newTags: string[]) {
const oldTags = new Set(this.currentItem.tags);
this.currentItem.tags = newTags;
// Update tag cache
newTags.forEach(tag => this.tagCache.addTag(tag));
oldTags.forEach(tag => {
if (!this.items.some(item => item.tags.includes(tag))) {
this.tagCache.removeTag(tag);
}
});
// Find the file containing the image
const files = this.app.vault.getMarkdownFiles();
for (const file of files) {
const content = await this.app.vault.read(file);
const lines = content.split('\n');
let updated = false;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(this.currentItem.src)) {
// Found the line with the image
const tagRegex = /\{.*?\}/;
if (tagRegex.test(lines[i])) {
// Replace existing tags
lines[i] = lines[i].replace(tagRegex, `{${newTags.join(', ')}}`);
} else {
// Add new tags
lines[i] += ` {${newTags.join(', ')}}`;
}
updated = true;
break;
}
}
if (updated) {
await this.app.vault.modify(file, lines.join('\n'));
break;
}
}
// Update the UI
this.updateContent();
}
private navigate(direction: number) {
if (this.items.length > 1) {
this.currentIndex = (this.currentIndex + direction + this.items.length) % this.items.length;
this.currentItem = this.items[this.currentIndex];
this.clearSuggestions();
this.updateContent();
}
}
private handleKeyDown(event: KeyboardEvent) {
// Check if the tag input is focused
if (this.tagInput && document.activeElement === this.tagInput) {
if (event.key === 'Escape') {
this.tagInput.blur(); // Remove focus from the input
event.preventDefault();
} else if (event.key === 'Enter') {
event.preventDefault();
const newTags = this.tagInput.value.split(',').map(tag => tag.trim()).filter(tag => tag);
this.updateTags(newTags);
}
return;
}
// If the tag input is not focused, handle navigation as before
switch (event.key) {
case 'ArrowLeft':
this.navigate(-1);
break;
case 'ArrowRight':
this.navigate(1);
break;
case 'Escape':
this.close();
break;
}
}
private close() {
document.removeEventListener('keydown', this.handleKeyDown);
if (this.handleMouseMove) {
document.removeEventListener('mousemove', this.handleMouseMove);
}
if (this.handleMouseUp) {
document.removeEventListener('mouseup', this.handleMouseUp);
}
this.clearSuggestions();
if (this.suggestionsContainer) {
this.suggestionsContainer.remove();
this.suggestionsContainer = null;
}
this.container.remove();
}
}