Skip to content

Commit 830fd76

Browse files
authored
feat(CherryMarkdown): inherits BootstrapModuleComponentBase (#4217)
* refactor: 更新基类复用 JSModule 类 * chore: bump version 8.0.1 * chore: 更新依赖包
1 parent 4487117 commit 830fd76

File tree

5 files changed

+50
-82
lines changed

5 files changed

+50
-82
lines changed

src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<PackageReference Include="BootstrapBlazor.BootstrapIcon" Version="8.0.3" />
3434
<PackageReference Include="BootstrapBlazor.BootstrapIcon.Extensions" Version="8.0.6" />
3535
<PackageReference Include="BootstrapBlazor.Chart" Version="8.2.3" />
36-
<PackageReference Include="BootstrapBlazor.CherryMarkdown" Version="8.0.0" />
36+
<PackageReference Include="BootstrapBlazor.CherryMarkdown" Version="8.0.1" />
3737
<PackageReference Include="BootstrapBlazor.CodeEditor" Version="8.0.2" />
3838
<PackageReference Include="BootstrapBlazor.Dock" Version="8.1.7" />
3939
<PackageReference Include="BootstrapBlazor.DockView" Version="8.1.4-beta01" />

src/Extensions/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>8.0.0</Version>
4+
<Version>8.0.1</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@namespace BootstrapBlazor.Components
2-
@inherits BootstrapComponentBase
2+
@inherits BootstrapModuleComponentBase
3+
@attribute [JSModuleAutoLoader("./_content/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/CherryMarkdown.razor.js", JSObjectReference = true)]
34

4-
<div @attributes="@AdditionalAttributes" @ref="@Element"></div>
5+
<div @attributes="@AdditionalAttributes" id="@Id"></div>

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

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
namespace BootstrapBlazor.Components;
88

99
/// <summary>
10-
///
10+
/// CherryMarkdown 组件
1111
/// </summary>
12-
public partial class CherryMarkdown : IAsyncDisposable
12+
public partial class CherryMarkdown
1313
{
1414
private CherryMarkdownOption Option { get; } = new();
1515

@@ -62,17 +62,6 @@ public partial class CherryMarkdown : IAsyncDisposable
6262
[Parameter]
6363
public bool? IsViewer { get; set; }
6464

65-
[NotNull]
66-
private IJSObjectReference? Module { get; set; }
67-
68-
[NotNull]
69-
private DotNetObjectReference<CherryMarkdown>? Interop { get; set; }
70-
71-
/// <summary>
72-
/// 获得/设置 DOM 元素实例
73-
/// </summary>
74-
private ElementReference Element { get; set; }
75-
7665
/// <summary>
7766
/// OnInitialized 方法
7867
/// </summary>
@@ -89,6 +78,8 @@ protected override void OnInitialized()
8978
Option.Editor.DefaultModel = "previewOnly";
9079
Option.Toolbars.Toolbar = false;
9180
}
81+
82+
_lastValue = Value;
9283
}
9384

9485
/// <summary>
@@ -98,23 +89,21 @@ protected override void OnInitialized()
9889
/// <returns></returns>
9990
protected override async Task OnAfterRenderAsync(bool firstRender)
10091
{
101-
if (firstRender)
102-
{
103-
// import JavaScript
104-
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/BootstrapBlazor.CherryMarkdown/Components/CherryMarkdown/CherryMarkdown.razor.js");
105-
Interop = DotNetObjectReference.Create(this);
106-
await Module.InvokeVoidAsync("init", Element, Interop, Option, nameof(Upload));
107-
}
108-
else
92+
await base.OnAfterRenderAsync(firstRender);
93+
94+
if (Value != _lastValue)
10995
{
110-
if (Value != _lastValue)
111-
{
112-
_lastValue = Value;
113-
await Module.InvokeVoidAsync("update", Element, Value);
114-
}
96+
_lastValue = Value;
97+
await InvokeVoidAsync("update", Id, Value);
11598
}
11699
}
117100

