Skip to content

Commit 3c5a20b

Browse files
committed
Set Cursor Position
1 parent 99bebe3 commit 3c5a20b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace LinkDotNet.Blog.Web.Shared.Services;
55

6-
public partial class MarkerService : IMarkerService
6+
public class MarkerService : IMarkerService
77
{
88
private readonly IJSRuntime jsRuntime;
99

@@ -23,6 +23,7 @@ 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);
2627
return beforeMarker + fenceBegin + marker + fenceEnd + afterMarker;
2728
}
2829
}

src/LinkDotNet.Blog.Web/wwwroot/components/selection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ window.getSelectionFromElement = function (id) {
33
const start = elem.selectionStart
44
const end = elem.selectionEnd
55
return { start, end }
6+
}
7+
8+
window.setSelectionFromElement = function (id, cursor) {
9+
const elem = document.getElementById(id)
10+
elem.selectionStart = cursor
11+
elem.selectionEnd = cursor
612
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System.Threading.Tasks;
23
using Bunit;
34
using FluentAssertions;
@@ -23,11 +24,27 @@ public MarkerServiceTests()
2324
public async Task ShouldMarkString(string source, int startSelect, int endSelect, string fence, string expected)
2425
{
2526
const string element = "id";
27+
JSInterop.Mode = JSRuntimeMode.Loose;
2628
JSInterop.Setup<SelectionRange>("getSelectionFromElement", element)
2729
.SetResult(new SelectionRange { Start = startSelect, End = endSelect });
2830

2931
var actual = await cut.GetNewMarkdownForElementAsync(element, source, fence, fence);
3032

3133
actual.Should().Be(expected);
3234
}
35+
36+
[Fact]
37+
public async Task ShouldSetCursorPosition()
38+
{
39+
const string element = "id";
40+
JSInterop.Mode = JSRuntimeMode.Loose;
41+
JSInterop.Setup<SelectionRange>("getSelectionFromElement", element)
42+
.SetResult(new SelectionRange { Start = 1, End = 3 });
43+
44+
await cut.GetNewMarkdownForElementAsync(element, "Test", "**", "**");
45+
46+
var setSelection = JSInterop.Invocations.SingleOrDefault(s => s.Identifier == "setSelectionFromElement");
47+
setSelection.Arguments.Should().Contain(element);
48+
setSelection.Arguments.Should().Contain(3);
49+
}
3350
}

0 commit comments

Comments
 (0)