index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { templates } from './templateData.js';
  2. import { renderTemplates } from './templateRenderer.js';
  3. import { setupEventListeners } from './eventHandlers.js';
  4. import { setupImageUpload } from './imageUpload.js';
  5. // 将模板和渲染函数挂载到window对象上
  6. window.templates = templates;
  7. window.renderTemplates = renderTemplates;
  8. document.addEventListener('DOMContentLoaded', () => {
  9. // 渲染所有模板缩略图
  10. renderTemplates(templates);
  11. // 设置事件监听器
  12. setupEventListeners();
  13. // 初始化图片上传功能
  14. setupImageUpload();
  15. // 添加预览区域的缩放控制
  16. setupPreviewZoom();
  17. // 默认选择第一个模板
  18. setTimeout(() => {
  19. const firstTemplate = document.querySelector('.template-item');
  20. if (firstTemplate) {
  21. firstTemplate.click();
  22. }
  23. }, 100);
  24. });
  25. // 设置预览区域的缩放控制
  26. function setupPreviewZoom() {
  27. const posterPreview = document.getElementById('poster-preview');
  28. const zoomInBtn = document.getElementById('zoom-in');
  29. const zoomOutBtn = document.getElementById('zoom-out');
  30. const resetZoomBtn = document.getElementById('zoom-reset');
  31. const previewContainer = document.querySelector('.preview-container');
  32. if (!posterPreview || !zoomInBtn || !zoomOutBtn || !resetZoomBtn || !previewContainer) return;
  33. // 设置当前缩放级别
  34. let currentZoom = 1;
  35. const zoomStep = 0.1;
  36. const maxZoom = 1.5;
  37. const minZoom = 0.5;
  38. // 添加放大事件
  39. zoomInBtn.addEventListener('click', () => {
  40. if (currentZoom < maxZoom) {
  41. currentZoom += zoomStep;
  42. applyZoom();
  43. }
  44. });
  45. // 添加缩小事件
  46. zoomOutBtn.addEventListener('click', () => {
  47. if (currentZoom > minZoom) {
  48. currentZoom -= zoomStep;
  49. applyZoom();
  50. }
  51. });
  52. // 添加重置事件
  53. resetZoomBtn.addEventListener('click', () => {
  54. currentZoom = 0.65;
  55. applyZoom();
  56. });
  57. // 应用缩放
  58. function applyZoom() {
  59. posterPreview.style.transform = `scale(${currentZoom})`;
  60. }
  61. // 添加鼠标滚轮缩放
  62. previewContainer.addEventListener('wheel', (e) => {
  63. e.preventDefault();
  64. if (e.deltaY < 0 && currentZoom < maxZoom) {
  65. // 向上滚动,放大
  66. currentZoom += zoomStep;
  67. } else if (e.deltaY > 0 && currentZoom > minZoom) {
  68. // 向下滚动,缩小
  69. currentZoom -= zoomStep;
  70. }
  71. applyZoom();
  72. });
  73. // 初始化时自动触发重置缩放
  74. resetZoomBtn.click();
  75. }
  76. /**
  77. * 打赏鼓励
  78. */
  79. document.getElementById('donateLink').addEventListener('click', (event ) => {
  80. event.preventDefault();
  81. chrome.runtime.sendMessage({
  82. type: 'fh-dynamic-any-thing',
  83. thing: 'open-donate-modal',
  84. params: { toolName: 'poster-maker' }
  85. });
  86. });