Refactor last PR

pull/448/head
Tyrrrz 4 years ago
parent cb47902d10
commit 047dccef7f

@ -1,6 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using DiscordChatExporter.Domain.Discord.Models.Common; using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Internal.Extensions; using DiscordChatExporter.Domain.Internal.Extensions;
@ -20,13 +20,11 @@ namespace DiscordChatExporter.Domain.Discord.Models
public int? Height { get; } public int? Height { get; }
public bool IsImage => public bool IsImage => ImageFileExtensions.Contains(Path.GetExtension(FileName));
ImageFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase);
public bool IsVideo => public bool IsVideo => VideoFileExtensions.Contains(Path.GetExtension(FileName));
WebSafeVideoFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase);
public bool IsAudio => public bool IsAudio => AudioFileExtensions.Contains(Path.GetExtension(FileName));
WebSafeAudioFileExtensions.Contains(Path.GetExtension(FileName), StringComparer.OrdinalIgnoreCase);
public bool IsSpoiler => public bool IsSpoiler =>
(IsImage || IsVideo || IsAudio) && FileName.StartsWith("SPOILER_", StringComparison.Ordinal); (IsImage || IsVideo || IsAudio) && FileName.StartsWith("SPOILER_", StringComparison.Ordinal);
@ -48,9 +46,17 @@ namespace DiscordChatExporter.Domain.Discord.Models
public partial class Attachment public partial class Attachment
{ {
private static readonly string[] ImageFileExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}; private static readonly HashSet<string> ImageFileExtensions =
private static readonly string[] WebSafeVideoFileExtensions = { ".mp4", ".webm" }; new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private static readonly string[] WebSafeAudioFileExtensions = { ".mp3", ".wav", ".ogg", ".flac", ".m4a" }; {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"};
private static readonly HashSet<string> VideoFileExtensions =
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{".mp4", ".webm"};
private static readonly HashSet<string> AudioFileExtensions =
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{".mp3", ".wav", ".ogg", ".flac", ".m4a"};
public static Attachment Parse(JsonElement json) public static Attachment Parse(JsonElement json)
{ {

@ -49,11 +49,14 @@ img {
.markdown { .markdown {
max-width: 100%; max-width: 100%;
white-space: pre-wrap;
line-height: 1.3; line-height: 1.3;
overflow-wrap: break-word; overflow-wrap: break-word;
} }
.preserve-whitespace {
white-space: pre-wrap;
}
.spoiler { .spoiler {
/* width: fit-content; */ /* width: fit-content; */
display: inline-block; display: inline-block;

@ -14,7 +14,7 @@
var userMember = Model.ExportContext.TryGetMember(Model.MessageGroup.Author.Id); var userMember = Model.ExportContext.TryGetMember(Model.MessageGroup.Author.Id);
var userColor = Model.ExportContext.TryGetUserColor(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 var userColorStyle = userColor != null
? $"color: rgb({userColor?.R},{userColor?.G},{userColor?.B})" ? $"color: rgb({userColor?.R},{userColor?.G},{userColor?.B})"
@ -40,43 +40,42 @@
var isPinnedStyle = message.IsPinned ? "chatlog__message--pinned" : null; var isPinnedStyle = message.IsPinned ? "chatlog__message--pinned" : null;
<div class="chatlog__message @isPinnedStyle" data-message-id="@message.Id" id="message-@message.Id"> <div class="chatlog__message @isPinnedStyle" data-message-id="@message.Id" id="message-@message.Id">
@if (!string.IsNullOrWhiteSpace(message.Content) || message.EditedTimestamp != null)
{
<div class="chatlog__content"> <div class="chatlog__content">
<div class="markdown">@Raw(FormatMarkdown(message.Content)) @if (message.EditedTimestamp != null) <div class="markdown">
{<span class="chatlog__edited-timestamp" title="@FormatDate(message.EditedTimestamp.Value)">(edited)</span>}</div> <span class="preserve-whitespace">@Raw(FormatMarkdown(message.Content))</span>
</div>
@if (message.EditedTimestamp != null)
{
<span class="chatlog__edited-timestamp" title="@FormatDate(message.EditedTimestamp.Value)">(edited)</span>
}
</div>
</div>
}
@foreach (var attachment in message.Attachments) @foreach (var attachment in message.Attachments)
{ {
<div class="chatlog__attachment"> <div class="chatlog__attachment">
<div class="@((attachment.IsSpoiler ? "spoiler spoiler--hidden" : ""))" onclick="@((attachment.IsSpoiler ? "showSpoiler(event, this)" : ""))"> <div class="@(attachment.IsSpoiler ? "spoiler spoiler--hidden" : "")" onclick="@(attachment.IsSpoiler ? "showSpoiler(event, this)" : "")">
<div class="@((attachment.IsSpoiler ? "spoiler-image" : ""))"> <div class="@(attachment.IsSpoiler ? "spoiler-image" : "")">
@if (attachment.IsImage) @if (attachment.IsImage)
{ {
<a href="@await ResolveUrlAsync(attachment.Url)"> <a href="@await ResolveUrlAsync(attachment.Url)">
@($"Image: {attachment.FileName} ({attachment.FileSize})") <img class="chatlog__attachment-thumbnail" src="@await ResolveUrlAsync(attachment.Url)" alt="Image attachment" title="@($"Image: {attachment.FileName} ({attachment.FileSize})")">
<br />
<img class="chatlog__attachment-thumbnail" src="@await ResolveUrlAsync(attachment.Url)" alt="Attachment">
</a> </a>
} }
else if (attachment.IsVideo) else if (attachment.IsVideo)
{ {
<a href="@await ResolveUrlAsync(attachment.Url)">
@($"Video: {attachment.FileName} ({attachment.FileSize})")
</a>
<br />
<video controls class="chatlog__attachment-thumbnail"> <video controls class="chatlog__attachment-thumbnail">
<source src="@await ResolveUrlAsync(attachment.Url)" alt="Video Attachment"> <source src="@await ResolveUrlAsync(attachment.Url)" alt="Video attachment" title="@($"Video: {attachment.FileName} ({attachment.FileSize})")">
</video> </video>
} }
else if (attachment.IsAudio) else if (attachment.IsAudio)
{ {
<a href="@await ResolveUrlAsync(attachment.Url)"> <audio controls class="chatlog__attachment-thumbnail">
@($"Audio: {attachment.FileName} ({attachment.FileSize})") <source src="@await ResolveUrlAsync(attachment.Url)" alt="Audio attachment" title="@($"Audio: {attachment.FileName} ({attachment.FileSize})")">
</a> </audio>
<br />
<audio controls class="chatlog__attachment-thumbnail" src="@await ResolveUrlAsync(attachment.Url)" alt="Audio Attachment" />
} }
else else
{ {
@ -84,7 +83,6 @@
@($"Attachment: {attachment.FileName} ({attachment.FileSize})") @($"Attachment: {attachment.FileName} ({attachment.FileSize})")
</a> </a>
} }
</div> </div>
</div> </div>
</div> </div>
@ -136,12 +134,12 @@
@if (!string.IsNullOrWhiteSpace(embed.Url)) @if (!string.IsNullOrWhiteSpace(embed.Url))
{ {
<a class="chatlog__embed-title-link" href="@embed.Url"> <a class="chatlog__embed-title-link" href="@embed.Url">
<div class="markdown">@Raw(FormatEmbedMarkdown(embed.Title))</div> <div class="markdown preserve-whitespace">@Raw(FormatEmbedMarkdown(embed.Title))</div>
</a> </a>
} }
else else
{ {
<div class="markdown">@Raw(FormatEmbedMarkdown(embed.Title))</div> <div class="markdown preserve-whitespace">@Raw(FormatEmbedMarkdown(embed.Title))</div>
} }
</div> </div>
} }
@ -149,7 +147,7 @@
@if (!string.IsNullOrWhiteSpace(embed.Description)) @if (!string.IsNullOrWhiteSpace(embed.Description))
{ {
<div class="chatlog__embed-description"> <div class="chatlog__embed-description">
<div class="markdown">@Raw(FormatEmbedMarkdown(embed.Description))</div> <div class="markdown preserve-whitespace">@Raw(FormatEmbedMarkdown(embed.Description))</div>
</div> </div>
} }
@ -164,14 +162,14 @@
@if (!string.IsNullOrWhiteSpace(field.Name)) @if (!string.IsNullOrWhiteSpace(field.Name))
{ {
<div class="chatlog__embed-field-name"> <div class="chatlog__embed-field-name">
<div class="markdown">@Raw(FormatEmbedMarkdown(field.Name))</div> <div class="markdown preserve-whitespace">@Raw(FormatEmbedMarkdown(field.Name))</div>
</div> </div>
} }
@if (!string.IsNullOrWhiteSpace(field.Value)) @if (!string.IsNullOrWhiteSpace(field.Value))
{ {
<div class="chatlog__embed-field-value"> <div class="chatlog__embed-field-value">
<div class="markdown">@Raw(FormatEmbedMarkdown(field.Value))</div> <div class="markdown preserve-whitespace">@Raw(FormatEmbedMarkdown(field.Value))</div>
</div> </div>
} }
</div> </div>

@ -12,7 +12,7 @@ namespace DiscordChatExporter.Gui.ViewModels.Framework
public void Close(T dialogResult = default) public void Close(T dialogResult = default)
{ {
DialogResult = dialogResult; DialogResult = dialogResult!;
Closed?.Invoke(this, EventArgs.Empty); Closed?.Invoke(this, EventArgs.Empty);
} }
} }

Loading…
Cancel
Save