Skip to content

Commit b377252

Browse files
committed
refactor: 移除 Option 配置类
1 parent 07b55b2 commit b377252

5 files changed

Lines changed: 80 additions & 111 deletions

File tree

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace BootstrapBlazor.Components;
1111
/// </summary>
1212
public partial class CherryMarkdown
1313
{
14-
private CherryMarkdownOption Option { get; } = new();
15-
1614
/// <summary>
1715
/// 获得/设置 编辑器设置
1816
/// </summary>
@@ -26,10 +24,10 @@ public partial class CherryMarkdown
2624
public ToolbarSettings? ToolbarSettings { get; set; }
2725

2826
/// <summary>
29-
/// 获得/ 设置 是否使用 Katex 渲染数学公式
27+
/// 获得/ 设置 是否使用 Katex 渲染数学公式 默认 true
3028
/// </summary>
3129
[Parameter]
32-
public bool UseKatex { get; set; }
30+
public bool IsSupportMath { get; set; } = true;
3331

3432
private string? _lastValue;
3533
/// <summary>
@@ -66,28 +64,7 @@ public partial class CherryMarkdown
6664
/// 获取/设置 组件是否为浏览器模式
6765
/// </summary>
6866
[Parameter]
69-
public bool? IsViewer { get; set; }
70-
71-
/// <summary>
72-
/// OnInitialized 方法
73-
/// </summary>
74-
protected override void OnInitialized()
75-
{
76-
base.OnInitialized();
77-
78-
_lastValue = Value;
79-
Option.Value = Value;
80-
Option.UseKatex = UseKatex;
81-
Option.Editor = EditorSettings ?? new EditorSettings();
82-
Option.Toolbars = ToolbarSettings ?? new ToolbarSettings();
83-
if (IsViewer == true)
84-
{
85-
Option.Editor.DefaultModel = "previewOnly";
86-
Option.Toolbars.Toolbar = false;
87-
}
88-
89-
_lastValue = Value;
90-
}
67+
public bool IsViewer { get; set; }
9168

9269
/// <summary>
9370
/// <inheritdoc/>
@@ -98,6 +75,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
9875
{
9976
await base.OnAfterRenderAsync(firstRender);
10077

78+
if (firstRender)
79+
{
80+
_lastValue = Value;
81+
return;
82+
}
83+
10184
if (Value != _lastValue)
10285
{
10386
_lastValue = Value;
@@ -109,7 +92,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
10992
/// <inheritdoc/>
11093
/// </summary>
11194
/// <returns></returns>
112-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Option, nameof(Upload));
95+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop,
96+
new { Value, IsSupportMath, IsViewer, Editor = EditorSettings ?? new(), Toolbars = ToolbarSettings ?? new() },
97+
nameof(Upload));
11398

11499
/// <summary>
115100
/// 文件上传回调
@@ -118,7 +103,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
118103
[JSInvokable]
119104
public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
120105
{
121-
#if NET6_0_OR_GREATER
122106
var ret = "";
123107
if (Module != null)
124108
{
@@ -134,10 +118,6 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
134118
}
135119
}
136120
return ret;
137-
#else
138-
await Task.Yield();
139-
throw new NotSupportedException();
140-
#endif
141121
}
142122

143123
/// <summary>

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

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,99 @@
22
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-
8-
if (options.useKatex){
9-
await addScript('./_content/BootstrapBlazor.CherryMarkdown/js/katex.min.js')
10-
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/katex.min.css')
11-
options.engine = {syntax : { mathBlock: {engine: 'katex', },
12-
inlineMath: {engine: 'katex'}}}
13-
options.externals = {katex: window.katex}
14-
}
15-
delete options.useKatexuseKatex
5+
export async function init(id, invoke, options, callback) {
166
const el = document.getElementById(id);
177
if (el === null) {
188
return;
199
}
20-
const md = {}
21-
Data.set(id, md)
2210

23-
md._invoker = invoker
24-
md._invokerMethod = callback
25-
md._element = el
26-
md._options = options
27-
28-
if (md._options.toolbars.toolbar === null) {
29-
delete md._options.toolbars.toolbar
30-
}
31-
if (md._options.toolbars.bubble === null) {
32-
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;
3323
}
34-
if (md._options.toolbars.float === null) {
35-
delete md._options.toolbars.float
24+
if (options.isViewer) {
25+
options.editor.defaultModel = 'previewOnly';
26+
options.toolbars.Toolbar = false;
3627
}
3728

38-
const fileUpload = (file, callback) => {
39-
md._file = file
40-
md._invoker.invokeMethodAsync(md._invokerMethod, {
29+
const fileUpload = (file, cb) => {
30+
md.file = file
31+
invoke.invokeMethodAsync(callback, {
4132
fileName: file.name,
4233
fileSize: file.size,
4334
contentType: file.type,
4435
lastModified: new Date(file.lastModified).toISOString(),
4536
}).then(data => {
4637
if (data !== "") {
47-
callback(data)
38+
cb(data)
4839
}
4940
})
5041
}
5142

52-
md._editor = new Cherry({
53-
el: md._element,
54-
... md._options,
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,
5554
callback: {
5655
afterChange: (markdown, html) => {
57-
md._invoker.invokeMethodAsync('Update', [markdown, html])
56+
invoke.invokeMethodAsync('Update', [markdown, html])
5857
}
5958
},
6059
fileUpload: fileUpload
61-
});
60+
};
61+
const editor = new Cherry(op);
62+
const md = { invoke, editor };
63+
Data.set(id, md);
6264
}
6365

6466
export function update(id, val) {
65-
const md = Data.get(id)
66-
md._editor.setMarkdown(val, true)
67+
const md = Data.get(id);
68+
const { editor } = md;
69+
if (md) {
70+
editor.setMarkdown(val, true)
71+
}
6772
}
6873

6974
export function fetch(id) {
7075
const md = Data.get(id)
71-
return md._file
76+
const { file } = md;
77+
return file
7278
}
7379

7480
export function invoke(id, method, parameters) {
7581
const md = Data.get(id)
82+
const { editor, invoke } = md;
83+
7684
if (method.indexOf('.') < 0) {
77-
md._editor[method](...parameters)
85+
editor[method](...parameters)
7886
}
7987
else {
8088
const methods = method.split('.');
81-
let m = md._editor[methods[0]];
89+
let m = editor[methods[0]];
8290
for (let i = 1; i < methods.length; i++) {
8391
m = m[methods[i]]
8492
}
8593
m(...parameters);
8694
}
87-
const val = md._editor.getMarkdown();
88-
const html = md._editor.getHtml();
89-
md._invoker.invokeMethodAsync('Update', [val, html]);
95+
const val = editor.getMarkdown();
96+
const html = editor.getHtml();
97+
invoke.invokeMethodAsync('Update', [val, html]);
9098
}
9199

92100
export function dispose(id) {

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

Lines changed: 0 additions & 31 deletions
This file was deleted.

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
}

0 commit comments

Comments
 (0)