Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.WordProcessor/Builders/TextBuilder.cs
2026-06-05 15:58:03 +07:00

110 lines
3.0 KiB
C#

namespace QWERTYkez.WordProcessor.Builders;
internal sealed class TextBuilder : ParagraphBuilderBase, IText
{
private readonly ParagraphProperties? _baseParagraphProperties;
internal TextBuilder(FontProps? baseFont, ParagraphProperties? baseParagraphProperties = null) : base(baseFont)
{
_baseParagraphProperties = baseParagraphProperties?.CloneNode(true) as ParagraphProperties;
}
internal static TextBuilder Create(FontProps? baseFont = null, ParagraphProperties? baseParagraphProperties = null) =>
new(baseFont, baseParagraphProperties);
// Переопределяем создание параграфа
protected override IParagraph CreateParagraphBuilder()
{
_paragraph = new Paragraph();
if (_baseParagraphProperties is not null)
{
_paragraph.ParagraphProperties = _baseParagraphProperties.CloneNode(true) as ParagraphProperties;
}
else
{
// Используем стандартные свойства (как в базовом классе)
_paragraph.ParagraphProperties = new ParagraphProperties(
new Justification { Val = JustificationValues.Center },
new SpacingBetweenLines
{
After = "0",
Before = "0",
LineRule = LineSpacingRuleValues.Auto
},
new Indentation
{
Left = "0",
Right = "0",
FirstLine = "0",
Hanging = "0"
}
);
}
_runs = [];
return this;
}
private readonly List<Paragraph> _paragraphs = [];
public List<Paragraph> Build() => _paragraphs;
IText IText.AddFormula(Action<IFormula> configure)
{
var paraBuilder = ParagraphBuilder;
paraBuilder.AddFormula(configure);
_paragraphs.Add(paraBuilder.Build());
return this;
}
// Добавление параграфов
public IText AddParagraph(Action<IParagraph> configure)
{
var paraBuilder = ParagraphBuilder;
configure(paraBuilder);
_paragraphs.Add(paraBuilder.Build());
return this;
}
public IText AddParagraph(string text, FontProps? font = null)
{
var paraBuilder = ParagraphBuilder;
if (font is not null)
{
paraBuilder.AddRun(text, font);
}
else
{
paraBuilder.AddRun(text);
}
_paragraphs.Add(paraBuilder.Build());
return this;
}
public IText AddParagraph(params string[] lines)
{
var paraBuilder = ParagraphBuilder;
if (lines.Length > 0)
{
paraBuilder.AddRun(lines[0]);
for (int i = 1; i < lines.Length; i++)
{
paraBuilder.Break();
paraBuilder.AddRun(lines[i]);
}
}
_paragraphs.Add(paraBuilder.Build());
return this;
}
}