-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMarkdown.razor.cs
More file actions
163 lines (140 loc) · 4.69 KB
/
Markdown.razor.cs
File metadata and controls
163 lines (140 loc) · 4.69 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Components;
using System.Globalization;
using System.Reflection.Metadata;
namespace BootstrapBlazor.Components;
/// <summary>
/// Markdown 组件
/// </summary>
[JSModuleAutoLoader("./_content/BootstrapBlazor.Markdown/Components/Markdown/Markdown.razor.js", JSObjectReference = true)]
public partial class Markdown : IAsyncDisposable
{
/// <summary>
/// 获得/设置 控件高度,默认300px
/// </summary>
[Parameter]
public int Height { get; set; } = 300;
/// <summary>
/// 获得/设置 控件最小高度,默认200px
/// </summary>
[Parameter]
public int MinHeight { get; set; } = 200;
/// <summary>
/// 获得/设置 初始化时显示的界面,markdown 界面,所见即所得界面
/// </summary>
[Parameter]
public InitialEditType InitialEditType { get; set; }
/// <summary>
/// 获得/设置 预览模式,Tab 页预览,分栏预览 默认分栏预览 Vertical
/// </summary>
[Parameter]
public PreviewStyle PreviewStyle { get; set; }
/// <summary>
/// 获得/设置 语言,默认为简体中文,如果改变,需要自行引入语言包
/// </summary>
[Parameter]
public string? Language { get; set; }
/// <summary>
/// 获得/设置 提示信息
/// </summary>
[Parameter]
public string? Placeholder { get; set; }
/// <summary>
/// 获得/设置 组件 Html 代码
/// </summary>
[Parameter]
public string? Html { get; set; }
/// <summary>
/// 获得/设置 组件 Html 代码回调
/// </summary>
[Parameter]
public EventCallback<string> HtmlChanged { get; set; }
/// <summary>
/// 获取/设置 组件是否为浏览器模式
/// </summary>
[Parameter]
public bool? IsViewer { get; set; }
/// <summary>
/// 获取/设置 组件是否为为暗黑主题,默认为false
/// </summary>
[Parameter]
public bool IsDark { get; set; } = false;
/// <summary>
/// 启用代码高亮插件,需引入对应的css js,默认为false
/// </summary>
[Parameter]
public bool EnableHighlight { get; set; } = false;
private MarkdownOption Option { get; } = new();
/// <summary>
/// 获得 组件样式
/// </summary>
protected string? GetClassString() => CssBuilder.Default()
.AddClass(CssClass).AddClass(ValidCss)
.Build();
/// <summary>
/// OnInitialized 方法
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
Option.PreviewStyle = PreviewStyle.ToDescriptionString();
Option.InitialEditType = InitialEditType.ToDescriptionString();
Option.Language = Language ?? CultureInfo.CurrentUICulture.Name;
Option.Placeholder = Placeholder;
Option.Height = $"{Height}px";
Option.MinHeight = $"{MinHeight}px";
Option.InitialValue = Value ?? "";
Option.Viewer = IsViewer;
Option.Theme = IsDark ? "dark" : "light";
Option.EnableHighlight = EnableHighlight;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Option, nameof(Update));
/// <summary>
/// 更新组件值方法
/// </summary>
/// <param name="vals"></param>
/// <returns></returns>
[JSInvokable]
public async Task Update(string[] vals)
{
if (vals.Length == 2)
{
CurrentValueAsString = vals[0];
var hasChanged = !EqualityComparer<string>.Default.Equals(vals[1], Html);
if (hasChanged)
{
Html = vals[1];
if (HtmlChanged.HasDelegate)
{
await HtmlChanged.InvokeAsync(Html);
}
}
if (ValidateForm != null)
{
StateHasChanged();
}
}
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public new async Task SetValue(string value)
{
CurrentValueAsString = value;
await InvokeVoidAsync("update", Id, Value);
}
/// <summary>
/// 执行方法
/// </summary>
/// <param name="method"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public Task DoMethodAsync(string method, params object[] parameters) => InvokeVoidAsync("invoke", Id, method, parameters);
}