Better MDEditor API with dynamic initialization to fully support lists. (look at window.MDEditors) (#239)

This commit is contained in:
Djamil Legato
2015-11-24 17:39:20 -08:00
parent 4c66c4025a
commit 300c6842de
2 changed files with 49 additions and 22 deletions

View File

@@ -490,6 +490,14 @@ $(function () {
holder.append(newItem); holder.append(newItem);
button.data('key-index', ++key); button.data('key-index', ++key);
// process markdown editors
var field = newItem.find('[name]').filter('textarea'),
name = field.attr('name');
if (field.length && field.data('grav-mdeditor') && typeof MDEditors !== 'undefined') {
MDEditors.add(field);
}
}); });
}); });

View File

@@ -1,6 +1,4 @@
((function(){ ((function(){
var editors = [];
var toolbarIdentifiers = [ 'bold', 'italic', 'strike', 'link', 'image', 'blockquote', 'listUl', 'listOl' ]; var toolbarIdentifiers = [ 'bold', 'italic', 'strike', 'link', 'image', 'blockquote', 'listUl', 'listOl' ];
if (typeof window.customToolbarElements !== 'undefined') { if (typeof window.customToolbarElements !== 'undefined') {
window.customToolbarElements.forEach(function(customToolbarElement) { window.customToolbarElements.forEach(function(customToolbarElement) {
@@ -89,7 +87,7 @@
].join(''); ].join('');
var MDEditor = function(editor, options){ var MDEditor = function(editor, options){
var tpl = template, $this = this, var tpl = '' + template, $this = this,
task = 'task' + GravAdmin.config.param_sep; task = 'task' + GravAdmin.config.param_sep;
this.defaults = { this.defaults = {
@@ -101,7 +99,7 @@
lblPreview : '<i class="fa fa-fw fa-eye"></i>', lblPreview : '<i class="fa fa-fw fa-eye"></i>',
lblCodeview : '<i class="fa fa-fw fa-code"></i>', lblCodeview : '<i class="fa fa-fw fa-code"></i>',
lblMarkedview: '<i class="fa fa-fw fa-code"></i>' lblMarkedview: '<i class="fa fa-fw fa-code"></i>'
} };
this.element = $(editor); this.element = $(editor);
this.options = $.extend({}, this.defaults, options); this.options = $.extend({}, this.defaults, options);
@@ -173,15 +171,15 @@
$this.editor.refresh(); $this.editor.refresh();
if ($this.activetab == 'preview') { if ($this.activetab == 'preview') {
$('.grav-mdeditor-toolbar').fadeOut(); $this.mdeditor.find('.grav-mdeditor-toolbar').fadeOut();
setTimeout(function() { setTimeout(function() {
$('.grav-mdeditor-preview-text').fadeIn(); $this.mdeditor.find('.grav-mdeditor-preview-text').fadeIn();
}, 500); }, 250);
} else { } else {
$('.grav-mdeditor-preview-text').fadeOut(); $this.mdeditor.find('.grav-mdeditor-preview-text').fadeOut();
setTimeout(function() { setTimeout(function() {
$('.grav-mdeditor-toolbar').fadeIn(); $this.mdeditor.find('.grav-mdeditor-toolbar').fadeIn();
}, 500); }, 250);
} }
} }
}); });
@@ -218,7 +216,7 @@
if($this.mdeditor.is(":visible")) $this.fit(); if($this.mdeditor.is(":visible")) $this.fit();
});*/ });*/
editors.push(this); MDEditors.editors[this.element.attr('name')] = this;
// Methods // Methods
@@ -461,7 +459,7 @@
$.extend(editor, { $.extend(editor, {
enableMarkdown: function() { enableMarkdown: function() {
enableMarkdown() enableMarkdown();
this.render(); this.render();
}, },
disableMarkdown: function() { disableMarkdown: function() {
@@ -520,23 +518,44 @@
} }
}); });
} }
} };
// toolbar actions // toolbar actions
this._initToolbar($this); this._initToolbar($this);
this._buildtoolbar(); this._buildtoolbar();
return this;
};
var MDEditors = {
editors: {},
init: function() {
var element;
$('textarea[data-grav-mdeditor]').each(function() {
element = $(this);
if (!element.parents('[data-collection-template="new"]').length) {
MDEditors.add(element);
} }
});
},
add: function(editor) {
editor = $(editor);
var mdeditor;
if (!editor.data('mededitor')) {
mdeditor = new MDEditor(editor, JSON.parse(editor.attr('data-grav-mdeditor') || '{}'));
}
return mdeditor || MDEditors.editors[editor.attr('name')];
}
};
// init // init
$(function(){ $(function(){
var editor; MDEditors.init();
$('textarea[data-grav-mdeditor]').each(function() {
editor = $(this);
if (!editor.data('mdeditor')) {
new MDEditor(editor, JSON.parse(editor.attr('data-grav-mdeditor') || '{}'));
}
}); });
})
window.MDEditors = MDEditors;
})()); })());