Skip to content

Commit 6372a3b

Browse files
j4587698ArgoZhang
andauthored
feat(cherry-markdown): add IsSupportMath parameter (#470)
* update * chore: bump version 9.0.1 Co-Authored-By: j4587698 <24642446+j4587698@users.noreply.github.com> * chore: 增加字库 * refactor: 移除 Option 配置类 --------- Co-authored-by: Argo Zhang <argo@live.ca> Co-authored-by: j4587698 <24642446+j4587698@users.noreply.github.com>
1 parent 4093d67 commit 6372a3b

76 files changed

Lines changed: 224 additions & 103 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.

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.1</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

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

Lines changed: 16 additions & 29 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>
@@ -25,6 +23,12 @@ public partial class CherryMarkdown
2523
[Parameter]
2624
public ToolbarSettings? ToolbarSettings { get; set; }
2725

26+
/// <summary>
27+
/// 获得/ 设置 是否使用 Katex 渲染数学公式 默认 true
28+
/// </summary>
29+
[Parameter]
30+
public bool IsSupportMath { get; set; } = true;
31+
2832
private string? _lastValue;
2933
/// <summary>
3034
/// 获得/设置 组件值
@@ -60,27 +64,7 @@ public partial class CherryMarkdown
6064
/// 获取/设置 组件是否为浏览器模式
6165
/// </summary>
6266
[Parameter]
63-
public bool? IsViewer { get; set; }
64-
65-
/// <summary>
66-
/// OnInitialized 方法
67-
/// </summary>
68-
protected override void OnInitialized()
69-
{
70-
base.OnInitialized();
71-
72-
_lastValue = Value;
73-
Option.Value = Value;
74-
Option.Editor = EditorSettings ?? new EditorSettings();
75-
Option.Toolbars = ToolbarSettings ?? new ToolbarSettings();
76-
if (IsViewer == true)
77-
{
78-
Option.Editor.DefaultModel = "previewOnly";
79-
Option.Toolbars.Toolbar = false;
80-
}
81-
82-
_lastValue = Value;
83-
}
67+
public bool IsViewer { get; set; }
8468

8569
/// <summary>
8670
/// <inheritdoc/>
@@ -91,6 +75,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
9175
{
9276
await base.OnAfterRenderAsync(firstRender);
9377

78+
if (firstRender)
79+
{
80+
_lastValue = Value;
81+
return;
82+
}
83+
9484
if (Value != _lastValue)
9585
{
9686
_lastValue = Value;
@@ -102,7 +92,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
10292
/// <inheritdoc/>
10393
/// </summary>
10494
/// <returns></returns>
105-
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));
10698

10799
/// <summary>
108100
/// 文件上传回调
@@ -111,7 +103,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
111103
[JSInvokable]
112104
public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
113105
{
114-
#if NET6_0_OR_GREATER
115106
var ret = "";
116107
if (Module != null)
117108
{
@@ -127,10 +118,6 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
127118
}
128119
}
129120
return ret;
130-
#else
131-
await Task.Yield();
132-
throw new NotSupportedException();
133-
#endif
134121
}
135122

136123
/// <summary>

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

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,100 @@
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)
14-
15-
md._invoker = invoker
16-
md._invokerMethod = callback
17-
md._element = el
18-
md._options = options
1910

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+
const editor = new Cherry(op);
62+
const md = { invoke, editor };
63+
Data.set(id, md);
5664
}
5765

5866
export function update(id, val) {
59-
const md = Data.get(id)
60-
md._editor.setMarkdown(val, true)
67+
const md = Data.get(id);
68+
const { editor } = md;
69+
if (md) {
70+
editor.setMarkdown(val, true)
71+
}
6172
}
6273

6374
export function fetch(id) {
6475
const md = Data.get(id)
65-
return md._file
76+
const { file } = md;
77+
return file
6678
}
6779

6880
export function invoke(id, method, parameters) {
6981
const md = Data.get(id)
82+
const { editor, invoke } = md;
83+
7084
if (method.indexOf('.') < 0) {
71-
md._editor[method](...parameters)
85+
editor[method](...parameters)
7286
}
7387
else {
7488
const methods = method.split('.');
75-
let m = md._editor[methods[0]];
89+
let m = editor[methods[0]];
7690
for (let i = 1; i < methods.length; i++) {
7791
m = m[methods[i]]
7892
}
7993
m(...parameters);
8094
}
81-
const val = md._editor.getMarkdown();
82-
const html = md._editor.getHtml();
83-
md._invoker.invokeMethodAsync('Update', [val, html]);
95+
const val = editor.getMarkdown();
96+
const html = editor.getHtml();
97+
invoke.invokeMethodAsync('Update', [val, html]);
8498
}
8599

86100
export function dispose(id) {

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

Lines changed: 0 additions & 26 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
}

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)