2017-05-28 02:24:46 +00:00
|
|
|
<template lang="pug">
|
|
|
|
transition(:duration="400")
|
|
|
|
.modal(v-show='isShown', v-cloak)
|
|
|
|
transition(name='modal-background')
|
|
|
|
.modal-background(v-show='isShown')
|
|
|
|
.modal-container
|
|
|
|
transition(name='modal-content')
|
|
|
|
.modal-content.is-expanded(v-show='isShown')
|
|
|
|
header.is-green
|
2017-05-28 23:56:10 +00:00
|
|
|
span {{ $t('editor.codeblocktitle') }}
|
|
|
|
p.modal-notify(v-bind:class='{ "is-active": isLoading }')
|
|
|
|
span {{ $t('editor.codeblockloading', { name: modeSelected }) }}
|
|
|
|
i
|
2017-05-28 02:24:46 +00:00
|
|
|
section.is-gapless
|
|
|
|
.columns.is-stretched
|
|
|
|
.column.is-one-quarter.modal-sidebar.is-green(style={'max-width':'350px'})
|
2017-05-28 23:56:10 +00:00
|
|
|
.model-sidebar-header {{ $t('editor.codeblocklanguage') }}
|
2017-05-28 02:24:46 +00:00
|
|
|
.model-sidebar-content
|
|
|
|
p.control.is-fullwidth
|
|
|
|
select(v-model='modeSelected')
|
|
|
|
option(v-for='mode in modes', v-bind:value='mode.name') {{ mode.caption }}
|
|
|
|
.column.ace-container
|
|
|
|
#codeblock-editor
|
|
|
|
footer
|
2017-05-28 23:56:10 +00:00
|
|
|
a.button.is-grey.is-outlined(v-on:click='cancel') {{ $t('editor.discard') }}
|
|
|
|
a.button.is-green(v-on:click='insertCode') {{ $t('editor.codeblockinsert') }}
|
2017-05-28 02:24:46 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2017-06-18 22:35:33 +00:00
|
|
|
let codeEditor
|
|
|
|
let ace
|
2017-05-28 18:34:50 +00:00
|
|
|
|
2017-06-18 22:35:33 +00:00
|
|
|
export default {
|
|
|
|
name: 'editor-codeblock',
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
modes: [],
|
|
|
|
modeSelected: 'text',
|
|
|
|
modelistLoaded: [],
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
content() {
|
|
|
|
return this.$store.state.editorCodeblock.content
|
2017-05-28 18:34:50 +00:00
|
|
|
},
|
2017-06-18 22:35:33 +00:00
|
|
|
isShown() {
|
|
|
|
return this.$store.state.editorCodeblock.shown
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
modeSelected(val, oldVal) {
|
|
|
|
this.loadMode(val)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
init() {
|
|
|
|
let self = this
|
|
|
|
self._.delay(() => {
|
|
|
|
codeEditor = ace.edit('codeblock-editor')
|
|
|
|
codeEditor.setTheme('ace/theme/tomorrow_night')
|
|
|
|
codeEditor.getSession().setMode('ace/mode/' + self.modeSelected)
|
|
|
|
codeEditor.setOption('fontSize', '14px')
|
|
|
|
codeEditor.setOption('hScrollBarAlwaysVisible', false)
|
|
|
|
codeEditor.setOption('wrap', true)
|
|
|
|
codeEditor.setOption('useSoftTabs', true)
|
|
|
|
codeEditor.setOption('tabSize', 2)
|
|
|
|
codeEditor.setOption('showPrintMargin', false)
|
2017-05-28 18:34:50 +00:00
|
|
|
|
2017-06-18 22:35:33 +00:00
|
|
|
codeEditor.setValue(self.content)
|
2017-05-28 18:34:50 +00:00
|
|
|
|
2017-06-18 22:35:33 +00:00
|
|
|
codeEditor.focus()
|
|
|
|
codeEditor.renderer.updateFull()
|
|
|
|
}, 100)
|
|
|
|
},
|
|
|
|
loadMode(m) {
|
|
|
|
let self = this
|
|
|
|
if (self._.includes(self.modelistLoaded, m)) {
|
|
|
|
codeEditor.getSession().setMode('ace/mode/' + m)
|
|
|
|
} else {
|
|
|
|
self.isLoading = true
|
|
|
|
self.$http.get('/js/ace/mode-' + m + '.js').then(resp => {
|
|
|
|
if (resp.ok) {
|
|
|
|
eval(resp.bodyText)
|
|
|
|
self.modelistLoaded.push(m)
|
|
|
|
ace.acequire('ace/mode/' + m)
|
|
|
|
codeEditor.getSession().setMode('ace/mode/' + m)
|
|
|
|
self._.delay(() => { self.isLoading = false }, 500)
|
|
|
|
} else {
|
|
|
|
this.$store.dispatch('alert', {
|
2017-05-28 23:56:10 +00:00
|
|
|
style: 'red',
|
2017-07-08 21:33:09 +00:00
|
|
|
icon: 'ui-2_square-remove-09',
|
2017-06-18 22:35:33 +00:00
|
|
|
msg: self.$t('editor.codeblockloadingerror')
|
2017-05-28 23:56:10 +00:00
|
|
|
})
|
2017-06-18 22:35:33 +00:00
|
|
|
}
|
|
|
|
}).catch(err => {
|
2017-07-06 04:10:41 +00:00
|
|
|
this.$store.dispatch('alert', {
|
2017-06-18 22:35:33 +00:00
|
|
|
style: 'red',
|
2017-07-08 21:33:09 +00:00
|
|
|
icon: 'ui-2_square-remove-09',
|
2017-06-18 22:35:33 +00:00
|
|
|
msg: 'Error: ' + err.body.msg
|
2017-05-28 18:34:50 +00:00
|
|
|
})
|
2017-05-28 02:24:46 +00:00
|
|
|
})
|
|
|
|
}
|
2017-05-28 18:34:50 +00:00
|
|
|
},
|
2017-06-18 22:35:33 +00:00
|
|
|
cancel() {
|
|
|
|
codeEditor.destroy()
|
|
|
|
this.$store.dispatch('editorCodeblock/close')
|
|
|
|
},
|
|
|
|
insertCode() {
|
|
|
|
let codeBlockText = '\n```' + this.modeSelected + '\n' + codeEditor.getValue() + '\n```\n'
|
|
|
|
this.$store.dispatch('editor/insert', codeBlockText)
|
|
|
|
this.$store.dispatch('alert', {
|
|
|
|
style: 'blue',
|
2017-07-08 21:33:09 +00:00
|
|
|
icon: 'files_archive-3d-check',
|
2017-06-18 22:35:33 +00:00
|
|
|
msg: this.$t('editor.codeblocksuccess')
|
2017-05-28 18:34:50 +00:00
|
|
|
})
|
2017-06-18 22:35:33 +00:00
|
|
|
this.cancel()
|
2017-05-28 02:24:46 +00:00
|
|
|
}
|
2017-06-18 22:35:33 +00:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
FuseBox.import('/js/ace/ace.js', (acePkg) => {
|
|
|
|
ace = acePkg
|
|
|
|
this.modes = ace.acequire('ace/ext/modelist').modesByName
|
|
|
|
})
|
|
|
|
this.$root.$on('editorCodeblock/init', this.init)
|
2017-05-28 02:24:46 +00:00
|
|
|
}
|
2017-06-18 22:35:33 +00:00
|
|
|
}
|
2017-05-28 02:24:46 +00:00
|
|
|
</script>
|