-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathDemo.razor.cs
More file actions
117 lines (89 loc) · 3.11 KB
/
Demo.razor.cs
File metadata and controls
117 lines (89 loc) · 3.11 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
namespace BlazorBootstrap.Demo.RCL;
public partial class Demo : BlazorBootstrapComponentBase
{
#region Fields and Constants
private IconColor clipboardTooltipIconColor = IconColor.None;
private IconName clipboardTooltipIconName = IconName.Clipboard;
private string? clipboardTooltipTitle = "Copy to clipboard";
/// <summary>
/// A reference to this component instance for use in JavaScript calls.
/// </summary>
private DotNetObjectReference<Demo> objRef = default!;
private string? snippet;
#endregion
#region Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
await SafeInvokeVoidAsync("highlightCode");
await base.OnAfterRenderAsync(firstRender);
}
protected override async Task OnInitializedAsync()
{
objRef ??= DotNetObjectReference.Create(this);
await base.OnInitializedAsync();
}
protected override async Task OnParametersSetAsync()
{
if (snippet is null)
{
var resourceFullName = Type.FullName + ".razor";
using (var stream = Type.Assembly.GetManifestResourceStream(resourceFullName)!)
{
try
{
if (stream is null)
return;
using (var reader = new StreamReader(stream))
{
snippet = await reader.ReadToEndAsync();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
/// <summary>
/// Handles a copy error event from JavaScript.
/// </summary>
/// <param name="errorMessage">The error message.</param>
[JSInvokable]
public void OnCopyFailJS(string errorMessage)
{
// TODO: show message
}
/// <summary>
/// Handles a copy success event from JavaScript.
/// </summary>
[JSInvokable]
public void OnCopySuccessJS()
{
clipboardTooltipTitle = "Copied!";
clipboardTooltipIconName = IconName.Check2;
clipboardTooltipIconColor = IconColor.Success;
StateHasChanged();
}
/// <summary>
/// Handles a copy status reset event from JavaScript.
/// </summary>
[JSInvokable]
public void ResetCopyStatusJS()
{
clipboardTooltipTitle = "Copy to clipboard";
clipboardTooltipIconName = IconName.Clipboard;
clipboardTooltipIconColor = IconColor.None;
StateHasChanged();
}
private async Task CopyToClipboardAsync() => await SafeInvokeVoidAsync("copyToClipboard", snippet, objRef);
#endregion
#region Properties, Indexers
protected override string? ClassNames => BuildClassNames(Class, ("bd-example-snippet bd-code-snippet", true));
[Parameter] public LanguageCode LanguageCode { get; set; } = LanguageCode.Razor;
[Parameter] public bool ShowCodeOnly { get; set; }
[Parameter] public bool Tabs { get; set; } = false;
[Parameter] public Type Type { get; set; } = default!;
#endregion
}