Skip to content

Commit 0d37bf8

Browse files
committed
Added upload component to upload markdown files
1 parent eb774ea commit 0d37bf8

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

src/LinkDotNet.Blog.Web/Shared/Admin/CreateNewBlogPost.razor

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
<label for="short">Short Description</label>
1818
<InputTextArea class="form-control" id="short" @bind-Value="model.ShortDescription" rows="4"
1919
@oninput="args => model.ShortDescription = args.Value.ToString()"/>
20-
<small id="short" class="form-text text-muted">You can use markdown to style your component.</small>
20+
<small for="short" class="form-text text-muted">You can use markdown to style your component.</small>
2121
</div>
2222
<div class="mb-3">
2323
<label for="content">Content</label>
2424
<InputTextArea class="form-control" id="content" @bind-Value="model.Content" @oninput="args => model.Content = args.Value.ToString()" rows="10" />
25-
<small id="content" class="form-text text-muted">You can use markdown to style your component. Additional features are listed <a @onclick="@(() => FeatureDialog.Open())">here</a></small>
26-
@* <small id="content" class="form-text text-muted">Drag and drop images to upload and insert picture.</small> *@
25+
<small for="content" class="form-text text-muted">You can use markdown to style your component. Additional features are listed <a @onclick="@(() => FeatureDialog.Open())">here</a></small>
26+
<UploadFile OnFileUploaded="SetContentFromFile" id="content-upload"></UploadFile>
27+
<small for="content-upload" class="form-text text-muted">Drag and drop markdown files to upload and
28+
insert them</small>
2729
</div>
2830
<div class="mb-3">
2931
<label for="preview">Preview-Url</label>
@@ -32,7 +34,7 @@
3234
<div class="form-check">
3335
<InputCheckbox class="form-check-input" id="published" @bind-Value="model.IsPublished" />
3436
<label class="form-check-label" for="published">Publish</label><br/>
35-
<small id="published" class="form-text text-muted">If this blog post is only draft uncheck the box</small>
37+
<small for="published" class="form-text text-muted">If this blog post is only draft uncheck the box</small>
3638
</div>
3739
<div class="mb-3">
3840
<label for="tags">Tags</label>
@@ -43,7 +45,8 @@
4345
<div class="form-check">
4446
<InputCheckbox class="form-check-input" id="updatedate" @bind-Value="model.ShouldUpdateDate" />
4547
<label class="form-check-label" for="updatedate">Update Publish Date?</label><br/>
46-
<small id="updatedate" class="form-text text-muted">If set the publish date is set to now, otherwise its original date</small>
48+
<small for="updatedate" class="form-text text-muted">If set the publish date is set to now,
49+
otherwise its original date</small>
4750
</div>
4851
}
4952
<button class="btn btn-primary" type="submit">Submit</button>
@@ -68,10 +71,10 @@
6871
@code {
6972
[Parameter]
7073
public BlogPost BlogPost { get; set; }
71-
74+
7275
[Parameter]
7376
public string Title { get; set; }
74-
77+
7578
[Parameter]
7679
public EventCallback<BlogPost> OnBlogPostCreated { get; set; }
7780

@@ -97,12 +100,17 @@
97100
await OnBlogPostCreated.InvokeAsync(model.ToBlogPost());
98101
ClearModel();
99102
}
100-
103+
101104
private void ClearModel()
102105
{
103106
if (ClearAfterCreated)
104107
{
105108
model = new CreateNewModel();
106109
}
107110
}
111+
112+
private void SetContentFromFile(string content)
113+
{
114+
model.Content = content;
115+
}
108116
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@using System.IO
2+
@inject IJSRuntime jsRuntime
3+
4+
<div>
5+
<InputFile
6+
class="@CssClass"
7+
@ondragenter="@(_ => dropClass = "can-drop")"
8+
@ondragleave="@(_ => dropClass = string.Empty)"
9+
id="@id"
10+
OnChange="HandleFileUpload"/>
11+
</div>
12+
13+
@code {
14+
private string dropClass = string.Empty;
15+
private string otherClasses = string.Empty;
16+
private string CssClass => dropClass + " " + otherClasses;
17+
private string id = string.Empty;
18+
19+
[Parameter(CaptureUnmatchedValues = true)]
20+
public IDictionary<string, object> AdditionalAttributes { get; set; }
21+
22+
[Parameter]
23+
public EventCallback<string> OnFileUploaded { get; set; }
24+
25+
protected override void OnParametersSet()
26+
{
27+
if (AdditionalAttributes != null && AdditionalAttributes.ContainsKey("class"))
28+
{
29+
otherClasses = AdditionalAttributes["class"].ToString();
30+
}
31+
32+
if (AdditionalAttributes != null && AdditionalAttributes.ContainsKey("id"))
33+
{
34+
id = AdditionalAttributes["id"].ToString();
35+
}
36+
}
37+
38+
private async Task HandleFileUpload(InputFileChangeEventArgs args)
39+
{
40+
await using var stream = args.File.OpenReadStream();
41+
var reader = new StreamReader(stream);
42+
var content = await reader.ReadToEndAsync();
43+
await OnFileUploaded.InvokeAsync(content);
44+
}
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
::deep .can-drop {
2+
border: 2px dashed green;
3+
}

0 commit comments

Comments
 (0)