Skip to content

Commit cd654a4

Browse files
committed
Fixed behavior of setting cursor after selection
1 parent 6c366d5 commit cd654a4

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<label for="short">Short Description</label>
2020
<textarea class="form-control" id="short" rows="4" @onkeyup="MarkShortDescription"
2121
@oninput="args => model.ShortDescription = args.Value.ToString()">@model.ShortDescription</textarea>
22-
<small for="short" class="form-text text-muted">You can use markdown to style your component.</small>
22+
<small for="short" class="form-text text-muted">You can use markdown to style your component</small>
2323
</div>
2424
<div class="mb-3">
2525
<label for="content">Content</label>
@@ -86,8 +86,6 @@
8686

8787
private FeatureInfoDialog FeatureDialog { get; set; }
8888

89-
private ElementReference reference;
90-
9189
private CreateNewModel model = new();
9290

9391
protected override void OnParametersSet()
@@ -123,11 +121,13 @@
123121
private async Task MarkShortDescription(KeyboardEventArgs keyboardEventArgs)
124122
{
125123
model.ShortDescription = await GetNewMarkdownForElementAsync(keyboardEventArgs, model.ShortDescription, "short");
124+
StateHasChanged();
126125
}
127126

128127
private async Task MarkContent(KeyboardEventArgs keyboardEventArgs)
129128
{
130129
model.Content = await GetNewMarkdownForElementAsync(keyboardEventArgs, model.Content, "content");
130+
StateHasChanged();
131131
}
132132

133133
private async Task<string> GetNewMarkdownForElementAsync(KeyboardEventArgs keyboardEventArgs, string original, string elementId)

src/LinkDotNet.Blog.Web/Shared/Services/MarkerService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public async Task<string> GetNewMarkdownForElementAsync(string elementId, string
2323
var beforeMarker = selectionRange.Start > 0 ? content[..selectionRange.Start] : string.Empty;
2424
var marker = content[selectionRange.Start..selectionRange.End];
2525
var afterMarker = content[selectionRange.End..];
26-
await jsRuntime.InvokeVoidAsync("setSelectionFromElement", elementId, selectionRange.Start + fenceBegin.Length);
26+
var shift = selectionRange.End + fenceBegin.Length + fenceEnd.Length;
27+
await jsRuntime.InvokeVoidAsync("setSelectionFromElement", elementId, shift);
2728
return beforeMarker + fenceBegin + marker + fenceEnd + afterMarker;
2829
}
2930
}

tests/LinkDotNet.Blog.UnitTests/Web/Shared/Admin/CreateNewBlogPostTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ public void ShouldAcceptInputWithoutLosingFocusOrEnter()
181181
[Theory]
182182
[InlineData("short", "b", true, "**", "**Test**")]
183183
[InlineData("short", "i", true, "*", "*Test*")]
184+
[InlineData("short", "h", true, "*", "Test")]
184185
[InlineData("short", "b", false, "**", "Test")]
185186
[InlineData("short", "f", false, "**", "Test")]
186187
[InlineData("content", "b", true, "**", "**Test**")]
187188
[InlineData("content", "i", true, "*", "*Test*")]
189+
[InlineData("content", "h", true, "*", "Test")]
188190
[InlineData("content", "b", false, "**", "Test")]
189191
[InlineData("content", "f", false, "**", "Test")]
190192
public void ShouldSetMarkerOnKeyUp(string id, string key, bool ctrlPressed, string fence, string expected)

tests/LinkDotNet.Blog.UnitTests/Web/Shared/Services/MarkerServiceTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public async Task ShouldSetCursorPosition()
3939
const string element = "id";
4040
JSInterop.Mode = JSRuntimeMode.Loose;
4141
JSInterop.Setup<SelectionRange>("getSelectionFromElement", element)
42-
.SetResult(new SelectionRange { Start = 1, End = 3 });
42+
.SetResult(new SelectionRange { Start = 2, End = 5 });
4343

44-
await cut.GetNewMarkdownForElementAsync(element, "Test", "**", "**");
44+
await cut.GetNewMarkdownForElementAsync(element, "Hello World", "**", "**");
4545

4646
var setSelection = JSInterop.Invocations.SingleOrDefault(s => s.Identifier == "setSelectionFromElement");
4747
setSelection.Arguments.Should().Contain(element);
48-
setSelection.Arguments.Should().Contain(3);
48+
setSelection.Arguments.Should().Contain(9);
4949
}
5050
}

0 commit comments

Comments
 (0)