Add a file upload button to the markdown toolbar (optional)

pull/1/head
yflory 4 years ago
parent a0cd1d7195
commit 1b56f26809

@ -936,7 +936,8 @@ define([
return button;
};
var createMdToolbar = function (common, editor) {
var createMdToolbar = function (common, editor, cfg) {
cfg = cfg || {};
var $toolbar = $('<div>', {
'class': 'cp-markdown-toolbar'
});
@ -1025,9 +1026,39 @@ define([
icon: 'fa-newspaper-o'
}
};
if (typeof(cfg.embed) === "function") {
actions.embed = {
icon: 'fa-picture-o',
action: function () {
var _cfg = {
types: ['file'],
where: ['root']
};
common.openFilePicker(_cfg, function (data) {
if (data.type !== 'file') {
console.log("Unexpected data type picked " + data.type);
return;
}
if (data.type !== 'file') { console.log('unhandled embed type ' + data.type); return; }
common.setPadAttribute('atime', +new Date(), null, data.href);
var privateDat = common.getMetadataMgr().getPrivateData();
var origin = privateDat.fileHost || privateDat.origin;
var src = data.src = data.src.slice(0,1) === '/' ? origin + data.src : data.src;
cfg.embed($('<media-tag src="' + src +
'" data-crypto-key="cryptpad:' + data.key + '"></media-tag>'), data);
});
}
};
}
var onClick = function () {
var type = $(this).attr('data-type');
var texts = editor.getSelections();
if (actions[type].action) {
return actions[type].action();
}
var newTexts = texts.map(function (str) {
str = str || Messages.mdToolbar_defaultText;
if (actions[type].apply) {
@ -1054,7 +1085,7 @@ define([
}).appendTo($toolbar);
return $toolbar;
};
UIElements.createMarkdownToolbar = function (common, editor) {
UIElements.createMarkdownToolbar = function (common, editor, opts) {
var readOnly = common.getMetadataMgr().getPrivateData().readOnly;
if (readOnly) {
return {
@ -1064,7 +1095,7 @@ define([
};
}
var $toolbar = createMdToolbar(common, editor);
var $toolbar = createMdToolbar(common, editor, opts);
var cfg = {
title: Messages.mdToolbar_button,
element: $toolbar

@ -646,11 +646,6 @@ define([
}).filter(Boolean);
};
var checkMt = function (framework) {
var cm = $('.CodeMirror').length;
if (!cm) { framework.setMediaTagEmbedder(); }
};
var STATIC_TYPES = {
md: {
defaultOpts: {
@ -666,7 +661,7 @@ define([
var cursorGetter;
return {
tag: tag,
edit: function (cb, tmp, framework) {
edit: function (cb, tmp) {
var t = h('textarea');
var block = h('div.cp-form-edit-options-block', [t]);
var cm = SFCodeMirror.create("gfm", CMeditor, t);
@ -676,14 +671,6 @@ define([
editor.setOption('styleActiveLine', true);
editor.setOption('readOnly', false);
editor.on('focus', function () {
framework.setMediaTagEmbedder();
framework.setMediaTagEmbedder(function (mt) {
editor.focus();
editor.replaceSelection($(mt)[0].outerHTML);
});
})
var text = opts.text;
var cursor;
if (tmp && tmp.content && tmp.old.text === text) {
@ -705,7 +692,12 @@ define([
editor.focus();
});
if (APP.common) {
var markdownTb = APP.common.createMarkdownToolbar(editor);
var markdownTb = APP.common.createMarkdownToolbar(editor, {
embed: function (mt) {
editor.focus();
editor.replaceSelection($(mt)[0].outerHTML);
}
});
$(block).prepend(markdownTb.toolbar);
$(markdownTb.toolbar).show();
cm.configureTheme(APP.common, function () {});
@ -714,7 +706,6 @@ define([
var cancelBlock = h('button.btn.btn-secondary', Messages.cancel);
$(cancelBlock).click(function () {
cb();
checkMt(framework);
});
// Save changes
var saveBlock = h('button.btn.btn-primary', [
@ -730,7 +721,6 @@ define([
$(saveBlock).click(function () {
$(saveBlock).attr('disabled', 'disabled');
cb(getContent());
checkMt(framework);
});
cursorGetter = function () {
@ -1698,7 +1688,6 @@ define([
$('.cp-form-block[data-id="'+uid+'"]').remove();
framework.localChange();
updateAddInline();
checkMt(framework);
});
// Values

@ -241,7 +241,12 @@ define([
e.stopPropagation();
});
var common = framework._.sfCommon;
var markdownTb = common.createMarkdownToolbar(editor);
var markdownTb = common.createMarkdownToolbar(editor, {
embed: function (mt) {
editor.focus();
editor.replaceSelection($(mt)[0].outerHTML);
}
});
$(text).before(markdownTb.toolbar);
$(markdownTb.toolbar).show();
editor.refresh();

Loading…
Cancel
Save