/* * MarkDoc * PHP MarkDown document manager * Hordé Nicolas * https://github.com/dahut87/MarkDoc * Release under GPLv3.0 license * Base on Pheditor "PHP file editor" By Hamid Samak Release under MIT license */ var external = RegExp('^((f|ht)tps?:)?//'); var javascript = RegExp('^javascript:'); var textual = RegExp('\.md$'); var internal = RegExp('^\/:'); var editor; var emoji; var editfile=""; var viewfile=""; function drawWarning(editor) { var cm = editor.codemirror; var output = ''; var selectedText = cm.getSelection(); var text = selectedText || 'placeholder'; output = '\n> :warning:' + text + ' !'; cm.replaceSelection(output); } function drawDanger(editor) { var cm = editor.codemirror; var output = ''; var selectedText = cm.getSelection(); var text = selectedText || 'placeholder'; output = '\n> :skull:' + text + ' !'; cm.replaceSelection(output); } function drawTip(editor) { var cm = editor.codemirror; var output = ''; var selectedText = cm.getSelection(); var text = selectedText || 'placeholder'; output = '\n> :bulb:' + text + ' .'; cm.replaceSelection(output); } function drawNote(editor) { var cm = editor.codemirror; var output = ''; var selectedText = cm.getSelection(); var text = selectedText || 'placeholder'; output = '\n> :memo:' + text + ' .'; cm.replaceSelection(output); } $(function(){ $("#files").jstree({ themes: { responsive: true }, plugins: ["contextmenu", "dnd" ], core: { strings:{ loading: LANG['LOADING'] }, check_callback: true, data : { type: "POST", dataType: "json", url : "/index.php", data : function (node) { return { 'action' : 'allchildren' }; } } } }); emoji = new EmojiConvertor(); editor = new SimpleMDE({ element: $("#editor>div")[0], spellChecker: false, previewRender: function(plainText, preview) { setTimeout(function() { preview.innerHTML = emoji.replace_colons(this.parent.markdown(plainText)); Prism.highlightAll(); }.bind(this), 1) return LANG['LOADING'] }, toolbar: ["bold","italic","strikethrough","|","heading-1","heading-2","heading-3","|","quote","unordered-list","ordered-list","horizontal-rule","|", { name: "Warning", action: drawWarning, className: "fa fa-exclamation-triangle", // Look for a suitable icon title: "Warning field", }, { name: "Tip", action: drawTip, className: "fa fa-lightbulb", // Look for a suitable icon title: "Tip field", }, { name: "Danger", action: drawDanger, className: "fas fa-skull-crossbones", // Look for a suitable icon title: "Danger Field", }, { name: "Notes", action: drawNote, className: "fas fa-sticky-note", // Look for a suitable icon title: "Note Field", }, "|","code","link","image","table","|","preview","side-by-side","fullscreen","guide"] }); $("#editor").hide(); $("#image").hide(); $("#save").hide(); $("#content").html(emoji.replace_colons($("#content").html())); setTimeout(function(){ $("#forkongithub").fadeOut(1500); }, 5000); $('#content > p> img').magnifik({ratio:1.0}); tocgenerate(); $("#files").on("ready.jstree", function (e) { const urlParams = new URLSearchParams(window.location.search); const file = urlParams.get('doc'); searchtree(file); }); $("#voir").click(function(e){ e.preventDefault(); node=$("#files").jstree("get_selected"); file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, ''); if ($("#files").jstree("is_leaf",node)) { action=$(this).val(); if (action=="Voir") { if (viewfile===file) viewmode(); else openlink(file,false) } else { if (editfile===file) editmode(); else editlink(file) } } }); $("#nouveau").click(function(e){ e.preventDefault(); viewfile=""; node=$("#files").jstree("get_selected"); file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, ''); if ($("#files").jstree("is_leaf",node)) { file=file.substring(0, file.lastIndexOf("/")); } file=file+"/"+$("#search").val()+".md"; $.ajax({ type: "POST", url: "/index.php", data: { action: "new", file: encodeURIComponent(file) }, success: function(data){ data = data.split("|"); alertBox(data[1], data[0]); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alertBox(LANG['AJAXERROR'],'danger'); } }); }) $("#del").click(function(e){ e.preventDefault(); viewfile=""; node=$("#files").jstree("get_selected"); file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, ''); if ($("#files").jstree("is_leaf",node)) { if (confirm(LANG['CONFIRM_DEL']) == true) { $.ajax({ type: "POST", url: "/index.php", data: { action: "delete", file: encodeURIComponent(file) }, success: function(data){ data = data.split("|"); alertBox(data[1], data[0]); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alertBox(LANG['AJAXERROR'],'danger'); } }); } } }) $("#ren").click(function(e){ e.preventDefault(); viewfile=""; node=$("#files").jstree("get_selected"); file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, ''); file2=file.substring(0, file.lastIndexOf("/"))+"/"+$("#search").val()+".md"; alert(file); alert(file2); if ($("#files").jstree("is_leaf",node)) { if (confirm(LANG['CONFIRM_REN']) == true) { $.ajax({ type: "POST", url: "/index.php", data: { action: "rename", file: encodeURIComponent(file), file2: encodeURIComponent(file2) }, success: function(data){ data = data.split("|"); alertBox(data[1], data[0]); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alertBox(LANG['AJAXERROR'],'danger'); } }); } } }) $("#save").click(function(e){ e.preventDefault(); viewfile=""; node=$("#files").jstree("get_selected"); file="/"+$("#files").jstree("get_path",node,"/").replace(/^.+?[/]/, ''); if ($("#files").jstree("is_leaf",node)) { data = editor.value(); $.ajax({ type: "POST", url: "/index.php", data: { action: "save", file: encodeURIComponent(file), data:data }, success: function(data){ data = data.split("|"); alertBox(data[1], data[0]); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alertBox(LANG['AJAXERROR'],'danger'); } }); } else alertBox(LANG['SELECTERROR'], 'danger'); }); $("#files").on("select_node.jstree", function (e, nodes) { file="/"+$("#files").jstree("get_path",nodes.node,"/").replace(/^.+?[/]/, ''); if ($("#files").jstree("is_leaf",nodes.node)) openlink(file,false); else { sendmode(); } }); $("input[name=submit]").click(function(e) { e.preventDefault(); search($("#search").val()); }); $("input[name=toc]").hover(function(e) { e.preventDefault(); tocshow(); }); majlink('head'); }); function tocgenerate() { tocshow(); $('.toc').toc({ 'selectors': 'h2,h3,h4', 'container': '#content' }) $('.toc').append(''); $('.toc > i').hover(function(e) { e.preventDefault(); tochide(); }); } function tochide() { $('.toc').slideUp() $('#toc').show(); } function tocshow() { $('.toc').slideDown() $('#toc').hide(); } function alertBox(message, className) { $(".alert").removeClass("alert-success alert-warning alert-danger"); $(".alert").html(message).addClass("alert-" + className).fadeIn(); setTimeout(function(){ $(".alert").fadeOut(); }, 5000); } function openlink(dest,majtree) { $("#content").html('