Добавлен Core
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using Lattice.Core.Abstractions;
|
||||
using Lattice.Core.Models;
|
||||
using Lattice.Core.Models.Enums;
|
||||
using Lattice.Core.Persistence;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Lattice.Core.Engine;
|
||||
|
||||
@@ -158,8 +161,68 @@ public class LayoutManager : ILayoutService
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string SaveLayout() { /* Реализация через JsonConverter */ return string.Empty; }
|
||||
public string SaveLayout()
|
||||
{
|
||||
if (_root == null) return string.Empty;
|
||||
|
||||
var options = GetJsonOptions();
|
||||
try
|
||||
{
|
||||
string json = JsonSerializer.Serialize(_root, options);
|
||||
_logger?.LogInformation("Макет успешно экспортирован в JSON. Длина: {Length}", json.Length);
|
||||
return json;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Ошибка при сохранении макета Lattice");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void LoadLayout(string jsonData) { /* Реализация через JsonConverter */ }
|
||||
public void LoadLayout(string jsonData)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(jsonData)) return;
|
||||
|
||||
var options = GetJsonOptions();
|
||||
try
|
||||
{
|
||||
var importedRoot = JsonSerializer.Deserialize<LayoutNode>(jsonData, options);
|
||||
if (importedRoot != null)
|
||||
{
|
||||
// При загрузке нужно восстановить связи Parent, так как они не сериализуются (циклические ссылки)
|
||||
RestoreParentLinks(importedRoot, null);
|
||||
_root = importedRoot;
|
||||
_logger?.LogInformation("Макет успешно загружен. Корневой узел: {Id}", _root.Id);
|
||||
LayoutUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "Ошибка при десериализации макета Lattice");
|
||||
}
|
||||
}
|
||||
|
||||
private JsonSerializerOptions GetJsonOptions()
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Converters = { new LayoutJsonConverter() },
|
||||
// Игнорируем циклы, так как мы восстановим Parent вручную
|
||||
ReferenceHandler = ReferenceHandler.IgnoreCycles
|
||||
};
|
||||
}
|
||||
|
||||
private void RestoreParentLinks(LayoutNode node, LayoutNode? parent)
|
||||
{
|
||||
node.Parent = parent;
|
||||
if (node is SplitContainerNode container)
|
||||
{
|
||||
foreach (var child in container.Children)
|
||||
{
|
||||
RestoreParentLinks(child, container);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user