api-client/components/ace-editor.vue

87 lines
1.5 KiB
Vue
Raw Normal View History

2019-11-09 19:28:05 +00:00
<template>
<pre ref="editor"></pre>
</template>
<script>
2019-11-12 04:52:50 +00:00
const DEFAULT_THEME = "dracula";
2019-11-12 04:52:50 +00:00
import ace from "ace-builds";
2019-11-09 19:28:05 +00:00
import "ace-builds/webpack-resolver";
export default {
props: {
value: {
type: String,
2019-11-12 04:52:50 +00:00
default: ""
2019-11-09 19:28:05 +00:00
},
theme: {
type: String,
required: false
2019-11-09 19:28:05 +00:00
},
lang: {
type: String,
2019-11-12 04:52:50 +00:00
default: "json"
2019-11-09 19:28:05 +00:00
},
options: {
type: Object,
default: {}
2019-11-09 19:28:05 +00:00
}
},
data() {
return {
editor: null,
2019-11-12 04:52:50 +00:00
cacheValue: ""
};
2019-11-09 19:28:05 +00:00
},
watch: {
value(value) {
2019-11-12 04:52:50 +00:00
if (value !== this.cacheValue) {
this.editor.session.setValue(value, 1);
this.cacheValue = value;
2019-11-09 19:28:05 +00:00
}
},
theme() {
2019-11-12 04:52:50 +00:00
this.editor.setTheme("ace/theme/" + this.defineTheme());
},
lang(value) {
2019-11-12 04:52:50 +00:00
this.editor.getSession().setMode("ace/mode/" + value);
},
options(value) {
this.editor.setOptions(value);
2019-11-09 19:28:05 +00:00
}
},
mounted() {
const editor = ace.edit(this.$refs.editor, {
2019-11-12 04:52:50 +00:00
theme: "ace/theme/" + this.defineTheme(),
2019-11-09 19:28:05 +00:00
mode: "ace/mode/" + this.lang,
...this.options
2019-11-12 04:52:50 +00:00
});
2019-11-09 19:28:05 +00:00
editor.setValue(this.value);
this.editor = editor;
this.cacheValue = this.value;
},
methods: {
defineTheme() {
2019-11-12 04:52:50 +00:00
if (this.theme) {
return this.theme;
} else {
2019-11-12 04:52:50 +00:00
return (
this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME
);
}
}
2019-11-09 19:28:05 +00:00
},
beforeDestroy() {
this.editor.destroy();
this.editor.container.remove();
}
2019-11-12 04:52:50 +00:00
};
2019-11-09 19:28:05 +00:00
</script>