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; return button;
}; };
var createMdToolbar = function (common, editor) { var createMdToolbar = function (common, editor, cfg) {
cfg = cfg || {};
var $toolbar = $('<div>', { var $toolbar = $('<div>', {
'class': 'cp-markdown-toolbar' 'class': 'cp-markdown-toolbar'
}); });
@ -1025,9 +1026,39 @@ define([
icon: 'fa-newspaper-o' 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 onClick = function () {
var type = $(this).attr('data-type'); var type = $(this).attr('data-type');
var texts = editor.getSelections(); var texts = editor.getSelections();
if (actions[type].action) {
return actions[type].action();
}
var newTexts = texts.map(function (str) { var newTexts = texts.map(function (str) {
str = str || Messages.mdToolbar_defaultText; str = str || Messages.mdToolbar_defaultText;
if (actions[type].apply) { if (actions[type].apply) {
@ -1054,7 +1085,7 @@ define([
}).appendTo($toolbar); }).appendTo($toolbar);
return $toolbar; return $toolbar;
}; };
UIElements.createMarkdownToolbar = function (common, editor) { UIElements.createMarkdownToolbar = function (common, editor, opts) {
var readOnly = common.getMetadataMgr().getPrivateData().readOnly; var readOnly = common.getMetadataMgr().getPrivateData().readOnly;
if (readOnly) { if (readOnly) {
return { return {
@ -1064,7 +1095,7 @@ define([
}; };
} }
var $toolbar = createMdToolbar(common, editor); var $toolbar = createMdToolbar(common, editor, opts);
var cfg = { var cfg = {
title: Messages.mdToolbar_button, title: Messages.mdToolbar_button,
element: $toolbar element: $toolbar

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

@ -241,7 +241,12 @@ define([
e.stopPropagation(); e.stopPropagation();
}); });
var common = framework._.sfCommon; 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); $(text).before(markdownTb.toolbar);
$(markdownTb.toolbar).show(); $(markdownTb.toolbar).show();
editor.refresh(); editor.refresh();

Loading…
Cancel
Save