forked from linkdotnet/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateNewBlogPost.razor
More file actions
457 lines (407 loc) · 22.4 KB
/
CreateNewBlogPost.razor
File metadata and controls
457 lines (407 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure
@using LinkDotNet.Blog.Infrastructure.Persistence
@using LinkDotNet.Blog.Web.Features.Services
@using NCronJob
@inject IJSRuntime JSRuntime
@inject ICacheInvalidator CacheInvalidator
@inject IInstantJobRegistry InstantJobRegistry
@inject IRepository<ShortCode> ShortCodeRepository
@inject IRepository<BlogPostTemplate> TemplateRepository
@inject IToastService ToastService
@inject IOptions<ApplicationConfiguration> AppConfiguration
@inject ICurrentUserService CurrentUserService
<PageTitle>Creating new Blog Post</PageTitle>
<div class="container-fluid px-4">
<div class="row">
<div class="col-12">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fw-bold text-primary mb-0">@Title</h2>
<div class="d-flex gap-2">
<span class="badge text-body-secondary px-3 py-2 fw-medium">
@GetStatusText()
</span>
@if (model.IsDirty)
{
<span class="badge bg-warning text-dark px-3 py-2 fw-medium">
Unsaved Changes
</span>
}
</div>
</div>
</div>
</div>
<EditForm Model="@model" OnValidSubmit="OnValidBlogPostCreatedAsync">
<DataAnnotationsValidator />
<div class="row g-4">
<div class="col-lg-8">
<div class="card shadow-sm border-0 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h5 class="card-title mb-0 text-primary">Content</h5>
</div>
<div class="card-body p-0">
<div class="p-4 border-bottom">
<div class="form-floating">
<input type="text" class="form-control form-control-lg shadow-none fs-3 fw-semibold"
@oninput="args => model.Title = args.Value!.ToString()!"
value="@model.Title"/>
<label for="title" class="fw-bold">Post Title</label>
</div>
<ValidationMessage For="() => model.Title" class="text-danger small mt-1"></ValidationMessage>
</div>
<div class="p-4 border-bottom">
<label class="form-label fw-bold mb-3">Short Description</label>
<MarkdownTextArea Id="short" Class="form-control border-0 shadow-none"
Rows="4" Placeholder="Write a compelling summary..."
@bind-Value="@model.ShortDescription"
PreviewFunction="ReplaceShortCodes"></MarkdownTextArea>
<ValidationMessage For="() => model.ShortDescription" class="text-danger small mt-1"></ValidationMessage>
</div>
<div class="p-4 position-relative">
<label class="form-label fw-bold mb-3">
Content <span class="badge bg-secondary ms-2 fw-medium">@ReadingTimeCalculator.GetWordCount(model.Content) words</span>
<span class="badge bg-info ms-1 fw-medium">~@GetEstimatedReadTime() min read</span>
</label>
<MarkdownTextArea Id="content" Class="form-control border-0 shadow-none"
Rows="25" Placeholder="Start writing your amazing content..."
PreviewFunction="ReplaceShortCodes"
@bind-Value="@model.Content"></MarkdownTextArea>
<ValidationMessage For="() => model.Content" class="text-danger small mt-1"></ValidationMessage>
<div class="position-absolute top-0 end-0 m-3">
<div class="btn-group me-2">
<button class="btn btn-outline-secondary btn-sm dropdown-toggle" type="button"
data-bs-toggle="dropdown" aria-expanded="false">
Templates
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li>
<button type="button" class="dropdown-item" @onclick="OpenTemplateDialog">
Manage Templates
</button>
</li>
@if (blogPostTemplates.Any())
{
<li><hr class="dropdown-divider"></li>
<h6 class="dropdown-header">Load Template</h6>
@foreach (var template in blogPostTemplates)
{
<li>
<button type="button" class="dropdown-item" @onclick="() => LoadTemplate(template)">
@template.Name
</button>
</li>
}
}
</ul>
</div>
<div class="btn-group">
<button class="btn btn-outline-primary btn-sm dropdown-toggle" type="button"
data-bs-toggle="dropdown" aria-expanded="false">
Tools
</button>
<ul class="dropdown-menu dropdown-menu-end">
@if (shortCodes.Count > 0)
{
<li>
<button type="button" @onclick="OpenShortCodeDialog" class="dropdown-item">
Insert Shortcode
</button>
</li>
<li><hr class="dropdown-divider"></li>
}
<li>
<button type="button" class="dropdown-item" @onclick="FeatureDialog.Open">
Experimental Features
</button>
</li>
<li>
<button id="convert" type="button" class="dropdown-item" @onclick="ConvertContent">
@ConvertLabel <i class="lab"></i>
</button>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card shadow-sm border-0 mb-4 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h6 class="card-title mb-0 text-primary">Quick Actions</h6>
</div>
<div class="card-body">
<div class="d-grid">
<button class="btn btn-primary fw-medium transition" type="submit" disabled="@(!canSubmit)">
@(BlogPost != null ? "Update Post" : "Create Post")
</button>
</div>
</div>
</div>
<div class="card shadow-sm border-0 mb-4 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h6 class="card-title mb-0 text-primary">Tags</h6>
</div>
<div class="card-body">
<div class="form-floating">
<InputText type="text" class="form-control" id="tags" @bind-Value="model.Tags" />
<label for="tags">Tags (comma separated)</label>
</div>
<small class="form-text text-muted mt-2">
Add relevant tags to improve content discoverability and SEO. Use specific, searchable terms that describe your content.
</small>
@if (!string.IsNullOrWhiteSpace(model.Tags))
{
<div class="mt-2">
@foreach (var tag in model.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
<span class="badge bg-secondary me-1 mb-1">@tag.Trim()</span>
}
</div>
}
</div>
</div>
<div class="card shadow-sm border-0 mb-4 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h6 class="card-title mb-0 text-primary">Publishing & Visibility</h6>
</div>
<div class="card-body">
<div class="form-check form-switch mb-4">
<InputCheckbox class="form-check-input" id="published" @bind-Value="model.IsPublished" />
<label class="form-check-label fw-bold" for="published">
@(model.IsPublished ? "Published" : "Draft")
</label>
<small class="form-text text-muted d-block">
@(model.IsPublished ? "Visible to everyone" : "Only visible to you")
</small>
</div>
<ValidationMessage For="() => model.IsPublished" class="text-danger small mb-3"></ValidationMessage>
<div class="mb-4">
<label class="form-label fw-bold">Schedule Publication</label>
<div class="form-floating">
<InputDate Type="InputDateType.DateTimeLocal" class="form-control" id="scheduled"
placeholder="Scheduled Publish Date" @bind-Value="model.ScheduledPublishDate"
@bind-Value:after="@(() => model.IsPublished &= !IsScheduled)" />
<label for="scheduled">Select date and time</label>
</div>
@if (IsScheduled)
{
<div class="alert alert-info mt-2 mb-0">
<small>Will be published on <strong>@model.ScheduledPublishDate?.ToString("MMMM dd, yyyy 'at' HH:mm")</strong></small>
</div>
}
<ValidationMessage For="() => model.ScheduledPublishDate" class="text-danger small mt-1"></ValidationMessage>
</div>
@if (BlogPost is not null && !IsScheduled)
{
<div class="form-check form-switch">
<InputCheckbox class="form-check-input" id="updatedate" @bind-Value="model.ShouldUpdateDate" />
<label class="form-check-label fw-bold" for="updatedate">Update Publication Date</label>
<small class="form-text text-muted d-block">
Update the post's publication date to now
</small>
</div>
}
</div>
</div>
<div class="card shadow-sm border-0 mb-4 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h6 class="card-title mb-0 text-primary">Featured Image</h6>
</div>
<div class="card-body">
<div class="mb-3">
<div class="form-floating">
<InputText type="url" class="form-control" id="preview" @bind-Value="model.PreviewImageUrl" />
<label for="preview">Primary Image URL</label>
</div>
<ValidationMessage For="() => model.PreviewImageUrl" class="text-danger small mt-1"></ValidationMessage>
</div>
<div class="mb-3">
<div class="form-floating">
<InputText type="url" class="form-control" id="fallback-preview" @bind-Value="model.PreviewImageUrlFallback" />
<label for="fallback-preview">Fallback Image URL</label>
</div>
<ValidationMessage For="() => model.PreviewImageUrlFallback" class="text-danger small mt-1"></ValidationMessage>
</div>
<small class="form-text text-muted">
The fallback image ensures compatibility across all platforms. Use JPG or PNG format for maximum support when your primary image is WebP or AVIF.
</small>
</div>
</div>
<div class="card shadow-sm border-0 hover-shadow-lg transition">
<div class="card-header border-bottom-0 py-3">
<h6 class="card-title mb-0 text-primary">Cache Management</h6>
</div>
<div class="card-body">
<div class="form-check form-switch mb-3">
<InputCheckbox class="form-check-input" id="invalidate-cache" @bind-Value="model.ShouldInvalidateCache" />
<label class="form-check-label fw-bold" for="invalidate-cache">Clear Cache</label>
<small class="form-text text-muted d-block">
Make this post visible immediately on the homepage
</small>
</div>
<div class="alert alert-info mb-0">
<small>Enable this to make your post immediately visible on the homepage after publishing.</small>
</div>
</div>
</div>
</div>
</div>
</EditForm>
</div>
<FeatureInfoDialog @ref="FeatureDialog"></FeatureInfoDialog>
<ShortCodeDialog @ref="ShortCodeDialog" ShortCodes="shortCodes"></ShortCodeDialog>
<AddTemplateDialog @ref="AddTemplateDialog" OnSave="SaveTemplate"></AddTemplateDialog>
<NavigationLock ConfirmExternalNavigation="@model.IsDirty" OnBeforeInternalNavigation="PreventNavigationWhenDirty"></NavigationLock>
@code {
[Parameter]
public BlogPost? BlogPost { get; set; }
[Parameter, EditorRequired]
public required string Title { get; set; }
[Parameter]
public EventCallback<BlogPost> OnBlogPostCreated { get; set; }
[Parameter]
public bool ClearAfterCreated { get; set; } = true;
private FeatureInfoDialog FeatureDialog { get; set; } = default!;
private ShortCodeDialog ShortCodeDialog { get; set; } = default!;
private AddTemplateDialog AddTemplateDialog { get; set; } = default!;
private CreateNewModel model = new();
private string? originalContent = null;
private bool IsContentConverted => !string.IsNullOrWhiteSpace(originalContent);
private string ConvertLabel => !IsContentConverted ? "Convert to markdown" : "Restore";
private bool canSubmit = true;
private IPagedList<ShortCode> shortCodes = PagedList<ShortCode>.Empty;
private IPagedList<BlogPostTemplate> blogPostTemplates = PagedList<BlogPostTemplate>.Empty;
private bool IsScheduled => model.ScheduledPublishDate.HasValue;
private string? authorName;
protected override async Task OnInitializedAsync()
{
shortCodes = await ShortCodeRepository.GetAllAsync();
blogPostTemplates = await TemplateRepository.GetAllAsync();
if (AppConfiguration.Value.UseMultiAuthorMode)
{
authorName = await CurrentUserService.GetDisplayNameAsync();
}
}
protected override void OnParametersSet()
{
if (BlogPost is null)
{
return;
}
model = CreateNewModel.FromBlogPost(BlogPost);
}
private string GetStatusText()
{
if (BlogPost != null)
{
return "Editing";
}
if (model.IsPublished)
{
return "Ready to Publish";
}
if (IsScheduled)
{
return "Scheduled";
}
return "Draft";
}
private int GetEstimatedReadTime()
{
if (string.IsNullOrWhiteSpace(model.Content))
return 1;
return ReadingTimeCalculator.CalculateReadingTime(model.Content);
}
private async Task OnValidBlogPostCreatedAsync()
{
canSubmit = false;
model.AuthorName = authorName;
await OnBlogPostCreated.InvokeAsync(model.ToBlogPost());
if (model.ShouldInvalidateCache)
{
await CacheInvalidator.ClearCacheAsync();
}
_ = InstantJobRegistry.RunInstantJob<SimilarBlogPostJob>(parameter: true);
ClearModel();
canSubmit = true;
}
private void ClearModel()
{
if (ClearAfterCreated)
{
model = new CreateNewModel();
}
}
private async Task PreventNavigationWhenDirty(LocationChangingContext context)
{
if (!model.IsDirty)
return;
var isConfirmed = await JSRuntime.InvokeAsync<bool>("confirm", "You have unsaved changes. Are you sure you want to continue?");
if (!isConfirmed)
{
context.PreventNavigation();
}
}
private Task<string> ReplaceShortCodes(string markdown)
{
foreach (var code in shortCodes)
{
markdown = markdown.Replace($"[[{code.Name}]]", code.MarkdownContent);
}
return Task.FromResult(MarkdownConverter.ToMarkupString(markdown).Value);
}
private void OpenShortCodeDialog()
{
ShortCodeDialog.Open();
StateHasChanged();
}
private async Task OpenTemplateDialog()
{
await AddTemplateDialog.Open();
}
private async Task SaveTemplate(string name)
{
var existingTemplate = blogPostTemplates.FirstOrDefault(t => t.Name == name);
if (existingTemplate != null)
{
existingTemplate.Update(name, model.Title, model.ShortDescription, model.Content);
await TemplateRepository.StoreAsync(existingTemplate);
ToastService.ShowSuccess($"Template {name} updated");
}
else
{
var template = BlogPostTemplate.Create(name, model.Title, model.ShortDescription, model.Content);
await TemplateRepository.StoreAsync(template);
ToastService.ShowSuccess($"Template {name} saved");
}
blogPostTemplates = await TemplateRepository.GetAllAsync();
StateHasChanged();
}
private void LoadTemplate(BlogPostTemplate template)
{
model.Title = template.Title;
model.ShortDescription = template.ShortDescription;
model.Content = template.Content;
StateHasChanged();
ToastService.ShowInfo($"Template {template.Name} loaded");
}
/// <summary>
/// Convert content from HTML to Markdown and viceversa
/// </summary>
private void ConvertContent()
{
if (IsContentConverted)
{
model.Content = originalContent!;
originalContent = null;
}
else
{
originalContent = model.Content;
var converter = new ReverseMarkdown.Converter();
model.Content = converter.Convert(model.Content);
}
}
}