UNPKG

wordxml-builder

Version:

Librería TypeScript para construir documentos XML compatibles con Microsoft Word

177 lines (176 loc) 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Paragraph = void 0; const paragraph_1 = require("../types/paragraph"); /** * Clase que representa un párrafo en un documento Word */ class Paragraph { /** * Crea una nueva instancia de Paragraph * @param text - El texto del párrafo * @param style - Estilos opcionales del párrafo */ constructor(text, style) { this.text = text; this.style = style; } /** * Genera el XML del párrafo * @returns El XML del párrafo como string */ toXML() { const styleXml = this.generateStyleXML(); return `<w:p>${styleXml}<w:r><w:t>${this.text}</w:t></w:r></w:p>`; } /** * Genera el XML de los estilos del párrafo * @returns XML con los estilos aplicados */ generateStyleXML() { if (!this.style) return ''; let styleXml = '<w:pPr>'; // Alineación if (this.style.alignment) { styleXml += `<w:jc w:val="${this.style.alignment}"/>`; } // Espaciado if (this.style.spacing) { styleXml += '<w:spacing'; if (this.style.spacing.before !== undefined) { styleXml += ` w:before="${this.style.spacing.before}"`; } if (this.style.spacing.after !== undefined) { styleXml += ` w:after="${this.style.spacing.after}"`; } if (this.style.spacing.line !== undefined) { styleXml += ` w:line="${this.style.spacing.line}"`; } if (this.style.spacing.lineRule) { styleXml += ` w:lineRule="${this.style.spacing.lineRule}"`; } styleXml += '/>'; } // Sangría if (this.style.indent) { styleXml += '<w:ind'; if (this.style.indent.left !== undefined) { styleXml += ` w:left="${this.style.indent.left}"`; } if (this.style.indent.right !== undefined) { styleXml += ` w:right="${this.style.indent.right}"`; } if (this.style.indent.firstLine !== undefined) { styleXml += ` w:firstLine="${this.style.indent.firstLine}"`; } if (this.style.indent.hanging !== undefined) { styleXml += ` w:hanging="${this.style.indent.hanging}"`; } styleXml += '/>'; } // Bordes if (this.style.borders) { styleXml += '<w:pBdr>'; if (this.style.borders.top) { styleXml += this.generateBorderXML('top', this.style.borders.top); } if (this.style.borders.right) { styleXml += this.generateBorderXML('right', this.style.borders.right); } if (this.style.borders.bottom) { styleXml += this.generateBorderXML('bottom', this.style.borders.bottom); } if (this.style.borders.left) { styleXml += this.generateBorderXML('left', this.style.borders.left); } if (this.style.borders.between) { styleXml += this.generateBorderXML('between', this.style.borders.between); } if (this.style.borders.bar) { styleXml += this.generateBorderXML('bar', this.style.borders.bar); } styleXml += '</w:pBdr>'; } // Sombreado if (this.style.shading) { styleXml += '<w:shd'; styleXml += ` w:fill="${this.style.shading.fill}"`; if (this.style.shading.color) { styleXml += ` w:color="${this.style.shading.color}"`; } if (this.style.shading.val) { styleXml += ` w:val="${this.style.shading.val}"`; } styleXml += '/>'; } // Lista if (this.style.listFormat) { styleXml += '<w:numPr>'; styleXml += `<w:ilvl w:val="${this.style.listFormat.level || 0}"/>`; styleXml += `<w:numId w:val="${this.getListStyleId(this.style.listFormat.style)}"/>`; if (this.style.listFormat.restart) { styleXml += '<w:lvlOverride w:ilvl="0">'; styleXml += `<w:startOverride w:val="${this.style.listFormat.start || 1}"/>`; styleXml += '</w:lvlOverride>'; } styleXml += '</w:numPr>'; } // Opciones adicionales if (this.style.keepLines) styleXml += '<w:keepLines/>'; if (this.style.keepNext) styleXml += '<w:keepNext/>'; if (this.style.pageBreakBefore) styleXml += '<w:pageBreakBefore/>'; if (this.style.widowControl) styleXml += '<w:widowControl/>'; if (this.style.suppressLineNumbers) styleXml += '<w:suppressLineNumbers/>'; if (this.style.suppressAutoHyphens) styleXml += '<w:suppressAutoHyphens/>'; if (this.style.contextualSpacing) styleXml += '<w:contextualSpacing/>'; if (this.style.mirrorIndents) styleXml += '<w:mirrorIndents/>'; if (this.style.suppressOverlap) styleXml += '<w:suppressOverlap/>'; if (this.style.textDirection) { styleXml += `<w:textDirection w:val="${this.style.textDirection}"/>`; } if (this.style.outlineLevel !== undefined) { styleXml += `<w:outlineLvl w:val="${this.style.outlineLevel}"/>`; } styleXml += '</w:pPr>'; return styleXml; } /** * Genera el XML para un borde específico * @param position - Posición del borde * @param border - Configuración del borde * @returns XML del borde */ generateBorderXML(position, border) { // eslint-disable-next-line max-len return `<w:${position} w:val="${border.style}" w:sz="${border.size}" w:color="${border.color}" w:space="${border.space}"/>`; } /** * Obtiene el ID de estilo de lista correspondiente * @param style - Estilo de lista * @returns ID del estilo */ getListStyleId(style) { const styleMap = { [paragraph_1.ListStyle.None]: 0, [paragraph_1.ListStyle.Bullet]: 1, [paragraph_1.ListStyle.Number]: 2, [paragraph_1.ListStyle.Decimal]: 3, [paragraph_1.ListStyle.LowerRoman]: 4, [paragraph_1.ListStyle.UpperRoman]: 5, [paragraph_1.ListStyle.LowerLetter]: 6, [paragraph_1.ListStyle.UpperLetter]: 7, }; return styleMap[style] || 0; } } exports.Paragraph = Paragraph;