feat: monaco-markdown
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2023-02-09 07:39:21 +08:00
parent 08a2281d17
commit f1f724caee
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 20 additions and 42 deletions

View File

@ -87,11 +87,13 @@ function blog_editor_init(name, editor_config) {
div_container_editor.wrap('<div class="blog-content-md-editor-in border"></div>'); div_container_editor.wrap('<div class="blog-content-md-editor-in border"></div>');
div_container_editor.append($('<div class="border d-flex justify-content-center align-items-center" style="width: 100%; height: 500px;" />').append('<div class="spinner-border text-muted" style="width: 3rem; height: 3rem;" />')); div_container_editor.append($('<div class="border d-flex justify-content-center align-items-center" style="width: 100%; height: 500px;" />').append('<div class="spinner-border text-muted" style="width: 3rem; height: 3rem;" />'));
require_monaco({}, function() { require_monaco({
markdown: true,
}, function() {
$(div_container_editor).empty(); $(div_container_editor).empty();
var monaco_editor_instance = monaco.editor.create(div_container_editor[0], { var monaco_editor_instance = monaco.editor.create(div_container_editor[0], {
language: editor_config.type == 'slide' ? 'yaml' : 'markdown', language: editor_config.type == 'slide' ? 'yaml' : 'markdown-math',
automaticLayout: true, automaticLayout: true,
fontSize: "16px", fontSize: "16px",
minimap: { minimap: {
@ -111,24 +113,13 @@ function blog_editor_init(name, editor_config) {
input_content_md.val(monaco_editor_instance.getModel().getValue()); input_content_md.val(monaco_editor_instance.getModel().getValue());
}); });
function add_around(sl, sr) {
var selection = monaco_editor_instance.getSelection();
var selection_value = monaco_editor_instance.getModel().getValueInRange(selection);
monaco_editor_instance.executeEdits(sl + selection_value + sr, [{
range: selection,
text: sl + selection_value + sr,
forceMoveMarkers: true,
}]);
}
bold_btn.click(function() { bold_btn.click(function() {
add_around("**", "**"); monaco_editor_instance.trigger(monaco.KeyMod.CtrlCmd + monaco.KeyCode.KEY_B, '', {});
monaco_editor_instance.focus(); monaco_editor_instance.focus();
}); });
italic_btn.click(function() { italic_btn.click(function() {
add_around("_", "_"); monaco_editor_instance.trigger(monaco.KeyMod.CtrlCmd + monaco.KeyCode.KEY_I, '', {});
monaco_editor_instance.focus(); monaco_editor_instance.focus();
}); });
@ -145,32 +136,6 @@ function blog_editor_init(name, editor_config) {
}, },
}); });
monaco_editor_instance.addAction({
id: 'bold',
label: 'Bold',
keybindings: [
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyB,
],
precondition: null,
keybindingContext: null,
run: function(ed) {
bold_btn.click();
},
});
monaco_editor_instance.addAction({
id: 'preview',
label: 'Preview',
keybindings: [
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyD,
],
precondition: null,
keybindingContext: null,
run: function(ed) {
preview_btn.click();
},
});
monaco_editor_instance.addAction({ monaco_editor_instance.addAction({
id: 'italic', id: 'italic',
label: 'Italic', label: 'Italic',
@ -183,6 +148,11 @@ function blog_editor_init(name, editor_config) {
italic_btn.click(); italic_btn.click();
}, },
}); });
require(['MonacoMarkdown'], function(MonacoMarkdown) {
var extension = new MonacoMarkdown.MonacoMarkdownExtension();
extension.activate(monaco_editor_instance);
});
}); });
} }

File diff suppressed because one or more lines are too long

View File

@ -604,7 +604,13 @@ function require_monaco(config, callback) {
.script('/js/monaco-editor/min/vs/editor/editor.main.nls.js').wait() .script('/js/monaco-editor/min/vs/editor/editor.main.nls.js').wait()
.script('/js/monaco-editor/min/vs/editor/editor.main.nls.zh-cn.js').wait() .script('/js/monaco-editor/min/vs/editor/editor.main.nls.zh-cn.js').wait()
.script('/js/monaco-editor/min/vs/editor/editor.main.js').wait(function() { .script('/js/monaco-editor/min/vs/editor/editor.main.js').wait(function() {
$LAB.script('/js/monaco-themes.js').wait(callback); $LAB.script('/js/monaco-themes.js').wait(function() {
if (config.markdown) {
$LAB.script('/js/blog-editor/monaco-markdown.js').wait(callback);
} else {
callback();
}
});
}); });
} }