2016-09-11 16:03:06 +00:00
|
|
|
|
|
|
|
let modelist = ace.require("ace/ext/modelist");
|
2016-12-22 01:38:12 +00:00
|
|
|
let codeEditor = null;
|
2016-09-11 16:03:06 +00:00
|
|
|
|
2016-10-17 23:52:04 +00:00
|
|
|
// ACE - Mode Loader
|
|
|
|
|
|
|
|
let modelistLoaded = [];
|
|
|
|
let loadAceMode = (m) => {
|
|
|
|
return $.ajax({
|
|
|
|
url: '/js/ace/mode-' + m + '.js',
|
|
|
|
dataType: "script",
|
|
|
|
cache: true,
|
|
|
|
beforeSend: () => {
|
|
|
|
if(_.includes(modelistLoaded, m)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
success: () => {
|
|
|
|
modelistLoaded.push(m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Vue Code Block instance
|
|
|
|
|
2016-09-11 16:03:06 +00:00
|
|
|
let vueCodeBlock = new Vue({
|
|
|
|
el: '#modal-editor-codeblock',
|
|
|
|
data: {
|
|
|
|
modes: modelist.modesByName,
|
2016-12-22 01:38:12 +00:00
|
|
|
modeSelected: 'text',
|
|
|
|
initContent: ''
|
2016-09-11 16:03:06 +00:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
modeSelected: (val, oldVal) => {
|
|
|
|
loadAceMode(val).done(() => {
|
|
|
|
ace.require("ace/mode/" + val);
|
|
|
|
codeEditor.getSession().setMode("ace/mode/" + val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2016-12-04 01:20:06 +00:00
|
|
|
open: (ev) => {
|
2016-12-22 01:38:12 +00:00
|
|
|
|
2016-12-04 01:20:06 +00:00
|
|
|
$('#modal-editor-codeblock').addClass('is-active');
|
|
|
|
|
|
|
|
_.delay(() => {
|
2016-12-22 01:38:12 +00:00
|
|
|
codeEditor = ace.edit("codeblock-editor");
|
|
|
|
codeEditor.setTheme("ace/theme/tomorrow_night");
|
|
|
|
codeEditor.getSession().setMode("ace/mode/" + vueCodeBlock.modeSelected);
|
|
|
|
codeEditor.setOption('fontSize', '14px');
|
|
|
|
codeEditor.setOption('hScrollBarAlwaysVisible', false);
|
|
|
|
codeEditor.setOption('wrap', true);
|
|
|
|
|
|
|
|
codeEditor.setValue(vueCodeBlock.initContent);
|
|
|
|
|
2016-12-04 01:20:06 +00:00
|
|
|
codeEditor.focus();
|
|
|
|
codeEditor.renderer.updateFull();
|
2016-12-22 01:38:12 +00:00
|
|
|
}, 300);
|
2016-12-04 01:20:06 +00:00
|
|
|
|
|
|
|
},
|
2016-09-11 16:03:06 +00:00
|
|
|
cancel: (ev) => {
|
|
|
|
mdeModalOpenState = false;
|
2016-12-04 01:20:06 +00:00
|
|
|
$('#modal-editor-codeblock').removeClass('is-active');
|
2016-12-22 01:38:12 +00:00
|
|
|
vueCodeBlock.initContent = '';
|
2016-09-11 16:03:06 +00:00
|
|
|
},
|
|
|
|
insertCode: (ev) => {
|
|
|
|
|
|
|
|
if(mde.codemirror.doc.somethingSelected()) {
|
|
|
|
mde.codemirror.execCommand('singleSelection');
|
|
|
|
}
|
2016-09-12 04:00:52 +00:00
|
|
|
let codeBlockText = '\n```' + vueCodeBlock.modeSelected + '\n' + codeEditor.getValue() + '\n```\n';
|
2016-09-11 16:03:06 +00:00
|
|
|
|
|
|
|
mde.codemirror.doc.replaceSelection(codeBlockText);
|
|
|
|
vueCodeBlock.cancel();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 23:52:04 +00:00
|
|
|
});
|