|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +namespace BootstrapBlazor.Server.Components.Samples; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Vditors 组件示例代码 |
| 10 | +/// </summary> |
| 11 | +public partial class Vditors |
| 12 | +{ |
| 13 | + [Inject] |
| 14 | + [NotNull] |
| 15 | + private IStringLocalizer<Vditors>? Localizer { get; set; } |
| 16 | + |
| 17 | + private VditorOptions _vditorOptions = new() |
| 18 | + { |
| 19 | + Height = "500px" |
| 20 | + }; |
| 21 | + |
| 22 | + private Vditor _vditor = default!; |
| 23 | + private string? _htmlString; |
| 24 | + private string _vditorValueString = "## 所见即所得(WYSIWYG)\n所见即所得模式对不熟悉 Markdown 的用户较为友好,熟悉 Markdown 的话也可以无缝使用。"; |
| 25 | + private VditorMode _mode = VditorMode.WYSIWYG; |
| 26 | + private ConsoleLogger _logger = default!; |
| 27 | + |
| 28 | + private async Task OnModeChanged(VditorMode mode) |
| 29 | + { |
| 30 | + _mode = mode; |
| 31 | + _vditorOptions.Mode = mode; |
| 32 | + if (mode == VditorMode.WYSIWYG) |
| 33 | + { |
| 34 | + _vditorValueString = "## 所见即所得(WYSIWYG)\n所见即所得模式对不熟悉 Markdown 的用户较为友好,熟悉 Markdown 的话也可以无缝使用。"; |
| 35 | + } |
| 36 | + else if (mode == VditorMode.IR) |
| 37 | + { |
| 38 | + _vditorValueString = "## 即时渲染(IR)\n即时渲染模式对熟悉 Typora 的用户应该不会感到陌生,理论上这是最优雅的 Markdown 编辑方式。"; |
| 39 | + } |
| 40 | + else if (mode == VditorMode.SV) |
| 41 | + { |
| 42 | + _vditorValueString = "## 分屏预览(SV)\n传统的分屏预览模式适合大屏下的 Markdown 编辑。"; |
| 43 | + } |
| 44 | + |
| 45 | + _htmlString = await _vditor.GetHtmlAsync(); |
| 46 | + StateHasChanged(); |
| 47 | + } |
| 48 | + |
| 49 | + private Task OnRenderAsync() |
| 50 | + { |
| 51 | + _logger.Log($"Trigger OnRenderAsync"); |
| 52 | + return Task.CompletedTask; |
| 53 | + } |
| 54 | + |
| 55 | + private Task OnFocusAsync(string value) |
| 56 | + { |
| 57 | + _logger.Log($"Trigger OnFocusAsync"); |
| 58 | + return Task.CompletedTask; |
| 59 | + } |
| 60 | + |
| 61 | + private async Task OnBlurAsync(string value) |
| 62 | + { |
| 63 | + _vditorValueString = value; |
| 64 | + _logger.Log($"Trigger OnBlurAsync"); |
| 65 | + |
| 66 | + _htmlString = await _vditor.GetHtmlAsync(); |
| 67 | + StateHasChanged(); |
| 68 | + } |
| 69 | + |
| 70 | + private Task OnEscapeAsync(string value) |
| 71 | + { |
| 72 | + _logger.Log($"Trigger OnEscapeAsync"); |
| 73 | + return Task.CompletedTask; |
| 74 | + } |
| 75 | + |
| 76 | + private Task OnSelectAsync(string value) |
| 77 | + { |
| 78 | + _logger.Log($"Trigger OnSelectAsync"); |
| 79 | + return Task.CompletedTask; |
| 80 | + } |
| 81 | + |
| 82 | + private async Task OnInputAsync(string value) |
| 83 | + { |
| 84 | + _vditorValueString = value; |
| 85 | + _htmlString = await _vditor.GetHtmlAsync(); |
| 86 | + |
| 87 | + _logger.Log($"Trigger OnInputAsync"); |
| 88 | + StateHasChanged(); |
| 89 | + } |
| 90 | + |
| 91 | + private async Task OnCtrlEnterAsync(string value) |
| 92 | + { |
| 93 | + _vditorValueString = value; |
| 94 | + _htmlString = await _vditor.GetHtmlAsync(); |
| 95 | + |
| 96 | + _logger.Log($"Trigger OnCtrlEnterAsync"); |
| 97 | + StateHasChanged(); |
| 98 | + } |
| 99 | + |
| 100 | + private async Task OnTriggerGetValueAsync() |
| 101 | + { |
| 102 | + _vditorValueString = await _vditor.GetValueAsync() ?? ""; |
| 103 | + } |
| 104 | + |
| 105 | + private async Task OnTriggerInsertValueAsync() |
| 106 | + { |
| 107 | + await _vditor.InsertValueAsync("光标处插入当前值"); |
| 108 | + } |
| 109 | + |
| 110 | + private async Task OnTriggerGetHtmlAsync() |
| 111 | + { |
| 112 | + _htmlString = await _vditor.GetHtmlAsync(); |
| 113 | + } |
| 114 | + |
| 115 | + private async Task OnTriggerGetSelectionAsync() |
| 116 | + { |
| 117 | + var selection = await _vditor.GetSelectionAsync() ?? ""; |
| 118 | + _logger.Log($"Trigger OnTriggerGetSelectionAsync: {selection}"); |
| 119 | + } |
| 120 | + |
| 121 | + private bool _isDisabled = false; |
| 122 | + private async Task OnTriggerEnableAsync() |
| 123 | + { |
| 124 | + await _vditor.EnableAsync(); |
| 125 | + _isDisabled = false; |
| 126 | + } |
| 127 | + |
| 128 | + private async Task OnTriggerDisableAsync() |
| 129 | + { |
| 130 | + await _vditor.DisableAsync(); |
| 131 | + _isDisabled = true; |
| 132 | + } |
| 133 | + |
| 134 | + private async Task OnTriggerFocusAsync() |
| 135 | + { |
| 136 | + await _vditor.FocusAsync(); |
| 137 | + } |
| 138 | + |
| 139 | + private async Task OnTriggerBlurAsync() |
| 140 | + { |
| 141 | + await _vditor.BlurAsync(); |
| 142 | + } |
| 143 | + |
| 144 | + private static AttributeItem[] GetAttributes() => |
| 145 | + [ |
| 146 | + new() |
| 147 | + { |
| 148 | + Name = "EditorSettings", |
| 149 | + Description = "编辑器设置", |
| 150 | + Type = "EditorSettings", |
| 151 | + ValueList = " — ", |
| 152 | + DefaultValue = " — " |
| 153 | + }, |
| 154 | + new() |
| 155 | + { |
| 156 | + Name = "ToolbarSettings", |
| 157 | + Description = "工具栏设置", |
| 158 | + Type = "ToolbarSettings", |
| 159 | + ValueList = " — ", |
| 160 | + DefaultValue = " — " |
| 161 | + }, |
| 162 | + new() |
| 163 | + { |
| 164 | + Name = "Value", |
| 165 | + Description = "组件值", |
| 166 | + Type = "string", |
| 167 | + ValueList = " — ", |
| 168 | + DefaultValue = " — " |
| 169 | + }, |
| 170 | + new() |
| 171 | + { |
| 172 | + Name = "Html", |
| 173 | + Description = "组件 Html 代码", |
| 174 | + Type = "string", |
| 175 | + ValueList = " — ", |
| 176 | + DefaultValue = " — " |
| 177 | + }, |
| 178 | + new() |
| 179 | + { |
| 180 | + Name = "OnFileUpload", |
| 181 | + Description = "文件上传回调方法", |
| 182 | + Type = "Func<CherryMarkdownUploadFile, Task<string>>", |
| 183 | + ValueList = " — ", |
| 184 | + DefaultValue = " — " |
| 185 | + }, |
| 186 | + new() |
| 187 | + { |
| 188 | + Name = "IsViewer", |
| 189 | + Description = "组件是否为浏览器模式", |
| 190 | + Type = "bool", |
| 191 | + ValueList = "true/false", |
| 192 | + DefaultValue = "false" |
| 193 | + } |
| 194 | + ]; |
| 195 | +} |
0 commit comments