File size: 2,371 Bytes
7b0525c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5683f0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { loadStyleConfigFromCloud, DEFAULT_STYLE_CONFIG, type CodeStyleConfig } from '../services/codeStyleService';

export const loadCodeStyle = async (): Promise<void> => {
  let codeStyle: CodeStyleConfig = DEFAULT_STYLE_CONFIG.code;

  try {
    const cloudConfig = await loadStyleConfigFromCloud();
    if (cloudConfig) {
      codeStyle = cloudConfig.code;
    }
  } catch (error) {
    console.error('Error loading from cloud, using defaults:', error);
  }

  const rgbaMatch = codeStyle.bgColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*[\d.]+)?\)/);
  let bgColorWithOpacity = codeStyle.bgColor;

  if (rgbaMatch) {
    const [_, r, g, b] = rgbaMatch;
    bgColorWithOpacity = `rgba(${r}, ${g}, ${b}, ${codeStyle.opacity / 100})`;
  }

  const widthValue = codeStyle.width === 'auto' ? 0 : parseFloat(codeStyle.width) || 0;
  const heightValue = codeStyle.height === 'auto' ? 0 : parseFloat(codeStyle.height) || 0;
  
  const existingStyle = document.getElementById('dynamic-code-style');
  if (existingStyle) {
    existingStyle.remove();
  }

  const style = document.createElement('style');
  style.id = 'dynamic-code-style';
  style.innerHTML = `

    :not(pre) > code {

      background-color: ${bgColorWithOpacity} !important;

      color: ${codeStyle.textColor} !important;

      font-size: ${codeStyle.fontSize} !important;

      border-radius: ${codeStyle.borderRadius} !important;

      padding: 0px 2px !important;

      ${codeStyle.width !== 'auto' && widthValue > 0 ? `min-width: ${codeStyle.width} !important;` : ''}

      ${codeStyle.height !== 'auto' && heightValue > 0 ? `min-height: ${codeStyle.height} !important;` : ''}

      

      display: inline-flex !important;

      align-items: center !important;

      justify-content: center !important;

      

      vertical-align: middle !important;

      border: none !important;

      font-family: 'Consolas', monospace !important;

      line-height: 1 !important;

      margin: 0 1px !important;

    }



    button code, a code, .btn code {

      background: transparent !important;

      padding: 0 !important;

      color: inherit !important;

      border: none !important;

    }



    code::before, code::after {

      content: "" !important;

      display: none !important;

    }

  `;

  document.head.appendChild(style);
};