Skip to content

Commit ac0cea0

Browse files
committed
Merge branch 'master' into feat-editor
2 parents 1bf508a + 789b58d commit ac0cea0

258 files changed

Lines changed: 105309 additions & 5445 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BootstrapBlazor.Extensions.sln

Lines changed: 86 additions & 44 deletions
Large diffs are not rendered by default.

src/components/BootstrapBlazor.CherryMarkdown/BootstrapBlazor.CherryMarkdown.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.0</Version>
4+
<Version>9.0.3</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/components/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/CherryMarkdown.razor.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

55
using Microsoft.AspNetCore.Components;
6+
using System.Globalization;
67

78
namespace BootstrapBlazor.Components;
89

@@ -11,8 +12,6 @@ namespace BootstrapBlazor.Components;
1112
/// </summary>
1213
public partial class CherryMarkdown
1314
{
14-
private CherryMarkdownOption Option { get; } = new();
15-
1615
/// <summary>
1716
/// 获得/设置 编辑器设置
1817
/// </summary>
@@ -25,6 +24,12 @@ public partial class CherryMarkdown
2524
[Parameter]
2625
public ToolbarSettings? ToolbarSettings { get; set; }
2726

27+
/// <summary>
28+
/// 获得/ 设置 是否使用 Katex 渲染数学公式 默认 true
29+
/// </summary>
30+
[Parameter]
31+
public bool IsSupportMath { get; set; } = true;
32+
2833
private string? _lastValue;
2934
/// <summary>
3035
/// 获得/设置 组件值
@@ -60,26 +65,25 @@ public partial class CherryMarkdown
6065
/// 获取/设置 组件是否为浏览器模式
6166
/// </summary>
6267
[Parameter]
63-
public bool? IsViewer { get; set; }
68+
public bool IsViewer { get; set; }
6469

6570
/// <summary>
66-
/// OnInitialized 方法
71+
/// 获取/设置 组件语言
6772
/// </summary>
68-
protected override void OnInitialized()
73+
[Parameter]
74+
public string? Language { get; set; }
75+
76+
/// <summary>
77+
/// <inheritdoc/>
78+
/// </summary>
79+
protected override void OnParametersSet()
6980
{
70-
base.OnInitialized();
81+
base.OnParametersSet();
7182

72-
_lastValue = Value;
73-
Option.Value = Value;
74-
Option.Editor = EditorSettings ?? new EditorSettings();
75-
Option.Toolbars = ToolbarSettings ?? new ToolbarSettings();
76-
if (IsViewer == true)
83+
if (string.IsNullOrEmpty(Language))
7784
{
78-
Option.Editor.DefaultModel = "previewOnly";
79-
Option.Toolbars.Toolbar = false;
85+
Language = CultureInfo.CurrentUICulture.Name;
8086
}
81-
82-
_lastValue = Value;
8387
}
8488

8589
/// <summary>
@@ -91,6 +95,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
9195
{
9296
await base.OnAfterRenderAsync(firstRender);
9397

98+
if (firstRender)
99+
{
100+
_lastValue = Value;
101+
return;
102+
}
103+
94104
if (Value != _lastValue)
95105
{
96106
_lastValue = Value;
@@ -102,7 +112,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
102112
/// <inheritdoc/>
103113
/// </summary>
104114
/// <returns></returns>
105-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Option, nameof(Upload));
115+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop,
116+
new { Value, IsSupportMath, IsViewer, Locale = Language, Editor = EditorSettings ?? new(), Toolbars = ToolbarSettings ?? new() },
117+
nameof(Upload));
106118

107119
/// <summary>
108120
/// 文件上传回调
@@ -111,7 +123,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
111123
[JSInvokable]
112124
public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
113125
{
114-
#if NET6_0_OR_GREATER
115126
var ret = "";
116127
if (Module != null)
117128
{
@@ -127,10 +138,6 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
127138
}
128139
}
129140
return ret;
130-
#else
131-
await Task.Yield();
132-
throw new NotSupportedException();
133-
#endif
134141
}
135142

136143
/// <summary>

src/components/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/CherryMarkdown.razor.js

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,105 @@
1-
import '../../js/cherry-markdown.min.js'
2-
import { addLink } from '../../../BootstrapBlazor/modules/utility.js'
1+
import '../../js/cherry-markdown.core.js'
2+
import { addLink, addScript } from '../../../BootstrapBlazor/modules/utility.js'
33
import Data from '../../../BootstrapBlazor/modules/data.js'
44

