-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCherryMarkdown.razor.js
More file actions
102 lines (92 loc) · 2.71 KB
/
CherryMarkdown.razor.js
File metadata and controls
102 lines (92 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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, invoke, options, callback) {
const el = document.getElementById(id);
if (el === null) {
return;
}
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 (options.isViewer) {
options.editor.defaultModel = 'previewOnly';
options.toolbars.Toolbar = false;
}
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 !== "") {
cb(data)
}
})
}
options.editor = {
theme: 'Default',
height: '100%',
defaultModel: 'edit&preview',
convertWhenPaste: true,
...options.editor
}
const op = {
el,
...options,
callback: {
afterChange: (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);
const { editor } = md;
if (md) {
editor.setMarkdown(val, true)
}
}
export function fetch(id) {
const md = Data.get(id)
const { file } = md;
return file
}
export function invoke(id, method, parameters) {
const md = Data.get(id)
const { editor, invoke } = md;
if (method.indexOf('.') < 0) {
editor[method](...parameters)
}
else {
const methods = method.split('.');
let m = editor[methods[0]];
for (let i = 1; i < methods.length; i++) {
m = m[methods[i]]
}
m(...parameters);
}
const val = editor.getMarkdown();
const html = editor.getHtml();
invoke.invokeMethodAsync('Update', [val, html]);
}
export function dispose(id) {
Data.remove(id)
}