All files / src/config StyleConfig.js

100% Statements 340/340
100% Branches 39/39
100% Functions 19/19
100% Lines 340/340

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 3411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 8x 8x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 3x 3x 8x 3x 3x 3x 3x 3x 8x 1x 1x 1x 8x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x  
/**
 * StyleConfig.js
 *
 * Gestiona la configuración de estilos del documento Word.
 *
 * Características:
 * - Lee configuración desde document-styles.json
 * - Proporciona estilos para fuentes, espaciado, tablas
 * - Soporta múltiples perfiles de estilos
 * - Proporciona valores por defecto si no existe configuración
 */
 
const fs = require("fs");
const path = require("path");
 
class StyleConfig {
  /**
   * @param {string|null} configPath - Ruta al archivo de configuración JSON
   */
  constructor(configPath = null) {
    // Ruta por defecto al archivo de configuración
    this.configPath =
      configPath || path.join(__dirname, "../../document-styles.json");
    this.config = this.loadConfig();
  }
 
  /**
   * Carga la configuración desde el archivo JSON
   * @returns {Object} Configuración de estilos cargada o por defecto
   */
  loadConfig() {
    try {
      if (fs.existsSync(this.configPath)) {
        const data = fs.readFileSync(this.configPath, "utf8");
        const config = JSON.parse(data);
 
        // Validar que tenga las propiedades básicas
        if (!config.fonts || !config.spacing || !config.tables) {
          console.warn(
            "⚠️ Configuración incompleta, usando valores por defecto"
          );
          return this.getDefaultConfig();
        }
 
        return config;
      } else {
        console.warn(
          `⚠️ Archivo de configuración no encontrado: ${this.configPath}`
        );
        return this.getDefaultConfig();
      }
    } catch (error) {
      console.error("❌ Error cargando configuración de estilos:", error);
      return this.getDefaultConfig();
    }
  }
 
  /**
   * Configuración por defecto si no existe el archivo
   */
  getDefaultConfig() {
    return {
      fonts: {
        title: { name: "Calibri", size: 16, bold: true, color: "2E75B6" },
        heading: { name: "Calibri", size: 13, bold: true, color: "1F4E78" },
        normal: { name: "Calibri", size: 11, bold: false, color: "000000" },
        tableHeader: { name: "Calibri", size: 11, bold: true, color: "000000" },
        tableContent: {
          name: "Calibri",
          size: 11,
          bold: false,
          color: "000000",
        },
        hyperlink: {
          name: "Calibri",
          size: 11,
          bold: false,
          color: "0563C1",
          underline: true,
        },
      },
      spacing: {
        beforeTitle: 0,
        afterTitle: 120,
        beforeHeading: 240,
        afterHeading: 120,
        beforeParagraph: 0,
        afterParagraph: 120,
        beforeTable: 120,
        afterTable: 240,
        lineSpacing: 1.15,
      },
      tables: {
        borders: { style: "single", size: 4, color: "CCCCCC" },
        cellPadding: { top: 50, bottom: 50, left: 100, right: 100 },
        headerBackground: { enabled: false, color: "D9E2F3" },
        alternateRows: { enabled: false, color: "F2F2F2" },
      },
      sections: {
        informacion: {
          emoji: "🗂️",
          text: "Información del pase",
          description: "Detalle del pase a producción...",
        },
        documentacion: {
          emoji: "🔖",
          text: "Documentación del pase",
          description: "Apartado para colocar los documentos...",
        },
        requerimientos: {
          emoji: "📋",
          text: "Listado de los requerimientos",
          description: "Listado de tareas a realizar...",
        },
      },
      documentCategories: {
        PLAN_MONTAJE: "Plan de montaje",
        PPT_CRT: "Presentación técnica del CRT",
        VB_DB: "Vb´s de Base de datos",
        VB_CRT: "Vb´s del CRT",
        CICLOS_PRUEBA: "Ciclos de pruebas",
        VB_NEGOCIO: "Vb de Negocio",
        PR_AZURE: "Pull Request de Azure",
        PRUEBAS_PREPROD: "Pruebas en ambiente de preproducción",
      },
      workItemsTable: {
        headers: ["ID", "Work Item Type", "Title", "Iteration Path"],
        columnWidths: [1000, 2000, 5000, 2500],
      },
      branding: {
        companyName: "Tu Empresa",
        department: "TI",
        documentType: "Pase a producción",
      },
    };
  }
 
  /**
   * Obtiene la configuración de fuente para un tipo específico
   * @param {string} type - Tipo de fuente: title, heading, normal, tableHeader, tableContent, hyperlink
   */
  getFont(type) {
    return this.config.fonts[type] || this.config.fonts.normal;
  }
 
