markdown编辑器增加cherryMarkdown

* 使用cherryMarkdown替换editorMd

* 支持历史&边栏

* 优化代码&支持html格式渲染为预览格式&保存主题配置

* 修复drawio异常

* 优化drawio异常改法

* 自定义提示面板主题颜色

* drawio增加样式,并且更新到最新版本

* 增加代码块复制功能&&修复drawio渲染图片过大&&drawio生成图片背景改为透明

* 恢复原有markdown编辑器,新增cherry markdown编辑器

* 修复复制功能异常

* 修复drawio偶尔无法编辑

---------

Co-authored-by: zhangsheng.93 <zhangsheng.93@bytedance.com>
This commit is contained in:
张胜
2023-07-03 09:41:27 +08:00
committed by GitHub
parent 048135a2d9
commit 21fe4b631a
386 changed files with 254574 additions and 581 deletions

View File

@@ -0,0 +1,698 @@
$(function () {
window.editormdLocales = {
'zh-CN': {
placeholder: '本编辑器支持 Markdown 编辑左边编写右边预览',
contentUnsaved: '编辑内容未保存需要保存吗',
noDocNeedPublish: '没有需要发布的文档',
loadDocFailed: '文档加载失败',
fetchDocFailed: '获取当前文档信息失败',
cannotAddToEmptyNode: '空节点不能添加内容',
overrideModified: '文档已被其他人修改确定覆盖已存在的文档吗',
confirm: '确定',
cancel: '取消',
contentsNameEmpty: '目录名称不能为空',
addDoc: '添加文档',
edit: '编辑',
delete: '删除',
loadFailed: '加载失败请重试',
tplNameEmpty: '模板名称不能为空',
tplContentEmpty: '模板内容不能为空',
saveSucc: '保存成功',
serverExcept: '服务器异常',
paramName: '参数名称',
paramType: '参数类型',
example: '示例值',
remark: '备注',
},
'en': {
placeholder: 'This editor supports Markdown editing, writing on the left and previewing on the right.',
contentUnsaved: 'The edited content is not saved, need to save it?',
noDocNeedPublish: 'No Document need to be publish',
loadDocFailed: 'Load Document failed',
fetchDocFailed: 'Fetch Document info failed',
cannotAddToEmptyNode: 'Cannot add content to empty node',
overrideModified: 'The document has been modified by someone else, are you sure to overwrite the document?',
confirm: 'Confirm',
cancel: 'Cancel',
contentsNameEmpty: 'Document Name cannot be empty',
addDoc: 'Add Document',
edit: 'Edit',
delete: 'Delete',
loadFailed: 'Failed to load, please try again',
tplNameEmpty: 'Template name cannot be empty',
tplContentEmpty: 'Template content cannot be empty',
saveSucc: 'Save success',
serverExcept: 'Server Exception',
paramName: 'Parameter',
paramType: 'Type',
example: 'Example',
remark: 'Remark',
}
};
var CustomHookA = Cherry.createSyntaxHook('codeBlock', Cherry.constants.HOOKS_TYPE_LIST.PAR, {
makeHtml(str) {
console.warn('custom hook', 'hello');
return str;
},
rule(str) {
const regex = {
begin: '',
content: '',
end: '',
};
regex.reg = new RegExp(regex.begin + regex.content + regex.end, 'g');
return regex;
},
});
/**
* 自定义一个自定义菜单
* 点第一次时,把选中的文字变成同时加粗和斜体
* 保持光标选区不变,点第二次时,把加粗斜体的文字变成普通文本
*/
var customMenuA = Cherry.createMenuHook('加粗斜体', {
iconName: 'font',
onClick: function (selection) {
// 获取用户选中的文字调用getSelection方法后如果用户没有选中任何文字会尝试获取光标所在位置的单词或句子
let $selection = this.getSelection(selection) || '同时加粗斜体';
// 如果是单选,并且选中内容的开始结束内没有加粗语法,则扩大选中范围
if (!this.isSelections && !/^\s*(\*\*\*)[\s\S]+(\1)/.test($selection)) {
this.getMoreSelection('***', '***', () => {
const newSelection = this.editor.editor.getSelection();
const isBoldItalic = /^\s*(\*\*\*)[\s\S]+(\1)/.test(newSelection);
if (isBoldItalic) {
$selection = newSelection;
}
return isBoldItalic;
});
}
// 如果选中的文本中已经有加粗语法了,则去掉加粗语法
if (/^\s*(\*\*\*)[\s\S]+(\1)/.test($selection)) {
return $selection.replace(/(^)(\s*)(\*\*\*)([^\n]+)(\3)(\s*)($)/gm, '$1$4$7');
}
/**
* 注册缩小选区的规则
* 注册后,插入“***TEXT***”,选中状态会变成“***【TEXT】***”
* 如果不注册,插入后效果为:“【***TEXT***】”
*/
this.registerAfterClickCb(() => {
this.setLessSelection('***', '***');
});
return $selection.replace(/(^)([^\n]+)($)/gm, '$1***$2***$3');
}
});
/**
* 定义一个空壳用于自行规划cherry已有工具栏的层级结构
*/
var customMenuB = Cherry.createMenuHook('发布', {
iconName: '',
onClick: releaseDocument,
});
var customMenuC = Cherry.createMenuHook("返回", {
iconName: '',
onClick: backWard,
})
var customMenuD = Cherry.createMenuHook('保存', {
iconName: '',
onClick: saveDocument,
});
var customMenuE = Cherry.createMenuHook('边栏', {
iconName: '',
onClick: siderChange,
});
var customMenuF = Cherry.createMenuHook('历史', {
iconName: '',
onClick: showHistory,
});
var basicConfig = {
id: 'manualEditorContainer',
externals: {
echarts: window.echarts,
katex: window.katex,
MathJax: window.MathJax,
},
isPreviewOnly: false,
engine: {
global: {
urlProcessor(url, srcType) {
//console.log(`url-processor`, url, srcType);
return url;
},
},
syntax: {
codeBlock: {
theme: 'twilight',
},
table: {
enableChart: false,
// chartEngine: Engine Class
},
fontEmphasis: {
allowWhitespace: false, // 是否允许首尾空格
},
strikethrough: {
needWhitespace: false, // 是否必须有前后空格
},
mathBlock: {
engine: 'MathJax', // katex或MathJax
src: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js', // 如果使用MathJax plugins则需要使用该url通过script标签引入
},
inlineMath: {
engine: 'MathJax', // katex或MathJax
},
emoji: {
useUnicode: false,
customResourceURL: 'https://github.githubassets.com/images/icons/emoji/unicode/${code}.png?v8',
upperCase: true,
},
// toc: {
// tocStyle: 'nested'
// }
// 'header': {
// strict: false
// }
},
customSyntax: {
// SyntaxHookClass
CustomHook: {
syntaxClass: CustomHookA,
force: false,
after: 'br',
},
},
},
toolbars: {
toolbar: [
'customMenuCName',
'undo',
'redo',
'bold',
'italic',
{
strikethrough: ['strikethrough', 'underline', 'sub', 'sup', 'ruby', 'customMenuAName'],
},
'size',
'|',
'color',
'header',
'|',
'drawIo',
'|',
'ol',
'ul',
'checklist',
'panel',
'detail',
'|',
'formula',
{
insert: ['image', 'audio', 'video', 'link', 'hr', 'br', 'code', 'formula', 'toc', 'table', 'pdf', 'word', 'ruby'],
},
'graph',
'togglePreview',
'settings',
'switchModel',
'codeTheme',
'export',
'customMenuDName',
'customMenuBName',
'customMenuEName',
'customMenuFName',
'theme'
],
bubble: ['bold', 'italic', 'underline', 'strikethrough', 'sub', 'sup', 'quote', 'ruby', '|', 'size', 'color'], // array or false
sidebar: ['mobilePreview', 'copy', 'theme'],
customMenu: {
customMenuAName: customMenuA,
customMenuBName: customMenuB,
customMenuCName: customMenuC,
customMenuDName: customMenuD,
customMenuEName: customMenuE,
customMenuFName: customMenuF,
},
},
drawioIframeUrl: '/static/cherry/drawio_demo.html',
editor: {
defaultModel: 'edit&preview',
height: "100%",
},
previewer: {
// 自定义markdown预览区域class
// className: 'markdown'
},
keydown: [],
//extensions: [],
//callback: {
//changeString2Pinyin: pinyin,
//}
};
fetch('').then((response) => response.text()).then((value) => {
//var markdownarea = document.getElementById("markdown_area").value
var config = Object.assign({}, basicConfig);// { value: markdownarea });// { value: value });不显示获取的初始化值
window.editor = new Cherry(config);
openLastSelectedNode();
uploadImage("manualEditorContainer", function ($state, $res) {
console.log("注册上传图片")
if ($state === "before") {
return layer.load(1, {
shade: [0.1, '#fff'] // 0.1 透明度的白色背景
});
} else if ($state === "success") {
if ($res.errcode === 0) {
var value = '![](' + $res.url + ')';
window.editor.insertValue(value);
}
}
});
});
function insertToMarkdown(body) {
window.isLoad = true;
window.editor.insertValue(body);
window.editor.setCursor({ line: 0, ch: 0 });
resetEditorChanged(true);
}
function insertAndClearToMarkdown(body) {
window.isLoad = true;
window.editor.clear();
window.editor.insertValue(body);
window.editor.setCursor({ line: 0, ch: 0 });
resetEditorChanged(true);
}
/***
* 加载指定的文档到编辑器中
* @param $node
*/
window.loadDocument = function ($node) {
var index = layer.load(1, {
shade: [0.1, '#fff'] // 0.1 透明度的白色背景
});
$.get(window.editURL + $node.node.id).done(function (res) {
layer.close(index);
if (res.errcode === 0) {
window.isLoad = true;
try {
window.editor.setTheme(res.data.markdown_theme);
window.editor.setMarkdown(res.data.markdown);
} catch (e) {
console.log(e);
}
var node = { "id": res.data.doc_id, 'parent': res.data.parent_id === 0 ? '#' : res.data.parent_id, "text": res.data.doc_name, "identify": res.data.identify, "version": res.data.version };
pushDocumentCategory(node);
window.selectNode = node;
pushVueLists(res.data.attach);
setLastSelectNode($node);
} else {
layer.msg(editormdLocales[lang].loadDocFailed);
}
}).fail(function () {
layer.close(index);
layer.msg(editormdLocales[lang].loadDocFailed);
});
};
/**
* 保存文档到服务器
* @param $is_cover 是否强制覆盖
*/
function saveDocument($is_cover, callback) {
var index = null;
var node = window.selectNode;
var content = window.editor.getMarkdown();
var html = window.editor.getHtml(true);
var markdownTheme = window.editor.getTheme();
var version = "";
if (!node) {
layer.msg(editormdLocales[lang].fetchDocFailed);
return;
}
if (node.a_attr && node.a_attr.disabled) {
layer.msg(editormdLocales[lang].cannotAddToEmptyNode);
return;
}
var doc_id = parseInt(node.id);
for (var i in window.documentCategory) {
var item = window.documentCategory[i];
if (item.id === doc_id) {
version = item.version;
break;
}
}
$.ajax({
beforeSend: function () {
index = layer.load(1, { shade: [0.1, '#fff'] });
window.saveing = true;
},
url: window.editURL,
data: { "identify": window.book.identify, "doc_id": doc_id, "markdown": content, "html": html, "markdown_theme": markdownTheme, "cover": $is_cover ? "yes" : "no", "version": version },
type: "post",
timeout: 30000,
dataType: "json",
success: function (res) {
if (res.errcode === 0) {
resetEditorChanged(false);
for (var i in window.documentCategory) {
var item = window.documentCategory[i];
if (item.id === doc_id) {
window.documentCategory[i].version = res.data.version;
break;
}
}
$.each(window.documentCategory, function (i, item) {
var $item = window.documentCategory[i];
if (item.id === doc_id) {
window.documentCategory[i].version = res.data.version;
}
});
if (typeof callback === "function") {
callback();
}
} else if (res.errcode === 6005) {
var confirmIndex = layer.confirm(editormdLocales[lang].overrideModified, {
btn: [editormdLocales[lang].confirm, editormdLocales[lang].cancel] // 按钮
}, function () {
layer.close(confirmIndex);
saveDocument(true, callback);
});
} else {
layer.msg(res.message);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg(window.editormdLocales[window.lang].serverExcept + errorThrown);
},
complete: function () {
layer.close(index);
window.saveing = false;
}
});
}
/**
* 设置编辑器变更状态
* @param $is_change
*/
function resetEditorChanged($is_change) {
if ($is_change && !window.isLoad) {
$("#markdown-save").removeClass('disabled').addClass('change');
} else {
$("#markdown-save").removeClass('change').addClass('disabled');
}
window.isLoad = false;
}
/**
* 返回上一个页面
*/
function backWard() {
history.back();
}
/**
* 发布文档
*/
function releaseDocument() {
if (Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0) {
if ($("#markdown-save").hasClass('change')) {
var confirm_result = confirm(editormdLocales[lang].contentUnsaved);
if (confirm_result) {
saveDocument(false, releaseBook);
return;
}
}
releaseBook();
return
}
layer.msg(editormdLocales[lang].noDocNeedPublish)
}
/**
* 显示/隐藏边栏
*/
function siderChange() {
$("#manualCategory").toggle(0, "swing", function () {
var $then = $("#manualEditorContainer");
var left = parseInt($then.css("left"));
if (left > 0) {
window.editorContainerLeft = left;
$then.css("left", "0");
} else {
$then.css("left", window.editorContainerLeft + "px");
}
});
}
/**
* 显示文档历史
*/
function showHistory() {
window.documentHistory();
}
/**
* 添加文档
*/
$("#addDocumentForm").ajaxForm({
beforeSubmit: function () {
var doc_name = $.trim($("#documentName").val());
if (doc_name === "") {
return showError(editormdLocales[lang].contentsNameEmpty, "#add-error-message")
}
$("#btnSaveDocument").button("loading");
return true;
},
success: function (res) {
if (res.errcode === 0) {
var data = {
"id": res.data.doc_id,
'parent': res.data.parent_id === 0 ? '#' : res.data.parent_id,
"text": res.data.doc_name,
"identify": res.data.identify,
"version": res.data.version,
state: { opened: res.data.is_open == 1 },
a_attr: { is_open: res.data.is_open == 1 }
};
var node = window.treeCatalog.get_node(data.id);
if (node) {
window.treeCatalog.rename_node({ "id": data.id }, data.text);
$("#sidebar").jstree(true).get_node(data.id).a_attr.is_open = data.state.opened;
} else {
window.treeCatalog.create_node(data.parent, data);
window.treeCatalog.deselect_all();
window.treeCatalog.select_node(data);
}
pushDocumentCategory(data);
$("#markdown-save").removeClass('change').addClass('disabled');
$("#addDocumentModal").modal('hide');
} else {
showError(res.message, "#add-error-message");
}
$("#btnSaveDocument").button("reset");
}
});
/**
* 文档目录树
*/
$("#sidebar").jstree({
'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
"types": {
"default": {
"icon": false // 删除默认图标
}
},
'core': {
'check_callback': true,
"multiple": false,
'animation': 0,
"data": window.documentCategory
},
"contextmenu": {
show_at_node: false,
select_node: false,
"items": {
"添加文档": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": window.editormdLocales[window.lang].addDoc,//"添加文档",
"icon": "fa fa-plus",
"action": function (data) {
var inst = $.jstree.reference(data.reference),
node = inst.get_node(data.reference);
openCreateCatalogDialog(node);
}
},
"编辑": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": window.editormdLocales[window.lang].edit,
"icon": "fa fa-edit",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
openEditCatalogDialog(node);
}
},
"删除": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": window.editormdLocales[window.lang].delete,
"icon": "fa fa-trash-o",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
openDeleteDocumentDialog(node);
}
}
}
}
}).on("ready.jstree", function () {
window.treeCatalog = $("#sidebar").jstree(true);
//如果没有选中节点则选中默认节点
// openLastSelectedNode();
}).on('select_node.jstree', function (node, selected) {
if ($("#markdown-save").hasClass('change')) {
if (confirm(window.editormdLocales[window.lang].contentUnsaved)) {
saveDocument(false, function () {
loadDocument(selected);
});
return true;
}
}
//如果是空目录则直接出发展开下一级功能
if (selected.node.a_attr && selected.node.a_attr.disabled) {
selected.instance.toggle_node(selected.node);
return false
}
loadDocument(selected);
}).on("move_node.jstree", jstree_save).on("delete_node.jstree", function ($node, $parent) {
openLastSelectedNode();
});
/**
* 打开文档模板
*/
$("#documentTemplateModal").on("click", ".section>a[data-type]", function () {
var $this = $(this).attr("data-type");
if ($this === "customs") {
$("#displayCustomsTemplateModal").modal("show");
return;
}
var body = $("#template-" + $this).html();
if (body) {
window.isLoad = true;
window.editor.clear();
window.editor.insertValue(body);
window.editor.setCursor({ line: 0, ch: 0 });
resetEditorChanged(true);
}
$("#documentTemplateModal").modal('hide');
});
});
function uploadImage($id, $callback) {
locales = {
'zh-CN': {
unsupportType: '不支持的图片格式',
uploadFailed: '图片上传失败'
},
'en': {
unsupportType: 'Unsupport image type',
uploadFailed: 'Upload image failed'
}
}
/** 粘贴上传图片 **/
document.getElementById($id).addEventListener('paste', function (e) {
if (e.clipboardData && e.clipboardData.items) {
var clipboard = e.clipboardData;
for (var i = 0, len = clipboard.items.length; i < len; i++) {
if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
var imageFile = clipboard.items[i].getAsFile();
var fileName = String((new Date()).valueOf());
switch (imageFile.type) {
case "image/png" :
fileName += ".png";
break;
case "image/jpg" :
fileName += ".jpg";
break;
case "image/jpeg" :
fileName += ".jpeg";
break;
case "image/gif" :
fileName += ".gif";
break;
default :
layer.msg(locales[lang].unsupportType);
return;
}
var form = new FormData();
form.append('editormd-image-file', imageFile, fileName);
var layerIndex = 0;
$.ajax({
url: window.imageUploadURL,
type: "POST",
dataType: "json",
data: form,
processData: false,
contentType: false,
beforeSend: function () {
layerIndex = $callback('before');
},
error: function () {
layer.close(layerIndex);
$callback('error');
layer.msg(locales[lang].uploadFailed);
},
success: function (data) {
layer.close(layerIndex);
$callback('success', data);
if (data.errcode !== 0) {
layer.msg(data.message);
}
}
});
e.preventDefault();
}
}
}
});
}

