2017-08-17 22:00:37 +05:30
|
|
|
/* global ace */
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-03-17 18:26:18 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import createFlash from '~/flash';
|
|
|
|
import { __ } from '~/locale';
|
2017-08-17 22:00:37 +05:30
|
|
|
import TemplateSelectorMediator from '../blob/file_template_mediator';
|
|
|
|
|
|
|
|
export default class EditBlob {
|
|
|
|
constructor(assetsPath, aceMode, currentAction) {
|
|
|
|
this.configureAceEditor(aceMode, assetsPath);
|
|
|
|
this.initModePanesAndLinks();
|
|
|
|
this.initSoftWrap();
|
|
|
|
this.initFileSelectors(currentAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
configureAceEditor(aceMode, assetsPath) {
|
|
|
|
ace.config.set('modePath', `${assetsPath}/ace`);
|
|
|
|
ace.config.loadModule('ace/ext/searchbox');
|
|
|
|
|
|
|
|
this.editor = ace.edit('editor');
|
|
|
|
|
|
|
|
// This prevents warnings re: automatic scrolling being logged
|
|
|
|
this.editor.$blockScrolling = Infinity;
|
|
|
|
|
|
|
|
this.editor.focus();
|
|
|
|
|
|
|
|
if (aceMode) {
|
|
|
|
this.editor.getSession().setMode(`ace/mode/${aceMode}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initFileSelectors(currentAction) {
|
|
|
|
this.fileTemplateMediator = new TemplateSelectorMediator({
|
|
|
|
currentAction,
|
|
|
|
editor: this.editor,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initModePanesAndLinks() {
|
|
|
|
this.$editModePanes = $('.js-edit-mode-pane');
|
|
|
|
this.$editModeLinks = $('.js-edit-mode a');
|
|
|
|
this.$editModeLinks.on('click', e => this.editModeLinkClickHandler(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
editModeLinkClickHandler(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const currentLink = $(e.target);
|
|
|
|
const paneId = currentLink.attr('href');
|
|
|
|
const currentPane = this.$editModePanes.filter(paneId);
|
|
|
|
|
|
|
|
this.$editModeLinks.parent().removeClass('active hover');
|
|
|
|
|
|
|
|
currentLink.parent().addClass('active hover');
|
|
|
|
|
|
|
|
this.$editModePanes.hide();
|
|
|
|
|
|
|
|
currentPane.fadeIn(200);
|
|
|
|
|
|
|
|
if (paneId === '#preview') {
|
|
|
|
this.$toggleButton.hide();
|
2018-03-27 19:54:05 +05:30
|
|
|
axios.post(currentLink.data('previewUrl'), {
|
2017-08-17 22:00:37 +05:30
|
|
|
content: this.editor.getValue(),
|
2018-03-17 18:26:18 +05:30
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
currentPane.empty().append(data);
|
|
|
|
currentPane.renderGFM();
|
|
|
|
})
|
|
|
|
.catch(() => createFlash(__('An error occurred previewing the blob')));
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
this.$toggleButton.show();
|
|
|
|
|
|
|
|
return this.editor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
initSoftWrap() {
|
|
|
|
this.isSoftWrapped = false;
|
|
|
|
this.$toggleButton = $('.soft-wrap-toggle');
|
|
|
|
this.$toggleButton.on('click', () => this.toggleSoftWrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleSoftWrap() {
|
|
|
|
this.isSoftWrapped = !this.isSoftWrapped;
|
|
|
|
this.$toggleButton.toggleClass('soft-wrap-active', this.isSoftWrapped);
|
|
|
|
this.editor.getSession().setUseWrapMode(this.isSoftWrapped);
|
|
|
|
}
|
|
|
|
}
|