Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.0</Version>
<Version>9.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace BootstrapBlazor.Components;
/// </summary>
public partial class CherryMarkdown
{
private CherryMarkdownOption Option { get; } = new();

/// <summary>
/// 获得/设置 编辑器设置
/// </summary>
Expand All @@ -25,6 +23,12 @@ public partial class CherryMarkdown
[Parameter]
public ToolbarSettings? ToolbarSettings { get; set; }

/// <summary>
/// 获得/ 设置 是否使用 Katex 渲染数学公式 默认 true
/// </summary>
[Parameter]
public bool IsSupportMath { get; set; } = true;

private string? _lastValue;
/// <summary>
/// 获得/设置 组件值
Expand Down Expand Up @@ -60,27 +64,7 @@ public partial class CherryMarkdown
/// 获取/设置 组件是否为浏览器模式
/// </summary>
[Parameter]
public bool? IsViewer { get; set; }

/// <summary>
/// OnInitialized 方法
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

_lastValue = Value;
Option.Value = Value;
Option.Editor = EditorSettings ?? new EditorSettings();
Option.Toolbars = ToolbarSettings ?? new ToolbarSettings();
if (IsViewer == true)
{
Option.Editor.DefaultModel = "previewOnly";
Option.Toolbars.Toolbar = false;
}

_lastValue = Value;
}
public bool IsViewer { get; set; }

/// <summary>
/// <inheritdoc/>
Expand All @@ -91,6 +75,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
_lastValue = Value;
return;
}

if (Value != _lastValue)
{
_lastValue = Value;
Expand All @@ -102,7 +92,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Option, nameof(Upload));
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop,
new { Value, IsSupportMath, IsViewer, Editor = EditorSettings ?? new(), Toolbars = ToolbarSettings ?? new() },
nameof(Upload));

/// <summary>
/// 文件上传回调
Expand All @@ -111,7 +103,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
[JSInvokable]
public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
{
#if NET6_0_OR_GREATER
var ret = "";
if (Module != null)
{
Expand All @@ -127,10 +118,6 @@ public async Task<string> Upload(CherryMarkdownUploadFile uploadFile)
}
}
return ret;
#else
await Task.Yield();
throw new NotSupportedException();
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,100 @@
import '../../js/cherry-markdown.min.js'
import { addLink } from '../../../BootstrapBlazor/modules/utility.js'
import '../../js/cherry-markdown.core.js'
import { addLink, addScript } from '../../../BootstrapBlazor/modules/utility.js'
import Data from '../../../BootstrapBlazor/modules/data.js'

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

export async function init(id, invoke, options, callback) {
const el = document.getElementById(id);
if (el === null) {
return;
}
const md = {}
Data.set(id, md)

md._invoker = invoker
md._invokerMethod = callback
md._element = el
md._options = options

if (md._options.toolbars.toolbar === null) {
delete md._options.toolbars.toolbar
}
if (md._options.toolbars.bubble === null) {
delete md._options.toolbars.bubble
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/cherry-markdown.min.css')
if (options.isSupportMath) {
await addScript('./_content/BootstrapBlazor.CherryMarkdown/js/katex.min.js')
await addLink('./_content/BootstrapBlazor.CherryMarkdown/css/katex.min.css')
options.engine = {
syntax: {
mathBlock: { engine: 'katex', },
inlineMath: { engine: 'katex' }
}
}
options.externals = { katex: window.katex };
delete options.isSupportMath;
}
if (md._options.toolbars.float === null) {
delete md._options.toolbars.float
if (options.isViewer) {
options.editor.defaultModel = 'previewOnly';
options.toolbars.Toolbar = false;
}

const fileUpload = (file, callback) => {
md._file = file
md._invoker.invokeMethodAsync(md._invokerMethod, {
const fileUpload = (file, cb) => {
md.file = file
invoke.invokeMethodAsync(callback, {
fileName: file.name,
fileSize: file.size,
contentType: file.type,
lastModified: new Date(file.lastModified).toISOString(),
}).then(data => {
if (data !== "") {
callback(data)
cb(data)
}
})
}

md._editor = new Cherry({
el: md._element,
value: md._options.value,
editor: md._options.editor,
toolbars: md._options.toolbars,
options.editor = {
theme: 'Default',
height: '100%',
defaultModel: 'edit&preview',
convertWhenPaste: true,
...options.editor
}

const op = {
el,
...options,
callback: {
afterChange: (markdown, html) => {
md._invoker.invokeMethodAsync('Update', [markdown, html])
invoke.invokeMethodAsync('Update', [markdown, html])
}
},
fileUpload: fileUpload
});
};
const editor = new Cherry(op);
const md = { invoke, editor };
Data.set(id, md);
}

export function update(id, val) {
const md = Data.get(id)
md._editor.setMarkdown(val, true)
const md = Data.get(id);
const { editor } = md;
if (md) {
editor.setMarkdown(val, true)
}
}

export function fetch(id) {
const md = Data.get(id)
return md._file
const { file } = md;
return file
}

export function invoke(id, method, parameters) {
const md = Data.get(id)
const { editor, invoke } = md;

if (method.indexOf('.') < 0) {
md._editor[method](...parameters)
editor[method](...parameters)
}
else {
const methods = method.split('.');
let m = md._editor[methods[0]];
let m = editor[methods[0]];
for (let i = 1; i < methods.length; i++) {
m = m[methods[i]]
}
m(...parameters);
}
const val = md._editor.getMarkdown();
const html = md._editor.getHtml();
md._invoker.invokeMethodAsync('Update', [val, html]);
const val = editor.getMarkdown();
const html = editor.getHtml();
invoke.invokeMethodAsync('Update', [val, html]);
}

export function dispose(id) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using System.Text.Json.Serialization;

namespace BootstrapBlazor.Components;

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

/// <summary>
/// 编辑器高度,默认为100%
/// </summary>
public string Height { get; set; } = "100%";
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Height { get; set; }

/// <summary>
/// 编辑器显示模式
/// edit&amp;preview: 双栏编辑预览模式,默认值
/// editOnly: 只显示编辑器
/// previewOnly: 预览模式
/// </summary>
public string DefaultModel { get; set; } = "edit&preview";
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? DefaultModel { get; set; }

/// <summary>
/// 粘贴Html时自动转换为Markdown格式
/// 粘贴 Html 时自动转换为 Markdown 格式
/// </summary>
public bool ConvertWhenPaste { get; set; } = true;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? ConvertWhenPaste { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using System.Text.Json.Serialization;

namespace BootstrapBlazor.Components;

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

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

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

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

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading