Skip to content

Commit

Permalink
rm EditorComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
dorthl committed Aug 8, 2023
1 parent 12ddea7 commit f6a2933
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 99 deletions.
73 changes: 0 additions & 73 deletions src/Blogifier.Admin/Components/EditorComponent.razor

This file was deleted.

3 changes: 1 addition & 2 deletions src/Blogifier.Admin/Components/PageTitleComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

protected override async Task OnInitializedAsync()
{
await SetTitleAsync();
await _jsRuntime.InvokeVoidAsync("commonJsFunctions.setTitle", Title);
}
private async Task SetTitleAsync() => await _jsRuntime.InvokeVoidAsync("commonJsFunctions.setTitle", Title);
}
71 changes: 64 additions & 7 deletions src/Blogifier.Admin/Components/PostEditorComponent.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
@using System.Text.RegularExpressions;
@using System.Text;

@implements IAsyncDisposable

@inject IStringLocalizer<Resource> _localizer
@inject IJSRuntime _jsruntime
@inject IJSRuntime _jsRuntime
@inject NavigationManager _navigation
@inject IToaster _toaster
@inject HttpClient _httpClient

<div class="bfeditor">
<div class="bfeditor-header">
Expand Down Expand Up @@ -73,7 +79,10 @@
</div>
</div>
</div>
<EditorComponent @ref="_editorComponent" Toolbar="fullToolbar" />
<div class="easymde-wrapper">
<textarea @ref="_textareaReference" tabindex="2" class="visually-hidden" placeholder="@_localizer["type-here"]"></textarea>
<InputFile @ref="_inputFileReference" OnChange="@LoadImageFiles" style="display:none;" accept="image/*" />
</div>
</div>

@code {
Expand All @@ -82,18 +91,60 @@
[Parameter] public EventCallback<PostEditorDto> OnSaveCallback { get; set; }
[Parameter] public EventCallback<int> OnRemoveCallback { get; set; }

private EditorComponent _editorComponent = default!;
private ValueTask<IJSObjectReference> _taskModule;
private ElementReference? _textareaReference;
private InputFile? _inputFileReference;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_taskModule = _jsRuntime.InvokeAsync<IJSObjectReference>("import", "./admin/js/editor.js");
var module = await _taskModule;
var element = _inputFileReference?.Element;
await module.InvokeVoidAsync("loadEditor", "fullToolbar", _textareaReference, element);
}
}

public async Task SetPostInfoAsync(PostEditorDto post)
{
var headTitle = _localizer["edit"] + " - " + post.Title;
await _jsruntime.InvokeVoidAsync("commonJsFunctions.setTitle", headTitle);
await _editorComponent.SetValueAsync(post.Content);
await _jsRuntime.InvokeVoidAsync("commonJsFunctions.setTitle", headTitle);
var module = await _taskModule;
await module.InvokeVoidAsync("setEditorValue", post.Content);
}

protected async Task LoadImageFiles(InputFileChangeEventArgs args)
{
var module = await _taskModule;
var element = _inputFileReference?.Element;
await module.InvokeVoidAsync("writeFrontFile", element);
}

async ValueTask<string?> GetValueAsync()
{
var module = await _taskModule;
var content = await module.InvokeAsync<string>("getEditorValue");
var imgsMatches = StringHelper.MatchesMarkdownImgBlob(content);

if (imgsMatches.Count > 0)
{
var contentStringBuilder = new StringBuilder(content);
foreach (Match match in imgsMatches)
{
var imageUrl = match.Groups[1].Value;
var imageBytes = await _httpClient.GetByteArrayAsync(imageUrl);
var base64String = Convert.ToBase64String(imageBytes);
contentStringBuilder.Replace(imageUrl, "data:image/png;base64," + base64String);
}
content = contentStringBuilder.ToString();
}
return content;
}

protected async Task SaveCoreAsync(PostState postState)
{
var content = await _editorComponent.GetValueAsync();
var content = await GetValueAsync();
if (string.IsNullOrEmpty(Post.Title) || string.IsNullOrEmpty(content))
{
_toaster.Error(_localizer["title-content-required"]);
Expand Down Expand Up @@ -125,7 +176,7 @@

protected async Task RemoveAsync(int id)
{
if (await _jsruntime.InvokeAsync<bool>("confirm", _localizer["confirm-delete"]))
if (await _jsRuntime.InvokeAsync<bool>("confirm", _localizer["confirm-delete"]))
{
await OnRemoveCallback.InvokeAsync(id);
}
Expand All @@ -138,4 +189,10 @@
return Task.CompletedTask;
}

async ValueTask IAsyncDisposable.DisposeAsync()
{
var module = await _taskModule;
await module.DisposeAsync();
}

}
18 changes: 1 addition & 17 deletions src/Blogifier.Admin/Pages/Blogs/EditorView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,6 @@
var result = await _httpClient.DeleteAsync($"api/post/{id}");
if (result.IsSuccessStatusCode) _toaster.Success(_localizer["completed"]);
else _toaster.Error(_localizer["generic-error"]);
_navigation.NavigateTo($"admin");
_navigation.NavigateTo("admin");
}

//protected async Task OnFileUploadAsync(FrontFilePreviewDto file)
//{
// using var fileContent = new StreamContent(file.BrowserFile.OpenReadStream());
// fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.BrowserFile.ContentType);
// using var content = new MultipartFormDataContent();
// content.Add(content: fileContent, name: "\"file\"", fileName: file.BrowserFile.Name);
// var response = await _httpClient.PostAsync("api/storage/upload", content);
// if (response.IsSuccessStatusCode)
// {
// var stream = await response.Content.ReadAsStreamAsync();
// var storage = JsonSerializer.Deserialize<StorageDto>(stream, BlogifierSharedConstant.DefaultJsonSerializerOptions)!;
// await _postEditorComponent.ReplaceSelectionAsync(file, storage);
// }
//}
}
3 changes: 3 additions & 0 deletions src/Blogifier.Shared/Resources/Resource.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,7 @@
<data name="view-all" xml:space="preserve">
<value>查看所有</value>
</data>
<data name="actions" xml:space="preserve">
<value>操作</value>
</data>
</root>

0 comments on commit f6a2933

Please sign in to comment.