5-
export async function init(id, invoker, options, callback) {
6-
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/cherry-markdown.min.css')
7-
5+
export async function init(id, invoke, options, callback) {
86
const el = document.getElementById(id);
97
if (el === null) {
108
return;
119
}
12-
const md = {}
13-
Data.set(id, md)
1410

15-
md._invoker = invoker
16-
md._invokerMethod = callback
17-
md._element = el
18-
md._options = options
19-
20-
if (md._options.toolbars.toolbar === null) {
21-
delete md._options.toolbars.toolbar
22-
}
23-
if (md._options.toolbars.bubble === null) {
24-
delete md._options.toolbars.bubble
11+
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/cherry-markdown.min.css')
12+
if (options.isSupportMath) {
13+
await addScript('./_content/BootstrapBlazor.CherryMarkdown/js/katex.min.js')
14+
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/katex.min.css')
15+
options.engine = {
16+
syntax: {
17+
mathBlock: { engine: 'katex', },
18+
inlineMath: { engine: 'katex' }
19+
}
20+
}
21+
options.externals = { katex: window.katex };
22+
delete options.isSupportMath;
2523
}
26-
if (md._options.toolbars.float === null) {
27-
delete md._options.toolbars.float
24+
if (options.isViewer) {
25+
options.editor.defaultModel = 'previewOnly';
26+
options.toolbars.Toolbar = false;
2827
}
2928

30-
const fileUpload = (file, callback) => {
31-
md._file = file
32-
md._invoker.invokeMethodAsync(md._invokerMethod, {
29+
const fileUpload = (file, cb) => {
30+
md.file = file
31+
invoke.invokeMethodAsync(callback, {
3332
fileName: file.name,
3433
fileSize: file.size,
3534
contentType: file.type,
3635
lastModified: new Date(file.lastModified).toISOString(),
3736
}).then(data => {
3837
if (data !== "") {
39-
callback(data)
38+
cb(data)
4039
}
4140
})
4241
}
4342

44-
md._editor = new Cherry({
45-
el: md._element,
46-
value: md._options.value,
47-
editor: md._options.editor,
48-
toolbars: md._options.toolbars,
43+
options.editor = {
44+
theme: 'Default',
45+
height: '100%',
46+
defaultModel: 'edit&preview',
47+
convertWhenPaste: true,
48+
...options.editor
49+
}
50+
51+
const op = {
52+
el,
53+
...options,
4954
callback: {
5055
afterChange: (markdown, html) => {
51-
md._invoker.invokeMethodAsync('Update', [markdown, html])
56+
invoke.invokeMethodAsync('Update', [markdown, html])
5257
}
5358
},
5459
fileUpload: fileUpload
55-
});
60+
};
61+
62+
if (op.locale) {
63+
op.locale = op.locale.replace('-', '_');
64+
}
65+
66+
const editor = new Cherry(op);
67+
const md = { invoke, editor };
68+
Data.set(id, md);
5669
}
5770

5871
export function update(id, val) {
59-
const md = Data.get(id)
60-
md._editor.setMarkdown(val, true)
72+
const md = Data.get(id);
73+
const { editor } = md;
74+
if (md) {
75+
editor.setMarkdown(val, true)
76+
}
6177
}
6278

6379
export function fetch(id) {
6480
const md = Data.get(id)
65-
return md._file
81+
const { file } = md;
82+
return file
6683
}
6784

6885
export function invoke(id, method, parameters) {
6986
const md = Data.get(id)
87+
const { editor, invoke } = md;
88+
7089
if (method.indexOf('.') < 0) {
71-
md._editor[method](...parameters)
90+
editor[method](...parameters)
7291
}
7392
else {
7493
const methods = method.split('.');
75-
let m = md._editor[methods[0]];
94+
let m = editor[methods[0]];
7695
for (let i = 1; i < methods.length; i++) {
7796
m = m[methods[i]]
7897
}
7998
m(...parameters);
8099
}
81-
const val = md._editor.getMarkdown();
82-
const html = md._editor.getHtml();
83-
md._invoker.invokeMethodAsync('Update', [val, html]);
100+
const val = editor.getMarkdown();
101+
const html = editor.getHtml();
102+
invoke.invokeMethodAsync('Update', [val, html]);
84103
}
85104

86105
export function dispose(id) {

src/components/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/EditorSettings.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using System.Text.Json.Serialization;
6+
57
namespace BootstrapBlazor.Components;
68

79
/// <summary>
@@ -12,23 +14,27 @@ public class EditorSettings
1214
/// <summary>
1315
/// CodeMirror主题,默认为 default
1416
/// </summary>
15-
public string Theme { get; set; } = "default";
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public string? Theme { get; set; }
1619

1720
/// <summary>
1821
/// 编辑器高度,默认为100%
1922
/// </summary>
20-
public string Height { get; set; } = "100%";
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public string? Height { get; set; }
2125

2226
/// <summary>
2327
/// 编辑器显示模式
2428
/// edit&amp;preview: 双栏编辑预览模式,默认值
2529
/// editOnly: 只显示编辑器
2630
/// previewOnly: 预览模式
2731
/// </summary>
28-
public string DefaultModel { get; set; } = "edit&preview";
32+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
33+
public string? DefaultModel { get; set; }
2934

3035
/// <summary>
31-
/// 粘贴Html时自动转换为Markdown格式
36+
/// 粘贴 Html 时自动转换为 Markdown 格式
3237
/// </summary>
33-
public bool ConvertWhenPaste { get; set; } = true;
38+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
39+
public bool? ConvertWhenPaste { get; set; }
3440
}

src/components/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/ToolbarSettings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using System.Text.Json.Serialization;
6+
57
namespace BootstrapBlazor.Components;
68

79
/// <summary>
@@ -12,20 +14,24 @@ public class ToolbarSettings
1214
/// <summary>
1315
/// 主题,light 、dark(默认值)
1416
/// </summary>
15-
public string Theme { get; set; } = "dark";
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
18+
public string? Theme { get; set; }
1619

1720
/// <summary>
1821
/// 自定义工具栏按钮,null则为默认工具栏,false则不显示工具栏
1922
/// </summary>
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2024
public object? Toolbar { get; set; }
2125

2226
/// <summary>
2327
/// 选中后的悬浮菜单,null为默认悬浮菜单,false则不显示悬浮菜单
2428
/// </summary>
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2530
public object? Bubble { get; set; }
2631

2732
/// <summary>
2833
/// 新行的悬浮菜单,null为默认悬浮菜单,false则不显示悬浮菜单
2934
/// </summary>
35+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3036
public object? Float { get; set; }
3137
}

src/components/BootstrapBlazor.CherryMarkdown/wwwroot/css/cherry-markdown.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)