|
| 1 | +@using OsayamiBlog.Services |
| 2 | +@inject AnnotationRegistry Registry |
| 3 | + |
| 4 | +<div @onmouseup="CaptureSelection"> |
| 5 | + <ParagraphRenderer Paragraph="Paragraph" /> |
| 6 | +</div> |
| 7 | + |
| 8 | +@if (_selection is not null) |
| 9 | +{ |
| 10 | + @if (_selection is not null) |
| 11 | +{ |
| 12 | + <button @onclick="OpenDialog">Annotate</button> |
| 13 | +} |
| 14 | + |
| 15 | +@if (_showDialog) |
| 16 | +{ |
| 17 | + <CreateAnnotationDialog |
| 18 | + OnConfirm="CreateAnnotation" |
| 19 | + OnCancel="CloseDialog" /> |
| 20 | +} |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | +@code { |
| 25 | + private bool _showDialog; |
| 26 | +private AnnotationDraft? _pendingDraft; |
| 27 | + |
| 28 | + [Parameter, EditorRequired] |
| 29 | +public string CurrentPostSlug { get; set; } = ""; |
| 30 | + |
| 31 | + [Inject] IJSRuntime JS { get; set; } = default!; |
| 32 | + |
| 33 | + [Parameter, EditorRequired] |
| 34 | + public Paragraph Paragraph { get; set; } = default!; |
| 35 | + |
| 36 | + private TextSelection? _selection; |
| 37 | + |
| 38 | + private async Task CaptureSelection() |
| 39 | + { |
| 40 | + _selection = await JS.InvokeAsync<TextSelection?>("getSelectionText"); |
| 41 | + } |
| 42 | + |
| 43 | + private void CreateAnnotation(AnnotationDraft draft) |
| 44 | +{ |
| 45 | + if (_selection is null) return; |
| 46 | + |
| 47 | + var annotation = new Annotation |
| 48 | + { |
| 49 | + Label = draft.Label, |
| 50 | + Body = draft.Body |
| 51 | + }; |
| 52 | + |
| 53 | + var location = new AnnotationLocation |
| 54 | + { |
| 55 | + PostSlug = CurrentPostSlug, |
| 56 | + ParagraphId = Paragraph.Id, |
| 57 | + Start = _selection.StartOffset, |
| 58 | + Length = _selection.EndOffset - _selection.StartOffset |
| 59 | + }; |
| 60 | + |
| 61 | + Registry.Register(annotation, location); |
| 62 | + |
| 63 | + var id = Registry.All.Last().Id; |
| 64 | + |
| 65 | + Paragraph.Annotations.Add(new InlineAnnotation |
| 66 | + { |
| 67 | + AnnotationId = id, |
| 68 | + Start = location.Start, |
| 69 | + Length = location.Length |
| 70 | + }); |
| 71 | + |
| 72 | + _showDialog = false; |
| 73 | + _selection = null; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +private void OpenDialog() |
| 78 | +{ |
| 79 | + _showDialog = true; |
| 80 | +} |
| 81 | + |
| 82 | +private void CloseDialog() |
| 83 | +{ |
| 84 | + _showDialog = false; |
| 85 | + _selection = null; |
| 86 | +} |
| 87 | + |
| 88 | +} |
0 commit comments