humhub/static/js/markdownEditor.js

131 lines
5.8 KiB
JavaScript
Raw Normal View History

2015-03-04 21:47:09 +01:00
// Newly uploaded file
var newFile = "";
function initMarkdownEditor(elementId) {
2016-10-30 17:23:54 +01:00
2017-07-20 17:59:40 +02:00
if(!$('#addFileModal_'+elementId).length) {
$("body").append($("#markdownEditor_dialogs_"+elementId).html());
}
2015-03-04 21:47:09 +01:00
$("#" + elementId).markdown({
iconlibrary: 'fa',
resize: 'vertical',
2015-03-04 21:47:09 +01:00
additionalButtons: [
[{
name: "groupCustom",
data: [{
name: "cmdLinkWiki",
title: "URL/Link",
2015-03-04 21:47:09 +01:00
icon: {glyph: 'glyphicon glyphicon-link', fa: 'fa fa-link', 'fa-3': 'icon-link'},
callback: function(e) {
addLinkModal = $('#addLinkModal_'+elementId);
linkTitleField = addLinkModal.find('.linkTitle');
linkTargetField = addLinkModal.find('.linkTarget');
2016-10-30 17:23:54 +01:00
addLinkModal.find(".close").off('click');
2016-10-30 17:23:54 +01:00
addLinkModal.modal('show');
2015-03-04 21:47:09 +01:00
linkTitleField.val(e.getSelection().text);
if (linkTitleField.val() == "") {
linkTitleField.focus();
2015-03-04 21:47:09 +01:00
} else {
linkTargetField.focus();
2015-03-04 21:47:09 +01:00
}
addLinkModal.find('.addLinkButton').off('click');
addLinkModal.find('.addLinkButton').on('click', function() {
chunk = "[" + linkTitleField.val() + "](" + linkTargetField.val() + ")";
2015-03-04 21:47:09 +01:00
selected = e.getSelection(), content = e.getContent(),
e.replaceSelection(chunk);
cursor = selected.start;
e.setSelection(cursor, cursor + chunk.length);
addLinkModal.modal('hide')
2015-03-04 21:47:09 +01:00
});
addLinkModal.on('hide.bs.modal', function(ee) {
linkTitleField.val("");
linkTargetField.val("");
2015-03-04 21:47:09 +01:00
})
}
},
{
name: "cmdImgWiki",
title: "Image/File",
2015-03-04 21:47:09 +01:00
icon: {glyph: 'glyphicon glyphicon-picture', fa: 'fa fa-picture-o', 'fa-3': 'icon-picture'},
callback: function(e) {
newFile = "";
addFileModal = $('#addFileModal_'+elementId);
addFileModal.modal('show');
addFileModal.find(".uploadForm").show();
addFileModal.find(".uploadProgress").hide();
2016-10-30 17:23:54 +01:00
addFileModal.on('hide.bs.modal', function(ee) {
2015-03-04 21:47:09 +01:00
if (newFile != "") {
2016-10-30 17:23:54 +01:00
if (newFile.mimeType.substring(0,6) == "image/") {
2015-03-04 21:47:09 +01:00
chunk = "![" + newFile.name + "](file-guid-" + newFile.guid + ")";
} else {
chunk = "[" + newFile.name + "](file-guid-" + newFile.guid + ")";
}
selected = e.getSelection(), content = e.getContent(),
e.replaceSelection(chunk);
cursor = selected.start;
e.setSelection(cursor, cursor + chunk.length);
}
})
}
},
]
}]
],
reorderButtonGroups: ["groupFont", "groupCustom", "groupMisc", "groupUtil"],
onPreview: function(e) {
$.ajax({
type: "POST",
url: markdownPreviewUrl,
data: {
markdown: e.getContent(),
}
}).done(function(previewHtml) {
$('#markdownpreview_'+elementId).html(previewHtml);
2015-03-04 21:47:09 +01:00
});
var previewContent = "<div id='markdownpreview_"+elementId+"'><div class='loader'></div></div>";
2015-03-04 21:47:09 +01:00
return previewContent;
}
});
$('#addFileModal_'+elementId).find(".uploadProgress").hide();
$('#addFileModal_'+elementId).find('.fileUploadButton').fileupload({
2015-03-04 21:47:09 +01:00
dataType: 'json',
done: function(e, data) {
$.each(data.result.files, function(index, file) {
addFileModal = $('#addFileModal_'+elementId);
2015-03-04 21:47:09 +01:00
if (!file.error) {
newFile = file;
hiddenValueField = $('#fileUploaderHiddenGuidField_'+elementId);
2015-03-04 21:47:09 +01:00
hiddenValueField.val(hiddenValueField.val() + "," + file.guid);
addFileModal.modal('hide');
2015-03-04 21:47:09 +01:00
} else {
alert("file upload error");
}
});
},
progressall: function(e, data) {
newFile = "";
2016-10-30 17:23:54 +01:00
addFileModal = $('#addFileModal_'+elementId);
2015-03-04 21:47:09 +01:00
var progress = parseInt(data.loaded / data.total * 100, 10);
addFileModal.find(".uploadForm").hide();
addFileModal.find(".uploadProgress").show();
2015-03-04 21:47:09 +01:00
if (progress == 100) {
addFileModal.find(".uploadProgress").hide();
addFileModal.find(".uploadForm").hide();
2015-03-04 21:47:09 +01:00
}
}
}).prop('disabled', !$.support.fileInput).parent().addClass($.support.fileInput ? undefined : 'disabled');
}