Jaward commited on
Commit
46a0267
·
verified ·
1 Parent(s): d38f758

fixed slide rendering bug

Browse files
Files changed (1) hide show
  1. app.py +125 -84
app.py CHANGED
@@ -1007,27 +1007,93 @@ js_code = """
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
 
@@ -1041,6 +1107,7 @@ js_code = """
1041
  console.error("Lecture data element is empty");
1042
  return;
1043
  }
 
1044
  let lectureData;
1045
  try {
1046
  lectureData = JSON.parse(dataElement.textContent);
@@ -1067,82 +1134,56 @@ js_code = """
1067
  console.log("Slide content element found");
1068
 
1069
  // Initialize audio elements
1070
- for (let i = 0; i < totalSlides; i++) {
1071
- const audio = document.getElementById(`audio-${i+1}`);
1072
- if (audio) {
1073
- audioElements.push(audio);
1074
- console.log(`Found audio element audio-${i+1}:`, audio);
1075
- } else {
1076
- console.error(`Audio element audio-${i+1} not found`);
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
- }
1140
- } else {
1141
- const iframe = document.getElementById('slide-iframe');
1142
- if (iframe) {
1143
  iframe.src = "about:blank";
1144
- console.log("No valid slide content for index:", currentSlide);
1145
  }
 
 
 
1146
  }
1147
  }
1148
 
@@ -1170,7 +1211,7 @@ js_code = """
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;
 
1007
  }, interval);
1008
  }
1009
 
1010
+ // Function to check if a file exists with retries
1011
+ async function checkFileExists(url, maxRetries = 5, delay = 1000) {
1012
+ for (let i = 0; i < maxRetries; i++) {
1013
+ try {
1014
+ const response = await fetch(url, { method: 'HEAD' });
1015
+ if (response.ok) {
1016
+ console.log(`File exists: ${url}`);
1017
+ return true;
1018
+ }
1019
+ console.log(`File not found (attempt ${i + 1}/${maxRetries}): ${url}`);
1020
+ await new Promise(resolve => setTimeout(resolve, delay));
1021
+ } catch (error) {
1022
+ console.error(`Error checking file (attempt ${i + 1}/${maxRetries}):`, error);
1023
+ await new Promise(resolve => setTimeout(resolve, delay));
1024
+ }
1025
+ }
1026
+ return false;
1027
+ }
1028
+
1029
+ // Function to validate and initialize audio elements
1030
+ async function initializeAudioElements(audioUrls) {
1031
+ console.log("Initializing audio elements with URLs:", audioUrls);
1032
+ const audioElements = [];
1033
+
1034
+ for (let i = 0; i < audioUrls.length; i++) {
1035
+ const url = audioUrls[i];
1036
+ const audioId = `audio-${i+1}`;
1037
+ let audio = document.getElementById(audioId);
1038
+
1039
+ if (!audio) {
1040
+ console.log(`Creating new audio element: ${audioId}`);
1041
+ audio = document.createElement('audio');
1042
+ audio.id = audioId;
1043
+ audio.controls = true;
1044
+ audio.style.display = 'inline-block';
1045
+ audio.style.margin = '0 10px';
1046
+ audio.style.width = '200px';
1047
+
1048
+ // Find the audio container and append the new element
1049
+ const audioContainer = document.querySelector('.audio-timeline');
1050
+ if (audioContainer) {
1051
+ audioContainer.appendChild(audio);
1052
+ }
1053
+ }
1054
+
1055
+ if (url) {
1056
+ const exists = await checkFileExists(url);
1057
+ if (exists) {
1058
+ audio.src = url;
1059
+ audio.load();
1060
+ console.log(`Audio source set for ${audioId}: ${url}`);
1061
+ } else {
1062
+ console.error(`Audio file not found: ${url}`);
1063
+ audio.innerHTML = "<span>Audio unavailable</span>";
1064
+ }
1065
+ } else {
1066
+ console.log(`No URL provided for ${audioId}`);
1067
+ audio.innerHTML = "<span>No audio</span>";
1068
+ }
1069
+
1070
+ audioElements.push(audio);
1071
  }
1072
+
1073
+ return audioElements;
1074
  }
