-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathMarkdownTextArea.razor
More file actions
415 lines (378 loc) · 14.9 KB
/
Copy pathMarkdownTextArea.razor
File metadata and controls
415 lines (378 loc) · 14.9 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
@using System.IO
@using System.Linq
@using LinkDotNet.Blog.Web.Features.Services.FileUpload
@using IToastService = Blazored.Toast.Services.IToastService
@inject IBlobUploadService BlobUploadService
@inject IToastService ToastService
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable
<div id="@Id" class="markdown-editor-container @Class">
<div class="markdown-toolbar">
<button type="button" class="toolbar-btn" title="@GetShortcutText("Undo", "Z")" aria-label="@GetShortcutText("Undo", "Z")" @onclick="Undo" disabled="@(!canUndo)">
<i class="bi bi-arrow-counterclockwise"></i>
</button>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Redo", "Y")" aria-label="@GetShortcutText("Redo", "Y")" @onclick="Redo" disabled="@(!canRedo)">
<i class="bi bi-arrow-clockwise"></i>
</button>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Bold", "B")" aria-label="@GetShortcutText("Bold", "B")" @onclick="InsertBold">
<i class="bi bi-type-bold"></i>
</button>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Italic", "I")" aria-label="@GetShortcutText("Italic", "I")" @onclick="InsertItalic">
<i class="bi bi-type-italic"></i>
</button>
<button type="button" class="toolbar-btn" title="Heading 1" aria-label="Heading 1" @onclick="InsertH1">
<i class="bi bi-type-h1"></i>
</button>
<button type="button" class="toolbar-btn" title="Heading 2" aria-label="Heading 2" @onclick="InsertH2">
<i class="bi bi-type-h2"></i>
</button>
<button type="button" class="toolbar-btn" title="Heading 3" aria-label="Heading 3" @onclick="InsertH3">
<i class="bi bi-type-h3"></i>
</button>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Quote", "⇧.")" aria-label="@GetShortcutText("Quote", "⇧.")" @onclick="InsertQuote">
<i class="bi bi-quote"></i>
</button>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Code", "E")" aria-label="@GetShortcutText("Code", "E")" @onclick="InsertCode">
<i class="bi bi-code"></i>
</button>
<button type="button" class="toolbar-btn" title="Code Block" aria-label="Code Block" @onclick="InsertCodeBlock">
<i class="bi bi-code-square"></i>
</button>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Horizontal Rule", "⇧H")" aria-label="@GetShortcutText("Horizontal Rule", "⇧H")" @onclick="InsertHorizontalRule">
<i class="bi bi-hr"></i>
</button>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn" title="@GetShortcutText("Link", "K")" @onclick="InsertLink">
<i class="bi bi-link-45deg"></i>
</button>
<button type="button" class="toolbar-btn" title="Image" @onclick="ShowImageUpload">
<i class="bi bi-image"></i>
</button>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn" title="Unordered List" @onclick="InsertUnorderedList">
<i class="bi bi-list-ul"></i>
</button>
<button type="button" class="toolbar-btn" title="Ordered List" @onclick="InsertOrderedList">
<i class="bi bi-list-ol"></i>
</button>
<button type="button" class="toolbar-btn" title="Task List" @onclick="InsertTaskList">
<i class="bi bi-list-check"></i>
</button>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn" title="Strikethrough" @onclick="InsertStrikethrough">
<i class="bi bi-type-strikethrough"></i>
</button>
<button type="button" class="toolbar-btn" title="Superscript" @onclick="InsertSuperscript">
<i class="bi bi-superscript"></i>
</button>
<button type="button" class="toolbar-btn" title="Subscript" @onclick="InsertSubscript">
<i class="bi bi-subscript"></i>
</button>
<div class="toolbar-divider"></div>
<div class="table-btn-wrapper">
<button type="button" class="toolbar-btn @(showTablePicker ? "active" : "")" title="Table" @onclick="ToggleTablePicker">
<i class="bi bi-table"></i>
</button>
@if (showTablePicker)
{
<div class="table-popover-backdrop" @onclick="ToggleTablePicker"></div>
<div class="table-popover">
<div class="table-popover-header">Insert Table</div>
<label class="table-popover-field">
<span>Columns</span>
<input type="number" min="1" max="10" @bind="tableCols" />
</label>
<label class="table-popover-field">
<span>Rows</span>
<input type="number" min="1" max="20" @bind="tableRows" />
</label>
<button type="button" class="btn btn-sm btn-primary" @onclick="() => InsertTable(tableCols, tableRows)">Insert</button>
</div>
}
</div>
<div class="toolbar-divider"></div>
<button type="button" class="toolbar-btn @(viewMode == ViewMode.Preview ? "active" : "")" title="@GetShortcutText("Preview", "⇧P")" @onclick="TogglePreview">
<i class="bi bi-eye"></i>
</button>
</div>
<div class="editor-content" style="height: @Height">
@if (viewMode == ViewMode.Preview)
{
<div class="markdown-preview preview-only">
@((MarkupString)previewHtml)
</div>
}
else
{
<textarea @ref="textAreaRef"
class="markdown-textarea"
placeholder="@Placeholder"
@bind="Value"
@bind:event="oninput"
@bind:after="OnInputAsync"></textarea>
}
</div>
</div>
<UploadFileModalDialog @ref="UploadDialog"></UploadFileModalDialog>
<InputFile @ref="inputFileRef" style="display: none;" accept="image/*" OnChange="HandleFileSelected" multiple />
@code {
private enum ViewMode { Editor, Preview }
public sealed class UndoRedoState
{
public bool CanUndo { get; set; }
public bool CanRedo { get; set; }
}
private string Height => $"{Rows * 25}px";
private ElementReference textAreaRef;
private InputFile inputFileRef = default!;
private UploadFileModalDialog UploadDialog { get; set; } = default!;
private ViewMode viewMode = ViewMode.Editor;
private string previewHtml = string.Empty;
private DotNetObjectReference<MarkdownTextArea>? dotNetHelper;
private bool isMac;
private bool canUndo;
private bool canRedo;
private bool showTablePicker;
private int tableCols = 2;
private int tableRows = 2;
private const long MaxFileSizeBytes = 512 * 1024; // 512 KB
#pragma warning disable BL0007
[Parameter]
public string Value
{
get;
set
{
if (field != value)
{
field = value;
_ = InvokeAsync(async () =>
{
await ValueChanged.InvokeAsync(value);
await UpdatePreview();
});
}
}
} = string.Empty;
#pragma warning restore
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
[Parameter] public string Class { get; set; } = string.Empty;
[Parameter] public string Id { get; set; } = string.Empty;
[Parameter]
public int Rows { get; set; }
[Parameter] public string Placeholder { get; set; } = string.Empty;
[Parameter] public Func<string, Task<string>> PreviewFunction { get; set; } =
s => Task.FromResult(MarkdownConverter.ToMarkupString(s).Value);
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
try
{
isMac = await JSRuntime.InvokeAsync<bool>("markdownEditor.isMac");
dotNetHelper = DotNetObjectReference.Create(this);
}
catch (JSException)
{
canUndo = false;
canRedo = false;
return;
}
}
if (viewMode == ViewMode.Editor && dotNetHelper is not null)
{
try
{
await JSRuntime.InvokeVoidAsync("markdownEditor.setupKeyboardShortcuts", textAreaRef, dotNetHelper);
}
catch (JSException)
{
// Keyboard shortcuts unavailable; toolbar buttons remain functional
}
}
}
[JSInvokable]
public async Task HandleKeyboardShortcut(string action)
{
switch (action)
{
case "bold":
await InsertBold();
break;
case "italic":
await InsertItalic();
break;
case "code":
await InsertCode();
break;
case "link":
await InsertLink();
break;
case "quote":
await InsertQuote();
break;
case "hr":
await InsertHorizontalRule();
break;
case "preview":
await TogglePreview();
break;
}
}
private string GetShortcutText(string action, string key)
{
var modifier = isMac ? "⌘" : "Ctrl";
return $"{action} ({modifier}+{key})";
}
private async Task OnInputAsync()
{
await UpdateUndoRedoState();
}
private async Task UpdatePreview()
{
if (viewMode == ViewMode.Preview)
{
previewHtml = await PreviewFunction(Value);
StateHasChanged();
await InvokeAsync(async () =>
{
await JSRuntime.InvokeVoidAsync("markdownEditor.highlightCodeBlocks");
});
}
}
private async Task TogglePreview()
{
viewMode = viewMode == ViewMode.Editor ? ViewMode.Preview : ViewMode.Editor;
if (viewMode == ViewMode.Preview)
{
await UpdatePreview();
}
}
private async Task UpdateUndoRedoState()
{
try
{
var state = await JSRuntime.InvokeAsync<UndoRedoState>("markdownEditor.getUndoRedoState", textAreaRef);
canUndo = state.CanUndo;
canRedo = state.CanRedo;
}
catch
{
canUndo = !string.IsNullOrEmpty(Value);
canRedo = false;
}
}
private async Task Undo()
{
await JSRuntime.InvokeVoidAsync("markdownEditor.undo", textAreaRef);
await UpdateUndoRedoState();
}
private async Task Redo()
{
await JSRuntime.InvokeVoidAsync("markdownEditor.redo", textAreaRef);
await UpdateUndoRedoState();
}
private Task InsertBold() => InsertMarkdown("**", "**");
private Task InsertItalic() => InsertMarkdown("*", "*");
private Task InsertH1() => InsertMarkdown("# ", "");
private Task InsertH2() => InsertMarkdown("## ", "");
private Task InsertH3() => InsertMarkdown("### ", "");
private Task InsertQuote() => InsertLinePrefixMarkdown("> ");
private Task InsertCode() => InsertMarkdown("`", "`");
private Task InsertCodeBlock() => InsertMarkdown("```\n", "\n```");
private Task InsertLink() => InsertMarkdown("[", "](url)");
private Task InsertUnorderedList() => InsertMarkdown("- ", "");
private Task InsertOrderedList() => InsertMarkdown("1. ", "");
private Task InsertStrikethrough() => InsertMarkdown("~~", "~~");
private Task InsertSuperscript() => InsertMarkdown("<sup>", "</sup>");
private Task InsertSubscript() => InsertMarkdown("<sub>", "</sub>");
private Task InsertTaskList() => InsertMarkdown("- [ ] ", "");
private void ToggleTablePicker() => showTablePicker = !showTablePicker;
private async Task InsertLinePrefixMarkdown(string prefix)
{
try
{
await JSRuntime.InvokeVoidAsync("markdownEditor.insertLinePrefixes", textAreaRef, prefix);
await textAreaRef.FocusAsync();
}
catch
{
Value += prefix;
}
}
private async Task InsertHorizontalRule()
{
try
{
await JSRuntime.InvokeVoidAsync("markdownEditor.insertHorizontalRule", textAreaRef);
await textAreaRef.FocusAsync();
}
catch
{
Value += "\n\n---\n\n";
}
}
private async Task InsertTable(int cols, int rows)
{
showTablePicker = false;
var emptyRow = "| " + string.Join(" | ", Enumerable.Repeat("", cols)) + " |";
var separator = "| " + string.Join(" | ", Enumerable.Repeat("---", cols)) + " |";
var table = string.Join("\n", new[] { emptyRow, separator }.Concat(Enumerable.Repeat(emptyRow, rows)));
await InsertMarkdown(table, "");
}
private async Task InsertMarkdown(string prefix, string suffix)
{
try
{
await JSRuntime.InvokeVoidAsync("markdownEditor.insertText", textAreaRef, prefix, suffix);
await textAreaRef.FocusAsync();
}
catch
{
Value += prefix + suffix;
}
}
private async Task ShowImageUpload()
{
await JSRuntime.InvokeVoidAsync("markdownEditor.clickInputFile", inputFileRef.Element);
}
public async ValueTask DisposeAsync()
{
dotNetHelper?.Dispose();
}
private async Task HandleFileSelected(InputFileChangeEventArgs e)
{
foreach (var file in e.GetMultipleFiles())
{
try
{
if (file.Size > MaxFileSizeBytes)
{
ToastService.ShowError($"File '{file.Name}' exceeds the maximum allowed size of 512 KB.");
continue;
}
using var memoryStream = new MemoryStream();
await file.OpenReadStream(maxAllowedSize: MaxFileSizeBytes).CopyToAsync(memoryStream);
memoryStream.Position = 0;
var options = await UploadDialog.ShowAsync(file.Name);
if (options is null)
{
continue;
}
var url = await BlobUploadService.UploadFileAsync(options.Name, memoryStream, new UploadOptions
{
SetCacheControlHeader = options.CacheMedia,
});
await InsertMarkdown($"", "");
ToastService.ShowSuccess($"Successfully uploaded {file.Name}");
}
catch (Exception ex)
{
ToastService.ShowError($"Error while uploading file: {ex.Message}");
}
}
}
}