Spaces:
Running
Running
fixed slide rendering bug
Browse files
app.py
CHANGED
@@ -984,12 +984,13 @@ Example: 'Received {total_slides} slides, {total_slides} scripts, and HTML files
|
|
984 |
js_code = """
|
985 |
() => {
|
986 |
// Function to wait for an element to appear in the DOM
|
987 |
-
window.addEventListener('load', function () {
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
});
|
|
|
993 |
function waitForElement(selector, callback, maxAttempts = 50, interval = 100) {
|
994 |
let attempts = 0;
|
995 |
const intervalId = setInterval(() => {
|
@@ -1006,12 +1007,36 @@ window.addEventListener('load', function () {
|
|
1006 |
}, interval);
|
1007 |
}
|
1008 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1009 |
// Main initialization function
|
1010 |
function initializeSlides() {
|
1011 |
console.log("Initializing slides...");
|
1012 |
|
1013 |
// Wait for lecture-data to load the JSON data
|
1014 |
-
waitForElement('#lecture-data', (dataElement) => {
|
1015 |
if (!dataElement.textContent) {
|
1016 |
console.error("Lecture data element is empty");
|
1017 |
return;
|
@@ -1038,7 +1063,7 @@ window.addEventListener('load', function () {
|
|
1038 |
let currentAudioIndex = 0;
|
1039 |
|
1040 |
// Wait for slide-content element
|
1041 |
-
waitForElement('#slide-content', (slideContent) => {
|
1042 |
console.log("Slide content element found");
|
1043 |
|
1044 |
// Initialize audio elements
|
@@ -1052,56 +1077,63 @@ window.addEventListener('load', function () {
|
|
1052 |
}
|
1053 |
}
|
1054 |
|
1055 |
-
function renderSlide() {
|
1056 |
console.log("Rendering slide:", currentSlide + 1);
|
1057 |
if (currentSlide >= 0 && currentSlide < totalSlides && lectureData.htmlFiles[currentSlide]) {
|
1058 |
const iframe = document.getElementById('slide-iframe');
|
1059 |
if (iframe) {
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
-
|
1068 |
-
const
|
1069 |
-
const
|
1070 |
-
|
1071 |
-
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
1075 |
-
|
1076 |
-
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
|
1081 |
-
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
|
1091 |
-
|
1092 |
-
|
1093 |
-
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1099 |
}
|
1100 |
-
|
1101 |
-
|
1102 |
-
|
1103 |
-
};
|
1104 |
-
|
|
|
1105 |
} else {
|
1106 |
console.error("Iframe not found");
|
1107 |
}
|
@@ -1114,9 +1146,9 @@ window.addEventListener('load', function () {
|
|
1114 |
}
|
1115 |
}
|
1116 |
|
1117 |
-
function updateSlide(callback) {
|
1118 |
console.log("Updating slide to index:", currentSlide);
|
1119 |
-
renderSlide();
|
1120 |
// Pause and reset all audio elements
|
1121 |
audioElements.forEach(audio => {
|
1122 |
if (audio && audio.pause) {
|
@@ -1132,18 +1164,28 @@ window.addEventListener('load', function () {
|
|
1132 |
}, 100);
|
1133 |
}
|
1134 |
|
1135 |
-
function updateAudioSources(audioUrls) {
|
1136 |
console.log("Updating audio sources:", audioUrls);
|
1137 |
-
audioUrls.
|
1138 |
-
const
|
1139 |
-
|
1140 |
-
|
1141 |
-
|
1142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1143 |
} else if (!audio) {
|
1144 |
-
console.error(`Audio element at index ${
|
1145 |
}
|
1146 |
-
}
|
1147 |
}
|
1148 |
|
1149 |
function prevSlide() {
|
|
|
984 |
js_code = """
|
985 |
() => {
|
986 |
// Function to wait for an element to appear in the DOM
|
987 |
+
window.addEventListener('load', function () {
|
988 |
+
gradioURL = window.location.href
|
989 |
+
if (!gradioURL.endsWith('?__theme=light')) {
|
990 |
+
window.location.replace(gradioURL + '?__theme=light');
|
991 |
+
}
|
992 |
+
});
|
993 |
+
|
994 |
function waitForElement(selector, callback, maxAttempts = 50, interval = 100) {
|
995 |
let attempts = 0;
|
996 |
const intervalId = setInterval(() => {
|
|
|
1007 |
}, interval);
|
1008 |
}
|
1009 |
|
1010 |
+
// Function to check if a file exists
|
1011 |
+
async function checkFileExists(url) {
|
1012 |
+
try {
|
1013 |
+
const response = await fetch(url, { method: 'HEAD' });
|
1014 |
+
return response.ok;
|
1015 |
+
} catch (error) {
|
1016 |
+
console.error(`Error checking file existence for ${url}:`, error);
|
1017 |
+
return false;
|
1018 |
+
}
|
1019 |
+
}
|
1020 |
+
|
1021 |
+
// Function to retry loading a file
|
1022 |
+
async function retryLoadFile(url, maxRetries = 3, delay = 1000) {
|
1023 |
+
for (let i = 0; i < maxRetries; i++) {
|
1024 |
+
const exists = await checkFileExists(url);
|
1025 |
+
if (exists) {
|
1026 |
+
return true;
|
1027 |
+
}
|
1028 |
+
console.log(`Retry ${i + 1}/${maxRetries} for ${url}`);
|
1029 |
+
await new Promise(resolve => setTimeout(resolve, delay));
|
1030 |
+
}
|
1031 |
+
return false;
|
1032 |
+
}
|
1033 |
+
|
1034 |
// Main initialization function
|
1035 |
function initializeSlides() {
|
1036 |
console.log("Initializing slides...");
|
1037 |
|
1038 |
// Wait for lecture-data to load the JSON data
|
1039 |
+
waitForElement('#lecture-data', async (dataElement) => {
|
1040 |
if (!dataElement.textContent) {
|
1041 |
console.error("Lecture data element is empty");
|
1042 |
return;
|
|
|
1063 |
let currentAudioIndex = 0;
|
1064 |
|
1065 |
// Wait for slide-content element
|
1066 |
+
waitForElement('#slide-content', async (slideContent) => {
|
1067 |
console.log("Slide content element found");
|
1068 |
|
1069 |
// Initialize audio elements
|
|
|
1077 |
}
|
1078 |
}
|
1079 |
|
1080 |
+
async function renderSlide() {
|
1081 |
console.log("Rendering slide:", currentSlide + 1);
|
1082 |
if (currentSlide >= 0 && currentSlide < totalSlides && lectureData.htmlFiles[currentSlide]) {
|
1083 |
const iframe = document.getElementById('slide-iframe');
|
1084 |
if (iframe) {
|
1085 |
+
const htmlUrl = lectureData.htmlFiles[currentSlide];
|
1086 |
+
const exists = await retryLoadFile(htmlUrl);
|
1087 |
+
if (exists) {
|
1088 |
+
iframe.src = htmlUrl;
|
1089 |
+
console.log("Set iframe src to:", htmlUrl);
|
1090 |
+
// Adjust font size based on content length and screen size
|
1091 |
+
waitForElement('iframe', (iframe) => {
|
1092 |
+
iframe.onload = () => {
|
1093 |
+
const doc = iframe.contentDocument || iframe.contentWindow.document;
|
1094 |
+
const body = doc.body;
|
1095 |
+
if (body) {
|
1096 |
+
const textLength = body.textContent.length;
|
1097 |
+
const screenWidth = window.innerWidth;
|
1098 |
+
const screenHeight = window.innerHeight;
|
1099 |
+
|
1100 |
+
// Base font size calculation
|
1101 |
+
let baseFontSize;
|
1102 |
+
if (screenWidth >= 1920) {
|
1103 |
+
baseFontSize = 20; // Large screens
|
1104 |
+
} else if (screenWidth >= 1366) {
|
1105 |
+
baseFontSize = 18; // Medium screens
|
1106 |
+
} else {
|
1107 |
+
baseFontSize = 16; // Small screens
|
1108 |
+
}
|
1109 |
+
|
1110 |
+
// Adjust based on content length
|
1111 |
+
let adjustedFontSize;
|
1112 |
+
if (textLength > 1000) {
|
1113 |
+
adjustedFontSize = baseFontSize * 0.8; // Reduce for long content
|
1114 |
+
} else if (textLength > 500) {
|
1115 |
+
adjustedFontSize = baseFontSize * 0.9; // Slightly reduce for medium content
|
1116 |
+
} else {
|
1117 |
+
adjustedFontSize = baseFontSize; // Keep base size for short content
|
1118 |
+
}
|
1119 |
+
|
1120 |
+
// Ensure minimum and maximum sizes
|
1121 |
+
adjustedFontSize = Math.max(14, Math.min(24, adjustedFontSize));
|
1122 |
+
|
1123 |
+
// Apply to all elements
|
1124 |
+
const elements = body.getElementsByTagName('*');
|
1125 |
+
for (let elem of elements) {
|
1126 |
+
elem.style.fontSize = `${adjustedFontSize}px`;
|
1127 |
+
}
|
1128 |
+
|
1129 |
+
console.log(`Adjusted font size to ${adjustedFontSize}px for ${textLength} characters on ${screenWidth}x${screenHeight} screen`);
|
1130 |
}
|
1131 |
+
};
|
1132 |
+
});
|
1133 |
+
} else {
|
1134 |
+
console.error(`HTML file not found after retries: ${htmlUrl}`);
|
1135 |
+
iframe.src = "about:blank";
|
1136 |
+
}
|
1137 |
} else {
|
1138 |
console.error("Iframe not found");
|
1139 |
}
|
|
|
1146 |
}
|
1147 |
}
|
1148 |
|
1149 |
+
async function updateSlide(callback) {
|
1150 |
console.log("Updating slide to index:", currentSlide);
|
1151 |
+
await renderSlide();
|
1152 |
// Pause and reset all audio elements
|
1153 |
audioElements.forEach(audio => {
|
1154 |
if (audio && audio.pause) {
|
|
|
1164 |
}, 100);
|
1165 |
}
|
1166 |
|
1167 |
+
async function updateAudioSources(audioUrls) {
|
1168 |
console.log("Updating audio sources:", audioUrls);
|
1169 |
+
for (let i = 0; i < audioUrls.length; i++) {
|
1170 |
+
const url = audioUrls[i];
|
1171 |
+
const audio = audioElements[i];
|
1172 |
+
if (audio && url) {
|
1173 |
+
const exists = await retryLoadFile(url);
|
1174 |
+
if (exists) {
|
1175 |
+
if (audio.src !== url) {
|
1176 |
+
audio.src = url;
|
1177 |
+
audio.load();
|
1178 |
+
console.log(`Updated audio-${i+1} src to:`, url);
|
1179 |
+
}
|
1180 |
+
} else {
|
1181 |
+
console.error(`Audio file not found after retries: ${url}`);
|
1182 |
+
audio.src = "";
|
1183 |
+
audio.innerHTML = "<span>Audio unavailable</span>";
|
1184 |
+
}
|
1185 |
} else if (!audio) {
|
1186 |
+
console.error(`Audio element at index ${i} not found`);
|
1187 |
}
|
1188 |
+
}
|
1189 |
}
|
1190 |
|
1191 |
function prevSlide() {
|