1075
 
1076
+ // Function to render slide with retries
1077
+ async function renderSlideWithRetry(iframe, url, maxRetries = 5) {
1078
+ console.log(`Attempting to render slide: ${url}`);
1079
+
1080
  for (let i = 0; i < maxRetries; i++) {
1081
+ try {
1082
+ const exists = await checkFileExists(url);
1083
+ if (exists) {
1084
+ iframe.src = url;
1085
+ console.log(`Slide rendered successfully: ${url}`);
1086
+ return true;
1087
+ }
1088
+ console.log(`Slide not found (attempt ${i + 1}/${maxRetries}): ${url}`);
1089
+ await new Promise(resolve => setTimeout(resolve, 1000));
1090
+ } catch (error) {
1091
+ console.error(`Error rendering slide (attempt ${i + 1}/${maxRetries}):`, error);
1092
+ await new Promise(resolve => setTimeout(resolve, 1000));
1093
  }
 
 
1094
  }
1095
+
1096
+ console.error(`Failed to render slide after ${maxRetries} attempts: ${url}`);
1097
  return false;
1098
  }
1099
 
 
1107
  console.error("Lecture data element is empty");
1108
  return;
1109
  }
1110
+
1111
  let lectureData;
1112
  try {
1113
  lectureData = JSON.parse(dataElement.textContent);
 
1134
  console.log("Slide content element found");
1135
 
1136
  // Initialize audio elements
1137
+ audioElements = await initializeAudioElements(lectureData.audioFiles);
1138
+ console.log(`Initialized ${audioElements.length} audio elements`);
 
 
 
 
 
 
 
1139
 
1140
  async function renderSlide() {
1141
  console.log("Rendering slide:", currentSlide + 1);
1142
+ const iframe = document.getElementById('slide-iframe');
1143
+ if (!iframe) {
1144
+ console.error("Iframe not found");
1145
+ return;
1146
+ }
1147
+
1148
  if (currentSlide >= 0 && currentSlide < totalSlides && lectureData.htmlFiles[currentSlide]) {
1149
+ const htmlUrl = lectureData.htmlFiles[currentSlide];
1150
+ const success = await renderSlideWithRetry(iframe, htmlUrl);
1151
+
1152
+ if (success) {
1153
+ // Adjust font size based on content
1154
+ iframe.onload = () => {
1155
+ try {
1156
+ const doc = iframe.contentDocument || iframe.contentWindow.document;
1157
+ const body = doc.body;
1158
+ if (body) {
1159
+ const textLength = body.textContent.length;
1160
+ const screenWidth = window.innerWidth;
1161
+
1162
+ let baseFontSize = screenWidth >= 1920 ? 20 : screenWidth >= 1366 ? 18 : 16;
1163
+ let adjustedFontSize = textLength > 1000 ? baseFontSize * 0.8 :
1164
+ textLength > 500 ? baseFontSize * 0.9 :
1165
+ baseFontSize;
1166
+
1167
+ adjustedFontSize = Math.max(14, Math.min(24, adjustedFontSize));
1168
+
1169
+ const elements = body.getElementsByTagName('*');
1170
+ for (let elem of elements) {
1171
+ elem.style.fontSize = `${adjustedFontSize}px`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1172
  }
1173
+
1174
+ console.log(`Adjusted font size to ${adjustedFontSize}px`);
1175
+ }
1176
+ } catch (error) {
1177
+ console.error("Error adjusting font size:", error);
1178
+ }
1179
+ };
1180
  } else {
 
 
 
 
 
1181
  iframe.src = "about:blank";
1182
+ console.error("Failed to render slide");
1183
  }
1184
+ } else {
1185
+ iframe.src = "about:blank";
1186
+ console.log("No valid slide content for index:", currentSlide);
1187
  }
1188
  }
1189
 
 
1211
  const url = audioUrls[i];
1212
  const audio = audioElements[i];
1213
  if (audio && url) {
1214
+ const exists = await checkFileExists(url);
1215
  if (exists) {
1216
  if (audio.src !== url) {
1217
  audio.src = url;