101+
/// <summary>
102+
/// <inheritdoc/>
103+
/// </summary>
104+
/// <returns></returns>
105+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Option, nameof(Upload));
106+
118107
/// <summary>
119108
/// 文件上传回调
120109
/// </summary>
@@ -126,12 +115,15 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
126115
var ret = "";
127116
if (Module != null)
128117
{
129-
var stream = await Module.InvokeAsync<IJSStreamReference>("fetch", Element);
130-
using var data = await stream.OpenReadStreamAsync();
131-
uploadFile.UploadStream = data;
132-
if (OnFileUpload != null)
118+
var stream = await InvokeAsync<IJSStreamReference>("fetch", Id);
119+
if (stream != null)
133120
{
134-
ret = await OnFileUpload(uploadFile);
121+
using var data = await stream.OpenReadStreamAsync();
122+
uploadFile.UploadStream = data;
123+
if (OnFileUpload != null)
124+
{
125+
ret = await OnFileUpload(uploadFile);
126+
}
135127
}
136128
}
137129
return ret;
@@ -144,17 +136,17 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
144136
/// <summary>
145137
/// 更新组件值方法
146138
/// </summary>
147-
/// <param name="vals"></param>
139+
/// <param name="values"></param>
148140
/// <returns></returns>
149141
[JSInvokable]
150-
public async Task Update(string[] vals)
142+
public async Task Update(string[] values)
151143
{
152-
if (vals.Length == 2)
144+
if (values.Length == 2)
153145
{
154-
var hasChanged = !EqualityComparer<string>.Default.Equals(vals[0], Value);
146+
var hasChanged = !EqualityComparer<string>.Default.Equals(values[0], Value);
155147
if (hasChanged)
156148
{
157-
Value = vals[0];
149+
Value = values[0];
158150
_lastValue = Value;
159151

160152
if (ValueChanged.HasDelegate)
@@ -163,10 +155,10 @@ public async Task Update(string[] vals)
163155
}
164156
}
165157

166-
hasChanged = !EqualityComparer<string>.Default.Equals(vals[1], Html);
158+
hasChanged = !EqualityComparer<string>.Default.Equals(values[1], Html);
167159
if (hasChanged)
168160
{
169-
Html = vals[1];
161+
Html = values[1];
170162
if (HtmlChanged.HasDelegate)
171163
{
172164
await HtmlChanged.InvokeAsync(Html);
@@ -181,34 +173,5 @@ public async Task Update(string[] vals)
181173
/// <param name="method"></param>
182174
/// <param name="parameters"></param>
183175
/// <returns></returns>
184-
public ValueTask DoMethodAsync(string method, params object[] parameters) => Module.InvokeVoidAsync("invoke", Element, method, parameters);
185-
186-
#region Dispose
187-
/// <summary>
188-
/// Dispose 方法
189-
/// </summary>
190-
/// <param name="disposing"></param>
191-
protected virtual async ValueTask DisposeAsync(bool disposing)
192-
{
193-
if (disposing)
194-
{
195-
Interop?.Dispose();
196-
197-
if (Module != null)
198-
{
199-
await Module.InvokeVoidAsync("dispose", Element);
200-
await Module.DisposeAsync();
201-
}
202-
}
203-
}
204-
205-
/// <summary>
206-
/// Dispose 方法
207-
/// </summary>
208-
public async ValueTask DisposeAsync()
209-
{
210-
await DisposeAsync(true);
211-
GC.SuppressFinalize(this);
212-
}
213-
#endregion
176+
public Task DoMethodAsync(string method, params object[] parameters) => InvokeVoidAsync("invoke", Id, method, parameters);
214177
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
import { addLink } from '../../../BootstrapBlazor/modules/utility.js'
33
import Data from '../../../BootstrapBlazor/modules/data.js'
44

5-
export async function init(el, invoker, options, callback) {
5+
export async function init(id, invoker, options, callback) {
66
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/cherry-markdown.min.css')
77

8+
const el = document.getElementById(id);
9+
if (el === null) {
10+
return;
11+
}
812
const md = {}
9-
Data.set(el, md)
13+
Data.set(id, md)
1014

1115
md._invoker = invoker
1216
md._invokerMethod = callback
@@ -51,18 +55,18 @@ export async function init(el, invoker, options, callback) {
5155
});
5256
}
5357

54-
export function update(el, val) {
55-
const md = Data.get(el)
58+
export function update(id, val) {
59+
const md = Data.get(id)
5660
md._editor.setMarkdown(val, true)
5761
}
5862

59-
export function fetch(el) {
60-
const md = Data.get(el)
63+
export function fetch(id) {
64+
const md = Data.get(id)
6165
return md._file
6266
}
6367

64-
export function invoke(el, method, parameters) {
65-
const md = Data.get(el)
68+
export function invoke(id, method, parameters) {
69+
const md = Data.get(id)
6670
if (method.indexOf('.') < 0) {
6771
md._editor[method](...parameters)
6872
}
@@ -79,6 +83,6 @@ export function invoke(el, method, parameters) {
7983
md._invoker.invokeMethodAsync('Update', [val, html]);
8084
}
8185

82-
export function dispose(el) {
83-
Data.remove(el)
86+
export function dispose(id) {
87+
Data.remove(id)
8488
}

0 commit comments

Comments
 (0)