Skip to content

Instantly share code, notes, and snippets.

@hypeJunction
Created July 4, 2019 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hypeJunction/8b29ee90abcb20a62733e0579cffd3cb to your computer and use it in GitHub Desktop.
Save hypeJunction/8b29ee90abcb20a62733e0579cffd3cb to your computer and use it in GitHub Desktop.
<template>
<textarea ref="codemirror"></textarea>
</template>
<script>
export default {
model: {
event: 'input',
prop: 'value',
},
data () {
return {
instance: null,
};
},
props: {
value: {
type: String,
},
options: {
type: Object,
default () {
return {
tabSize: 2,
lineNumbers: true,
lineWrapping: false,
line: true,
};
},
},
modes: {
type: Array,
default () {
return ['htmlmixed'];
},
},
},
mounted () {
this.$nextTick(() => {
this.$codemirror.load({
modes: this.modes,
}).then((CodeMirror) => {
this.instance = CodeMirror.fromTextArea(this.$refs.codemirror, this.options);
this.instance.setValue(this.value);
this.instance.on('change', () => {
this.$emit('input', this.instance.getValue());
});
});
});
},
methods: {
setValue (value) {
if (this.instance) {
this.instance.setValue(value);
this.refresh();
}
},
refresh () {
this.$nextTick(() => {
this.instance.refresh();
});
},
},
watch: {
value () {
this.setValue(this.value);
},
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment