-
Notifications
You must be signed in to change notification settings - Fork 0
/
swiper.html
255 lines (229 loc) · 8.51 KB
/
swiper.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Sefaria Topic Swiper</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
overflow-x: hidden;
}
#app {
width: 100%;
max-width: 400px;
height: 100vh;
background-color: white;
overflow: hidden;
position: relative;
touch-action: pan-y;
}
.topic {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 20px;
box-sizing: border-box;
transition: transform 0.3s ease-out;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
.topic.current {
transform: translateX(0);
}
.topic.next {
transform: translateX(-100%);
}
.topic.previous {
transform: translateX(100%);
}
.topic.hidden {
display: none;
}
h1 {
font-size: 24px;
margin-top: 0;
text-align: center;
}
img {
max-width: 100%;
max-height: 50vh;
display: block;
margin: 0 auto;
pointer-events: none; /* Prevent image from interfering with swipes */
}
p {
font-size: 16px;
line-height: 1.5;
}
#loading {
text-align: center;
padding: 20px;
}
#instructions {
position: absolute;
bottom: 10px;
left: 0;
right: 0;
text-align: center;
font-size: 14px;
color: #666;
pointer-events: none; /* Prevent instructions from interfering with swipes */
}
</style>
</head>
<body>
<div id="app">
<div id="topic1" class="topic current"></div>
<div id="topic2" class="topic next"></div>
<div id="instructions">
Swipe or arrow left for new topic. Right to learn more about this one.
</div>
</div>
<script>
const app = document.getElementById('app');
const topicElements = [
document.getElementById('topic1'),
document.getElementById('topic2')
];
let currentIndex = 0;
let topics = [null, null];
let startX = 0;
let startY = 0;
let distX = 0;
let distY = 0;
let startTime = 0;
let elapsedTime = 0;
async function fetchRandomTopic() {
try {
const response = await fetch('https://www.sefaria.org/api/texts/random-by-topic');
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching topic:', error);
return null;
}
}
function isValidTopic(topic) {
return topic.topic.image && topic.topic.image.image_uri;
}
async function getValidTopic() {
let topic;
do {
topic = await fetchRandomTopic();
} while (topic && !isValidTopic(topic));
return topic;
}
function parseMarkdown(markdown) {
let html = markdown;
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>');
return html;
}
function renderTopic(topic, element) {
if (!topic) {
element.innerHTML = '<p>Error loading topic. Please try again.</p>';
return;
}
let html = `<h1>${topic.topic.primaryTitle.en}</h1>`;
html += `<img src="${topic.topic.image.image_uri}" alt="${topic.topic.primaryTitle.en}">`;
if (topic.topic.description && topic.topic.description.en) {
const parsedDescription = parseMarkdown(topic.topic.description.en);
html += `<p>${parsedDescription}</p>`;
}
element.innerHTML = html;
}
async function preloadNextTopic() {
const nextIndex = (currentIndex + 1) % 2;
topics[nextIndex] = await getValidTopic();
renderTopic(topics[nextIndex], topicElements[nextIndex]);
}
async function swapTopics() {
if (topicElements[0].classList.contains('transitioning') ||
topicElements[1].classList.contains('transitioning')) {
return; // Prevent multiple transitions
}
const oldIndex = currentIndex;
const newIndex = (currentIndex + 1) % 2;
topicElements[oldIndex].classList.add('transitioning');
topicElements[newIndex].classList.add('transitioning');
topicElements[oldIndex].style.transform = 'translateX(100%)';
topicElements[newIndex].style.transform = 'translateX(0)';
topicElements[oldIndex].classList.remove('current');
topicElements[oldIndex].classList.add('previous');
topicElements[newIndex].classList.remove('next');
topicElements[newIndex].classList.add('current');
await new Promise(resolve => setTimeout(resolve, 300));
topicElements[oldIndex].classList.add('hidden');
topicElements[oldIndex].style.transition = 'none';
topicElements[oldIndex].style.transform = 'translateX(-100%)';
void topicElements[oldIndex].offsetWidth;
topicElements[oldIndex].style.transition = '';
topicElements[oldIndex].classList.remove('hidden', 'previous', 'transitioning');
topicElements[oldIndex].classList.add('next');
topicElements[newIndex].classList.remove('transitioning');
currentIndex = newIndex;
await preloadNextTopic();
}
function navigateToSefaria() {
const currentTopic = topics[currentIndex];
if (currentTopic && currentTopic.topic.slug) {
window.location.href = `https://www.sefaria.org/topics/${currentTopic.topic.slug}`;
}
}
app.addEventListener('touchstart', (e) => {
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
startTime = new Date().getTime();
e.preventDefault(); // Prevent default to ensure swipe works everywhere
}, { passive: false });
app.addEventListener('touchmove', (e) => {
distX = e.touches[0].clientX - startX;
distY = e.touches[0].clientY - startY;
if (Math.abs(distX) > Math.abs(distY)) {
e.preventDefault(); // Prevent default only for horizontal swipes
}
}, { passive: false });
app.addEventListener('touchend', (e) => {
elapsedTime = new Date().getTime() - startTime;
if (Math.abs(distX) > 50 && Math.abs(distY) < 100 && elapsedTime < 300) { // Minimum swipe distance and maximum swipe time
if (distX > 0) {
swapTopics(); // Right swipe (LTR) loads new topic
} else {
navigateToSefaria(); // Left swipe (RTL) navigates to Sefaria
}
}
startX = 0;
startY = 0;
distX = 0;
distY = 0;
});
// Keyboard support
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
swapTopics(); // Left arrow now loads new topic
} else if (event.key === 'ArrowRight') {
navigateToSefaria(); // Right arrow now navigates to Sefaria
}
});
// Initial load
(async function() {
topics[currentIndex] = await getValidTopic();
renderTopic(topics[currentIndex], topicElements[currentIndex]);
await preloadNextTopic();
topicElements[currentIndex].classList.add('current');
topicElements[(currentIndex + 1) % 2].classList.add('next');
})();
</script>
</body>
</html>