Skip to content

Commit 649cfca

Browse files
authored
Custom Partial variancy support for RTE as it uses a wrapped model (#18290)
1 parent bb47b7e commit 649cfca

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Umbraco.Infrastructure/PropertyEditors/BlockValuePropertyValueEditorBase.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ private void MapBlockItemDataFromEditor(List<BlockItemData> items)
296296
BlockEditorData<TValue, TLayout>? source = BlockEditorValues.DeserializeAndClean(sourceValue);
297297
BlockEditorData<TValue, TLayout>? target = BlockEditorValues.DeserializeAndClean(targetValue);
298298

299+
TValue? mergedBlockValue =
300+
MergeVariantInvariantPropertyValueTyped(source, target, canUpdateInvariantData, allowedCultures);
301+
302+
return _jsonSerializer.Serialize(mergedBlockValue);
303+
}
304+
305+
internal virtual TValue? MergeVariantInvariantPropertyValueTyped(
306+
BlockEditorData<TValue, TLayout>? source,
307+
BlockEditorData<TValue, TLayout>? target,
308+
bool canUpdateInvariantData,
309+
HashSet<string> allowedCultures)
310+
{
299311
source = UpdateSourceInvariantData(source, target, canUpdateInvariantData);
300312

301313
if (source is null && target is null)
@@ -328,7 +340,7 @@ private void MapBlockItemDataFromEditor(List<BlockItemData> items)
328340
CleanupVariantValues(source.BlockValue.ContentData, target.BlockValue.ContentData, canUpdateInvariantData, allowedCultures);
329341
CleanupVariantValues(source.BlockValue.SettingsData, target.BlockValue.SettingsData, canUpdateInvariantData, allowedCultures);
330342

331-
return _jsonSerializer.Serialize(target.BlockValue);
343+
return target.BlockValue;
332344
}
333345

334346
private void CleanupVariantValues(

src/Umbraco.Infrastructure/PropertyEditors/RichTextPropertyEditor.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,70 @@ public override IEnumerable<Guid> ConfiguredElementTypeKeys()
274274
return configuration?.Blocks?.SelectMany(ConfiguredElementTypeKeys) ?? Enumerable.Empty<Guid>();
275275
}
276276

277+
internal override object? MergeVariantInvariantPropertyValue(
278+
object? sourceValue,
279+
object? targetValue,
280+
bool canUpdateInvariantData,
281+
HashSet<string> allowedCultures)
282+
{
283+
TryParseEditorValue(sourceValue, out RichTextEditorValue? sourceRichTextEditorValue);
284+
TryParseEditorValue(targetValue, out RichTextEditorValue? targetRichTextEditorValue);
285+
286+
var mergedBlockValue = MergeBlockVariantInvariantData(
287+
sourceRichTextEditorValue?.Blocks,
288+
targetRichTextEditorValue?.Blocks,
289+
canUpdateInvariantData,
290+
allowedCultures);
291+
292+
var mergedMarkupValue = MergeMarkupValue(
293+
sourceRichTextEditorValue?.Markup ?? string.Empty,
294+
targetRichTextEditorValue?.Markup ?? string.Empty,
295+
mergedBlockValue,
296+
canUpdateInvariantData);
297+
298+
var mergedEditorValue = new RichTextEditorValue { Markup = mergedMarkupValue, Blocks = mergedBlockValue };
299+
return RichTextPropertyEditorHelper.SerializeRichTextEditorValue(mergedEditorValue, _jsonSerializer);
300+
}
301+
302+
private string MergeMarkupValue(
303+
string source,
304+
string target,
305+
RichTextBlockValue? mergedBlockValue,
306+
bool canUpdateInvariantData)
307+
{
308+
// pick source or target based on culture permissions
309+
var mergedMarkup = canUpdateInvariantData ? target : source;
310+
311+
// todo? strip all invalid block links from markup, those tat are no longer in the layout
312+
return mergedMarkup;
313+
}
314+
315+
private RichTextBlockValue? MergeBlockVariantInvariantData(
316+
RichTextBlockValue? sourceRichTextBlockValue,
317+
RichTextBlockValue? targetRichTextBlockValue,
318+
bool canUpdateInvariantData,
319+
HashSet<string> allowedCultures)
320+
{
321+
if (sourceRichTextBlockValue is null && targetRichTextBlockValue is null)
322+
{
323+
return null;
324+
}
325+
326+
BlockEditorData<RichTextBlockValue, RichTextBlockLayoutItem> sourceBlockEditorData =
327+
(sourceRichTextBlockValue is not null ? ConvertAndClean(sourceRichTextBlockValue) : null)
328+
?? new BlockEditorData<RichTextBlockValue, RichTextBlockLayoutItem>([], new RichTextBlockValue());
329+
330+
BlockEditorData<RichTextBlockValue, RichTextBlockLayoutItem> targetBlockEditorData =
331+
(targetRichTextBlockValue is not null ? ConvertAndClean(targetRichTextBlockValue) : null)
332+
?? new BlockEditorData<RichTextBlockValue, RichTextBlockLayoutItem>([], new RichTextBlockValue());
333+
334+
return MergeVariantInvariantPropertyValueTyped(
335+
sourceBlockEditorData,
336+
targetBlockEditorData,
337+
canUpdateInvariantData,
338+
allowedCultures);
339+
}
340+
277341
internal override object? MergePartialPropertyValueForCulture(object? sourceValue, object? targetValue, string? culture)
278342
{
279343
if (sourceValue is null || TryParseEditorValue(sourceValue, out RichTextEditorValue? sourceRichTextEditorValue) is false)

0 commit comments

Comments
 (0)