CatPtain commited on
Commit
01a8b48
·
verified ·
1 Parent(s): 22fed99

Upload public.js

Browse files
Files changed (1) hide show
  1. backend/src/routes/public.js +57 -76
backend/src/routes/public.js CHANGED
@@ -98,34 +98,26 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
98
 
99
  const pptDimensions = calculatePptDimensions(slide);
100
 
101
- // 渲染幻灯片元素 - 使用相对单位,支持动态缩放
102
  const renderElements = (elements) => {
103
  if (!elements || elements.length === 0) return '';
104
 
105
  return elements.map(element => {
106
- // 计算相对于PPT原始尺寸的百分比位置和大小
107
- const leftPercent = ((element.left || 0) / pptDimensions.width) * 100;
108
- const topPercent = ((element.top || 0) / pptDimensions.height) * 100;
109
- const widthPercent = ((element.width || 0) / pptDimensions.width) * 100;
110
- const heightPercent = ((element.height || 0) / pptDimensions.height) * 100;
111
-
112
  const style = `
113
  position: absolute;
114
- left: ${leftPercent}%;
115
- top: ${topPercent}%;
116
- width: ${widthPercent}%;
117
- height: ${heightPercent}%;
118
  transform: rotate(${element.rotate || 0}deg);
119
  z-index: ${element.zIndex || 1};
120
  `;
121
 
122
  switch (element.type) {
123
  case 'text':
124
- // 字体大小也需要相对于容器尺寸进行缩放
125
- const fontSizePercent = ((element.fontSize || 14) / pptDimensions.height) * 100;
126
  return `
127
  <div style="${style}
128
- font-size: ${fontSizePercent}vh;
129
  font-family: ${element.fontName || 'Arial'};
130
  color: ${element.defaultColor || '#000'};
131
  font-weight: ${element.bold ? 'bold' : 'normal'};
@@ -133,7 +125,7 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
133
  text-decoration: ${element.underline ? 'underline' : 'none'};
134
  text-align: ${element.align || 'left'};
135
  line-height: ${element.lineHeight || 1.2};
136
- padding: 1vh;
137
  word-wrap: break-word;
138
  overflow: hidden;
139
  box-sizing: border-box;
@@ -149,7 +141,7 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
149
  `;
150
  case 'shape':
151
  const shapeStyle = element.fill ? `background-color: ${element.fill};` : '';
152
- const borderStyle = element.outline ? `border: ${((element.outline.width || 1) / pptDimensions.height) * 100}vh ${element.outline.style || 'solid'} ${element.outline.color || '#000'};` : '';
153
  return `
154
  <div style="${style} ${shapeStyle} ${borderStyle} box-sizing: border-box;"></div>
155
  `;
@@ -187,7 +179,7 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
187
  left: 0 !important;
188
  }
189
 
190
- /* Body - 填满整个窗口,黑色背景,居中显示容器 */
191
  body {
192
  width: 100vw !important;
193
  height: 100vh !important;
@@ -202,20 +194,15 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
202
  justify-content: center !important;
203
  }
204
 
205
- /* PPT容器 - 保持PPT长宽比,最大化填满窗口 */
206
  .slide-container {
 
 
207
  background-color: ${slide.background?.color || theme.backgroundColor || '#ffffff'} !important;
208
  position: relative !important;
209
  overflow: hidden !important;
210
- /* 尺寸将通过JavaScript动态设置 */
211
- }
212
-
213
- /* PPT内容区域 - 完全填满容器,不需要额外缩放 */
214
- .ppt-content {
215
- width: 100% !important;
216
- height: 100% !important;
217
- position: relative !important;
218
- overflow: hidden !important;
219
  }
220
 
221
  /* 背景图片处理 */
@@ -268,9 +255,7 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
268
  </head>
269
  <body>
270
  <div class="slide-container" id="slideContainer">
271
- <div class="ppt-content" id="pptContent">
272
- ${renderElements(slide.elements)}
273
- </div>
274
  </div>
275
 
276
  <script>
@@ -280,65 +265,45 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
280
  height: ${pptDimensions.height}
281
  };
282
 
283
- console.log('PPT页面初始化 - 响应式元素缩放模式:', window.PPT_DIMENSIONS);
284
 
285
- // 计算容器尺寸,保持PPT长宽比,最大化填满窗口
286
- function calculateContainerSize() {
287
  const windowWidth = window.innerWidth;
288
  const windowHeight = window.innerHeight;
289
  const pptWidth = ${pptDimensions.width};
290
  const pptHeight = ${pptDimensions.height};
291
 
292
- // PPT的长宽比
293
- const pptAspectRatio = pptWidth / pptHeight;
294
- // 窗口的长宽比
295
- const windowAspectRatio = windowWidth / windowHeight;
296
 
297
- let containerWidth, containerHeight;
 
298
 
299
- if (windowAspectRatio > pptAspectRatio) {
300
- // 窗口比PPT更宽,以高度为准
301
- containerHeight = windowHeight;
302
- containerWidth = containerHeight * pptAspectRatio;
303
- } else {
304
- // 窗口比PPT更高,以宽度为准
305
- containerWidth = windowWidth;
306
- containerHeight = containerWidth / pptAspectRatio;
307
- }
308
-
309
- return {
310
- width: Math.floor(containerWidth),
311
- height: Math.floor(containerHeight)
312
- };
313
  }
314
 
315
- // 应用容器尺寸,PPT元素会自动适应
316
- function adaptPPTToWindow() {
317
  const container = document.getElementById('slideContainer');
318
- const content = document.getElementById('pptContent');
319
- if (!container || !content) return;
320
 
321
- const containerSize = calculateContainerSize();
322
 
323
- // 设置容器尺寸(保持PPT长宽比)
324
- container.style.width = containerSize.width + 'px';
325
- container.style.height = containerSize.height + 'px';
326
 
327
- // PPT内容完全填满容器,元素使用百分比单位会自动缩放
328
- content.style.width = '100%';
329
- content.style.height = '100%';
330
- content.style.position = 'relative';
331
-
332
- console.log(`容器尺寸: ${containerSize.width}x${containerSize.height}`);
333
- console.log(`PPT长宽比: ${(pptDimensions.width/pptDimensions.height).toFixed(3)}, 容器长宽比: ${(containerSize.width/containerSize.height).toFixed(3)}`);
334
  }
335
 
336
  // 页面加载完成后初始化
337
  window.addEventListener('load', function() {
338
  const html = document.documentElement;
339
  const body = document.body;
 
340
 
341
- console.log('页面加载完成,开始响应式元素布局');
342
 
343
  // 确保页面元素填满窗口
344
  html.style.width = '100vw';
@@ -358,8 +323,15 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
358
  body.style.alignItems = 'center';
359
  body.style.justifyContent = 'center';
360
 
361
- // 执行自适应布局
362
- adaptPPTToWindow();
 
 
 
 
 
 
 
363
 
364
  // 禁用各种用户交互
365
  document.addEventListener('wheel', function(e) {
@@ -392,19 +364,28 @@ const generateSlideHTML = (pptData, slideIndex, title) => {
392
  }
393
  }, { passive: false });
394
 
395
- console.log('响应式元素PPT页面初始化完成');
 
 
 
 
 
 
 
 
 
396
  });
397
 
398
- // 监听窗口大小变化,实时调整布局
399
  window.addEventListener('resize', function() {
400
- console.log('窗口大小变化,重新计算响应式布局');
401
- adaptPPTToWindow();
402
  });
403
 
404
  // 监听屏幕方向变化(移动设备)
405
  window.addEventListener('orientationchange', function() {
406
- console.log('屏幕方向变化,重新计算响应式布局');
407
- setTimeout(adaptPPTToWindow, 100);
408
  });
409
  </script>
410
  </body>
 
98
 
99
  const pptDimensions = calculatePptDimensions(slide);
100
 
101
+ // 渲染幻灯片元素 - 使用原始像素值,完全保真
102
  const renderElements = (elements) => {
103
  if (!elements || elements.length === 0) return '';
104
 
105
  return elements.map(element => {
 
 
 
 
 
 
106
  const style = `
107
  position: absolute;
108
+ left: ${element.left || 0}px;
109
+ top: ${element.top || 0}px;
110
+ width: ${element.width || 0}px;
111
+ height: ${element.height || 0}px;
112
  transform: rotate(${element.rotate || 0}deg);
113
  z-index: ${element.zIndex || 1};
114
  `;
115
 
116
  switch (element.type) {
117
  case 'text':
 
 
118
  return `
119
  <div style="${style}
120
+ font-size: ${element.fontSize || 14}px;
121
  font-family: ${element.fontName || 'Arial'};
122
  color: ${element.defaultColor || '#000'};
123
  font-weight: ${element.bold ? 'bold' : 'normal'};
 
125
  text-decoration: ${element.underline ? 'underline' : 'none'};
126
  text-align: ${element.align || 'left'};
127
  line-height: ${element.lineHeight || 1.2};
128
+ padding: 10px;
129
  word-wrap: break-word;
130
  overflow: hidden;
131
  box-sizing: border-box;
 
141
  `;
142
  case 'shape':
143
  const shapeStyle = element.fill ? `background-color: ${element.fill};` : '';
144
+ const borderStyle = element.outline ? `border: ${element.outline.width || 1}px ${element.outline.style || 'solid'} ${element.outline.color || '#000'};` : '';
145
  return `
146
  <div style="${style} ${shapeStyle} ${borderStyle} box-sizing: border-box;"></div>
147
  `;
 
179
  left: 0 !important;
180
  }
181
 
182
+ /* Body - 填满整个窗口,黑色背景,居中显示PPT */
183
  body {
184
  width: 100vw !important;
185
  height: 100vh !important;
 
194
  justify-content: center !important;
195
  }
196
 
197
+ /* PPT容器 - 使用PPT原始尺寸 */
198
  .slide-container {
199
+ width: ${pptDimensions.width}px !important;
200
+ height: ${pptDimensions.height}px !important;
201
  background-color: ${slide.background?.color || theme.backgroundColor || '#ffffff'} !important;
202
  position: relative !important;
203
  overflow: hidden !important;
204
+ transform-origin: center center !important;
205
+ /* 缩放将通过JavaScript动态设置 */
 
 
 
 
 
 
 
206
  }
207
 
208
  /* 背景图片处理 */
 
255
  </head>
256
  <body>
257
  <div class="slide-container" id="slideContainer">
258
+ ${renderElements(slide.elements)}
 
 
259
  </div>
260
 
261
  <script>
 
265
  height: ${pptDimensions.height}
266
  };
267
 
268
+ console.log('PPT页面初始化 - 原始尺寸整体缩放模式:', window.PPT_DIMENSIONS);
269
 
270
+ // 计算整体缩放比例,让PPT适应窗口大小
271
+ function calculateScale() {
272
  const windowWidth = window.innerWidth;
273
  const windowHeight = window.innerHeight;
274
  const pptWidth = ${pptDimensions.width};
275
  const pptHeight = ${pptDimensions.height};
276
 
277
+ // 计算缩放比例,保持PPT长宽比
278
+ const scaleX = windowWidth / pptWidth;
279
+ const scaleY = windowHeight / pptHeight;
 
280
 
281
+ // 使用较小的缩放比例,确保PPT完全可见(会有黑边)
282
+ const scale = Math.min(scaleX, scaleY);
283
 
284
+ return Math.min(scale, 1); // 最大不超过1(不放大)
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  }
286
 
287
+ // 应用缩放变换
288
+ function scalePPTToFitWindow() {
289
  const container = document.getElementById('slideContainer');
290
+ if (!container) return;
 
291
 
292
+ const scale = calculateScale();
293
 
294
+ // 应用缩放变换
295
+ container.style.transform = \`scale(\${scale})\`;
 
296
 
297
+ console.log(\`PPT整体缩放: \${scale.toFixed(3)}x (窗口: \${window.innerWidth}x\${window.innerHeight}, PPT: \${${pptDimensions.width}}x\${${pptDimensions.height}})\`);
 
 
 
 
 
 
298
  }
299
 
300
  // 页面加载完成后初始化
301
  window.addEventListener('load', function() {
302
  const html = document.documentElement;
303
  const body = document.body;
304
+ const container = document.getElementById('slideContainer');
305
 
306
+ console.log('页面加载完成,开始整体缩放布局');
307
 
308
  // 确保页面元素填满窗口
309
  html.style.width = '100vw';
 
323
  body.style.alignItems = 'center';
324
  body.style.justifyContent = 'center';
325
 
326
+ // 确保PPT容器使用原始尺寸
327
+ if (container) {
328
+ container.style.width = '${pptDimensions.width}px';
329
+ container.style.height = '${pptDimensions.height}px';
330
+ container.style.transformOrigin = 'center center';
331
+ }
332
+
333
+ // 执行整体缩放
334
+ scalePPTToFitWindow();
335
 
336
  // 禁用各种用户交互
337
  document.addEventListener('wheel', function(e) {
 
364
  }
365
  }, { passive: false });
366
 
367
+ console.log('原始尺寸PPT页面初始化完成');
368
+
369
+ // 验证最终效果
370
+ setTimeout(() => {
371
+ const actualScale = container.style.transform.match(/scale\\(([^)]+)\\)/);
372
+ const scaleValue = actualScale ? parseFloat(actualScale[1]) : 1;
373
+
374
+ console.log(\`✅ PPT以原始尺寸(\${${pptDimensions.width}}x\${${pptDimensions.height}})显示,整体缩放: \${scaleValue.toFixed(3)}x\`);
375
+ console.log('🎯 显示效果与编辑器完全一致,周围黑边正常');
376
+ }, 200);
377
  });
378
 
379
+ // 监听窗口大小变化,实时调整缩放
380
  window.addEventListener('resize', function() {
381
+ console.log('窗口大小变化,重新计算整体缩放');
382
+ scalePPTToFitWindow();
383
  });
384
 
385
  // 监听屏幕方向变化(移动设备)
386
  window.addEventListener('orientationchange', function() {
387
+ console.log('屏幕方向变化,重新计算整体缩放');
388
+ setTimeout(scalePPTToFitWindow, 100);
389
  });
390
  </script>
391
  </body>