2021-02-21 19:22:03 +01:00
|
|
|
<template>
|
|
|
|
<div class="editor-wrapper">
|
|
|
|
<div
|
|
|
|
:id="`editor-${id}`"
|
|
|
|
class="editor"
|
|
|
|
:class="editorClass"
|
|
|
|
:style="{height: `${height}px`}"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import * as ace from 'ace-builds';
|
|
|
|
import 'ace-builds/webpack-resolver';
|
|
|
|
import { mapGetters } from 'vuex';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'BaseTextEditor',
|
|
|
|
props: {
|
|
|
|
value: String,
|
|
|
|
mode: { type: String, default: 'text' },
|
|
|
|
editorClass: { type: String, default: '' },
|
|
|
|
autoFocus: { type: Boolean, default: false },
|
|
|
|
readOnly: { type: Boolean, default: false },
|
2021-04-03 12:17:40 +02:00
|
|
|
showLineNumbers: { type: Boolean, default: true },
|
2021-02-21 19:22:03 +01:00
|
|
|
height: { type: Number, default: 200 }
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
editor: null,
|
|
|
|
id: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
editorTheme: 'settings/getEditorTheme',
|
2021-06-19 11:54:15 +02:00
|
|
|
editorFontSize: 'settings/getEditorFontSize',
|
2021-02-21 19:22:03 +01:00
|
|
|
autoComplete: 'settings/getAutoComplete',
|
|
|
|
lineWrap: 'settings/getLineWrap'
|
|
|
|
})
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
mode () {
|
|
|
|
if (this.editor)
|
|
|
|
this.editor.session.setMode(`ace/mode/${this.mode}`);
|
|
|
|
},
|
|
|
|
editorTheme () {
|
|
|
|
if (this.editor)
|
|
|
|
this.editor.setTheme(`ace/theme/${this.editorTheme}`);
|
|
|
|
},
|
2021-06-19 11:54:15 +02:00
|
|
|
editorFontSize () {
|
|
|
|
const sizes = {
|
|
|
|
small: '12px',
|
|
|
|
medium: '14px',
|
|
|
|
large: '16px'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.editor) {
|
|
|
|
this.editor.setOptions({
|
|
|
|
fontSize: sizes[this.editorFontSize]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-02-21 19:22:03 +01:00
|
|
|
autoComplete () {
|
|
|
|
if (this.editor) {
|
|
|
|
this.editor.setOptions({
|
|
|
|
enableLiveAutocompletion: this.autoComplete
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lineWrap () {
|
|
|
|
if (this.editor) {
|
|
|
|
this.editor.setOptions({
|
|
|
|
wrap: this.lineWrap
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created () {
|
|
|
|
this.id = this._uid;
|
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
this.editor = ace.edit(`editor-${this.id}`, {
|
|
|
|
mode: `ace/mode/${this.mode}`,
|
|
|
|
theme: `ace/theme/${this.editorTheme}`,
|
2021-04-09 19:31:41 +02:00
|
|
|
value: this.value || '',
|
2021-02-21 19:22:03 +01:00
|
|
|
fontSize: '14px',
|
|
|
|
printMargin: false,
|
2021-04-03 12:17:40 +02:00
|
|
|
readOnly: this.readOnly,
|
|
|
|
showLineNumbers: this.showLineNumbers,
|
|
|
|
showGutter: this.showLineNumbers
|
2021-02-21 19:22:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this.editor.setOptions({
|
|
|
|
enableBasicAutocompletion: false,
|
|
|
|
wrap: this.lineWrap,
|
|
|
|
enableSnippets: false,
|
|
|
|
enableLiveAutocompletion: false
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editor.session.on('change', () => {
|
|
|
|
const content = this.editor.getValue();
|
|
|
|
this.$emit('update:value', content);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.autoFocus) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.editor.focus();
|
|
|
|
this.editor.resize();
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.editor.resize();
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.editor-wrapper {
|
|
|
|
.editor {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.ace_.mdi {
|
|
|
|
display: inline-block;
|
|
|
|
width: 17px;
|
|
|
|
}
|
|
|
|
</style>
|