@inherits LocalizableComponentBase
@(MessageForm.Content?.Length ?? 0)/5000
@if (showPreviewButton && isPreviewOpen && MessageForm.Content is { Length: > 0 }) {
@switch (MessageForm.ContentType) { case ContentType.Markdown: @((MarkupString)Markdown.ToHtml(MessageForm.Content)) break; case ContentType.HTML:

@((MarkupString)MessageForm.Content)

break; }
}
@contentError
@foreach (var media in MessageForm.Media) { switch (MessageForm.MediaType) { case MediaType.Images:
@media.AltText

@media.FileName

@media.ContentType @media.Size.GetFileSize(Localizer._pLocalizer)

break; case MediaType.Video: case MediaType.Documents:

@media.FileName

@media.ContentType @media.Size.GetFileSize(Localizer._pLocalizer)

break; case MediaType.Audio:

@media.FileName

@media.ContentType @media.Size.GetFileSize(Localizer._pLocalizer)

break; } }
@if (fileInputErrorMessage is { Length: > 0 }) {
@((MarkupString)fileInputErrorMessage)
}
@foreach (var mediaType in Enum.GetValues()) { }
@foreach (var contentType in Enum.GetValues()) { }
@if (showPreviewButton) { }
@code { [CascadingParameter] CascadingState CascadingState { get; set; } [Parameter] public Message AnsweringMessage { get; set; } [Parameter] public EventCallback OnMessageSubmit { get; set; } MessageForm MessageForm { get; set; } = new(); int totalCharacters { get; set; } = 0; string fileInputErrorMessage { get; set; } string contentError { get; set; } string acceptedFilesTypes { get; set; } = ".jpg,.jpeg,.png,.gif"; bool showPreviewButton { get; set; } = false; bool isPreviewOpen { get; set; } = false; void OpenCloseMessageType() { MessageForm.IsScopeOptionsOpen = !MessageForm.IsScopeOptionsOpen; } void UpdateMessageType(MessageType messageType) { MessageForm.MessageType = messageType; MessageForm.IsScopeOptionsOpen = false; } protected override void OnInitialized() { if (AnsweringMessage != default) { MessageForm.Title = AnsweringMessage.Title; MessageForm.RootMessageId = AnsweringMessage.RootMessageId ?? AnsweringMessage.MessageId; } } bool ShouldDisableUpload() { switch (MessageForm.MediaType) { case MediaType.Images: return MessageForm.Media.Count == 5; case MediaType.Video: case MediaType.Audio: return MessageForm.Media.Count == 1; case MediaType.Documents: return MessageForm.Media.Count == 3; default: return true; } } bool ShouldHaveMultipleUpload() { return MessageForm.MediaType is MediaType.Images or MediaType.Documents; } async Task OnFileChange(InputFileChangeEventArgs eventArgs) { try { fileInputErrorMessage = string.Empty; var maximumFileCount = MessageForm.MediaType switch { MediaType.Images => 5, MediaType.Audio => 1, MediaType.Video => 1, MediaType.Documents => 3, _ => 0 }; if (eventArgs.FileCount > maximumFileCount) { fileInputErrorMessage = string.Format(Localizer["The maximum number of files accepted is {0}, but {1} were supplied."], maximumFileCount, eventArgs.FileCount); return; } if (eventArgs.FileCount + MessageForm.Media.Count > maximumFileCount) { fileInputErrorMessage = string.Format(Localizer["The maximum number of files accepted is {0}, but {1} were supplied."], maximumFileCount, $"{MessageForm.Media.Count}+{eventArgs.FileCount}"); return; } var maxAllowedSize = MessageForm.MediaType switch { MediaType.Images => 3_145_728, MediaType.Audio => 5_242_880, MediaType.Video => 20_971_520, MediaType.Documents => 3_145_728, _ => 0 }; var uploadMedia = default(UploadMedia); using (var memStream = new MemoryStream()) foreach (var file in eventArgs.GetMultipleFiles(maximumFileCount)) { if (file.Name == default || file.ContentType == default) continue; if (MessageForm.Media.Any(m => m.FileName == file.Name)) continue; if (file.Size > maxAllowedSize) { fileInputErrorMessage += string.Format(Localizer["Supplied file \"{0}\" with size {1:N0}MBs exceeds the maximum of {2:N0}MBs."], file.Name, file.Size / 1_048_576, maxAllowedSize / 1_048_576) + "
"; continue; } uploadMedia = new() { FileName = file.Name, ContentType = file.ContentType, Size = file.Size }; try { using (var imgStream = file.OpenReadStream(maxAllowedSize)) { await imgStream.CopyToAsync(memStream); memStream.Position = 0; uploadMedia.Blob = memStream.ToArray(); await memStream.FlushAsync(); } } catch (IOException e) { fileInputErrorMessage = e.Message; continue; } catch (Exception e) { fileInputErrorMessage = e.Message; continue; } if (MessageForm.MediaType is MediaType.Images or MediaType.Audio) uploadMedia.Base64Preview = $"data:{uploadMedia.ContentType};base64,{Convert.ToBase64String(uploadMedia.Blob)}"; MessageForm.Media.Add(uploadMedia); } } catch (Exception e) { fileInputErrorMessage = e.Message; } } void RemoveFile(UploadMedia media) { MessageForm.Media.Remove(media); } void OnTitleChanged(string value) { MessageForm.Title = value; } void ContentLengthChanged() { totalCharacters = MessageForm.Content?.Length ?? 0; StateHasChanged(); } void OnContentTypeChanged(ContentType contentType) { MessageForm.ContentType = contentType; showPreviewButton = contentType is ContentType.Markdown or ContentType.HTML; } void OnMediaTypeChanged(MediaType mediaType) { MessageForm.MediaType = mediaType; acceptedFilesTypes = mediaType switch { MediaType.Images => ".jpg,.jpeg,.png,.gif", MediaType.Video => ".webm,.mp4,.m4v", MediaType.Audio => ".mp3,.wav,.flac,.m4a", MediaType.Documents => ".xlsx,.csv,.ppt,.odt", _ => default }; } void OnOpenClosePreview() { isPreviewOpen = !isPreviewOpen; } async Task OnValidSubmit() { contentError = default; if ((MessageForm.Content is { Length: 0 } && MessageForm.Media.Count == 0)) { contentError = Localizer["Missing content, either message or media"]; return; } await OnMessageSubmit.InvokeAsync(MessageForm); } }