-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshare.js
More file actions
527 lines (468 loc) · 14.4 KB
/
share.js
File metadata and controls
527 lines (468 loc) · 14.4 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Related Projects Component
// Include this script on project pages to suggest related projects
(function () {
'use strict';
// Only run once
if (window.relatedProjectsInitialized) return;
window.relatedProjectsInitialized = true;
// Don't show on the home page
if (window.location.pathname === '/' || window.location.pathname === '/index.html') {
return;
}
function loadAppsRegistryForShareSync() {
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', '/apps-registry.json', false);
xhr.send(null);
const ok = xhr.status === 200 || xhr.status === 304 || xhr.status === 0;
if (!ok) return [];
const data = JSON.parse(xhr.responseText);
return Array.isArray(data) ? data : [];
} catch (e) {
console.warn('[share.js] Could not load apps-registry.json', e);
return [];
}
}
const registryApps = loadAppsRegistryForShareSync();
const projectRelationships = {};
const projectMetadata = {};
for (const app of registryApps) {
projectMetadata[app.id] = {
name: app.shortName || app.name,
icon: app.icon || '📦',
description: app.description || ''
};
if (app.related && app.related.length) {
projectRelationships[app.id] = {
category: app.shareCategory || app.category || 'utility',
related: app.related
};
}
}
// Get current project from URL
function getCurrentProject() {
const path = window.location.pathname;
const segments = path.split('/').filter((s) => s);
return segments[0] || null;
}
// Create related projects widget
function createRelatedProjects() {
const currentProject = getCurrentProject();
if (!currentProject || !projectRelationships[currentProject]) {
return null;
}
const relatedIds = projectRelationships[currentProject].related;
if (!relatedIds || relatedIds.length === 0) {
return null;
}
// Take first 3 related projects
const relatedProjects = relatedIds.slice(0, 3).map((id) => ({
id,
...projectMetadata[id]
}));
const container = document.createElement('div');
container.className = 'related-projects-container';
container.id = 'related-projects-widget';
container.innerHTML = `
<button class="related-projects-toggle" aria-label="View related projects" title="More projects like this">
🎯
</button>
<div class="related-projects-panel">
<button class="related-projects-close" aria-label="Close suggestions" title="Close">
×
</button>
<div class="related-projects-header">
<h3>🎯 You might also like</h3>
</div>
<div class="related-projects-grid">
${relatedProjects
.map(
(project) => `
<a href="/${project.id}/"
class="related-project-card"
data-event="related_project_click"
data-event-category="Engagement"
data-event-label="${project.name}">
<div class="related-project-icon">${project.icon}</div>
<div class="related-project-info">
<div class="related-project-name">${project.name}</div>
<div class="related-project-description">${project.description}</div>
</div>
</a>
`
)
.join('')}
</div>
<div class="related-projects-footer">
<a href="/"
data-event="view_all_from_related"
data-event-category="Engagement"
data-event-label="${currentProject}">
View all projects →
</a>
<button class="share-btn-mini" id="share-url-btn-mini" title="Copy link to clipboard">
🔗 Share
</button>
<button class="feedback-btn-mini" id="feedback-btn-mini" title="Send Feedback">
💬 Feedback
</button>
</div>
</div>
`;
// Toggle functionality
const toggleBtn = container.querySelector('.related-projects-toggle');
const panel = container.querySelector('.related-projects-panel');
toggleBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation(); // Prevent triggering parent elements
const isOpen = panel.classList.contains('open');
if (isOpen) {
panel.classList.remove('open');
} else {
panel.classList.add('open');
// Track opening
if (window.trackEvent) {
window.trackEvent('related_projects_opened', 'Engagement', currentProject);
}
}
});
// Close button functionality
const closeBtn = container.querySelector('.related-projects-close');
closeBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
panel.classList.remove('open');
});
// Close panel when clicking outside
document.addEventListener('click', (e) => {
if (panel.classList.contains('open') && !container.contains(e.target)) {
panel.classList.remove('open');
}
});
// Close panel on Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && panel.classList.contains('open')) {
panel.classList.remove('open');
e.stopPropagation();
}
});
// Prevent clicks inside panel from closing it
panel.addEventListener('click', (e) => {
e.stopPropagation();
});
// Share button: copy current URL to clipboard
const shareBtn = container.querySelector('#share-url-btn-mini');
if (shareBtn) {
shareBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
const url = window.location.href;
navigator.clipboard
.writeText(url)
.then(() => {
const origText = shareBtn.innerHTML;
shareBtn.innerHTML = '✓ Copied!';
shareBtn.disabled = true;
setTimeout(() => {
shareBtn.innerHTML = origText;
shareBtn.disabled = false;
}, 1500);
})
.catch(() => {
shareBtn.innerHTML = 'Copy failed';
setTimeout(() => {
shareBtn.innerHTML = '🔗 Share';
}, 2000);
});
if (window.trackEvent) {
window.trackEvent('share_url_click', 'Engagement', currentProject);
}
});
}
// Feedback button functionality
const feedbackBtn = container.querySelector('#feedback-btn-mini');
if (feedbackBtn) {
feedbackBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
// Track feedback click
if (window.trackEvent) {
window.trackEvent('feedback_opened_from_related', 'Engagement', currentProject);
}
// Close the related-projects panel first so it doesn’t cover the modal
panel.classList.remove('open');
// Prefer existing feedback-button on the page; call openModal() directly
const existingFeedback = document.querySelector('feedback-button');
if (existingFeedback && typeof existingFeedback.openModal === 'function') {
existingFeedback.openModal();
return;
}
// Fallback: create an instance with trigger hidden (so modal can paint). Do not use display:none on host or the modal won’t show.
if (typeof window.FeedbackButton !== 'undefined') {
const tempFeedback = document.createElement('feedback-button');
tempFeedback.setAttribute('label', '💬 Feedback');
tempFeedback.setAttribute('theme', 'gradient');
tempFeedback.setAttribute('hide-trigger', '');
document.body.appendChild(tempFeedback);
requestAnimationFrame(() => {
if (typeof tempFeedback.openModal === 'function') {
tempFeedback.openModal();
}
});
} else {
alert('Feedback feature is loading. Please try again in a moment.');
}
});
}
return container;
}
// Add styles
const style = document.createElement('style');
style.textContent = `
.related-projects-container {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9000;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}
/* Adjust position if there's another button in bottom-right (like info button) */
body:has(.info-btn) .related-projects-container,
body:has(#info-btn) .related-projects-container {
bottom: 85px;
}
.related-projects-toggle {
width: 56px;
height: 56px;
border-radius: 50%;
border: none;
background: linear-gradient(135deg, #8b5cf6, #7c3aed);
color: white;
font-size: 24px;
cursor: pointer;
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.4);
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
position: relative;
z-index: 9001;
}
.related-projects-toggle:hover {
transform: scale(1.1);
box-shadow: 0 6px 20px rgba(139, 92, 246, 0.6);
}
.related-projects-toggle:active {
transform: scale(1.05);
}
.related-projects-panel {
position: absolute;
bottom: 70px;
right: 0;
max-width: 360px;
width: 360px;
background: linear-gradient(135deg, rgba(255, 255, 255, 0.98), rgba(249, 250, 251, 0.98));
border-radius: 16px;
padding: 20px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
backdrop-filter: blur(10px);
border: 1px solid rgba(139, 92, 246, 0.2);
opacity: 0;
transform: translateY(10px) scale(0.95);
pointer-events: none;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.related-projects-panel.open {
opacity: 1;
transform: translateY(0) scale(1);
pointer-events: auto;
}
.related-projects-close {
position: absolute;
top: 8px;
right: 8px;
width: 28px;
height: 28px;
border: none;
background: rgba(139, 92, 246, 0.1);
color: #7c3aed;
border-radius: 50%;
font-size: 20px;
line-height: 1;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
padding: 0;
font-weight: 300;
}
.related-projects-close:hover {
background: rgba(139, 92, 246, 0.2);
transform: scale(1.1);
}
.related-projects-header h3 {
margin: 0 0 16px 0;
font-size: 16px;
font-weight: 700;
color: #4c1d95;
letter-spacing: -0.2px;
}
.related-projects-grid {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 16px;
}
.related-project-card {
display: flex;
align-items: center;
gap: 12px;
padding: 12px;
background: white;
border-radius: 10px;
text-decoration: none;
color: inherit;
transition: all 0.2s ease;
border: 1px solid rgba(139, 92, 246, 0.1);
}
.related-project-card:hover {
transform: translateX(-4px);
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.2);
border-color: rgba(139, 92, 246, 0.3);
}
.related-project-icon {
font-size: 32px;
line-height: 1;
flex-shrink: 0;
}
.related-project-info {
flex: 1;
min-width: 0;
}
.related-project-name {
font-size: 14px;
font-weight: 600;
color: #1f2937;
margin-bottom: 2px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.related-project-description {
font-size: 12px;
color: #6b7280;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.related-projects-footer {
padding-top: 12px;
border-top: 1px solid rgba(139, 92, 246, 0.1);
text-align: center;
display: flex;
flex-direction: column;
gap: 8px;
}
.related-projects-footer a {
display: inline-block;
font-size: 13px;
font-weight: 600;
color: #7c3aed;
text-decoration: none;
padding: 8px 16px;
border-radius: 8px;
transition: all 0.2s ease;
}
.related-projects-footer a:hover {
background: rgba(139, 92, 246, 0.1);
transform: translateX(2px);
}
.share-btn-mini,
.feedback-btn-mini {
display: inline-block;
font-size: 13px;
font-weight: 600;
color: #8b5cf6;
background: transparent;
border: 1px solid rgba(139, 92, 246, 0.3);
padding: 8px 16px;
border-radius: 8px;
transition: all 0.2s ease;
cursor: pointer;
text-align: center;
}
.share-btn-mini:hover:not(:disabled),
.feedback-btn-mini:hover {
background: rgba(139, 92, 246, 0.1);
border-color: rgba(139, 92, 246, 0.5);
}
.share-btn-mini:disabled {
cursor: default;
opacity: 0.9;
}
/* Mobile responsive */
@media (max-width: 768px) {
.related-projects-container {
bottom: 80px;
right: 12px;
}
.related-projects-toggle {
width: 48px;
height: 48px;
font-size: 20px;
}
.related-projects-panel {
width: calc(100vw - 24px);
right: 0;
max-width: none;
padding: 16px;
}
.related-projects-header h3 {
font-size: 15px;
}
.related-project-icon {
font-size: 28px;
}
.related-project-name {
font-size: 13px;
}
.related-project-description {
font-size: 11px;
}
}
/* Print - hide widget */
@media print {
.related-projects-container {
display: none !important;
}
}
/* Hide on very small screens to avoid overlap */
@media (max-height: 600px) {
.related-projects-container {
display: none;
}
}
/* Small mobile adjustment */
@media (max-width: 640px) {
.related-projects-container {
bottom: 12px;
right: 12px;
}
}
`;
// Insert when DOM is ready
function insertWidget() {
const widget = createRelatedProjects();
if (widget) {
document.head.appendChild(style);
document.body.appendChild(widget);
}
}
// Wait for DOM to be ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', insertWidget);
} else {
insertWidget();
}
})();