Skip to content

Commit 8cfd0e1

Browse files
committed
feat: 增加 Vditor 组件
1 parent dc2bf00 commit 8cfd0e1

8 files changed

Lines changed: 262 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<Version>9.0.0</Version>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Vditor Markdown</PackageTags>
9+
<Description>Bootstrap UI components extensions of Vditor Markdown</Description>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Using Include="Microsoft.JSInterop" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits ValidateBase<string>
3+
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.Vditor/Vditor.razor.js", JSObjectReference = true)]
4+
5+
<div @attributes="@AdditionalAttributes" class="@ClassString" id="@Id"></div>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using Microsoft.AspNetCore.Components;
6+
7+
namespace BootstrapBlazor.Components;
8+
9+
/// <summary>
10+
/// Vditor component
11+
/// </summary>
12+
public partial class Vditor
13+
{
14+
/// <summary>
15+
/// 获得/设置 组件 <see cref="VditorOptions"/> 实例 默认 null
16+
/// </summary>
17+
[Parameter]
18+
public VditorOptions? Options { get; set; }
19+
20+
private string? ClassString => CssBuilder.Default("bb-vditor")
21+
.AddClassFromAttributes(AdditionalAttributes)
22+
.Build();
23+
24+
private string? _lastValue;
25+
26+
/// <summary>
27+
/// <inheritdoc/>
28+
/// </summary>
29+
/// <param name="firstRender"></param>
30+
/// <returns></returns>
31+
protected override async Task OnAfterRenderAsync(bool firstRender)
32+
{
33+
await base.OnAfterRenderAsync(firstRender);
34+
35+
if (firstRender)
36+
{
37+
_lastValue = Value;
38+
return;
39+
}
40+
41+
if (_lastValue != Value)
42+
{
43+
_lastValue = Value;
44+
await InvokeVoidAsync("update", Id, Value);
45+
}
46+
}
47+
48+
/// <summary>
49+
/// <inheritdoc/>
50+
/// </summary>
51+
/// <returns></returns>
52+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
53+
{
54+
Options,
55+
Value,
56+
TriggerUpdateValueAsync = nameof(TriggerUpdateValueAsync)
57+
});
58+
59+
/// <summary>
60+
/// 触发 Value 值改变回调方法由 JavaScript 调用
61+
/// </summary>
62+
/// <param name="value"></param>
63+
/// <returns></returns>
64+
[JSInvokable]
65+
public Task TriggerUpdateValueAsync(string value)
66+
{
67+
CurrentValue = value;
68+
return Task.CompletedTask;
69+
}
70+
}
71+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { addLink, addScript } from '../BootstrapBlazor/modules/utility.js';
2+
import Data from '../BootstrapBlazor/modules/data.js';
3+
4+
export async function init(id, invoke, options) {
5+
const el = document.getElementById(id);
6+
if (el === null) {
7+
return;
8+
}
9+
10+
await addLink('./_content/BootstrapBlazor.Vditor/css/index.css');
11+
await addScript('./_content/BootstrapBlazor.Vditor/js/vditor.js');
12+
13+
const { options: op, value } = options;
14+
const vditor = new Vditor(id, getOptions({ ...op, value }));
15+
16+
Data.set(id, { el, invoke, vditor });
17+
}
18+
19+
const getOptions = options => {
20+
return {
21+
cache: {
22+
enable: false,
23+
},
24+
...options
25+
};
26+
}
27+
28+
export async function update(id, value) {
29+
30+
}
31+
32+
export function dispose(id) {
33+
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// 图标风格
9+
/// </summary>
10+
public enum VditorIconStyle
11+
{
12+
/// <summary>
13+
/// ant
14+
/// </summary>
15+
Ant,
16+
17+
/// <summary>
18+
/// material
19+
/// </summary>
20+
Material
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// 编辑器渲染模式
9+
/// </summary>
10+
public enum VditorMode
11+
{
12+
/// <summary>
13+
/// 所见即所得
14+
/// </summary>
15+
WYSIWYG,
16+
17+
/// <summary>
18+
/// 即使渲染
19+
/// </summary>
20+
IR,
21+
22+
/// <summary>
23+
/// 左右分屏
24+
/// </summary>
25+
SV
26+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BootstrapBlazor.Components;
4+
5+
/// <summary>
6+
/// Vditor 配置类
7+
/// </summary>
8+
public class VditorOptions
9+
{
10+
/// <summary>
11+
/// 获得/设置 编辑器模式。默认 <see cref="VditorMode.IR"/>
12+
/// </summary>
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
14+
[JsonEnumConverter(camelCase: true)]
15+
public VditorMode Mode { get; set; } = VditorMode.IR;
16+
17+
/// <summary>
18+
/// 获得/设置 显示的语言。默认取当前 UI 文化信息
19+
/// </summary>
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
21+
public string? Language { get; set; }
22+
23+
/// <summary>
24+
/// 获得/设置 图标风格
25+
/// </summary>
26+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
27+
[JsonEnumConverter(camelCase: true)]
28+
public VditorIconStyle IconStyle { get; set; }
29+
30+
/// <summary>
31+
/// 获得/设置 是否输出日志。
32+
/// </summary>
33+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
34+
public bool Debug { get; set; }
35+
36+
/// <summary>
37+
/// 获得/设置 输入区域为空时的提示。
38+
/// </summary>
39+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
40+
public string? Placeholder { get; set; }
41+
42+
/// <summary>
43+
/// 获得/设置 编辑器总宽度。
44+
/// </summary>
45+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
46+
public string? Width { get; set; }
47+
48+
/// <summary>
49+
/// 获得/设置 编辑器总高度。
50+
/// </summary>
51+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
52+
public string? Height { get; set; }
53+
54+
/// <summary>
55+
/// 获得/设置 编辑区域最小高度。
56+
/// </summary>
57+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
58+
public string? MinHeight { get; set; }
59+
60+
/// <summary>
61+
/// 获得/设置 配置自建 CDN 地址。
62+
/// </summary>
63+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
64+
public string? CDN { get; set; }
65+
66+
/// <summary>
67+
/// 获得/设置 按下 <c>tab</c> 键操作的字符串。
68+
/// </summary>
69+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
70+
public string? Tab { get; set; }
71+
72+
/// <summary>
73+
/// 获得/设置 回撤的延迟时间。
74+
/// </summary>
75+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
76+
public int UndoDelay { get; set; }
77+
78+
/// <summary>
79+
/// 获得/设置 是否启用打字机模式。
80+
/// </summary>
81+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
82+
public bool TypeWriterMode { get; set; }
83+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@using BootstrapBlazor.Components;
2+
@using Microsoft.AspNetCore.Components.Web

0 commit comments

Comments
 (0)