From 047dccef7fb848cbd2d1905665c86495c913771e Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Wed, 18 Nov 2020 19:04:53 +0200 Subject: [PATCH] Refactor last PR --- .../Discord/Models/Attachment.cs | 26 ++++++---- .../Exporting/Writers/Html/Core.css | 5 +- .../Writers/Html/MessageGroupTemplate.cshtml | 52 +++++++++---------- .../ViewModels/Framework/DialogScreen.cs | 2 +- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/DiscordChatExporter.Domain/Discord/Models/Attachment.cs b/DiscordChatExporter.Domain/Discord/Models/Attachment.cs index a7dddf65..4c9d2679 100644 --- a/DiscordChatExporter.Domain/Discord/Models/Attachment.cs +++ b/DiscordChatExporter.Domain/Discord/Models/Attachment.cs @@ -1,6 +1,6 @@ using System; +using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.Json; using DiscordChatExporter.Domain.Discord.Models.Common; using DiscordChatExporter.Domain.Internal.Extensions; @@ -20,13 +20,11 @@ namespace DiscordChatExporter.Domain.Discord.Models public int? Height { get; } - public bool IsImage => - ImageFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase); + public bool IsImage => ImageFileExtensions.Contains(Path.GetExtension(FileName)); - public bool IsVideo => - WebSafeVideoFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase); - public bool IsAudio => - WebSafeAudioFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase); + public bool IsVideo => VideoFileExtensions.Contains(Path.GetExtension(FileName)); + + public bool IsAudio => AudioFileExtensions.Contains(Path.GetExtension(FileName)); public bool IsSpoiler => (IsImage || IsVideo || IsAudio) && FileName.StartsWith("SPOILER_", StringComparison.Ordinal); @@ -48,9 +46,17 @@ namespace DiscordChatExporter.Domain.Discord.Models public partial class Attachment { - private static readonly string[] ImageFileExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}; - private static readonly string[] WebSafeVideoFileExtensions = { ".mp4", ".webm" }; - private static readonly string[] WebSafeAudioFileExtensions = { ".mp3", ".wav", ".ogg", ".flac", ".m4a" }; + private static readonly HashSet ImageFileExtensions = + new HashSet(StringComparer.OrdinalIgnoreCase) + {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}; + + private static readonly HashSet VideoFileExtensions = + new HashSet(StringComparer.OrdinalIgnoreCase) + {".mp4", ".webm"}; + + private static readonly HashSet AudioFileExtensions = + new HashSet(StringComparer.OrdinalIgnoreCase) + {".mp3", ".wav", ".ogg", ".flac", ".m4a"}; public static Attachment Parse(JsonElement json) { diff --git a/DiscordChatExporter.Domain/Exporting/Writers/Html/Core.css b/DiscordChatExporter.Domain/Exporting/Writers/Html/Core.css index f407a81d..f0609060 100644 --- a/DiscordChatExporter.Domain/Exporting/Writers/Html/Core.css +++ b/DiscordChatExporter.Domain/Exporting/Writers/Html/Core.css @@ -49,11 +49,14 @@ img { .markdown { max-width: 100%; - white-space: pre-wrap; line-height: 1.3; overflow-wrap: break-word; } +.preserve-whitespace { + white-space: pre-wrap; +} + .spoiler { /* width: fit-content; */ display: inline-block; diff --git a/DiscordChatExporter.Domain/Exporting/Writers/Html/MessageGroupTemplate.cshtml b/DiscordChatExporter.Domain/Exporting/Writers/Html/MessageGroupTemplate.cshtml index 01c1af72..5b0939a1 100644 --- a/DiscordChatExporter.Domain/Exporting/Writers/Html/MessageGroupTemplate.cshtml +++ b/DiscordChatExporter.Domain/Exporting/Writers/Html/MessageGroupTemplate.cshtml @@ -14,7 +14,7 @@ var userMember = Model.ExportContext.TryGetMember(Model.MessageGroup.Author.Id); var userColor = Model.ExportContext.TryGetUserColor(Model.MessageGroup.Author.Id); - var userNick = (Model.MessageGroup.Author.IsBot ? Model.MessageGroup.Author.Name : (userMember?.Nick ?? Model.MessageGroup.Author.Name)); + var userNick = Model.MessageGroup.Author.IsBot ? Model.MessageGroup.Author.Name : userMember?.Nick ?? Model.MessageGroup.Author.Name; var userColorStyle = userColor != null ? $"color: rgb({userColor?.R},{userColor?.G},{userColor?.B})" @@ -40,43 +40,42 @@ var isPinnedStyle = message.IsPinned ? "chatlog__message--pinned" : null;
-
-
@Raw(FormatMarkdown(message.Content)) @if (message.EditedTimestamp != null) - {(edited)}
-
+ @if (!string.IsNullOrWhiteSpace(message.Content) || message.EditedTimestamp != null) + { +
+
+ @Raw(FormatMarkdown(message.Content)) + @if (message.EditedTimestamp != null) + { + (edited) + } +
+
+ } @foreach (var attachment in message.Attachments) {
-
-
- +
+
@if (attachment.IsImage) { - @($"Image: {attachment.FileName} ({attachment.FileSize})") -
- Attachment + Image attachment
} else if (attachment.IsVideo) { - - @($"Video: {attachment.FileName} ({attachment.FileSize})") - -
} else if (attachment.IsAudio) { - - @($"Audio: {attachment.FileName} ({attachment.FileSize})") - -
-
@@ -136,12 +134,12 @@ @if (!string.IsNullOrWhiteSpace(embed.Url)) { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
} else { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
}
} @@ -149,7 +147,7 @@ @if (!string.IsNullOrWhiteSpace(embed.Description)) {
-
@Raw(FormatEmbedMarkdown(embed.Description))
+
@Raw(FormatEmbedMarkdown(embed.Description))
} @@ -164,14 +162,14 @@ @if (!string.IsNullOrWhiteSpace(field.Name)) {
-
@Raw(FormatEmbedMarkdown(field.Name))
+
@Raw(FormatEmbedMarkdown(field.Name))
} @if (!string.IsNullOrWhiteSpace(field.Value)) {
-
@Raw(FormatEmbedMarkdown(field.Value))
+
@Raw(FormatEmbedMarkdown(field.Value))
}
diff --git a/DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs b/DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs index 91b6e1ca..0e103a6c 100644 --- a/DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs +++ b/DiscordChatExporter.Gui/ViewModels/Framework/DialogScreen.cs @@ -12,7 +12,7 @@ namespace DiscordChatExporter.Gui.ViewModels.Framework public void Close(T dialogResult = default) { - DialogResult = dialogResult; + DialogResult = dialogResult!; Closed?.Invoke(this, EventArgs.Empty); } }