  /**
   * Obtiene la configuración de espaciado
   */
  getSpacing() {
    return this.config.spacing;
  }
 
  /**
   * Obtiene la configuración de tablas
   */
  getTableConfig() {
    return this.config.tables;
  }
 
  /**
   * Obtiene el texto de una sección
   * @param {string} sectionKey - informacion, documentacion, requerimientos
   */
  getSection(sectionKey) {
    return this.config.sections[sectionKey];
  }
 
  /**
   * Obtiene todas las secciones
   */
  getAllSections() {
    return this.config.sections;
  }
 
  /**
   * Obtiene el nombre de una categoría de documento
   */
  getDocumentCategory(key) {
    return this.config.documentCategories[key];
  }
 
  /**
   * Obtiene todas las categorías de documentos
   */
  getAllDocumentCategories() {
    return this.config.documentCategories;
  }
 
  /**
   * Obtiene la configuración de la tabla de work items
   */
  getWorkItemsTableConfig() {
    return this.config.workItemsTable;
  }
 
  /**
   * Obtiene la información de branding
   */
  getBranding() {
    return this.config.branding;
  }
 
  /**
   * Crea un objeto TextRun con los estilos configurados
   * @param {string} text - Texto a formatear
   * @param {string} styleType - Tipo de estilo: title, heading, normal, tableHeader, tableContent, hyperlink
   * @param {object} overrides - Propiedades adicionales para sobreescribir
   */
  createStyledText(text, styleType = "normal", overrides = {}) {
    const font = this.getFont(styleType);
 
    return {
      text: text,
      font: font.name,
      size: font.size * 2, // docx usa half-points (11pt = 22)
      bold: font.bold,
      color: font.color,
      underline: font.underline ? { type: "single" } : undefined,
      ...overrides,
    };
  }
 
  /**
   * Crea propiedades de párrafo con el espaciado configurado
   * @param {string} spacingType - beforeTitle, afterTitle, beforeHeading, etc.
   */
  createParagraphSpacing(spacingType) {
    const spacing = this.getSpacing();
 
    switch (spacingType) {
      case "title":
        return {
          before: spacing.beforeTitle,
          after: spacing.afterTitle,
          line: Math.round(spacing.lineSpacing * 240),
        };
      case "heading":
        return {
          before: spacing.beforeHeading,
          after: spacing.afterHeading,
          line: Math.round(spacing.lineSpacing * 240),
        };
      case "paragraph":
        return {
          before: spacing.beforeParagraph,
          after: spacing.afterParagraph,
          line: Math.round(spacing.lineSpacing * 240),
        };
      case "table":
        return {
          before: spacing.beforeTable,
          after: spacing.afterTable,
        };
      default:
        return {
          before: 0,
          after: 0,
          line: Math.round(spacing.lineSpacing * 240),
        };
    }
  }
 
  /**
   * Crea las propiedades de bordes para una tabla
   */
  createTableBorders() {
    const borders = this.config.tables.borders;
 
    return {
      top: { style: borders.style, size: borders.size, color: borders.color },
      bottom: {
        style: borders.style,
        size: borders.size,
        color: borders.color,
      },
      left: { style: borders.style, size: borders.size, color: borders.color },
      right: { style: borders.style, size: borders.size, color: borders.color },
      insideHorizontal: {
        style: borders.style,
        size: borders.size,
        color: borders.color,
      },
      insideVertical: {
        style: borders.style,
        size: borders.size,
        color: borders.color,
      },
    };
  }
 
  /**
   * Crea las propiedades de padding para una celda
   */
  createCellPadding() {
    const padding = this.config.tables.cellPadding;
 
    return {
      top: padding.top,
      bottom: padding.bottom,
      left: padding.left,
      right: padding.right,
    };
  }
 
  /**
   * Obtiene el color de fondo para encabezados de tabla
   */
  getTableHeaderBackground() {
    const config = this.config.tables.headerBackground;
    return config.enabled ? config.color : null;
  }
 
  /**
   * Recarga la configuración desde el archivo
   */
  reload() {
    this.config = this.loadConfig();
  }
 
  /**
   * Guarda cambios en la configuración
   * @param {object} newConfig - Nueva configuración a guardar
   */
  save(newConfig) {
    try {
      this.config = { ...this.config, ...newConfig };
      fs.writeFileSync(
        this.configPath,
        JSON.stringify(this.config, null, 2),
        "utf8"
      );
      return true;
    } catch (error) {
      console.error("❌ Error guardando configuración:", error);
      return false;
    }
  }
}
 
module.exports = StyleConfig;