-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlazorMergelyComponent.razor.cs
132 lines (111 loc) · 4.27 KB
/
BlazorMergelyComponent.razor.cs
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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Threading.Tasks;
namespace com.github.akovac35.BlazorMergely
{
public partial class BlazorMergelyComponent : ComponentBase
{
[Inject]
protected IJSRuntime IJSRuntimeInstance { get; private set; } = null!;
/// <summary>
/// Use this property for Mergely initialization. E.g. "{\"cmsettings\":{\"mode\":\"text/javascript\",\"readOnly\":false,\"styleSelectedText\": true}}"
/// Changing this property after the first render will have no effect.
/// </summary>
[Parameter]
public string? MergelyOptions { get; set; }
/// <summary>
/// Component title.
/// </summary>
[Parameter]
public string? Title { get; set; }
[Parameter]
public string HtmlId { get; set; } = Guid.NewGuid().ToString();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
await Init();
}
}
protected virtual ValueTask Init()
{
return IJSRuntimeInstance.InvokeVoidAsync("BlazorMergely.init", $"#{HtmlId}", MergelyOptions!);
}
/// <summary>
/// Changes the Code Mirror language display mode.
/// </summary>
/// <param name="modeStr">E.g. text/javascript, js, javascript</param>
public ValueTask ChangeLangDisplayModeAsync(string? modeStr)
{
return IJSRuntimeInstance.InvokeVoidAsync("BlazorMergely.changeLangDisplayMode", $"#{HtmlId}", modeStr!);
}
/// <summary>
/// Gets Mergely options.
/// </summary>
public ValueTask<string> GetOptionsAsync()
{
return IJSRuntimeInstance.InvokeAsync<string>("BlazorMergely.getOptions", $"#{HtmlId}");
}
/// <summary>
/// Sets Mergely options.
/// </summary>
public ValueTask SetOptionsAsync(string optionsStr)
{
if (optionsStr is null)
{
throw new ArgumentNullException(nameof(optionsStr));
}
return IJSRuntimeInstance.InvokeVoidAsync("BlazorMergely.setOptions", $"#{HtmlId}", optionsStr);
}
/// <summary>
/// Clears value.
/// </summary>
public ValueTask ClearAsync()
{
return IJSRuntimeInstance.InvokeVoidAsync("BlazorMergely.clear", $"#{HtmlId}");
}
/// <summary>
/// Gets value.
/// </summary>
public ValueTask<string?> GetValueAsync(MergelySide side)
{
return IJSRuntimeInstance.InvokeAsync<string?>("BlazorMergely.get", $"#{HtmlId}", (side == MergelySide.LEFT ? "lhs" : "rhs"));
}
/// <summary>
/// Gets left and right values.
/// </summary>
public async ValueTask<(string? Left, string? Right)> GetValuesAsync()
{
var arr = await IJSRuntimeInstance.InvokeAsync<string?[]>("BlazorMergely.getBoth", $"#{HtmlId}");
return (arr[0], arr[1]);
}
/// <summary>
/// Sets value.
/// </summary>
public ValueTask SetValueAsync(string? value, MergelySide side)
{
return IJSRuntimeInstance.InvokeVoidAsync($"BlazorMergely.{(side == MergelySide.LEFT ? "lhs" : "rhs")}", $"#{HtmlId}", value ?? "");
}
/// <summary>
/// Sets value.
/// </summary>
public ValueTask SetValueAsync(string? lhsValue, string? rhsValue)
{
return IJSRuntimeInstance.InvokeVoidAsync($"BlazorMergely.set", $"#{HtmlId}", lhsValue ?? "", rhsValue ?? "");
}
/// <summary>
/// Triggers Mergely update. This method does not need to be invoked if the Mergely autoupdate property is set to true.
/// </summary>
public ValueTask UpdateAsync()
{
return IJSRuntimeInstance.InvokeVoidAsync("BlazorMergely.update", $"#{HtmlId}");
}
public enum MergelySide
{
LEFT = 0,
RIGHT = 1
}
}
}