coursewareStyle.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 构建课件背景样式
  3. * @param {object} background 背景信息
  4. * @param {string} type 预览类型,commonPreview: 公共预览,courseware: 课件预览
  5. * @returns {object} 样式对象
  6. */
  7. export function buildCoursewareStyle(background, type) {
  8. const { background_image_url: bcImgUrl = '', background_position: pos = {}, background: back } = background || {};
  9. if (type === 'commonPreview' && !back?.is_global) {
  10. return {};
  11. }
  12. if (type === 'courseware' && back?.is_global) {
  13. return {};
  14. }
  15. const canvasStyle = {
  16. backgroundSize: bcImgUrl ? `${pos.width}% ${pos.height}%` : '',
  17. backgroundPosition: bcImgUrl ? `${pos.left}% ${pos.top}%` : '',
  18. backgroundImage: bcImgUrl ? `url(${bcImgUrl})` : '',
  19. };
  20. if (!back) {
  21. return canvasStyle;
  22. }
  23. if (!back.has_image) {
  24. canvasStyle.backgroundBlendMode = '';
  25. canvasStyle.backgroundImage = '';
  26. canvasStyle.backgroundRepeat = '';
  27. canvasStyle.backgroundPosition = '';
  28. canvasStyle.backgroundSize = '';
  29. }
  30. if (back.imageMode === 'fill') {
  31. canvasStyle.backgroundRepeat = 'repeat';
  32. canvasStyle.backgroundSize = '';
  33. canvasStyle.backgroundPosition = '';
  34. } else {
  35. canvasStyle.backgroundRepeat = 'no-repeat';
  36. }
  37. if (back.imageMode === 'stretch') {
  38. canvasStyle.backgroundSize = '100% 100%';
  39. }
  40. if (back.imageMode === 'adapt') {
  41. canvasStyle.backgroundSize = 'contain';
  42. }
  43. if (back.imageMode === 'auto') {
  44. canvasStyle.backgroundPosition = `${pos.imgX}% ${pos.imgY}%`;
  45. }
  46. if (back.has_color) {
  47. canvasStyle.backgroundColor = back.color;
  48. } else {
  49. canvasStyle.backgroundColor = '#fff';
  50. }
  51. if (back.enable_border) {
  52. canvasStyle.border = `${back.border_width}px ${back.border_style} ${back.border_color}`;
  53. } else {
  54. canvasStyle.border = 'none';
  55. }
  56. if (back.enable_radius) {
  57. canvasStyle['border-top-left-radius'] = `${back.top_left_radius}px`;
  58. canvasStyle['border-top-right-radius'] = `${back.top_right_radius}px`;
  59. canvasStyle['border-bottom-left-radius'] = `${back.bottom_left_radius}px`;
  60. canvasStyle['border-bottom-right-radius'] = `${back.bottom_right_radius}px`;
  61. } else {
  62. canvasStyle.borderRadius = '0';
  63. }
  64. return canvasStyle;
  65. }