api-client/components/ui/ace-editor.vue

150 lines
3.1 KiB
Vue
Raw Normal View History

2019-11-09 19:28:05 +00:00
<template>
<div class="show-if-initialized" :class="{ initialized }">
<pre ref="editor"></pre>
</div>
2019-11-09 19:28:05 +00:00
</template>
<style lang="scss">
.show-if-initialized {
2020-09-22 17:06:37 +00:00
@apply opacity-0;
&.initialized {
2020-09-22 17:06:37 +00:00
@apply opacity-100;
}
& > * {
2020-09-22 17:06:37 +00:00
@apply transition-none;
}
}
</style>
2019-11-09 19:28:05 +00:00
<script>
import ace from "ace-builds"
import "ace-builds/webpack-resolver"
import jsonParse from "~/helpers/jsonParse"
import debounce from "~/helpers/utils/debounce"
2019-11-09 19:28:05 +00:00
export default {
props: {
value: {
type: String,
default: "",
2019-11-09 19:28:05 +00:00
},
theme: {
type: String,
2020-02-24 18:44:50 +00:00
required: false,
2020-09-24 16:07:27 +00:00
default: null,
2019-11-09 19:28:05 +00:00
},
lang: {
type: String,
default: "json",
2019-11-09 19:28:05 +00:00
},
lint: {
type: Boolean,
default: true,
required: false,
},
options: {
type: Object,
2020-02-24 18:44:50 +00:00
default: {},
},
2019-11-09 19:28:05 +00:00
},
data() {
return {
initialized: false,
2019-11-09 19:28:05 +00:00
editor: null,
cacheValue: "",
2020-02-24 18:44:50 +00:00
}
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
2020-02-26 01:48:48 +00:00
if (this.lint) this.provideLinting(value)
2019-11-09 19:28:05 +00:00
}
},
theme() {
this.initialized = false
this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => {
this.initialized = true
})
})
},
lang(value) {
2020-10-21 06:50:32 +00:00
this.editor.getSession().setMode(`ace/mode/${value}`)
},
options(value) {
2020-02-24 18:44:50 +00:00
this.editor.setOptions(value)
},
2019-11-09 19:28:05 +00:00
},
mounted() {
const editor = ace.edit(this.$refs.editor, {
2020-01-30 18:48:20 +00:00
mode: `ace/mode/${this.lang}`,
2020-02-24 18:44:50 +00:00
...this.options,
})
2019-11-09 19:28:05 +00:00
// Set the theme and show the editor only after it's been set to prevent FOUC.
editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => {
this.initialized = true
})
})
if (this.value) editor.setValue(this.value, 1)
2019-11-09 19:28:05 +00:00
2020-02-24 18:44:50 +00:00
this.editor = editor
this.cacheValue = this.value
2019-12-10 16:28:08 +00:00
editor.on("change", () => {
const content = editor.getValue()
this.$emit("input", content)
this.cacheValue = content
2020-02-26 01:48:48 +00:00
if (this.lint) this.provideLinting(content)
})
// Disable linting, if lint prop is false
2020-02-26 01:48:48 +00:00
if (this.lint) this.provideLinting(this.value)
},
methods: {
defineTheme() {
2020-01-31 12:55:55 +00:00
if (this.theme) {
2020-02-24 18:44:50 +00:00
return this.theme
2020-01-31 12:55:55 +00:00
}
2020-09-24 02:52:54 +00:00
const strip = (str) => str.replace(/#/g, "").replace(/ /g, "").replace(/"/g, "")
return strip(
window.getComputedStyle(document.documentElement).getPropertyValue("--editor-theme")
)
2020-02-24 18:44:50 +00:00
},
2020-06-19 06:56:04 +00:00
provideLinting: debounce(function (code) {
if (this.lang === "json") {
try {
jsonParse(code)
this.editor.session.setAnnotations([])
} catch (e) {
const pos = this.editor.session.getDocument().indexToPosition(e.start, 0)
this.editor.session.setAnnotations([
{
row: pos.row,
column: pos.column,
text: e.message,
type: "error",
},
])
}
}
}, 2000),
2019-11-09 19:28:05 +00:00
},
destroyed() {
this.editor.destroy()
},
}
2019-11-09 19:28:05 +00:00
</script>