2016-12-14 00:26:26 -05:00
|
|
|
/* global ace */
|
2017-03-07 19:04:20 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-01-29 12:53:48 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import createFlash from '~/flash';
|
|
|
|
import { __ } from '~/locale';
|
2017-04-03 13:54:40 -04:00
|
|
|
import TemplateSelectorMediator from '../blob/file_template_mediator';
|
2018-08-03 09:24:26 -04:00
|
|
|
import getModeByFileExtension from '~/lib/utils/ace_utils';
|
2017-03-07 19:04:20 -05:00
|
|
|
|
|
|
|
export default class EditBlob {
|
2018-10-02 19:00:38 -04:00
|
|
|
constructor(assetsPath, aceMode, currentAction, projectId) {
|
2017-03-07 19:04:20 -05:00
|
|
|
this.configureAceEditor(aceMode, assetsPath);
|
|
|
|
this.initModePanesAndLinks();
|
|
|
|
this.initSoftWrap();
|
2018-10-02 19:00:38 -04:00
|
|
|
this.initFileSelectors(currentAction, projectId);
|
2017-03-07 19:04:20 -05:00
|
|
|
}
|
|
|
|
|
2018-08-03 09:24:26 -04:00
|
|
|
configureAceEditor(filePath, assetsPath) {
|
2017-03-07 19:04:20 -05:00
|
|
|
ace.config.set('modePath', `${assetsPath}/ace`);
|
|
|
|
ace.config.loadModule('ace/ext/searchbox');
|
2018-08-03 09:24:26 -04:00
|
|
|
ace.config.loadModule('ace/ext/modelist');
|
2017-03-07 19:04:20 -05:00
|
|
|
|
|
|
|
this.editor = ace.edit('editor');
|
2017-04-03 13:54:40 -04:00
|
|
|
|
|
|
|
// This prevents warnings re: automatic scrolling being logged
|
|
|
|
this.editor.$blockScrolling = Infinity;
|
|
|
|
|
2017-03-07 19:04:20 -05:00
|
|
|
this.editor.focus();
|
|
|
|
|
2018-08-03 09:24:26 -04:00
|
|
|
if (filePath) {
|
|
|
|
this.editor.getSession().setMode(getModeByFileExtension(filePath));
|
2017-03-07 19:04:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 19:00:38 -04:00
|
|
|
initFileSelectors(currentAction, projectId) {
|
2017-04-03 13:54:40 -04:00
|
|
|
this.fileTemplateMediator = new TemplateSelectorMediator({
|
|
|
|
currentAction,
|
|
|
|
editor: this.editor,
|
2018-10-02 19:00:38 -04:00
|
|
|
projectId,
|
2017-03-07 19:04:20 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-02 19:00:38 -04:00
|
|
|
axios
|
|
|
|
.post(currentLink.data('previewUrl'), {
|
|
|
|
content: this.editor.getValue(),
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
currentPane.empty().append(data);
|
|
|
|
currentPane.renderGFM();
|
|
|
|
})
|
|
|
|
.catch(() => createFlash(__('An error occurred previewing the blob')));
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2017-03-07 19:04:20 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|