diff --git a/modules/backend/formwidgets/markdowneditor/assets/js/markdowneditor.js b/modules/backend/formwidgets/markdowneditor/assets/js/markdowneditor.js index 279dfac3a..3ddb2fbac 100644 --- a/modules/backend/formwidgets/markdowneditor/assets/js/markdowneditor.js +++ b/modules/backend/formwidgets/markdowneditor/assets/js/markdowneditor.js @@ -31,6 +31,120 @@ this.$el.attr('id', 'element-' + Math.random().toString(36).substring(7)) } + + this.createCodeContainer() + this.createToolbar() + + this.$toolbar.on('click', '.btn, .md-dropdown-button', this.proxy(this.onClickButton)) + + $('[data-control="tooltip"]', this.$toolbar).tooltip() + $('[data-toggle="dropdown"]', this.$toolbar).dropdown() + } + + MarkdownEditor.prototype.dispose = function() { + this.$el.off('dispose-control', this.proxy(this.dispose)) + this.$toolbar.off('click', '.btn, .md-dropdown-button', this.proxy(this.onClickButton)) + + this.$el.removeData('oc.markdownEditor') + + this.$el = null + this.$textarea = null + this.$toolbar = null + this.$write = null + this.$preview = null + this.$code = null + this.editor = null + this.$form = null + + this.options = null + + BaseProto.dispose.call(this) + } + + MarkdownEditor.prototype.onClickButton = function(ev) { + var $button = $(ev.target), + action = $button.data('button-action'), + template = $button.data('button-template') + + this[action](template) + } + + MarkdownEditor.prototype.createToolbar = function() { + var self = this, + $button, + $buttons = $('
'), + $fixedButtons = $('') + + $.each($.oc.markdownEditorButtons, function(code, button) { + $button = $('').attr({ + 'type': "button", + 'class': 'btn', + 'title': $.oc.lang.get(button.label), + 'data-control': "tooltip", + 'data-placement': "bottom", + 'data-container': "body", + 'data-button-code': code, + 'data-button-action': button.action + }) + + if (button.template) { + $button.attr('data-button-template', button.template) + } + + if (button.cssClass) { + $button.addClass(button.cssClass) + } + else { + $button.addClass('tb-icon tb-' + button.icon) + } + + if (button.fixed) { + $fixedButtons.append($button) + } + else { + $buttons.append($button) + } + + if (button.dropdown) { + $button.attr('data-toggle', 'dropdown') + self.createToolbarDropdown(button, $button) + } + }) + + $buttons.wrapInner('') + this.$toolbar.append($buttons) + this.$toolbar.append($fixedButtons) + } + + MarkdownEditor.prototype.createToolbarDropdown = function(button, $el) { + var $dropdown = $(''), + $childButton + + $dropdown.attr('data-dropdown-title', $.oc.lang.get(button.label)) + $.each(button.dropdown, function(code, childButton) { + $childButton = $('').attr({ + 'href': 'javascript:;', + 'class': 'md-dropdown-button', + 'tabindex': '-1', + 'data-button-code': code, + 'data-button-action': childButton.action + }) + + if (childButton.cssClass) { + $childButton.addClass(childButton.cssClass) + } + + $childButton.text($.oc.lang.get(childButton.label)) + + $dropdown.append($childButton) + $childButton.wrap('') + }) + + $el.wrap('') + $el.after($dropdown) + } + + MarkdownEditor.prototype.createCodeContainer = function() { /* * Create code container */ @@ -63,19 +177,6 @@ editor.on('focus', this.proxy(this.onFocus)) } - MarkdownEditor.prototype.dispose = function() { - this.$el.off('dispose-control', this.proxy(this.dispose)) - this.$el.removeData('oc.markdownEditor') - - this.$el = null - - // In some cases options could contain callbacks, - // so it's better to clean them up too. - this.options = null - - BaseProto.dispose.call(this) - } - MarkdownEditor.prototype.onResize = function() { this.editor.resize() } @@ -88,6 +189,25 @@ this.$el.addClass('editor-focus') } + /* + * Button actions + */ + + MarkdownEditor.prototype.insertLine = function(template) { + var editor = this.editor, + pos = this.editor.getCursorPosition() + + if (pos.column == 0) { + editor.selection.clearSelection() + } + else { + editor.navigateTo(editor.getSelectionRange().start.row, Number.MAX_VALUE) + } + + editor.insert('\n'+template+'\n') + editor.focus() + } + MarkdownEditor.DEFAULTS = { buttons: ['formatting', 'bold', 'italic', 'unorderedlist', 'orderedlist', 'link', 'horizontalrule'], viewMode: 'tab' @@ -216,18 +336,20 @@ horizontalrule: { label: 'markdowneditor.horizontalrule', icon: 'horizontalrule', - action: 'line.insert', + action: 'insertLine', template: '---' }, fullscreen: { label: 'markdowneditor.fullscreen', icon: 'fullscreen', - action: 'fullscreen.toggle' + action: 'fullscreen.toggle', + fixed: true }, preview: { label: 'markdowneditor.preview', cssClass: 'oc-button oc-icon-eye', - action: 'preview.toggle' + action: 'preview.toggle', + fixed: true } } diff --git a/modules/backend/formwidgets/markdowneditor/partials/_markdowneditor.htm b/modules/backend/formwidgets/markdowneditor/partials/_markdowneditor.htm index 21465ddae..76fd23e85 100644 --- a/modules/backend/formwidgets/markdowneditor/partials/_markdowneditor.htm +++ b/modules/backend/formwidgets/markdowneditor/partials/_markdowneditor.htm @@ -5,58 +5,9 @@