mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.6 KiB
91 lines
2.6 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.Json.Objects;
|
|
|
|
#pragma warning disable ORL1001
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Contents.Text
|
|
{
|
|
[Serializable]
|
|
public sealed class TextContent : Dictionary<string, string>
|
|
{
|
|
public TextContent()
|
|
{
|
|
}
|
|
|
|
public TextContent(NamedContentData data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var languages = new Dictionary<string, StringBuilder>();
|
|
|
|
void AppendText(string language, string text)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(text))
|
|
{
|
|
var sb = languages.GetOrAddNew(language);
|
|
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Append(" ");
|
|
}
|
|
|
|
sb.Append(text);
|
|
}
|
|
}
|
|
|
|
foreach (var field in data)
|
|
{
|
|
if (field.Value != null)
|
|
{
|
|
foreach (var fieldValue in field.Value)
|
|
{
|
|
var appendText = new Action<string>(text => AppendText(fieldValue.Key, text));
|
|
|
|
AppendJsonText(fieldValue.Value, appendText);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var kvp in languages)
|
|
{
|
|
this[kvp.Key] = kvp.Value.ToString();
|
|
}
|
|
}
|
|
|
|
private static void AppendJsonText(IJsonValue value, Action<string> appendText)
|
|
{
|
|
if (value.Type == JsonValueType.String)
|
|
{
|
|
appendText(value.ToString());
|
|
}
|
|
else if (value is JsonArray array)
|
|
{
|
|
foreach (var item in array)
|
|
{
|
|
AppendJsonText(item, appendText);
|
|
}
|
|
}
|
|
else if (value is JsonObject obj)
|
|
{
|
|
foreach (var item in obj.Values)
|
|
{
|
|
AppendJsonText(item, appendText);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|