7
static/js/clipboard.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -19,7 +19,7 @@ var events = function () {
$(".manual-right").scrollTop(0);
//使用layer相册功能查看图片
layer.photos({photos: "#page-content"});
layer.photos({ photos: "#page-content" });
};
return {
@@ -65,13 +65,13 @@ function pageClicked($page, $docid) {
}
$("#articleComment").removeClass('not-show-comment');
$.ajax({
url : "/comment/lists?page=" + $page + "&docid=" + $docid,
type : "GET",
success : function ($res) {
url: "/comment/lists?page=" + $page + "&docid=" + $docid,
type: "GET",
success: function ($res) {
console.log($res.data);
loadComment($res.data.page, $res.data.doc_id);
},
error : function () {
error: function () {
layer.msg("加载失败");
}
});
@@ -84,15 +84,15 @@ function loadComment($page, $docid) {
var c = $page.List;
for (var i = 0; c && i < c.length; i++) {
html += "<div class=\"comment-item\" data-id=\"" + c[i].comment_id + "\">";
html += "<p class=\"info\"><a class=\"name\">" + c[i].author + "</a><span class=\"date\">" + timeFormat(c[i].comment_date) + "</span></p>";
html += "<div class=\"content\">" + c[i].content + "</div>";
html += "<p class=\"util\">";
if (c[i].show_del == 1) html += "<span class=\"operate toggle\">";
else html += "<span class=\"operate\">";
html += "<span class=\"number\">" + c[i].index + "#</span>";
if (c[i].show_del == 1) html += "<i class=\"delete e-delete glyphicon glyphicon-remove\" style=\"color:red\" onclick=\"onDelComment(" + c[i].comment_id + ")\"></i>";
html += "</span>";
html += "</p>";
html += "<p class=\"info\"><a class=\"name\">" + c[i].author + "</a><span class=\"date\">" + timeFormat(c[i].comment_date) + "</span></p>";
html += "<div class=\"content\">" + c[i].content + "</div>";
html += "<p class=\"util\">";
if (c[i].show_del == 1) html += "<span class=\"operate toggle\">";
else html += "<span class=\"operate\">";
html += "<span class=\"number\">" + c[i].index + "#</span>";
if (c[i].show_del == 1) html += "<i class=\"delete e-delete glyphicon glyphicon-remove\" style=\"color:red\" onclick=\"onDelComment(" + c[i].comment_id + ")\"></i>";
html += "</span>";
html += "</p>";
html += "</div>";
}
$("#commentList").append(html);
@@ -103,7 +103,7 @@ function loadComment($page, $docid) {
totalPages: $page.TotalPage,
bootstrapMajorVersion: 3,
size: "middle",
onPageClicked: function(e, originalEvent, type, page){
onPageClicked: function (e, originalEvent, type, page) {
pageClicked(page, $docid);
}
});
@@ -116,10 +116,10 @@ function loadComment($page, $docid) {
function onDelComment($id) {
console.log($id);
$.ajax({
url : "/comment/delete",
data : {"id": $id},
type : "POST",
success : function ($res) {
url: "/comment/delete",
data: { "id": $id },
type: "POST",
success: function ($res) {
if ($res.errcode == 0) {
layer.msg("删除成功");
$("div[data-id=" + $id + "]").remove();
@@ -127,7 +127,7 @@ function onDelComment($id) {
layer.msg($res.message);
}
},
error : function () {
error: function () {
layer.msg("删除失败");
}
});
@@ -143,10 +143,19 @@ function renderPage($data) {
$("#doc_id").val($data.doc_id);
if ($data.page) {
loadComment($data.page, $data.doc_id);
}
else {
pageClicked(-1, $data.doc_id);
}
if ($data.is_markdown) {
if ($("#view_container").hasClass($data.markdown_theme)) {
return
}
$("#view_container").removeClass("theme__dark theme__green theme__light theme__red theme__default")
$("#view_container").addClass($data.markdown_theme)
}
}
/***
@@ -157,19 +166,19 @@ function renderPage($data) {
*/
function loadDocument($url, $id, $callback) {
$.ajax({
url : $url,
type : "GET",
beforeSend : function () {
url: $url,
type: "GET",
beforeSend: function () {
var data = events.data($id);
if(data) {
if (data) {
if (typeof $callback === "function") {
data.body = $callback(data.body);
}else if(data.version && data.version != $callback){
} else if (data.version && data.version != $callback) {
return true;
}
renderPage(data);
events.trigger('article.open', {$url: $url, $id: $id});
events.trigger('article.open', { $url: $url, $id: $id });
return false;
@@ -177,28 +186,29 @@ function loadDocument($url, $id, $callback) {
NProgress.start();
},
success : function ($res) {
success: function ($res) {
if ($res.errcode === 0) {
renderPage($res.data);
$body = $res.data.body;
if (typeof $callback === "function" ) {
if (typeof $callback === "function") {
$body = $callback(body);
}
loadCopySnippets();
events.data($id, $res.data);
events.trigger('article.open', { $url : $url, $id : $id });
events.trigger('article.open', { $url: $url, $id: $id });
} else if ($res.errcode === 6000) {
window.location.href = "/";
} else {
layer.msg("加载失败");
}
},
complete : function () {
complete: function () {
NProgress.done();
},
error : function () {
error: function () {
layer.msg("加载失败");
}
});
@@ -216,7 +226,7 @@ function initHighlighting() {
hljs.highlightBlock(block);
});
// hljs.initLineNumbersOnLoad();
}catch (e){
} catch (e) {
console.log(e);
}
}
@@ -233,41 +243,41 @@ $(function () {
} else {
$(".view-backtop").removeClass("active");
}
}catch (e) {
} catch (e) {
console.log(e);
}
try{
try {
var scrollTop = $("body").scrollTop();
var oItem = $(".markdown-heading").find(".reference-link");
var oName = "";
$.each(oItem,function(){
$.each(oItem, function () {
var oneItem = $(this);
var offsetTop = oneItem.offset().top;
if(offsetTop-scrollTop < 100){
if (offsetTop - scrollTop < 100) {
oName = "#" + oneItem.attr("name");
}
});
$(".markdown-toc-list a").each(function () {
if(oName === $(this).attr("href")) {
if (oName === $(this).attr("href")) {
$(this).parents("li").addClass("directory-item-active");
}else{
} else {
$(this).parents("li").removeClass("directory-item-active");
}
});
if(!$(".markdown-toc-list li").hasClass('directory-item-active')) {
if (!$(".markdown-toc-list li").hasClass('directory-item-active')) {
$(".markdown-toc-list li:eq(0)").addClass("directory-item-active");
}
}catch (e) {
} catch (e) {
console.log(e);
}
}).on("click",".markdown-toc-list a", function () {
}).on("click", ".markdown-toc-list a", function () {
var $this = $(this);
setTimeout(function () {
$(".markdown-toc-list li").removeClass("directory-item-active");
$this.parents("li").addClass("directory-item-active");
},10);
}, 10);
}).find(".markdown-toc-list li:eq(0)").addClass("directory-item-active");
@@ -280,16 +290,16 @@ $(function () {
initHighlighting();
window.jsTree = $("#sidebar").jstree({
'plugins' : ["wholerow", "types"],
'plugins': ["wholerow", "types"],
"types": {
"default" : {
"icon" : false // 删除默认图标
"default": {
"icon": false // 删除默认图标
}
},
'core' : {
'check_callback' : true,
"multiple" : false,
'animation' : 0
'core': {
'check_callback': true,
"multiple": false,
'animation': 0
}
}).on('select_node.jstree', function (node, selected) {
//如果是空目录则直接出发展开下一级功能
@@ -298,7 +308,7 @@ $(function () {
return false
}
$(".m-manual").removeClass('manual-mobile-show-left');
loadDocument(selected.node.a_attr.href, selected.node.id,selected.node.a_attr['data-version']);
loadDocument(selected.node.a_attr.href, selected.node.id, selected.node.a_attr['data-version']);
});
$("#slidebar").on("click", function () {
@@ -330,7 +340,7 @@ $(function () {
* 项目内搜索
*/
$("#searchForm").ajaxForm({
beforeSubmit : function () {
beforeSubmit: function () {
var keyword = $.trim($("#searchForm").find("input[name='keyword']").val());
if (keyword === "") {
$(".search-empty").show();
@@ -340,10 +350,10 @@ $(function () {
$("#btnSearch").attr("disabled", "disabled").find("i").removeClass("fa-search").addClass("loading");
window.keyword = keyword;
},
success : function (res) {
success: function (res) {
var html = "";
if (res.errcode === 0) {
for(var i in res.data) {
for (var i in res.data) {
var item = res.data[i];
html += '<li><a href="javascript:;" title="' + item.doc_name + '" data-id="' + item.doc_id + '"> ' + item.doc_name + ' </a></li>';
}
@@ -355,7 +365,7 @@ $(function () {
}
$("#searchList").html(html);
},
complete : function () {
complete: function () {
$("#btnSearch").removeAttr("disabled").find("i").removeClass("loading").addClass("fa-search");
}
});
@@ -363,12 +373,12 @@ $(function () {
window.onpopstate = function (e) {
var $param = e.state;
if (!$param) return;
if($param.hasOwnProperty("$url")) {
if ($param.hasOwnProperty("$url")) {
window.jsTree.jstree().deselect_all();
if ($param.$id) {
window.jsTree.jstree().select_node({ id : $param.$id });
}else{
window.jsTree.jstree().select_node({ id: $param.$id });
} else {
window.location.assign($param.$url);
}
// events.trigger('article.open', $param);
@@ -379,22 +389,34 @@ $(function () {
// 提交评论
$("#commentForm").ajaxForm({
beforeSubmit : function () {
beforeSubmit: function () {
$("#btnSubmitComment").button("loading");
},
success : function (res) {
if(res.errcode === 0){
success: function (res) {
if (res.errcode === 0) {
layer.msg("保存成功");
}else{
} else {
layer.msg(res.message);
}
$("#btnSubmitComment").button("reset");
$("#commentContent").val("");
pageClicked(-1, res.data.doc_id); // -1 表示请求最后一页
},
error : function () {
error: function () {
layer.msg("服务错误");
$("#btnSubmitComment").button("reset");
}
});
});
loadCopySnippets();
});
function loadCopySnippets() {
$("pre").addClass("line-numbers language-bash");
$("pre").attr('data-prismjs-copy', '复制');
$("pre").attr('data-prismjs-copy-error', '按Ctrl+C复制');
$("pre").attr('data-prismjs-copy-success', '代码已复制');
var snippets = document.querySelectorAll('pre code');
[].forEach.call(snippets, function (snippet) {
Prism.highlightElement(snippet);
});
}

View File

@@ -1,7 +1,7 @@
$(function () {
editormd.katexURL = {
js : window.katex.js,
css : window.katex.css
js: window.katex.js,
css: window.katex.css
};
window.editormdLocales = {
@@ -54,7 +54,7 @@ $(function () {
remark: 'Remark',
}
};
var htmlDecodeList = ["style","script","title","onmouseover","onmouseout","style"];
var htmlDecodeList = ["style", "script", "title", "onmouseover", "onmouseout", "style"];
if (!window.IS_ENABLE_IFRAME) {
htmlDecodeList.unshift("iframe");
}
@@ -65,7 +65,7 @@ $(function () {
toolbar: true,
placeholder: window.editormdLocales[window.lang].placeholder,
imageUpload: true,
imageFormats: ["jpg", "jpeg", "gif", "png","svg", "JPG", "JPEG", "GIF", "PNG","SVG"],
imageFormats: ["jpg", "jpeg", "gif", "png", "svg", "JPG", "JPEG", "GIF", "PNG", "SVG"],
imageUploadURL: window.imageUploadURL,
toolbarModes: "full",
fileUpload: true,
@@ -79,19 +79,19 @@ $(function () {
tocm: true,
previewCodeHighlight: 1,
highlightStyle: window.highlightStyle ? window.highlightStyle : "github",
tex:true,
tex: true,
saveHTMLToTextarea: true,
onload: function() {
onload: function () {
this.hideToolbar();
var keyMap = {
"Ctrl-S": function(cm) {
"Ctrl-S": function (cm) {
saveDocument(false);
},
"Cmd-S": function(cm){
"Cmd-S": function (cm) {
saveDocument(false);
},
"Ctrl-A": function(cm) {
"Ctrl-A": function (cm) {
cm.execCommand("selectAll");
}
};
@@ -111,7 +111,7 @@ $(function () {
}
}
});
window.isLoad = true;
},
onchange: function () {
@@ -137,20 +137,20 @@ $(function () {
* 实现标题栏操作
*/
$("#editormd-tools").on("click", "a[class!='disabled']", function () {
var name = $(this).find("i").attr("name");
if (name === "attachment") {
$("#uploadAttachModal").modal("show");
} else if (name === "history") {
window.documentHistory();
} else if (name === "save") {
var name = $(this).find("i").attr("name");
if (name === "attachment") {
$("#uploadAttachModal").modal("show");
} else if (name === "history") {
window.documentHistory();
} else if (name === "save") {
saveDocument(false);
} else if (name === "template") {
$("#documentTemplateModal").modal("show");
} else if(name === "save-template"){
$("#saveTemplateModal").modal("show");
} else if(name === 'json'){
$("#convertJsonToTableModal").modal("show");
} else if (name === "sidebar") {
} else if (name === "template") {
$("#documentTemplateModal").modal("show");
} else if (name === "save-template") {
$("#saveTemplateModal").modal("show");
} else if (name === 'json') {
$("#convertJsonToTableModal").modal("show");
} else if (name === "sidebar") {
$("#manualCategory").toggle(0, "swing", function () {
var $then = $("#manualEditorContainer");
var left = parseInt($then.css("left"));
@@ -162,7 +162,7 @@ $(function () {
}
window.editor.resize();
});
} else if (name === "release") {
} else if (name === "release") {
if (Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0) {
if ($("#markdown-save").hasClass('change')) {
var confirm_result = confirm(editormdLocales[lang].contentUnsaved);
@@ -176,43 +176,43 @@ $(function () {
} else {
layer.msg(editormdLocales[lang].noDocNeedPublish)
}
} else if (name === "tasks") {
// 插入 GFM 任务列表
var cm = window.editor.cm;
var selection = cm.getSelection();
var cursor = cm.getCursor();
if (selection === "") {
cm.setCursor(cursor.line, 0);
cm.replaceSelection("- [x] " + selection);
cm.setCursor(cursor.line, cursor.ch + 6);
} else {
var selectionText = selection.split("\n");
} else if (name === "tasks") {
// 插入 GFM 任务列表
var cm = window.editor.cm;
var selection = cm.getSelection();
var cursor = cm.getCursor();
if (selection === "") {
cm.setCursor(cursor.line, 0);
cm.replaceSelection("- [x] " + selection);
cm.setCursor(cursor.line, cursor.ch + 6);
} else {
var selectionText = selection.split("\n");
for (var i = 0, len = selectionText.length; i < len; i++) {
selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
}
cm.replaceSelection(selectionText.join("\n"));
}
} else {
var action = window.editor.toolbarHandlers[name];
for (var i = 0, len = selectionText.length; i < len; i++) {
selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
}
cm.replaceSelection(selectionText.join("\n"));
}
} else {
var action = window.editor.toolbarHandlers[name];
if (!!action && action !== "undefined") {
$.proxy(action, window.editor)();
window.editor.focus();
}
}
}) ;
if (!!action && action !== "undefined") {
$.proxy(action, window.editor)();
window.editor.focus();
}
}
});
/***
* 加载指定的文档到编辑器中
* @param $node
*/
window.loadDocument = function($node) {
window.loadDocument = function ($node) {
var index = layer.load(1, {
shade: [0.1, '#fff'] // 0.1 透明度的白色背景
});
$.get(window.editURL + $node.node.id ).done(function (res) {
$.get(window.editURL + $node.node.id).done(function (res) {
layer.close(index);
if (res.errcode === 0) {
@@ -220,8 +220,8 @@ $(function () {
try {
window.editor.clear();
window.editor.insertValue(res.data.markdown);
window.editor.setCursor({line: 0, ch: 0});
}catch(e){
window.editor.setCursor({ line: 0, ch: 0 });
} catch (e) {
console.log(e);
}
var node = { "id": res.data.doc_id, 'parent': res.data.parent_id === 0 ? '#' : res.data.parent_id, "text": res.data.doc_name, "identify": res.data.identify, "version": res.data.version };
@@ -276,7 +276,7 @@ $(function () {
url: window.editURL,
data: { "identify": window.book.identify, "doc_id": doc_id, "markdown": content, "html": html, "cover": $is_cover ? "yes" : "no", "version": version },
type: "post",
timeout : 30000,
timeout: 30000,
dataType: "json",
success: function (res) {
if (res.errcode === 0) {
@@ -289,7 +289,7 @@ $(function () {
break;
}
}
$.each(window.documentCategory,function (i, item) {
$.each(window.documentCategory, function (i, item) {
var $item = window.documentCategory[i];
if (item.id === doc_id) {
@@ -300,10 +300,10 @@ $(function () {
callback();
}
} else if(res.errcode === 6005) {
} else if (res.errcode === 6005) {
var confirmIndex = layer.confirm(editormdLocales[lang].overrideModified, {
btn: [editormdLocales[lang].confirm, editormdLocales[lang].cancel] // 按钮
}, function() {
}, function () {
layer.close(confirmIndex);
saveDocument(true, callback);
});
@@ -311,16 +311,16 @@ $(function () {
layer.msg(res.message);
}
},
error : function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg(window.editormdLocales[window.lang].serverExcept + errorThrown);
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg(window.editormdLocales[window.lang].serverExcept + errorThrown);
},
complete :function () {
complete: function () {
layer.close(index);
window.saveing = false;
}
});
}
/**
* 设置编辑器变更状态
@@ -351,12 +351,12 @@ $(function () {
if (res.errcode === 0) {
var data = {
"id": res.data.doc_id,
'parent': res.data.parent_id === 0 ? '#' : res.data.parent_id ,
'parent': res.data.parent_id === 0 ? '#' : res.data.parent_id,
"text": res.data.doc_name,
"identify": res.data.identify,
"version": res.data.version ,
state: { opened: res.data.is_open == 1},
a_attr: { is_open: res.data.is_open == 1}
"version": res.data.version,
state: { opened: res.data.is_open == 1 },
a_attr: { is_open: res.data.is_open == 1 }
};
var node = window.treeCatalog.get_node(data.id);
@@ -437,7 +437,7 @@ $(function () {
}
}
}
}).on("ready.jstree",function () {
}).on("ready.jstree", function () {
window.treeCatalog = $("#sidebar").jstree(true);
//如果没有选中节点则选中默认节点
@@ -460,7 +460,7 @@ $(function () {
loadDocument(selected);
}).on("move_node.jstree", jstree_save).on("delete_node.jstree",function($node,$parent) {
}).on("move_node.jstree", jstree_save).on("delete_node.jstree", function ($node, $parent) {
openLastSelectedNode();
});
/**
@@ -468,7 +468,7 @@ $(function () {
*/
$("#documentTemplateModal").on("click", ".section>a[data-type]", function () {
var $this = $(this).attr("data-type");
if($this === "customs"){
if ($this === "customs") {
$("#displayCustomsTemplateModal").modal("show");
return;
}
@@ -485,30 +485,30 @@ $(function () {
/**
* 展示自定义模板列表
*/
$("#displayCustomsTemplateModal").on("show.bs.modal",function () {
window.sessionStorage.setItem("displayCustomsTemplateList",$("#displayCustomsTemplateList").html());
$("#displayCustomsTemplateModal").on("show.bs.modal", function () {
window.sessionStorage.setItem("displayCustomsTemplateList", $("#displayCustomsTemplateList").html());
var index ;
var index;
$.ajax({
beforeSend: function () {
index = layer.load(1, { shade: [0.1, '#fff'] });
},
url : window.template.listUrl,
data: {"identify":window.book.identify},
type: "POST",
dataType: "html",
url: window.template.listUrl,
data: { "identify": window.book.identify },
type: "POST",
dataType: "html",
success: function ($res) {
$("#displayCustomsTemplateList").html($res);
},
error : function () {
error: function () {
layer.msg(window.editormdLocales[window.lang].loadFailed);
},
complete : function () {
complete: function () {
layer.close(index);
}
});
$("#documentTemplateModal").modal("hide");
}).on("hidden.bs.modal",function () {
}).on("hidden.bs.modal", function () {
var cache = window.sessionStorage.getItem("displayCustomsTemplateList");
$("#displayCustomsTemplateList").html(cache);
});
@@ -522,7 +522,7 @@ $(function () {
return showError(window.editormdLocales[window.lang].tplNameEmpty, "#saveTemplateForm .show-error-message");
}
var content = $("#saveTemplateForm").find("input[name='content']").val();
if (content === ""){
if (content === "") {
return showError(window.editormdLocales[window.lang].tplContentEmpty, "#saveTemplateForm .show-error-message");
}
@@ -531,71 +531,71 @@ $(function () {
return true;
},
success: function ($res) {
if($res.errcode === 0){
if ($res.errcode === 0) {
$("#saveTemplateModal").modal("hide");
layer.msg(window.editormdLocales[window.lang].saveSucc);
}else{
} else {
return showError($res.message, "#saveTemplateForm .show-error-message");
}
},
complete : function () {
complete: function () {
$("#btnSaveTemplate").button("reset");
}
});
/**
* 当添加模板弹窗事件发生
*/
$("#saveTemplateModal").on("show.bs.modal",function () {
window.sessionStorage.setItem("saveTemplateModal",$(this).find(".modal-body").html());
$("#saveTemplateModal").on("show.bs.modal", function () {
window.sessionStorage.setItem("saveTemplateModal", $(this).find(".modal-body").html());
var content = window.editor.getMarkdown();
$("#saveTemplateForm").find("input[name='content']").val(content);
$("#saveTemplateForm .show-error-message").html("");
}).on("hidden.bs.modal",function () {
}).on("hidden.bs.modal", function () {
$(this).find(".modal-body").html(window.sessionStorage.getItem("saveTemplateModal"));
});
/**
* 插入自定义模板内容
*/
$("#displayCustomsTemplateList").on("click",".btn-insert",function () {
$("#displayCustomsTemplateList").on("click", ".btn-insert", function () {
var templateId = $(this).attr("data-id");
$.ajax({
url: window.template.getUrl,
data :{"identify": window.book.identify, "template_id": templateId},
data: { "identify": window.book.identify, "template_id": templateId },
dataType: "json",
type: "get",
success : function ($res) {
if ($res.errcode !== 0){
layer.msg($res.message);
return;
}
success: function ($res) {
if ($res.errcode !== 0) {
layer.msg($res.message);
return;
}
window.isLoad = true;
window.editor.clear();
window.editor.insertValue($res.data.template_content);
window.editor.setCursor({ line: 0, ch: 0 });
resetEditorChanged(true);
$("#displayCustomsTemplateModal").modal("hide");
},error : function () {
}, error: function () {
layer.msg(window.editormdLocales[window.lang].serverExcept);
}
});
}).on("click",".btn-delete",function () {
}).on("click", ".btn-delete", function () {
var $then = $(this);
var templateId = $then.attr("data-id");
$then.button("loading");
$.ajax({
url : window.template.deleteUrl,
data: {"identify": window.book.identify, "template_id": templateId},
url: window.template.deleteUrl,
data: { "identify": window.book.identify, "template_id": templateId },
dataType: "json",
type: "post",
success: function ($res) {
if($res.errcode !== 0){
if ($res.errcode !== 0) {
layer.msg($res.message);
}else{
} else {
$then.parents("tr").empty().remove();
}
},error : function () {
}, error: function () {
layer.msg(window.editormdLocales[window.lang].serverExcept);
},
complete: function () {
@@ -604,31 +604,31 @@ $(function () {
})
});
$("#btnInsertTable").on("click",function () {
var content = $("#jsonContent").val();
if(content !== "") {
try {
var jsonObj = $.parseJSON(content);
var data = foreachJson(jsonObj,"");
var table = "| " + window.editormdLocales[window.lang].paramName
+ " | " + window.editormdLocales[window.lang].paramType
+ " | " + window.editormdLocales[window.lang].example
+ " | " + window.editormdLocales[window.lang].remark
+ " |\n| ------------ | ------------ | ------------ | ------------ |\n";
$.each(data,function (i,item) {
table += "|" + item.key + "|" + item.type + "|" + item.value +"| |\n";
});
$("#btnInsertTable").on("click", function () {
var content = $("#jsonContent").val();
if (content !== "") {
try {
var jsonObj = $.parseJSON(content);
var data = foreachJson(jsonObj, "");
var table = "| " + window.editormdLocales[window.lang].paramName
+ " | " + window.editormdLocales[window.lang].paramType
+ " | " + window.editormdLocales[window.lang].example
+ " | " + window.editormdLocales[window.lang].remark
+ " |\n| ------------ | ------------ | ------------ | ------------ |\n";
$.each(data, function (i, item) {
table += "|" + item.key + "|" + item.type + "|" + item.value + "| |\n";
});
insertToMarkdown(table);
}catch (e) {
showError("Json 格式错误:" + e.toString(),"#json-error-message");
return;
}
}
$("#convertJsonToTableModal").modal("hide");
} catch (e) {
showError("Json 格式错误:" + e.toString(), "#json-error-message");
return;
}
}
$("#convertJsonToTableModal").modal("hide");
});
$("#convertJsonToTableModal").on("hidden.bs.modal",function () {
$("#convertJsonToTableModal").on("hidden.bs.modal", function () {
$("#jsonContent").val("");
}).on("shown.bs.modal",function () {
}).on("shown.bs.modal", function () {
$("#jsonContent").focus();
});
});